Skip to content

Commit

Permalink
Merge branch 'tomas/fix-pos-tx-test-input-lower-bound' (#813) into main
Browse files Browse the repository at this point in the history
* tomas/fix-pos-tx-test-input-lower-bound:
  changelog: add #813
  wasm/tx_unbond: add regressions file
  wasm/tx_{bond,unbond,withdraw}: avoid zero-amount action for test inputs
  PoS: allow 0 amount bond/unbond in tx
  • Loading branch information
juped committed Dec 13, 2022
2 parents f9d72f0 + ff9e899 commit 732a8d0
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 29 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- Allow size zero bonds in PoS for testing.
([#813](https://github.com/anoma/namada/pull/813))
8 changes: 8 additions & 0 deletions core/src/types/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -502,4 +502,12 @@ pub mod testing {
pub fn arb_amount_ceiled(max: u64) -> impl Strategy<Value = Amount> {
(0..=max).prop_map(Amount::from)
}

/// Generate an arbitrary non-zero token amount up to and including given
/// `max` value
pub fn arb_amount_non_zero_ceiled(
max: u64,
) -> impl Strategy<Value = Amount> {
(1..=max).prop_map(Amount::from)
}
}
10 changes: 0 additions & 10 deletions proof_of_stake/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -968,8 +968,6 @@ pub enum BondError {
InactiveValidator(Address),
#[error("Voting power overflow: {0}")]
VotingPowerOverflow(TryFromIntError),
#[error("Given zero amount to bond")]
ZeroAmount,
}

#[allow(missing_docs)]
Expand All @@ -987,8 +985,6 @@ pub enum UnbondError {
ValidatorHasNoVotingPower(Address),
#[error("Voting power overflow: {0}")]
VotingPowerOverflow(TryFromIntError),
#[error("Given zero amount to unbond")]
ZeroAmount,
}

#[allow(missing_docs)]
Expand Down Expand Up @@ -1291,9 +1287,6 @@ fn bond_tokens(
validator_set: &mut ValidatorSets,
current_epoch: Epoch,
) -> Result<BondData, BondError> {
if amount == token::Amount::default() {
return Err(BondError::ZeroAmount);
}
// Check the validator state
match validator_state {
None => {
Expand Down Expand Up @@ -1400,9 +1393,6 @@ fn unbond_tokens(
validator_set: &mut ValidatorSets,
current_epoch: Epoch,
) -> Result<UnbondData, UnbondError> {
if amount == token::Amount::default() {
return Err(UnbondError::ZeroAmount);
}
// We can unbond tokens that are bonded for a future epoch (not yet
// active), hence we check the total at the pipeline offset
let unbondable_amount = bond
Expand Down
1 change: 1 addition & 0 deletions wasm/wasm_source/proptest-regressions/tx_unbond.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
cc f22e874350910b197cb02a4a07ec5bef18e16c0d1a39eaabaee43d1fc05ce11d
2 changes: 1 addition & 1 deletion wasm/wasm_source/src/tx_bond.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ mod tests {
(
arb_established_address(),
prop::option::of(arb_non_internal_address()),
token::testing::arb_amount_ceiled(max_amount),
token::testing::arb_amount_non_zero_ceiled(max_amount),
)
.prop_map(|(validator, source, amount)| {
transaction::pos::Bond {
Expand Down
25 changes: 12 additions & 13 deletions wasm/wasm_source/src/tx_unbond.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,21 +258,20 @@ mod tests {
epoch {epoch}"
);
}
// Check that the unbond is as expected
let start_epoch = match &unbond.source {
Some(_) => {
// This bond was a delegation
Epoch::from(pos_params.pipeline_len)
}
None => {
// This bond was a genesis validator self-bond
Epoch::default()
}
let start_epoch = if is_delegation {
// This bond was a delegation
Epoch::from(pos_params.pipeline_len)
} else {
// This bond was a genesis validator self-bond
Epoch::default()
};
let end_epoch = Epoch::from(pos_params.unbonding_len - 1);

let expected_unbond =
HashMap::from_iter([((start_epoch, end_epoch), unbond.amount)]);
let expected_unbond = if unbond.amount == token::Amount::default() {
HashMap::new()
} else {
HashMap::from_iter([((start_epoch, end_epoch), unbond.amount)])
};
let actual_unbond: Unbond =
unbonds_post.get(pos_params.unbonding_len).unwrap();
assert_eq!(
Expand Down Expand Up @@ -342,7 +341,7 @@ mod tests {
(
address::testing::arb_established_address(),
prop::option::of(address::testing::arb_non_internal_address()),
token::testing::arb_amount_ceiled(max_amount),
token::testing::arb_amount_non_zero_ceiled(max_amount),
)
.prop_map(|(validator, source, amount)| {
let validator = Address::Established(validator);
Expand Down
11 changes: 6 additions & 5 deletions wasm/wasm_source/src/tx_withdraw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,16 +201,17 @@ mod tests {
fn arb_initial_stake_and_unbonded_amount()
-> impl Strategy<Value = (token::Amount, token::Amount)> {
// Generate initial stake
token::testing::arb_amount_ceiled((i64::MAX / 8) as u64).prop_flat_map(
|initial_stake| {
token::testing::arb_amount_non_zero_ceiled((i64::MAX / 8) as u64)
.prop_flat_map(|initial_stake| {
// Use the initial stake to limit the unbonded amount from the
// stake
let unbonded_amount =
token::testing::arb_amount_ceiled(initial_stake.into());
token::testing::arb_amount_non_zero_ceiled(
initial_stake.into(),
);
// Use the generated initial stake too too
(Just(initial_stake), unbonded_amount)
},
)
})
}

fn arb_withdraw() -> impl Strategy<Value = transaction::pos::Withdraw> {
Expand Down

0 comments on commit 732a8d0

Please sign in to comment.