diff --git a/Cargo.lock b/Cargo.lock index c855c1b4109..fca69c94843 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -943,6 +943,7 @@ dependencies = [ "sp-finality-grandpa", "sp-runtime", "sp-std", + "xcm", ] [[package]] diff --git a/primitives/test-utils/Cargo.toml b/primitives/test-utils/Cargo.toml index 0a591334577..2bc77e632e5 100644 --- a/primitives/test-utils/Cargo.toml +++ b/primitives/test-utils/Cargo.toml @@ -15,6 +15,9 @@ sp-finality-grandpa = { git = "https://github.com/paritytech/substrate", branch sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false } sp-std = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false } +[dev-dependencies] +xcm = { git = "https://github.com/paritytech/polkadot", branch = "master", default-features = false } + [features] default = ["std"] std = [ diff --git a/primitives/test-utils/src/lib.rs b/primitives/test-utils/src/lib.rs index c1e95ec6fef..186d192014b 100644 --- a/primitives/test-utils/src/lib.rs +++ b/primitives/test-utils/src/lib.rs @@ -299,3 +299,47 @@ macro_rules! generate_owned_bridge_module_tests { } }; } + +#[cfg(test)] +mod tests { + use codec::Encode; + use sp_application_crypto::sp_core::{hexdisplay, hexdisplay::HexDisplay}; + use xcm::VersionedXcm; + + fn print_xcm(xcm: &VersionedXcm) { + println!("-----------------"); + println!("xcm (plain): {:?}", xcm); + println!("xcm (bytes): {:?}", xcm.encode()); + println!("xcm (hex): {:?}", hexdisplay::HexDisplay::from(&xcm.encode())); + } + + fn as_hex(xcm: &VersionedXcm) -> String { + HexDisplay::from(&xcm.encode()).to_string() + } + + pub type RuntimeCall = (); + + #[test] + fn generate_versioned_xcm_message_hex_bytes() { + let xcm: xcm::v2::Xcm = xcm::v2::Xcm(vec![xcm::v2::Instruction::Trap(43)]); + let xcm: VersionedXcm = From::from(xcm); + print_xcm(&xcm); + assert_eq!("020419ac", format!("{}", as_hex(&xcm))); + + let xcm: xcm::v3::Xcm = vec![xcm::v3::Instruction::Trap(43)].into(); + let xcm: VersionedXcm = From::from(xcm); + print_xcm(&xcm); + assert_eq!("030419ac", format!("{}", as_hex(&xcm))); + + let xcm: xcm::v3::Xcm = vec![ + xcm::v3::Instruction::ClearError, + xcm::v3::Instruction::ClearTopic, + xcm::v3::Instruction::ClearTransactStatus, + xcm::v3::Instruction::Trap(43), + ] + .into(); + let xcm: VersionedXcm = From::from(xcm); + print_xcm(&xcm); + assert_eq!("0310172c2319ac", format!("{}", as_hex(&xcm))); + } +}