penumbra_sdk_ibc/component/
app_handler.rs

1/// IBC "app handlers" for the IBC component.
2///
3/// An app handler defines an interface for any IBC application to consume verified IBC events from
4/// the core IBC component. IBC applications listen to channel events that occur on the Port ID
5/// that they have subscribed to, and apply application-specific state transition logic.
6///
7/// The primary IBC application is the Ics20 transfer application, which allows for interchain
8/// token transfers.
9use anyhow::Result;
10use async_trait::async_trait;
11use cnidarium::{StateRead, StateWrite};
12use ibc_types::core::channel::msgs::{
13    MsgAcknowledgement, MsgChannelCloseConfirm, MsgChannelCloseInit, MsgChannelOpenAck,
14    MsgChannelOpenConfirm, MsgChannelOpenInit, MsgChannelOpenTry, MsgRecvPacket, MsgTimeout,
15};
16
17/// AppHandlerCheck defines the interface for an IBC application to consume IBC channel and packet
18/// events, and apply their validation logic. This validation logic is used for stateful validation
19/// only.
20#[async_trait]
21pub trait AppHandlerCheck: Send + Sync {
22    async fn chan_open_init_check<S: StateRead>(state: S, msg: &MsgChannelOpenInit) -> Result<()>;
23    async fn chan_open_try_check<S: StateRead>(state: S, msg: &MsgChannelOpenTry) -> Result<()>;
24    async fn chan_open_ack_check<S: StateRead>(state: S, msg: &MsgChannelOpenAck) -> Result<()>;
25    async fn chan_open_confirm_check<S: StateRead>(
26        state: S,
27        msg: &MsgChannelOpenConfirm,
28    ) -> Result<()>;
29    async fn chan_close_confirm_check<S: StateRead>(
30        state: S,
31        msg: &MsgChannelCloseConfirm,
32    ) -> Result<()>;
33    async fn chan_close_init_check<S: StateRead>(state: S, msg: &MsgChannelCloseInit)
34        -> Result<()>;
35
36    async fn recv_packet_check<S: StateRead>(state: S, msg: &MsgRecvPacket) -> Result<()>;
37    async fn timeout_packet_check<S: StateRead>(state: S, msg: &MsgTimeout) -> Result<()>;
38    async fn acknowledge_packet_check<S: StateRead>(
39        state: S,
40        msg: &MsgAcknowledgement,
41    ) -> Result<()>;
42}
43
44// AppHandlerExecute defines the interface for an IBC application to consume IBC channel and packet
45// events and apply their state transition logic. The IBC component will only call these methods
46// once the transaction has been validated using the AppHandlerCheck interface.
47#[async_trait]
48pub trait AppHandlerExecute: Send + Sync {
49    async fn chan_open_init_execute<S: StateWrite>(state: S, msg: &MsgChannelOpenInit);
50    async fn chan_open_try_execute<S: StateWrite>(state: S, msg: &MsgChannelOpenTry);
51    async fn chan_open_ack_execute<S: StateWrite>(state: S, msg: &MsgChannelOpenAck);
52    async fn chan_open_confirm_execute<S: StateWrite>(state: S, msg: &MsgChannelOpenConfirm);
53    async fn chan_close_confirm_execute<S: StateWrite>(state: S, msg: &MsgChannelCloseConfirm);
54    async fn chan_close_init_execute<S: StateWrite>(state: S, msg: &MsgChannelCloseInit);
55
56    async fn recv_packet_execute<S: StateWrite>(state: S, msg: &MsgRecvPacket) -> Result<()>;
57    async fn timeout_packet_execute<S: StateWrite>(state: S, msg: &MsgTimeout) -> Result<()>;
58    async fn acknowledge_packet_execute<S: StateWrite>(
59        state: S,
60        msg: &MsgAcknowledgement,
61    ) -> Result<()>;
62}
63
64pub trait AppHandler: AppHandlerCheck + AppHandlerExecute {}