Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
brentstone committed Apr 18, 2024
1 parent 8a439ac commit b0877c1
Showing 1 changed file with 59 additions and 52 deletions.
111 changes: 59 additions & 52 deletions crates/apps/src/lib/node/ledger/shell/finalize_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1757,7 +1757,7 @@ mod test_finalize_block {

let (current_epoch, inflation) =
advance_epoch(&mut shell, &pkh1, &votes, None);
total_rewards += inflation;
total_rewards += token::Amount::from(inflation);

// Query the available rewards
let query_rewards = namada_proof_of_stake::query_reward_tokens(
Expand Down Expand Up @@ -1810,7 +1810,7 @@ mod test_finalize_block {
// Go to the next epoch
let (current_epoch, inflation) =
advance_epoch(&mut shell, &pkh1, &votes, None);
total_rewards += inflation;
total_rewards += token::Amount::from(inflation);

// Unbond some tokens
let unbond_amount = token::Amount::native_whole(50_000);
Expand Down Expand Up @@ -1887,9 +1887,9 @@ mod test_finalize_block {
advance_epoch(&mut shell, &pkh1, &votes, None);
current_epoch = new_epoch;

total_rewards += inflation;
total_rewards += token::Amount::from(inflation);
if current_epoch <= pipeline_epoch_from_unbond {
missed_rewards += inflation;
missed_rewards += token::Amount::from(inflation);
}
}

Expand Down Expand Up @@ -2034,7 +2034,7 @@ mod test_finalize_block {
let (new_epoch, inflation) =
advance_epoch(&mut shell, &pkh1, &votes, None);
current_epoch = new_epoch;
total_rewards += inflation;
total_rewards += token::Amount::from(inflation);
}

// Claim the rewards for the validator for the first two epochs
Expand All @@ -2060,6 +2060,7 @@ mod test_finalize_block {
);
let (new_epoch, inflation_3) =
advance_epoch(&mut shell, &pkh1, &votes, None);
let inflation_3 = token::Amount::from(inflation_3);
current_epoch = new_epoch;
total_rewards += inflation_3;

Expand Down Expand Up @@ -3489,7 +3490,7 @@ mod test_finalize_block {
}

/// Current test procedure (prefixed by epoch in which the event occurs):
/// 0) Validator initial stake of 00_000
/// 0) Validator initial stake of 100_000
/// 1) Delegate 37_231 to validator
/// 1) Self-unbond 84_654
/// 2) Unbond delegation of 18_000
Expand Down Expand Up @@ -4009,13 +4010,17 @@ mod test_finalize_block {

// Check the balance of the Slash Pool
// TODO: finish once implemented
let slash_pool_balance = read_balance(&shell.state, &staking_token, &namada_proof_of_stake::SLASH_POOL_ADDRESS).unwrap();
let exp_slashed_3 = decimal_mult_amount(
std::cmp::min(Dec::two() * cubic_rate, Dec::one()),
val_stake_3 - del_unbond_1_amount + self_bond_1_amount
- self_unbond_2_amount,
);
assert_eq!(slash_pool_balance, exp_slashed_3);
let slash_pool_balance = read_balance(
&shell.state,
&staking_token,
&namada_proof_of_stake::SLASH_POOL_ADDRESS,
)
.unwrap();
let exp_slashed_3 = (val_stake_3 - del_unbond_1_amount
+ self_bond_1_amount
- self_unbond_2_amount)
.mul_ceil(std::cmp::min(Dec::two() * cubic_rate, Dec::one()));
assert!(slash_pool_balance - exp_slashed_3 <= 1.into());

// Advance to epoch 10, where the infraction committed in epoch 4 will
// be processed
Expand All @@ -4028,32 +4033,30 @@ mod test_finalize_block {

// Check the balance of the Slash Pool
// TODO: finish once implemented
// let slash_pool_balance: token::Amount = shell
// .state
// .read(&slash_balance_key)
// .expect("must be able to read")
// .unwrap_or_default();
let slash_pool_balance = read_balance(
&shell.state,
&staking_token,
&namada_proof_of_stake::SLASH_POOL_ADDRESS,
)
.unwrap();

// let exp_slashed_4 = if dec!(2) * cubic_rate >= Decimal::ONE {
// token::Amount::zero()
// } else if dec!(3) * cubic_rate >= Decimal::ONE {
// decimal_mult_amount(
// Decimal::ONE - dec!(2) * cubic_rate,
// val_stake_4 + self_bond_1_amount - self_unbond_2_amount,
// )
// } else {
// decimal_mult_amount(
// std::cmp::min(cubic_rate, Decimal::ONE),
// val_stake_4 + self_bond_1_amount - self_unbond_2_amount,
// )
// };
// dbg!(slash_pool_balance, exp_slashed_3 + exp_slashed_4);
// assert!(
// (slash_pool_balance.change()
// - (exp_slashed_3 + exp_slashed_4).change())
// .abs()
// <= 1
// );
let exp_slashed_4 = if Dec::two() * cubic_rate >= Dec::one() {
token::Amount::zero()
} else if Dec::from(3u64) * cubic_rate >= Dec::one() {
(val_stake_4 + self_bond_1_amount - self_unbond_2_amount)
.mul_ceil(Dec::one() - Dec::two() * cubic_rate)
} else {
(val_stake_4 + self_bond_1_amount - self_unbond_2_amount)
.mul_ceil(std::cmp::min(cubic_rate, Dec::one()))
};

dbg!(slash_pool_balance, exp_slashed_3 + exp_slashed_4);
assert!(
(slash_pool_balance.change()
- (exp_slashed_3 + exp_slashed_4).change())
.abs()
<= Uint::one()
);

let val_stake = read_validator_stake(
&shell.state,
Expand Down Expand Up @@ -4086,11 +4089,14 @@ mod test_finalize_block {

// dbg!(&exp_slashed_during_processing_9);
// TODO: finish once implemented
// assert!(
// ((pre_stake_11 - post_stake_11).change() -
// exp_slashed_4.change()) .abs()
// <= 1
// );
assert!(
(dbg!(
(pre_stake_10 - post_stake_10).change()
- exp_slashed_4.change()
))
.abs()
<= Uint::one()
);

// dbg!(&val_stake, &exp_stake);
// dbg!(exp_slashed_during_processing_8 +
Expand All @@ -4100,14 +4106,15 @@ mod test_finalize_block {
// exp_slashed_during_processing_9)
// );

// let exp_stake = val_stake_3 - del_unbond_1_amount +
// self_bond_1_amount
// - self_unbond_2_amount
// + del_2_amount
// - exp_slashed_3
// - exp_slashed_4;
let exp_stake = val_stake_3 - del_unbond_1_amount + self_bond_1_amount
- self_unbond_2_amount
+ del_2_amount
- exp_slashed_3
- exp_slashed_4;

// assert!((exp_stake.change() - post_stake_11.change()).abs() <= 1);
assert!(
(exp_stake.change() - post_stake_10.change()).abs() <= Uint::one()
);

for _ in 0..2 {
let votes = get_default_true_votes(
Expand Down Expand Up @@ -4657,7 +4664,7 @@ mod test_finalize_block {
proposer_address: &[u8],
consensus_votes: &[VoteInfo],
misbehaviors: Option<Vec<Misbehavior>>,
) -> (Epoch, token::Amount) {
) -> (Epoch, token::Change) {
let current_epoch = shell.state.in_mem().block.epoch;
let staking_token =
namada_proof_of_stake::staking_token_address(&shell.state);
Expand Down Expand Up @@ -4695,7 +4702,7 @@ mod test_finalize_block {

(
shell.state.in_mem().block.epoch,
pos_balance_post - pos_balance_pre,
pos_balance_post.change() - pos_balance_pre.change(),
)
}

Expand Down

0 comments on commit b0877c1

Please sign in to comment.