Skip to content

Commit

Permalink
Integration test that verifies PoS checkpoints and chain sessions are…
Browse files Browse the repository at this point in the history
… aligned (#633)
  • Loading branch information
mateuszaaa authored Oct 31, 2023
2 parents 9bb564d + 9fdb335 commit 504ec52
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 2 deletions.
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions mvr/runtime/integration-test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ orml-xtokens = { git = "https://github.com/open-web3-stack/open-runtime-module-l
# Local
mangata-polkadot-runtime = { path = "../mangata-polkadot", optional = true }
xtokens-parachain = { path = "../xtokens-parachain", optional = true }
pallet-proof-of-stake = { path = "../../pallets/proof-of-stake" }

# Xcm simulator
xcm-executor = { git = "https://github.com/paritytech/polkadot", branch = "release-v0.9.42" }
Expand Down
3 changes: 3 additions & 0 deletions runtime/integration-test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ mangata-types = { git = "https://github.com/mangata-finance/substrate", branch =
frame-support = { git = "https://github.com/mangata-finance/substrate", branch = "mangata-dev" }
frame-system = { git = "https://github.com/mangata-finance/substrate", branch = "mangata-dev" }
pallet-balances = { git = "https://github.com/mangata-finance/substrate", branch = "mangata-dev" }
pallet-session = { git = "https://github.com/mangata-finance/substrate", branch = "mangata-dev" }
sp-io = { git = "https://github.com/mangata-finance/substrate", branch = "mangata-dev" }
sp-runtime = { git = "https://github.com/mangata-finance/substrate", branch = "mangata-dev" }
pallet-proxy = { git = "https://github.com/mangata-finance/substrate", branch = "mangata-dev" }
pallet-identity = { git = "https://github.com/mangata-finance/substrate", branch = "mangata-dev" }
parachain-staking = { git = "https://github.com/mangata-finance/moonbeam.git", branch = "mangata-dev" }

# Polkadot
pallet-xcm = { git = "https://github.com/mangata-finance/polkadot", branch = "mangata-dev" }
Expand Down Expand Up @@ -41,6 +43,7 @@ mangata-kusama-runtime = { path = "../mangata-kusama", optional = true }
common-runtime = { path = "../common"}

pallet-xyk = { path = '../../pallets/xyk', version = '0.1.0' }
pallet-proof-of-stake = { path = '../../pallets/proof-of-stake', version = '0.1.0' }
pallet-bootstrap = { path = '../../pallets/bootstrap', version = '0.1.0' }
xyk-runtime-api = { path = '../../pallets/xyk/runtime-api', version = '2.0.0' }
pallet-sudo-origin = { path = '../../pallets/sudo-origin' }
Expand Down
3 changes: 3 additions & 0 deletions runtime/integration-test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ mod setup;
#[cfg(any(feature = "with-kusama-runtime",))]
mod xyk;

#[cfg(any(feature = "with-kusama-runtime",))]
mod proof_of_stake;

#[cfg(any(feature = "with-kusama-runtime",))]
mod bootstrap;

Expand Down
63 changes: 63 additions & 0 deletions runtime/integration-test/src/proof_of_stake.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
use crate::setup::*;
use frame_support::traits::Hooks;
use orml_tokens::MultiTokenCurrencyExtended;

type TokensOf<Test> = <Test as pallet_proof_of_stake::Config>::Currency;

fn forward_to_block(n: u32) {
while frame_system::Pallet::<Runtime>::block_number() < n {
let i = frame_system::Pallet::<Runtime>::block_number() + 1;
frame_system::Pallet::<Runtime>::set_block_number(i);

frame_system::Pallet::<Runtime>::on_initialize(i);
parachain_staking::Pallet::<Runtime>::on_initialize(i);
pallet_session::Pallet::<Runtime>::on_initialize(i);

pallet_session::Pallet::<Runtime>::on_finalize(i);
parachain_staking::Pallet::<Runtime>::on_initialize(i);
frame_system::Pallet::<Runtime>::on_finalize(i);
}
}

#[test]
fn rewards_are_aligned_with_sessions() {
ExtBuilder::default().build().execute_with(|| {
let alice: sp_runtime::AccountId32 = [0u8; 32].into();
let bob: sp_runtime::AccountId32 = [1u8; 32].into();
let charlie: sp_runtime::AccountId32 = [2u8; 32].into();
let amount: u128 = 100_000u128;
let blocks_per_round = <Runtime as parachain_staking::Config>::BlocksPerRound::get();

let token = TokensOf::<Runtime>::create(&alice, amount).unwrap();
TokensOf::<Runtime>::mint(token, &bob, amount).unwrap();
TokensOf::<Runtime>::mint(token, &charlie, amount).unwrap();

assert_eq!(0, pallet_session::Pallet::<Runtime>::current_index());
ProofOfStake::update_pool_promotion(RuntimeOrigin::root(), token, 1u8).unwrap();
ProofOfStake::activate_liquidity(RuntimeOrigin::signed(alice.clone()), token, amount, None)
.unwrap();

forward_to_block(blocks_per_round - 10);
assert_eq!(0, pallet_session::Pallet::<Runtime>::current_index());
ProofOfStake::activate_liquidity(
RuntimeOrigin::signed(charlie.clone()),
token,
amount,
None,
)
.unwrap();

forward_to_block(blocks_per_round - 2);
assert_eq!(0, pallet_session::Pallet::<Runtime>::current_index());
ProofOfStake::activate_liquidity(RuntimeOrigin::signed(bob.clone()), token, amount, None)
.unwrap();

forward_to_block(blocks_per_round - 1);
assert_eq!(1, pallet_session::Pallet::<Runtime>::current_index());

assert_eq!(
ProofOfStake::get_rewards_info(charlie.clone(), token),
ProofOfStake::get_rewards_info(bob.clone(), token)
);
});
}
8 changes: 6 additions & 2 deletions runtime/integration-test/src/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ mod kusama_imports {
};
pub use mangata_kusama_runtime::{
xcm_config::*, AccountId, AssetRegistry, Balance, Bootstrap, CustomMetadata, Identity,
PolkadotXcm, Proxy, Runtime, RuntimeCall, RuntimeOrigin, System, TokenId, Tokens,
VersionedMultiLocation, XTokens, XcmMetadata, XcmpQueue, Xyk, XykMetadata,
PolkadotXcm, ProofOfStake, Proxy, Runtime, RuntimeCall, RuntimeOrigin, System, TokenId,
Tokens, VersionedMultiLocation, XTokens, XcmMetadata, XcmpQueue, Xyk, XykMetadata,
};
pub use xcm::latest::Weight as XcmWeight;

Expand Down Expand Up @@ -91,6 +91,10 @@ impl ExtBuilder {
.assimilate_storage(&mut t)
.unwrap();

parachain_staking::GenesisConfig::<Runtime>::default()
.assimilate_storage(&mut t)
.unwrap();

let encoded: Vec<(TokenId, Vec<u8>)> = self
.assets
.iter()
Expand Down

0 comments on commit 504ec52

Please sign in to comment.