Skip to content

Commit

Permalink
Change LaneId underlying type from [u8; 4] to H256 (#2221)
Browse files Browse the repository at this point in the history
* change LaneId underlying type from [u8; 4] to H256

* fixed typo

* added some tests

* spelling

* started fixing testnets

* uncommented call size test

* changed RewardsAccountParams encoding + added values separator when computing LaneId

* review suggestions
  • Loading branch information
svyatonik authored and bkontur committed Jul 23, 2024
1 parent 169a487 commit 90e608d
Show file tree
Hide file tree
Showing 30 changed files with 370 additions and 231 deletions.
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions bridges/bin/runtime-common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ xcm-builder = { workspace = true }
bp-test-utils = { workspace = true }
pallet-balances = { workspace = true }
pallet-bridge-messages = { features = ["std", "test-helpers"], workspace = true }
sp-core = { workspace = true }

[features]
default = ["std"]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@
//! with calls that are: delivering new message and all necessary underlying headers
//! (parachain or relay chain).

use crate::messages_call_ext::{
CallHelper as MessagesCallHelper, CallInfo as MessagesCallInfo, MessagesCallSubType,
use crate::{
messages_call_ext::{
CallHelper as MessagesCallHelper, CallInfo as MessagesCallInfo, MessagesCallSubType,
},
messages_xcm_extension::LaneIdFromChainId,
};
use bp_messages::{ChainWithMessages, LaneId, MessageNonce};
use bp_relayers::{ExplicitOrAccountParams, RewardsAccountOwner, RewardsAccountParams};
Expand Down Expand Up @@ -92,15 +95,15 @@ pub trait RefundableMessagesLaneId {
}

/// Default implementation of `RefundableMessagesLaneId`.
pub struct RefundableMessagesLane<Instance, Id>(PhantomData<(Instance, Id)>);
pub struct RefundableMessagesLane<Runtime, Instance>(PhantomData<(Runtime, Instance)>);

impl<Instance, Id> RefundableMessagesLaneId for RefundableMessagesLane<Instance, Id>
impl<Runtime, Instance> RefundableMessagesLaneId for RefundableMessagesLane<Runtime, Instance>
where
Runtime: MessagesConfig<Instance>,
Instance: 'static,
Id: Get<LaneId>,
{
type Instance = Instance;
type Id = Id;
type Id = LaneIdFromChainId<Runtime, Instance>;
}

/// Refund calculator.
Expand Down Expand Up @@ -968,14 +971,14 @@ pub(crate) mod tests {
};

parameter_types! {
pub TestLaneId: LaneId = TEST_LANE_ID;
TestParachain: u32 = 1000;
pub MsgProofsRewardsAccount: RewardsAccountParams = RewardsAccountParams::new(
TEST_LANE_ID,
test_lane_id(),
TEST_BRIDGED_CHAIN_ID,
RewardsAccountOwner::ThisChain,
);
pub MsgDeliveryProofsRewardsAccount: RewardsAccountParams = RewardsAccountParams::new(
TEST_LANE_ID,
test_lane_id(),
TEST_BRIDGED_CHAIN_ID,
RewardsAccountOwner::BridgedChain,
);
Expand All @@ -985,7 +988,7 @@ pub(crate) mod tests {

type TestMessagesExtensionProvider = RefundBridgedMessages<
TestRuntime,
RefundableMessagesLane<(), TestLaneId>,
RefundableMessagesLane<TestRuntime, ()>,
ActualFeeRefund<TestRuntime>,
ConstU64<1>,
StrTestExtension,
Expand All @@ -994,7 +997,7 @@ pub(crate) mod tests {
type TestGrandpaExtensionProvider = RefundBridgedGrandpaMessages<
TestRuntime,
(),
RefundableMessagesLane<(), TestLaneId>,
RefundableMessagesLane<TestRuntime, ()>,
ActualFeeRefund<TestRuntime>,
ConstU64<1>,
StrTestExtension,
Expand All @@ -1003,7 +1006,7 @@ pub(crate) mod tests {
type TestExtensionProvider = RefundBridgedParachainMessages<
TestRuntime,
RefundableParachain<(), BridgedUnderlyingParachain>,
RefundableMessagesLane<(), TestLaneId>,
RefundableMessagesLane<TestRuntime, ()>,
ActualFeeRefund<TestRuntime>,
ConstU64<1>,
StrTestExtension,
Expand Down Expand Up @@ -1060,7 +1063,7 @@ pub(crate) mod tests {
};
pallet_bridge_parachains::ParasInfo::<TestRuntime>::insert(para_id, para_info);

let lane_id = TestLaneId::get();
let lane_id = test_lane_id();
let in_lane_data =
InboundLaneData { last_confirmed_nonce: best_message, ..Default::default() };
pallet_bridge_messages::InboundLanes::<TestRuntime>::insert(lane_id, in_lane_data);
Expand Down Expand Up @@ -1145,9 +1148,9 @@ pub(crate) mod tests {
proof: Box::new(FromBridgedChainMessagesProof {
bridged_header_hash: Default::default(),
storage_proof: Default::default(),
lane: TestLaneId::get(),
lane: test_lane_id(),
nonces_start: pallet_bridge_messages::InboundLanes::<TestRuntime>::get(
TEST_LANE_ID,
test_lane_id(),
)
.unwrap()
.last_delivered_nonce() +
Expand All @@ -1164,7 +1167,7 @@ pub(crate) mod tests {
proof: FromBridgedChainMessagesDeliveryProof {
bridged_header_hash: Default::default(),
storage_proof: Default::default(),
lane: TestLaneId::get(),
lane: test_lane_id(),
},
relayers_state: UnrewardedRelayersState {
last_delivered_nonce: best_message,
Expand Down Expand Up @@ -1321,7 +1324,7 @@ pub(crate) mod tests {
},
MessagesCallInfo::ReceiveMessagesProof(ReceiveMessagesProofInfo {
base: BaseMessagesProofInfo {
lane_id: TEST_LANE_ID,
lane_id: test_lane_id(),
bundled_range: 101..=200,
best_stored_nonce: 100,
},
Expand Down Expand Up @@ -1363,7 +1366,7 @@ pub(crate) mod tests {
},
MessagesCallInfo::ReceiveMessagesDeliveryProof(ReceiveMessagesDeliveryProofInfo(
BaseMessagesProofInfo {
lane_id: TEST_LANE_ID,
lane_id: test_lane_id(),
bundled_range: 101..=200,
best_stored_nonce: 100,
},
Expand Down Expand Up @@ -1393,7 +1396,7 @@ pub(crate) mod tests {
},
MessagesCallInfo::ReceiveMessagesProof(ReceiveMessagesProofInfo {
base: BaseMessagesProofInfo {
lane_id: TEST_LANE_ID,
lane_id: test_lane_id(),
bundled_range: 101..=200,
best_stored_nonce: 100,
},
Expand Down Expand Up @@ -1429,7 +1432,7 @@ pub(crate) mod tests {
},
MessagesCallInfo::ReceiveMessagesDeliveryProof(ReceiveMessagesDeliveryProofInfo(
BaseMessagesProofInfo {
lane_id: TEST_LANE_ID,
lane_id: test_lane_id(),
bundled_range: 101..=200,
best_stored_nonce: 100,
},
Expand Down Expand Up @@ -1457,7 +1460,7 @@ pub(crate) mod tests {
},
MessagesCallInfo::ReceiveMessagesProof(ReceiveMessagesProofInfo {
base: BaseMessagesProofInfo {
lane_id: TEST_LANE_ID,
lane_id: test_lane_id(),
bundled_range: 101..=200,
best_stored_nonce: 100,
},
Expand All @@ -1484,7 +1487,7 @@ pub(crate) mod tests {
},
MessagesCallInfo::ReceiveMessagesDeliveryProof(ReceiveMessagesDeliveryProofInfo(
BaseMessagesProofInfo {
lane_id: TEST_LANE_ID,
lane_id: test_lane_id(),
bundled_range: 101..=200,
best_stored_nonce: 100,
},
Expand All @@ -1499,7 +1502,7 @@ pub(crate) mod tests {
call_info: CallInfo::Msgs(MessagesCallInfo::ReceiveMessagesProof(
ReceiveMessagesProofInfo {
base: BaseMessagesProofInfo {
lane_id: TEST_LANE_ID,
lane_id: test_lane_id(),
bundled_range: 101..=200,
best_stored_nonce: 100,
},
Expand All @@ -1519,7 +1522,7 @@ pub(crate) mod tests {
relayer: relayer_account_at_this_chain(),
call_info: CallInfo::Msgs(MessagesCallInfo::ReceiveMessagesDeliveryProof(
ReceiveMessagesDeliveryProofInfo(BaseMessagesProofInfo {
lane_id: TEST_LANE_ID,
lane_id: test_lane_id(),
bundled_range: 101..=200,
best_stored_nonce: 100,
}),
Expand Down Expand Up @@ -2883,7 +2886,7 @@ pub(crate) mod tests {
.unwrap();

// allow empty message delivery transactions
let lane_id = TestLaneId::get();
let lane_id = test_lane_id();
let in_lane_data = InboundLaneData {
state: LaneState::Opened,
last_confirmed_nonce: 0,
Expand Down
28 changes: 15 additions & 13 deletions bridges/bin/runtime-common/src/messages_call_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -361,26 +361,28 @@ mod tests {
};
use sp_std::ops::RangeInclusive;

const TEST_LANE_ID: LaneId = LaneId([0, 0, 0, 0]);
fn test_lane_id() -> LaneId {
LaneId::new(1, 2)
}

fn fill_unrewarded_relayers() {
let mut inbound_lane_state =
pallet_bridge_messages::InboundLanes::<TestRuntime>::get(TEST_LANE_ID).unwrap();
pallet_bridge_messages::InboundLanes::<TestRuntime>::get(test_lane_id()).unwrap();
for n in 0..BridgedUnderlyingChain::MAX_UNREWARDED_RELAYERS_IN_CONFIRMATION_TX {
inbound_lane_state.relayers.push_back(UnrewardedRelayer {
relayer: Default::default(),
messages: DeliveredMessages { begin: n + 1, end: n + 1 },
});
}
pallet_bridge_messages::InboundLanes::<TestRuntime>::insert(
TEST_LANE_ID,
test_lane_id(),
inbound_lane_state,
);
}

fn fill_unrewarded_messages() {
let mut inbound_lane_state =
pallet_bridge_messages::InboundLanes::<TestRuntime>::get(TEST_LANE_ID).unwrap();
pallet_bridge_messages::InboundLanes::<TestRuntime>::get(test_lane_id()).unwrap();
inbound_lane_state.relayers.push_back(UnrewardedRelayer {
relayer: Default::default(),
messages: DeliveredMessages {
Expand All @@ -389,14 +391,14 @@ mod tests {
},
});
pallet_bridge_messages::InboundLanes::<TestRuntime>::insert(
TEST_LANE_ID,
test_lane_id(),
inbound_lane_state,
);
}

fn deliver_message_10() {
pallet_bridge_messages::InboundLanes::<TestRuntime>::insert(
TEST_LANE_ID,
test_lane_id(),
bp_messages::InboundLaneData {
state: LaneState::Opened,
relayers: Default::default(),
Expand All @@ -418,7 +420,7 @@ mod tests {
proof: Box::new(FromBridgedChainMessagesProof {
bridged_header_hash: Default::default(),
storage_proof: Default::default(),
lane: TEST_LANE_ID,
lane: test_lane_id(),
nonces_start,
nonces_end,
}),
Expand All @@ -431,11 +433,11 @@ mod tests {
fn run_test<T>(test: impl Fn() -> T) -> T {
sp_io::TestExternalities::new(Default::default()).execute_with(|| {
pallet_bridge_messages::InboundLanes::<TestRuntime>::insert(
TEST_LANE_ID,
test_lane_id(),
InboundLaneData::opened(),
);
pallet_bridge_messages::OutboundLanes::<TestRuntime>::insert(
TEST_LANE_ID,
test_lane_id(),
OutboundLaneData::opened(),
);
test()
Expand Down Expand Up @@ -537,7 +539,7 @@ mod tests {

fn confirm_message_10() {
pallet_bridge_messages::OutboundLanes::<TestRuntime>::insert(
TEST_LANE_ID,
test_lane_id(),
bp_messages::OutboundLaneData {
state: LaneState::Opened,
oldest_unpruned_nonce: 0,
Expand All @@ -553,7 +555,7 @@ mod tests {
proof: FromBridgedChainMessagesDeliveryProof {
bridged_header_hash: Default::default(),
storage_proof: Default::default(),
lane: TEST_LANE_ID,
lane: test_lane_id(),
},
relayers_state: UnrewardedRelayersState {
last_delivered_nonce,
Expand Down Expand Up @@ -611,7 +613,7 @@ mod tests {
CallHelper::<TestRuntime, ()>::was_successful(&CallInfo::ReceiveMessagesProof(
ReceiveMessagesProofInfo {
base: BaseMessagesProofInfo {
lane_id: TEST_LANE_ID,
lane_id: test_lane_id(),
bundled_range,
best_stored_nonce: 0, // doesn't matter for `was_successful`
},
Expand Down Expand Up @@ -671,7 +673,7 @@ mod tests {
fn was_message_confirmation_successful(bundled_range: RangeInclusive<MessageNonce>) -> bool {
CallHelper::<TestRuntime, ()>::was_successful(&CallInfo::ReceiveMessagesDeliveryProof(
ReceiveMessagesDeliveryProofInfo(BaseMessagesProofInfo {
lane_id: TEST_LANE_ID,
lane_id: test_lane_id(),
bundled_range,
best_stored_nonce: 0, // doesn't matter for `was_successful`
}),
Expand Down
Loading

0 comments on commit 90e608d

Please sign in to comment.