Skip to content

Commit

Permalink
refactor(channel): clean up some into implementations
Browse files Browse the repository at this point in the history
More relevant when we have the queue, no real need to have all of these functions
  • Loading branch information
AwesomeIbex committed Feb 27, 2023
1 parent 1de00b9 commit d585990
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 38 deletions.
14 changes: 14 additions & 0 deletions crates/channel-primitives/src/traits/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,20 @@ impl<Weight> From<(Vec<u8>, Weight)> for HandlerInfo<Weight> {
}
}

// Justification: no need for from here
#[allow(clippy::from_over_into)]
#[cfg(feature = "frame")]
impl Into<frame_support::weights::PostDispatchInfo>
for HandlerInfo<frame_support::weights::Weight>
{
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.
Expand Down
30 changes: 15 additions & 15 deletions crates/channel/src/receiver/frame/mod.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -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<E: ChannelProgressionEmitter>(
xbi_id: &Vec<u8>,
instruction_handle: &Result<
HandlerInfo<frame_support::weights::Weight>,
sp_runtime::DispatchErrorWithPostInfo<frame_support::weights::PostDispatchInfo>,
>,
msg: &mut XbiFormat,
) -> XbiResult {
match instruction_handle {
Ok(info) => handler_to_xbi_result::<E>(&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<Emitter: ChannelProgressionEmitter>(
xbi_id: &Vec<u8>,
Expand Down Expand Up @@ -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<HandlerInfo<frame_support::weights::Weight>, 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;
Expand Down
18 changes: 8 additions & 10 deletions crates/channel/src/receiver/frame/queue_backed.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
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};
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<T, Emitter, Queue, InstructionHandler> {
Expand Down Expand Up @@ -47,10 +47,8 @@ where

let instruction_result = InstructionHandler::handle(origin, msg);

let xbi_result = match &instruction_result {
Ok(info) => handler_to_xbi_result::<Emitter>(&xbi_id.encode(), info, msg),
Err(e) => instruction_error_to_xbi_result(&xbi_id.encode(), e),
};
let xbi_result =
handle_instruction_result::<Emitter>(&xbi_id.encode(), &instruction_result, msg);

// progress to executed
msg.metadata.timesheet.progress(current_block);
Expand All @@ -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
Expand Down
20 changes: 7 additions & 13 deletions crates/channel/src/receiver/frame/sync.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
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};
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.
Expand Down Expand Up @@ -52,10 +48,8 @@ where

let instruction_handle = InstructionHandler::handle(origin, msg);

let xbi_result = match &instruction_handle {
Ok(info) => handler_to_xbi_result::<Emitter>(&xbi_id.encode(), info, msg),
Err(e) => instruction_error_to_xbi_result(&xbi_id.encode(), e),
};
let xbi_result =
handle_instruction_result::<Emitter>(&xbi_id.encode(), &instruction_handle, msg);

msg.metadata.timesheet.progress(current_block);

Expand All @@ -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.
Expand Down

0 comments on commit d585990

Please sign in to comment.