Use TypedLaneId instead of LaneId (#1738)

* TypedLaneId -> LaneId

* fix benchmarks compilation
This commit is contained in:
Svyatoslav Nikolsky
2022-12-30 12:09:19 +03:00
committed by Bastian Köcher
parent 41daa32acb
commit 03425b33ae
20 changed files with 65 additions and 50 deletions
@@ -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.
@@ -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.
+1 -1
View File
@@ -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()) },
};
@@ -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<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()) },
};
@@ -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.
@@ -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.
+3 -3
View File
@@ -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<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()) },
};
+2 -2
View File
@@ -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::<OnThisChainBridge>::verify_message(
&ThisChainOrigin(Ok(frame_system::RawOrigin::Root)),
b"dsbl",
&LaneId(*b"dsbl"),
&test_lane_outbound_data(),
&regular_outbound_message_payload(),
),
@@ -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 },
);
}
@@ -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::<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,
@@ -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,
@@ -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<
+3 -3
View File
@@ -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);
+1 -1
View File
@@ -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);
+4 -4
View File
@@ -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);
+1 -1
View File
@@ -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;
+22 -8
View File
@@ -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 TypeId for TypedLaneId {
impl AsRef<[u8]> for LaneId {
fn as_ref(&self) -> &[u8] {
&self.0
}
}
impl TypeId for LaneId {
const TYPE_ID: [u8; 4] = *b"blan";
}
@@ -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(),
)
}
@@ -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(),
)
}
@@ -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(),
+6 -6
View File
@@ -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(),
);
+3 -3
View File
@@ -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<HexLaneId> 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<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))
}
@@ -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();
@@ -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),