diff --git a/.changelog/unreleased/testing/590-fix-tx-bond-test-condition.md b/.changelog/unreleased/testing/590-fix-tx-bond-test-condition.md new file mode 100644 index 0000000000..80435089b2 --- /dev/null +++ b/.changelog/unreleased/testing/590-fix-tx-bond-test-condition.md @@ -0,0 +1,2 @@ +- Fix a condition in tx_bond test that causes a false negative result + ([#590](https://github.com/anoma/namada/pull/590)) \ No newline at end of file diff --git a/wasm/wasm_source/proptest-regressions/tx_bond.txt b/wasm/wasm_source/proptest-regressions/tx_bond.txt new file mode 100644 index 0000000000..3a88756618 --- /dev/null +++ b/wasm/wasm_source/proptest-regressions/tx_bond.txt @@ -0,0 +1 @@ +cc e54347c5114ef29538127ba9ad68d1572af839ec63c015318fc0827818853a22 diff --git a/wasm/wasm_source/src/tx_bond.rs b/wasm/wasm_source/src/tx_bond.rs index e8a85800f4..6718988657 100644 --- a/wasm/wasm_source/src/tx_bond.rs +++ b/wasm/wasm_source/src/tx_bond.rs @@ -66,6 +66,8 @@ mod tests { key: key::common::SecretKey, pos_params: PosParams, ) -> TxResult { + let is_delegation = matches!( + &bond.source, Some(source) if *source != bond.validator); let staking_reward_address = address::testing::established_address_1(); let consensus_key = key::testing::keypair_1().ref_to(); let staking_reward_key = key::testing::keypair_2().ref_to(); @@ -154,71 +156,63 @@ mod tests { source: bond_src, }; let bonds_post = ctx().read_bond(&bond_id)?.unwrap(); - match &bond.source { - Some(_) => { - // This bond was a delegation - for epoch in 0..pos_params.pipeline_len { - let bond: Option> = - bonds_post.get(epoch); - assert!( - bond.is_none(), - "Delegation before pipeline offset should be empty - \ - checking epoch {epoch}" - ); - } - for epoch in pos_params.pipeline_len..=pos_params.unbonding_len - { - let start_epoch = - namada_tx_prelude::proof_of_stake::types::Epoch::from( - pos_params.pipeline_len, - ); - let expected_bond = - HashMap::from_iter([(start_epoch, bond.amount)]); - let bond: Bond = - bonds_post.get(epoch).unwrap(); - assert_eq!( - bond.pos_deltas, expected_bond, - "Delegation at and after pipeline offset should be \ - equal to the bonded amount - checking epoch {epoch}" - ); - } + + if is_delegation { + // A delegation is applied at pipeline offset + for epoch in 0..pos_params.pipeline_len { + let bond: Option> = bonds_post.get(epoch); + assert!( + bond.is_none(), + "Delegation before pipeline offset should be empty - \ + checking epoch {epoch}, got {bond:#?}" + ); } - None => { - let genesis_epoch = - namada_tx_prelude::proof_of_stake::types::Epoch::from(0); - // It was a self-bond - for epoch in 0..pos_params.pipeline_len { - let expected_bond = - HashMap::from_iter([(genesis_epoch, initial_stake)]); - let bond: Bond = - bonds_post.get(epoch).expect( - "Genesis validator should already have self-bond", - ); - assert_eq!( - bond.pos_deltas, expected_bond, - "Delegation before pipeline offset should be equal to \ - the genesis initial stake - checking epoch {epoch}" + for epoch in pos_params.pipeline_len..=pos_params.unbonding_len { + let start_epoch = + namada_tx_prelude::proof_of_stake::types::Epoch::from( + pos_params.pipeline_len, ); - } - for epoch in pos_params.pipeline_len..=pos_params.unbonding_len - { - let start_epoch = - namada_tx_prelude::proof_of_stake::types::Epoch::from( - pos_params.pipeline_len, - ); - let expected_bond = HashMap::from_iter([ - (genesis_epoch, initial_stake), - (start_epoch, bond.amount), - ]); - let bond: Bond = - bonds_post.get(epoch).unwrap(); - assert_eq!( - bond.pos_deltas, expected_bond, - "Delegation at and after pipeline offset should \ - contain genesis stake and the bonded amount - \ - checking epoch {epoch}" + let expected_bond = + HashMap::from_iter([(start_epoch, bond.amount)]); + let bond: Bond = bonds_post.get(epoch).unwrap(); + assert_eq!( + bond.pos_deltas, expected_bond, + "Delegation at and after pipeline offset should be equal \ + to the bonded amount - checking epoch {epoch}" + ); + } + } else { + let genesis_epoch = + namada_tx_prelude::proof_of_stake::types::Epoch::from(0); + // It was a self-bond + for epoch in 0..pos_params.pipeline_len { + let expected_bond = + HashMap::from_iter([(genesis_epoch, initial_stake)]); + let bond: Bond = bonds_post + .get(epoch) + .expect("Genesis validator should already have self-bond"); + assert_eq!( + bond.pos_deltas, expected_bond, + "Self-bond before pipeline offset should be equal to the \ + genesis initial stake - checking epoch {epoch}" + ); + } + for epoch in pos_params.pipeline_len..=pos_params.unbonding_len { + let start_epoch = + namada_tx_prelude::proof_of_stake::types::Epoch::from( + pos_params.pipeline_len, ); - } + let expected_bond = HashMap::from_iter([ + (genesis_epoch, initial_stake), + (start_epoch, bond.amount), + ]); + let bond: Bond = bonds_post.get(epoch).unwrap(); + assert_eq!( + bond.pos_deltas, expected_bond, + "Self-bond at and after pipeline offset should contain \ + genesis stake and the bonded amount - checking epoch \ + {epoch}" + ); } }