From 5b9869f2f1e6bad1b3349906bc509ab8f60a1bff Mon Sep 17 00:00:00 2001 From: fluidvanadium Date: Wed, 24 Apr 2024 17:04:26 +0000 Subject: [PATCH] upgraded sapling note builder to build later and allow value changes --- zingolib/src/test_framework/mocks.rs | 1 + zingolib/src/wallet/notes/sapling.rs | 18 +++++++++++++----- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/zingolib/src/test_framework/mocks.rs b/zingolib/src/test_framework/mocks.rs index c1df3fe097..ca07355212 100644 --- a/zingolib/src/test_framework/mocks.rs +++ b/zingolib/src/test_framework/mocks.rs @@ -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, value: Option, diff --git a/zingolib/src/wallet/notes/sapling.rs b/zingolib/src/wallet/notes/sapling.rs index a7342c5e0e..f8d099e2ac 100644 --- a/zingolib/src/wallet/notes/sapling.rs +++ b/zingolib/src/wallet/notes/sapling.rs @@ -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}, }; @@ -211,7 +212,7 @@ pub mod mocks { /// to create a mock SaplingNote pub(crate) struct SaplingNoteBuilder { diversifier: Option, - note: Option, + note: Option, witnessed_position: Option>, output_index: Option>, nullifier: Option>, @@ -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); build_method!(output_index, Option); build_method!(nullifier, Option); @@ -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(), @@ -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])))