From 879048a163f77e4d433a4f3a04fcdbd4f3f57dc8 Mon Sep 17 00:00:00 2001 From: Dino Pacandi Date: Fri, 30 Aug 2024 08:42:04 +0200 Subject: [PATCH] Integration test fix --- pallets/dapp-staking/src/test/tests_types.rs | 11 ---------- pallets/dapp-staking/src/types.rs | 22 +++++++++++++++++--- tests/integration/src/dapp_staking.rs | 6 +++--- tests/integration/src/governance.rs | 2 +- tests/integration/src/setup.rs | 15 ------------- 5 files changed, 23 insertions(+), 33 deletions(-) diff --git a/pallets/dapp-staking/src/test/tests_types.rs b/pallets/dapp-staking/src/test/tests_types.rs index 416985eb8..c655141e1 100644 --- a/pallets/dapp-staking/src/test/tests_types.rs +++ b/pallets/dapp-staking/src/test/tests_types.rs @@ -86,17 +86,6 @@ fn period_info_basic_checks() { assert!(info.is_next_period(next_subperiod_start_era + 1)); } -#[test] -fn protocol_state_default() { - let protocol_state = ProtocolState::default(); - - assert_eq!(protocol_state.era, 0); - assert_eq!( - protocol_state.next_era_start, 1, - "Era should start immediately on the first block" - ); -} - #[test] fn protocol_state_basic_checks() { let mut protocol_state = ProtocolState::default(); diff --git a/pallets/dapp-staking/src/types.rs b/pallets/dapp-staking/src/types.rs index d10fc3253..d49720865 100644 --- a/pallets/dapp-staking/src/types.rs +++ b/pallets/dapp-staking/src/types.rs @@ -194,10 +194,10 @@ pub struct ProtocolState { impl Default for ProtocolState { fn default() -> Self { Self { - era: 0, - next_era_start: 1, + era: 1, + next_era_start: 2, period_info: PeriodInfo { - number: 0, + number: 1, subperiod: Subperiod::Voting, next_subperiod_start_era: 2, }, @@ -212,6 +212,17 @@ impl ProtocolState { self.era } + /// Block number at which the next era should start. + pub fn next_era_start(&self) -> BlockNumber { + self.next_era_start + } + + /// Set the next era start block number. + /// Not perfectly clean approach but helps speed up integration tests significantly. + pub fn set_next_era_start(&mut self, next_era_start: BlockNumber) { + self.next_era_start = next_era_start; + } + /// Current subperiod. pub fn subperiod(&self) -> Subperiod { self.period_info.subperiod @@ -380,6 +391,11 @@ impl AccountLedger where UnlockingLen: Get, { + /// How much active locked amount an account has. This can be used for staking. + pub fn locked(&self) -> Balance { + self.locked + } + /// Unlocking chunks. pub fn unlocking_chunks(&self) -> &[UnlockingChunk] { &self.unlocking diff --git a/tests/integration/src/dapp_staking.rs b/tests/integration/src/dapp_staking.rs index bff9c14ce..e991141fa 100644 --- a/tests/integration/src/dapp_staking.rs +++ b/tests/integration/src/dapp_staking.rs @@ -31,7 +31,7 @@ fn dapp_staking_triggers_inflation_recalculation() { // It's not feasible to run through all the blocks needed to trigger all the eras. // Instead, we force the era to change on a block by block basis. - while ActiveProtocolState::::get().era < recalculation_era - 1 { + while ActiveProtocolState::::get().era() < recalculation_era - 1 { assert_ok!(DappStaking::force(RuntimeOrigin::root(), ForcingType::Era,)); run_for_blocks(1); assert_eq!( @@ -49,11 +49,11 @@ fn dapp_staking_triggers_inflation_recalculation() { // Again, hacky approach to speed things up. // This doesn't influence anything in the protocol essentially. ActiveProtocolState::::mutate(|state| { - state.next_era_start = System::block_number() + 5; + state.set_next_era_start(System::block_number() + 5); }); // Another sanity check, move block by block and ensure protocol works as expected. - let target_block = ActiveProtocolState::::get().next_era_start; + let target_block = ActiveProtocolState::::get().next_era_start(); run_to_block(target_block - 2); assert_eq!( init_inflation_config, diff --git a/tests/integration/src/governance.rs b/tests/integration/src/governance.rs index ac8667b5f..746a5e99a 100644 --- a/tests/integration/src/governance.rs +++ b/tests/integration/src/governance.rs @@ -162,7 +162,7 @@ fn community_council_can_execute_dapp_staking_calls() { // Check that the lock was successful assert_eq!( - pallet_dapp_staking::Ledger::::get(&proxy_account).locked, + pallet_dapp_staking::Ledger::::get(&proxy_account).locked(), lock_amount ); }) diff --git a/tests/integration/src/setup.rs b/tests/integration/src/setup.rs index 8b555ae23..7fcf40649 100644 --- a/tests/integration/src/setup.rs +++ b/tests/integration/src/setup.rs @@ -32,7 +32,6 @@ use cumulus_primitives_parachain_inherent::ParachainInherentData; use cumulus_test_relay_sproof_builder::RelayStateSproofBuilder; pub use astar_primitives::{ - dapp_staking::CycleConfiguration, governance::{ CommunityCouncilMembershipInst, MainCouncilMembershipInst, OracleMembershipInst, TechnicalCommitteeMembershipInst, @@ -195,20 +194,6 @@ impl ExtBuilder { ext.execute_with(|| { System::set_block_number(1); - let era_length = ::CycleConfiguration::blocks_per_era(); - let voting_period_length_in_eras = - ::CycleConfiguration::eras_per_voting_subperiod(); - - pallet_dapp_staking::ActiveProtocolState::::put(pallet_dapp_staking::ProtocolState { - era: 1, - next_era_start: era_length.saturating_mul(voting_period_length_in_eras.into()) + 1, - period_info: pallet_dapp_staking::PeriodInfo { - number: 1, - subperiod: pallet_dapp_staking::Subperiod::Voting, - next_subperiod_start_era: 2, - }, - maintenance: false, - }); pallet_dapp_staking::Safeguard::::put(false); // Ensure the initial state is set for the first block