Skip to content

Commit

Permalink
Merge pull request #952 from fluidvanadium/expand_mocks_r3
Browse files Browse the repository at this point in the history
Expand Mocks R3
  • Loading branch information
zancas authored Apr 17, 2024
2 parents b1b9f27 + 59fc31f commit 31c067a
Show file tree
Hide file tree
Showing 13 changed files with 437 additions and 152 deletions.
2 changes: 1 addition & 1 deletion darkside-tests/src/constants.rs

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions zingolib/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `wallet::keys::is_transparent_address` fn
- `wallet::notes::NoteRecordIdentifier` struct
- `utils` mod
- `utils::txid_from_hex_encoded_str` fn
- `lightclient::LightClient`:
- `do_propose` behind "zip317" feature
- `do_send_proposal` behind "zip317" feature
- `commands`:
- `ProposeCommand` struct and methods behind "zip317" feature
- `QuickSendCommand` struct and methods behind "zip317" feature
- `test_framework` mod

- `test_framework` mod behind "test-features" feature

### Changed

Expand Down
2 changes: 1 addition & 1 deletion zingolib/src/blaze/block_management_reorg_detection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -744,7 +744,7 @@ pub fn update_tree_with_compact_transaction<D: DomainWalletExt>(
}

#[cfg(test)]
mod test {
mod tests {
use crate::{blaze::test_utils::FakeCompactBlock, wallet::data::BlockData};
use orchard::tree::MerkleHashOrchard;
use zcash_primitives::block::BlockHash;
Expand Down
2 changes: 1 addition & 1 deletion zingolib/src/blaze/fetch_taddr_transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ impl FetchTaddrTransactions {
// TODO: Reexamine this test, which relies on explicit creation of transparent spend auths
/*
#[cfg(test)]
mod test {
mod tests {
use futures::future::join_all;
use rand::Rng;
use std::sync::Arc;
Expand Down
49 changes: 0 additions & 49 deletions zingolib/src/test_framework.rs
Original file line number Diff line number Diff line change
@@ -1,51 +1,2 @@
use zcash_primitives::transaction::TxId;

use crate::wallet::notes::TransparentNote;
pub(crate) mod macros;
pub mod mocks;

#[allow(dead_code)]
pub(crate) fn create_empty_txid_and_tnote() -> (zcash_primitives::transaction::TxId, TransparentNote)
{
// A single transparent note makes is_incoming_trsaction true.
let txid = zcash_primitives::transaction::TxId::from_bytes([0u8; 32]);
(
txid,
mocks::TransparentNoteBuilder::new()
.address("t".to_string())
.spent(Some((txid, 3)))
.build(),
)
}
#[allow(dead_code)]
pub(crate) fn create_transaction_record_with_one_tnote(
txid: TxId,
transparent_note: TransparentNote,
) -> crate::wallet::transaction_record::TransactionRecord {
// A single transparent note makes is_incoming_trsaction true.
let mut transaction_record = crate::wallet::transaction_record::TransactionRecord::new(
zingo_status::confirmation_status::ConfirmationStatus::Confirmed(
zcash_primitives::consensus::BlockHeight::from_u32(5),
),
1705077003,
&txid,
);
transaction_record.transparent_notes.push(transparent_note);
transaction_record
}
#[allow(dead_code)]
pub(crate) fn default_trecord_with_one_tnote(
) -> crate::wallet::transaction_record::TransactionRecord {
let (txid, transparent_note) = create_empty_txid_and_tnote();
create_transaction_record_with_one_tnote(txid, transparent_note)
}
#[allow(dead_code)]
pub(crate) fn create_note_record_id() -> crate::wallet::notes::NoteRecordIdentifier {
let (txid, _tnote) = create_empty_txid_and_tnote();
let index = 5u32;
crate::wallet::notes::NoteRecordIdentifier {
txid,
pool: zcash_client_backend::PoolType::Transparent,
index,
}
}
170 changes: 114 additions & 56 deletions zingolib/src/test_framework/mocks.rs
Original file line number Diff line number Diff line change
@@ -1,67 +1,125 @@
//! Tools to facilitate mocks for testing

use zcash_primitives::transaction::TxId;

use crate::wallet::notes::TransparentNote;

pub use proposal::{ProposalBuilder, StepBuilder};

macro_rules! build_method {
($name:ident, $localtype:ty) => {
#[doc = "Set the $name field of the builder."]
pub fn $name(mut self, $name: $localtype) -> Self {
self.$name = Some($name);
self
}
};
}
pub(crate) use build_method;
pub use proposal::{ProposalBuilder, StepBuilder};
pub use sapling_note::LRZSaplingNoteBuilder;

pub struct TransparentNoteBuilder {
address: Option<String>,
txid: Option<TxId>,
output_index: Option<u64>,
script: Option<Vec<u8>>,
value: Option<u64>,
spent: Option<Option<(TxId, u32)>>,
unconfirmed_spent: Option<Option<(TxId, u32)>>,
fn zaddr_from_seed(
seed: [u8; 32],
) -> (
ExtendedSpendingKey,
PreparedIncomingViewingKey,
PaymentAddress,
) {
let extsk = ExtendedSpendingKey::master(&seed);
let dfvk = extsk.to_diversifiable_full_viewing_key();
let fvk = dfvk;
let (_, addr) = fvk.default_address();

(
extsk,
PreparedIncomingViewingKey::new(&fvk.fvk().vk.ivk()),
addr,
)
}
#[allow(dead_code)] //TODO: fix this gross hack that I tossed in to silence the language-analyzer false positive
impl TransparentNoteBuilder {
pub fn new() -> Self {
Self::default()
}
// Methods to set each field
build_method!(address, String);
build_method!(txid, TxId);
build_method!(output_index, u64);
build_method!(script, Vec<u8>);
build_method!(value, u64);
build_method!(spent, Option<(TxId, u32)>);
build_method!(unconfirmed_spent, Option<(TxId, u32)>);

// Build method
pub fn build(self) -> TransparentNote {
TransparentNote::from_parts(
self.address.unwrap(),
self.txid.unwrap(),
self.output_index.unwrap(),
self.script.unwrap(),
self.value.unwrap(),
self.spent.unwrap(),
self.unconfirmed_spent.unwrap(),
)
}

pub fn default_txid() -> zcash_primitives::transaction::TxId {
zcash_primitives::transaction::TxId::from_bytes([0u8; 32])
}
pub fn default_zaddr() -> (
ExtendedSpendingKey,
PreparedIncomingViewingKey,
PaymentAddress,
) {
zaddr_from_seed([0u8; 32])
}

use rand::{rngs::OsRng, Rng};
use sapling_crypto::{
note_encryption::PreparedIncomingViewingKey, zip32::ExtendedSpendingKey, PaymentAddress,
};

pub fn random_txid() -> zcash_primitives::transaction::TxId {
let mut rng = OsRng;
let mut seed = [0u8; 32];
rng.fill(&mut seed);
zcash_primitives::transaction::TxId::from_bytes(seed)
}
pub fn random_zaddr() -> (
ExtendedSpendingKey,
PreparedIncomingViewingKey,
PaymentAddress,
) {
let mut rng = OsRng;
let mut seed = [0u8; 32];
rng.fill(&mut seed);

zaddr_from_seed(seed)
}

// Sapling Note Mocker
mod sapling_note {

use sapling_crypto::value::NoteValue;
use sapling_crypto::Note;
use sapling_crypto::PaymentAddress;
use sapling_crypto::Rseed;

use super::default_zaddr;

/// A struct to build a mock sapling_crypto::Note from scratch.
/// Distinguish [`sapling_crypto::Note`] from [`crate::wallet::notes::SaplingNote`]. The latter wraps the former with some other attributes.
pub struct LRZSaplingNoteBuilder {
recipient: Option<PaymentAddress>,
value: Option<NoteValue>,
rseed: Option<Rseed>,
}

impl LRZSaplingNoteBuilder {
/// Instantiate an empty builder.
pub fn new() -> Self {
LRZSaplingNoteBuilder {
recipient: None,
value: None,
rseed: None,
}
}

// Methods to set each field
build_method!(recipient, PaymentAddress);
build_method!(value, NoteValue);
build_method!(rseed, Rseed);

impl Default for TransparentNoteBuilder {
fn default() -> Self {
TransparentNoteBuilder {
address: Some("default_address".to_string()),
txid: Some(TxId::from_bytes([0u8; 32])),
output_index: Some(0),
script: Some(vec![]),
value: Some(0),
spent: Some(None),
unconfirmed_spent: Some(None),
pub fn randomize_recipient(self) -> Self {
let (_, _, address) = super::random_zaddr();
self.recipient(address)
}

/// Build the note.
pub fn build(self) -> Note {
Note::from_parts(
self.recipient.unwrap(),
self.value.unwrap(),
self.rseed.unwrap(),
)
}
}
impl Default for LRZSaplingNoteBuilder {
fn default() -> Self {
let (_, _, address) = default_zaddr();
Self::new()
.recipient(address)
.value(NoteValue::from_raw(1000000))
.rseed(Rseed::AfterZip212([7; 32]))
}
}
}
Expand All @@ -74,7 +132,7 @@ pub mod proposal {
use incrementalmerkletree::Position;
use nonempty::NonEmpty;
use sapling_crypto::value::NoteValue;
use sapling_crypto::zip32::ExtendedSpendingKey;

use sapling_crypto::Rseed;
use zcash_client_backend::fees::TransactionBalance;
use zcash_client_backend::proposal::{Proposal, ShieldedInputs, Step, StepOutput};
Expand All @@ -83,11 +141,13 @@ pub mod proposal {
use zcash_client_backend::PoolType;
use zcash_primitives::consensus::BlockHeight;
use zcash_primitives::transaction::{
components::amount::NonNegativeAmount, fees::zip317::FeeRule, TxId,
components::amount::NonNegativeAmount, fees::zip317::FeeRule,
};

use crate::wallet::notes::NoteRecordIdentifier;

use super::{default_txid, default_zaddr};

/// Provides a builder for constructing a mock [`zcash_client_backend::proposal::Proposal`].
///
/// # Examples
Expand Down Expand Up @@ -221,10 +281,8 @@ pub mod proposal {
impl Default for StepBuilder {
/// Constructs a new [`StepBuilder`] where all fields are preset to default values.
fn default() -> Self {
let txid = TxId::from_bytes([0u8; 32]);
let seed = [0u8; 32];
let dfvk = ExtendedSpendingKey::master(&seed).to_diversifiable_full_viewing_key();
let (_, address) = dfvk.default_address();
let txid = default_txid();
let (_, _, address) = default_zaddr();
let note = sapling_crypto::Note::from_parts(
address,
NoteValue::from_raw(20_000),
Expand Down
2 changes: 1 addition & 1 deletion zingolib/src/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ fn decode_orchard_spending_key(
}

#[cfg(test)]
mod test {
mod tests {
use incrementalmerkletree::frontier::CommitmentTree;
use orchard::tree::MerkleHashOrchard;

Expand Down
7 changes: 0 additions & 7 deletions zingolib/src/wallet/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -699,13 +699,6 @@ pub mod summaries {

pub use crate::wallet::transaction_record::TransactionRecord;

#[test]
#[cfg(feature = "test-features")]
fn single_transparent_note_makes_is_incoming_true() {
// A single transparent note makes is_incoming_trsaction true.
let transaction_record = crate::test_framework::default_trecord_with_one_tnote();
assert!(transaction_record.is_incoming_transaction());
}
#[derive(Debug)]
pub struct SpendableSaplingNote {
pub transaction_id: TxId,
Expand Down
36 changes: 8 additions & 28 deletions zingolib/src/wallet/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,37 +208,17 @@ impl Message {
}

#[cfg(test)]
pub mod tests {
mod tests {
use ff::Field;
use sapling_crypto::zip32::ExtendedSpendingKey;
use zcash_note_encryption::OUT_PLAINTEXT_SIZE;

use super::*;

fn get_random_zaddr() -> (
ExtendedSpendingKey,
PreparedIncomingViewingKey,
PaymentAddress,
) {
let mut rng = OsRng;
let mut seed = [0u8; 32];
rng.fill(&mut seed);

let extsk = ExtendedSpendingKey::master(&seed);
let dfvk = extsk.to_diversifiable_full_viewing_key();
let fvk = dfvk;
let (_, addr) = fvk.default_address();
use crate::test_framework::mocks::random_zaddr;

(
extsk,
PreparedIncomingViewingKey::new(&fvk.fvk().vk.ivk()),
addr,
)
}
use super::*;

#[test]
fn test_encrpyt_decrypt() {
let (_, ivk, to) = get_random_zaddr();
let (_, ivk, to) = random_zaddr();

let msg = Memo::from_bytes("Hello World with some value!".to_string().as_bytes()).unwrap();

Expand All @@ -264,8 +244,8 @@ pub mod tests {

#[test]
fn test_bad_inputs() {
let (_, ivk1, to1) = get_random_zaddr();
let (_, ivk2, _) = get_random_zaddr();
let (_, ivk1, to1) = random_zaddr();
let (_, ivk2, _) = random_zaddr();

let msg = Memo::from_bytes("Hello World with some value!".to_string().as_bytes()).unwrap();

Expand All @@ -286,7 +266,7 @@ pub mod tests {
let magic_len = Message::magic_word().len();
let prefix_len = magic_len + 1; // version byte

let (_, ivk, to) = get_random_zaddr();
let (_, ivk, to) = random_zaddr();
let msg_str = "Hello World with some value!";
let msg = Memo::from_bytes(msg_str.to_string().as_bytes()).unwrap();

Expand Down Expand Up @@ -373,7 +353,7 @@ pub mod tests {

#[test]
fn test_individual_bytes() {
let (_, ivk, to) = get_random_zaddr();
let (_, ivk, to) = random_zaddr();
let msg_str = "Hello World with some value!";
let msg = Memo::from_bytes(msg_str.to_string().as_bytes()).unwrap();

Expand Down
Loading

0 comments on commit 31c067a

Please sign in to comment.