Skip to content

Commit c0b53e9

Browse files
authored
Use TypedLaneId instead of LaneId (paritytech#1738)
* TypedLaneId -> LaneId * fix benchmarks compilation
1 parent 1044ecf commit c0b53e9

File tree

20 files changed

+65
-50
lines changed

20 files changed

+65
-50
lines changed

bin/millau/runtime/src/rialto_messages.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use bridge_runtime_common::messages::{self, MessageBridge};
2828
use frame_support::{parameter_types, weights::Weight, RuntimeDebug};
2929

3030
/// Default lane that is used to send messages to Rialto.
31-
pub const XCM_LANE: LaneId = [0, 0, 0, 0];
31+
pub const XCM_LANE: LaneId = LaneId([0, 0, 0, 0]);
3232
/// Weight of 2 XCM instructions is for simple `Trap(42)` program, coming through bridge
3333
/// (it is prepended with `UniversalOrigin` instruction). It is used just for simplest manual
3434
/// tests, confirming that we don't break encoding somewhere between.

bin/millau/runtime/src/rialto_parachain_messages.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use bridge_runtime_common::messages::{self, MessageBridge};
2828
use frame_support::{parameter_types, weights::Weight, RuntimeDebug};
2929

3030
/// Default lane that is used to send messages to Rialto parachain.
31-
pub const XCM_LANE: LaneId = [0, 0, 0, 0];
31+
pub const XCM_LANE: LaneId = LaneId([0, 0, 0, 0]);
3232
/// Weight of 2 XCM instructions is for simple `Trap(42)` program, coming through bridge
3333
/// (it is prepended with `UniversalOrigin` instruction). It is used just for simplest manual
3434
/// tests, confirming that we don't break encoding somewhere between.

bin/millau/runtime/src/xcm_config.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ mod tests {
298298
let xcm: Xcm<RuntimeCall> = vec![Instruction::Trap(42)].into();
299299

300300
let mut incoming_message = DispatchMessage {
301-
key: MessageKey { lane_id: [0, 0, 0, 0], nonce: 1 },
301+
key: MessageKey { lane_id: LaneId([0, 0, 0, 0]), nonce: 1 },
302302
data: DispatchMessageData { payload: Ok((location, xcm).into()) },
303303
};
304304

bin/rialto-parachain/runtime/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -832,7 +832,7 @@ mod tests {
832832
use crate::millau_messages::WeightCredit;
833833
use bp_messages::{
834834
target_chain::{DispatchMessage, DispatchMessageData, MessageDispatch},
835-
MessageKey,
835+
LaneId, MessageKey,
836836
};
837837
use bp_runtime::messages::MessageDispatchResult;
838838
use bridge_runtime_common::messages::target::FromBridgedChainMessageDispatch;
@@ -875,7 +875,7 @@ mod tests {
875875
let xcm: Xcm<RuntimeCall> = vec![Instruction::Trap(42)].into();
876876

877877
let mut incoming_message = DispatchMessage {
878-
key: MessageKey { lane_id: [0, 0, 0, 0], nonce: 1 },
878+
key: MessageKey { lane_id: LaneId([0, 0, 0, 0]), nonce: 1 },
879879
data: DispatchMessageData { payload: Ok((location, xcm).into()) },
880880
};
881881

bin/rialto-parachain/runtime/src/millau_messages.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ use bridge_runtime_common::messages::{self, MessageBridge};
3131
use frame_support::{parameter_types, weights::Weight, RuntimeDebug};
3232

3333
/// Default lane that is used to send messages to Millau.
34-
pub const XCM_LANE: LaneId = [0, 0, 0, 0];
34+
pub const XCM_LANE: LaneId = LaneId([0, 0, 0, 0]);
3535
/// Weight of 2 XCM instructions is for simple `Trap(42)` program, coming through bridge
3636
/// (it is prepended with `UniversalOrigin` instruction). It is used just for simplest manual
3737
/// tests, confirming that we don't break encoding somewhere between.

bin/rialto/runtime/src/millau_messages.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use bridge_runtime_common::messages::{self, MessageBridge};
2828
use frame_support::{parameter_types, weights::Weight, RuntimeDebug};
2929

3030
/// Lane that is used for XCM messages exchange.
31-
pub const XCM_LANE: LaneId = [0, 0, 0, 0];
31+
pub const XCM_LANE: LaneId = LaneId([0, 0, 0, 0]);
3232
/// Weight of 2 XCM instructions is for simple `Trap(42)` program, coming through bridge
3333
/// (it is prepended with `UniversalOrigin` instruction). It is used just for simplest manual
3434
/// tests, confirming that we don't break encoding somewhere between.

bin/rialto/runtime/src/xcm_config.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ impl XcmBridge for ToMillauBridge {
204204
}
205205

206206
fn xcm_lane() -> bp_messages::LaneId {
207-
[0, 0, 0, 0]
207+
bp_messages::LaneId([0, 0, 0, 0])
208208
}
209209
}
210210

@@ -214,7 +214,7 @@ mod tests {
214214
use crate::millau_messages::WeightCredit;
215215
use bp_messages::{
216216
target_chain::{DispatchMessage, DispatchMessageData, MessageDispatch},
217-
MessageKey,
217+
LaneId, MessageKey,
218218
};
219219
use bp_runtime::messages::MessageDispatchResult;
220220
use bridge_runtime_common::messages::target::FromBridgedChainMessageDispatch;
@@ -257,7 +257,7 @@ mod tests {
257257
let xcm: Xcm<RuntimeCall> = vec![Instruction::Trap(42)].into();
258258

259259
let mut incoming_message = DispatchMessage {
260-
key: MessageKey { lane_id: [0, 0, 0, 0], nonce: 1 },
260+
key: MessageKey { lane_id: LaneId([0, 0, 0, 0]), nonce: 1 },
261261
data: DispatchMessageData { payload: Ok((location, xcm).into()) },
262262
};
263263

bin/runtime-common/src/messages.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -892,7 +892,7 @@ mod tests {
892892
OutboundLaneData::default()
893893
}
894894

895-
const TEST_LANE_ID: &LaneId = b"test";
895+
const TEST_LANE_ID: &LaneId = &LaneId(*b"test");
896896
const MAXIMAL_PENDING_MESSAGES_AT_TEST_LANE: MessageNonce = 32;
897897

898898
fn regular_outbound_message_payload() -> source::FromThisChainMessagePayload {
@@ -904,7 +904,7 @@ mod tests {
904904
assert_eq!(
905905
source::FromThisChainMessageVerifier::<OnThisChainBridge>::verify_message(
906906
&ThisChainOrigin(Ok(frame_system::RawOrigin::Root)),
907-
b"dsbl",
907+
&LaneId(*b"dsbl"),
908908
&test_lane_outbound_data(),
909909
&regular_outbound_message_payload(),
910910
),

bin/runtime-common/src/messages_extension.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ mod tests {
110110

111111
fn deliver_message_10() {
112112
pallet_bridge_messages::InboundLanes::<Runtime, WithRialtoMessagesInstance>::insert(
113-
[0, 0, 0, 0],
113+
bp_messages::LaneId([0, 0, 0, 0]),
114114
bp_messages::InboundLaneData { relayers: Default::default(), last_confirmed_nonce: 10 },
115115
);
116116
}
@@ -128,7 +128,7 @@ mod tests {
128128
proof: FromBridgedChainMessagesProof {
129129
bridged_header_hash: Default::default(),
130130
storage_proof: vec![],
131-
lane: [0, 0, 0, 0],
131+
lane: bp_messages::LaneId([0, 0, 0, 0]),
132132
nonces_start,
133133
nonces_end,
134134
},
@@ -170,7 +170,7 @@ mod tests {
170170

171171
fn confirm_message_10() {
172172
pallet_bridge_messages::OutboundLanes::<Runtime, WithRialtoMessagesInstance>::insert(
173-
[0, 0, 0, 0],
173+
bp_messages::LaneId([0, 0, 0, 0]),
174174
bp_messages::OutboundLaneData {
175175
oldest_unpruned_nonce: 0,
176176
latest_received_nonce: 10,
@@ -188,7 +188,7 @@ mod tests {
188188
proof: FromBridgedChainMessagesDeliveryProof {
189189
bridged_header_hash: Default::default(),
190190
storage_proof: Vec::new(),
191-
lane: [0, 0, 0, 0],
191+
lane: bp_messages::LaneId([0, 0, 0, 0]),
192192
},
193193
relayers_state: UnrewardedRelayersState {
194194
last_delivered_nonce,

bin/runtime-common/src/refund_relayer_extension.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,7 @@ mod tests {
489489

490490
parameter_types! {
491491
pub TestParachain: u32 = 1000;
492-
pub TestLaneId: LaneId = [0, 0, 0, 0];
492+
pub TestLaneId: LaneId = LaneId([0, 0, 0, 0]);
493493
}
494494

495495
type TestExtension = RefundRelayerForMessagesFromParachain<

modules/messages/src/mock.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -190,13 +190,13 @@ pub const TEST_RELAYER_C: AccountId = 102;
190190
pub const TEST_ERROR: &str = "Test error";
191191

192192
/// Lane that we're using in tests.
193-
pub const TEST_LANE_ID: LaneId = [0, 0, 0, 1];
193+
pub const TEST_LANE_ID: LaneId = LaneId([0, 0, 0, 1]);
194194

195195
/// Secondary lane that we're using in tests.
196-
pub const TEST_LANE_ID_2: LaneId = [0, 0, 0, 2];
196+
pub const TEST_LANE_ID_2: LaneId = LaneId([0, 0, 0, 2]);
197197

198198
/// Inactive outbound lane.
199-
pub const TEST_LANE_ID_3: LaneId = [0, 0, 0, 3];
199+
pub const TEST_LANE_ID_3: LaneId = LaneId([0, 0, 0, 3]);
200200

201201
/// Regular message payload.
202202
pub const REGULAR_PAYLOAD: TestPayload = message_payload(0, 50);

modules/relayers/src/benchmarking.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ pub trait Config: crate::Config {
3838
benchmarks! {
3939
// Benchmark `claim_rewards` call.
4040
claim_rewards {
41-
let lane = [0, 0, 0, 0];
41+
let lane = LaneId([0, 0, 0, 0]);
4242
let relayer: T::AccountId = whitelisted_caller();
4343
let reward = T::Reward::from(REWARD_AMOUNT);
4444

modules/relayers/src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -244,22 +244,22 @@ mod tests {
244244

245245
run_test(|| {
246246
let lane0_rewards_account =
247-
PayLaneRewardFromAccount::lane_rewards_account([0, 0, 0, 0]);
247+
PayLaneRewardFromAccount::lane_rewards_account(LaneId([0, 0, 0, 0]));
248248
let lane1_rewards_account =
249-
PayLaneRewardFromAccount::lane_rewards_account([0, 0, 0, 1]);
249+
PayLaneRewardFromAccount::lane_rewards_account(LaneId([0, 0, 0, 1]));
250250

251251
Balances::mint_into(&lane0_rewards_account, 100).unwrap();
252252
Balances::mint_into(&lane1_rewards_account, 100).unwrap();
253253
assert_eq!(Balances::balance(&lane0_rewards_account), 100);
254254
assert_eq!(Balances::balance(&lane1_rewards_account), 100);
255255
assert_eq!(Balances::balance(&1), 0);
256256

257-
PayLaneRewardFromAccount::pay_reward(&1, [0, 0, 0, 0], 100).unwrap();
257+
PayLaneRewardFromAccount::pay_reward(&1, LaneId([0, 0, 0, 0]), 100).unwrap();
258258
assert_eq!(Balances::balance(&lane0_rewards_account), 0);
259259
assert_eq!(Balances::balance(&lane1_rewards_account), 100);
260260
assert_eq!(Balances::balance(&1), 100);
261261

262-
PayLaneRewardFromAccount::pay_reward(&1, [0, 0, 0, 1], 100).unwrap();
262+
PayLaneRewardFromAccount::pay_reward(&1, LaneId([0, 0, 0, 1]), 100).unwrap();
263263
assert_eq!(Balances::balance(&lane0_rewards_account), 0);
264264
assert_eq!(Balances::balance(&lane1_rewards_account), 0);
265265
assert_eq!(Balances::balance(&1), 200);

modules/relayers/src/mock.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ impl pallet_bridge_relayers::Config for TestRuntime {
9696
}
9797

9898
/// Message lane that we're using in tests.
99-
pub const TEST_LANE_ID: LaneId = [0, 0, 0, 0];
99+
pub const TEST_LANE_ID: LaneId = LaneId([0, 0, 0, 0]);
100100

101101
/// Regular relayer that may receive rewards.
102102
pub const REGULAR_RELAYER: AccountId = 1;

primitives/messages/src/lib.rs

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -67,16 +67,30 @@ impl OperatingMode for MessagesOperatingMode {
6767
}
6868
}
6969

70-
/// Lane identifier.
71-
pub type LaneId = [u8; 4];
72-
7370
/// Lane id which implements `TypeId`.
74-
// TODO (https://github.com/paritytech/parity-bridges-common/issues/1694):
75-
// `LaneId` shall be replaced with this across all codebase (codec-compatible)
76-
#[derive(Decode, Encode, RuntimeDebug)]
77-
pub struct TypedLaneId(pub [u8; 4]);
71+
#[derive(
72+
Clone,
73+
Copy,
74+
Decode,
75+
Default,
76+
Encode,
77+
Eq,
78+
Ord,
79+
PartialOrd,
80+
PartialEq,
81+
RuntimeDebug,
82+
TypeInfo,
83+
MaxEncodedLen,
84+
)]
85+
pub struct LaneId(pub [u8; 4]);
86+
87+
impl AsRef<[u8]> for LaneId {
88+
fn as_ref(&self) -> &[u8] {
89+
&self.0
90+
}
91+
}
7892

79-
impl TypeId for TypedLaneId {
93+
impl TypeId for LaneId {
8094
const TYPE_ID: [u8; 4] = *b"blan";
8195
}
8296

primitives/messages/src/storage_keys.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ pub fn outbound_lane_data_key(pallet_prefix: &str, lane: &LaneId) -> StorageKey
5656
bp_runtime::storage_map_final_key::<Blake2_128Concat>(
5757
pallet_prefix,
5858
OUTBOUND_LANES_MAP_NAME,
59-
lane,
59+
&lane.encode(),
6060
)
6161
}
6262

@@ -65,7 +65,7 @@ pub fn inbound_lane_data_key(pallet_prefix: &str, lane: &LaneId) -> StorageKey {
6565
bp_runtime::storage_map_final_key::<Blake2_128Concat>(
6666
pallet_prefix,
6767
INBOUND_LANES_MAP_NAME,
68-
lane,
68+
&lane.encode(),
6969
)
7070
}
7171

@@ -91,7 +91,7 @@ mod tests {
9191
fn storage_message_key_computed_properly() {
9292
// If this test fails, then something has been changed in module storage that is breaking
9393
// all previously crafted messages proofs.
94-
let storage_key = message_key("BridgeMessages", b"test", 42).0;
94+
let storage_key = message_key("BridgeMessages", &LaneId(*b"test"), 42).0;
9595
assert_eq!(
9696
storage_key,
9797
hex!("dd16c784ebd3390a9bc0357c7511ed018a395e6242c6813b196ca31ed0547ea79446af0e09063bd4a7874aef8a997cec746573742a00000000000000").to_vec(),
@@ -104,7 +104,7 @@ mod tests {
104104
fn outbound_lane_data_key_computed_properly() {
105105
// If this test fails, then something has been changed in module storage that is breaking
106106
// all previously crafted outbound lane state proofs.
107-
let storage_key = outbound_lane_data_key("BridgeMessages", b"test").0;
107+
let storage_key = outbound_lane_data_key("BridgeMessages", &LaneId(*b"test")).0;
108108
assert_eq!(
109109
storage_key,
110110
hex!("dd16c784ebd3390a9bc0357c7511ed0196c246acb9b55077390e3ca723a0ca1f44a8995dd50b6657a037a7839304535b74657374").to_vec(),
@@ -117,7 +117,7 @@ mod tests {
117117
fn inbound_lane_data_key_computed_properly() {
118118
// If this test fails, then something has been changed in module storage that is breaking
119119
// all previously crafted inbound lane state proofs.
120-
let storage_key = inbound_lane_data_key("BridgeMessages", b"test").0;
120+
let storage_key = inbound_lane_data_key("BridgeMessages", &LaneId(*b"test")).0;
121121
assert_eq!(
122122
storage_key,
123123
hex!("dd16c784ebd3390a9bc0357c7511ed01e5f83cf83f2127eb47afdc35d6e43fab44a8995dd50b6657a037a7839304535b74657374").to_vec(),

primitives/relayers/src/lib.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
#![warn(missing_docs)]
2020
#![cfg_attr(not(feature = "std"), no_std)]
2121

22-
use bp_messages::{LaneId, TypedLaneId};
22+
use bp_messages::LaneId;
2323
use sp_runtime::{
2424
codec::{Decode, Encode},
2525
traits::AccountIdConversion,
@@ -45,7 +45,7 @@ where
4545
{
4646
/// Return account that pay rewards for serving given lane.
4747
pub fn lane_rewards_account(lane_id: LaneId) -> Relayer {
48-
TypedLaneId(lane_id).into_sub_account_truncating(b"bridge-lane")
48+
lane_id.into_sub_account_truncating(b"bridge-lane")
4949
}
5050
}
5151

@@ -72,17 +72,17 @@ mod tests {
7272
#[test]
7373
fn lanes_are_using_different_accounts() {
7474
assert_eq!(
75-
PayLaneRewardFromAccount::<(), bp_rialto::AccountId>::lane_rewards_account([
75+
PayLaneRewardFromAccount::<(), bp_rialto::AccountId>::lane_rewards_account(LaneId([
7676
0, 0, 0, 0
77-
]),
77+
])),
7878
hex_literal::hex!("626c616e000000006272696467652d6c616e6500000000000000000000000000")
7979
.into(),
8080
);
8181

8282
assert_eq!(
83-
PayLaneRewardFromAccount::<(), bp_rialto::AccountId>::lane_rewards_account([
83+
PayLaneRewardFromAccount::<(), bp_rialto::AccountId>::lane_rewards_account(LaneId([
8484
0, 0, 0, 1
85-
]),
85+
])),
8686
hex_literal::hex!("626c616e000000016272696467652d6c616e6500000000000000000000000000")
8787
.into(),
8888
);

relays/bin-substrate/src/cli/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -203,19 +203,19 @@ pub trait CliChain: relay_substrate_client::Chain {
203203

204204
/// Lane id.
205205
#[derive(Debug, Clone, PartialEq, Eq)]
206-
pub struct HexLaneId(pub LaneId);
206+
pub struct HexLaneId(pub [u8; 4]);
207207

208208
impl From<HexLaneId> for LaneId {
209209
fn from(lane_id: HexLaneId) -> LaneId {
210-
lane_id.0
210+
LaneId(lane_id.0)
211211
}
212212
}
213213

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

217217
fn from_str(s: &str) -> Result<Self, Self::Err> {
218-
let mut lane_id = LaneId::default();
218+
let mut lane_id = [0u8; 4];
219219
hex::decode_to_slice(s, &mut lane_id)?;
220220
Ok(HexLaneId(lane_id))
221221
}

relays/lib-substrate-relay/src/messages_source.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -630,7 +630,8 @@ mod tests {
630630
msgs_to_refine.push((payload, out_msg_details));
631631
}
632632

633-
let maybe_batches = split_msgs_to_refine::<Rialto, Rococo>([0, 0, 0, 0], msgs_to_refine);
633+
let maybe_batches =
634+
split_msgs_to_refine::<Rialto, Rococo>(LaneId([0, 0, 0, 0]), msgs_to_refine);
634635
match expected_batches {
635636
Ok(expected_batches) => {
636637
let batches = maybe_batches.unwrap();

relays/messages/src/message_lane_loop.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -938,7 +938,7 @@ pub(crate) mod tests {
938938
};
939939
let _ = run(
940940
Params {
941-
lane: [0, 0, 0, 0],
941+
lane: LaneId([0, 0, 0, 0]),
942942
source_tick: Duration::from_millis(100),
943943
target_tick: Duration::from_millis(100),
944944
reconnect_delay: Duration::from_millis(0),

0 commit comments

Comments
 (0)