From d58599023cf4c1684fedf9f1ee0d1370d937bf3e Mon Sep 17 00:00:00 2001 From: AwesomeIbex Date: Mon, 27 Feb 2023 17:48:35 +0000 Subject: [PATCH] refactor(channel): clean up some into implementations More relevant when we have the queue, no real need to have all of these functions --- crates/channel-primitives/src/traits/mod.rs | 14 +++++++++ crates/channel/src/receiver/frame/mod.rs | 30 +++++++++---------- .../src/receiver/frame/queue_backed.rs | 18 +++++------ crates/channel/src/receiver/frame/sync.rs | 20 +++++-------- 4 files changed, 44 insertions(+), 38 deletions(-) diff --git a/crates/channel-primitives/src/traits/mod.rs b/crates/channel-primitives/src/traits/mod.rs index e8b8a08..daf3701 100644 --- a/crates/channel-primitives/src/traits/mod.rs +++ b/crates/channel-primitives/src/traits/mod.rs @@ -33,6 +33,20 @@ impl From<(Vec, Weight)> for HandlerInfo { } } +// Justification: no need for from here +#[allow(clippy::from_over_into)] +#[cfg(feature = "frame")] +impl Into + for HandlerInfo +{ + fn into(self) -> frame_support::weights::PostDispatchInfo { + frame_support::weights::PostDispatchInfo { + actual_weight: Some(self.weight), + pays_fee: frame_support::weights::Pays::Yes, + } + } +} + /// A simple trait that allows a parachain to specify how they would handle an xbi instruction. /// /// This is also utilised as a simple gateway for routing messages within a parachain, and could be used for different pallets to contact each other. diff --git a/crates/channel/src/receiver/frame/mod.rs b/crates/channel/src/receiver/frame/mod.rs index 5da3ab7..2e1efcb 100644 --- a/crates/channel/src/receiver/frame/mod.rs +++ b/crates/channel/src/receiver/frame/mod.rs @@ -1,8 +1,5 @@ use codec::Encode; -use frame_support::{ - dispatch::DispatchErrorWithPostInfo, pallet_prelude::DispatchResultWithPostInfo, - weights::PostDispatchInfo, -}; + use sp_runtime::sp_std::prelude::*; use xp_channel::{traits::HandlerInfo, ChannelProgressionEmitter}; use xp_format::{Status, XbiFormat, XbiMetadata, XbiResult}; @@ -20,7 +17,20 @@ pub(crate) fn invert_destination_from_message(metadata: &mut XbiMetadata) { metadata.src_para_id = my_para; } -// TODO[style]: migrate to `From` for XbiResult +pub(crate) fn handle_instruction_result( + xbi_id: &Vec, + instruction_handle: &Result< + HandlerInfo, + sp_runtime::DispatchErrorWithPostInfo, + >, + msg: &mut XbiFormat, +) -> XbiResult { + match instruction_handle { + Ok(info) => handler_to_xbi_result::(&xbi_id.encode(), info, msg), + Err(e) => instruction_error_to_xbi_result(&xbi_id.encode(), e), + } +} + /// Map a result from an xbi handler to a result pub(crate) fn handler_to_xbi_result( xbi_id: &Vec, @@ -57,16 +67,6 @@ pub(crate) fn instruction_error_to_xbi_result( } } -// TODO[style]: migrate to `From` for XbiResult -pub(crate) fn handler_to_dispatch_info( - r: Result, DispatchErrorWithPostInfo>, -) -> DispatchResultWithPostInfo { - r.map(|info| PostDispatchInfo { - actual_weight: Some(info.weight), - ..Default::default() - }) -} - #[cfg(test)] mod tests { use crate::receiver::frame::instruction_error_to_xbi_result; diff --git a/crates/channel/src/receiver/frame/queue_backed.rs b/crates/channel/src/receiver/frame/queue_backed.rs index 9e8d2cc..9b9ce33 100644 --- a/crates/channel/src/receiver/frame/queue_backed.rs +++ b/crates/channel/src/receiver/frame/queue_backed.rs @@ -1,7 +1,5 @@ -use crate::receiver::frame::{ - handler_to_xbi_result, instruction_error_to_xbi_result, invert_destination_from_message, -}; -use crate::receiver::{frame::handler_to_dispatch_info, Receiver as ReceiverExt}; +use crate::receiver::frame::invert_destination_from_message; +use crate::receiver::Receiver as ReceiverExt; use codec::Encode; use frame_support::pallet_prelude::DispatchResultWithPostInfo; use frame_system::{ensure_signed, Config}; @@ -9,11 +7,13 @@ use sp_runtime::{traits::UniqueSaturatedInto, Either}; use sp_std::marker::PhantomData; use xp_channel::{ queue::{QueueSignal, Queueable}, - traits::XbiInstructionHandler, + traits::{HandlerInfo, XbiInstructionHandler}, ChannelProgressionEmitter, Message, }; use xp_format::{XbiFormat, XbiMetadata, XbiResult}; +use super::handle_instruction_result; + /// This is an asynchronous queue backed frame receiver, which expects some queue handler to transport the messages back via the transport layer, /// detaching the message handling part with the transport of the message. pub struct Receiver { @@ -47,10 +47,8 @@ where let instruction_result = InstructionHandler::handle(origin, msg); - let xbi_result = match &instruction_result { - Ok(info) => handler_to_xbi_result::(&xbi_id.encode(), info, msg), - Err(e) => instruction_error_to_xbi_result(&xbi_id.encode(), e), - }; + let xbi_result = + handle_instruction_result::(&xbi_id.encode(), &instruction_result, msg); // progress to executed msg.metadata.timesheet.progress(current_block); @@ -70,7 +68,7 @@ where QueueSignal::ResponseReceived, )); - handler_to_dispatch_info(instruction_result) + instruction_result.map(HandlerInfo::into) } /// Response should delegate to the queue handler who would know about how to handle the message diff --git a/crates/channel/src/receiver/frame/sync.rs b/crates/channel/src/receiver/frame/sync.rs index 4406150..f36a336 100644 --- a/crates/channel/src/receiver/frame/sync.rs +++ b/crates/channel/src/receiver/frame/sync.rs @@ -1,10 +1,4 @@ -use crate::{ - receiver::frame::{ - handler_to_dispatch_info, handler_to_xbi_result, instruction_error_to_xbi_result, - invert_destination_from_message, - }, - receiver::Receiver as ReceiverExt, -}; +use crate::{receiver::frame::invert_destination_from_message, receiver::Receiver as ReceiverExt}; use codec::Encode; use frame_support::pallet_prelude::DispatchResultWithPostInfo; use frame_system::{ensure_signed, Config}; @@ -12,11 +6,13 @@ use sp_runtime::{traits::UniqueSaturatedInto, Either}; use sp_std::marker::PhantomData; use xp_channel::{ queue::{QueueSignal, Queueable}, - traits::XbiInstructionHandler, + traits::{HandlerInfo, XbiInstructionHandler}, ChannelProgressionEmitter, Message, }; use xp_format::{XbiFormat, XbiMetadata, XbiResult}; +use super::handle_instruction_result; + /// This is a synchronous backed Frame receiver /// It services the `REQ-REP` side of an async channel, that is to say, it receives a message, handles it, then responds with the result /// all in one step. @@ -52,10 +48,8 @@ where let instruction_handle = InstructionHandler::handle(origin, msg); - let xbi_result = match &instruction_handle { - Ok(info) => handler_to_xbi_result::(&xbi_id.encode(), info, msg), - Err(e) => instruction_error_to_xbi_result(&xbi_id.encode(), e), - }; + let xbi_result = + handle_instruction_result::(&xbi_id.encode(), &instruction_handle, msg); msg.metadata.timesheet.progress(current_block); @@ -71,7 +65,7 @@ where Sender::send(Message::Response(xbi_result, msg.metadata.clone())); - handler_to_dispatch_info(instruction_handle) + instruction_handle.map(HandlerInfo::into) } // TODO: this should not have a queue anymore, we should provide some storage interface to write the result and add the cost.