Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added generic DispatchLevelResult to the MessageDispatchResult #1670

Merged
merged 2 commits into from
Nov 30, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions bin/millau/runtime/src/xcm_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,7 @@ mod tests {
MessageDispatchResult {
unspent_weight: frame_support::weights::Weight::from_ref_time(0),
dispatch_fee_paid_during_dispatch: false,
dispatch_level_result: (),
}
);
})
Expand Down
1 change: 1 addition & 0 deletions bin/rialto-parachain/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -897,6 +897,7 @@ mod tests {
MessageDispatchResult {
unspent_weight: frame_support::weights::Weight::from_ref_time(0),
dispatch_fee_paid_during_dispatch: false,
dispatch_level_result: (),
}
);
})
Expand Down
1 change: 1 addition & 0 deletions bin/rialto/runtime/src/xcm_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ mod tests {
MessageDispatchResult {
unspent_weight: frame_support::weights::Weight::from_ref_time(0),
dispatch_fee_paid_during_dispatch: false,
dispatch_level_result: (),
}
);
})
Expand Down
4 changes: 3 additions & 1 deletion bin/runtime-common/src/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,7 @@ pub mod target {
WeightCredit: Get<Weight>,
{
type DispatchPayload = FromBridgedChainMessagePayload<CallOf<ThisChain<B>>>;
type DispatchLevelResult = ();
bkontur marked this conversation as resolved.
Show resolved Hide resolved

fn dispatch_weight(
message: &mut DispatchMessage<Self::DispatchPayload>,
Expand Down Expand Up @@ -482,7 +483,7 @@ pub mod target {
fn dispatch(
_relayer_account: &AccountIdOf<ThisChain<B>>,
message: DispatchMessage<Self::DispatchPayload>,
) -> MessageDispatchResult {
) -> MessageDispatchResult<Self::DispatchLevelResult> {
let message_id = (message.key.lane_id, message.key.nonce);
let do_dispatch = move || -> sp_std::result::Result<Outcome, codec::Error> {
let FromBridgedChainMessagePayload { xcm: (location, xcm), weight: weight_limit } =
Expand Down Expand Up @@ -544,6 +545,7 @@ pub mod target {
MessageDispatchResult {
unspent_weight: Weight::zero(),
dispatch_fee_paid_during_dispatch: false,
dispatch_level_result: (),
}
}
}
Expand Down
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>,
) -> 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
10 changes: 9 additions & 1 deletion modules/messages/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,15 @@ 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<
ReceivalResult<
<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 @@ -60,12 +60,13 @@ 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>,
}
pub type TestMessageFee = u64;
pub type TestRelayer = u64;
pub type TestDispatchLevelResult = ();

type Block = frame_system::mocking::MockBlock<TestRuntime>;
type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic<TestRuntime>;
Expand Down Expand Up @@ -345,6 +346,7 @@ pub struct TestMessageDispatch;

impl MessageDispatch<AccountId> for TestMessageDispatch {
type DispatchPayload = TestPayload;
type DispatchLevelResult = TestDispatchLevelResult;

fn dispatch_weight(message: &mut DispatchMessage<TestPayload>) -> Weight {
match message.data.payload.as_ref() {
Expand All @@ -356,7 +358,7 @@ impl MessageDispatch<AccountId> for TestMessageDispatch {
fn dispatch(
_relayer_account: &AccountId,
message: DispatchMessage<TestPayload>,
) -> MessageDispatchResult {
) -> MessageDispatchResult<TestDispatchLevelResult> {
match message.data.payload.as_ref() {
Ok(payload) => payload.dispatch_result.clone(),
Err(_) => dispatch_result(0),
Expand Down Expand Up @@ -391,10 +393,13 @@ pub const fn message_payload(id: u64, declared_weight: u64) -> TestPayload {
}

/// 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 {
unspent_weight: Weight::from_ref_time(unspent_weight),
dispatch_fee_paid_during_dispatch: true,
dispatch_level_result: (),
}
}

Expand Down
6 changes: 3 additions & 3 deletions primitives/messages/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ pub mod source_chain;
pub mod storage_keys;
pub mod target_chain;

// 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;

/// Messages pallet operating mode.
Expand Down Expand Up @@ -237,12 +237,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: Clone + Decode + sp_std::fmt::Debug + Eq> {
bkontur marked this conversation as resolved.
Show resolved Hide resolved
/// 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
12 changes: 10 additions & 2 deletions primitives/messages/src/target_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ pub trait MessageDispatch<AccountId> {
/// (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 @@ -106,7 +109,7 @@ pub trait MessageDispatch<AccountId> {
fn dispatch(
relayer_account: &AccountId,
message: DispatchMessage<Self::DispatchPayload>,
) -> MessageDispatchResult;
) -> MessageDispatchResult<Self::DispatchLevelResult>;
}

impl<Message> Default for ProvedLaneMessages<Message> {
Expand Down Expand Up @@ -149,15 +152,20 @@ impl SourceHeaderChain for ForbidInboundMessages {

impl<AccountId> MessageDispatch<AccountId> for ForbidInboundMessages {
type DispatchPayload = ();
type DispatchLevelResult = ();

fn dispatch_weight(_message: &mut DispatchMessage<Self::DispatchPayload>) -> Weight {
Weight::MAX
}

fn dispatch(_: &AccountId, _: DispatchMessage<Self::DispatchPayload>) -> MessageDispatchResult {
fn dispatch(
_: &AccountId,
_: DispatchMessage<Self::DispatchPayload>,
) -> MessageDispatchResult<Self::DispatchLevelResult> {
MessageDispatchResult {
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 @@ -22,7 +22,7 @@ use scale_info::TypeInfo;

/// Message dispatch result.
#[derive(Encode, Decode, RuntimeDebug, Clone, PartialEq, Eq, TypeInfo)]
pub struct MessageDispatchResult {
pub struct MessageDispatchResult<DispatchLevelResult: Clone + Decode + sp_std::fmt::Debug + Eq> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also try NoBound versions here? :) I've found them recently && iiuc it should help with removing these constraints (maybe I'm wrong though)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when I tried to change RuntimeDebug to RuntimeDebugNoBound I get this error:
image

/// Unspent dispatch weight. This weight that will be deducted from total delivery transaction
/// weight, thus reducing the transaction cost. This shall not be zero in (at least) two cases:
///
Expand All @@ -34,4 +34,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,
}