Skip to content

Commit

Permalink
Update era/period transition in mock & tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Dinonard committed Oct 6, 2023
1 parent 1fcbed5 commit 5dcfe02
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 18 deletions.
7 changes: 4 additions & 3 deletions pallets/dapp-staking-v3/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ pub mod pallet {
let ending_era =
next_era.saturating_add(T::BuildAndEarnPeriodLength::get());
let build_and_earn_start_block = now.saturating_add(T::EraLength::get());
protocol_state.next_period(ending_era, build_and_earn_start_block);
protocol_state.next_period_type(ending_era, build_and_earn_start_block);

Some(Event::<T>::NewPeriod {
period_type: protocol_state.period_type(),
Expand All @@ -320,10 +320,11 @@ pub mod pallet {
let ending_era = next_era.saturating_add(1);
let voting_period_length = T::EraLength::get()
.saturating_mul(T::VotingPeriodLength::get().into());
let voting_start_block = now
let next_era_start_block = now
.saturating_add(voting_period_length)
.saturating_add(One::one());
protocol_state.next_period(ending_era, voting_start_block);
protocol_state.next_period_type(ending_era, next_era_start_block);
protocol_state.period_info.number.saturating_accrue(1);

// TODO: trigger tier configuration calculation based on internal & external params.

Expand Down
37 changes: 24 additions & 13 deletions pallets/dapp-staking-v3/src/test/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,9 @@ impl pallet_dapp_staking::Config for Test {
type Currency = Balances;
type SmartContract = MockSmartContract;
type ManagerOrigin = frame_system::EnsureRoot<AccountId>;
type EraLength = ConstU64<8>;
type VotingPeriodLength = ConstU32<2>;
type BuildAndEarnPeriodLength = ConstU32<4>;
type EraLength = ConstU64<10>;
type VotingPeriodLength = ConstU32<8>;
type BuildAndEarnPeriodLength = ConstU32<16>;
type MaxNumberOfContracts = ConstU16<10>;
type MaxLockedChunks = ConstU32<5>;
type MaxUnlockingChunks = ConstU32<5>;
Expand Down Expand Up @@ -156,14 +156,21 @@ impl ExtBuilder {
System::set_block_number(1);
DappStaking::on_initialize(System::block_number());

// TODO: remove this after proper on_init handling is implemented
// TODO: should pallet handle this?
// TODO2: not sure why the mess with type happens here, I can check it later
let era_length: BlockNumber =
<<Test as pallet_dapp_staking::Config>::EraLength as sp_core::Get<_>>::get();
let voting_period_length_in_eras: EraNumber =
<<Test as pallet_dapp_staking::Config>::VotingPeriodLength as sp_core::Get<_>>::get(
);

pallet_dapp_staking::ActiveProtocolState::<Test>::put(ProtocolState {
era: 1,
next_era_start: BlockNumber::from(101_u32),
next_era_start: era_length.saturating_mul(voting_period_length_in_eras.into()),
period_info: PeriodInfo {
number: 1,
period_type: PeriodType::Voting,
ending_era: 16,
ending_era: 2,
},
maintenance: false,
});
Expand All @@ -175,7 +182,7 @@ impl ExtBuilder {

/// Run to the specified block number.
/// Function assumes first block has been initialized.
pub(crate) fn _run_to_block(n: u64) {
pub(crate) fn run_to_block(n: u64) {
while System::block_number() < n {
DappStaking::on_finalize(System::block_number());
System::set_block_number(System::block_number() + 1);
Expand All @@ -187,15 +194,17 @@ pub(crate) fn _run_to_block(n: u64) {
/// Run for the specified number of blocks.
/// Function assumes first block has been initialized.
pub(crate) fn run_for_blocks(n: u64) {
_run_to_block(System::block_number() + n);
run_to_block(System::block_number() + n);
}

/// Advance blocks until the specified era has been reached.
///
/// Function has no effect if era is already passed.
pub(crate) fn advance_to_era(era: EraNumber) {
// TODO: Properly implement this later when additional logic has been implemented
ActiveProtocolState::<Test>::mutate(|state| state.era = era);
assert!(era >= ActiveProtocolState::<Test>::get().era);
while ActiveProtocolState::<Test>::get().era < era {
run_for_blocks(1);
}
}

/// Advance blocks until next era has been reached.
Expand All @@ -207,11 +216,13 @@ pub(crate) fn advance_to_next_era() {
///
/// Function has no effect if period is already passed.
pub(crate) fn advance_to_period(period: PeriodNumber) {
// TODO: Properly implement this later when additional logic has been implemented
ActiveProtocolState::<Test>::mutate(|state| state.period_info.number = period);
assert!(period >= ActiveProtocolState::<Test>::get().period_number());
while ActiveProtocolState::<Test>::get().period_number() < period {
run_for_blocks(1);
}
}

/// Advance blocks until next period has been reached.
pub(crate) fn advance_to_next_period() {
advance_to_period(ActiveProtocolState::<Test>::get().period_info.number + 1);
advance_to_period(ActiveProtocolState::<Test>::get().period_number() + 1);
}
2 changes: 1 addition & 1 deletion pallets/dapp-staking-v3/src/test/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -722,7 +722,7 @@ fn stake_basic_example_is_ok() {
assert_stake(account, &smart_contract, stake_amount_3);

// 3rd scenario - advance era again but create a gap, and then stake
advance_to_era(ActiveProtocolState::<Test>::get().era + 3);
advance_to_era(ActiveProtocolState::<Test>::get().era + 2);
let stake_amount_4 = 19;
assert_stake(account, &smart_contract, stake_amount_4);

Expand Down
2 changes: 1 addition & 1 deletion pallets/dapp-staking-v3/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ where
}

/// TODO
pub fn next_period(&mut self, ending_era: EraNumber, next_era_start: BlockNumber) {
pub fn next_period_type(&mut self, ending_era: EraNumber, next_era_start: BlockNumber) {
self.period_info = PeriodInfo {
number: self.period_number(),
period_type: self.period_type().next(),
Expand Down

0 comments on commit 5dcfe02

Please sign in to comment.