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

Commit 4b2bd54

Browse files
authored
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.
1 parent 18de14d commit 4b2bd54

File tree

11 files changed

+1107
-3
lines changed

11 files changed

+1107
-3
lines changed

Cargo.lock

+42-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ members = [
3939
"xcm",
4040
"xcm/xcm-builder",
4141
"xcm/xcm-executor",
42+
"xcm/xcm-simulator",
43+
"xcm/xcm-simulator/example",
4244
"xcm/pallet-xcm",
4345
"node/client",
4446
"node/collation-generation",

parachain/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ parity-util-mem = { version = "0.10.0", optional = true }
1414
sp-std = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
1515
sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
1616
sp-core = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
17+
frame-support = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
1718
polkadot-core-primitives = { path = "../core-primitives", default-features = false }
1819
derive_more = "0.99.11"
1920

parachain/src/primitives.rs

+54
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use sp_std::vec::Vec;
2222
use parity_scale_codec::{Encode, Decode, CompactAs};
2323
use sp_core::{RuntimeDebug, TypeId};
2424
use sp_runtime::traits::Hash as _;
25+
use frame_support::weights::Weight;
2526

2627
#[cfg(feature = "std")]
2728
use serde::{Serialize, Deserialize};
@@ -318,6 +319,59 @@ pub struct HrmpChannelId {
318319
/// A message from a parachain to its Relay Chain.
319320
pub type UpwardMessage = Vec<u8>;
320321

322+
/// Something that should be called when a downward message is received.
323+
pub trait DmpMessageHandler {
324+
/// Handle some incoming DMP messages (note these are individual XCM messages).
325+
///
326+
/// Also, process messages up to some `max_weight`.
327+
fn handle_dmp_messages(
328+
iter: impl Iterator<Item = (RelayChainBlockNumber, Vec<u8>)>,
329+
max_weight: Weight,
330+
) -> Weight;
331+
}
332+
impl DmpMessageHandler for () {
333+
fn handle_dmp_messages(
334+
iter: impl Iterator<Item = (RelayChainBlockNumber, Vec<u8>)>,
335+
_max_weight: Weight,
336+
) -> Weight {
337+
iter.for_each(drop);
338+
0
339+
}
340+
}
341+
342+
/// The aggregate XCMP message format.
343+
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Encode, Decode)]
344+
pub enum XcmpMessageFormat {
345+
/// Encoded `VersionedXcm` messages, all concatenated.
346+
ConcatenatedVersionedXcm,
347+
/// Encoded `Vec<u8>` messages, all concatenated.
348+
ConcatenatedEncodedBlob,
349+
/// One or more channel control signals; these should be interpreted immediately upon receipt
350+
/// from the relay-chain.
351+
Signals,
352+
}
353+
354+
/// Something that should be called for each batch of messages received over XCMP.
355+
pub trait XcmpMessageHandler {
356+
/// Handle some incoming XCMP messages (note these are the big one-per-block aggregate
357+
/// messages).
358+
///
359+
/// Also, process messages up to some `max_weight`.
360+
fn handle_xcmp_messages<'a, I: Iterator<Item = (Id, RelayChainBlockNumber, &'a [u8])>>(
361+
iter: I,
362+
max_weight: Weight,
363+
) -> Weight;
364+
}
365+
impl XcmpMessageHandler for () {
366+
fn handle_xcmp_messages<'a, I: Iterator<Item = (Id, RelayChainBlockNumber, &'a [u8])>>(
367+
iter: I,
368+
_max_weight: Weight,
369+
) -> Weight {
370+
for _ in iter {}
371+
0
372+
}
373+
}
374+
321375
/// Validation parameters for evaluating the parachain validity function.
322376
// TODO: balance downloads (https://github.com/paritytech/polkadot/issues/220)
323377
#[derive(PartialEq, Eq, Decode, Clone)]

xcm/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "xcm"
33
version = "0.9.8"
4-
authors = ["Parity Technologies x<[email protected]>"]
4+
authors = ["Parity Technologies <[email protected]>"]
55
description = "The basic XCM datastructures."
66
edition = "2018"
77

xcm/xcm-simulator/Cargo.toml

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
[package]
2+
name = "xcm-simulator"
3+
version = "0.9.8"
4+
authors = ["Parity Technologies <[email protected]>"]
5+
description = "Test kit to simulate cross-chain message passing and XCM execution"
6+
edition = "2018"
7+
8+
[dependencies]
9+
codec = { package = "parity-scale-codec", version = "2.0.0" }
10+
paste = "1.0.5"
11+
12+
frame-support = { git = "https://github.com/paritytech/substrate", branch = "master" }
13+
sp-io = { git = "https://github.com/paritytech/substrate", branch = "master" }
14+
sp-std = { git = "https://github.com/paritytech/substrate", branch = "master" }
15+
16+
xcm = { path = "../" }
17+
xcm-executor = { path = "../xcm-executor" }
18+
polkadot-core-primitives = { path = "../../core-primitives"}
19+
polkadot-parachain = { path = "../../parachain" }
20+
polkadot-runtime-parachains = { path = "../../runtime/parachains" }

xcm/xcm-simulator/example/Cargo.toml

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
[package]
2+
name = "xcm-simulator-example"
3+
version = "0.9.8"
4+
authors = ["Parity Technologies <[email protected]>"]
5+
description = "Examples of xcm-simulator usage."
6+
edition = "2018"
7+
8+
[dependencies]
9+
codec = { package = "parity-scale-codec", version = "2.0.0" }
10+
paste = "1.0.5"
11+
12+
frame-system = { git = "https://github.com/paritytech/substrate", branch = "master" }
13+
frame-support = { git = "https://github.com/paritytech/substrate", branch = "master" }
14+
pallet-balances = { git = "https://github.com/paritytech/substrate", branch = "master" }
15+
sp-std = { git = "https://github.com/paritytech/substrate", branch = "master" }
16+
sp-core = { git = "https://github.com/paritytech/substrate", branch = "master" }
17+
sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "master" }
18+
sp-io = { git = "https://github.com/paritytech/substrate", branch = "master" }
19+
20+
xcm = { path = "../../" }
21+
xcm-simulator = { path = "../" }
22+
xcm-executor = { path = "../../xcm-executor" }
23+
xcm-builder = { path = "../../xcm-builder" }
24+
pallet-xcm = { path = "../../pallet-xcm" }
25+
polkadot-core-primitives = { path = "../../../core-primitives"}
26+
polkadot-runtime-parachains = { path = "../../../runtime/parachains" }
27+
polkadot-parachain = { path = "../../../parachain" }

0 commit comments

Comments
 (0)