diff --git a/bin/millau/runtime/src/rialto_messages.rs b/bin/millau/runtime/src/rialto_messages.rs index ff4b6ba1dda..0d80cdd7b1a 100644 --- a/bin/millau/runtime/src/rialto_messages.rs +++ b/bin/millau/runtime/src/rialto_messages.rs @@ -28,7 +28,7 @@ use bridge_runtime_common::messages::{self, MessageBridge}; use frame_support::{parameter_types, weights::Weight, RuntimeDebug}; /// Default lane that is used to send messages to Rialto. -pub const XCM_LANE: LaneId = [0, 0, 0, 0]; +pub const XCM_LANE: LaneId = LaneId([0, 0, 0, 0]); /// Weight of 2 XCM instructions is for simple `Trap(42)` program, coming through bridge /// (it is prepended with `UniversalOrigin` instruction). It is used just for simplest manual /// tests, confirming that we don't break encoding somewhere between. diff --git a/bin/millau/runtime/src/rialto_parachain_messages.rs b/bin/millau/runtime/src/rialto_parachain_messages.rs index b6e8f52d342..097e2798227 100644 --- a/bin/millau/runtime/src/rialto_parachain_messages.rs +++ b/bin/millau/runtime/src/rialto_parachain_messages.rs @@ -28,7 +28,7 @@ use bridge_runtime_common::messages::{self, MessageBridge}; use frame_support::{parameter_types, weights::Weight, RuntimeDebug}; /// Default lane that is used to send messages to Rialto parachain. -pub const XCM_LANE: LaneId = [0, 0, 0, 0]; +pub const XCM_LANE: LaneId = LaneId([0, 0, 0, 0]); /// Weight of 2 XCM instructions is for simple `Trap(42)` program, coming through bridge /// (it is prepended with `UniversalOrigin` instruction). It is used just for simplest manual /// tests, confirming that we don't break encoding somewhere between. diff --git a/bin/millau/runtime/src/xcm_config.rs b/bin/millau/runtime/src/xcm_config.rs index b5e238d28b3..e353137e823 100644 --- a/bin/millau/runtime/src/xcm_config.rs +++ b/bin/millau/runtime/src/xcm_config.rs @@ -298,7 +298,7 @@ mod tests { let xcm: Xcm = vec![Instruction::Trap(42)].into(); let mut incoming_message = DispatchMessage { - key: MessageKey { lane_id: [0, 0, 0, 0], nonce: 1 }, + key: MessageKey { lane_id: LaneId([0, 0, 0, 0]), nonce: 1 }, data: DispatchMessageData { payload: Ok((location, xcm).into()) }, }; diff --git a/bin/rialto-parachain/runtime/src/lib.rs b/bin/rialto-parachain/runtime/src/lib.rs index 6a936bd9f07..e2e9663510c 100644 --- a/bin/rialto-parachain/runtime/src/lib.rs +++ b/bin/rialto-parachain/runtime/src/lib.rs @@ -832,7 +832,7 @@ mod tests { use crate::millau_messages::WeightCredit; use bp_messages::{ target_chain::{DispatchMessage, DispatchMessageData, MessageDispatch}, - MessageKey, + LaneId, MessageKey, }; use bp_runtime::messages::MessageDispatchResult; use bridge_runtime_common::messages::target::FromBridgedChainMessageDispatch; @@ -875,7 +875,7 @@ mod tests { let xcm: Xcm = vec![Instruction::Trap(42)].into(); let mut incoming_message = DispatchMessage { - key: MessageKey { lane_id: [0, 0, 0, 0], nonce: 1 }, + key: MessageKey { lane_id: LaneId([0, 0, 0, 0]), nonce: 1 }, data: DispatchMessageData { payload: Ok((location, xcm).into()) }, }; diff --git a/bin/rialto-parachain/runtime/src/millau_messages.rs b/bin/rialto-parachain/runtime/src/millau_messages.rs index 25d37f15988..e21cf76a82c 100644 --- a/bin/rialto-parachain/runtime/src/millau_messages.rs +++ b/bin/rialto-parachain/runtime/src/millau_messages.rs @@ -31,7 +31,7 @@ use bridge_runtime_common::messages::{self, MessageBridge}; use frame_support::{parameter_types, weights::Weight, RuntimeDebug}; /// Default lane that is used to send messages to Millau. -pub const XCM_LANE: LaneId = [0, 0, 0, 0]; +pub const XCM_LANE: LaneId = LaneId([0, 0, 0, 0]); /// Weight of 2 XCM instructions is for simple `Trap(42)` program, coming through bridge /// (it is prepended with `UniversalOrigin` instruction). It is used just for simplest manual /// tests, confirming that we don't break encoding somewhere between. diff --git a/bin/rialto/runtime/src/millau_messages.rs b/bin/rialto/runtime/src/millau_messages.rs index 66480c3168d..b2e55ccfa02 100644 --- a/bin/rialto/runtime/src/millau_messages.rs +++ b/bin/rialto/runtime/src/millau_messages.rs @@ -28,7 +28,7 @@ use bridge_runtime_common::messages::{self, MessageBridge}; use frame_support::{parameter_types, weights::Weight, RuntimeDebug}; /// Lane that is used for XCM messages exchange. -pub const XCM_LANE: LaneId = [0, 0, 0, 0]; +pub const XCM_LANE: LaneId = LaneId([0, 0, 0, 0]); /// Weight of 2 XCM instructions is for simple `Trap(42)` program, coming through bridge /// (it is prepended with `UniversalOrigin` instruction). It is used just for simplest manual /// tests, confirming that we don't break encoding somewhere between. diff --git a/bin/rialto/runtime/src/xcm_config.rs b/bin/rialto/runtime/src/xcm_config.rs index feab0f35c79..02729f75659 100644 --- a/bin/rialto/runtime/src/xcm_config.rs +++ b/bin/rialto/runtime/src/xcm_config.rs @@ -204,7 +204,7 @@ impl XcmBridge for ToMillauBridge { } fn xcm_lane() -> bp_messages::LaneId { - [0, 0, 0, 0] + bp_messages::LaneId([0, 0, 0, 0]) } } @@ -214,7 +214,7 @@ mod tests { use crate::millau_messages::WeightCredit; use bp_messages::{ target_chain::{DispatchMessage, DispatchMessageData, MessageDispatch}, - MessageKey, + LaneId, MessageKey, }; use bp_runtime::messages::MessageDispatchResult; use bridge_runtime_common::messages::target::FromBridgedChainMessageDispatch; @@ -257,7 +257,7 @@ mod tests { let xcm: Xcm = vec![Instruction::Trap(42)].into(); let mut incoming_message = DispatchMessage { - key: MessageKey { lane_id: [0, 0, 0, 0], nonce: 1 }, + key: MessageKey { lane_id: LaneId([0, 0, 0, 0]), nonce: 1 }, data: DispatchMessageData { payload: Ok((location, xcm).into()) }, }; diff --git a/bin/runtime-common/src/messages.rs b/bin/runtime-common/src/messages.rs index ca48a6015f1..9aced1c361a 100644 --- a/bin/runtime-common/src/messages.rs +++ b/bin/runtime-common/src/messages.rs @@ -892,7 +892,7 @@ mod tests { OutboundLaneData::default() } - const TEST_LANE_ID: &LaneId = b"test"; + const TEST_LANE_ID: &LaneId = &LaneId(*b"test"); const MAXIMAL_PENDING_MESSAGES_AT_TEST_LANE: MessageNonce = 32; fn regular_outbound_message_payload() -> source::FromThisChainMessagePayload { @@ -904,7 +904,7 @@ mod tests { assert_eq!( source::FromThisChainMessageVerifier::::verify_message( &ThisChainOrigin(Ok(frame_system::RawOrigin::Root)), - b"dsbl", + &LaneId(*b"dsbl"), &test_lane_outbound_data(), ®ular_outbound_message_payload(), ), diff --git a/bin/runtime-common/src/messages_extension.rs b/bin/runtime-common/src/messages_extension.rs index 28606d58dad..0f680f84e49 100644 --- a/bin/runtime-common/src/messages_extension.rs +++ b/bin/runtime-common/src/messages_extension.rs @@ -110,7 +110,7 @@ mod tests { fn deliver_message_10() { pallet_bridge_messages::InboundLanes::::insert( - [0, 0, 0, 0], + bp_messages::LaneId([0, 0, 0, 0]), bp_messages::InboundLaneData { relayers: Default::default(), last_confirmed_nonce: 10 }, ); } @@ -128,7 +128,7 @@ mod tests { proof: FromBridgedChainMessagesProof { bridged_header_hash: Default::default(), storage_proof: vec![], - lane: [0, 0, 0, 0], + lane: bp_messages::LaneId([0, 0, 0, 0]), nonces_start, nonces_end, }, @@ -170,7 +170,7 @@ mod tests { fn confirm_message_10() { pallet_bridge_messages::OutboundLanes::::insert( - [0, 0, 0, 0], + bp_messages::LaneId([0, 0, 0, 0]), bp_messages::OutboundLaneData { oldest_unpruned_nonce: 0, latest_received_nonce: 10, @@ -188,7 +188,7 @@ mod tests { proof: FromBridgedChainMessagesDeliveryProof { bridged_header_hash: Default::default(), storage_proof: Vec::new(), - lane: [0, 0, 0, 0], + lane: bp_messages::LaneId([0, 0, 0, 0]), }, relayers_state: UnrewardedRelayersState { last_delivered_nonce, diff --git a/bin/runtime-common/src/refund_relayer_extension.rs b/bin/runtime-common/src/refund_relayer_extension.rs index 4f019265087..b4ed3ca5893 100644 --- a/bin/runtime-common/src/refund_relayer_extension.rs +++ b/bin/runtime-common/src/refund_relayer_extension.rs @@ -489,7 +489,7 @@ mod tests { parameter_types! { pub TestParachain: u32 = 1000; - pub TestLaneId: LaneId = [0, 0, 0, 0]; + pub TestLaneId: LaneId = LaneId([0, 0, 0, 0]); } type TestExtension = RefundRelayerForMessagesFromParachain< diff --git a/modules/messages/src/mock.rs b/modules/messages/src/mock.rs index 3f38d923b28..c628ab2cbb3 100644 --- a/modules/messages/src/mock.rs +++ b/modules/messages/src/mock.rs @@ -190,13 +190,13 @@ pub const TEST_RELAYER_C: AccountId = 102; pub const TEST_ERROR: &str = "Test error"; /// Lane that we're using in tests. -pub const TEST_LANE_ID: LaneId = [0, 0, 0, 1]; +pub const TEST_LANE_ID: LaneId = LaneId([0, 0, 0, 1]); /// Secondary lane that we're using in tests. -pub const TEST_LANE_ID_2: LaneId = [0, 0, 0, 2]; +pub const TEST_LANE_ID_2: LaneId = LaneId([0, 0, 0, 2]); /// Inactive outbound lane. -pub const TEST_LANE_ID_3: LaneId = [0, 0, 0, 3]; +pub const TEST_LANE_ID_3: LaneId = LaneId([0, 0, 0, 3]); /// Regular message payload. pub const REGULAR_PAYLOAD: TestPayload = message_payload(0, 50); diff --git a/modules/relayers/src/benchmarking.rs b/modules/relayers/src/benchmarking.rs index e79dda14acb..7195b316bb6 100644 --- a/modules/relayers/src/benchmarking.rs +++ b/modules/relayers/src/benchmarking.rs @@ -38,7 +38,7 @@ pub trait Config: crate::Config { benchmarks! { // Benchmark `claim_rewards` call. claim_rewards { - let lane = [0, 0, 0, 0]; + let lane = LaneId([0, 0, 0, 0]); let relayer: T::AccountId = whitelisted_caller(); let reward = T::Reward::from(REWARD_AMOUNT); diff --git a/modules/relayers/src/lib.rs b/modules/relayers/src/lib.rs index 11bbeb10845..27ffcaf1ea5 100644 --- a/modules/relayers/src/lib.rs +++ b/modules/relayers/src/lib.rs @@ -244,9 +244,9 @@ mod tests { run_test(|| { let lane0_rewards_account = - PayLaneRewardFromAccount::lane_rewards_account([0, 0, 0, 0]); + PayLaneRewardFromAccount::lane_rewards_account(LaneId([0, 0, 0, 0])); let lane1_rewards_account = - PayLaneRewardFromAccount::lane_rewards_account([0, 0, 0, 1]); + PayLaneRewardFromAccount::lane_rewards_account(LaneId([0, 0, 0, 1])); Balances::mint_into(&lane0_rewards_account, 100).unwrap(); Balances::mint_into(&lane1_rewards_account, 100).unwrap(); @@ -254,12 +254,12 @@ mod tests { assert_eq!(Balances::balance(&lane1_rewards_account), 100); assert_eq!(Balances::balance(&1), 0); - PayLaneRewardFromAccount::pay_reward(&1, [0, 0, 0, 0], 100).unwrap(); + PayLaneRewardFromAccount::pay_reward(&1, LaneId([0, 0, 0, 0]), 100).unwrap(); assert_eq!(Balances::balance(&lane0_rewards_account), 0); assert_eq!(Balances::balance(&lane1_rewards_account), 100); assert_eq!(Balances::balance(&1), 100); - PayLaneRewardFromAccount::pay_reward(&1, [0, 0, 0, 1], 100).unwrap(); + PayLaneRewardFromAccount::pay_reward(&1, LaneId([0, 0, 0, 1]), 100).unwrap(); assert_eq!(Balances::balance(&lane0_rewards_account), 0); assert_eq!(Balances::balance(&lane1_rewards_account), 0); assert_eq!(Balances::balance(&1), 200); diff --git a/modules/relayers/src/mock.rs b/modules/relayers/src/mock.rs index 4880e908039..89b3ead0411 100644 --- a/modules/relayers/src/mock.rs +++ b/modules/relayers/src/mock.rs @@ -96,7 +96,7 @@ impl pallet_bridge_relayers::Config for TestRuntime { } /// Message lane that we're using in tests. -pub const TEST_LANE_ID: LaneId = [0, 0, 0, 0]; +pub const TEST_LANE_ID: LaneId = LaneId([0, 0, 0, 0]); /// Regular relayer that may receive rewards. pub const REGULAR_RELAYER: AccountId = 1; diff --git a/primitives/messages/src/lib.rs b/primitives/messages/src/lib.rs index e4f3cb5998b..7775b3e7dbc 100644 --- a/primitives/messages/src/lib.rs +++ b/primitives/messages/src/lib.rs @@ -67,16 +67,30 @@ impl OperatingMode for MessagesOperatingMode { } } -/// Lane identifier. -pub type LaneId = [u8; 4]; - /// Lane id which implements `TypeId`. -// TODO (https://github.com/paritytech/parity-bridges-common/issues/1694): -// `LaneId` shall be replaced with this across all codebase (codec-compatible) -#[derive(Decode, Encode, RuntimeDebug)] -pub struct TypedLaneId(pub [u8; 4]); +#[derive( + Clone, + Copy, + Decode, + Default, + Encode, + Eq, + Ord, + PartialOrd, + PartialEq, + RuntimeDebug, + TypeInfo, + MaxEncodedLen, +)] +pub struct LaneId(pub [u8; 4]); + +impl AsRef<[u8]> for LaneId { + fn as_ref(&self) -> &[u8] { + &self.0 + } +} -impl TypeId for TypedLaneId { +impl TypeId for LaneId { const TYPE_ID: [u8; 4] = *b"blan"; } diff --git a/primitives/messages/src/storage_keys.rs b/primitives/messages/src/storage_keys.rs index 4db11edec34..4edf9828cfd 100644 --- a/primitives/messages/src/storage_keys.rs +++ b/primitives/messages/src/storage_keys.rs @@ -56,7 +56,7 @@ pub fn outbound_lane_data_key(pallet_prefix: &str, lane: &LaneId) -> StorageKey bp_runtime::storage_map_final_key::( pallet_prefix, OUTBOUND_LANES_MAP_NAME, - lane, + &lane.encode(), ) } @@ -65,7 +65,7 @@ pub fn inbound_lane_data_key(pallet_prefix: &str, lane: &LaneId) -> StorageKey { bp_runtime::storage_map_final_key::( pallet_prefix, INBOUND_LANES_MAP_NAME, - lane, + &lane.encode(), ) } @@ -91,7 +91,7 @@ mod tests { fn storage_message_key_computed_properly() { // If this test fails, then something has been changed in module storage that is breaking // all previously crafted messages proofs. - let storage_key = message_key("BridgeMessages", b"test", 42).0; + let storage_key = message_key("BridgeMessages", &LaneId(*b"test"), 42).0; assert_eq!( storage_key, hex!("dd16c784ebd3390a9bc0357c7511ed018a395e6242c6813b196ca31ed0547ea79446af0e09063bd4a7874aef8a997cec746573742a00000000000000").to_vec(), @@ -104,7 +104,7 @@ mod tests { fn outbound_lane_data_key_computed_properly() { // If this test fails, then something has been changed in module storage that is breaking // all previously crafted outbound lane state proofs. - let storage_key = outbound_lane_data_key("BridgeMessages", b"test").0; + let storage_key = outbound_lane_data_key("BridgeMessages", &LaneId(*b"test")).0; assert_eq!( storage_key, hex!("dd16c784ebd3390a9bc0357c7511ed0196c246acb9b55077390e3ca723a0ca1f44a8995dd50b6657a037a7839304535b74657374").to_vec(), @@ -117,7 +117,7 @@ mod tests { fn inbound_lane_data_key_computed_properly() { // If this test fails, then something has been changed in module storage that is breaking // all previously crafted inbound lane state proofs. - let storage_key = inbound_lane_data_key("BridgeMessages", b"test").0; + let storage_key = inbound_lane_data_key("BridgeMessages", &LaneId(*b"test")).0; assert_eq!( storage_key, hex!("dd16c784ebd3390a9bc0357c7511ed01e5f83cf83f2127eb47afdc35d6e43fab44a8995dd50b6657a037a7839304535b74657374").to_vec(), diff --git a/primitives/relayers/src/lib.rs b/primitives/relayers/src/lib.rs index 04c43d5c781..d00b5f626e4 100644 --- a/primitives/relayers/src/lib.rs +++ b/primitives/relayers/src/lib.rs @@ -19,7 +19,7 @@ #![warn(missing_docs)] #![cfg_attr(not(feature = "std"), no_std)] -use bp_messages::{LaneId, TypedLaneId}; +use bp_messages::LaneId; use sp_runtime::{ codec::{Decode, Encode}, traits::AccountIdConversion, @@ -45,7 +45,7 @@ where { /// Return account that pay rewards for serving given lane. pub fn lane_rewards_account(lane_id: LaneId) -> Relayer { - TypedLaneId(lane_id).into_sub_account_truncating(b"bridge-lane") + lane_id.into_sub_account_truncating(b"bridge-lane") } } @@ -72,17 +72,17 @@ mod tests { #[test] fn lanes_are_using_different_accounts() { assert_eq!( - PayLaneRewardFromAccount::<(), bp_rialto::AccountId>::lane_rewards_account([ + PayLaneRewardFromAccount::<(), bp_rialto::AccountId>::lane_rewards_account(LaneId([ 0, 0, 0, 0 - ]), + ])), hex_literal::hex!("626c616e000000006272696467652d6c616e6500000000000000000000000000") .into(), ); assert_eq!( - PayLaneRewardFromAccount::<(), bp_rialto::AccountId>::lane_rewards_account([ + PayLaneRewardFromAccount::<(), bp_rialto::AccountId>::lane_rewards_account(LaneId([ 0, 0, 0, 1 - ]), + ])), hex_literal::hex!("626c616e000000016272696467652d6c616e6500000000000000000000000000") .into(), ); diff --git a/relays/bin-substrate/src/cli/mod.rs b/relays/bin-substrate/src/cli/mod.rs index 4bd735c6334..6784f1a6065 100644 --- a/relays/bin-substrate/src/cli/mod.rs +++ b/relays/bin-substrate/src/cli/mod.rs @@ -203,11 +203,11 @@ pub trait CliChain: relay_substrate_client::Chain { /// Lane id. #[derive(Debug, Clone, PartialEq, Eq)] -pub struct HexLaneId(pub LaneId); +pub struct HexLaneId(pub [u8; 4]); impl From for LaneId { fn from(lane_id: HexLaneId) -> LaneId { - lane_id.0 + LaneId(lane_id.0) } } @@ -215,7 +215,7 @@ impl std::str::FromStr for HexLaneId { type Err = hex::FromHexError; fn from_str(s: &str) -> Result { - let mut lane_id = LaneId::default(); + let mut lane_id = [0u8; 4]; hex::decode_to_slice(s, &mut lane_id)?; Ok(HexLaneId(lane_id)) } diff --git a/relays/lib-substrate-relay/src/messages_source.rs b/relays/lib-substrate-relay/src/messages_source.rs index 8dceb6df53b..e86f7abdd64 100644 --- a/relays/lib-substrate-relay/src/messages_source.rs +++ b/relays/lib-substrate-relay/src/messages_source.rs @@ -630,7 +630,8 @@ mod tests { msgs_to_refine.push((payload, out_msg_details)); } - let maybe_batches = split_msgs_to_refine::([0, 0, 0, 0], msgs_to_refine); + let maybe_batches = + split_msgs_to_refine::(LaneId([0, 0, 0, 0]), msgs_to_refine); match expected_batches { Ok(expected_batches) => { let batches = maybe_batches.unwrap(); diff --git a/relays/messages/src/message_lane_loop.rs b/relays/messages/src/message_lane_loop.rs index 521baabb195..e26849bbb9b 100644 --- a/relays/messages/src/message_lane_loop.rs +++ b/relays/messages/src/message_lane_loop.rs @@ -938,7 +938,7 @@ pub(crate) mod tests { }; let _ = run( Params { - lane: [0, 0, 0, 0], + lane: LaneId([0, 0, 0, 0]), source_tick: Duration::from_millis(100), target_tick: Duration::from_millis(100), reconnect_delay: Duration::from_millis(0),