Skip to content

Commit

Permalink
Add statemint as trustable chain for pendulum + integration tests (#192)
Browse files Browse the repository at this point in the history
* update pendulum runtime to support XCM reserve transfer for USDT

* add pendulum integration tests for XCM (#190)

add pendulum integration tests

* add polkadot runtime to integration tests

* add xcm test configuration

* fix type XcmConfig instead of XCMConfig

* add relay chain + parachain base integration tests

* add Pendulum parachain to decl_test_network in xcm integration tests

* Configure pendulum runtime for xcm simulator (#193)

configure pendulum runtime for xcm simulator

* Add xcm tests for pendulum (#195)

* finished configuration of polkadot + pendulum TestNet

* add transfer_ksm_from_relay_chain_to_pendulum

* add unit tests to integration

* use xcm emulator instead of xcm simulator from polkadot repo. (#194)

* transfer_polkadot_from_relay_chain_to_pendulum works

* added test for transfer polkadot_from_pendulum_to_relay_chain

* Statemint integration tests to cover USDT bidirectional reserve transfer Pendulum <-> Statemint (#196)

* add statemint-runtime to integration tests

* add Statemint to RelayNet as system chain 1000

* add statemine_transfer_asset_to_pendulum unit tests

* check BOB balance in statemine_transfer_asset_to_pendulum test

* add statemine_transfer_asset_to_statemint

* rename statemine to statemint. because this is tests for polkadot net

* add statemint_transfer_incorrect_asset_to_pendulum_fails

* move pendulum and statemint configuration from lib.rs to setup.rs

* move test from lib.rs to test.rs

* move polkadot net configuration to polkadot_test_net

* split tests, setup and polkadot_test_net to different files

* remove unused import in all files

* rename to pendulum-runtime-integration-tests

* Revert "rename to pendulum-runtime-integration-tests"

This reverts commit 2f18be6.

* rename to runtime-integration-tests

* Final refactor xcm integration tests (#198)

* rename dot function ot one

* refactor setup rs file

* add #[cfg(test)] attribute for all modules

* add comments about fees and rename TEN to TEN_UNITS

* update comment and constants name to show what is it

* wrap Parachain into X1 to have the same approach everywhere

* update transfer_dot_from_pendulum_to_relay_chain assert statement

* add. comment statemint_transfer_incorrect_asset_to_pendulum_should_fails

* add comment statemint_transfer_asset_to_statemint

* move use pendulum_runtime::{RuntimeEvent, System}; to top of the file

* move comment up function statemint_transfer_incorrect_asset_to_pendulum

* renamed to renamed to fn units(amount)

* wrap AccountId32 to X1 when call reserve_transfer_assets

* make Foucoco xcm config identical to pendulum runtime (#199)

make foucoco xcm config identical to pendulum runtime

* remove duplicate of "runtime/development"
  • Loading branch information
RustNinja authored Apr 7, 2023
1 parent 1dbf537 commit a75ef03
Show file tree
Hide file tree
Showing 10 changed files with 1,241 additions and 228 deletions.
737 changes: 523 additions & 214 deletions Cargo.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ members = [
"runtime/foucoco",
"runtime/pendulum",
"runtime/development",
"runtime/integration-tests/pendulum",
]
34 changes: 27 additions & 7 deletions runtime/foucoco/src/xcm_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,23 @@ use frame_support::{
log, match_types, parameter_types,
traits::{Everything, Nothing},
};
use orml_traits::{location::AbsoluteReserveProvider, parameter_type_with_key};
use orml_traits::{
location::{RelativeReserveProvider, Reserve},
parameter_type_with_key,
};
use pallet_xcm::XcmPassthrough;
use polkadot_parachain::primitives::Sibling;
use polkadot_runtime_common::impls::ToAuthor;
use sp_runtime::traits::Convert;
use xcm::latest::{prelude::*, Weight as XCMWeight};
use xcm_builder::{
AccountId32Aliases, AllowUnpaidExecutionFrom, ConvertedConcreteAssetId, EnsureXcmOrigin,
FixedWeightBounds, FungiblesAdapter, LocationInverter, NativeAsset, ParentIsPreset,
RelayChainAsNative, SiblingParachainAsNative, SiblingParachainConvertsVia,
SignedAccountId32AsNative, SignedToAccountId32, SovereignSignedViaLocation, UsingComponents,
FixedWeightBounds, FungiblesAdapter, LocationInverter, ParentIsPreset, RelayChainAsNative,
SiblingParachainAsNative, SiblingParachainConvertsVia, SignedAccountId32AsNative,
SignedToAccountId32, SovereignSignedViaLocation, UsingComponents,
};
use xcm_executor::{
traits::{JustTry, ShouldExecute},
traits::{FilterAssetLocation, JustTry, ShouldExecute},
XcmExecutor,
};

Expand Down Expand Up @@ -94,6 +97,23 @@ impl xcm_executor::traits::Convert<MultiLocation, CurrencyId> for CurrencyIdConv
}
}

/// A `FilterAssetLocation` implementation. Filters multi native assets whose
/// reserve is same with `origin`.
pub struct MultiNativeAsset<ReserveProvider>(PhantomData<ReserveProvider>);
impl<ReserveProvider> FilterAssetLocation for MultiNativeAsset<ReserveProvider>
where
ReserveProvider: Reserve,
{
fn filter_asset_location(asset: &MultiAsset, origin: &MultiLocation) -> bool {
if let Some(ref reserve) = ReserveProvider::reserve(asset) {
if reserve == origin {
return true
}
}
false
}
}

/// Means for transacting the fungibles assets of ths parachain.
pub type FungiblesTransactor = FungiblesAdapter<
// Use this fungibles implementation
Expand Down Expand Up @@ -220,7 +240,7 @@ impl xcm_executor::Config for XcmConfig {
// How to withdraw and deposit an asset.
type AssetTransactor = FungiblesTransactor;
type OriginConverter = XcmOriginToTransactDispatchOrigin;
type IsReserve = NativeAsset;
type IsReserve = MultiNativeAsset<RelativeReserveProvider>;
type IsTeleporter = (); // Teleporting is disabled.
type LocationInverter = LocationInverter<Ancestry>;
type Barrier = Barrier;
Expand Down Expand Up @@ -286,7 +306,7 @@ impl orml_xtokens::Config for Runtime {
type MaxAssetsForTransfer = MaxAssetsForTransfer;
type MinXcmFee = ParachainMinFee; //TODO to support hrmp transfer beetween parachain adjust this parameter
type MultiLocationsFilter = Everything;
type ReserveProvider = AbsoluteReserveProvider;
type ReserveProvider = RelativeReserveProvider;
}

pub struct AccountIdToMultiLocation;
Expand Down
42 changes: 42 additions & 0 deletions runtime/integration-tests/pendulum/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
[package]
name = "runtime-integration-tests"
description = "Pendulum runtime integration tests"
authors = ["Pendulum"]
edition = "2021"
version = "0.1.0"

[dependencies]
codec = { package = "parity-scale-codec", version = "3.0.0" }
scale-info = { version = "2.1.2", features = ["derive"] }

frame-system = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.37" }
frame-support = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.37" }
pallet-balances = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.37" }
sp-std = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.37" }
sp-core = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.37" }
sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.37" }
sp-io = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.37" }
parachain-info = { git = "https://github.com/paritytech/cumulus", branch = "polkadot-v0.9.37" }

xcm-simulator = { git = "https://github.com/paritytech/polkadot", branch = "release-v0.9.37" }
xcm = { git = "https://github.com/paritytech/polkadot", branch = "release-v0.9.37" }
xcm-executor = { git = "https://github.com/paritytech/polkadot", branch = "release-v0.9.37" }
xcm-builder = { git = "https://github.com/paritytech/polkadot", branch = "release-v0.9.37" }
pallet-xcm = { git = "https://github.com/paritytech/polkadot", branch = "release-v0.9.37" }
polkadot-core-primitives = { git = "https://github.com/paritytech/polkadot", branch = "release-v0.9.37" }
polkadot-runtime-parachains = { git = "https://github.com/paritytech/polkadot", branch = "release-v0.9.37" }
polkadot-parachain = { git = "https://github.com/paritytech/polkadot", branch = "release-v0.9.37" }
polkadot-primitives = { git = "https://github.com/paritytech/polkadot", branch = "release-v0.9.37" }
polkadot-runtime = { git = "https://github.com/paritytech/polkadot", branch = "release-v0.9.37" }
polkadot-runtime-common = { git = "https://github.com/paritytech/polkadot", branch = "release-v0.9.37" }

xcm-emulator = { git = "https://github.com/shaunxw/xcm-simulator", rev = "6847a58888e483f0ed2e0b72f90e00767ea0ecac" }

orml-tokens = {git = "https://github.com/open-web3-stack/open-runtime-module-library.git", default-features = false, branch = "polkadot-v0.9.37" }
cumulus-pallet-dmp-queue = {git = "https://github.com/paritytech/cumulus", default-features = false, branch = "polkadot-v0.9.37"}
orml-xtokens = { git = "https://github.com/open-web3-stack/open-runtime-module-library", default-features = false, branch = "polkadot-v0.9.37" }
pendulum-runtime = { path = "../../pendulum"}

statemint-runtime = { git = "https://github.com/paritytech/cumulus", branch = "polkadot-v0.9.37" }

cumulus-pallet-xcmp-queue = {git = "https://github.com/paritytech/cumulus", default-features = false, branch = "polkadot-v0.9.37"}
8 changes: 8 additions & 0 deletions runtime/integration-tests/pendulum/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#[cfg(test)]
mod polkadot_test_net;

#[cfg(test)]
mod setup;

#[cfg(test)]
mod tests;
129 changes: 129 additions & 0 deletions runtime/integration-tests/pendulum/src/polkadot_test_net.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
use crate::setup::{units, ExtBuilderPendulum, ExtStatemintBuilder, ALICE, BOB};
use frame_support::traits::GenesisBuild;
use polkadot_core_primitives::{AccountId, BlockNumber};
use polkadot_parachain::primitives::Id as ParaId;
use polkadot_primitives::v2::{MAX_CODE_SIZE, MAX_POV_SIZE};
use polkadot_runtime_parachains::configuration::HostConfiguration;
use sp_runtime::traits::AccountIdConversion;
use xcm_emulator::{decl_test_network, decl_test_parachain, decl_test_relay_chain, Weight};

decl_test_relay_chain! {
pub struct Relay {
Runtime = polkadot_runtime::Runtime,
XcmConfig = polkadot_runtime::xcm_config::XcmConfig,
new_ext = relay_ext(),
}
}

decl_test_parachain! {
pub struct PendulumParachain {
Runtime = pendulum_runtime::Runtime,
RuntimeOrigin = pendulum_runtime::RuntimeOrigin,
XcmpMessageHandler = pendulum_runtime::XcmpQueue,
DmpMessageHandler = pendulum_runtime::DmpQueue,
new_ext = para_ext_pendulum(2094),
}
}

decl_test_parachain! {
pub struct Statemint {
Runtime = statemint_runtime::Runtime,
RuntimeOrigin = statemint_runtime::RuntimeOrigin,
XcmpMessageHandler = statemint_runtime::XcmpQueue,
DmpMessageHandler = statemint_runtime::DmpQueue,
new_ext = para_ext_statemint(1000),
}
}

decl_test_network! {
pub struct MockNet {
relay_chain = Relay,
parachains = vec![
(1000, Statemint),
(2094, PendulumParachain),
],
}
}

pub fn para_account_id(id: u32) -> polkadot_core_primitives::AccountId {
ParaId::from(id).into_account_truncating()
}

pub fn relay_ext() -> sp_io::TestExternalities {
use polkadot_runtime::{Runtime, System};
let mut t = frame_system::GenesisConfig::default().build_storage::<Runtime>().unwrap();
pallet_balances::GenesisConfig::<Runtime> {
balances: vec![
(AccountId::from(ALICE), units(100000)),
(AccountId::from(BOB), units(100)),
(para_account_id(2094), 10 * units(100000)),
],
}
.assimilate_storage(&mut t)
.unwrap();
polkadot_runtime_parachains::configuration::GenesisConfig::<Runtime> {
config: default_parachains_host_configuration(),
}
.assimilate_storage(&mut t)
.unwrap();
<pallet_xcm::GenesisConfig as GenesisBuild<Runtime>>::assimilate_storage(
&pallet_xcm::GenesisConfig { safe_xcm_version: Some(2) },
&mut t,
)
.unwrap();
let mut ext = sp_io::TestExternalities::new(t);
ext.execute_with(|| System::set_block_number(1));
ext
}

fn default_parachains_host_configuration() -> HostConfiguration<BlockNumber> {
HostConfiguration {
minimum_validation_upgrade_delay: 5,
validation_upgrade_cooldown: 5u32,
validation_upgrade_delay: 5,
code_retention_period: 1200,
max_code_size: MAX_CODE_SIZE,
max_pov_size: MAX_POV_SIZE,
max_head_data_size: 32 * 1024,
group_rotation_frequency: 20,
chain_availability_period: 4,
thread_availability_period: 4,
max_upward_queue_count: 8,
max_upward_queue_size: 1024 * 1024,
max_downward_message_size: 1024,
ump_service_total_weight: Weight::from_ref_time(4 * 1_000_000_000),
max_upward_message_size: 50 * 1024,
max_upward_message_num_per_candidate: 5,
hrmp_sender_deposit: 0,
hrmp_recipient_deposit: 0,
hrmp_channel_max_capacity: 8,
hrmp_channel_max_total_size: 8 * 1024,
hrmp_max_parachain_inbound_channels: 4,
hrmp_max_parathread_inbound_channels: 4,
hrmp_channel_max_message_size: 1024 * 1024,
hrmp_max_parachain_outbound_channels: 4,
hrmp_max_parathread_outbound_channels: 4,
hrmp_max_message_num_per_candidate: 5,
dispute_period: 6,
no_show_slots: 2,
n_delay_tranches: 25,
needed_approvals: 2,
relay_vrf_modulo_samples: 2,
zeroth_delay_tranche_width: 0,
..Default::default()
}
}

pub fn para_ext_pendulum(parachain_id: u32) -> sp_io::TestExternalities {
ExtBuilderPendulum::default()
.balances(vec![])
.parachain_id(parachain_id)
.build()
}

pub fn para_ext_statemint(parachain_id: u32) -> sp_io::TestExternalities {
ExtStatemintBuilder::default()
.balances(vec![])
.parachain_id(parachain_id)
.build()
}
117 changes: 117 additions & 0 deletions runtime/integration-tests/pendulum/src/setup.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
use frame_support::traits::GenesisBuild;
use pendulum_runtime::{PendulumCurrencyId, Runtime, System};
use polkadot_core_primitives::{AccountId, Balance};

pub fn units(amount: Balance) -> Balance {
amount * 10u128.saturating_pow(9)
}

pub const ALICE: [u8; 32] = [4u8; 32];
pub const BOB: [u8; 32] = [5u8; 32];
pub const INITIAL_BALANCE: u128 = 1_000_000_000;

pub struct ExtBuilderPendulum {
balances: Vec<(AccountId, PendulumCurrencyId, Balance)>,
parachain_id: u32,
}

impl Default for ExtBuilderPendulum {
fn default() -> Self {
Self { balances: vec![], parachain_id: 2094 }
}
}

impl ExtBuilderPendulum {
pub fn balances(mut self, balances: Vec<(AccountId, PendulumCurrencyId, Balance)>) -> Self {
self.balances = balances;
self
}

pub fn parachain_id(mut self, parachain_id: u32) -> Self {
self.parachain_id = parachain_id;
self
}

pub fn build(self) -> sp_io::TestExternalities {
let mut t = frame_system::GenesisConfig::default().build_storage::<Runtime>().unwrap();
pallet_balances::GenesisConfig::<Runtime> {
balances: vec![
(AccountId::from(ALICE), INITIAL_BALANCE),
(AccountId::from(BOB), INITIAL_BALANCE),
],
}
.assimilate_storage(&mut t)
.unwrap();

orml_tokens::GenesisConfig::<Runtime> {
balances: vec![(AccountId::from(BOB), PendulumCurrencyId::XCM(0), units(100))],
}
.assimilate_storage(&mut t)
.unwrap();
<parachain_info::GenesisConfig as GenesisBuild<Runtime>>::assimilate_storage(
&parachain_info::GenesisConfig { parachain_id: self.parachain_id.into() },
&mut t,
)
.unwrap();
<pallet_xcm::GenesisConfig as GenesisBuild<Runtime>>::assimilate_storage(
&pallet_xcm::GenesisConfig { safe_xcm_version: Some(2) },
&mut t,
)
.unwrap();
let mut ext = sp_io::TestExternalities::new(t);
ext.execute_with(|| System::set_block_number(1));
ext
}
}

pub struct ExtStatemintBuilder {
balances: Vec<(AccountId, u128, Balance)>,
parachain_id: u32,
}

impl Default for ExtStatemintBuilder {
fn default() -> Self {
Self { balances: vec![], parachain_id: 1000 }
}
}

impl ExtStatemintBuilder {
pub fn balances(mut self, balances: Vec<(AccountId, u128, Balance)>) -> Self {
self.balances = balances;
self
}

#[allow(dead_code)]
pub fn parachain_id(mut self, parachain_id: u32) -> Self {
self.parachain_id = parachain_id;
self
}

pub fn build(self) -> sp_io::TestExternalities {
use statemint_runtime::Runtime as StatemintRuntime;

let mut t = frame_system::GenesisConfig::default()
.build_storage::<StatemintRuntime>()
.unwrap();

pallet_balances::GenesisConfig::<StatemintRuntime> { balances: vec![] }
.assimilate_storage(&mut t)
.unwrap();

<parachain_info::GenesisConfig as GenesisBuild<StatemintRuntime>>::assimilate_storage(
&parachain_info::GenesisConfig { parachain_id: self.parachain_id.into() },
&mut t,
)
.unwrap();

<pallet_xcm::GenesisConfig as GenesisBuild<Runtime>>::assimilate_storage(
&pallet_xcm::GenesisConfig { safe_xcm_version: Some(2) },
&mut t,
)
.unwrap();

let mut ext = sp_io::TestExternalities::new(t);
ext.execute_with(|| System::set_block_number(1));
ext
}
}
Loading

0 comments on commit a75ef03

Please sign in to comment.