From 1cb6d314f1694e7b70351bc41139cce66676f378 Mon Sep 17 00:00:00 2001 From: Roman Date: Wed, 15 Apr 2026 14:03:11 +0800 Subject: [PATCH] fix: tests - make_empty_state - formatting --- fuzz_props/src/generators.rs | 67 +++++++++++++++++------------------- fuzz_props/src/invariants.rs | 34 +++++++++--------- 2 files changed, 47 insertions(+), 54 deletions(-) diff --git a/fuzz_props/src/generators.rs b/fuzz_props/src/generators.rs index c5452a8..84f8015 100644 --- a/fuzz_props/src/generators.rs +++ b/fuzz_props/src/generators.rs @@ -12,8 +12,7 @@ pub fn arbitrary_transaction(u: &mut Unstructured<'_>) -> arbitrary::Result::arbitrary(u)?; - borsh::from_slice::(&raw) - .map_err(|_| arbitrary::Error::IncorrectFormat) + borsh::from_slice::(&raw).map_err(|_| arbitrary::Error::IncorrectFormat) } else { // Generate a minimal empty public tx using known test keys let signing_key = PrivateKey::try_new([u8::arbitrary(u)?; 32]) @@ -27,9 +26,9 @@ pub fn arbitrary_transaction(u: &mut Unstructured<'_>) -> arbitrary::Result impl Strategy { let accounts = test_accounts(); proptest::collection::vec(arb_native_transfer_tx(accounts), 0..8).prop_map(|txs| { HashableBlockData { - block_id: 1, + block_id: 1, prev_block_hash: common::HashType([0; 32]), - timestamp: 0, - transactions: txs, + timestamp: 0, + transactions: txs, } }) } @@ -121,18 +120,16 @@ prop_compose! { /// fuzz target. pub fn arb_duplicate_tx_sequence() -> impl Strategy> { let accounts = test_accounts(); - proptest::collection::vec(arb_native_transfer_tx(accounts), 1..5_usize).prop_flat_map( - |txs| { - // Build a sequence that: original | duplicates | reversed - let duped: Vec = txs - .iter() - .cloned() - .chain(txs.iter().cloned()) // append exact duplicates - .chain(txs.iter().rev().cloned()) // append reversed order - .collect(); - Just(duped) - }, - ) + proptest::collection::vec(arb_native_transfer_tx(accounts), 1..5_usize).prop_flat_map(|txs| { + // Build a sequence that: original | duplicates | reversed + let duped: Vec = txs + .iter() + .cloned() + .chain(txs.iter().cloned()) // append exact duplicates + .chain(txs.iter().rev().cloned()) // append reversed order + .collect(); + Just(duped) + }) } // ── IS-5: Pathological sequences intended to violate protocol rules ─────────── @@ -145,21 +142,19 @@ pub fn arb_duplicate_tx_sequence() -> impl Strategy pub fn arb_pathological_sequence() -> impl Strategy> { let accounts = test_accounts(); let n = accounts.len(); - proptest::collection::vec( - (0..n, 0..n, 0u128..5u128, any::()), - 1..8_usize, + proptest::collection::vec((0..n, 0..n, 0u128..5u128, any::()), 1..8_usize).prop_map( + move |params| { + params + .into_iter() + .map(|(from_idx, to_idx, nonce, zero_amount)| { + let (from_id, from_key) = &accounts[from_idx]; + let (to_id, _) = &accounts[to_idx]; + let amount = if zero_amount { 0u128 } else { u128::MAX }; // 0 or overflow + common::test_utils::create_transaction_native_token_transfer( + *from_id, nonce, *to_id, amount, from_key, + ) + }) + .collect() + }, ) - .prop_map(move |params| { - params - .into_iter() - .map(|(from_idx, to_idx, nonce, zero_amount)| { - let (from_id, from_key) = &accounts[from_idx]; - let (to_id, _) = &accounts[to_idx]; - let amount = if zero_amount { 0u128 } else { u128::MAX }; // 0 or overflow - common::test_utils::create_transaction_native_token_transfer( - *from_id, nonce, *to_id, amount, from_key, - ) - }) - .collect() - }) } diff --git a/fuzz_props/src/invariants.rs b/fuzz_props/src/invariants.rs index 19fa6d6..c2ae85c 100644 --- a/fuzz_props/src/invariants.rs +++ b/fuzz_props/src/invariants.rs @@ -14,17 +14,17 @@ impl BalanceSnapshot { /// Shared context threaded through every invariant check. pub struct InvariantCtx<'a> { - pub state_before: &'a V03State, - pub state_after: &'a V03State, - pub tx: &'a NSSATransaction, - pub result: &'a Result<(), NssaError>, + pub state_before: &'a V03State, + pub state_after: &'a V03State, + pub tx: &'a NSSATransaction, + pub result: &'a Result<(), NssaError>, pub balances_before: BalanceSnapshot, } #[derive(Debug)] pub struct InvariantViolation { pub invariant: &'static str, - pub message: String, + pub message: String, } pub trait ProtocolInvariant { @@ -70,10 +70,7 @@ impl ProtocolInvariant for ReplayRejection { /// Run every registered invariant and panic with a structured message on first violation. pub fn assert_invariants(ctx: &InvariantCtx<'_>) { - let invariants: &[&dyn ProtocolInvariant] = &[ - &StateIsolationOnFailure, - &ReplayRejection, - ]; + let invariants: &[&dyn ProtocolInvariant] = &[&StateIsolationOnFailure, &ReplayRejection]; for inv in invariants { if let Some(violation) = inv.check(ctx) { panic!( @@ -91,7 +88,8 @@ mod tests { use nssa::V03State; fn make_empty_state() -> V03State { - V03State::new_with_genesis_accounts(&[], &[]) + //V03State::new_with_genesis_accounts(&[], &[]) + V03State::new_with_genesis_accounts(&[], vec![], 0) } fn make_empty_snapshot() -> BalanceSnapshot { @@ -104,10 +102,10 @@ mod tests { let tx = common::test_utils::produce_dummy_empty_transaction(); let result: Result<(), NssaError> = Err(NssaError::InvalidInput("test".to_owned())); let ctx = InvariantCtx { - state_before: &state, - state_after: &state, - tx: &tx, - result: &result, + state_before: &state, + state_after: &state, + tx: &tx, + result: &result, balances_before: make_empty_snapshot(), }; // Should not panic — invariant check is a placeholder @@ -120,10 +118,10 @@ mod tests { let tx = common::test_utils::produce_dummy_empty_transaction(); let result: Result<(), NssaError> = Ok(()); let ctx = InvariantCtx { - state_before: &state, - state_after: &state, - tx: &tx, - result: &result, + state_before: &state, + state_after: &state, + tx: &tx, + result: &result, balances_before: make_empty_snapshot(), }; assert_invariants(&ctx);