diff --git a/pallets/dapp-staking-v3/src/lib.rs b/pallets/dapp-staking-v3/src/lib.rs index 423ee9ecb4..ff68fd5f4e 100644 --- a/pallets/dapp-staking-v3/src/lib.rs +++ b/pallets/dapp-staking-v3/src/lib.rs @@ -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::::NewPeriod { period_type: protocol_state.period_type(), @@ -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. diff --git a/pallets/dapp-staking-v3/src/test/mock.rs b/pallets/dapp-staking-v3/src/test/mock.rs index 487202e833..381ef96f34 100644 --- a/pallets/dapp-staking-v3/src/test/mock.rs +++ b/pallets/dapp-staking-v3/src/test/mock.rs @@ -109,9 +109,9 @@ impl pallet_dapp_staking::Config for Test { type Currency = Balances; type SmartContract = MockSmartContract; type ManagerOrigin = frame_system::EnsureRoot; - 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>; @@ -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 = + <::EraLength as sp_core::Get<_>>::get(); + let voting_period_length_in_eras: EraNumber = + <::VotingPeriodLength as sp_core::Get<_>>::get( + ); + pallet_dapp_staking::ActiveProtocolState::::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, }); @@ -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); @@ -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::::mutate(|state| state.era = era); + assert!(era >= ActiveProtocolState::::get().era); + while ActiveProtocolState::::get().era < era { + run_for_blocks(1); + } } /// Advance blocks until next era has been reached. @@ -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::::mutate(|state| state.period_info.number = period); + assert!(period >= ActiveProtocolState::::get().period_number()); + while ActiveProtocolState::::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::::get().period_info.number + 1); + advance_to_period(ActiveProtocolState::::get().period_number() + 1); } diff --git a/pallets/dapp-staking-v3/src/test/tests.rs b/pallets/dapp-staking-v3/src/test/tests.rs index f952c378a0..e883fca602 100644 --- a/pallets/dapp-staking-v3/src/test/tests.rs +++ b/pallets/dapp-staking-v3/src/test/tests.rs @@ -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::::get().era + 3); + advance_to_era(ActiveProtocolState::::get().era + 2); let stake_amount_4 = 19; assert_stake(account, &smart_contract, stake_amount_4); diff --git a/pallets/dapp-staking-v3/src/types.rs b/pallets/dapp-staking-v3/src/types.rs index db1ad2f669..8f325af51f 100644 --- a/pallets/dapp-staking-v3/src/types.rs +++ b/pallets/dapp-staking-v3/src/types.rs @@ -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(),