Skip to content

Commit

Permalink
use BridgedChain::Account instead of InbundRelayer (#2193)
Browse files Browse the repository at this point in the history
  • Loading branch information
svyatonik authored and bkontur committed May 14, 2024
1 parent 4b4bbb6 commit a97583c
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 43 deletions.
9 changes: 4 additions & 5 deletions bridges/bin/runtime-common/src/messages_call_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,14 @@
// You should have received a copy of the GNU General Public License
// along with Parity Bridges Common. If not, see <http://www.gnu.org/licenses/>.

//! Signed extension for the `pallet-bridge-messages` that is able to reject obsolete
//! (and some other invalid) transactions.
//! Helpers for easier manipulation of call processing with signed extensions.

use bp_messages::{
target_chain::MessageDispatch, ChainWithMessages, InboundLaneData, LaneId, MessageNonce,
};
use bp_runtime::OwnedBridgeModule;
use bp_runtime::{AccountIdOf, OwnedBridgeModule};
use frame_support::{dispatch::CallableCallFor, traits::IsSubType};
use pallet_bridge_messages::{Config, Pallet};
use pallet_bridge_messages::{BridgedChainOf, Config, Pallet};
use sp_runtime::{transaction_validity::TransactionValidity, RuntimeDebug};
use sp_std::ops::RangeInclusive;

Expand Down Expand Up @@ -326,7 +325,7 @@ impl<

/// Returns occupation state of unrewarded relayers vector.
fn unrewarded_relayers_occupation<T: Config<I>, I: 'static>(
inbound_lane_data: &InboundLaneData<T::InboundRelayer>,
inbound_lane_data: &InboundLaneData<AccountIdOf<BridgedChainOf<T, I>>>,
) -> UnrewardedRelayerOccupation {
UnrewardedRelayerOccupation {
free_relayer_slots: T::BridgedChain::MAX_UNREWARDED_RELAYERS_IN_CONFIRMATION_TX
Expand Down
1 change: 0 additions & 1 deletion bridges/bin/runtime-common/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,6 @@ impl pallet_bridge_messages::Config for TestRuntime {
type OutboundPayload = XcmAsPlainPayload;

type InboundPayload = Vec<u8>;
type InboundRelayer = BridgedChainAccountId;
type DeliveryPayments = ();

type DeliveryConfirmationPayments = pallet_bridge_relayers::DeliveryConfirmationPaymentsAdapter<
Expand Down
10 changes: 5 additions & 5 deletions bridges/modules/messages/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use bp_messages::{
InboundLaneData, LaneId, MessageNonce, OutboundLaneData, UnrewardedRelayer,
UnrewardedRelayersState,
};
use bp_runtime::{HashOf, StorageProofSize};
use bp_runtime::{AccountIdOf, HashOf, StorageProofSize};
use codec::Decode;
use frame_benchmarking::{account, v2::*};
use frame_support::weights::Weight;
Expand Down Expand Up @@ -83,8 +83,8 @@ pub trait Config<I: 'static>: crate::Config<I> {
/// Return id of relayer account at the bridged chain.
///
/// By default, zero account is returned.
fn bridged_relayer_id() -> Self::InboundRelayer {
Self::InboundRelayer::decode(&mut TrailingZeroInput::zeroes()).unwrap()
fn bridged_relayer_id() -> AccountIdOf<BridgedChainOf<Self, I>> {
Decode::decode(&mut TrailingZeroInput::zeroes()).unwrap()
}

/// Create given account and give it enough balance for test purposes. Used to create
Expand Down Expand Up @@ -132,7 +132,7 @@ fn receive_messages<T: Config<I>, I: 'static>(nonce: MessageNonce) {
}

struct ReceiveMessagesProofSetup<T: Config<I>, I: 'static> {
relayer_id_on_src: T::InboundRelayer,
relayer_id_on_src: AccountIdOf<BridgedChainOf<T, I>>,
relayer_id_on_tgt: T::AccountId,
msgs_count: u32,
_phantom_data: sp_std::marker::PhantomData<I>,
Expand All @@ -155,7 +155,7 @@ impl<T: Config<I>, I: 'static> ReceiveMessagesProofSetup<T, I> {
setup
}

fn relayer_id_on_src(&self) -> T::InboundRelayer {
fn relayer_id_on_src(&self) -> AccountIdOf<BridgedChainOf<T, I>> {
self.relayer_id_on_src.clone()
}

Expand Down
15 changes: 9 additions & 6 deletions bridges/modules/messages/src/inbound_lane.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use bp_messages::{
ChainWithMessages, DeliveredMessages, InboundLaneData, LaneId, MessageKey, MessageNonce,
OutboundLaneData, ReceptionResult, UnrewardedRelayer,
};
use bp_runtime::AccountIdOf;
use codec::{Decode, Encode, EncodeLike, MaxEncodedLen};
use scale_info::{Type, TypeInfo};
use sp_runtime::RuntimeDebug;
Expand Down Expand Up @@ -54,10 +55,12 @@ pub trait InboundLaneStorage {
///
/// The encoding of this type matches encoding of the corresponding `MessageData`.
#[derive(Encode, Decode, Clone, RuntimeDebug, PartialEq, Eq)]
pub struct StoredInboundLaneData<T: Config<I>, I: 'static>(pub InboundLaneData<T::InboundRelayer>);
pub struct StoredInboundLaneData<T: Config<I>, I: 'static>(
pub InboundLaneData<AccountIdOf<BridgedChainOf<T, I>>>,
);

impl<T: Config<I>, I: 'static> sp_std::ops::Deref for StoredInboundLaneData<T, I> {
type Target = InboundLaneData<T::InboundRelayer>;
type Target = InboundLaneData<AccountIdOf<BridgedChainOf<T, I>>>;

fn deref(&self) -> &Self::Target {
&self.0
Expand All @@ -77,29 +80,29 @@ impl<T: Config<I>, I: 'static> Default for StoredInboundLaneData<T, I> {
}

impl<T: Config<I>, I: 'static> From<StoredInboundLaneData<T, I>>
for InboundLaneData<T::InboundRelayer>
for InboundLaneData<AccountIdOf<BridgedChainOf<T, I>>>
{
fn from(data: StoredInboundLaneData<T, I>) -> Self {
data.0
}
}

impl<T: Config<I>, I: 'static> EncodeLike<StoredInboundLaneData<T, I>>
for InboundLaneData<T::InboundRelayer>
for InboundLaneData<AccountIdOf<BridgedChainOf<T, I>>>
{
}

impl<T: Config<I>, I: 'static> TypeInfo for StoredInboundLaneData<T, I> {
type Identity = Self;

fn type_info() -> Type {
InboundLaneData::<T::InboundRelayer>::type_info()
InboundLaneData::<AccountIdOf<BridgedChainOf<T, I>>>::type_info()
}
}

impl<T: Config<I>, I: 'static> MaxEncodedLen for StoredInboundLaneData<T, I> {
fn max_encoded_len() -> usize {
InboundLaneData::<T::InboundRelayer>::encoded_size_hint(
InboundLaneData::<AccountIdOf<BridgedChainOf<T, I>>>::encoded_size_hint(
BridgedChainOf::<T, I>::MAX_UNREWARDED_RELAYERS_IN_CONFIRMATION_TX as usize,
)
.unwrap_or(usize::MAX)
Expand Down
38 changes: 17 additions & 21 deletions bridges/modules/messages/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ use bp_messages::{
OutboundMessageDetails, UnrewardedRelayersState, VerificationError,
};
use bp_runtime::{
BasicOperatingMode, HashOf, OwnedBridgeModule, PreComputedSize, RangeInclusiveExt, Size,
AccountIdOf, BasicOperatingMode, HashOf, OwnedBridgeModule, PreComputedSize, RangeInclusiveExt,
Size,
};
use codec::{Decode, Encode, MaxEncodedLen};
use frame_support::{dispatch::PostDispatchInfo, ensure, fail, traits::Get, DefaultNoBound};
Expand Down Expand Up @@ -120,26 +121,19 @@ pub mod pallet {

/// Payload type of outbound messages. This payload is dispatched on the bridged chain.
type OutboundPayload: Parameter + Size;

/// Payload type of inbound messages. This payload is dispatched on this chain.
type InboundPayload: Decode;
/// Identifier of relayer that deliver messages to this chain. Relayer reward is paid on the
/// bridged chain.
type InboundRelayer: Parameter + MaxEncodedLen;

// Types that are used by outbound_lane (on source chain).

/// Delivery confirmation payments.
/// Handler for relayer payments that happen during message delivery transaction.
type DeliveryPayments: DeliveryPayments<Self::AccountId>;
/// Handler for relayer payments that happen during message delivery confirmation
/// transaction.
type DeliveryConfirmationPayments: DeliveryConfirmationPayments<Self::AccountId>;
/// Delivery confirmation callback.
type OnMessagesDelivered: OnMessagesDelivered;

// Types that are used by inbound_lane (on target chain).

/// Message dispatch.
/// Message dispatch handler.
type MessageDispatch: MessageDispatch<DispatchPayload = Self::InboundPayload>;
/// Delivery payments.
type DeliveryPayments: DeliveryPayments<Self::AccountId>;
}

/// Shortcut to this chain type for Config.
Expand Down Expand Up @@ -241,7 +235,7 @@ pub mod pallet {
#[pallet::weight(T::WeightInfo::receive_messages_proof_weight(proof, *messages_count, *dispatch_weight))]
pub fn receive_messages_proof(
origin: OriginFor<T>,
relayer_id_at_bridged_chain: T::InboundRelayer,
relayer_id_at_bridged_chain: AccountIdOf<BridgedChainOf<T, I>>,
proof: FromBridgedChainMessagesProof<HashOf<BridgedChainOf<T, I>>>,
messages_count: u32,
dispatch_weight: Weight,
Expand Down Expand Up @@ -645,7 +639,9 @@ pub mod pallet {
}

/// Return inbound lane data.
pub fn inbound_lane_data(lane: LaneId) -> InboundLaneData<T::InboundRelayer> {
pub fn inbound_lane_data(
lane: LaneId,
) -> InboundLaneData<AccountIdOf<BridgedChainOf<T, I>>> {
InboundLanes::<T, I>::get(lane).0
}
}
Expand Down Expand Up @@ -744,7 +740,7 @@ fn outbound_lane<T: Config<I>, I: 'static>(
/// Runtime inbound lane storage.
struct RuntimeInboundLaneStorage<T: Config<I>, I: 'static = ()> {
lane_id: LaneId,
cached_data: Option<InboundLaneData<T::InboundRelayer>>,
cached_data: Option<InboundLaneData<AccountIdOf<BridgedChainOf<T, I>>>>,
_phantom: PhantomData<I>,
}

Expand All @@ -768,14 +764,14 @@ impl<T: Config<I>, I: 'static> RuntimeInboundLaneStorage<T, I> {
let max_encoded_len = StoredInboundLaneData::<T, I>::max_encoded_len();
let relayers_count = self.get_or_init_data().relayers.len();
let actual_encoded_len =
InboundLaneData::<T::InboundRelayer>::encoded_size_hint(relayers_count)
InboundLaneData::<AccountIdOf<BridgedChainOf<T, I>>>::encoded_size_hint(relayers_count)
.unwrap_or(usize::MAX);
max_encoded_len.saturating_sub(actual_encoded_len) as _
}
}

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

fn id(&self) -> LaneId {
self.lane_id
Expand All @@ -789,19 +785,19 @@ impl<T: Config<I>, I: 'static> InboundLaneStorage for RuntimeInboundLaneStorage<
BridgedChainOf::<T, I>::MAX_UNCONFIRMED_MESSAGES_IN_CONFIRMATION_TX
}

fn get_or_init_data(&mut self) -> InboundLaneData<T::InboundRelayer> {
fn get_or_init_data(&mut self) -> InboundLaneData<AccountIdOf<BridgedChainOf<T, I>>> {
match self.cached_data {
Some(ref data) => data.clone(),
None => {
let data: InboundLaneData<T::InboundRelayer> =
let data: InboundLaneData<AccountIdOf<BridgedChainOf<T, I>>> =
InboundLanes::<T, I>::get(self.lane_id).into();
self.cached_data = Some(data.clone());
data
},
}
}

fn set_data(&mut self, data: InboundLaneData<T::InboundRelayer>) {
fn set_data(&mut self, data: InboundLaneData<AccountIdOf<BridgedChainOf<T, I>>>) {
self.cached_data = Some(data.clone());
InboundLanes::<T, I>::insert(self.lane_id, StoredInboundLaneData::<T, I>(data))
}
Expand Down
3 changes: 1 addition & 2 deletions bridges/modules/messages/src/tests/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ impl Chain for BridgedChain {
type Hash = BridgedHeaderHash;
type Hasher = BlakeTwo256;
type Header = BridgedChainHeader;
type AccountId = AccountId;
type AccountId = TestRelayer;
type Balance = Balance;
type Nonce = u64;
type Signature = sp_runtime::MultiSignature;
Expand Down Expand Up @@ -204,7 +204,6 @@ impl Config for TestRuntime {
type OutboundPayload = TestPayload;

type InboundPayload = TestPayload;
type InboundRelayer = TestRelayer;
type DeliveryPayments = TestDeliveryPayments;

type DeliveryConfirmationPayments = TestDeliveryConfirmationPayments;
Expand Down
1 change: 0 additions & 1 deletion bridges/modules/xcm-bridge-hub/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ impl pallet_bridge_messages::Config for TestRuntime {
type ActiveOutboundLanes = ActiveOutboundLanes;
type OutboundPayload = Vec<u8>;
type InboundPayload = Vec<u8>;
type InboundRelayer = ();
type DeliveryPayments = ();
type DeliveryConfirmationPayments = ();
type OnMessagesDelivered = ();
Expand Down
5 changes: 3 additions & 2 deletions bridges/relays/lib-substrate-relay/src/messages/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,9 +307,10 @@ pub struct DirectReceiveMessagesProofCallBuilder<P, R, I> {
impl<P, R, I> ReceiveMessagesProofCallBuilder<P> for DirectReceiveMessagesProofCallBuilder<P, R, I>
where
P: SubstrateMessageLane,
R: BridgeMessagesConfig<I, InboundRelayer = AccountIdOf<P::SourceChain>>,
R: BridgeMessagesConfig<I>,
I: 'static,
R::BridgedChain: bp_runtime::Chain<Hash = HashOf<P::SourceChain>>,
R::BridgedChain:
bp_runtime::Chain<AccountId = AccountIdOf<P::SourceChain>, Hash = HashOf<P::SourceChain>>,
CallOf<P::TargetChain>: From<BridgeMessagesCall<R, I>> + GetDispatchInfo,
{
fn build_receive_messages_proof_call(
Expand Down

0 comments on commit a97583c

Please sign in to comment.