Skip to content

Commit

Permalink
Merge pull request #990 from fluidvanadium/patient_sapling_note_builder
Browse files Browse the repository at this point in the history
upgraded sapling note builder to build later and allow value changes
  • Loading branch information
AloeareV authored Apr 24, 2024
2 parents abc0790 + 5b9869f commit a577073
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
1 change: 1 addition & 0 deletions zingolib/src/test_framework/mocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ mod sapling_crypto_note {

/// 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.
#[derive(Clone, Copy)]
pub struct SaplingCryptoNoteBuilder {
recipient: Option<PaymentAddress>,
value: Option<NoteValue>,
Expand Down
18 changes: 13 additions & 5 deletions zingolib/src/wallet/notes/sapling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,10 +199,11 @@ impl ShieldedNoteInterface for SaplingNote {
pub mod mocks {
//! Mock version of the struct for testing
use incrementalmerkletree::Position;
use sapling_crypto::value::NoteValue;
use zcash_primitives::{memo::Memo, transaction::TxId};

use crate::{
test_framework::mocks::build_method,
test_framework::mocks::{build_method, SaplingCryptoNoteBuilder},
wallet::{notes::ShieldedNoteInterface, traits::FromBytes},
};

Expand All @@ -211,7 +212,7 @@ pub mod mocks {
/// to create a mock SaplingNote
pub(crate) struct SaplingNoteBuilder {
diversifier: Option<sapling_crypto::Diversifier>,
note: Option<sapling_crypto::Note>,
note: Option<SaplingCryptoNoteBuilder>,
witnessed_position: Option<Option<Position>>,
output_index: Option<Option<u32>>,
nullifier: Option<Option<sapling_crypto::Nullifier>>,
Expand Down Expand Up @@ -242,7 +243,7 @@ pub mod mocks {

// Methods to set each field
build_method!(diversifier, sapling_crypto::Diversifier);
build_method!(note, sapling_crypto::Note);
build_method!(note, SaplingCryptoNoteBuilder);
build_method!(witnessed_position, Option<Position>);
build_method!(output_index, Option<u32>);
build_method!(nullifier, Option<sapling_crypto::Nullifier>);
Expand All @@ -255,12 +256,19 @@ pub mod mocks {
self
}
build_method!(have_spending_key, bool);
pub fn value(mut self, value: u64) -> Self {
self.note
.as_mut()
.unwrap()
.value(NoteValue::from_raw(value));
self
}

/// builds a mock SaplingNote after all pieces are supplied
pub fn build(self) -> SaplingNote {
SaplingNote::from_parts(
self.diversifier.unwrap(),
self.note.unwrap(),
self.note.unwrap().build(),
self.witnessed_position.unwrap(),
self.nullifier.unwrap(),
self.spent.unwrap(),
Expand All @@ -277,7 +285,7 @@ pub mod mocks {
fn default() -> Self {
SaplingNoteBuilder::new()
.diversifier(sapling_crypto::Diversifier([0; 11]))
.note(crate::test_framework::mocks::SaplingCryptoNoteBuilder::default().build())
.note(crate::test_framework::mocks::SaplingCryptoNoteBuilder::default())
.witnessed_position(Some(Position::from(0)))
.output_index(Some(0))
.nullifier(Some(sapling_crypto::Nullifier::from_bytes([0; 32])))
Expand Down

0 comments on commit a577073

Please sign in to comment.