Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
brentstone committed Apr 23, 2024
1 parent d5bd388 commit 77bdded
Showing 1 changed file with 63 additions and 53 deletions.
116 changes: 63 additions & 53 deletions crates/apps/src/lib/node/ledger/shell/finalize_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3551,12 +3551,6 @@ mod test_finalize_block {
let votes = get_default_true_votes(&shell.state, Epoch::default());
assert!(!votes.is_empty());

// Advance to epoch 1 and
// 1. Delegate 37_231 NAM to validator
// 2. Validator self-unbond 84_654 NAM
let (current_epoch, _) = advance_epoch(&mut shell, &pkh1, &votes, None);
assert_eq!(shell.state.in_mem().block.epoch.0, 1_u64);

// Make an account with balance and delegate some tokens
let delegator = address::testing::gen_implicit_address();
let del_1_amount = token::Amount::native_whole(37_231);
Expand All @@ -3568,6 +3562,14 @@ mod test_finalize_block {
token::Amount::native_whole(200_000),
)
.unwrap();

// Advance to epoch 1 and
// 1. Delegate 37_231 NAM to validator
// 2. Validator self-unbond 84_654 NAM
let (current_epoch, _) = advance_epoch(&mut shell, &pkh1, &votes, None);
assert_eq!(shell.state.in_mem().block.epoch.0, 1_u64);

// Delegate
namada_proof_of_stake::bond_tokens(
&mut shell.state,
Some(&delegator),
Expand All @@ -3590,6 +3592,7 @@ mod test_finalize_block {
)
.unwrap();

// Check stakes
let val_stake = namada_proof_of_stake::storage::read_validator_stake(
&shell.state,
&params,
Expand Down Expand Up @@ -3633,6 +3636,7 @@ mod test_finalize_block {
)
.unwrap();

// Check stakes
let val_stake = namada_proof_of_stake::storage::read_validator_stake(
&shell.state,
&params,
Expand Down Expand Up @@ -3668,6 +3672,7 @@ mod test_finalize_block {
let (current_epoch, _) = advance_epoch(&mut shell, &pkh1, &votes, None);
tracing::debug!("\nBonding in epoch 3");

// Self-bond
let self_bond_1_amount = token::Amount::native_whole(9_123);
namada_proof_of_stake::bond_tokens(
&mut shell.state,
Expand All @@ -3688,6 +3693,7 @@ mod test_finalize_block {
let (current_epoch, _) = advance_epoch(&mut shell, &pkh1, &votes, None);
assert_eq!(current_epoch.0, 4_u64);

// Self-unbond
let self_unbond_2_amount = token::Amount::native_whole(15_000);
namada_proof_of_stake::unbond_tokens(
&mut shell.state,
Expand Down Expand Up @@ -3723,7 +3729,7 @@ mod test_finalize_block {

tracing::debug!("Advancing to epoch 6");

// Advance to epoch 6
// Advance to epoch 6 and discover a misbehavior
let votes = get_default_true_votes(
&shell.state,
shell.state.in_mem().block.epoch,
Expand Down Expand Up @@ -3760,8 +3766,8 @@ mod test_finalize_block {

// Assertions
assert_eq!(current_epoch.0, 6_u64);
let processing_epoch = misbehavior_epoch
+ params.slash_processing_epoch_offset();
let processing_epoch =
misbehavior_epoch + params.slash_processing_epoch_offset();
let enqueued_slash = enqueued_slashes_handle()
.at(&processing_epoch)
.at(&val1.address)
Expand Down Expand Up @@ -3866,16 +3872,15 @@ mod test_finalize_block {
.unwrap()
);

let pre_stake_10 =
namada_proof_of_stake::storage::read_validator_stake(
&shell.state,
&params,
&val1.address,
Epoch(10),
)
.unwrap();
let pre_stake_9 = namada_proof_of_stake::storage::read_validator_stake(
&shell.state,
&params,
&val1.address,
Epoch(9),
)
.unwrap();
assert_eq!(
pre_stake_10,
pre_stake_9,
initial_stake + del_1_amount
- self_unbond_1_amount
- del_unbond_1_amount
Expand Down Expand Up @@ -3935,7 +3940,10 @@ mod test_finalize_block {
Dec::one(),
Dec::new(9, 0).unwrap() * tot_frac * tot_frac,
);
dbg!(cubic_rate);
println!(
"\nThe cubic slash rate for epoch 3 infractions is {}\n",
cubic_rate
);

let equal_enough = |rate1: Dec, rate2: Dec| -> bool {
let tolerance = Dec::new(1, 9).unwrap();
Expand All @@ -3957,14 +3965,14 @@ mod test_finalize_block {

// Check the amount of stake deducted from the futuremost epoch while
// processing the slashes
let post_stake_10 = read_validator_stake(
let post_stake_9 = read_validator_stake(
&shell.state,
&params,
&val1.address,
Epoch(10),
Epoch(9),
)
.unwrap();

// The amount unbonded after the infraction that affected the deltas
// before processing is `del_unbond_1_amount + self_bond_1_amount -
// self_unbond_2_amount` (since this self-bond was enacted then unbonded
Expand All @@ -3977,18 +3985,24 @@ mod test_finalize_block {
+ self_bond_1_amount
- self_unbond_2_amount)
.mul_ceil(slash_rate_3);

println!("Slash rate for epoch 3 infractions: {}", slash_rate_3);
println!(
"Pre Val stake at epoch 10: {}\nPost Val stake at epoch 10: {}",
pre_stake_9.to_string_native(),
post_stake_9.to_string_native()
);
assert!(
((pre_stake_10 - post_stake_10).change()
((pre_stake_9 - post_stake_9).change()
- exp_slashed_during_processing_9.change())
.abs()
< Uint::from(1000),
< Uint::from(2),
"Expected {}, got {} (with less than 1000 err)",
exp_slashed_during_processing_9.to_string_native(),
(pre_stake_10 - post_stake_10).to_string_native(),
(pre_stake_9 - post_stake_9).to_string_native(),
);

// Check that we can compute the stake at the pipeline epoch
// NOTE: may be off. by 1 namnam due to rounding;
let exp_pipeline_stake = (Dec::one() - slash_rate_3)
* Dec::from(
initial_stake + del_1_amount
Expand All @@ -4000,28 +4014,31 @@ mod test_finalize_block {
+ Dec::from(del_2_amount);

assert!(
exp_pipeline_stake.abs_diff(&Dec::from(post_stake_10))
exp_pipeline_stake.abs_diff(&Dec::from(post_stake_9))
<= Dec::new(2, NATIVE_MAX_DECIMAL_PLACES).unwrap(),
"Expected {}, got {} (with less than 2 err), diff {}",
exp_pipeline_stake,
post_stake_10.to_string_native(),
exp_pipeline_stake.abs_diff(&Dec::from(post_stake_10)),
post_stake_9.to_string_native(),
exp_pipeline_stake.abs_diff(&Dec::from(post_stake_9)),
);

// 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 = (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()));
let exp_slashed_3 = exp_slashed_during_processing_9;
assert!(slash_pool_balance - exp_slashed_3 <= 1.into());

let pre_stake_10 = read_validator_stake(
&shell.state,
&params,
&val1.address,
Epoch(10),
)?;

// Advance to epoch 10, where the infraction committed in epoch 4 will
// be processed
let votes = get_default_true_votes(
Expand All @@ -4032,7 +4049,6 @@ mod test_finalize_block {
assert_eq!(current_epoch.0, 10_u64);

// Check the balance of the Slash Pool
// TODO: finish once implemented
let slash_pool_balance = read_balance(
&shell.state,
&staking_token,
Expand All @@ -4043,19 +4059,19 @@ mod test_finalize_block {
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)
dbg!(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)
dbg!(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);
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()
<= Uint::one() * 2u64
);

let val_stake = read_validator_stake(
Expand Down Expand Up @@ -4084,36 +4100,26 @@ mod test_finalize_block {
post_stake_10.to_string_native()
);

// dbg!(&val_stake);
// dbg!(pre_stake_10 - post_stake_10);
dbg!(&val_stake, &post_stake_10, &pre_stake_10, &exp_slashed_4);

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

// dbg!(&val_stake, &exp_stake);
// dbg!(exp_slashed_during_processing_8 +
// exp_slashed_during_processing_9); dbg!(
// val_stake_3
// - (exp_slashed_during_processing_8 +
// 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;

assert!(
(exp_stake.change() - post_stake_10.change()).abs() <= Uint::one()
(exp_stake.change() - post_stake_10.change()).abs()
<= Uint::one() * 3u64
);

for _ in 0..2 {
Expand Down Expand Up @@ -4253,6 +4259,8 @@ mod test_finalize_block {
)
.unwrap();

println!("\nDBG0\n");

let exp_del_withdraw_slashed_amount =
del_unbond_1_amount.mul_ceil(slash_rate_3);
assert!(
Expand All @@ -4262,6 +4270,8 @@ mod test_finalize_block {
<= Uint::one()
);

println!("\nDBG1\n");

// TODO: finish once implemented
// Check the balance of the Slash Pool
// let slash_pool_balance: token::Amount = shell
Expand Down

0 comments on commit 77bdded

Please sign in to comment.