Skip to content

Commit

Permalink
Minor renaming, docs
Browse files Browse the repository at this point in the history
  • Loading branch information
Dinonard committed Nov 3, 2023
1 parent d0d196e commit 9723d9f
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 58 deletions.
2 changes: 1 addition & 1 deletion pallets/dapp-staking-v3/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ pub fn initial_config<T: Config>() {
period_info: PeriodInfo {
number: 1,
subperiod: Subperiod::Voting,
ending_era: 2,
subperiod_end_era: 2,
},
maintenance: false,
});
Expand Down
13 changes: 7 additions & 6 deletions pallets/dapp-staking-v3/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@ pub mod pallet {
period_info: PeriodInfo {
number: 1,
subperiod: Subperiod::Voting,
ending_era: 2,
subperiod_end_era: 2,
},
maintenance: false,
};
Expand Down Expand Up @@ -524,11 +524,12 @@ pub mod pallet {
dapp_reward_pool: Balance::zero(),
};

let ending_era =
let subperiod_end_era =
next_era.saturating_add(T::StandardErasPerBuildAndEarnPeriod::get());
let build_and_earn_start_block =
now.saturating_add(T::StandardEraLength::get());
protocol_state.into_next_subperiod(ending_era, build_and_earn_start_block);
protocol_state
.into_next_subperiod(subperiod_end_era, build_and_earn_start_block);

era_info.migrate_to_next_era(Some(protocol_state.subperiod()));

Expand Down Expand Up @@ -576,11 +577,11 @@ pub mod pallet {

// For the sake of consistency we treat the whole `Voting` period as a single era.
// This means no special handling is required for this period, it only lasts potentially longer than a single standard era.
let ending_era = next_era.saturating_add(1);
let subperiod_end_era = next_era.saturating_add(1);
let voting_period_length = Self::blocks_per_voting_period();
let next_era_start_block = now.saturating_add(voting_period_length);

protocol_state.into_next_subperiod(ending_era, next_era_start_block);
protocol_state.into_next_subperiod(subperiod_end_era, next_era_start_block);

era_info.migrate_to_next_era(Some(protocol_state.subperiod()));

Expand Down Expand Up @@ -1522,7 +1523,7 @@ pub mod pallet {
match force_type {
ForcingType::Era => (),
ForcingType::Subperiod => {
state.period_info.ending_era = state.era.saturating_add(1);
state.period_info.subperiod_end_era = state.era.saturating_add(1);
}
}
});
Expand Down
2 changes: 1 addition & 1 deletion pallets/dapp-staking-v3/src/test/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ impl ExtBuilder {
period_info: PeriodInfo {
number: 1,
subperiod: Subperiod::Voting,
ending_era: 2,
subperiod_end_era: 2,
},
maintenance: false,
});
Expand Down
9 changes: 7 additions & 2 deletions pallets/dapp-staking-v3/src/test/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -870,7 +870,7 @@ fn stake_in_final_era_fails() {
// Force Build&Earn period
ActiveProtocolState::<Test>::mutate(|state| {
state.period_info.subperiod = Subperiod::BuildAndEarn;
state.period_info.ending_era = state.era + 1;
state.period_info.subperiod_end_era = state.era + 1;
});

// Try to stake in the final era of the period, which should fail.
Expand Down Expand Up @@ -1293,7 +1293,12 @@ fn claim_staker_rewards_after_expiry_fails() {
ActiveProtocolState::<Test>::get().period_number() + reward_retention_in_periods,
);
advance_to_into_next_subperiod();
advance_to_era(ActiveProtocolState::<Test>::get().period_info.ending_era - 1);
advance_to_era(
ActiveProtocolState::<Test>::get()
.period_info
.subperiod_end_era
- 1,
);
assert_claim_staker_rewards(account);

// Ensure we're still in the first period for the sake of test validity
Expand Down
84 changes: 44 additions & 40 deletions pallets/dapp-staking-v3/src/test/tests_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,23 +45,27 @@ fn subperiod_sanity_check() {
#[test]
fn period_info_basic_checks() {
let period_number = 2;
let ending_era = 5;
let subperiod_end_era = 5;
let info = PeriodInfo {
number: period_number,
subperiod: Subperiod::Voting,
ending_era: ending_era,
subperiod_end_era: subperiod_end_era,
};

// Sanity checks
assert_eq!(info.number, period_number);
assert_eq!(info.subperiod, Subperiod::Voting);
assert_eq!(info.ending_era, ending_era);
assert_eq!(info.subperiod_end_era, subperiod_end_era);

// Voting period checks
assert!(!info.is_next_period(ending_era - 1));
assert!(!info.is_next_period(ending_era));
assert!(!info.is_next_period(ending_era + 1));
for era in vec![ending_era - 1, ending_era, ending_era + 1] {
assert!(!info.is_next_period(subperiod_end_era - 1));
assert!(!info.is_next_period(subperiod_end_era));
assert!(!info.is_next_period(subperiod_end_era + 1));
for era in vec![
subperiod_end_era - 1,
subperiod_end_era,
subperiod_end_era + 1,
] {
assert!(
!info.is_next_period(era),
"Cannot trigger 'true' in the Voting period type."
Expand All @@ -72,11 +76,11 @@ fn period_info_basic_checks() {
let info = PeriodInfo {
number: period_number,
subperiod: Subperiod::BuildAndEarn,
ending_era: ending_era,
subperiod_end_era: subperiod_end_era,
};
assert!(!info.is_next_period(ending_era - 1));
assert!(info.is_next_period(ending_era));
assert!(info.is_next_period(ending_era + 1));
assert!(!info.is_next_period(subperiod_end_era - 1));
assert!(info.is_next_period(subperiod_end_era));
assert!(info.is_next_period(subperiod_end_era + 1));
}

#[test]
Expand All @@ -94,12 +98,12 @@ fn protocol_state_default() {
fn protocol_state_basic_checks() {
let mut protocol_state = ProtocolState::<BlockNumber>::default();
let period_number = 5;
let ending_era = 11;
let subperiod_end_era = 11;
let next_era_start = 31;
protocol_state.period_info = PeriodInfo {
number: period_number,
subperiod: Subperiod::Voting,
ending_era: ending_era,
subperiod_end_era: subperiod_end_era,
};
protocol_state.next_era_start = next_era_start;

Expand All @@ -112,30 +116,30 @@ fn protocol_state_basic_checks() {
assert!(protocol_state.is_new_era(next_era_start + 1));

// Toggle new period type check - 'Voting' to 'BuildAndEarn'
let ending_era_1 = 23;
let subperiod_end_era_1 = 23;
let next_era_start_1 = 41;
protocol_state.into_next_subperiod(ending_era_1, next_era_start_1);
protocol_state.into_next_subperiod(subperiod_end_era_1, next_era_start_1);
assert_eq!(protocol_state.subperiod(), Subperiod::BuildAndEarn);
assert_eq!(
protocol_state.period_number(),
period_number,
"Switching from 'Voting' to 'BuildAndEarn' should not trigger period bump."
);
assert_eq!(protocol_state.period_end_era(), ending_era_1);
assert_eq!(protocol_state.period_end_era(), subperiod_end_era_1);
assert!(!protocol_state.is_new_era(next_era_start_1 - 1));
assert!(protocol_state.is_new_era(next_era_start_1));

// Toggle from 'BuildAndEarn' over to 'Voting'
let ending_era_2 = 24;
let subperiod_end_era_2 = 24;
let next_era_start_2 = 91;
protocol_state.into_next_subperiod(ending_era_2, next_era_start_2);
protocol_state.into_next_subperiod(subperiod_end_era_2, next_era_start_2);
assert_eq!(protocol_state.subperiod(), Subperiod::Voting);
assert_eq!(
protocol_state.period_number(),
period_number + 1,
"Switching from 'BuildAndEarn' to 'Voting' must trigger period bump."
);
assert_eq!(protocol_state.period_end_era(), ending_era_2);
assert_eq!(protocol_state.period_end_era(), subperiod_end_era_2);
assert!(protocol_state.is_new_era(next_era_start_2));
}

Expand Down Expand Up @@ -481,7 +485,7 @@ fn account_ledger_add_stake_amount_basic_example_works() {
PeriodInfo {
number: period_number,
subperiod: Subperiod::Voting,
ending_era: 0
subperiod_end_era: 0
}
)
.is_ok());
Expand All @@ -494,7 +498,7 @@ fn account_ledger_add_stake_amount_basic_example_works() {
let period_info_1 = PeriodInfo {
number: period_1,
subperiod: Subperiod::Voting,
ending_era: 100,
subperiod_end_era: 100,
};
let lock_amount = 17;
let stake_amount = 11;
Expand Down Expand Up @@ -531,7 +535,7 @@ fn account_ledger_add_stake_amount_basic_example_works() {
let period_info_2 = PeriodInfo {
number: period_1,
subperiod: Subperiod::BuildAndEarn,
ending_era: 100,
subperiod_end_era: 100,
};
assert!(acc_ledger
.add_stake_amount(1, first_era, period_info_2)
Expand Down Expand Up @@ -559,7 +563,7 @@ fn account_ledger_add_stake_amount_advanced_example_works() {
let period_info_1 = PeriodInfo {
number: period_1,
subperiod: Subperiod::Voting,
ending_era: 100,
subperiod_end_era: 100,
};
let lock_amount = 17;
let stake_amount_1 = 11;
Expand Down Expand Up @@ -606,7 +610,7 @@ fn account_ledger_add_stake_amount_invalid_era_or_period_fails() {
let period_info_1 = PeriodInfo {
number: period_1,
subperiod: Subperiod::Voting,
ending_era: 100,
subperiod_end_era: 100,
};
let lock_amount = 13;
let stake_amount = 7;
Expand All @@ -629,7 +633,7 @@ fn account_ledger_add_stake_amount_invalid_era_or_period_fails() {
PeriodInfo {
number: period_1 + 1,
subperiod: Subperiod::Voting,
ending_era: 100
subperiod_end_era: 100
}
),
Err(AccountLedgerError::InvalidPeriod)
Expand All @@ -650,7 +654,7 @@ fn account_ledger_add_stake_amount_invalid_era_or_period_fails() {
PeriodInfo {
number: period_1 + 1,
subperiod: Subperiod::Voting,
ending_era: 100
subperiod_end_era: 100
}
),
Err(AccountLedgerError::InvalidPeriod)
Expand All @@ -670,7 +674,7 @@ fn account_ledger_add_stake_amount_too_large_amount_fails() {
PeriodInfo {
number: 1,
subperiod: Subperiod::Voting,
ending_era: 100
subperiod_end_era: 100
}
),
Err(AccountLedgerError::UnavailableStakeFunds)
Expand All @@ -682,7 +686,7 @@ fn account_ledger_add_stake_amount_too_large_amount_fails() {
let period_info_1 = PeriodInfo {
number: period_1,
subperiod: Subperiod::Voting,
ending_era: 100,
subperiod_end_era: 100,
};
let lock_amount = 13;
acc_ledger.add_lock_amount(lock_amount);
Expand Down Expand Up @@ -713,7 +717,7 @@ fn account_ledger_unstake_amount_basic_scenario_works() {
let period_info_1 = PeriodInfo {
number: period_1,
subperiod: Subperiod::BuildAndEarn,
ending_era: 100,
subperiod_end_era: 100,
};
acc_ledger.add_lock_amount(amount_1);

Expand Down Expand Up @@ -764,7 +768,7 @@ fn account_ledger_unstake_amount_advanced_scenario_works() {
let period_info_1 = PeriodInfo {
number: period_1,
subperiod: Subperiod::BuildAndEarn,
ending_era: 100,
subperiod_end_era: 100,
};
acc_ledger.add_lock_amount(amount_1);

Expand Down Expand Up @@ -836,7 +840,7 @@ fn account_ledger_unstake_from_invalid_era_fails() {
let period_info_1 = PeriodInfo {
number: period_1,
subperiod: Subperiod::BuildAndEarn,
ending_era: 100,
subperiod_end_era: 100,
};
acc_ledger.add_lock_amount(amount_1);
assert!(acc_ledger
Expand All @@ -857,7 +861,7 @@ fn account_ledger_unstake_from_invalid_era_fails() {
PeriodInfo {
number: period_1 + 1,
subperiod: Subperiod::Voting,
ending_era: 100
subperiod_end_era: 100
}
),
Err(AccountLedgerError::InvalidPeriod)
Expand All @@ -878,7 +882,7 @@ fn account_ledger_unstake_from_invalid_era_fails() {
PeriodInfo {
number: period_1 + 1,
subperiod: Subperiod::Voting,
ending_era: 100
subperiod_end_era: 100
}
),
Err(AccountLedgerError::InvalidPeriod)
Expand All @@ -897,7 +901,7 @@ fn account_ledger_unstake_too_much_fails() {
let period_info_1 = PeriodInfo {
number: period_1,
subperiod: Subperiod::BuildAndEarn,
ending_era: 100,
subperiod_end_era: 100,
};
acc_ledger.add_lock_amount(amount_1);
assert!(acc_ledger
Expand Down Expand Up @@ -930,7 +934,7 @@ fn account_ledger_unlockable_amount_works() {
let period_info = PeriodInfo {
number: stake_period,
subperiod: Subperiod::Voting,
ending_era: 100,
subperiod_end_era: 100,
};
assert!(acc_ledger
.add_stake_amount(stake_amount, lock_era, period_info)
Expand Down Expand Up @@ -1412,7 +1416,7 @@ fn contract_stake_info_stake_is_ok() {
let period_info_1 = PeriodInfo {
number: period_1,
subperiod: Subperiod::Voting,
ending_era: 20,
subperiod_end_era: 20,
};
let amount_1 = 31;
contract_stake.stake(amount_1, period_info_1, era_1);
Expand All @@ -1433,7 +1437,7 @@ fn contract_stake_info_stake_is_ok() {
let period_info_1 = PeriodInfo {
number: period_1,
subperiod: Subperiod::BuildAndEarn,
ending_era: 20,
subperiod_end_era: 20,
};
contract_stake.stake(amount_1, period_info_1, era_1);
let entry_1_2 = contract_stake.get(stake_era_1, period_1).unwrap();
Expand Down Expand Up @@ -1463,7 +1467,7 @@ fn contract_stake_info_stake_is_ok() {
let period_info_2 = PeriodInfo {
number: period_2,
subperiod: Subperiod::BuildAndEarn,
ending_era: 20,
subperiod_end_era: 20,
};
let amount_3 = 41;

Expand Down Expand Up @@ -1508,7 +1512,7 @@ fn contract_stake_info_unstake_is_ok() {
let period_info = PeriodInfo {
number: period,
subperiod: Subperiod::Voting,
ending_era: 20,
subperiod_end_era: 20,
};
let stake_amount = 100;
contract_stake.stake(stake_amount, period_info, era_1);
Expand All @@ -1529,7 +1533,7 @@ fn contract_stake_info_unstake_is_ok() {
let period_info = PeriodInfo {
number: period,
subperiod: Subperiod::BuildAndEarn,
ending_era: 40,
subperiod_end_era: 40,
};
let era_2 = era_1 + 3;
let amount_2 = 7;
Expand Down
Loading

0 comments on commit 9723d9f

Please sign in to comment.