1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
use crate::prelude::*;

use alloc::borrow::{Borrow, Cow};
use core::any::Any;
use core::fmt::Debug;
use core::{fmt, str::FromStr};

use serde::{Deserialize, Serialize};

use crate::core::ics02_client::context::{ClientKeeper, ClientReader};
use crate::core::ics03_connection::context::{ConnectionKeeper, ConnectionReader};
use crate::core::ics04_channel::channel::{Counterparty, Order};
use crate::core::ics04_channel::context::{ChannelKeeper, ChannelReader};
use crate::core::ics04_channel::error::Error;
use crate::core::ics04_channel::msgs::acknowledgement::Acknowledgement as GenericAcknowledgement;
use crate::core::ics04_channel::packet::Packet;
use crate::core::ics04_channel::Version;
use crate::core::ics05_port::context::PortReader;
use crate::core::ics24_host::identifier::{ChannelId, ConnectionId, PortId};
use crate::events::ModuleEvent;
use crate::handler::HandlerOutputBuilder;
use crate::signer::Signer;

/// This trait captures all the functional dependencies (i.e., context) which the ICS26 module
/// requires to be able to dispatch and process IBC messages. In other words, this is the
/// representation of a chain from the perspective of the IBC module of that chain.
pub trait Ics26Context:
    ClientReader
    + ClientKeeper
    + ConnectionReader
    + ConnectionKeeper
    + ChannelKeeper
    + ChannelReader
    + PortReader
{
    type Router: Router;

    fn router(&self) -> &Self::Router;

    fn router_mut(&mut self) -> &mut Self::Router;
}

#[derive(Debug, PartialEq, Eq)]
pub struct InvalidModuleId;

#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Deserialize, Serialize)]
pub struct ModuleId(String);

impl ModuleId {
    pub fn new(s: Cow<'_, str>) -> Result<Self, InvalidModuleId> {
        if !s.trim().is_empty() && s.chars().all(char::is_alphanumeric) {
            Ok(Self(s.into_owned()))
        } else {
            Err(InvalidModuleId)
        }
    }
}

impl fmt::Display for ModuleId {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.0)
    }
}

impl FromStr for ModuleId {
    type Err = InvalidModuleId;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Self::new(Cow::Borrowed(s))
    }
}

impl Borrow<str> for ModuleId {
    fn borrow(&self) -> &str {
        self.0.as_str()
    }
}

/// Types implementing this trait are expected to implement `From<GenericAcknowledgement>`
pub trait Acknowledgement: AsRef<[u8]> {}

pub type WriteFn = dyn FnOnce(&mut dyn Any) -> Result<(), String>;

pub enum OnRecvPacketAck {
    Nil(Box<WriteFn>),
    Successful(Box<dyn Acknowledgement>, Box<WriteFn>),
    Failed(Box<dyn Acknowledgement>),
}

impl OnRecvPacketAck {
    pub fn is_successful(&self) -> bool {
        matches!(self, OnRecvPacketAck::Successful(_, _))
    }
}

pub type ModuleOutputBuilder = HandlerOutputBuilder<(), ModuleEvent>;

pub trait Module: Send + Sync + AsAnyMut {
    #[allow(clippy::too_many_arguments)]
    fn on_chan_open_init(
        &mut self,
        _output: &mut ModuleOutputBuilder,
        _order: Order,
        _connection_hops: &[ConnectionId],
        _port_id: &PortId,
        _channel_id: &ChannelId,
        _counterparty: &Counterparty,
        _version: &Version,
    ) -> Result<(), Error> {
        Ok(())
    }

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

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

    fn on_chan_open_confirm(
        &mut self,
        _output: &mut ModuleOutputBuilder,
        _port_id: &PortId,
        _channel_id: &ChannelId,
    ) -> Result<(), Error> {
        Ok(())
    }

    fn on_chan_close_init(
        &mut self,
        _output: &mut ModuleOutputBuilder,
        _port_id: &PortId,
        _channel_id: &ChannelId,
    ) -> Result<(), Error> {
        Ok(())
    }

    fn on_chan_close_confirm(
        &mut self,
        _output: &mut ModuleOutputBuilder,
        _port_id: &PortId,
        _channel_id: &ChannelId,
    ) -> Result<(), Error> {
        Ok(())
    }

    fn on_recv_packet(
        &self,
        _output: &mut ModuleOutputBuilder,
        _packet: &Packet,
        _relayer: &Signer,
    ) -> OnRecvPacketAck {
        OnRecvPacketAck::Nil(Box::new(|_| Ok(())))
    }

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

    fn on_timeout_packet(
        &mut self,
        _output: &mut ModuleOutputBuilder,
        _packet: &Packet,
        _relayer: &Signer,
    ) -> Result<(), Error> {
        Ok(())
    }
}

pub trait RouterBuilder: Sized {
    /// The `Router` type that the builder must build
    type Router: Router;

    /// Registers `Module` against the specified `ModuleId` in the `Router`'s internal map
    ///
    /// Returns an error if a `Module` has already been registered against the specified `ModuleId`
    fn add_route(self, module_id: ModuleId, module: impl Module) -> Result<Self, String>;

    /// Consumes the `RouterBuilder` and returns a `Router` as configured
    fn build(self) -> Self::Router;
}

pub trait AsAnyMut: Any {
    fn as_any_mut(&mut self) -> &mut dyn Any;
}

impl<M: Any + Module> AsAnyMut for M {
    fn as_any_mut(&mut self) -> &mut dyn Any {
        self
    }
}

/// A router maintains a mapping of `ModuleId`s against `Modules`. Implementations must not publicly
/// expose APIs to add new routes once constructed. Routes may only be added at the time of
/// instantiation using the `RouterBuilder`.
pub trait Router {
    /// Returns a mutable reference to a `Module` registered against the specified `ModuleId`
    fn get_route_mut(&mut self, module_id: &impl Borrow<ModuleId>) -> Option<&mut dyn Module>;

    /// Returns true if the `Router` has a `Module` registered against the specified `ModuleId`
    fn has_route(&self, module_id: &impl Borrow<ModuleId>) -> bool;
}