Skip to content

Commit

Permalink
Use TypedLaneId instead of LaneId (#1738)
Browse files Browse the repository at this point in the history
* TypedLaneId -> LaneId

* fix benchmarks compilation
  • Loading branch information
svyatonik authored Dec 30, 2022
1 parent 6683b81 commit 3d9d290
Show file tree
Hide file tree
Showing 20 changed files with 65 additions and 50 deletions.
2 changes: 1 addition & 1 deletion bin/millau/runtime/src/rialto_messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion bin/millau/runtime/src/rialto_parachain_messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion bin/millau/runtime/src/xcm_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ mod tests {
let xcm: Xcm<RuntimeCall> = 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()) },
};

Expand Down
4 changes: 2 additions & 2 deletions bin/rialto-parachain/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -875,7 +875,7 @@ mod tests {
let xcm: Xcm<RuntimeCall> = 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()) },
};

Expand Down
2 changes: 1 addition & 1 deletion bin/rialto-parachain/runtime/src/millau_messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion bin/rialto/runtime/src/millau_messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 3 additions & 3 deletions bin/rialto/runtime/src/xcm_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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])
}
}

Expand All @@ -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;
Expand Down Expand Up @@ -257,7 +257,7 @@ mod tests {
let xcm: Xcm<RuntimeCall> = 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()) },
};

Expand Down
4 changes: 2 additions & 2 deletions bin/runtime-common/src/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -904,7 +904,7 @@ mod tests {
assert_eq!(
source::FromThisChainMessageVerifier::<OnThisChainBridge>::verify_message(
&ThisChainOrigin(Ok(frame_system::RawOrigin::Root)),
b"dsbl",
&LaneId(*b"dsbl"),
&test_lane_outbound_data(),
&regular_outbound_message_payload(),
),
Expand Down
8 changes: 4 additions & 4 deletions bin/runtime-common/src/messages_extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ mod tests {

fn deliver_message_10() {
pallet_bridge_messages::InboundLanes::<Runtime, WithRialtoMessagesInstance>::insert(
[0, 0, 0, 0],
bp_messages::LaneId([0, 0, 0, 0]),
bp_messages::InboundLaneData { relayers: Default::default(), last_confirmed_nonce: 10 },
);
}
Expand All @@ -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,
},
Expand Down Expand Up @@ -170,7 +170,7 @@ mod tests {

fn confirm_message_10() {
pallet_bridge_messages::OutboundLanes::<Runtime, WithRialtoMessagesInstance>::insert(
[0, 0, 0, 0],
bp_messages::LaneId([0, 0, 0, 0]),
bp_messages::OutboundLaneData {
oldest_unpruned_nonce: 0,
latest_received_nonce: 10,
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion bin/runtime-common/src/refund_relayer_extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<
Expand Down
6 changes: 3 additions & 3 deletions modules/messages/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion modules/relayers/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
8 changes: 4 additions & 4 deletions modules/relayers/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,22 +244,22 @@ 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();
assert_eq!(Balances::balance(&lane0_rewards_account), 100);
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);
Expand Down
2 changes: 1 addition & 1 deletion modules/relayers/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
30 changes: 22 additions & 8 deletions primitives/messages/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}

Expand Down
10 changes: 5 additions & 5 deletions primitives/messages/src/storage_keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ pub fn outbound_lane_data_key(pallet_prefix: &str, lane: &LaneId) -> StorageKey
bp_runtime::storage_map_final_key::<Blake2_128Concat>(
pallet_prefix,
OUTBOUND_LANES_MAP_NAME,
lane,
&lane.encode(),
)
}

Expand All @@ -65,7 +65,7 @@ pub fn inbound_lane_data_key(pallet_prefix: &str, lane: &LaneId) -> StorageKey {
bp_runtime::storage_map_final_key::<Blake2_128Concat>(
pallet_prefix,
INBOUND_LANES_MAP_NAME,
lane,
&lane.encode(),
)
}

Expand All @@ -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(),
Expand All @@ -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(),
Expand All @@ -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(),
Expand Down
12 changes: 6 additions & 6 deletions primitives/relayers/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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")
}
}

Expand All @@ -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(),
);
Expand Down
6 changes: 3 additions & 3 deletions relays/bin-substrate/src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,19 +203,19 @@ 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<HexLaneId> for LaneId {
fn from(lane_id: HexLaneId) -> LaneId {
lane_id.0
LaneId(lane_id.0)
}
}

impl std::str::FromStr for HexLaneId {
type Err = hex::FromHexError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
let mut lane_id = LaneId::default();
let mut lane_id = [0u8; 4];
hex::decode_to_slice(s, &mut lane_id)?;
Ok(HexLaneId(lane_id))
}
Expand Down
3 changes: 2 additions & 1 deletion relays/lib-substrate-relay/src/messages_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,8 @@ mod tests {
msgs_to_refine.push((payload, out_msg_details));
}

let maybe_batches = split_msgs_to_refine::<Rialto, Rococo>([0, 0, 0, 0], msgs_to_refine);
let maybe_batches =
split_msgs_to_refine::<Rialto, Rococo>(LaneId([0, 0, 0, 0]), msgs_to_refine);
match expected_batches {
Ok(expected_batches) => {
let batches = maybe_batches.unwrap();
Expand Down
2 changes: 1 addition & 1 deletion relays/messages/src/message_lane_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down

0 comments on commit 3d9d290

Please sign in to comment.