Skip to content

Commit

Permalink
Integration test fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Dinonard committed Aug 30, 2024
1 parent 4c1c3a8 commit 879048a
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 33 deletions.
11 changes: 0 additions & 11 deletions pallets/dapp-staking/src/test/tests_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
22 changes: 19 additions & 3 deletions pallets/dapp-staking/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
Expand All @@ -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
Expand Down Expand Up @@ -380,6 +391,11 @@ impl<UnlockingLen> AccountLedger<UnlockingLen>
where
UnlockingLen: Get<u32>,
{
/// 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
Expand Down
6 changes: 3 additions & 3 deletions tests/integration/src/dapp_staking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Runtime>::get().era < recalculation_era - 1 {
while ActiveProtocolState::<Runtime>::get().era() < recalculation_era - 1 {
assert_ok!(DappStaking::force(RuntimeOrigin::root(), ForcingType::Era,));
run_for_blocks(1);
assert_eq!(
Expand All @@ -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::<Runtime>::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::<Runtime>::get().next_era_start;
let target_block = ActiveProtocolState::<Runtime>::get().next_era_start();
run_to_block(target_block - 2);
assert_eq!(
init_inflation_config,
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/src/governance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ fn community_council_can_execute_dapp_staking_calls() {

// Check that the lock was successful
assert_eq!(
pallet_dapp_staking::Ledger::<Runtime>::get(&proxy_account).locked,
pallet_dapp_staking::Ledger::<Runtime>::get(&proxy_account).locked(),
lock_amount
);
})
Expand Down
15 changes: 0 additions & 15 deletions tests/integration/src/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -195,20 +194,6 @@ impl ExtBuilder {
ext.execute_with(|| {
System::set_block_number(1);

let era_length = <Runtime as pallet_dapp_staking::Config>::CycleConfiguration::blocks_per_era();
let voting_period_length_in_eras =
<Runtime as pallet_dapp_staking::Config>::CycleConfiguration::eras_per_voting_subperiod();

pallet_dapp_staking::ActiveProtocolState::<Runtime>::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::<Runtime>::put(false);

// Ensure the initial state is set for the first block
Expand Down

0 comments on commit 879048a

Please sign in to comment.