Skip to content

Commit

Permalink
Utility to generate hex bytes for send-message command (paritytech#1658)
Browse files Browse the repository at this point in the history
  • Loading branch information
bkontur authored Nov 22, 2022
1 parent bf56bb1 commit dca73d1
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
3 changes: 3 additions & 0 deletions bridges/primitives/test-utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down
44 changes: 44 additions & 0 deletions bridges/primitives/test-utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<RuntimeCall>(xcm: &VersionedXcm<RuntimeCall>) {
println!("-----------------");
println!("xcm (plain): {:?}", xcm);
println!("xcm (bytes): {:?}", xcm.encode());
println!("xcm (hex): {:?}", hexdisplay::HexDisplay::from(&xcm.encode()));
}

fn as_hex<RuntimeCall>(xcm: &VersionedXcm<RuntimeCall>) -> String {
HexDisplay::from(&xcm.encode()).to_string()
}

pub type RuntimeCall = ();

#[test]
fn generate_versioned_xcm_message_hex_bytes() {
let xcm: xcm::v2::Xcm<RuntimeCall> = xcm::v2::Xcm(vec![xcm::v2::Instruction::Trap(43)]);
let xcm: VersionedXcm<RuntimeCall> = From::from(xcm);
print_xcm(&xcm);
assert_eq!("020419ac", format!("{}", as_hex(&xcm)));

let xcm: xcm::v3::Xcm<RuntimeCall> = vec![xcm::v3::Instruction::Trap(43)].into();
let xcm: VersionedXcm<RuntimeCall> = From::from(xcm);
print_xcm(&xcm);
assert_eq!("030419ac", format!("{}", as_hex(&xcm)));

let xcm: xcm::v3::Xcm<RuntimeCall> = vec![
xcm::v3::Instruction::ClearError,
xcm::v3::Instruction::ClearTopic,
xcm::v3::Instruction::ClearTransactStatus,
xcm::v3::Instruction::Trap(43),
]
.into();
let xcm: VersionedXcm<RuntimeCall> = From::from(xcm);
print_xcm(&xcm);
assert_eq!("0310172c2319ac", format!("{}", as_hex(&xcm)));
}
}

0 comments on commit dca73d1

Please sign in to comment.