Skip to content
This repository has been archived by the owner on Feb 29, 2024. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Guantong committed Jan 6, 2023
1 parent 030a562 commit b96b41c
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 19 deletions.
2 changes: 1 addition & 1 deletion modules/messages/src/inbound_lane.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ impl<S: InboundLaneStorage> InboundLane<S> {
relayer_at_this_chain: &AccountId,
nonce: MessageNonce,
message_data: DispatchMessageData<Dispatch::DispatchPayload, S::MessageFee>,
) -> ReceivalResult {
) -> ReceivalResult<Dispatch::DispatchLevelResult> {
let mut data = self.storage.data();
let is_correct_message = nonce == data.last_delivered_nonce() + 1;
if !is_correct_message {
Expand Down
8 changes: 7 additions & 1 deletion modules/messages/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -607,7 +607,13 @@ pub mod pallet {
/// Message has been accepted and is waiting to be delivered.
MessageAccepted { lane_id: LaneId, nonce: MessageNonce },
/// Messages have been received from the bridged chain.
MessagesReceived(Vec<ReceivedMessages<ReceivalResult>>),
MessagesReceived(
Vec<
ReceivedMessages<
<T::MessageDispatch as MessageDispatch<T::AccountId>>::DispatchLevelResult,
>,
>,
),
/// Messages in the inclusive range have been delivered to the bridged chain.
MessagesDelivered { lane_id: LaneId, messages: DeliveredMessages },
}
Expand Down
11 changes: 8 additions & 3 deletions modules/messages/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ pub type Balance = u64;

pub type TestMessageFee = u64;
pub type TestRelayer = u64;
pub type TestDispatchLevelResult = ();

type Block = MockBlock<TestRuntime>;
type UncheckedExtrinsic = MockUncheckedExtrinsic<TestRuntime>;
Expand Down Expand Up @@ -103,7 +104,7 @@ pub struct TestPayload {
///
/// Note: in correct code `dispatch_result.unspent_weight` will always be <= `declared_weight`,
/// but for test purposes we'll be making it larger than `declared_weight` sometimes.
pub dispatch_result: MessageDispatchResult,
pub dispatch_result: MessageDispatchResult<TestDispatchLevelResult>,
/// Extra bytes that affect payload size.
pub extra: Vec<u8>,
}
Expand Down Expand Up @@ -514,6 +515,7 @@ impl SourceHeaderChain<TestMessageFee> for TestSourceHeaderChain {
pub struct TestMessageDispatch;
impl MessageDispatch<AccountId, TestMessageFee> for TestMessageDispatch {
type DispatchPayload = TestPayload;
type DispatchLevelResult = TestDispatchLevelResult;

fn dispatch_weight(message: &mut DispatchMessage<TestPayload, TestMessageFee>) -> Weight {
match message.data.payload.as_ref() {
Expand All @@ -532,7 +534,7 @@ impl MessageDispatch<AccountId, TestMessageFee> for TestMessageDispatch {
fn dispatch(
_relayer_account: &AccountId,
message: DispatchMessage<TestPayload, TestMessageFee>,
) -> MessageDispatchResult {
) -> MessageDispatchResult<TestDispatchLevelResult> {
match message.data.payload.as_ref() {
Ok(payload) => payload.dispatch_result.clone(),
Err(_) => dispatch_result(0),
Expand Down Expand Up @@ -561,11 +563,14 @@ pub fn message_data(payload: TestPayload) -> MessageData<TestMessageFee> {
}

/// Returns message dispatch result with given unspent weight.
pub const fn dispatch_result(unspent_weight: u64) -> MessageDispatchResult {
pub const fn dispatch_result(
unspent_weight: u64,
) -> MessageDispatchResult<TestDispatchLevelResult> {
MessageDispatchResult {
dispatch_result: true,
unspent_weight: Weight::from_ref_time(unspent_weight),
dispatch_fee_paid_during_dispatch: true,
dispatch_level_result: (),
}
}

Expand Down
21 changes: 11 additions & 10 deletions primitives/messages/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ use bp_runtime::{BasicOperatingMode, OperatingMode};
use frame_support::RuntimeDebug;
use sp_std::{collections::vec_deque::VecDeque, prelude::*};

// Weight is reexported to avoid additional frame-support dependencies in related crates.
use bp_runtime::messages::MessageDispatchResult;
// Weight is reexported to avoid additional frame-support dependencies in related crates.
pub use frame_support::weights::Weight;

/// Lane identifier.
Expand Down Expand Up @@ -209,21 +209,22 @@ pub struct UnrewardedRelayer<RelayerId> {

/// Received messages with their dispatch result.
#[derive(Clone, Default, Encode, Decode, RuntimeDebug, PartialEq, Eq, TypeInfo)]
pub struct ReceivedMessages<Result> {
pub struct ReceivedMessages<DispatchLevelResult> {
/// Id of the lane which is receiving messages.
pub lane: LaneId,
/// Result of messages which we tried to dispatch
pub receive_results: Vec<(MessageNonce, Result)>,
pub receive_results: Vec<(MessageNonce, ReceivalResult<DispatchLevelResult>)>,
/// Messages which were skipped and never dispatched
pub skipped_for_not_enough_weight: Vec<MessageNonce>,
}

impl<Result> ReceivedMessages<Result> {
pub fn new(lane: LaneId, receive_results: Vec<(MessageNonce, Result)>) -> Self {
ReceivedMessages { lane, receive_results, skipped_for_not_enough_weight: Vec::new() }
}
impl<DispatchLevelResult> ReceivedMessages<DispatchLevelResult> {
pub fn new(
lane: LaneId,
receive_results: Vec<(MessageNonce, ReceivalResult<DispatchLevelResult>)>,
) -> Self {

pub fn push(&mut self, message: MessageNonce, result: Result) {
pub fn push(&mut self, message: MessageNonce, result: ReceivalResult<DispatchLevelResult>) {
self.receive_results.push((message, result));
}

Expand All @@ -234,12 +235,12 @@ impl<Result> ReceivedMessages<Result> {

/// Result of single message receival.
#[derive(RuntimeDebug, Encode, Decode, PartialEq, Eq, Clone, TypeInfo)]
pub enum ReceivalResult {
pub enum ReceivalResult<DispatchLevelResult> {
/// Message has been received and dispatched. Note that we don't care whether dispatch has
/// been successful or not - in both case message falls into this category.
///
/// The message dispatch result is also returned.
Dispatched(MessageDispatchResult),
Dispatched(MessageDispatchResult<DispatchLevelResult>),
/// Message has invalid nonce and lane has rejected to accept this message.
InvalidNonce,
/// There are too many unrewarded relayer entries at the lane.
Expand Down
11 changes: 8 additions & 3 deletions primitives/messages/src/target_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ pub trait MessageDispatch<AccountId, Fee> {
/// (opaque `MessagePayload` used in delivery and this `DispatchPayload` used in dispatch).
type DispatchPayload: Decode;

/// Fine-grained result of single message dispatch (for better diagnostic purposes)
type DispatchLevelResult: Clone + Decode + sp_std::fmt::Debug + Eq;

/// Estimate dispatch weight.
///
/// This function must return correct upper bound of dispatch weight. The return value
Expand All @@ -94,7 +97,7 @@ pub trait MessageDispatch<AccountId, Fee> {
fn dispatch(
relayer_account: &AccountId,
message: DispatchMessage<Self::DispatchPayload, Fee>,
) -> MessageDispatchResult;
) -> MessageDispatchResult<Self::DispatchLevelResult>;
}

/// Proved messages from single lane of the source chain.
Expand Down Expand Up @@ -159,6 +162,7 @@ impl<Fee> SourceHeaderChain<Fee> for ForbidInboundMessages {
}
}
impl<AccountId, Fee> MessageDispatch<AccountId, Fee> for ForbidInboundMessages {
type DispatchLevelResult = ();
type DispatchPayload = ();

fn dispatch_weight(_message: &mut DispatchMessage<Self::DispatchPayload, Fee>) -> Weight {
Expand All @@ -174,12 +178,13 @@ impl<AccountId, Fee> MessageDispatch<AccountId, Fee> for ForbidInboundMessages {

fn dispatch(
_: &AccountId,
_: DispatchMessage<Self::DispatchPayload, Fee>,
) -> MessageDispatchResult {
_: DispatchMessage<Self::DispatchPayload>,
) -> MessageDispatchResult<Self::DispatchLevelResult> {
MessageDispatchResult {
dispatch_result: false,
unspent_weight: Weight::zero(),
dispatch_fee_paid_during_dispatch: false,
dispatch_level_result: (),
}
}
}
4 changes: 3 additions & 1 deletion primitives/runtime/src/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pub enum DispatchFeePayment {

/// Message dispatch result.
#[derive(Clone, PartialEq, Eq, Encode, Decode, RuntimeDebug, TypeInfo)]
pub struct MessageDispatchResult {
pub struct MessageDispatchResult<DispatchLevelResult> {
/// Dispatch result flag. This flag is relayed back to the source chain and, generally
/// speaking, may bring any (that fits in single bit) information from the dispatcher at
/// the target chain to the message submitter at the source chain. If you're using immediate
Expand All @@ -56,4 +56,6 @@ pub struct MessageDispatchResult {
/// configuration supports pay-dispatch-fee-at-target-chain option and message sender has
/// enabled this option.
pub dispatch_fee_paid_during_dispatch: bool,
/// Fine-grained result of single message dispatch (for better diagnostic purposes)
pub dispatch_level_result: DispatchLevelResult,
}

0 comments on commit b96b41c

Please sign in to comment.