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

Commit

Permalink
XCM v3: Context & ID hash (#4756)
Browse files Browse the repository at this point in the history
* send_xcm returns message hash

* cargo fmt

* Create topic register and instructions

* Fix weights

* Use tabs

* Sketch out XcmContext

* Fix doc test

* Add the XCM context as a parameter to executor trait fns

* Fixes

* Add XcmContext parameter

* Revert adding context as an arg to SendXcm trait methods

* Revert adding context argument to ConvertOrigin trait methods

* cargo fmt

* Do not change the API of XcmExecutor::execute

* Fixes

* Fixes

* Fixes

* Fixes

* Remove convenience method

* Fixes

* Fixes

* cargo fmt

* Fixes

* Add benchmarks for XCM topic instructions

* cargo run --quiet --profile=production  --features=runtime-benchmarks -- benchmark --chain=westend-dev --steps=50 --repeat=20 --pallet=pallet_xcm_benchmarks::generic --extrinsic=* --execution=wasm --wasm-execution=compiled --heap-pages=4096 --header=./file_header.txt --template=./xcm/pallet-xcm-benchmarks/template.hbs --output=./runtime/westend/src/weights/xcm/pallet_xcm_benchmarks_generic.rs

* Remove context argument on FilterAssetLocation

* Fixes

* Remove unused import

* Fixes

* Fixes

* Fixes

* Accept XCM hash parameter in ExecuteXcm trait methods

* cargo fmt

* Properly enable sp-io/std

* Fixes

* default-features = false

* Fixes

* Fixes

* Fixes

* Make XcmContext optional in withdraw_asset

* Fixes

* Fixes

* Fixes

* Modify tests to check for the correct XCM hash

* Small refactor

* cargo fmt

* Check for expected hash in xcm-builder unit tests

* Add doc comment for the optionality of the XCM context in withdraw_asset

* Update xcm/src/v3/traits.rs

* Update xcm/src/v3/traits.rs

* Store XcmContext and avoid rebuilding

* Use ref for XcmContext

* Formatting

* Fix incorrect hash CC @KiChjang

* Refactor and make clear fake hashes

* Fixes

* Fixes

* Fixes

* Fix broken hashing

* Docs

* Fixes

* Fixes

* Fixes

* Formatting

* Fixes

* Fixes

* Fixes

* Remove unknowable hash

* Formatting

* Use message hash for greater identifiability

* Formatting

* Fixes

* Formatting

Co-authored-by: Keith Yeung <[email protected]>
Co-authored-by: Parity Bot <[email protected]>
  • Loading branch information
3 people authored Mar 8, 2022
1 parent 3d5ab32 commit c117f99
Show file tree
Hide file tree
Showing 53 changed files with 1,452 additions and 1,173 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion bridges/bin/rialto-parachain/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ parameter_types! {
pub const MaxAuthorities: u32 = 100_000;
}

match_type! {
match_types! {
pub type ParentOrParentsUnitPlurality: impl Contains<MultiLocation> = {
MultiLocation { parents: 1, interior: Here } |
MultiLocation { parents: 1, interior: X1(Plurality { id: BodyId::Unit, .. }) }
Expand Down
4 changes: 3 additions & 1 deletion runtime/common/src/xcm_sender.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,10 @@ impl<T: configuration::Config + dmp::Config, W: xcm::WrapVersion> SendXcm

fn deliver(
(config, para, blob): (HostConfiguration<T::BlockNumber>, ParaId, Vec<u8>),
) -> Result<(), SendError> {
) -> Result<XcmHash, SendError> {
let hash = sp_io::hashing::blake2_256(&blob[..]);
<dmp::Pallet<T>>::queue_downward_message(&config, para, blob)
.map(|()| hash)
.map_err(|_| SendError::Transport(&"Error placing into DMP queue"))
}
}
2 changes: 1 addition & 1 deletion runtime/parachains/src/ump.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ impl<XcmExecutor: xcm::latest::ExecuteXcm<C::Call>, C: Config> UmpSink for XcmSi
},
Ok((Ok(xcm_message), weight_used)) => {
let xcm_junction = Junction::Parachain(origin.into());
let outcome = XcmExecutor::execute_xcm(xcm_junction, xcm_message, max_weight);
let outcome = XcmExecutor::execute_xcm(xcm_junction, xcm_message, id, max_weight);
match outcome {
Outcome::Error(XcmError::WeightLimitReached(required)) => Err((id, required)),
outcome => {
Expand Down
12 changes: 8 additions & 4 deletions runtime/test-runtime/src/xcm_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,24 @@ impl SendXcm for DoNothingRouter {
fn validate(_dest: &mut Option<MultiLocation>, _msg: &mut Option<Xcm<()>>) -> SendResult<()> {
Ok(((), MultiAssets::new()))
}
fn deliver(_: ()) -> Result<(), SendError> {
Ok(())
fn deliver(_: ()) -> Result<XcmHash, SendError> {
Ok([0; 32])
}
}

pub type Barrier = AllowUnpaidExecutionFrom<Everything>;

pub struct DummyAssetTransactor;
impl TransactAsset for DummyAssetTransactor {
fn deposit_asset(_what: &MultiAsset, _who: &MultiLocation) -> XcmResult {
fn deposit_asset(_what: &MultiAsset, _who: &MultiLocation, _context: &XcmContext) -> XcmResult {
Ok(())
}

fn withdraw_asset(_what: &MultiAsset, _who: &MultiLocation) -> Result<Assets, XcmError> {
fn withdraw_asset(
_what: &MultiAsset,
_who: &MultiLocation,
_maybe_context: Option<&XcmContext>,
) -> Result<Assets, XcmError> {
let asset: MultiAsset = (Parent, 100_000).into();
Ok(asset.into())
}
Expand Down
6 changes: 6 additions & 0 deletions runtime/westend/src/weights/xcm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,4 +238,10 @@ impl<Call> XcmWeightInfo<Call> for WestendXcmWeight<Call> {
fn set_fees_mode(_: &bool) -> Weight {
Weight::MAX // todo fix
}
fn set_topic(_topic: &[u8; 32]) -> Weight {
XcmGeneric::<Runtime>::set_topic()
}
fn clear_topic() -> Weight {
XcmGeneric::<Runtime>::clear_topic()
}
}
60 changes: 33 additions & 27 deletions runtime/westend/src/weights/xcm/pallet_xcm_benchmarks_generic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
//! Autogenerated weights for `pallet_xcm_benchmarks::generic`
//!
//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev
//! DATE: 2021-12-20, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]`
//! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("westend-dev"), DB CACHE: 128
//! DATE: 2022-02-05, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]`
//! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("westend-dev"), DB CACHE: 1024

// Executed Command:
// target/release/polkadot
// target/production/polkadot
// benchmark
// --chain=westend-dev
// --steps=50
Expand Down Expand Up @@ -51,57 +51,57 @@ impl<T: frame_system::Config> WeightInfo<T> {
// Storage: Dmp DownwardMessageQueueHeads (r:1 w:1)
// Storage: Dmp DownwardMessageQueues (r:1 w:1)
pub(crate) fn report_holding() -> Weight {
(35_446_000 as Weight)
(24_000_000 as Weight)
.saturating_add(T::DbWeight::get().reads(5 as Weight))
.saturating_add(T::DbWeight::get().writes(3 as Weight))
}
pub(crate) fn buy_execution() -> Weight {
(5_228_000 as Weight)
(3_377_000 as Weight)
}
// Storage: XcmPallet Queries (r:1 w:0)
pub(crate) fn query_response() -> Weight {
(18_708_000 as Weight)
(12_418_000 as Weight)
.saturating_add(T::DbWeight::get().reads(1 as Weight))
}
pub(crate) fn transact() -> Weight {
(20_401_000 as Weight)
(12_795_000 as Weight)
}
pub(crate) fn refund_surplus() -> Weight {
(5_238_000 as Weight)
(3_485_000 as Weight)
}
pub(crate) fn set_error_handler() -> Weight {
(5_104_000 as Weight)
(3_314_000 as Weight)
}
pub(crate) fn set_appendix() -> Weight {
(5_095_000 as Weight)
(3_334_000 as Weight)
}
pub(crate) fn clear_error() -> Weight {
(5_010_000 as Weight)
(3_240_000 as Weight)
}
pub(crate) fn descend_origin() -> Weight {
(6_368_000 as Weight)
(4_339_000 as Weight)
}
pub(crate) fn clear_origin() -> Weight {
(5_011_000 as Weight)
(3_338_000 as Weight)
}
// Storage: XcmPallet SupportedVersion (r:1 w:0)
// Storage: XcmPallet VersionDiscoveryQueue (r:1 w:1)
// Storage: XcmPallet SafeXcmVersion (r:1 w:0)
// Storage: Dmp DownwardMessageQueueHeads (r:1 w:1)
// Storage: Dmp DownwardMessageQueues (r:1 w:1)
pub(crate) fn report_error() -> Weight {
(27_823_000 as Weight)
(20_469_000 as Weight)
.saturating_add(T::DbWeight::get().reads(5 as Weight))
.saturating_add(T::DbWeight::get().writes(3 as Weight))
}
// Storage: XcmPallet AssetTraps (r:1 w:1)
pub(crate) fn claim_asset() -> Weight {
(12_402_000 as Weight)
(7_749_000 as Weight)
.saturating_add(T::DbWeight::get().reads(1 as Weight))
.saturating_add(T::DbWeight::get().writes(1 as Weight))
}
pub(crate) fn trap() -> Weight {
(5_022_000 as Weight)
(3_271_000 as Weight)
}
// Storage: XcmPallet VersionNotifyTargets (r:1 w:1)
// Storage: XcmPallet SupportedVersion (r:1 w:0)
Expand All @@ -110,13 +110,13 @@ impl<T: frame_system::Config> WeightInfo<T> {
// Storage: Dmp DownwardMessageQueueHeads (r:1 w:1)
// Storage: Dmp DownwardMessageQueues (r:1 w:1)
pub(crate) fn subscribe_version() -> Weight {
(32_492_000 as Weight)
(22_263_000 as Weight)
.saturating_add(T::DbWeight::get().reads(6 as Weight))
.saturating_add(T::DbWeight::get().writes(4 as Weight))
}
// Storage: XcmPallet VersionNotifyTargets (r:0 w:1)
pub(crate) fn unsubscribe_version() -> Weight {
(7_777_000 as Weight)
(5_366_000 as Weight)
.saturating_add(T::DbWeight::get().writes(1 as Weight))
}
// Storage: XcmPallet SupportedVersion (r:1 w:0)
Expand All @@ -125,46 +125,52 @@ impl<T: frame_system::Config> WeightInfo<T> {
// Storage: Dmp DownwardMessageQueueHeads (r:1 w:1)
// Storage: Dmp DownwardMessageQueues (r:1 w:1)
pub(crate) fn initiate_reserve_withdraw() -> Weight {
(37_066_000 as Weight)
(23_659_000 as Weight)
.saturating_add(T::DbWeight::get().reads(5 as Weight))
.saturating_add(T::DbWeight::get().writes(3 as Weight))
}
pub(crate) fn burn_asset() -> Weight {
(7_935_000 as Weight)
(4_910_000 as Weight)
}
pub(crate) fn expect_asset() -> Weight {
(5_237_000 as Weight)
(3_488_000 as Weight)
}
pub(crate) fn expect_origin() -> Weight {
(5_245_000 as Weight)
(3_400_000 as Weight)
}
pub(crate) fn expect_error() -> Weight {
(5_062_000 as Weight)
(3_358_000 as Weight)
}
// Storage: XcmPallet SupportedVersion (r:1 w:0)
// Storage: XcmPallet VersionDiscoveryQueue (r:1 w:1)
// Storage: XcmPallet SafeXcmVersion (r:1 w:0)
// Storage: Dmp DownwardMessageQueueHeads (r:1 w:1)
// Storage: Dmp DownwardMessageQueues (r:1 w:1)
pub(crate) fn query_pallet() -> Weight {
(28_876_000 as Weight)
(21_841_000 as Weight)
.saturating_add(T::DbWeight::get().reads(5 as Weight))
.saturating_add(T::DbWeight::get().writes(3 as Weight))
}
pub(crate) fn expect_pallet() -> Weight {
(5_526_000 as Weight)
(3_716_000 as Weight)
}
// Storage: XcmPallet SupportedVersion (r:1 w:0)
// Storage: XcmPallet VersionDiscoveryQueue (r:1 w:1)
// Storage: XcmPallet SafeXcmVersion (r:1 w:0)
// Storage: Dmp DownwardMessageQueueHeads (r:1 w:1)
// Storage: Dmp DownwardMessageQueues (r:1 w:1)
pub(crate) fn report_transact_status() -> Weight {
(27_889_000 as Weight)
(20_503_000 as Weight)
.saturating_add(T::DbWeight::get().reads(5 as Weight))
.saturating_add(T::DbWeight::get().writes(3 as Weight))
}
pub(crate) fn clear_transact_status() -> Weight {
(5_100_000 as Weight)
(3_270_000 as Weight)
}
pub(crate) fn set_topic() -> Weight {
(3_269_000 as Weight)
}
pub(crate) fn clear_topic() -> Weight {
(3_268_000 as Weight)
}
}
4 changes: 3 additions & 1 deletion xcm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ edition = "2021"

[dependencies]
impl-trait-for-tuples = "0.2.2"
parity-scale-codec = { version = "3.0.0", default-features = false, features = [ "derive" ] }
parity-scale-codec = { version = "3.0.0", default-features = false, features = [ "derive", "max-encoded-len" ] }
scale-info = { version = "2.0.0", default-features = false, features = ["derive"] }
sp-io = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
derivative = {version = "2.2.0", default-features = false, features = [ "use_core" ] }
log = { version = "0.4.14", default-features = false }
xcm-procedural = { path = "procedural" }
Expand All @@ -20,4 +21,5 @@ runtime-benchmarks = []
std = [
"parity-scale-codec/std",
"scale-info/std",
"sp-io/std"
]
5 changes: 3 additions & 2 deletions xcm/pallet-xcm-benchmarks/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ log = "0.4.0"
pallet-balances = { branch = "master", git = "https://github.com/paritytech/substrate" }
pallet-assets = { branch = "master", git = "https://github.com/paritytech/substrate" }
sp-core = { branch = "master", git = "https://github.com/paritytech/substrate" }
sp-io = { branch = "master", git = "https://github.com/paritytech/substrate" }
sp-tracing = { branch = "master", git = "https://github.com/paritytech/substrate" }
xcm-builder = { path = "../xcm-builder" }
xcm = { path = ".." }
Expand All @@ -41,6 +40,8 @@ std = [
"frame-benchmarking/std",
"frame-support/std",
"frame-system/std",
"sp-io/std",
"sp-runtime/std",
"sp-std/std"
"sp-std/std",
"xcm-executor/std"
]
44 changes: 34 additions & 10 deletions xcm/pallet-xcm-benchmarks/src/fungible/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,15 @@ benchmarks_instance_pallet! {
let worst_case_holding = T::worst_case_holding(0);
let asset = T::get_multi_asset();

<AssetTransactorOf<T>>::deposit_asset(&asset, &sender_location).unwrap();
<AssetTransactorOf<T>>::deposit_asset(
&asset,
&sender_location,
&XcmContext {
origin: Some(sender_location.clone()),
message_hash: [0; 32],
topic: None,
},
).unwrap();
// check the assets of origin.
assert!(!T::TransactAsset::balance(&sender_account).is_zero());

Expand All @@ -53,7 +61,7 @@ benchmarks_instance_pallet! {
let instruction = Instruction::<XcmCallOf<T>>::WithdrawAsset(vec![asset.clone()].into());
let xcm = Xcm(vec![instruction]);
}: {
executor.execute(xcm)?;
executor.bench_process(xcm)?;
} verify {
// check one of the assets of origin.
assert!(T::TransactAsset::balance(&sender_account).is_zero());
Expand All @@ -69,14 +77,22 @@ benchmarks_instance_pallet! {
let dest_location = T::valid_destination()?;
let dest_account = T::AccountIdConverter::convert(dest_location.clone()).unwrap();

<AssetTransactorOf<T>>::deposit_asset(&asset, &sender_location).unwrap();
<AssetTransactorOf<T>>::deposit_asset(
&asset,
&sender_location,
&XcmContext {
origin: Some(sender_location.clone()),
message_hash: [0; 32],
topic: None,
},
).unwrap();
assert!(T::TransactAsset::balance(&dest_account).is_zero());

let mut executor = new_executor::<T>(sender_location);
let instruction = Instruction::TransferAsset { assets, beneficiary: dest_location };
let xcm = Xcm(vec![instruction]);
}: {
executor.execute(xcm)?;
executor.bench_process(xcm)?;
} verify {
assert!(T::TransactAsset::balance(&sender_account).is_zero());
assert!(!T::TransactAsset::balance(&dest_account).is_zero());
Expand All @@ -88,7 +104,15 @@ benchmarks_instance_pallet! {
let dest_account = T::AccountIdConverter::convert(dest_location.clone()).unwrap();

let asset = T::get_multi_asset();
<AssetTransactorOf<T>>::deposit_asset(&asset, &sender_location).unwrap();
<AssetTransactorOf<T>>::deposit_asset(
&asset,
&sender_location,
&XcmContext {
origin: Some(sender_location.clone()),
message_hash: [0; 32],
topic: None,
},
).unwrap();
let assets: MultiAssets = vec![ asset ].into();
assert!(T::TransactAsset::balance(&dest_account).is_zero());

Expand All @@ -100,7 +124,7 @@ benchmarks_instance_pallet! {
};
let xcm = Xcm(vec![instruction]);
}: {
executor.execute(xcm)?;
executor.bench_process(xcm)?;
} verify {
assert!(T::TransactAsset::balance(&sender_account).is_zero());
assert!(!T::TransactAsset::balance(&dest_account).is_zero());
Expand Down Expand Up @@ -129,7 +153,7 @@ benchmarks_instance_pallet! {
let instruction = Instruction::ReceiveTeleportedAsset(assets.clone());
let xcm = Xcm(vec![instruction]);
}: {
executor.execute(xcm).map_err(|_| {
executor.bench_process(xcm).map_err(|_| {
BenchmarkError::Override(
BenchmarkResult::from_weight(T::BlockWeights::get().max_block)
)
Expand Down Expand Up @@ -158,7 +182,7 @@ benchmarks_instance_pallet! {
};
let xcm = Xcm(vec![instruction]);
}: {
executor.execute(xcm)?;
executor.bench_process(xcm)?;
} verify {
// dest should have received some asset.
assert!(!T::TransactAsset::balance(&dest_account).is_zero())
Expand All @@ -185,7 +209,7 @@ benchmarks_instance_pallet! {
};
let xcm = Xcm(vec![instruction]);
}: {
executor.execute(xcm)?;
executor.bench_process(xcm)?;
} verify {
// dest should have received some asset.
assert!(!T::TransactAsset::balance(&dest_account).is_zero())
Expand All @@ -210,7 +234,7 @@ benchmarks_instance_pallet! {
};
let xcm = Xcm(vec![instruction]);
}: {
executor.execute(xcm)?;
executor.bench_process(xcm)?;
} verify {
if let Some(checked_account) = T::CheckedAccount::get() {
// teleport checked account should have received some asset.
Expand Down
Loading

0 comments on commit c117f99

Please sign in to comment.