From e9390924c9b5fc1462859b2b710de92f7f3557a7 Mon Sep 17 00:00:00 2001 From: Arthur Welf Date: Thu, 2 Jan 2025 01:35:06 +0100 Subject: [PATCH] Move helper function to the test::helpers module to reuse in many test files --- wallet-core/src/notes/tests/mod.rs | 46 ++++++++++++++++++- wallet-core/src/notes/tests/property_tests.rs | 44 ++---------------- 2 files changed, 49 insertions(+), 41 deletions(-) diff --git a/wallet-core/src/notes/tests/mod.rs b/wallet-core/src/notes/tests/mod.rs index 7178d6a4b..8836118f5 100644 --- a/wallet-core/src/notes/tests/mod.rs +++ b/wallet-core/src/notes/tests/mod.rs @@ -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( + 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; diff --git a/wallet-core/src/notes/tests/property_tests.rs b/wallet-core/src/notes/tests/property_tests.rs index db28bf978..0ef299d8a 100644 --- a/wallet-core/src/notes/tests/property_tests.rs +++ b/wallet-core/src/notes/tests/property_tests.rs @@ -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( - 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