Skip to content

Commit

Permalink
Move helper function to the test::helpers module to reuse in many tes…
Browse files Browse the repository at this point in the history
…t files
  • Loading branch information
welf committed Jan 2, 2025
1 parent b07ed77 commit e939092
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 41 deletions.
46 changes: 45 additions & 1 deletion wallet-core/src/notes/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,48 @@
//
// Copyright (c) DUSK NETWORK. All rights reserved.

mod property_tests;
#[cfg(test)]
pub(crate) mod helpers {
use dusk_core::transfer::phoenix::{Note, PublicKey as PhoenixPublicKey};
use dusk_core::JubJubScalar;
use ff::Field;
use rand::{CryptoRng, RngCore};

/// Helper function to generate test notes with specific parameters.
/// Used across different test modules to ensure consistent note generation.
pub(crate) fn gen_note<R>(
rng: &mut R,
owner_pk: &PhoenixPublicKey,
value: u64,
is_obfuscated: bool,
) -> Note
where
R: RngCore + CryptoRng,
{
let value_blinder = JubJubScalar::random(&mut *rng);
let blinder1 = JubJubScalar::random(&mut *rng);
let blinder2 = JubJubScalar::random(&mut *rng);
let sender_blinder = [blinder1, blinder2];

if is_obfuscated {
Note::obfuscated(
&mut *rng,
owner_pk,
owner_pk,
value,
value_blinder,
sender_blinder,
)
} else {
Note::transparent(
&mut *rng,
owner_pk,
owner_pk,
value,
sender_blinder,
)
}
}
}

pub(crate) mod property_tests;
44 changes: 4 additions & 40 deletions wallet-core/src/notes/tests/property_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,56 +6,20 @@

#[cfg(test)]
mod tests {
use super::super::helpers::gen_note;
use dusk_core::transfer::phoenix::{
Note, NoteLeaf, PublicKey as PhoenixPublicKey,
SecretKey as PhoenixSecretKey, ViewKey as PhoenixViewKey,
NoteLeaf, PublicKey as PhoenixPublicKey, SecretKey as PhoenixSecretKey,
ViewKey as PhoenixViewKey,
};
use dusk_core::JubJubScalar;
use ff::Field;
use proptest::collection::vec;
use proptest::prelude::*;
use rand::{CryptoRng, RngCore, SeedableRng};
use rand::SeedableRng;
use rand_chacha::ChaCha12Rng;

use crate::notes::owned::NoteList;
use crate::notes::MAX_INPUT_NOTES;
use crate::{phoenix_balance, pick_notes};

// Helper function to generate arbitrary valid notes for testing
fn gen_note<R>(
rng: &mut R,
owner_pk: &PhoenixPublicKey,
value: u64,
is_obfuscated: bool,
) -> Note
where
R: RngCore + CryptoRng,
{
let value_blinder = JubJubScalar::random(&mut *rng);
let blinder1 = JubJubScalar::random(&mut *rng);
let blinder2 = JubJubScalar::random(&mut *rng);
let sender_blinder = [blinder1, blinder2];

if is_obfuscated {
Note::obfuscated(
&mut *rng,
owner_pk,
owner_pk,
value,
value_blinder,
sender_blinder,
)
} else {
Note::transparent(
&mut *rng,
owner_pk,
owner_pk,
value,
sender_blinder,
)
}
}

proptest! {
/// Tests the balance calculation functionality ensuring that:
/// 1. Total balance correctly represents sum of all note values
Expand Down

0 comments on commit e939092

Please sign in to comment.