fix: tests - make_empty_state

- formatting
This commit is contained in:
Roman 2026-04-15 14:03:11 +08:00
parent 570edbba90
commit 1cb6d314f1
No known key found for this signature in database
GPG Key ID: 583BDF43C238B83E
2 changed files with 47 additions and 54 deletions

View File

@ -12,8 +12,7 @@ pub fn arbitrary_transaction(u: &mut Unstructured<'_>) -> arbitrary::Result<NSSA
// Prefer structured generation; raw decode as fallback
if bool::arbitrary(u)? {
let raw = Vec::<u8>::arbitrary(u)?;
borsh::from_slice::<NSSATransaction>(&raw)
.map_err(|_| arbitrary::Error::IncorrectFormat)
borsh::from_slice::<NSSATransaction>(&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<NSSA
)
.map_err(|_| arbitrary::Error::IncorrectFormat)?;
let witness = nssa::public_transaction::WitnessSet::for_message(&message, &[&signing_key]);
Ok(NSSATransaction::Public(
nssa::PublicTransaction::new(message, witness),
))
Ok(NSSATransaction::Public(nssa::PublicTransaction::new(
message, witness,
)))
}
}
@ -78,10 +77,10 @@ pub fn arb_hashable_block_data() -> impl Strategy<Value = HashableBlockData> {
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<Value = Vec<NSSATransaction>> {
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<NSSATransaction> = 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<NSSATransaction> = 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<Value = Vec<NSSATransaction>
pub fn arb_pathological_sequence() -> impl Strategy<Value = Vec<NSSATransaction>> {
let accounts = test_accounts();
let n = accounts.len();
proptest::collection::vec(
(0..n, 0..n, 0u128..5u128, any::<bool>()),
1..8_usize,
proptest::collection::vec((0..n, 0..n, 0u128..5u128, any::<bool>()), 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()
})
}

View File

@ -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);