ibc_core_router/
module.rs

1/// The trait that defines an IBC application
2use core::fmt::Debug;
3
4use ibc_core_channel_types::acknowledgement::Acknowledgement;
5use ibc_core_channel_types::channel::{Counterparty, Order};
6use ibc_core_channel_types::error::ChannelError;
7use ibc_core_channel_types::packet::Packet;
8use ibc_core_channel_types::Version;
9use ibc_core_host_types::identifiers::{ChannelId, ConnectionId, PortId};
10use ibc_core_router_types::module::ModuleExtras;
11use ibc_primitives::prelude::*;
12use ibc_primitives::Signer;
13
14pub trait Module: Debug {
15    fn on_chan_open_init_validate(
16        &self,
17        order: Order,
18        connection_hops: &[ConnectionId],
19        port_id: &PortId,
20        channel_id: &ChannelId,
21        counterparty: &Counterparty,
22        version: &Version,
23    ) -> Result<Version, ChannelError>;
24
25    fn on_chan_open_init_execute(
26        &mut self,
27        order: Order,
28        connection_hops: &[ConnectionId],
29        port_id: &PortId,
30        channel_id: &ChannelId,
31        counterparty: &Counterparty,
32        version: &Version,
33    ) -> Result<(ModuleExtras, Version), ChannelError>;
34
35    fn on_chan_open_try_validate(
36        &self,
37        order: Order,
38        connection_hops: &[ConnectionId],
39        port_id: &PortId,
40        channel_id: &ChannelId,
41        counterparty: &Counterparty,
42        counterparty_version: &Version,
43    ) -> Result<Version, ChannelError>;
44
45    fn on_chan_open_try_execute(
46        &mut self,
47        order: Order,
48        connection_hops: &[ConnectionId],
49        port_id: &PortId,
50        channel_id: &ChannelId,
51        counterparty: &Counterparty,
52        counterparty_version: &Version,
53    ) -> Result<(ModuleExtras, Version), ChannelError>;
54
55    fn on_chan_open_ack_validate(
56        &self,
57        _port_id: &PortId,
58        _channel_id: &ChannelId,
59        _counterparty_version: &Version,
60    ) -> Result<(), ChannelError> {
61        Ok(())
62    }
63
64    fn on_chan_open_ack_execute(
65        &mut self,
66        _port_id: &PortId,
67        _channel_id: &ChannelId,
68        _counterparty_version: &Version,
69    ) -> Result<ModuleExtras, ChannelError> {
70        Ok(ModuleExtras::empty())
71    }
72
73    fn on_chan_open_confirm_validate(
74        &self,
75        _port_id: &PortId,
76        _channel_id: &ChannelId,
77    ) -> Result<(), ChannelError> {
78        Ok(())
79    }
80
81    fn on_chan_open_confirm_execute(
82        &mut self,
83        _port_id: &PortId,
84        _channel_id: &ChannelId,
85    ) -> Result<ModuleExtras, ChannelError> {
86        Ok(ModuleExtras::empty())
87    }
88
89    fn on_chan_close_init_validate(
90        &self,
91        _port_id: &PortId,
92        _channel_id: &ChannelId,
93    ) -> Result<(), ChannelError> {
94        Ok(())
95    }
96
97    fn on_chan_close_init_execute(
98        &mut self,
99        _port_id: &PortId,
100        _channel_id: &ChannelId,
101    ) -> Result<ModuleExtras, ChannelError> {
102        Ok(ModuleExtras::empty())
103    }
104
105    fn on_chan_close_confirm_validate(
106        &self,
107        _port_id: &PortId,
108        _channel_id: &ChannelId,
109    ) -> Result<(), ChannelError> {
110        Ok(())
111    }
112
113    fn on_chan_close_confirm_execute(
114        &mut self,
115        _port_id: &PortId,
116        _channel_id: &ChannelId,
117    ) -> Result<ModuleExtras, ChannelError> {
118        Ok(ModuleExtras::empty())
119    }
120
121    // Note: no `on_recv_packet_validate()`
122    // the `onRecvPacket` callback always succeeds
123    // if any error occurs, than an "error acknowledgement"
124    // must be returned
125
126    /// ICS-26 `onRecvPacket` callback implementation.
127    ///
128    /// # Note on optional acknowledgements
129    ///
130    /// Acknowledgements can be committed asynchronously, hence
131    /// the `Option` type. In general, acknowledgements should
132    /// be committed to storage, accompanied by an ack event,
133    /// as soon as a packet is received. This will be done
134    /// automatically as long as `Some(ack)` is returned from
135    /// this callback. However, in some cases, such as when
136    /// implementing a multiple hop packet delivery protocol,
137    /// a packet can only be acknowledged after it has reached
138    /// the last hop.
139    ///
140    /// ## Committing a packet asynchronously
141    ///
142    /// Event emission and state updates for packet acknowledgements
143    /// can be performed asynchronously using [`emit_packet_acknowledgement_event`]
144    /// and [`commit_packet_acknowledgment`], respectively.
145    ///
146    /// [`commit_packet_acknowledgment`]: ../../channel/handler/fn.commit_packet_acknowledgment.html
147    /// [`emit_packet_acknowledgement_event`]: ../../channel/handler/fn.emit_packet_acknowledgement_event.html
148    fn on_recv_packet_execute(
149        &mut self,
150        packet: &Packet,
151        relayer: &Signer,
152    ) -> (ModuleExtras, Option<Acknowledgement>);
153
154    fn on_acknowledgement_packet_validate(
155        &self,
156        _packet: &Packet,
157        _acknowledgement: &Acknowledgement,
158        _relayer: &Signer,
159    ) -> Result<(), ChannelError>;
160
161    fn on_acknowledgement_packet_execute(
162        &mut self,
163        _packet: &Packet,
164        _acknowledgement: &Acknowledgement,
165        _relayer: &Signer,
166    ) -> (ModuleExtras, Result<(), ChannelError>);
167
168    /// Note: `MsgTimeout` and `MsgTimeoutOnClose` use the same callback
169    fn on_timeout_packet_validate(
170        &self,
171        packet: &Packet,
172        relayer: &Signer,
173    ) -> Result<(), ChannelError>;
174
175    /// Note: `MsgTimeout` and `MsgTimeoutOnClose` use the same callback
176    fn on_timeout_packet_execute(
177        &mut self,
178        packet: &Packet,
179        relayer: &Signer,
180    ) -> (ModuleExtras, Result<(), ChannelError>);
181}