Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
14 changes: 14 additions & 0 deletions bin/runtime-common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,21 @@ pub enum CustomNetworkId {
RialtoParachain,
}

impl TryFrom<bp_runtime::ChainId> for CustomNetworkId {
type Error = ();

fn try_from(chain: bp_runtime::ChainId) -> Result<Self, Self::Error> {
Ok(match chain {
bp_runtime::MILLAU_CHAIN_ID => Self::Millau,
bp_runtime::RIALTO_CHAIN_ID => Self::Rialto,
bp_runtime::RIALTO_PARACHAIN_CHAIN_ID => Self::RialtoParachain,
_ => return Err(()),
})
}
}

impl CustomNetworkId {
/// Converts self to XCM' network id.
pub const fn as_network_id(&self) -> NetworkId {
match *self {
CustomNetworkId::Millau => NetworkId::Kusama,
Expand Down
5 changes: 0 additions & 5 deletions deployments/networks/millau.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ services:
image: ${MILLAU_BRIDGE_NODE_IMAGE:-paritytech/millau-bridge-node}
entrypoint:
- /home/user/millau-bridge-node
- --execution=Native
- --chain=local
- --bootnodes=/dns4/millau-node-bob/tcp/30333/p2p/12D3KooWM5LFR5ne4yTQ4sBSXJ75M4bDo2MAhAW2GhL3i8fe5aRb
- --alice
Expand All @@ -32,7 +31,6 @@ services:
<<: *millau-bridge-node
entrypoint:
- /home/user/millau-bridge-node
- --execution=Native
- --chain=local
- --bootnodes=/dns4/millau-node-alice/tcp/30333/p2p/12D3KooWFqiV73ipQ1jpfVmCfLqBCp8G9PLH3zPkY9EhmdrSGA4H
- --bob
Expand All @@ -51,7 +49,6 @@ services:
<<: *millau-bridge-node
entrypoint:
- /home/user/millau-bridge-node
- --execution=Native
- --chain=local
- --bootnodes=/dns4/millau-node-alice/tcp/30333/p2p/12D3KooWFqiV73ipQ1jpfVmCfLqBCp8G9PLH3zPkY9EhmdrSGA4H
- --charlie
Expand All @@ -69,7 +66,6 @@ services:
<<: *millau-bridge-node
entrypoint:
- /home/user/millau-bridge-node
- --execution=Native
- --chain=local
- --bootnodes=/dns4/millau-node-alice/tcp/30333/p2p/12D3KooWFqiV73ipQ1jpfVmCfLqBCp8G9PLH3zPkY9EhmdrSGA4H
- --dave
Expand All @@ -87,7 +83,6 @@ services:
<<: *millau-bridge-node
entrypoint:
- /home/user/millau-bridge-node
- --execution=Native
- --chain=local
- --bootnodes=/dns4/millau-node-alice/tcp/30333/p2p/12D3KooWFqiV73ipQ1jpfVmCfLqBCp8G9PLH3zPkY9EhmdrSGA4H
- --eve
Expand Down
6 changes: 0 additions & 6 deletions deployments/networks/rialto.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ services:
image: ${RIALTO_BRIDGE_NODE_IMAGE:-paritytech/rialto-bridge-node}
entrypoint:
- /home/user/rialto-bridge-node
- --execution=Native
- --chain=local
- --bootnodes=/dns4/rialto-node-bob/tcp/30333/p2p/12D3KooWSEpHJj29HEzgPFcRYVc5X3sEuP3KgiUoqJNCet51NiMX
- --alice
Expand All @@ -32,7 +31,6 @@ services:
<<: *rialto-bridge-node
entrypoint:
- /home/user/rialto-bridge-node
- --execution=Native
- --chain=local
- --bootnodes=/dns4/rialto-node-alice/tcp/30333/p2p/12D3KooWMF6JvV319a7kJn5pqkKbhR3fcM2cvK5vCbYZHeQhYzFE
- --bob
Expand All @@ -51,7 +49,6 @@ services:
<<: *rialto-bridge-node
entrypoint:
- /home/user/rialto-bridge-node
- --execution=Native
- --chain=local
- --bootnodes=/dns4/rialto-node-alice/tcp/30333/p2p/12D3KooWMF6JvV319a7kJn5pqkKbhR3fcM2cvK5vCbYZHeQhYzFE
- --charlie
Expand All @@ -69,7 +66,6 @@ services:
<<: *rialto-bridge-node
entrypoint:
- /home/user/rialto-bridge-node
- --execution=Native
- --chain=local
- --bootnodes=/dns4/rialto-node-alice/tcp/30333/p2p/12D3KooWMF6JvV319a7kJn5pqkKbhR3fcM2cvK5vCbYZHeQhYzFE
- --dave
Expand All @@ -87,7 +83,6 @@ services:
<<: *rialto-bridge-node
entrypoint:
- /home/user/rialto-bridge-node
- --execution=Native
- --chain=local
- --bootnodes=/dns4/rialto-node-alice/tcp/30333/p2p/12D3KooWMF6JvV319a7kJn5pqkKbhR3fcM2cvK5vCbYZHeQhYzFE
- --eve
Expand All @@ -105,7 +100,6 @@ services:
<<: *rialto-bridge-node
entrypoint:
- /home/user/rialto-bridge-node
- --execution=Native
- --chain=local
- --bootnodes=/dns4/rialto-node-alice/tcp/30333/p2p/12D3KooWMF6JvV319a7kJn5pqkKbhR3fcM2cvK5vCbYZHeQhYzFE
- --ferdie
Expand Down
4 changes: 2 additions & 2 deletions relays/bin-substrate/src/chains/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ mod wococo;

#[cfg(test)]
mod tests {
use crate::cli::encode_message;
use crate::cli::{encode_message, encode_message::XCM_EXPORTER_OVERHEAD};
use bp_messages::source_chain::TargetHeaderChain;
use bp_runtime::Chain as _;
use codec::Encode;
Expand All @@ -42,7 +42,7 @@ mod tests {
let maximal_message_size = encode_message::compute_maximal_message_size(
bp_rialto::Rialto::max_extrinsic_size(),
bp_millau::Millau::max_extrinsic_size(),
);
) + XCM_EXPORTER_OVERHEAD;

let message = vec![42; maximal_message_size as _];
assert_eq!(MillauAsTargetHeaderChain::verify_message(&message), Ok(()));
Expand Down
55 changes: 36 additions & 19 deletions relays/bin-substrate/src/cli/encode_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,14 @@

use crate::cli::{ExplicitOrMaximal, HexBytes};
use bp_runtime::EncodedOrDecodedCall;
use bridge_runtime_common::CustomNetworkId;
use codec::Encode;
use frame_support::weights::Weight;
use relay_substrate_client::Chain;
use structopt::StructOpt;

pub(crate) const XCM_EXPORTER_OVERHEAD: u32 = 128;

/// All possible messages that may be delivered to generic Substrate chain.
///
/// Note this enum may be used in the context of both Source (as part of `encode-call`)
Expand Down Expand Up @@ -62,6 +65,9 @@ pub(crate) fn encode_message<Source: Chain, Target: Chain>(
Ok(match message {
Message::Raw { ref data } => data.0.clone(),
Message::Sized { ref size } => {
let destination = CustomNetworkId::try_from(Target::ID)
.map(|n| n.as_network_id())
.map_err(|_| anyhow::format_err!("Unsupported target chain: {:?}", Target::ID))?;
let expected_xcm_size = match *size {
ExplicitOrMaximal::Explicit(size) => size,
ExplicitOrMaximal::Maximal => compute_maximal_message_size(
Expand All @@ -73,24 +79,30 @@ pub(crate) fn encode_message<Source: Chain, Target: Chain>(
// there's no way to craft XCM of the given size - we'll be using `ExpectPallet`
// instruction, which has byte vector inside
let mut current_vec_size = expected_xcm_size;
let xcm = loop {
let xcm = xcm::VersionedXcm::<()>::V3(
vec![xcm::v3::Instruction::ExpectPallet {
index: 0,
name: vec![42; current_vec_size as usize],
module_name: vec![],
crate_major: 0,
min_crate_minor: 0,
}]
.into(),
);
let at_target_xcm = loop {
let xcm = xcm::v3::Xcm(vec![xcm::v3::Instruction::ExpectPallet {
index: 0,
name: vec![42; current_vec_size as usize],
module_name: vec![],
crate_major: 0,
min_crate_minor: 0,
}]);
if xcm.encode().len() <= expected_xcm_size as usize {
break xcm
}

current_vec_size -= 1;
};
xcm.encode()

xcm::VersionedXcm::<()>::V3(
vec![xcm::v3::Instruction::ExportMessage {
network: destination,
destination: destination.into(),
xcm: at_target_xcm,
}]
.into(),
)
.encode()
},
})
}
Expand All @@ -108,11 +120,8 @@ pub(crate) fn compute_maximal_message_size(
bridge_runtime_common::messages::target::maximal_incoming_message_size(
maximal_target_extrinsic_size,
);
if maximal_message_size > maximal_source_extrinsic_size {
maximal_source_extrinsic_size
} else {
maximal_message_size
}

std::cmp::min(maximal_message_size, maximal_source_extrinsic_size) - XCM_EXPORTER_OVERHEAD
}

#[cfg(test)]
Expand All @@ -129,7 +138,11 @@ mod tests {
size: ExplicitOrMaximal::Explicit(100),
})
.unwrap();
assert_eq!(msg.len(), 100);
assert_eq!(
msg.len(),
// requested size + ExportMessage size
107
);
// check that it decodes to valid xcm
let _ = decode_xcm::<()>(msg).unwrap();
}
Expand All @@ -144,7 +157,11 @@ mod tests {
let msg =
encode_message::<Rialto, Millau>(&Message::Sized { size: ExplicitOrMaximal::Maximal })
.unwrap();
assert_eq!(msg.len(), maximal_size as usize);
assert_eq!(
msg.len(),
// maximal size + ExportMessage size
maximal_size as usize + 7
);
// check that it decodes to valid xcm
let _ = decode_xcm::<()>(msg).unwrap();
}
Expand Down