Unverified Commit 4b2bd54a authored by Shaun W's avatar Shaun W Committed by GitHub
Browse files

XCM simulator (#3538)

* Add xcm-simulator and xcm-simulator-example.

* Abstract xcmp and dmp handling.

* Use mock message queue.

* Xcm simulator example unit tests.

* Use relay chain block number on sending msg.

* Fix typo.

* fmt

* more fmt

* Fix deps.
parent 18de14db
Pipeline #150421 passed with stages
in 43 minutes and 13 seconds
......@@ -6701,6 +6701,7 @@ name = "polkadot-parachain"
version = "0.9.8"
dependencies = [
"derive_more",
"frame-support",
"parity-scale-codec",
"parity-util-mem",
"polkadot-core-primitives",
......@@ -10660,9 +10661,9 @@ checksum = "502d53007c02d7605a05df1c1a73ee436952781653da5d0bf57ad608f66932c1"
[[package]]
name = "syn"
version = "1.0.67"
version = "1.0.74"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6498a9efc342871f91cc2d0d694c674368b4ceb40f62b65a7a08c3792935e702"
checksum = "1873d832550d4588c3dbc20f01361ab00bfe741048f71e3fecf145a7cc18b29c"
dependencies = [
"proc-macro2",
"quote",
......@@ -12303,6 +12304,45 @@ dependencies = [
"xcm",
]
[[package]]
name = "xcm-simulator"
version = "0.9.8"
dependencies = [
"frame-support",
"parity-scale-codec",
"paste",
"polkadot-core-primitives",
"polkadot-parachain",
"polkadot-runtime-parachains",
"sp-io",
"sp-std",
"xcm",
"xcm-executor",
]
[[package]]
name = "xcm-simulator-example"
version = "0.9.8"
dependencies = [
"frame-support",
"frame-system",
"pallet-balances",
"pallet-xcm",
"parity-scale-codec",
"paste",
"polkadot-core-primitives",
"polkadot-parachain",
"polkadot-runtime-parachains",
"sp-core",
"sp-io",
"sp-runtime",
"sp-std",
"xcm",
"xcm-builder",
"xcm-executor",
"xcm-simulator",
]
[[package]]
name = "yamux"
version = "0.9.0"
......
......@@ -39,6 +39,8 @@ members = [
"xcm",
"xcm/xcm-builder",
"xcm/xcm-executor",
"xcm/xcm-simulator",
"xcm/xcm-simulator/example",
"xcm/pallet-xcm",
"node/client",
"node/collation-generation",
......
......@@ -14,6 +14,7 @@ parity-util-mem = { version = "0.10.0", optional = true }
sp-std = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
sp-core = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
frame-support = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
polkadot-core-primitives = { path = "../core-primitives", default-features = false }
derive_more = "0.99.11"
......
......@@ -22,6 +22,7 @@ use sp_std::vec::Vec;
use parity_scale_codec::{Encode, Decode, CompactAs};
use sp_core::{RuntimeDebug, TypeId};
use sp_runtime::traits::Hash as _;
use frame_support::weights::Weight;
#[cfg(feature = "std")]
use serde::{Serialize, Deserialize};
......@@ -318,6 +319,59 @@ pub struct HrmpChannelId {
/// A message from a parachain to its Relay Chain.
pub type UpwardMessage = Vec<u8>;
/// Something that should be called when a downward message is received.
pub trait DmpMessageHandler {
/// Handle some incoming DMP messages (note these are individual XCM messages).
///
/// Also, process messages up to some `max_weight`.
fn handle_dmp_messages(
iter: impl Iterator<Item = (RelayChainBlockNumber, Vec<u8>)>,
max_weight: Weight,
) -> Weight;
}
impl DmpMessageHandler for () {
fn handle_dmp_messages(
iter: impl Iterator<Item = (RelayChainBlockNumber, Vec<u8>)>,
_max_weight: Weight,
) -> Weight {
iter.for_each(drop);
0
}
}
/// The aggregate XCMP message format.
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Encode, Decode)]
pub enum XcmpMessageFormat {
/// Encoded `VersionedXcm` messages, all concatenated.
ConcatenatedVersionedXcm,
/// Encoded `Vec<u8>` messages, all concatenated.
ConcatenatedEncodedBlob,
/// One or more channel control signals; these should be interpreted immediately upon receipt
/// from the relay-chain.
Signals,
}
/// Something that should be called for each batch of messages received over XCMP.
pub trait XcmpMessageHandler {
/// Handle some incoming XCMP messages (note these are the big one-per-block aggregate
/// messages).
///
/// Also, process messages up to some `max_weight`.
fn handle_xcmp_messages<'a, I: Iterator<Item = (Id, RelayChainBlockNumber, &'a [u8])>>(
iter: I,
max_weight: Weight,
) -> Weight;
}
impl XcmpMessageHandler for () {
fn handle_xcmp_messages<'a, I: Iterator<Item = (Id, RelayChainBlockNumber, &'a [u8])>>(
iter: I,
_max_weight: Weight,
) -> Weight {
for _ in iter {}
0
}
}
/// Validation parameters for evaluating the parachain validity function.
// TODO: balance downloads (https://github.com/paritytech/polkadot/issues/220)
#[derive(PartialEq, Eq, Decode, Clone)]
......
[package]
name = "xcm"
version = "0.9.8"
authors = ["Parity Technologies x<admin@parity.io>"]
authors = ["Parity Technologies <admin@parity.io>"]
description = "The basic XCM datastructures."
edition = "2018"
......
[package]
name = "xcm-simulator"
version = "0.9.8"
authors = ["Parity Technologies <admin@parity.io>"]
description = "Test kit to simulate cross-chain message passing and XCM execution"
edition = "2018"
[dependencies]
codec = { package = "parity-scale-codec", version = "2.0.0" }
paste = "1.0.5"
frame-support = { git = "https://github.com/paritytech/substrate", branch = "master" }
sp-io = { git = "https://github.com/paritytech/substrate", branch = "master" }
sp-std = { git = "https://github.com/paritytech/substrate", branch = "master" }
xcm = { path = "../" }
xcm-executor = { path = "../xcm-executor" }
polkadot-core-primitives = { path = "../../core-primitives"}
polkadot-parachain = { path = "../../parachain" }
polkadot-runtime-parachains = { path = "../../runtime/parachains" }
[package]
name = "xcm-simulator-example"
version = "0.9.8"
authors = ["Parity Technologies <admin@parity.io>"]
description = "Examples of xcm-simulator usage."
edition = "2018"
[dependencies]
codec = { package = "parity-scale-codec", version = "2.0.0" }
paste = "1.0.5"
frame-system = { git = "https://github.com/paritytech/substrate", branch = "master" }
frame-support = { git = "https://github.com/paritytech/substrate", branch = "master" }
pallet-balances = { git = "https://github.com/paritytech/substrate", branch = "master" }
sp-std = { git = "https://github.com/paritytech/substrate", branch = "master" }
sp-core = { git = "https://github.com/paritytech/substrate", branch = "master" }
sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "master" }
sp-io = { git = "https://github.com/paritytech/substrate", branch = "master" }
xcm = { path = "../../" }
xcm-simulator = { path = "../" }
xcm-executor = { path = "../../xcm-executor" }
xcm-builder = { path = "../../xcm-builder" }
pallet-xcm = { path = "../../pallet-xcm" }
polkadot-core-primitives = { path = "../../../core-primitives"}
polkadot-runtime-parachains = { path = "../../../runtime/parachains" }
polkadot-parachain = { path = "../../../parachain" }
\ No newline at end of file
// Copyright 2021 Parity Technologies (UK) Ltd.
// This file is part of Polkadot.
// Polkadot 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.
// Polkadot 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 Polkadot. If not, see <http://www.gnu.org/licenses/>.
mod parachain;
mod relay_chain;
use sp_runtime::AccountId32;
use xcm_simulator::{decl_test_network, decl_test_parachain, decl_test_relay_chain};
pub const ALICE: AccountId32 = AccountId32::new([0u8; 32]);
decl_test_parachain! {
pub struct ParaA {
Runtime = parachain::Runtime,
XcmpMessageHandler = parachain::MsgQueue,
DmpMessageHandler = parachain::MsgQueue,
new_ext = para_ext(1),
}
}
decl_test_parachain! {
pub struct ParaB {
Runtime = parachain::Runtime,
XcmpMessageHandler = parachain::MsgQueue,
DmpMessageHandler = parachain::MsgQueue,
new_ext = para_ext(2),
}
}
decl_test_relay_chain! {
pub struct Relay {
Runtime = relay_chain::Runtime,
XcmConfig = relay_chain::XcmConfig,
new_ext = relay_ext(),
}
}
decl_test_network! {
pub struct MockNet {
relay_chain = Relay,
parachains = vec![
(1, ParaA),
(2, ParaB),
],
}
}
pub const INITIAL_BALANCE: u128 = 1_000_000_000;
pub fn para_ext(para_id: u32) -> sp_io::TestExternalities {
use parachain::{MsgQueue, Runtime, System};
let mut t = frame_system::GenesisConfig::default().build_storage::<Runtime>().unwrap();
pallet_balances::GenesisConfig::<Runtime> { balances: vec![(ALICE, INITIAL_BALANCE)] }
.assimilate_storage(&mut t)
.unwrap();
let mut ext = sp_io::TestExternalities::new(t);
ext.execute_with(|| {
System::set_block_number(1);
MsgQueue::set_para_id(para_id.into());
});
ext
}
pub fn relay_ext() -> sp_io::TestExternalities {
use relay_chain::{Runtime, System};
let mut t = frame_system::GenesisConfig::default().build_storage::<Runtime>().unwrap();
pallet_balances::GenesisConfig::<Runtime> { balances: vec![(ALICE, INITIAL_BALANCE)] }
.assimilate_storage(&mut t)
.unwrap();
let mut ext = sp_io::TestExternalities::new(t);
ext.execute_with(|| System::set_block_number(1));
ext
}
pub type RelayChainPalletXcm = pallet_xcm::Pallet<relay_chain::Runtime>;
pub type ParachainPalletXcm = pallet_xcm::Pallet<parachain::Runtime>;
#[cfg(test)]
mod tests {
use super::*;
use codec::Encode;
use frame_support::assert_ok;
use xcm::v0::{
Junction::{self, Parachain, Parent},
MultiAsset::*,
MultiLocation::*,
NetworkId, OriginKind,
Xcm::*,
};
use xcm_simulator::TestExt;
#[test]
fn dmp() {
MockNet::reset();
let remark = parachain::Call::System(
frame_system::Call::<parachain::Runtime>::remark_with_event(vec![1, 2, 3]),
);
Relay::execute_with(|| {
assert_ok!(RelayChainPalletXcm::send_xcm(
Null,
X1(Parachain(1)),
Transact {
origin_type: OriginKind::SovereignAccount,
require_weight_at_most: INITIAL_BALANCE as u64,
call: remark.encode().into(),
},
));
});
ParaA::execute_with(|| {
use parachain::{Event, System};
assert!(System::events()
.iter()
.any(|r| matches!(r.event, Event::System(frame_system::Event::Remarked(_, _)))));
});
}
#[test]
fn ump() {
MockNet::reset();
let remark = relay_chain::Call::System(
frame_system::Call::<relay_chain::Runtime>::remark_with_event(vec![1, 2, 3]),
);
ParaA::execute_with(|| {
assert_ok!(ParachainPalletXcm::send_xcm(
Null,
X1(Parent),
Transact {
origin_type: OriginKind::SovereignAccount,
require_weight_at_most: INITIAL_BALANCE as u64,
call: remark.encode().into(),
},
));
});
Relay::execute_with(|| {
use relay_chain::{Event, System};
assert!(System::events()
.iter()
.any(|r| matches!(r.event, Event::System(frame_system::Event::Remarked(_, _)))));
});
}
#[test]
fn xcmp() {
MockNet::reset();
let remark = parachain::Call::System(
frame_system::Call::<parachain::Runtime>::remark_with_event(vec![1, 2, 3]),
);
ParaA::execute_with(|| {
assert_ok!(ParachainPalletXcm::send_xcm(
Null,
X2(Parent, Parachain(2)),
Transact {
origin_type: OriginKind::SovereignAccount,
require_weight_at_most: INITIAL_BALANCE as u64,
call: remark.encode().into(),
},
));
});
ParaB::execute_with(|| {
use parachain::{Event, System};
assert!(System::events()
.iter()
.any(|r| matches!(r.event, Event::System(frame_system::Event::Remarked(_, _)))));
});
}
#[test]
fn reserve_transfer() {
MockNet::reset();
Relay::execute_with(|| {
assert_ok!(RelayChainPalletXcm::reserve_transfer_assets(
relay_chain::Origin::signed(ALICE),
X1(Parachain(1)),
X1(Junction::AccountId32 { network: NetworkId::Any, id: ALICE.into() }),
vec![ConcreteFungible { id: Null, amount: 123 }],
123,
));
});
ParaA::execute_with(|| {
// free execution, full amount received
assert_eq!(
pallet_balances::Pallet::<parachain::Runtime>::free_balance(&ALICE),
INITIAL_BALANCE + 123
);
});
}
}
// Copyright 2021 Parity Technologies (UK) Ltd.
// This file is part of Polkadot.
// Polkadot 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.
// Polkadot 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 Polkadot. If not, see <http://www.gnu.org/licenses/>.
//! Parachain runtime mock.
use codec::{Decode, Encode};
use frame_support::{
construct_runtime, parameter_types,
traits::{All, AllowAll},
weights::{constants::WEIGHT_PER_SECOND, Weight},
};
use sp_core::H256;
use sp_runtime::{
testing::Header,
traits::{Hash, IdentityLookup},
AccountId32,
};
use sp_std::{convert::TryFrom, prelude::*};
use pallet_xcm::XcmPassthrough;
use polkadot_core_primitives::BlockNumber as RelayBlockNumber;
use polkadot_parachain::primitives::{
DmpMessageHandler, Id as ParaId, Sibling, XcmpMessageFormat, XcmpMessageHandler,
};
use xcm::{
v0::{
Error as XcmError, ExecuteXcm,
Junction::{Parachain, Parent},
MultiAsset,
MultiLocation::{self, X1},
NetworkId, Outcome, Xcm,
},
VersionedXcm,
};
use xcm_builder::{
AccountId32Aliases, AllowUnpaidExecutionFrom, CurrencyAdapter as XcmCurrencyAdapter,
EnsureXcmOrigin, FixedRateOfConcreteFungible, FixedWeightBounds, IsConcrete, LocationInverter,
NativeAsset, ParentIsDefault, SiblingParachainConvertsVia, SignedAccountId32AsNative,
SignedToAccountId32, SovereignSignedViaLocation,
};
use xcm_executor::{Config, XcmExecutor};
pub type AccountId = AccountId32;
pub type Balance = u128;
parameter_types! {
pub const BlockHashCount: u64 = 250;
}
impl frame_system::Config for Runtime {
type Origin = Origin;
type Call = Call;
type Index = u64;
type BlockNumber = u64;
type Hash = H256;
type Hashing = ::sp_runtime::traits::BlakeTwo256;
type AccountId = AccountId;
type Lookup = IdentityLookup<Self::AccountId>;
type Header = Header;
type Event = Event;
type BlockHashCount = BlockHashCount;
type BlockWeights = ();
type BlockLength = ();
type Version = ();
type PalletInfo = PalletInfo;
type AccountData = pallet_balances::AccountData<Balance>;
type OnNewAccount = ();
type OnKilledAccount = ();
type DbWeight = ();
type BaseCallFilter = AllowAll;
type SystemWeightInfo = ();
type SS58Prefix = ();
type OnSetCode = ();
}
parameter_types! {
pub ExistentialDeposit: Balance = 1;
pub const MaxLocks: u32 = 50;
pub const MaxReserves: u32 = 50;
}
impl pallet_balances::Config for Runtime {
type MaxLocks = MaxLocks;
type Balance = Balance;
type Event = Event;
type DustRemoval = ();
type ExistentialDeposit = ExistentialDeposit;
type AccountStore = System;
type WeightInfo = ();
type MaxReserves = MaxReserves;
type ReserveIdentifier = [u8; 8];
}
parameter_types! {
pub const ReservedXcmpWeight: Weight = WEIGHT_PER_SECOND / 4;
pub const ReservedDmpWeight: Weight = WEIGHT_PER_SECOND / 4;
}
parameter_types! {
pub const KsmLocation: MultiLocation = MultiLocation::X1(Parent);
pub const RelayNetwork: NetworkId = NetworkId::Kusama;
pub Ancestry: MultiLocation = Parachain(MsgQueue::parachain_id().into()).into();
}
pub type LocationToAccountId = (
ParentIsDefault<AccountId>,
SiblingParachainConvertsVia<Sibling, AccountId>,
AccountId32Aliases<RelayNetwork, AccountId>,
);
pub type XcmOriginToCallOrigin = (
SovereignSignedViaLocation<LocationToAccountId, Origin>,
SignedAccountId32AsNative<RelayNetwork, Origin>,
XcmPassthrough<Origin>,
);
parameter_types! {
pub const UnitWeightCost: Weight = 1;
pub KsmPerSecond: (MultiLocation, u128) = (X1(Parent), 1);
}
pub type LocalAssetTransactor =
XcmCurrencyAdapter<Balances, IsConcrete<KsmLocation>, LocationToAccountId, AccountId, ()>;
pub type XcmRouter = super::ParachainXcmRouter<MsgQueue>;
pub type Barrier = AllowUnpaidExecutionFrom<All<MultiLocation>>;
pub struct XcmConfig;
impl Config for XcmConfig {
type Call = Call;
type XcmSender = XcmRouter;
type AssetTransactor = LocalAssetTransactor;
type OriginConverter = XcmOriginToCallOrigin;
type IsReserve = NativeAsset;
type IsTeleporter = ();
type LocationInverter = LocationInverter<Ancestry>;
type Barrier = Barrier;
type Weigher = FixedWeightBounds<UnitWeightCost, Call>;
type Trader = FixedRateOfConcreteFungible<KsmPerSecond, ()>;
type ResponseHandler = ();
}
#[frame_support::pallet]
pub mod mock_msg_queue {
use super::*;
use frame_support::pallet_prelude::*;
#[pallet::config]
pub trait Config: frame_system::Config {
type Event: From<Event<Self>> + IsType<<Self as frame_system::Config>::Event>;
type XcmExecutor: ExecuteXcm<Self::Call>;
}
#[pallet::call]
impl<T: Config> Pallet<T> {}
#[pallet::pallet]
#[pallet::generate_store(pub(super) trait Store)]
pub struct Pallet<T>(_);
#[pallet::storage]
#[pallet::getter(fn parachain_id)]
pub(super) type ParachainId<T: Config> = StorageValue<_, ParaId, ValueQuery>;
impl<T: Config> Get<ParaId> for Pallet<T> {
fn get() -> ParaId {
Self::parachain_id()