Skip to content

Commit

Permalink
Generic LaneId for pallet_bridge_messages
Browse files Browse the repository at this point in the history
  • Loading branch information
bkontur committed Sep 9, 2024
1 parent b272d94 commit 14b8469
Show file tree
Hide file tree
Showing 11 changed files with 197 additions and 159 deletions.
18 changes: 9 additions & 9 deletions bridges/modules/messages/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use crate::{
use bp_messages::{
source_chain::FromBridgedChainMessagesDeliveryProof,
target_chain::FromBridgedChainMessagesProof, ChainWithMessages, DeliveredMessages,
InboundLaneData, LaneId, LaneState, MessageNonce, OutboundLaneData, UnrewardedRelayer,
InboundLaneData, LaneIdType, LaneState, MessageNonce, OutboundLaneData, UnrewardedRelayer,
UnrewardedRelayersState,
};
use bp_runtime::{AccountIdOf, HashOf, UnverifiedStorageProofParams};
Expand All @@ -44,7 +44,7 @@ pub struct Pallet<T: Config<I>, I: 'static = ()>(crate::Pallet<T, I>);

/// Benchmark-specific message proof parameters.
#[derive(Debug)]
pub struct MessageProofParams {
pub struct MessageProofParams<LaneId> {
/// Id of the lane.
pub lane: LaneId,
/// Range of messages to include in the proof.
Expand All @@ -62,7 +62,7 @@ pub struct MessageProofParams {

/// Benchmark-specific message delivery proof parameters.
#[derive(Debug)]
pub struct MessageDeliveryProofParams<ThisChainAccountId> {
pub struct MessageDeliveryProofParams<ThisChainAccountId, LaneId> {
/// Id of the lane.
pub lane: LaneId,
/// The proof needs to include this inbound lane data.
Expand All @@ -74,8 +74,8 @@ pub struct MessageDeliveryProofParams<ThisChainAccountId> {
/// Trait that must be implemented by runtime.
pub trait Config<I: 'static>: crate::Config<I> {
/// Lane id to use in benchmarks.
fn bench_lane_id() -> LaneId {
LaneId::new(1, 2)
fn bench_lane_id() -> Self::LaneId {
Self::LaneId::new(1, 2)
}

/// Return id of relayer account at the bridged chain.
Expand All @@ -94,12 +94,12 @@ pub trait Config<I: 'static>: crate::Config<I> {

/// Prepare messages proof to receive by the module.
fn prepare_message_proof(
params: MessageProofParams,
) -> (FromBridgedChainMessagesProof<HashOf<BridgedChainOf<Self, I>>>, Weight);
params: MessageProofParams<Self::LaneId>,
) -> (FromBridgedChainMessagesProof<HashOf<BridgedChainOf<Self, I>>, Self::LaneId>, Weight);
/// Prepare messages delivery proof to receive by the module.
fn prepare_message_delivery_proof(
params: MessageDeliveryProofParams<Self::AccountId>,
) -> FromBridgedChainMessagesDeliveryProof<HashOf<BridgedChainOf<Self, I>>>;
params: MessageDeliveryProofParams<Self::AccountId, Self::LaneId>,
) -> FromBridgedChainMessagesDeliveryProof<HashOf<BridgedChainOf<Self, I>>, Self::LaneId>;

/// Returns true if message has been successfully dispatched or not.
fn is_message_successfully_dispatched(_nonce: MessageNonce) -> bool {
Expand Down
30 changes: 15 additions & 15 deletions bridges/modules/messages/src/call_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ use crate::{BridgedChainOf, Config, InboundLanes, OutboundLanes, Pallet, LOG_TAR

use bp_messages::{
target_chain::MessageDispatch, BaseMessagesProofInfo, ChainWithMessages, InboundLaneData,
LaneId, MessageNonce, MessagesCallInfo, ReceiveMessagesDeliveryProofInfo,
ReceiveMessagesProofInfo, UnrewardedRelayerOccupation,
MessageNonce, MessagesCallInfo, ReceiveMessagesDeliveryProofInfo, ReceiveMessagesProofInfo,
UnrewardedRelayerOccupation,
};
use bp_runtime::{AccountIdOf, OwnedBridgeModule};
use frame_support::{dispatch::CallableCallFor, traits::IsSubType};
Expand All @@ -39,7 +39,7 @@ impl<T: Config<I>, I: 'static> CallHelper<T, I> {
///
/// - call is `receive_messages_delivery_proof` and all messages confirmations have been
/// received.
pub fn was_successful(info: &MessagesCallInfo) -> bool {
pub fn was_successful(info: &MessagesCallInfo<T::LaneId>) -> bool {
match info {
MessagesCallInfo::ReceiveMessagesProof(info) => {
let inbound_lane_data = match InboundLanes::<T, I>::get(info.base.lane_id) {
Expand Down Expand Up @@ -75,19 +75,21 @@ pub trait CallSubType<T: Config<I, RuntimeCall = Self>, I: 'static>:
IsSubType<CallableCallFor<Pallet<T, I>, T>>
{
/// Create a new instance of `ReceiveMessagesProofInfo` from a `ReceiveMessagesProof` call.
fn receive_messages_proof_info(&self) -> Option<ReceiveMessagesProofInfo>;
fn receive_messages_proof_info(&self) -> Option<ReceiveMessagesProofInfo<T::LaneId>>;

/// Create a new instance of `ReceiveMessagesDeliveryProofInfo` from
/// a `ReceiveMessagesDeliveryProof` call.
fn receive_messages_delivery_proof_info(&self) -> Option<ReceiveMessagesDeliveryProofInfo>;
fn receive_messages_delivery_proof_info(
&self,
) -> Option<ReceiveMessagesDeliveryProofInfo<T::LaneId>>;

/// Create a new instance of `MessagesCallInfo` from a `ReceiveMessagesProof`
/// or a `ReceiveMessagesDeliveryProof` call.
fn call_info(&self) -> Option<MessagesCallInfo>;
fn call_info(&self) -> Option<MessagesCallInfo<T::LaneId>>;

/// Create a new instance of `MessagesCallInfo` from a `ReceiveMessagesProof`
/// or a `ReceiveMessagesDeliveryProof` call, if the call is for the provided lane.
fn call_info_for(&self, lane_id: LaneId) -> Option<MessagesCallInfo>;
fn call_info_for(&self, lane_id: T::LaneId) -> Option<MessagesCallInfo<T::LaneId>>;

/// Ensures that a `ReceiveMessagesProof` or a `ReceiveMessagesDeliveryProof` call:
///
Expand All @@ -114,7 +116,7 @@ impl<
I: 'static,
> CallSubType<T, I> for T::RuntimeCall
{
fn receive_messages_proof_info(&self) -> Option<ReceiveMessagesProofInfo> {
fn receive_messages_proof_info(&self) -> Option<ReceiveMessagesProofInfo<T::LaneId>> {
if let Some(crate::Call::<T, I>::receive_messages_proof { ref proof, .. }) =
self.is_sub_type()
{
Expand All @@ -135,7 +137,9 @@ impl<
None
}

fn receive_messages_delivery_proof_info(&self) -> Option<ReceiveMessagesDeliveryProofInfo> {
fn receive_messages_delivery_proof_info(
&self,
) -> Option<ReceiveMessagesDeliveryProofInfo<T::LaneId>> {
if let Some(crate::Call::<T, I>::receive_messages_delivery_proof {
ref proof,
ref relayers_state,
Expand All @@ -159,7 +163,7 @@ impl<
None
}

fn call_info(&self) -> Option<MessagesCallInfo> {
fn call_info(&self) -> Option<MessagesCallInfo<T::LaneId>> {
if let Some(info) = self.receive_messages_proof_info() {
return Some(MessagesCallInfo::ReceiveMessagesProof(info))
}
Expand All @@ -171,7 +175,7 @@ impl<
None
}

fn call_info_for(&self, lane_id: LaneId) -> Option<MessagesCallInfo> {
fn call_info_for(&self, lane_id: T::LaneId) -> Option<MessagesCallInfo<T::LaneId>> {
self.call_info().filter(|info| {
let actual_lane_id = match info {
MessagesCallInfo::ReceiveMessagesProof(info) => info.base.lane_id,
Expand Down Expand Up @@ -251,10 +255,6 @@ mod tests {
};
use sp_std::ops::RangeInclusive;

fn test_lane_id() -> LaneId {
LaneId::new(1, 2)
}

fn fill_unrewarded_relayers() {
let mut inbound_lane_state = InboundLanes::<TestRuntime>::get(test_lane_id()).unwrap();
for n in 0..BridgedChain::MAX_UNREWARDED_RELAYERS_IN_CONFIRMATION_TX {
Expand Down
10 changes: 6 additions & 4 deletions bridges/modules/messages/src/inbound_lane.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ use crate::{BridgedChainOf, Config};

use bp_messages::{
target_chain::{DispatchMessage, DispatchMessageData, MessageDispatch},
ChainWithMessages, DeliveredMessages, InboundLaneData, LaneId, LaneState, MessageKey,
MessageNonce, OutboundLaneData, ReceptionResult, UnrewardedRelayer,
ChainWithMessages, DeliveredMessages, InboundLaneData, LaneState, MessageKey, MessageNonce,
OutboundLaneData, ReceptionResult, UnrewardedRelayer,
};
use bp_runtime::AccountIdOf;
use codec::{Decode, Encode, EncodeLike, MaxEncodedLen};
Expand All @@ -33,9 +33,11 @@ use sp_std::prelude::PartialEq;
pub trait InboundLaneStorage {
/// Id of relayer on source chain.
type Relayer: Clone + PartialEq;
/// Lane identifier type.
type LaneId: Encode;

/// Lane id.
fn id(&self) -> LaneId;
fn id(&self) -> Self::LaneId;
/// Return maximal number of unrewarded relayer entries in inbound lane.
fn max_unrewarded_relayer_entries(&self) -> MessageNonce;
/// Return maximal number of unconfirmed messages in inbound lane.
Expand Down Expand Up @@ -181,7 +183,7 @@ impl<S: InboundLaneStorage> InboundLane<S> {
}

/// Receive new message.
pub fn receive_message<Dispatch: MessageDispatch>(
pub fn receive_message<Dispatch: MessageDispatch<LaneId = S::LaneId>>(
&mut self,
relayer_at_bridged_chain: &S::Relayer,
nonce: MessageNonce,
Expand Down
32 changes: 17 additions & 15 deletions bridges/modules/messages/src/lanes_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ use crate::{
};

use bp_messages::{
target_chain::MessageDispatch, ChainWithMessages, InboundLaneData, LaneId, LaneState,
MessageKey, MessageNonce, OutboundLaneData,
target_chain::MessageDispatch, ChainWithMessages, InboundLaneData, LaneState, MessageKey,
MessageNonce, OutboundLaneData,
};
use bp_runtime::AccountIdOf;
use codec::{Decode, Encode, MaxEncodedLen};
Expand Down Expand Up @@ -68,7 +68,7 @@ impl<T: Config<I>, I: 'static> LanesManager<T, I> {
/// Create new inbound lane in `Opened` state.
pub fn create_inbound_lane(
&self,
lane_id: LaneId,
lane_id: T::LaneId,
) -> Result<InboundLane<RuntimeInboundLaneStorage<T, I>>, LanesManagerError> {
InboundLanes::<T, I>::try_mutate(lane_id, |lane| match lane {
Some(_) => Err(LanesManagerError::InboundLaneAlreadyExists),
Expand All @@ -87,7 +87,7 @@ impl<T: Config<I>, I: 'static> LanesManager<T, I> {
/// Create new outbound lane in `Opened` state.
pub fn create_outbound_lane(
&self,
lane_id: LaneId,
lane_id: T::LaneId,
) -> Result<OutboundLane<RuntimeOutboundLaneStorage<T, I>>, LanesManagerError> {
OutboundLanes::<T, I>::try_mutate(lane_id, |lane| match lane {
Some(_) => Err(LanesManagerError::OutboundLaneAlreadyExists),
Expand All @@ -103,46 +103,46 @@ impl<T: Config<I>, I: 'static> LanesManager<T, I> {
/// Get existing inbound lane, checking that it is in usable state.
pub fn active_inbound_lane(
&self,
lane_id: LaneId,
lane_id: T::LaneId,
) -> Result<InboundLane<RuntimeInboundLaneStorage<T, I>>, LanesManagerError> {
Ok(InboundLane::new(RuntimeInboundLaneStorage::from_lane_id(lane_id, true)?))
}

/// Get existing outbound lane, checking that it is in usable state.
pub fn active_outbound_lane(
&self,
lane_id: LaneId,
lane_id: T::LaneId,
) -> Result<OutboundLane<RuntimeOutboundLaneStorage<T, I>>, LanesManagerError> {
Ok(OutboundLane::new(RuntimeOutboundLaneStorage::from_lane_id(lane_id, true)?))
}

/// Get existing inbound lane without any additional state checks.
pub fn any_state_inbound_lane(
&self,
lane_id: LaneId,
lane_id: T::LaneId,
) -> Result<InboundLane<RuntimeInboundLaneStorage<T, I>>, LanesManagerError> {
Ok(InboundLane::new(RuntimeInboundLaneStorage::from_lane_id(lane_id, false)?))
}

/// Get existing outbound lane without any additional state checks.
pub fn any_state_outbound_lane(
&self,
lane_id: LaneId,
lane_id: T::LaneId,
) -> Result<OutboundLane<RuntimeOutboundLaneStorage<T, I>>, LanesManagerError> {
Ok(OutboundLane::new(RuntimeOutboundLaneStorage::from_lane_id(lane_id, false)?))
}
}

/// Runtime inbound lane storage.
pub struct RuntimeInboundLaneStorage<T: Config<I>, I: 'static = ()> {
pub(crate) lane_id: LaneId,
pub(crate) lane_id: T::LaneId,
pub(crate) cached_data: InboundLaneData<AccountIdOf<BridgedChainOf<T, I>>>,
}

impl<T: Config<I>, I: 'static> RuntimeInboundLaneStorage<T, I> {
/// Creates new runtime inbound lane storage for given **existing** lane.
fn from_lane_id(
lane_id: LaneId,
lane_id: T::LaneId,
check_active: bool,
) -> Result<RuntimeInboundLaneStorage<T, I>, LanesManagerError> {
let cached_data =
Expand Down Expand Up @@ -196,8 +196,9 @@ impl<T: Config<I>, I: 'static> RuntimeInboundLaneStorage<T, I> {

impl<T: Config<I>, I: 'static> InboundLaneStorage for RuntimeInboundLaneStorage<T, I> {
type Relayer = AccountIdOf<BridgedChainOf<T, I>>;
type LaneId = T::LaneId;

fn id(&self) -> LaneId {
fn id(&self) -> Self::LaneId {
self.lane_id
}

Expand Down Expand Up @@ -225,15 +226,15 @@ impl<T: Config<I>, I: 'static> InboundLaneStorage for RuntimeInboundLaneStorage<

/// Runtime outbound lane storage.
#[derive(Debug, PartialEq, Eq)]
pub struct RuntimeOutboundLaneStorage<T, I = ()> {
pub(crate) lane_id: LaneId,
pub struct RuntimeOutboundLaneStorage<T: Config<I>, I: 'static> {
pub(crate) lane_id: T::LaneId,
pub(crate) cached_data: OutboundLaneData,
pub(crate) _phantom: PhantomData<(T, I)>,
}

impl<T: Config<I>, I: 'static> RuntimeOutboundLaneStorage<T, I> {
/// Creates new runtime outbound lane storage for given **existing** lane.
fn from_lane_id(lane_id: LaneId, check_active: bool) -> Result<Self, LanesManagerError> {
fn from_lane_id(lane_id: T::LaneId, check_active: bool) -> Result<Self, LanesManagerError> {
let cached_data =
OutboundLanes::<T, I>::get(lane_id).ok_or(LanesManagerError::UnknownOutboundLane)?;
ensure!(
Expand All @@ -246,8 +247,9 @@ impl<T: Config<I>, I: 'static> RuntimeOutboundLaneStorage<T, I> {

impl<T: Config<I>, I: 'static> OutboundLaneStorage for RuntimeOutboundLaneStorage<T, I> {
type StoredMessagePayload = StoredMessagePayload<T, I>;
type LaneId = T::LaneId;

fn id(&self) -> LaneId {
fn id(&self) -> Self::LaneId {
self.lane_id
}

Expand Down
Loading

0 comments on commit 14b8469

Please sign in to comment.