-
Notifications
You must be signed in to change notification settings - Fork 93
/
Copy pathcontext.rs
351 lines (306 loc) · 9.89 KB
/
context.rs
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
use crate::core::ics04_channel::handler::ModuleExtras;
use crate::prelude::*;
use alloc::borrow::{Borrow, Cow};
use core::any::Any;
use core::{
fmt::{Debug, Display, Error as FmtError, Formatter},
str::FromStr,
};
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::{ChannelError, PacketError};
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 RouterContext:
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;
#[cfg_attr(
feature = "parity-scale-codec",
derive(
parity_scale_codec::Encode,
parity_scale_codec::Decode,
scale_info::TypeInfo
)
)]
#[cfg_attr(
feature = "borsh",
derive(borsh::BorshSerialize, borsh::BorshDeserialize)
)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
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 Display for ModuleId {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), FmtError> {
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 + Debug {
#[cfg(feature = "val_exec_ctx")]
#[allow(clippy::too_many_arguments)]
fn on_chan_open_init_validate(
&self,
order: Order,
connection_hops: &[ConnectionId],
port_id: &PortId,
channel_id: &ChannelId,
counterparty: &Counterparty,
version: &Version,
) -> Result<Version, ChannelError>;
#[cfg(feature = "val_exec_ctx")]
#[allow(clippy::too_many_arguments)]
fn on_chan_open_init_execute(
&mut self,
order: Order,
connection_hops: &[ConnectionId],
port_id: &PortId,
channel_id: &ChannelId,
counterparty: &Counterparty,
version: &Version,
) -> Result<(ModuleExtras, Version), ChannelError>;
#[allow(clippy::too_many_arguments)]
fn on_chan_open_init(
&mut self,
order: Order,
connection_hops: &[ConnectionId],
port_id: &PortId,
channel_id: &ChannelId,
counterparty: &Counterparty,
version: &Version,
) -> Result<(ModuleExtras, Version), ChannelError>;
#[cfg(feature = "val_exec_ctx")]
#[allow(clippy::too_many_arguments)]
fn on_chan_open_try_validate(
&self,
order: Order,
connection_hops: &[ConnectionId],
port_id: &PortId,
channel_id: &ChannelId,
counterparty: &Counterparty,
counterparty_version: &Version,
) -> Result<Version, ChannelError>;
#[cfg(feature = "val_exec_ctx")]
#[allow(clippy::too_many_arguments)]
fn on_chan_open_try_execute(
&mut self,
order: Order,
connection_hops: &[ConnectionId],
port_id: &PortId,
channel_id: &ChannelId,
counterparty: &Counterparty,
counterparty_version: &Version,
) -> Result<(ModuleExtras, Version), ChannelError>;
#[allow(clippy::too_many_arguments)]
fn on_chan_open_try(
&mut self,
order: Order,
connection_hops: &[ConnectionId],
port_id: &PortId,
channel_id: &ChannelId,
counterparty: &Counterparty,
counterparty_version: &Version,
) -> Result<(ModuleExtras, Version), ChannelError>;
#[cfg(feature = "val_exec_ctx")]
fn on_chan_open_ack_validate(
&self,
_port_id: &PortId,
_channel_id: &ChannelId,
_counterparty_version: &Version,
) -> Result<(), ChannelError> {
Ok(())
}
#[cfg(feature = "val_exec_ctx")]
fn on_chan_open_ack_execute(
&mut self,
_port_id: &PortId,
_channel_id: &ChannelId,
_counterparty_version: &Version,
) -> Result<ModuleExtras, ChannelError> {
Ok(ModuleExtras::empty())
}
fn on_chan_open_ack(
&mut self,
_port_id: &PortId,
_channel_id: &ChannelId,
_counterparty_version: &Version,
) -> Result<ModuleExtras, ChannelError> {
Ok(ModuleExtras::empty())
}
#[cfg(feature = "val_exec_ctx")]
fn on_chan_open_confirm_validate(
&self,
_port_id: &PortId,
_channel_id: &ChannelId,
) -> Result<(), ChannelError> {
Ok(())
}
#[cfg(feature = "val_exec_ctx")]
fn on_chan_open_confirm_execute(
&mut self,
_port_id: &PortId,
_channel_id: &ChannelId,
) -> Result<ModuleExtras, ChannelError> {
Ok(ModuleExtras::empty())
}
fn on_chan_open_confirm(
&mut self,
_port_id: &PortId,
_channel_id: &ChannelId,
) -> Result<ModuleExtras, ChannelError> {
Ok(ModuleExtras::empty())
}
#[cfg(feature = "val_exec_ctx")]
fn on_chan_close_init_validate(
&self,
_port_id: &PortId,
_channel_id: &ChannelId,
) -> Result<(), ChannelError> {
Ok(())
}
#[cfg(feature = "val_exec_ctx")]
fn on_chan_close_init_execute(
&mut self,
_port_id: &PortId,
_channel_id: &ChannelId,
) -> Result<ModuleExtras, ChannelError> {
Ok(ModuleExtras::empty())
}
fn on_chan_close_init(
&mut self,
_port_id: &PortId,
_channel_id: &ChannelId,
) -> Result<ModuleExtras, ChannelError> {
Ok(ModuleExtras::empty())
}
#[cfg(feature = "val_exec_ctx")]
fn on_chan_close_confirm_validate(
&self,
_port_id: &PortId,
_channel_id: &ChannelId,
) -> Result<(), ChannelError> {
Ok(())
}
#[cfg(feature = "val_exec_ctx")]
fn on_chan_close_confirm_execute(
&mut self,
_port_id: &PortId,
_channel_id: &ChannelId,
) -> Result<ModuleExtras, ChannelError> {
Ok(ModuleExtras::empty())
}
fn on_chan_close_confirm(
&mut self,
_port_id: &PortId,
_channel_id: &ChannelId,
) -> Result<ModuleExtras, ChannelError> {
Ok(ModuleExtras::empty())
}
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<(), PacketError> {
Ok(())
}
fn on_timeout_packet(
&mut self,
_output: &mut ModuleOutputBuilder,
_packet: &Packet,
_relayer: &Signer,
) -> Result<(), PacketError> {
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;
}