Skip to content
This repository was archived by the owner on Feb 29, 2024. It is now read-only.
Prev Previous commit
Next Next commit
A first review
  • Loading branch information
boundless-forest committed Jun 17, 2022
commit d90fd0809cb47982dee625055fe7ebb8bf54bb35
9 changes: 9 additions & 0 deletions bin/runtime-common/src/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,15 @@ pub mod target {
message.data.payload.as_ref().map(|payload| payload.weight).unwrap_or(0)
}

fn pre_dispatch(
message: &DispatchMessage<Self::DispatchPayload, BalanceOf<BridgedChain<B>>>,
) -> bool {
pallet_bridge_dispatch::Pallet::<ThisRuntime, ThisDispatchInstance>::pre_dispatch(
// TODO: update this type
&message.data.payload.as_ref().unwrap(),
)
}

fn dispatch(
relayer_account: &AccountIdOf<ThisChain<B>>,
message: DispatchMessage<Self::DispatchPayload, BalanceOf<BridgedChain<B>>>,
Expand Down
5 changes: 5 additions & 0 deletions modules/dispatch/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,11 @@ impl<T: Config<I>, I: 'static> MessageDispatch<T::AccountId, T::BridgeMessageId>
message.weight
}

fn pre_dispatch(message: &Self::Message) -> bool {
// T::CallValidator::check_relayer_balance(relayer_account, &dispatch_origin, &call)
true
}

fn dispatch<P: FnOnce(&T::AccountId, bp_message_dispatch::Weight) -> Result<(), ()>>(
source_chain: ChainId,
target_chain: ChainId,
Expand Down
17 changes: 9 additions & 8 deletions modules/messages/src/inbound_lane.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ impl<S: InboundLaneStorage> InboundLane<S> {
}

/// Receive new message.
pub fn receive_message<P: MessageDispatch<AccountId, S::MessageFee>, V: CallValidate<AccountId>, AccountId>(
pub fn receive_message<P: MessageDispatch<AccountId, S::MessageFee>, AccountId>(
&mut self,
relayer_at_bridged_chain: &S::Relayer,
relayer_at_this_chain: &AccountId,
Expand All @@ -142,16 +142,17 @@ impl<S: InboundLaneStorage> InboundLane<S> {
return ReceivalResult::TooManyUnconfirmedMessages
}

let dispatch_message = DispatchMessage {
key: MessageKey { lane_id: self.storage.id(), nonce },
data: message_data,
};
// Ensure relayer has enough balance to pay for the derived account.
if P::pre_dispatch(&dispatch_message) {
return ReceivalResult::RelayerInsufficientBalance
}

// then, dispatch message
let dispatch_result = P::dispatch(
relayer_at_this_chain,
DispatchMessage {
key: MessageKey { lane_id: self.storage.id(), nonce },
data: message_data,
},
);
let dispatch_result = P::dispatch(relayer_at_this_chain, dispatch_message);

// now let's update inbound lane storage
let push_new = match data.relayers.back_mut() {
Expand Down
1 change: 1 addition & 0 deletions modules/messages/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,7 @@ pub mod pallet {
},
ReceivalResult::InvalidNonce |
ReceivalResult::TooManyUnrewardedRelayers |
ReceivalResult::RelayerInsufficientBalance |
ReceivalResult::TooManyUnconfirmedMessages => (dispatch_weight, true),
};

Expand Down
2 changes: 2 additions & 0 deletions primitives/message-dispatch/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ pub trait MessageDispatch<AccountId, BridgeMessageId> {
/// of dispatch weight.
fn dispatch_weight(message: &Self::Message) -> Weight;

fn pre_dispatch(message: &Self::Message) -> bool;

/// Dispatches the message internally.
///
/// `source_chain` indicates the chain where the message came from.
Expand Down
5 changes: 5 additions & 0 deletions primitives/messages/src/target_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ pub trait MessageDispatch<AccountId, Fee> {
/// of dispatch weight.
fn dispatch_weight(message: &DispatchMessage<Self::DispatchPayload, Fee>) -> Weight;

fn pre_dispatch(message: &DispatchMessage<Self::DispatchPayload, Fee>) -> bool;
/// Called when inbound message is received.
///
/// It is up to the implementers of this trait to determine whether the message
Expand Down Expand Up @@ -160,6 +161,10 @@ impl<AccountId, Fee> MessageDispatch<AccountId, Fee> for ForbidInboundMessages {
Weight::MAX
}

fn pre_dispatch(_message: &DispatchMessage<Self::DispatchPayload, Fee>) -> bool {
false
}

fn dispatch(
_: &AccountId,
_: DispatchMessage<Self::DispatchPayload, Fee>,
Expand Down