Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Improve xcm-simulator #3546

Closed
wants to merge 91 commits into from
Closed
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
91 commits
Select commit Hold shift + click to select a range
3e15173
MultiAsset TWO
gavofyork Apr 4, 2021
12054df
Merge remote-tracking branch 'origin/master' into gav-multiasset-2
gavofyork Jul 29, 2021
ee80e03
Draft next MultiAsset API.
gavofyork Jul 30, 2021
2661b91
XCM core builds
gavofyork Jul 30, 2021
6fc030e
XCM Executor builds
gavofyork Jul 30, 2021
8017453
XCM Builder builds
gavofyork Jul 31, 2021
fde986a
init
4meta5 Aug 1, 2021
f806a95
reserve trasfer asset and query holding tests without message checking
4meta5 Aug 1, 2021
eb4224b
XcmSender short circuits on first successful impl for tuples apparently
4meta5 Aug 1, 2021
5445d06
API changes making their way throughout
gavofyork Aug 1, 2021
958d79a
Some TODOs
gavofyork Aug 1, 2021
c65624d
add TestSendXcm XcmRouter to track sent messages
4meta5 Aug 1, 2021
e9da5ea
fix mistake removing local env stuff
4meta5 Aug 1, 2021
abc999f
fix
4meta5 Aug 1, 2021
7d47d21
Further build fixes
gavofyork Aug 1, 2021
bb5bf44
Basic compile builds
gavofyork Aug 1, 2021
acef319
First test fixed
gavofyork Aug 1, 2021
7d700b6
All executor tests fixed
gavofyork Aug 1, 2021
3e62aa3
Typo
gavofyork Aug 1, 2021
ca477f0
Optimize subsume_assets and add test
gavofyork Aug 1, 2021
3cc428f
Optimize checked_sub
gavofyork Aug 1, 2021
099aea7
XCM Builder first test fixed
gavofyork Aug 1, 2021
5158442
Fix builder tests
gavofyork Aug 2, 2021
8b5f212
add ReceivedDmp storage value to MockMsgQueue pallet to store all rec…
4meta5 Aug 2, 2021
6814aa8
Fix doc test
gavofyork Aug 2, 2021
6e4e6db
improve ump docs
apopiak Aug 2, 2021
f0e3ee3
make xcm-simulator XCM sending asynchronous
apopiak Aug 2, 2021
52bc4f0
remove xcm-test-utils
apopiak Aug 2, 2021
529a931
fix some doc tests
gavofyork Aug 2, 2021
3e93d54
spelling
gavofyork Aug 2, 2021
f42b3ab
named fields for AllOf
gavofyork Aug 2, 2021
bfa9913
Update xcm/xcm-simulator/src/lib.rs
apopiak Aug 2, 2021
3f09caa
Merge branch 'apopiak/async-xcm-sim' of https://github.com/paritytech…
4meta5 Aug 2, 2021
86f7d51
use automatic dispatch of message queues to check query response func…
4meta5 Aug 2, 2021
d2b81f9
clean
4meta5 Aug 2, 2021
287cd76
Update xcm/xcm-simulator/src/lib.rs
4meta5 Aug 2, 2021
8305934
Update xcm/xcm-simulator/src/lib.rs
4meta5 Aug 2, 2021
0444d45
Update xcm/xcm-simulator/src/lib.rs
4meta5 Aug 2, 2021
8e60c11
spellcheck nit
4meta5 Aug 2, 2021
35250b5
Merge branch 'amar-more-simulator-tests' of https://github.com/4meta5…
4meta5 Aug 2, 2021
daed1fd
clean
4meta5 Aug 2, 2021
68c725f
fix fmt in macro
4meta5 Aug 2, 2021
fa4bac1
address review comments
4meta5 Aug 3, 2021
97e3bd6
move process all messages to trait implementation so execute with use…
4meta5 Aug 3, 2021
91cccf5
better naming and organization
4meta5 Aug 3, 2021
e5cb209
rm local env
4meta5 Aug 3, 2021
847b99a
fix docs
4meta5 Aug 3, 2021
97051b2
Update xcm/src/v0/multiasset.rs
gavofyork Aug 3, 2021
84b79cf
Update xcm/src/v0/multiasset.rs
gavofyork Aug 3, 2021
1f98872
Update xcm/src/v0/multiasset.rs
gavofyork Aug 3, 2021
0cc61de
Update xcm/src/v0/multiasset.rs
gavofyork Aug 3, 2021
a2dc485
Update xcm/xcm-simulator/example/src/lib.rs
4meta5 Aug 3, 2021
0778e76
follow review suggestions
4meta5 Aug 3, 2021
f164f1e
fix
4meta5 Aug 3, 2021
4f57497
Merge branch 'master' into gav-multiasset-2-reformat
gavofyork Aug 3, 2021
723990c
Reformat
gavofyork Aug 3, 2021
9e2e009
Move to XCM version 1
gavofyork Aug 3, 2021
00ca37b
Spelling
gavofyork Aug 3, 2021
768c96a
warnings
gavofyork Aug 3, 2021
2936419
Replace some more v0->v1s
gavofyork Aug 3, 2021
015fff2
warnings
gavofyork Aug 3, 2021
b5fe03e
format
gavofyork Aug 3, 2021
608a212
Add max_assets param
gavofyork Aug 3, 2021
dee3be5
building
gavofyork Aug 3, 2021
a6eaaa2
test fixes
gavofyork Aug 3, 2021
f58a659
tests
gavofyork Aug 3, 2021
8a78100
another test
gavofyork Aug 3, 2021
6eb75ad
final test
gavofyork Aug 3, 2021
1302cc5
fix names as per suggestion
4meta5 Aug 4, 2021
9d0b9f9
tests
gavofyork Aug 4, 2021
b5b63c8
Rename Null -> Here
gavofyork Aug 4, 2021
e9dbc72
Introduce
gavofyork Aug 4, 2021
36ccb47
More ergonomics
gavofyork Aug 4, 2021
de33d7d
More ergonomics
gavofyork Aug 4, 2021
7ff0493
test fix
gavofyork Aug 4, 2021
d43d987
Merge branch 'gav-multiasset-2' into gav-max-assets
gavofyork Aug 4, 2021
84c44f8
test fixes
gavofyork Aug 4, 2021
7d52b93
docs
gavofyork Aug 4, 2021
51c7d05
BuyExecution includes
gavofyork Aug 4, 2021
851b00e
Fix XCM extrinsics
gavofyork Aug 4, 2021
20bb714
fmt
gavofyork Aug 4, 2021
3f85ebd
Make Vec<MultiAsset>/MultiAssets conversions safe
gavofyork Aug 4, 2021
61a2231
More MultiAssets conversion safety
gavofyork Aug 4, 2021
7646446
Merge remote-tracking branch 'origin/master' into gav-multiasset-2
gavofyork Aug 4, 2021
8644265
spelling
gavofyork Aug 4, 2021
1e75953
fix doc test
gavofyork Aug 4, 2021
669dcb0
Apply suggestions from code review
gavofyork Aug 5, 2021
767bdb5
Apply suggestions from code review
gavofyork Aug 5, 2021
b569f77
xcmv1.into
4meta5 Aug 5, 2021
410266e
fmt
4meta5 Aug 5, 2021
ab8c01b
nit
4meta5 Aug 5, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
148 changes: 140 additions & 8 deletions xcm/xcm-simulator/example/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
mod parachain;
mod relay_chain;

use sp_runtime::AccountId32;
use polkadot_parachain::primitives::Id as ParaId;
use sp_runtime::{traits::AccountIdConversion, AccountId32};
use xcm_simulator::{decl_test_network, decl_test_parachain, decl_test_relay_chain};

pub const ALICE: AccountId32 = AccountId32::new([0u8; 32]);
Expand Down Expand Up @@ -79,12 +80,14 @@ pub fn para_ext(para_id: u32) -> sp_io::TestExternalities {

pub fn relay_ext() -> sp_io::TestExternalities {
use relay_chain::{Runtime, System};
let para_account_a: relay_chain::AccountId = ParaId::from(1).into_account();

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();
pallet_balances::GenesisConfig::<Runtime> {
balances: vec![(ALICE, INITIAL_BALANCE), (para_account_a, INITIAL_BALANCE)],
}
.assimilate_storage(&mut t)
.unwrap();

let mut ext = sp_io::TestExternalities::new(t);
ext.execute_with(|| System::set_block_number(1));
Expand All @@ -99,16 +102,23 @@ mod tests {
use super::*;

use codec::Encode;
use frame_support::assert_ok;
use frame_support::{assert_ok, weights::Weight};
use xcm::v0::{
prelude::OnlyChild,
Junction::{self, Parachain, Parent},
MultiAsset::*,
MultiLocation::*,
NetworkId, OriginKind,
NetworkId, Order, OriginKind,
Xcm::*,
};
use xcm_simulator::TestExt;

// Helper function for forming buy execution message
fn buy_execution<C>(debt: Weight) -> Order<C> {
use xcm::opaque::v0::prelude::*;
Order::BuyExecution { fees: All, weight: 0, debt, halt_on_error: false, xcm: vec![] }
}

#[test]
fn dmp() {
MockNet::reset();
Expand Down Expand Up @@ -190,8 +200,12 @@ mod tests {
});
}

/// Scenario:
/// A user Alice sends funds from the relaychain to a parachain.
///
/// Asserts that the correct XCM is sent and the balances are set as expected.
#[test]
fn reserve_transfer() {
fn reserve_transfer_assets() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reserve_asset_transfer is correct, since ReserveAsset is basically one word, has a specific meaning and should not be separated.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function tested from pallet_xcm is named reserve_transfer_assets

pub fn reserve_transfer_assets(

Should this test still be renamed?

MockNet::reset();

Relay::execute_with(|| {
Expand All @@ -202,6 +216,8 @@ mod tests {
vec![ConcreteFungible { id: Null, amount: 123 }],
123,
));
let para_account_a = ParaId::from(1).into_account();
assert_eq!(parachain::Balances::free_balance(&para_account_a), INITIAL_BALANCE + 123);
});

ParaA::execute_with(|| {
Expand All @@ -212,4 +228,120 @@ mod tests {
);
});
}

/// Scenario:
/// A parachain transfers funds on the relaychain to another parachain's account.
///
/// Asserts that the parachain accounts are updated as expected.
#[test]
fn withdraw_and_deposit() {
MockNet::reset();

ParaA::execute_with(|| {
let amount = 10;
let weight = 3 * relay_chain::BaseXcmWeight::get();
let message = WithdrawAsset {
assets: vec![ConcreteFungible { id: Null, amount }],
effects: vec![
buy_execution(weight),
Order::DepositAsset { assets: vec![All], dest: Parachain(2).into() },
],
};
// Send withdraw and deposit
assert_ok!(ParachainPalletXcm::send_xcm(Null, X1(Parent), message.clone(),));
4meta5 marked this conversation as resolved.
Show resolved Hide resolved
});

Relay::execute_with(|| {
let amount = 10;
let para_account_a: relay_chain::AccountId = ParaId::from(1).into_account();
assert_eq!(
relay_chain::Balances::free_balance(para_account_a),
INITIAL_BALANCE - amount
);
let para_account_b: relay_chain::AccountId = ParaId::from(2).into_account();
assert_eq!(relay_chain::Balances::free_balance(para_account_b), 10);
});
}

/// Scenario:
/// A parachain wants to be notified that a transfer worked correctly.
/// It sends a `QueryHolding` after the deposit to get notified on success.
///
/// Asserts that the balances are updated correctly and the expected XCM is sent.
#[test]
fn query_holding() {
MockNet::reset();

// First send a message which fails on the relay chain
ParaA::execute_with(|| {
let amount = 10;
let weight = 3 * relay_chain::BaseXcmWeight::get();
//let para_account_b: relay_chain::AccountId = ParaId::from(2).into_account();
4meta5 marked this conversation as resolved.
Show resolved Hide resolved
let query_id = 1234;
let message = WithdrawAsset {
assets: vec![ConcreteFungible { id: Null, amount }],
effects: vec![
buy_execution(weight),
Order::DepositAsset {
assets: vec![All],
dest: OnlyChild.into(), // invalid destination
},
// is not triggered becasue the deposit fails
Order::QueryHolding { query_id, dest: Parachain(2).into(), assets: vec![All] },
],
};
// Send withdraw and deposit with query holding
assert_ok!(ParachainPalletXcm::send_xcm(Null, X1(Parent), message.clone(),));
});

// Check that no transfer was executed and no response message was sent
Relay::execute_with(|| {
let amount = 10;
let para_account_a: relay_chain::AccountId = ParaId::from(1).into_account();
4meta5 marked this conversation as resolved.
Show resolved Hide resolved
// withdraw did execute
assert_eq!(
relay_chain::Balances::free_balance(para_account_a),
INITIAL_BALANCE - amount
);
// but deposit did not execute
let para_account_b: relay_chain::AccountId = ParaId::from(2).into_account();
assert_eq!(relay_chain::Balances::free_balance(para_account_b), 0);
assert_eq!(relay_chain::sent_xcm(), vec![]);
});

// Now send a message which fully succeeds on the relay chain
ParaA::execute_with(|| {
let amount = 10;
let weight = 3 * relay_chain::BaseXcmWeight::get();
let query_id = 1234;
let message = WithdrawAsset {
assets: vec![ConcreteFungible { id: Null, amount }],
effects: vec![
buy_execution(weight),
Order::DepositAsset {
assets: vec![All],
dest: Parachain(2).into(), // valid destination
},
Order::QueryHolding { query_id, dest: Parachain(2).into(), assets: vec![All] },
apopiak marked this conversation as resolved.
Show resolved Hide resolved
],
};
// Send withdraw and deposit with query holding
assert_ok!(ParachainPalletXcm::send_xcm(Null, X1(Parent), message.clone(),));
});

// Check that transfer was executed and response message was sent
Relay::execute_with(|| {
let spent = 20;
let para_account_a: relay_chain::AccountId = ParaId::from(1).into_account();
// withdraw did execute
assert_eq!(
relay_chain::Balances::free_balance(para_account_a),
INITIAL_BALANCE - spent
);
// and deposit did execute
let para_account_b: relay_chain::AccountId = ParaId::from(2).into_account();
assert_eq!(relay_chain::Balances::free_balance(para_account_b), 10);
assert_eq!(relay_chain::sent_xcm(), vec![]);
});
}
}
22 changes: 20 additions & 2 deletions xcm/xcm-simulator/example/src/relay_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ use sp_runtime::{testing::Header, traits::IdentityLookup, AccountId32};

use polkadot_parachain::primitives::Id as ParaId;
use polkadot_runtime_parachains::{configuration, origin, shared, ump};
use xcm::v0::{MultiAsset, MultiLocation, NetworkId};
use sp_std::cell::RefCell;
use xcm::{
opaque::v0::{Result as XcmResult, SendXcm, Xcm},
v0::{MultiAsset, MultiLocation, NetworkId},
};
use xcm_builder::{
AccountId32Aliases, AllowUnpaidExecutionFrom, ChildParachainAsNative,
ChildParachainConvertsVia, ChildSystemParachainAsSuperuser,
Expand Down Expand Up @@ -120,6 +124,20 @@ parameter_types! {
pub type XcmRouter = super::RelayChainXcmRouter;
pub type Barrier = AllowUnpaidExecutionFrom<All<MultiLocation>>;

thread_local! {
pub static SENT_XCM: RefCell<Vec<(MultiLocation, Xcm)>> = RefCell::new(Vec::new());
}
pub(crate) fn sent_xcm() -> Vec<(MultiLocation, Xcm)> {
SENT_XCM.with(|q| (*q.borrow()).clone())
}
pub struct TestSendXcm;
impl SendXcm for TestSendXcm {
fn send_xcm(dest: MultiLocation, msg: Xcm) -> XcmResult {
SENT_XCM.with(|q| q.borrow_mut().push((dest, msg)));
Ok(())
}
}

pub struct XcmConfig;
impl Config for XcmConfig {
type Call = Call;
Expand All @@ -140,7 +158,7 @@ pub type LocalOriginToLocation = SignedToAccountId32<Origin, AccountId, KusamaNe
impl pallet_xcm::Config for Runtime {
type Event = Event;
type SendXcmOrigin = xcm_builder::EnsureXcmOrigin<Origin, LocalOriginToLocation>;
type XcmRouter = XcmRouter;
type XcmRouter = (XcmRouter, TestSendXcm);
4meta5 marked this conversation as resolved.
Show resolved Hide resolved
// Anyone can execute XCM messages locally...
type ExecuteXcmOrigin = xcm_builder::EnsureXcmOrigin<Origin, LocalOriginToLocation>;
type XcmExecuteFilter = ();
Expand Down