Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

created OrchardCryptoNoteBuilder and uses last-minute building #993

Merged
merged 3 commits into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
132 changes: 91 additions & 41 deletions zingolib/src/test_framework/mocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,55 +156,105 @@ pub mod orchard_note {
keys::{FullViewingKey, SpendingKey},
note::{RandomSeed, Rho},
value::NoteValue,
Note,
Address, Note,
};
use rand::{rngs::OsRng, Rng};
use zip32::Scope;

/// mocks a random orchard note
pub fn mock_random_orchard_note() -> Note {
let mut rng = OsRng;

let sk = {
loop {
let mut bytes = [0; 32];
rng.fill(&mut bytes);
let sk = SpendingKey::from_bytes(bytes);
if sk.is_some().into() {
break sk.unwrap();
}
/// A struct to build a mock orchard::Note from scratch.
/// Distinguish [`orchard::Note`] from [`crate::wallet::notes::OrchardNote`]. The latter wraps the former with some other attributes.
#[derive(Clone, Copy)]
pub struct OrchardCryptoNoteBuilder {
recipient: Option<Address>,
value: Option<NoteValue>,
rho: Option<Rho>,
random_seed: Option<RandomSeed>,
}

impl OrchardCryptoNoteBuilder {
/// Instantiate an empty builder.
pub fn new() -> Self {
OrchardCryptoNoteBuilder {
recipient: None,
value: None,
rho: None,
random_seed: None,
}
};
let fvk: FullViewingKey = (&sk).into();
let recipient = fvk.address_at(0u32, Scope::External);

let value = NoteValue::from_raw(800000);
let rho = {
loop {
let mut bytes = [0u8; 32];
rng.fill(&mut bytes);
let rho = Rho::from_bytes(&bytes);
if rho.is_some().into() {
break rho.unwrap();
}

// Methods to set each field
build_method!(recipient, Address);
build_method!(value, NoteValue);
build_method!(rho, Rho);
build_method!(random_seed, RandomSeed);

/// selects a random recipient address for the orchard note
pub fn randomize_recipient(self) -> Self {
let mut rng = OsRng;

let sk = {
loop {
let mut bytes = [0; 32];
rng.fill(&mut bytes);
let sk = SpendingKey::from_bytes(bytes);
if sk.is_some().into() {
break sk.unwrap();
}
}
}
};
let random_seed = {
loop {
let mut bytes = [0; 32];
rng.fill(&mut bytes);
let random_seed = RandomSeed::from_bytes(bytes, &rho);
if random_seed.is_some().into() {
break random_seed.unwrap();
};
let fvk: FullViewingKey = (&sk).into();
let recipient = fvk.address_at(0u32, Scope::External);

self.recipient(recipient)
}

/// selects a random nullifier for the orchard note
pub fn randomize_rho_and_rseed(self) -> Self {
let mut rng = OsRng;

let rho = {
loop {
let mut bytes = [0u8; 32];
rng.fill(&mut bytes);
let rho = Rho::from_bytes(&bytes);
if rho.is_some().into() {
break rho.unwrap();
}
}
}
};
};

let random_seed = {
loop {
let mut bytes = [0; 32];
rng.fill(&mut bytes);
let random_seed = RandomSeed::from_bytes(bytes, &rho);
if random_seed.is_some().into() {
break random_seed.unwrap();
}
}
};

loop {
let note = Note::from_parts(recipient, value, rho, random_seed);
if note.is_some().into() {
break note.unwrap();
}
self.rho(rho).random_seed(random_seed)
}

/// Build the note.
pub fn build(self) -> Note {
Note::from_parts(
self.recipient.unwrap(),
self.value.unwrap(),
self.rho.unwrap(),
self.random_seed.unwrap(),
)
.unwrap()
}
}
/// mocks a random orchard note
impl Default for OrchardCryptoNoteBuilder {
fn default() -> Self {
Self::new()
.randomize_recipient()
.randomize_rho_and_rseed()
.value(NoteValue::from_raw(800000))
}
}
}
Expand Down
22 changes: 16 additions & 6 deletions zingolib/src/wallet/notes/orchard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,17 +178,20 @@ impl ShieldedNoteInterface for OrchardNote {
pub mod mocks {
//! Mock version of the struct for testing
use incrementalmerkletree::Position;
use orchard::{keys::Diversifier, note::Nullifier, Note};
use orchard::{keys::Diversifier, note::Nullifier, value::NoteValue};
use zcash_primitives::{memo::Memo, transaction::TxId};

use crate::{test_framework::mocks::build_method, wallet::notes::ShieldedNoteInterface};
use crate::{
test_framework::mocks::{build_method, orchard_note::OrchardCryptoNoteBuilder},
wallet::notes::ShieldedNoteInterface,
};

use super::OrchardNote;

/// to create a mock SaplingNote
pub(crate) struct OrchardNoteBuilder {
diversifier: Option<Diversifier>,
note: Option<Note>,
note: Option<OrchardCryptoNoteBuilder>,
witnessed_position: Option<Option<Position>>,
output_index: Option<Option<u32>>,
nullifier: Option<Option<Nullifier>>,
Expand Down Expand Up @@ -219,7 +222,7 @@ pub mod mocks {

// Methods to set each field
build_method!(diversifier, Diversifier);
build_method!(note, Note);
build_method!(note, OrchardCryptoNoteBuilder);
build_method!(witnessed_position, Option<Position>);
build_method!(output_index, Option<u32>);
build_method!(nullifier, Option<Nullifier>);
Expand All @@ -232,12 +235,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) -> OrchardNote {
OrchardNote::from_parts(
self.diversifier.unwrap(),
self.note.unwrap(),
self.note.unwrap().build(),
self.witnessed_position.unwrap(),
self.nullifier.unwrap(),
self.spent.unwrap(),
Expand All @@ -254,7 +264,7 @@ pub mod mocks {
fn default() -> Self {
OrchardNoteBuilder::new()
.diversifier(Diversifier::from_bytes([0; 11]))
.note(crate::test_framework::mocks::orchard_note::mock_random_orchard_note())
.note(OrchardCryptoNoteBuilder::default())
.witnessed_position(Some(Position::from(0)))
.output_index(Some(0))
.nullifier(Some(Nullifier::from_bytes(&[0u8; 32]).unwrap()))
Expand Down
Loading