Skip to content

Commit

Permalink
Merge branch 'tomas/fix-tx-bond-test-condition' (#590)
Browse files Browse the repository at this point in the history
* tomas/fix-tx-bond-test-condition:
  Apply suggestions from code review
  changelog: add #590
  wasm/tx_bond: fix delegation detection to compare source and validator
  wasm/tx_bond: add test seed which makes it fail
  • Loading branch information
tzemanovic committed Oct 14, 2022
2 parents f2104ec + 6ea6baf commit 94367d5
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 62 deletions.
Original file line number Diff line number Diff line change
@@ -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))
1 change: 1 addition & 0 deletions wasm/wasm_source/proptest-regressions/tx_bond.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
cc e54347c5114ef29538127ba9ad68d1572af839ec63c015318fc0827818853a22
118 changes: 56 additions & 62 deletions wasm/wasm_source/src/tx_bond.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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<Bond<token::Amount>> =
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<token::Amount> =
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<Bond<token::Amount>> = 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<token::Amount> =
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<token::Amount> =
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<token::Amount> = 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<token::Amount> = 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<token::Amount> = 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}"
);
}
}

Expand Down

0 comments on commit 94367d5

Please sign in to comment.