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 29 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
7 changes: 4 additions & 3 deletions runtime/parachains/src/ump.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,16 @@ pub use pallet::*;
/// It is possible that by the time the message is sank the origin parachain was offboarded. It is
/// up to the implementer to check that if it cares.
pub trait UmpSink {
/// Process an incoming upward message and return the amount of weight it consumed, or `None` if
/// it did not begin processing a message since it would otherwise exceed `max_weight`.
/// Process an incoming upward message and return the amount of weight it consumed, or
/// `Err(message_id, required_weight)` if it did not begin processing a message since it would
/// otherwise exceed `max_weight`.
///
/// See the trait docs for more details.
fn process_upward_message(origin: ParaId, msg: &[u8], max_weight: Weight) -> Result<Weight, (MessageId, Weight)>;
}

/// An implementation of a sink that just swallows the message without consuming any weight. Returns
/// `Some(0)` indicating that no messages existed for it to process.
/// `Ok(0)` indicating that no messages existed for it to process.
impl UmpSink for () {
fn process_upward_message(_: ParaId, _: &[u8], _: Weight) -> Result<Weight, (MessageId, Weight)> {
Ok(0)
Expand Down
170 changes: 148 additions & 22 deletions xcm/xcm-simulator/example/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@
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]);

decl_test_parachain! {
pub struct ParaA {
Runtime = parachain::Runtime,
Expand Down Expand Up @@ -58,8 +57,13 @@ decl_test_network! {
}
}

pub const ALICE: AccountId32 = AccountId32::new([0u8; 32]);
pub const INITIAL_BALANCE: u128 = 1_000_000_000;

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

pub fn para_ext(para_id: u32) -> sp_io::TestExternalities {
use parachain::{MsgQueue, Runtime, System};

Expand All @@ -81,10 +85,11 @@ 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();
pallet_balances::GenesisConfig::<Runtime> {
balances: vec![(ALICE, INITIAL_BALANCE), (para_account_id(1), 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,24 +104,30 @@ mod tests {
use super::*;

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

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

#[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(|| {
Relay::execute_and_dispatch_xcm(|| {
assert_ok!(RelayChainPalletXcm::send_xcm(
Null,
X1(Parachain(1)),
Expand All @@ -128,7 +139,7 @@ mod tests {
));
});

ParaA::execute_with(|| {
ParaA::execute_and_dispatch_xcm(|| {
use parachain::{Event, System};
assert!(System::events()
.iter()
Expand All @@ -143,7 +154,7 @@ mod tests {
let remark = relay_chain::Call::System(
frame_system::Call::<relay_chain::Runtime>::remark_with_event(vec![1, 2, 3]),
);
ParaA::execute_with(|| {
ParaA::execute_and_dispatch_xcm(|| {
assert_ok!(ParachainPalletXcm::send_xcm(
Null,
X1(Parent),
Expand All @@ -155,7 +166,7 @@ mod tests {
));
});

Relay::execute_with(|| {
Relay::execute_and_dispatch_xcm(|| {
use relay_chain::{Event, System};
assert!(System::events()
.iter()
Expand All @@ -170,7 +181,7 @@ mod tests {
let remark = parachain::Call::System(
frame_system::Call::<parachain::Runtime>::remark_with_event(vec![1, 2, 3]),
);
ParaA::execute_with(|| {
ParaA::execute_and_dispatch_xcm(|| {
assert_ok!(ParachainPalletXcm::send_xcm(
Null,
X2(Parent, Parachain(2)),
Expand All @@ -182,33 +193,148 @@ mod tests {
));
});

ParaB::execute_with(|| {
ParaB::execute_and_dispatch_xcm(|| {
use parachain::{Event, System};
assert!(System::events()
.iter()
.any(|r| matches!(r.event, Event::System(frame_system::Event::Remarked(_, _)))));
});
}

/// 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(|| {
let withdraw_amount = 123;
let max_weight_for_execution = 10;

Relay::execute_and_dispatch_xcm(|| {
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,
vec![ConcreteFungible { id: Null, amount: withdraw_amount }],
max_weight_for_execution,
));
assert_eq!(
parachain::Balances::free_balance(&para_account_id(1)),
INITIAL_BALANCE + withdraw_amount
);
});

ParaA::execute_with(|| {
// free execution, full amount received
ParaA::execute_and_dispatch_xcm(|| {
// Check message received
let expected_message = (
X1(Parent),
ReserveAssetDeposit {
assets: vec![ConcreteFungible { id: X1(Parent), amount: withdraw_amount }],
effects: vec![
buy_execution(max_weight_for_execution),
Order::DepositAsset {
assets: vec![All],
dest: X1(Junction::AccountId32 {
network: NetworkId::Any,
id: ALICE.into(),
}),
},
],
},
);
assert_eq!(parachain::MsgQueue::received_dmp(), vec![expected_message]);
// Check message execution with full amount received
assert_eq!(
pallet_balances::Pallet::<parachain::Runtime>::free_balance(&ALICE),
INITIAL_BALANCE + 123
INITIAL_BALANCE + withdraw_amount
);
});
}

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

let send_amount = 10;
let weight_for_execution = 3 * relay_chain::BaseXcmWeight::get();

ParaA::execute_and_dispatch_xcm(|| {
let message = WithdrawAsset {
assets: vec![ConcreteFungible { id: Null, amount: send_amount }],
effects: vec![
buy_execution(weight_for_execution),
Order::DepositAsset { assets: vec![All], dest: Parachain(2).into() },
],
};
// Send withdraw and deposit
assert_ok!(ParachainPalletXcm::send_xcm(Null, X1(Parent), message.clone()));
});

Relay::execute_and_dispatch_xcm(|| {
assert_eq!(
relay_chain::Balances::free_balance(para_account_id(1)),
INITIAL_BALANCE - send_amount
);
assert_eq!(relay_chain::Balances::free_balance(para_account_id(2)), send_amount);
});
}

/// 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();

let send_amount = 10;
let weight_for_execution = 3 * relay_chain::BaseXcmWeight::get();
let query_id_set = 1234;

// Send a message which fully succeeds on the relay chain
ParaA::execute_and_dispatch_xcm(|| {
4meta5 marked this conversation as resolved.
Show resolved Hide resolved
let message = WithdrawAsset {
assets: vec![ConcreteFungible { id: Null, amount: send_amount }],
effects: vec![
buy_execution(weight_for_execution),
Order::DepositAsset { assets: vec![All], dest: Parachain(2).into() },
Order::QueryHolding {
query_id: query_id_set,
dest: Parachain(1).into(),
assets: vec![All],
},
],
};
// Send withdraw and deposit with query holding
assert_ok!(ParachainPalletXcm::send_xcm(Null, X1(Parent), message.clone(),));
});

// Check that transfer was executed
Relay::execute_and_dispatch_xcm(|| {
// Withdraw executed
assert_eq!(
relay_chain::Balances::free_balance(para_account_id(1)),
INITIAL_BALANCE - send_amount
);
// Deposit executed
assert_eq!(relay_chain::Balances::free_balance(para_account_id(2)), send_amount);
});

// Check that QueryResponse message was received
ParaA::execute_and_dispatch_xcm(|| {
assert_eq!(
parachain::MsgQueue::received_dmp(),
vec![(
X1(Parent),
QueryResponse { query_id: query_id_set, response: Assets(vec![]) }
)]
);
});
}
Expand Down
11 changes: 9 additions & 2 deletions xcm/xcm-simulator/example/src/parachain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,12 @@ pub mod mock_msg_queue {
#[pallet::getter(fn parachain_id)]
pub(super) type ParachainId<T: Config> = StorageValue<_, ParaId, ValueQuery>;

#[pallet::storage]
#[pallet::getter(fn received_dmp)]
/// A queue of received DMP messages
pub(super) type ReceivedDmp<T: Config> =
StorageValue<_, Vec<(MultiLocation, Xcm<T::Call>)>, ValueQuery>;

impl<T: Config> Get<ParaId> for Pallet<T> {
fn get() -> ParaId {
Self::parachain_id()
Expand All @@ -187,7 +193,7 @@ pub mod mock_msg_queue {
#[pallet::generate_deposit(pub(super) fn deposit_event)]
pub enum Event<T: Config> {
// XCMP
/// Some XCM was executed ok.
/// Some XCM was executed OK.
Success(Option<T::Hash>),
/// Some XCM failed.
Fail(Option<T::Hash>, XcmError),
Expand Down Expand Up @@ -275,7 +281,8 @@ pub mod mock_msg_queue {
Self::deposit_event(Event::UnsupportedVersion(id));
},
Ok(Ok(x)) => {
let outcome = T::XcmExecutor::execute_xcm(Parent.into(), x, limit);
let outcome = T::XcmExecutor::execute_xcm(Parent.into(), x.clone(), limit);
<ReceivedDmp<T>>::mutate(|y| y.push((Parent.into(), x)));
Self::deposit_event(Event::ExecutedDownward(id, outcome));
},
}
Expand Down
Loading