Skip to content
This repository has been archived by the owner on Nov 17, 2023. It is now read-only.

Commit

Permalink
Callbacks return ModuleOutput<T>
Browse files Browse the repository at this point in the history
  • Loading branch information
hu55a1n1 committed Mar 17, 2022
1 parent 3b138bb commit 1d430c9
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 91 deletions.
73 changes: 38 additions & 35 deletions modules/src/core/ics04_channel/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::core::ics04_channel::{msgs::PacketMsg, packet::PacketResult};
use crate::core::ics05_port::capabilities::ChannelCapability;
use crate::core::ics24_host::identifier::{ChannelId, PortId};
use crate::core::ics26_routing::context::{Ics26Context, ModuleId, ModuleOutput, Router};
use crate::handler::HandlerOutput;
use crate::handler::{HandlerOutput, HandlerOutputBuilder};

pub mod acknowledgement;
pub mod chan_close_confirm;
Expand Down Expand Up @@ -108,8 +108,7 @@ pub fn channel_callback<Ctx>(
module_id: &ModuleId,
msg: &ChannelMsg,
result: &mut ChannelResult,
module_output: &mut ModuleOutput,
) -> Result<(), Error>
) -> Result<ModuleOutput<()>, Error>
where
Ctx: Ics26Context,
{
Expand All @@ -118,9 +117,8 @@ where
.get_route_mut(module_id)
.ok_or_else(Error::route_not_found)?;

match msg {
let output = match msg {
ChannelMsg::ChannelOpenInit(msg) => cb.on_chan_open_init(
module_output,
msg.channel.ordering,
&msg.channel.connection_hops,
&msg.port_id,
Expand All @@ -130,8 +128,7 @@ where
&msg.channel.version,
)?,
ChannelMsg::ChannelOpenTry(msg) => {
let version = cb.on_chan_open_try(
module_output,
let output = cb.on_chan_open_try(
msg.channel.ordering,
&msg.channel.connection_hops,
&msg.port_id,
Expand All @@ -140,25 +137,24 @@ where
msg.channel.counterparty(),
&msg.counterparty_version,
)?;
let (version, output) = destructure_output(output);
result.channel_end.version = version;
output
}
ChannelMsg::ChannelOpenAck(msg) => {
cb.on_chan_open_ack(&msg.port_id, &result.channel_id, &msg.counterparty_version)?
}
ChannelMsg::ChannelOpenAck(msg) => cb.on_chan_open_ack(
module_output,
&msg.port_id,
&result.channel_id,
&msg.counterparty_version,
)?,
ChannelMsg::ChannelOpenConfirm(msg) => {
cb.on_chan_open_confirm(module_output, &msg.port_id, &result.channel_id)?
cb.on_chan_open_confirm(&msg.port_id, &result.channel_id)?
}
ChannelMsg::ChannelCloseInit(msg) => {
cb.on_chan_close_init(module_output, &msg.port_id, &result.channel_id)?
cb.on_chan_close_init(&msg.port_id, &result.channel_id)?
}
ChannelMsg::ChannelCloseConfirm(msg) => {
cb.on_chan_close_confirm(module_output, &msg.port_id, &result.channel_id)?
cb.on_chan_close_confirm(&msg.port_id, &result.channel_id)?
}
}
Ok(())
};
Ok(output)
}

pub fn packet_validate<Ctx>(ctx: &Ctx, msg: &PacketMsg) -> Result<ModuleId, Error>
Expand Down Expand Up @@ -214,8 +210,7 @@ pub fn packet_callback<Ctx>(
ctx: &mut Ctx,
module_id: &ModuleId,
msg: &PacketMsg,
module_output: &mut ModuleOutput,
) -> Result<(), Error>
) -> Result<ModuleOutput<()>, Error>
where
Ctx: Ics26Context,
{
Expand All @@ -224,9 +219,10 @@ where
.get_route_mut(module_id)
.ok_or_else(Error::route_not_found)?;

match msg {
let output = match msg {
PacketMsg::RecvPacket(msg) => {
let (ack, write_fn) = cb.on_recv_packet(module_output, &msg.packet, &msg.signer);
let output = cb.on_recv_packet(&msg.packet, &msg.signer);
let ((ack, write_fn), output) = destructure_output(output);
match ack {
None => {}
Some(ack) => {
Expand All @@ -239,19 +235,26 @@ where
// TODO(hu55a1n1): write ack
}
}
output
}
PacketMsg::AckPacket(msg) => cb.on_acknowledgement_packet(
module_output,
&msg.packet,
&msg.acknowledgement,
&msg.signer,
)?,
PacketMsg::ToPacket(msg) => {
cb.on_timeout_packet(module_output, &msg.packet, &msg.signer)?
}
PacketMsg::ToClosePacket(msg) => {
cb.on_timeout_packet(module_output, &msg.packet, &msg.signer)?
PacketMsg::AckPacket(msg) => {
cb.on_acknowledgement_packet(&msg.packet, &msg.acknowledgement, &msg.signer)?
}
}
Ok(())
PacketMsg::ToPacket(msg) => cb.on_timeout_packet(&msg.packet, &msg.signer)?,
PacketMsg::ToClosePacket(msg) => cb.on_timeout_packet(&msg.packet, &msg.signer)?,
};
Ok(output)
}

fn destructure_output<T>(output: ModuleOutput<T>) -> (T, ModuleOutput<()>) {
let HandlerOutput {
result,
log,
events,
} = output;
let output = HandlerOutputBuilder::new()
.with_log(log)
.with_events(events)
.with_result(());
(result, output)
}
47 changes: 19 additions & 28 deletions modules/src/core/ics26_routing/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use crate::core::ics05_port::capabilities::ChannelCapability;
use crate::core::ics05_port::context::PortReader;
use crate::core::ics24_host::identifier::{ChannelId, ConnectionId, PortId};
use crate::events::IbcEvent;
use crate::handler::HandlerOutput;
use crate::handler::{HandlerOutput, HandlerOutputBuilder};
use crate::signer::Signer;

/// This trait captures all the functional dependencies (i.e., context) which the ICS26 module
Expand Down Expand Up @@ -88,100 +88,91 @@ pub type DeferredWriteResult<T> = (Option<Box<T>>, Option<Box<WriteFn>>);
// FIXME(hu55a1n1): Define concrete type that implements `Into<AbciEvent>`?
pub type ModuleEvent = IbcEvent;

pub type ModuleOutput = HandlerOutput<(), ModuleEvent>;
pub type ModuleOutput<T> = HandlerOutput<T, ModuleEvent>;

pub trait Module: Debug + Send + Sync + AsAnyMut + 'static {
#[allow(clippy::too_many_arguments)]
fn on_chan_open_init(
&mut self,
_output: &mut ModuleOutput,
_order: Order,
_connection_hops: &[ConnectionId],
_port_id: &PortId,
_channel_id: &ChannelId,
_channel_cap: &ChannelCapability,
_counterparty: &Counterparty,
_version: &Version,
) -> Result<(), Error> {
Ok(())
) -> Result<ModuleOutput<()>, Error> {
Ok(HandlerOutputBuilder::new().with_result(()))
}

#[allow(clippy::too_many_arguments)]
fn on_chan_open_try(
&mut self,
_output: &mut ModuleOutput,
_order: Order,
_connection_hops: &[ConnectionId],
_port_id: &PortId,
_channel_id: &ChannelId,
_channel_cap: &ChannelCapability,
_counterparty: &Counterparty,
_counterparty_version: &Version,
) -> Result<Version, Error>;
) -> Result<ModuleOutput<Version>, Error>;

fn on_chan_open_ack(
&mut self,
_output: &mut ModuleOutput,
_port_id: &PortId,
_channel_id: &ChannelId,
_counterparty_version: &Version,
) -> Result<(), Error> {
Ok(())
) -> Result<ModuleOutput<()>, Error> {
Ok(HandlerOutputBuilder::new().with_result(()))
}

fn on_chan_open_confirm(
&mut self,
_output: &mut ModuleOutput,
_port_id: &PortId,
_channel_id: &ChannelId,
) -> Result<(), Error> {
Ok(())
) -> Result<ModuleOutput<()>, Error> {
Ok(HandlerOutputBuilder::new().with_result(()))
}

fn on_chan_close_init(
&mut self,
_output: &mut ModuleOutput,
_port_id: &PortId,
_channel_id: &ChannelId,
) -> Result<(), Error> {
Ok(())
) -> Result<ModuleOutput<()>, Error> {
Ok(HandlerOutputBuilder::new().with_result(()))
}

fn on_chan_close_confirm(
&mut self,
_output: &mut ModuleOutput,
_port_id: &PortId,
_channel_id: &ChannelId,
) -> Result<(), Error> {
Ok(())
) -> Result<ModuleOutput<()>, Error> {
Ok(HandlerOutputBuilder::new().with_result(()))
}

fn on_recv_packet(
&self,
_output: &mut ModuleOutput,
_packet: &Packet,
_relayer: &Signer,
) -> DeferredWriteResult<dyn Acknowledgement> {
(None, None)
) -> ModuleOutput<DeferredWriteResult<dyn Acknowledgement>> {
HandlerOutputBuilder::new().with_result((None, None))
}

fn on_acknowledgement_packet(
&mut self,
_output: &mut ModuleOutput,
_packet: &Packet,
_acknowledgement: &GenericAcknowledgement,
_relayer: &Signer,
) -> Result<(), Error> {
Ok(())
) -> Result<ModuleOutput<()>, Error> {
Ok(HandlerOutputBuilder::new().with_result(()))
}

fn on_timeout_packet(
&mut self,
_output: &mut ModuleOutput,
_packet: &Packet,
_relayer: &Signer,
) -> Result<(), Error> {
Ok(())
) -> Result<ModuleOutput<()>, Error> {
Ok(HandlerOutputBuilder::new().with_result(()))
}
}

Expand Down
20 changes: 6 additions & 14 deletions modules/src/core/ics26_routing/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,16 +83,9 @@ where
let mut handler_output =
ics4_msg_dispatcher(ctx, &msg).map_err(Error::ics04_channel)?;

let mut module_output = HandlerOutput::builder().with_result(());
let cb_result = ics4_callback(
ctx,
&module_id,
&msg,
&mut handler_output.result,
&mut module_output,
);
handler_output.merge(module_output);
cb_result.map_err(Error::ics04_channel)?;
let cb_result = ics4_callback(ctx, &module_id, &msg, &mut handler_output.result)
.map_err(Error::ics04_channel)?;
handler_output.merge(cb_result);

// Apply any results to the host chain store.
ctx.store_channel_result(handler_output.result)
Expand Down Expand Up @@ -133,10 +126,9 @@ where
.with_result(()));
}

let mut module_output = HandlerOutput::builder().with_result(());
let cb_result = ics4_packet_callback(ctx, &module_id, &msg, &mut module_output);
handler_output.merge(module_output);
cb_result.map_err(Error::ics04_channel)?;
let cb_result =
ics4_packet_callback(ctx, &module_id, &msg).map_err(Error::ics04_channel)?;
handler_output.merge(cb_result);

// Apply any results to the host chain store.
ctx.store_packet_result(handler_output.result)
Expand Down
23 changes: 9 additions & 14 deletions modules/src/mock/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1250,6 +1250,7 @@ mod tests {
use crate::core::ics26_routing::context::{
Acknowledgement, DeferredWriteResult, Module, ModuleId, ModuleOutput, Router, RouterBuilder,
};
use crate::handler::HandlerOutputBuilder;
use crate::mock::context::MockContext;
use crate::mock::context::MockRouterBuilder;
use crate::mock::host::HostType;
Expand Down Expand Up @@ -1422,31 +1423,30 @@ mod tests {
impl Module for FooModule {
fn on_chan_open_try(
&mut self,
_output: &mut ModuleOutput,
_order: Order,
_connection_hops: &[ConnectionId],
_port_id: &PortId,
_channel_id: &ChannelId,
_channel_cap: &ChannelCapability,
_counterparty: &Counterparty,
counterparty_version: &Version,
) -> Result<Version, Error> {
Ok(counterparty_version.clone())
) -> Result<ModuleOutput<Version>, Error> {
Ok(HandlerOutputBuilder::new().with_result(counterparty_version.clone()))
}

fn on_recv_packet(
&self,
_output: &mut ModuleOutput,
_packet: &Packet,
_relayer: &Signer,
) -> DeferredWriteResult<dyn Acknowledgement> {
(
) -> ModuleOutput<DeferredWriteResult<dyn Acknowledgement>> {
let result = (
Some(Box::new(MockAck::default())),
Some(Box::new(|module| {
let module = module.downcast_mut::<FooModule>().unwrap();
module.counter += 1;
})),
)
);
HandlerOutputBuilder::new().with_result(result)
}
}

Expand All @@ -1456,7 +1456,6 @@ mod tests {
impl Module for BarModule {
fn on_chan_open_try(
&mut self,
_output: &mut ModuleOutput,
_order: Order,
_connection_hops: &[ConnectionId],
_port_id: &PortId,
Expand All @@ -1465,7 +1464,7 @@ mod tests {
_counterparty: &Counterparty,
counterparty_version: &Version,
) -> Result<Version, Error> {
Ok(counterparty_version.clone())
Ok(HandlerOutputBuilder::new().with_result(counterparty_version.clone()))
}
}

Expand All @@ -1487,11 +1486,7 @@ mod tests {
let mut on_recv_packet_result = |module_id: &'static str| {
let module_id = ModuleId::from_str(module_id).unwrap();
let m = ctx.router.get_route_mut(&module_id).unwrap();
let result = m.on_recv_packet(
&mut ModuleOutput::builder().with_result(()),
&Packet::default(),
&Signer::new(""),
);
let result = m.on_recv_packet(&Packet::default(), &Signer::new(""));
(module_id, result)
};

Expand Down

0 comments on commit 1d430c9

Please sign in to comment.