Skip to content

Commit 5abfea8

Browse files
committed
Added event MessagesReceived for better visibility on receiving side
1 parent 82a849d commit 5abfea8

File tree

5 files changed

+106
-28
lines changed

5 files changed

+106
-28
lines changed

modules/messages/src/inbound_lane.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -181,12 +181,12 @@ impl<S: InboundLaneStorage> InboundLane<S> {
181181
}
182182

183183
/// Receive new message.
184-
pub fn receive_message<P: MessageDispatch<AccountId>, AccountId>(
184+
pub fn receive_message<Dispatch: MessageDispatch<AccountId>, AccountId>(
185185
&mut self,
186186
relayer_at_bridged_chain: &S::Relayer,
187187
relayer_at_this_chain: &AccountId,
188188
nonce: MessageNonce,
189-
message_data: DispatchMessageData<P::DispatchPayload>,
189+
message_data: DispatchMessageData<Dispatch::DispatchPayload>,
190190
) -> ReceivalResult {
191191
let mut data = self.storage.data();
192192
let is_correct_message = nonce == data.last_delivered_nonce() + 1;
@@ -206,7 +206,7 @@ impl<S: InboundLaneStorage> InboundLane<S> {
206206
}
207207

208208
// then, dispatch message
209-
let dispatch_result = P::dispatch(
209+
let dispatch_result = Dispatch::dispatch(
210210
relayer_at_this_chain,
211211
DispatchMessage {
212212
key: MessageKey { lane_id: self.storage.id(), nonce },

modules/messages/src/lib.rs

Lines changed: 57 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ pub const LOG_TARGET: &str = "runtime::bridge-messages";
9191
#[frame_support::pallet]
9292
pub mod pallet {
9393
use super::*;
94+
use bp_messages::{NotDispatchedReason, ReceivedMessageResult, ReceivedMessages};
9495
use frame_support::pallet_prelude::*;
9596
use frame_system::pallet_prelude::*;
9697

@@ -298,6 +299,7 @@ pub mod pallet {
298299
// dispatch messages and (optionally) update lane(s) state(s)
299300
let mut total_messages = 0;
300301
let mut valid_messages = 0;
302+
let mut messages_received_status = Vec::with_capacity(messages.len());
301303
let mut dispatch_weight_left = dispatch_weight;
302304
for (lane_id, lane_data) in messages {
303305
let mut lane = inbound_lane::<T, I>(lane_id);
@@ -314,24 +316,41 @@ pub mod pallet {
314316
}
315317
}
316318

319+
let mut lane_messages_received_status =
320+
Vec::with_capacity(lane_data.messages.len());
321+
let mut is_lane_processing_stopped_no_weight_left = false;
322+
317323
for mut message in lane_data.messages {
318324
debug_assert_eq!(message.key.lane_id, lane_id);
325+
total_messages += 1;
326+
327+
if is_lane_processing_stopped_no_weight_left {
328+
lane_messages_received_status.push((
329+
message.key.nonce,
330+
NotDispatchedReason::NotEnoughWeightForLane.into(),
331+
));
332+
continue
333+
}
319334

320335
// ensure that relayer has declared enough weight for dispatching next message
321336
// on this lane. We can't dispatch lane messages out-of-order, so if declared
322337
// weight is not enough, let's move to next lane
323-
let dispatch_weight = T::MessageDispatch::dispatch_weight(&mut message);
324-
if dispatch_weight.any_gt(dispatch_weight_left) {
338+
let message_dispatch_weight = T::MessageDispatch::dispatch_weight(&mut message);
339+
if message_dispatch_weight.any_gt(dispatch_weight_left) {
325340
log::trace!(
326341
target: LOG_TARGET,
327342
"Cannot dispatch any more messages on lane {:?}. Weight: declared={}, left={}",
328343
lane_id,
329-
dispatch_weight,
344+
message_dispatch_weight,
330345
dispatch_weight_left,
331346
);
332-
break
347+
lane_messages_received_status.push((
348+
message.key.nonce,
349+
NotDispatchedReason::NotEnoughWeightForLane.into(),
350+
));
351+
is_lane_processing_stopped_no_weight_left = true;
352+
continue
333353
}
334-
total_messages += 1;
335354

336355
let receival_result = lane.receive_message::<T::MessageDispatch, T::AccountId>(
337356
&relayer_id_at_bridged_chain,
@@ -349,18 +368,38 @@ pub mod pallet {
349368
let (unspent_weight, refund_pay_dispatch_fee) = match receival_result {
350369
ReceivalResult::Dispatched(dispatch_result) => {
351370
valid_messages += 1;
371+
lane_messages_received_status
372+
.push((message.key.nonce, ReceivedMessageResult::Dispatched));
352373
(
353374
dispatch_result.unspent_weight,
354375
!dispatch_result.dispatch_fee_paid_during_dispatch,
355376
)
356377
},
357-
ReceivalResult::InvalidNonce |
358-
ReceivalResult::TooManyUnrewardedRelayers |
359-
ReceivalResult::TooManyUnconfirmedMessages => (dispatch_weight, true),
378+
ReceivalResult::InvalidNonce => {
379+
lane_messages_received_status.push((
380+
message.key.nonce,
381+
NotDispatchedReason::InvalidNonce.into(),
382+
));
383+
(message_dispatch_weight, true)
384+
},
385+
ReceivalResult::TooManyUnrewardedRelayers => {
386+
lane_messages_received_status.push((
387+
message.key.nonce,
388+
NotDispatchedReason::TooManyUnrewardedRelayers.into(),
389+
));
390+
(message_dispatch_weight, true)
391+
},
392+
ReceivalResult::TooManyUnconfirmedMessages => {
393+
lane_messages_received_status.push((
394+
message.key.nonce,
395+
NotDispatchedReason::TooManyUnconfirmedMessages.into(),
396+
));
397+
(message_dispatch_weight, true)
398+
},
360399
};
361400

362-
let unspent_weight = unspent_weight.min(dispatch_weight);
363-
dispatch_weight_left -= dispatch_weight - unspent_weight;
401+
let unspent_weight = unspent_weight.min(message_dispatch_weight);
402+
dispatch_weight_left -= message_dispatch_weight - unspent_weight;
364403
actual_weight = actual_weight.saturating_sub(unspent_weight).saturating_sub(
365404
// delivery call weight formula assumes that the fee is paid at
366405
// this (target) chain. If the message is prepaid at the source
@@ -372,9 +411,12 @@ pub mod pallet {
372411
},
373412
);
374413
}
414+
415+
messages_received_status
416+
.push(ReceivedMessages::new(lane_id, lane_messages_received_status));
375417
}
376418

377-
log::trace!(
419+
log::debug!(
378420
target: LOG_TARGET,
379421
"Received messages: total={}, valid={}. Weight used: {}/{}",
380422
total_messages,
@@ -383,6 +425,8 @@ pub mod pallet {
383425
declared_weight,
384426
);
385427

428+
Self::deposit_event(Event::MessagesReceived(messages_received_status));
429+
386430
Ok(PostDispatchInfo { actual_weight: Some(actual_weight), pays_fee: Pays::Yes })
387431
}
388432

@@ -494,6 +538,8 @@ pub mod pallet {
494538
pub enum Event<T: Config<I>, I: 'static = ()> {
495539
/// Message has been accepted and is waiting to be delivered.
496540
MessageAccepted { lane_id: LaneId, nonce: MessageNonce },
541+
/// Messages have been received from the bridged chain.
542+
MessagesReceived(Vec<ReceivedMessages<ReceivedMessageResult>>),
497543
/// Messages in the inclusive range have been delivered to the bridged chain.
498544
MessagesDelivered { lane_id: LaneId, messages: DeliveredMessages },
499545
}

primitives/messages/src/lib.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,50 @@ pub struct UnrewardedRelayer<RelayerId> {
218218
pub messages: DeliveredMessages,
219219
}
220220

221+
/// Received messages with their dispatch result.
222+
#[derive(Clone, Default, Encode, Decode, RuntimeDebug, PartialEq, Eq, TypeInfo)]
223+
pub struct ReceivedMessages<Result> {
224+
pub lane: LaneId,
225+
pub receive_results: Vec<(MessageNonce, Result)>,
226+
}
227+
228+
impl<Result> ReceivedMessages<Result> {
229+
pub fn new(lane: LaneId, receive_results: Vec<(MessageNonce, Result)>) -> Self {
230+
ReceivedMessages { lane, receive_results }
231+
}
232+
}
233+
234+
/// Result of single message receival.
235+
#[derive(RuntimeDebug, Encode, Decode, PartialEq, Eq, Clone, TypeInfo)]
236+
pub enum ReceivedMessageResult {
237+
/// Message has been received and dispatched. Note that we don't care whether dispatch has
238+
/// been successful or not - in both case message falls into this category.
239+
///
240+
/// The message dispatch result is also returned.
241+
Dispatched,
242+
/// Reason why messages was not dispatched
243+
NotDispatched(NotDispatchedReason),
244+
}
245+
246+
/// Result of single message receival.
247+
#[derive(RuntimeDebug, Encode, Decode, PartialEq, Eq, Clone, TypeInfo)]
248+
pub enum NotDispatchedReason {
249+
/// Message has invalid nonce and lane has rejected to accept this message.
250+
InvalidNonce,
251+
/// There are too many unrewarded relayer entries at the lane.
252+
TooManyUnrewardedRelayers,
253+
/// There are too many unconfirmed messages at the lane.
254+
TooManyUnconfirmedMessages,
255+
/// There are too many unconfirmed messages at the lane.
256+
NotEnoughWeightForLane,
257+
}
258+
259+
impl From<NotDispatchedReason> for ReceivedMessageResult {
260+
fn from(value: NotDispatchedReason) -> Self {
261+
ReceivedMessageResult::NotDispatched(value)
262+
}
263+
}
264+
221265
/// Delivered messages with their dispatch result.
222266
#[derive(Clone, Default, Encode, Decode, RuntimeDebug, PartialEq, Eq, TypeInfo)]
223267
pub struct DeliveredMessages {

scripts/send-message-from-millau-rialto.sh

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,14 @@ case "$1" in
1515
--source-host localhost \
1616
--source-port $MILLAU_PORT \
1717
--source-signer //Alice \
18-
--target-signer //Bob \
19-
--lane 00000000 \
20-
--origin Target \
21-
remark \
18+
raw 020419ac
2219
;;
2320
transfer)
2421
RUST_LOG=runtime=trace,substrate-relay=trace,bridge=trace \
2522
./target/debug/substrate-relay send-message millau-to-rialto \
2623
--source-host localhost \
2724
--source-port $MILLAU_PORT \
2825
--source-signer //Alice \
29-
--target-signer //Bob \
30-
--lane 00000000 \
31-
--origin Target \
3226
transfer \
3327
--amount 100000000000000 \
3428
--recipient 5DZvVvd1udr61vL7Xks17TFQ4fi9NiagYLaBobnbPCP14ewA \

scripts/send-message-from-rialto-millau.sh

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,15 @@ case "$1" in
1414
./target/debug/substrate-relay send-message rialto-to-millau \
1515
--source-host localhost \
1616
--source-port $RIALTO_PORT \
17-
--target-signer //Alice \
1817
--source-signer //Bob \
19-
--lane 00000000 \
20-
--origin Target \
21-
remark \
18+
raw 020419ac
2219
;;
2320
transfer)
2421
RUST_LOG=runtime=trace,substrate-relay=trace,bridge=trace \
2522
./target/debug/substrate-relay send-message rialto-to-millau \
2623
--source-host localhost \
2724
--source-port $RIALTO_PORT \
28-
--target-signer //Alice \
2925
--source-signer //Bob \
30-
--lane 00000000 \
31-
--origin Target \
3226
transfer \
3327
--amount 100000000000000 \
3428
--recipient 5DZvVvd1udr61vL7Xks17TFQ4fi9NiagYLaBobnbPCP14ewA \

0 commit comments

Comments
 (0)