Skip to content

Commit

Permalink
integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Dinonard committed Jan 12, 2024
1 parent 0ee8739 commit a434791
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 8 deletions.
5 changes: 4 additions & 1 deletion pallets/dapp-staking-v3/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1496,6 +1496,7 @@ pub mod pallet {
.into())
}

// TODO: make this unavailable in production, to be only used for testing
/// Used to force a change of era or subperiod.
/// The effect isn't immediate but will happen on the next block.
///
Expand All @@ -1521,8 +1522,10 @@ pub mod pallet {
}
}

// TODO: Right now it won't account for the full weight incurred by calling this notification.
// Right now it won't account for the full weight incurred by calling this notification.
// It's not a big problem since this call is not expected to be called ever in production.
// Also, in case of subperiod forcing, the alignment will be broken but since this is only call for testing,
// we don't need to concern ourselves with it.
Self::notify_block_before_new_era(&state);
});

Expand Down
2 changes: 1 addition & 1 deletion pallets/inflation/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ pub mod pallet {

// 4. Block at which the inflation must be recalculated.
let recalculation_era =
next_era.saturating_add(T::CycleConfiguration::blocks_per_cycle());
next_era.saturating_add(T::CycleConfiguration::eras_per_cycle());

// 5. Return calculated values
InflationConfiguration {
Expand Down
6 changes: 3 additions & 3 deletions pallets/inflation/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ fn inflation_parameters_validity_check_works() {
}

#[test]
fn inflation_recalucation_works() {
fn inflation_recalculation_works() {
ExternalityBuilder::build().execute_with(|| {
let total_issuance = Balances::total_issuance();
let params = InflationParams::<Test>::get();
Expand All @@ -252,7 +252,7 @@ fn inflation_recalucation_works() {
// Verify basics are ok
assert_eq!(
new_config.recalculation_era,
now + <Test as Config>::CycleConfiguration::blocks_per_cycle()
now + <Test as Config>::CycleConfiguration::eras_per_cycle()
);
assert_eq!(
new_config.issuance_safety_cap,
Expand Down Expand Up @@ -432,7 +432,7 @@ fn payout_reward_fails_when_relaxed_cap_is_exceeded() {
}

#[test]
fn cylcle_configuration_works() {
fn cycle_configuration_works() {
ExternalityBuilder::build().execute_with(|| {
type CycleConfig = <Test as Config>::CycleConfiguration;

Expand Down
2 changes: 1 addition & 1 deletion runtime/shibuya/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -757,7 +757,7 @@ impl WeightToFeePolynomial for WeightToFee {
}
}

/// Handles coverting weight consumed by XCM into native currency fee.
/// Handles converting weight consumed by XCM into native currency fee.
///
/// Similar to standard `WeightToFee` handler, but force uses the minimum multiplier.
pub struct XcmWeightToFee;
Expand Down
80 changes: 80 additions & 0 deletions tests/integration/src/dapp_staking_v3.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// This file is part of Astar.

// Copyright (C) 2019-2023 Stake Technologies Pte.Ltd.
// SPDX-License-Identifier: GPL-3.0-or-later

// Astar is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Astar is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Astar. If not, see <http://www.gnu.org/licenses/>.

#![cfg(test)]

use crate::setup::*;

use pallet_dapp_staking_v3::*;

#[test]
fn dapp_staking_triggers_inflation_recalculation() {
new_test_ext().execute_with(|| {
let init_inflation_config = pallet_inflation::ActiveInflationConfig::<Runtime>::get();

let recalculation_era = init_inflation_config.recalculation_era;

// 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 {
assert_ok!(DappStaking::force(RuntimeOrigin::root(), ForcingType::Era,));
run_for_blocks(1);
assert_eq!(
init_inflation_config,
pallet_inflation::ActiveInflationConfig::<Runtime>::get(),
"Must not change until recalculation"
);
}
assert_eq!(
ActiveProtocolState::<Runtime>::get().subperiod(),
Subperiod::BuildAndEarn,
"Sanity check."
);

// 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;
});

// Another sanity check, move block by block and ensure protocol works as expected.
let target_block = ActiveProtocolState::<Runtime>::get().next_era_start;
run_to_block(target_block - 2);
assert_eq!(
init_inflation_config,
pallet_inflation::ActiveInflationConfig::<Runtime>::get(),
"Sanity check."
);

// So far inflation config remained unchanged.
// Now we expect the trigger which will update it.
run_for_blocks(1);
assert_eq!(
init_inflation_config,
pallet_inflation::ActiveInflationConfig::<Runtime>::get(),
"Still the same, should be updated ONLY after the block has been finalized."
);

run_for_blocks(1);
let new_inflation_config = pallet_inflation::ActiveInflationConfig::<Runtime>::get();
assert_ne!(
init_inflation_config, new_inflation_config,
"Must be updated after the block has been finalized."
);
});
}
3 changes: 3 additions & 0 deletions tests/integration/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,6 @@ mod dispatch_precompile_filter_old;

#[cfg(feature = "shibuya")]
mod unified_accounts;

#[cfg(feature = "shibuya")]
mod dapp_staking_v3;
8 changes: 6 additions & 2 deletions tests/integration/src/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub use sp_core::{H160, H256, U256};
pub use sp_io::hashing::keccak_256;
pub use sp_runtime::{AccountId32, MultiAddress};

pub use astar_primitives::evm::UnifiedAddressMapper;
pub use astar_primitives::{evm::UnifiedAddressMapper, BlockNumber};

#[cfg(feature = "shibuya")]
pub use shibuya::*;
Expand Down Expand Up @@ -218,7 +218,7 @@ pub fn new_test_ext() -> sp_io::TestExternalities {
// Block time: 12 seconds.
pub const BLOCK_TIME: u64 = 12_000;

pub fn run_to_block(n: u32) {
pub fn run_to_block(n: BlockNumber) {
while System::block_number() < n {
let block_number = System::block_number();
TransactionPayment::on_finalize(block_number);
Expand Down Expand Up @@ -260,6 +260,10 @@ pub fn run_to_block(n: u32) {
}
}

pub fn run_for_blocks(n: BlockNumber) {
run_to_block(System::block_number() + n)
}

fn last_events(n: usize) -> Vec<RuntimeEvent> {
frame_system::Pallet::<Runtime>::events()
.into_iter()
Expand Down

0 comments on commit a434791

Please sign in to comment.