From fd2cff11572214f683e8f4f4d60fe5acf8883b34 Mon Sep 17 00:00:00 2001 From: Mateusz Nowakowski Date: Thu, 26 Oct 2023 14:49:26 +0000 Subject: [PATCH] Integration test that verifies PoS checkpoints and chain sessions are aligned --- Cargo.lock | 3 + mvr/runtime/integration-test/Cargo.toml | 1 + runtime/integration-test/Cargo.toml | 3 + runtime/integration-test/src/lib.rs | 3 + .../integration-test/src/proof_of_stake.rs | 63 +++++++++++++++++++ runtime/integration-test/src/setup.rs | 8 ++- 6 files changed, 79 insertions(+), 2 deletions(-) create mode 100644 runtime/integration-test/src/proof_of_stake.rs diff --git a/Cargo.lock b/Cargo.lock index 85ccdcabe6..d76e1b0f16 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -9805,11 +9805,14 @@ dependencies = [ "pallet-identity", "pallet-issuance", "pallet-multipurpose-liquidity", + "pallet-proof-of-stake", "pallet-proxy", + "pallet-session", "pallet-sudo-origin", "pallet-xcm", "pallet-xyk", "parachain-info", + "parachain-staking", "polkadot-parachain", "polkadot-primitives", "polkadot-runtime-parachains", diff --git a/mvr/runtime/integration-test/Cargo.toml b/mvr/runtime/integration-test/Cargo.toml index 4b19e30854..56c918397d 100644 --- a/mvr/runtime/integration-test/Cargo.toml +++ b/mvr/runtime/integration-test/Cargo.toml @@ -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" } diff --git a/runtime/integration-test/Cargo.toml b/runtime/integration-test/Cargo.toml index ab4c2158bb..0417623dc0 100644 --- a/runtime/integration-test/Cargo.toml +++ b/runtime/integration-test/Cargo.toml @@ -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" } @@ -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' } diff --git a/runtime/integration-test/src/lib.rs b/runtime/integration-test/src/lib.rs index 880b19249a..d067bf66cf 100644 --- a/runtime/integration-test/src/lib.rs +++ b/runtime/integration-test/src/lib.rs @@ -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; diff --git a/runtime/integration-test/src/proof_of_stake.rs b/runtime/integration-test/src/proof_of_stake.rs new file mode 100644 index 0000000000..7458de8776 --- /dev/null +++ b/runtime/integration-test/src/proof_of_stake.rs @@ -0,0 +1,63 @@ +use crate::setup::*; +use frame_support::traits::Hooks; +use orml_tokens::MultiTokenCurrencyExtended; + +type TokensOf = ::Currency; + +fn forward_to_block(n: u32) { + while frame_system::Pallet::::block_number() < n { + let i = frame_system::Pallet::::block_number() + 1; + frame_system::Pallet::::set_block_number(i); + + frame_system::Pallet::::on_initialize(i); + parachain_staking::Pallet::::on_initialize(i); + pallet_session::Pallet::::on_initialize(i); + + pallet_session::Pallet::::on_finalize(i); + parachain_staking::Pallet::::on_initialize(i); + frame_system::Pallet::::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 = ::BlocksPerRound::get(); + + let token = TokensOf::::create(&alice, amount).unwrap(); + TokensOf::::mint(token, &bob, amount).unwrap(); + TokensOf::::mint(token, &charlie, amount).unwrap(); + + assert_eq!(0, pallet_session::Pallet::::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::::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::::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::::current_index()); + + assert_eq!( + ProofOfStake::get_rewards_info(charlie.clone(), token), + ProofOfStake::get_rewards_info(bob.clone(), token) + ); + }); +} diff --git a/runtime/integration-test/src/setup.rs b/runtime/integration-test/src/setup.rs index 7a0dcf410d..6bc4dfd02d 100644 --- a/runtime/integration-test/src/setup.rs +++ b/runtime/integration-test/src/setup.rs @@ -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; @@ -91,6 +91,10 @@ impl ExtBuilder { .assimilate_storage(&mut t) .unwrap(); + parachain_staking::GenesisConfig::::default() + .assimilate_storage(&mut t) + .unwrap(); + let encoded: Vec<(TokenId, Vec)> = self .assets .iter()