Skip to content

Commit

Permalink
fix(node): add some comments to the rewards tests
Browse files Browse the repository at this point in the history
  • Loading branch information
muXxer committed Nov 3, 2024
1 parent 8e2782b commit a5d37b8
Showing 1 changed file with 184 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,22 +47,41 @@ module iota_system::rewards_distribution_tests {
// need to advance epoch so validator's staking starts counting
advance_epoch(scenario);

// rewards of 100 IOTA are split evenly between the validators
advance_epoch_with_reward_amounts(0, 100, scenario);

// before: V1: 100, V2: 200, V3: 300, V4: 400
// after: V1: 125, V2: 225, V3: 325, V4: 425
assert_validator_total_stake_amounts(
validator_addrs(),
vector[125 * NANOS_PER_IOTA, 225 * NANOS_PER_IOTA, 325 * NANOS_PER_IOTA, 425 * NANOS_PER_IOTA],
vector[
(100 + 25) * NANOS_PER_IOTA,
(200 + 25) * NANOS_PER_IOTA,
(300 + 25) * NANOS_PER_IOTA,
(400 + 25) * NANOS_PER_IOTA,
],
scenario
);

stake_with(VALIDATOR_ADDR_2, VALIDATOR_ADDR_2, 720, scenario);

advance_epoch(scenario);
advance_epoch_with_reward_amounts(0, 100, scenario);

// Even though validator 2 has a lot more stake now, it should not get more rewards because
// the voting power is capped at 10%.
// rewards of 100 IOTA are split evenly between the validators
advance_epoch_with_reward_amounts(0, 100, scenario);

// before: V1: 125, V2: 225+720, V3: 325, V4: 425
// after: V1: 150, V2: 250+720, V3: 350, V4: 450
assert_validator_total_stake_amounts(
validator_addrs(),
vector[150 * NANOS_PER_IOTA, 970 * NANOS_PER_IOTA, 350 * NANOS_PER_IOTA, 450 * NANOS_PER_IOTA],
vector[
(125 + 25) * NANOS_PER_IOTA,
(225 + 720 + 25) * NANOS_PER_IOTA,
(325 + 25) * NANOS_PER_IOTA,
(425 + 25) * NANOS_PER_IOTA,
],
scenario
);

Expand All @@ -77,9 +96,21 @@ module iota_system::rewards_distribution_tests {

// need to advance epoch so validator's staking starts counting
advance_epoch(scenario);


// rewards of 100 IOTA are split evenly between the validators
advance_epoch_with_reward_amounts(0, 100, scenario);
assert_validator_total_stake_amounts(validator_addrs(), vector[100_000_025 * NANOS_PER_IOTA, 200_000_025 * NANOS_PER_IOTA, 300_000_025 * NANOS_PER_IOTA, 400_000_025 * NANOS_PER_IOTA], scenario);

// before: V1: 100000000, V2: 200000000, V3: 300000000, V4: 400000000
// after: V1: 100000025, V2: 200000025, V3: 300000025, V4: 400000025
assert_validator_total_stake_amounts(
validator_addrs(),
vector[
(100_000_000 + 25) * NANOS_PER_IOTA,
(200_000_000 + 25) * NANOS_PER_IOTA,
(300_000_000 + 25) * NANOS_PER_IOTA,
(400_000_000 + 25) * NANOS_PER_IOTA,
],
scenario);
scenario_val.end();
}

Expand Down Expand Up @@ -120,6 +151,7 @@ module iota_system::rewards_distribution_tests {
advance_epoch_with_target_reward_amounts(validator_target_reward, 0, computation_reward, scenario);

let new_supply = total_supply(scenario);

// The difference between target reward and computation reward should have been burned.
assert_eq(prev_supply - (computation_reward - validator_target_reward) * NANOS_PER_IOTA, new_supply);

Expand All @@ -141,6 +173,7 @@ module iota_system::rewards_distribution_tests {
advance_epoch_with_target_reward_amounts(validator_target_reward, 0, computation_reward, scenario);

let new_supply = total_supply(scenario);

// The difference between target reward and computation reward should have been minted.
assert_eq(prev_supply + (validator_target_reward - computation_reward) * NANOS_PER_IOTA, new_supply);

Expand All @@ -154,19 +187,17 @@ module iota_system::rewards_distribution_tests {
let scenario = &mut scenario_val;

advance_epoch(scenario);
// V1: 100, V2: 200, V3: 300, V4: 400

advance_epoch_with_target_reward_amounts(800, 0, 400, scenario);

// The computation reward is lower than the target reward, so 400 IOTA should be minted.
// Each validator pool has 25% of the voting power and thus gets 25% of the reward (200 IOTA).
assert_validator_total_stake_amounts(
validator_addrs(),
vector[
(100 + 200) * NANOS_PER_IOTA,
(200 + 200) * NANOS_PER_IOTA,
(300 + 200) * NANOS_PER_IOTA,
(400 + 200) * NANOS_PER_IOTA,
(100 + 200) * NANOS_PER_IOTA,
(200 + 200) * NANOS_PER_IOTA,
(300 + 200) * NANOS_PER_IOTA,
(400 + 200) * NANOS_PER_IOTA,
],
scenario
);
Expand Down Expand Up @@ -195,10 +226,10 @@ module iota_system::rewards_distribution_tests {
assert_validator_total_stake_amounts(
validator_addrs(),
vector[
(100 + 200) * NANOS_PER_IOTA,
(200 + 200) * NANOS_PER_IOTA,
(300 + 200) * NANOS_PER_IOTA,
(400 + 200) * NANOS_PER_IOTA,
(100 + 200) * NANOS_PER_IOTA,
(200 + 200) * NANOS_PER_IOTA,
(300 + 200) * NANOS_PER_IOTA,
(400 + 200) * NANOS_PER_IOTA,
],
scenario
);
Expand Down Expand Up @@ -262,19 +293,53 @@ module iota_system::rewards_distribution_tests {
stake_with(STAKER_ADDR_2, VALIDATOR_ADDR_2, 100, scenario);
advance_epoch(scenario);

assert_validator_total_stake_amounts(validator_addrs(), vector[300 * NANOS_PER_IOTA, 300 * NANOS_PER_IOTA, 300 * NANOS_PER_IOTA, 400 * NANOS_PER_IOTA], scenario);
assert_validator_self_stake_amounts(validator_addrs(), vector[100 * NANOS_PER_IOTA, 200 * NANOS_PER_IOTA, 300 * NANOS_PER_IOTA, 400 * NANOS_PER_IOTA], scenario);
assert_validator_total_stake_amounts(
validator_addrs(),
vector[
300 * NANOS_PER_IOTA,
300 * NANOS_PER_IOTA,
300 * NANOS_PER_IOTA,
400 * NANOS_PER_IOTA,
],
scenario);

assert_validator_self_stake_amounts(
validator_addrs(),
vector[
100 * NANOS_PER_IOTA,
200 * NANOS_PER_IOTA,
300 * NANOS_PER_IOTA,
400 * NANOS_PER_IOTA,
], scenario);

// Each pool gets 30 IOTA.
advance_epoch_with_reward_amounts(0, 120, scenario);
assert_validator_self_stake_amounts(validator_addrs(), vector[110 * NANOS_PER_IOTA, 220 * NANOS_PER_IOTA, 330 * NANOS_PER_IOTA, 430 * NANOS_PER_IOTA], scenario);
assert_validator_self_stake_amounts(
validator_addrs(),
vector[
110 * NANOS_PER_IOTA,
220 * NANOS_PER_IOTA,
330 * NANOS_PER_IOTA,
430 * NANOS_PER_IOTA,
],
scenario);

unstake(STAKER_ADDR_1, 0, scenario);
stake_with(STAKER_ADDR_2, VALIDATOR_ADDR_1, 600, scenario);
// Each pool gets 30 IOTA.
advance_epoch_with_reward_amounts(0, 120, scenario);
// staker 1 receives only 20 IOTA of rewards, not 40 since we are using pre-epoch exchange rate.
assert_eq(total_iota_balance(STAKER_ADDR_1, scenario), 220 * NANOS_PER_IOTA);
assert_validator_self_stake_amounts(validator_addrs(), vector[140 * NANOS_PER_IOTA, 240 * NANOS_PER_IOTA, 360 * NANOS_PER_IOTA, 460 * NANOS_PER_IOTA], scenario);
assert_validator_self_stake_amounts(
validator_addrs(),
vector[
140 * NANOS_PER_IOTA,
240 * NANOS_PER_IOTA,
360 * NANOS_PER_IOTA,
460 * NANOS_PER_IOTA,
],
scenario);

unstake(STAKER_ADDR_2, 0, scenario);
assert_eq(total_iota_balance(STAKER_ADDR_2, scenario), 120 * NANOS_PER_IOTA); // 20 IOTA of rewards received

Expand Down Expand Up @@ -331,21 +396,62 @@ module iota_system::rewards_distribution_tests {
advance_epoch_with_reward_amounts(0, 120, scenario);
// V1: 230, V2: 330, V3: 330, V4: 430
// 2 IOTA, or 20 % of staker_2's rewards, goes to validator_2
assert_validator_non_self_stake_amounts(validator_addrs(), vector[115 * NANOS_PER_IOTA, 108 * NANOS_PER_IOTA, 0, 0], scenario);
assert_validator_self_stake_amounts(validator_addrs(), vector[115 * NANOS_PER_IOTA, 222 * NANOS_PER_IOTA, 330 * NANOS_PER_IOTA, 430 * NANOS_PER_IOTA], scenario);
assert_validator_non_self_stake_amounts(
validator_addrs(),
vector[
115 * NANOS_PER_IOTA,
108 * NANOS_PER_IOTA,
0,
0,
],
scenario);

assert_validator_self_stake_amounts(
validator_addrs(),
vector[
115 * NANOS_PER_IOTA,
222 * NANOS_PER_IOTA,
330 * NANOS_PER_IOTA,
430 * NANOS_PER_IOTA,
],
scenario);

set_commission_rate_and_advance_epoch(VALIDATOR_ADDR_1, 1000, scenario); // 10% commission

advance_epoch_with_reward_amounts(0, 240, scenario);
assert_validator_total_stake_amounts(validator_addrs(), vector[290 * NANOS_PER_IOTA, 390 * NANOS_PER_IOTA, 390 * NANOS_PER_IOTA, 490 * NANOS_PER_IOTA], scenario);
assert_validator_total_stake_amounts(
validator_addrs(),
vector[
290 * NANOS_PER_IOTA,
390 * NANOS_PER_IOTA,
390 * NANOS_PER_IOTA,
490 * NANOS_PER_IOTA,
], scenario);

// Staker 1 rewards in the recent distribution is 0.9 x 30 = 27 IOTA
// Validator 1 rewards in the recent distribution is 60 - 27 = 33 IOTA

// Staker 2 amounts for 0.8 * 60 * (108 / 330) + 108 = 123.709 IOTA
// Validator 2 amounts for 390 - 123.709 = 266.291 IOTA
assert_validator_non_self_stake_amounts(validator_addrs(), vector[142 * NANOS_PER_IOTA, 123709090909, 0, 0], scenario);
assert_validator_self_stake_amounts(validator_addrs(), vector[148 * NANOS_PER_IOTA, 266290909091, 390 * NANOS_PER_IOTA, 490 * NANOS_PER_IOTA], scenario);
assert_validator_non_self_stake_amounts(
validator_addrs(),
vector[
142 * NANOS_PER_IOTA,
123709090909,
0,
0,
],
scenario);

assert_validator_self_stake_amounts(
validator_addrs(),
vector[
148 * NANOS_PER_IOTA,
266290909091,
390 * NANOS_PER_IOTA,
490 * NANOS_PER_IOTA,
],
scenario);

scenario_val.end();
}
Expand Down Expand Up @@ -422,7 +528,15 @@ module iota_system::rewards_distribution_tests {
// Without reward slashing, the validator's stakes should be [100+450, 200+600, 300+900, 400+900]
// after the last epoch advancement.
// Since 60 IOTA, or 10% of validator_2's rewards (600) are slashed, she only has 800 - 60 = 740 now.
assert_validator_self_stake_amounts(validator_addrs(), vector[550 * NANOS_PER_IOTA, 740 * NANOS_PER_IOTA, 1200 * NANOS_PER_IOTA, 1300 * NANOS_PER_IOTA], scenario);
assert_validator_self_stake_amounts(
validator_addrs(),
vector[
550 * NANOS_PER_IOTA,
740 * NANOS_PER_IOTA,
1200 * NANOS_PER_IOTA,
1300 * NANOS_PER_IOTA,
],
scenario);

// Unstake so we can check the stake rewards as well.
unstake(STAKER_ADDR_1, 0, scenario);
Expand Down Expand Up @@ -462,7 +576,15 @@ module iota_system::rewards_distribution_tests {
// Without reward slashing, the validator's stakes should be [100+450, 200+600, 300+900, 400+900]
// after the last epoch advancement.
// The entire rewards of validator 2's staking pool are slashed, which is 900 IOTA.
assert_validator_self_stake_amounts(validator_addrs(), vector[550 * NANOS_PER_IOTA, 200 * NANOS_PER_IOTA, 1200 * NANOS_PER_IOTA, 1300 * NANOS_PER_IOTA], scenario);
assert_validator_self_stake_amounts(
validator_addrs(),
vector[
550 * NANOS_PER_IOTA,
200 * NANOS_PER_IOTA,
1200 * NANOS_PER_IOTA,
1300 * NANOS_PER_IOTA,
],
scenario);

// Unstake so we can check the stake rewards as well.
unstake(STAKER_ADDR_1, 0, scenario);
Expand All @@ -480,9 +602,16 @@ module iota_system::rewards_distribution_tests {
let mut scenario_val = test_scenario::begin(VALIDATOR_ADDR_1);
let scenario = &mut scenario_val;

// Put 300 IOTA into the storage fund. This should not change the pools' stake or give rewards.
// Put 300 IOTA into the storage fund. This should not change the pools' stake or give rewards.
advance_epoch_with_reward_amounts(300, 0, scenario);
assert_validator_total_stake_amounts(validator_addrs(), vector[100 * NANOS_PER_IOTA, 200 * NANOS_PER_IOTA, 300 * NANOS_PER_IOTA, 400 * NANOS_PER_IOTA], scenario);
assert_validator_total_stake_amounts(
validator_addrs(),
vector[
100 * NANOS_PER_IOTA,
200 * NANOS_PER_IOTA,
300 * NANOS_PER_IOTA,
400 * NANOS_PER_IOTA,
], scenario);

// Add a few stakes.
stake_with(STAKER_ADDR_1, VALIDATOR_ADDR_3, 200, scenario);
Expand All @@ -500,12 +629,19 @@ module iota_system::rewards_distribution_tests {
1000, 1500, 2000, scenario
);

// Each unslashed validator staking pool gets 375 IOTA of computation rewards + 25 IOTA (1/3) of validator 4's slashed computation reward,
// Validator 1's should get 375 * 1 = 375 in rewards.
// Validator 2's should get 375 * 1 = 375 in rewards.
// Validator 3's should get 375 * 3/5 = 225 in rewards.
// Validator 4's should get (375 - 75) * 4/5 = 240 in rewards.
assert_validator_self_stake_amounts(validator_addrs(), vector[(100 + 375) * NANOS_PER_IOTA, (200 + 375) * NANOS_PER_IOTA, (300 + 225) * NANOS_PER_IOTA, (400 + 240) * NANOS_PER_IOTA], scenario);
assert_validator_self_stake_amounts(
validator_addrs(),
vector[
(100 + 375) * NANOS_PER_IOTA,
(200 + 375) * NANOS_PER_IOTA,
(300 + 225) * NANOS_PER_IOTA,
(400 + 240) * NANOS_PER_IOTA,
],
scenario);

// Unstake so we can check the stake rewards as well.
unstake(STAKER_ADDR_1, 0, scenario);
Expand Down Expand Up @@ -534,7 +670,14 @@ module iota_system::rewards_distribution_tests {
);

// All validators should have 0 rewards added so their stake stays the same.
assert_validator_self_stake_amounts(validator_addrs(), vector[100 * NANOS_PER_IOTA, 200 * NANOS_PER_IOTA, 300 * NANOS_PER_IOTA, 400 * NANOS_PER_IOTA], scenario);
assert_validator_self_stake_amounts(
validator_addrs(),
vector[
100 * NANOS_PER_IOTA,
200 * NANOS_PER_IOTA,
300 * NANOS_PER_IOTA,
400 * NANOS_PER_IOTA,
], scenario);

scenario.next_tx(@0x0);
// Storage fund balance should be the same as before.
Expand Down Expand Up @@ -703,6 +846,11 @@ module iota_system::rewards_distribution_tests {
scenario_val.end();
}

// This will set up the IOTA system state with the following validator stakes:
// Valdiator 1 => 100
// Valdiator 2 => 200
// Valdiator 3 => 300
// Valdiator 4 => 400
fun set_up_iota_system_state() {
let mut scenario_val = test_scenario::begin(@0x0);
let scenario = &mut scenario_val;
Expand All @@ -718,6 +866,11 @@ module iota_system::rewards_distribution_tests {
scenario_val.end();
}

// This will set up the IOTA system state with the following validator stakes:
// Valdiator 1 => 100000000
// Valdiator 2 => 200000000
// Valdiator 3 => 300000000
// Valdiator 4 => 400000000
fun set_up_iota_system_state_with_big_amounts() {
let mut scenario_val = test_scenario::begin(@0x0);
let scenario = &mut scenario_val;
Expand Down

0 comments on commit a5d37b8

Please sign in to comment.