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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
use crate::prelude::*;
use prost_types::Any;
use tendermint::Time;
use crate::applications::ics20_fungible_token_transfer::relay_application_logic::send_transfer::send_transfer as ics20_msg_dispatcher;
use crate::core::ics02_client::handler::dispatch as ics2_msg_dispatcher;
use crate::core::ics03_connection::handler::dispatch as ics3_msg_dispatcher;
use crate::core::ics04_channel::handler::channel_dispatch as ics4_msg_dispatcher;
use crate::core::ics04_channel::handler::packet_dispatch as ics04_packet_msg_dispatcher;
use crate::core::ics26_routing::context::Ics26Context;
use crate::core::ics26_routing::error::Error;
use crate::core::ics26_routing::msgs::Ics26Envelope::{
self, Ics20Msg, Ics2Msg, Ics3Msg, Ics4ChannelMsg, Ics4PacketMsg,
};
use crate::{events::IbcEvent, handler::HandlerOutput};
pub fn deliver<Ctx>(now: Time, ctx: &mut Ctx, messages: Vec<Any>) -> Result<Vec<IbcEvent>, Error>
where
Ctx: Ics26Context,
{
let mut ctx_interim = ctx.clone();
let mut res: Vec<IbcEvent> = Vec::new();
for any_msg in messages {
let envelope = decode(any_msg)?;
let mut output = dispatch(now, &mut ctx_interim, envelope)?;
res.append(&mut output.events);
}
*ctx = ctx_interim;
Ok(res)
}
pub fn decode(message: Any) -> Result<Ics26Envelope, Error> {
message.try_into()
}
pub fn dispatch<Ctx>(
now: Time,
ctx: &mut Ctx,
msg: Ics26Envelope,
) -> Result<HandlerOutput<()>, Error>
where
Ctx: Ics26Context,
{
let output = match msg {
Ics2Msg(msg) => {
let handler_output = ics2_msg_dispatcher(now, ctx, msg).map_err(Error::ics02_client)?;
ctx.store_client_result(handler_output.result)
.map_err(Error::ics02_client)?;
HandlerOutput::builder()
.with_log(handler_output.log)
.with_events(handler_output.events)
.with_result(())
}
Ics3Msg(msg) => {
let handler_output = ics3_msg_dispatcher(ctx, msg).map_err(Error::ics03_connection)?;
ctx.store_connection_result(handler_output.result)
.map_err(Error::ics03_connection)?;
HandlerOutput::builder()
.with_log(handler_output.log)
.with_events(handler_output.events)
.with_result(())
}
Ics4ChannelMsg(msg) => {
let handler_output = ics4_msg_dispatcher(ctx, msg).map_err(Error::ics04_channel)?;
ctx.store_channel_result(handler_output.result)
.map_err(Error::ics04_channel)?;
HandlerOutput::builder()
.with_log(handler_output.log)
.with_events(handler_output.events)
.with_result(())
}
Ics20Msg(msg) => {
let handler_output =
ics20_msg_dispatcher(ctx, msg).map_err(Error::ics20_fungible_token_transfer)?;
ctx.store_packet_result(handler_output.result)
.map_err(Error::ics04_channel)?;
HandlerOutput::builder()
.with_log(handler_output.log)
.with_events(handler_output.events)
.with_result(())
}
Ics4PacketMsg(msg) => {
let handler_output =
ics04_packet_msg_dispatcher(ctx, msg).map_err(Error::ics04_channel)?;
ctx.store_packet_result(handler_output.result)
.map_err(Error::ics04_channel)?;
HandlerOutput::builder()
.with_log(handler_output.log)
.with_events(handler_output.events)
.with_result(())
}
};
Ok(output)
}
#[cfg(test)]
mod tests {
use crate::prelude::*;
use tendermint::Time;
use test_log::test;
use crate::core::ics02_client::client_consensus::AnyConsensusState;
use crate::core::ics02_client::client_state::AnyClientState;
use crate::events::IbcEvent;
use crate::{
applications::ics20_fungible_token_transfer::msgs::transfer::test_util::get_dummy_msg_transfer,
core::ics23_commitment::commitment::test_util::get_dummy_merkle_proof,
};
use crate::core::ics02_client::msgs::{
create_client::MsgCreateAnyClient, update_client::MsgUpdateAnyClient,
upgrade_client::MsgUpgradeAnyClient, ClientMsg,
};
use crate::core::ics03_connection::msgs::{
conn_open_ack::{test_util::get_dummy_raw_msg_conn_open_ack, MsgConnectionOpenAck},
conn_open_init::{test_util::get_dummy_raw_msg_conn_open_init, MsgConnectionOpenInit},
conn_open_try::{test_util::get_dummy_raw_msg_conn_open_try, MsgConnectionOpenTry},
ConnectionMsg,
};
use crate::core::ics04_channel::msgs::{
chan_close_confirm::{
test_util::get_dummy_raw_msg_chan_close_confirm, MsgChannelCloseConfirm,
},
chan_close_init::{test_util::get_dummy_raw_msg_chan_close_init, MsgChannelCloseInit},
chan_open_ack::{test_util::get_dummy_raw_msg_chan_open_ack, MsgChannelOpenAck},
chan_open_init::{test_util::get_dummy_raw_msg_chan_open_init, MsgChannelOpenInit},
chan_open_try::{test_util::get_dummy_raw_msg_chan_open_try, MsgChannelOpenTry},
recv_packet::{test_util::get_dummy_raw_msg_recv_packet, MsgRecvPacket},
timeout_on_close::{test_util::get_dummy_raw_msg_timeout_on_close, MsgTimeoutOnClose},
ChannelMsg, PacketMsg,
};
use crate::core::ics24_host::identifier::ConnectionId;
use crate::core::ics26_routing::handler::dispatch;
use crate::core::ics26_routing::msgs::Ics26Envelope;
use crate::mock::client_state::{MockClientState, MockConsensusState};
use crate::mock::context::MockContext;
use crate::mock::header::MockHeader;
use crate::test_utils::get_dummy_account_id;
use crate::timestamp::Timestamp;
use crate::Height;
#[test]
fn routing_module_and_keepers() {
struct Test {
name: String,
msg: Ics26Envelope,
want_pass: bool,
}
let default_signer = get_dummy_account_id();
let client_height = 5;
let start_client_height = Height::new(0, client_height);
let update_client_height = Height::new(0, 34);
let update_client_height_after_send = Height::new(0, 35);
let update_client_height_after_second_send = Height::new(0, 36);
let upgrade_client_height = Height::new(1, 2);
let upgrade_client_height_second = Height::new(1, 1);
let mut ctx = MockContext::default();
let create_client_msg = MsgCreateAnyClient::new(
AnyClientState::from(MockClientState::new(MockHeader::new(start_client_height))),
AnyConsensusState::Mock(MockConsensusState::new(MockHeader::new(
start_client_height,
))),
default_signer.clone(),
)
.unwrap();
let msg_conn_init =
MsgConnectionOpenInit::try_from(get_dummy_raw_msg_conn_open_init()).unwrap();
let correct_msg_conn_try = MsgConnectionOpenTry::try_from(get_dummy_raw_msg_conn_open_try(
client_height,
client_height,
))
.unwrap();
let incorrect_msg_conn_try = MsgConnectionOpenTry::try_from(
get_dummy_raw_msg_conn_open_try(client_height + 1, client_height + 1),
)
.unwrap();
let msg_conn_ack = MsgConnectionOpenAck::try_from(get_dummy_raw_msg_conn_open_ack(
client_height,
client_height,
))
.unwrap();
let msg_chan_init =
MsgChannelOpenInit::try_from(get_dummy_raw_msg_chan_open_init()).unwrap();
let mut incorrect_msg_chan_init = msg_chan_init.clone();
incorrect_msg_chan_init.channel.connection_hops = vec![ConnectionId::new(590)];
let msg_chan_try =
MsgChannelOpenTry::try_from(get_dummy_raw_msg_chan_open_try(client_height)).unwrap();
let msg_chan_ack =
MsgChannelOpenAck::try_from(get_dummy_raw_msg_chan_open_ack(client_height)).unwrap();
let msg_chan_close_init =
MsgChannelCloseInit::try_from(get_dummy_raw_msg_chan_close_init()).unwrap();
let msg_chan_close_confirm =
MsgChannelCloseConfirm::try_from(get_dummy_raw_msg_chan_close_confirm(client_height))
.unwrap();
let msg_transfer = get_dummy_msg_transfer(35);
let msg_transfer_two = get_dummy_msg_transfer(36);
let mut msg_to_on_close =
MsgTimeoutOnClose::try_from(get_dummy_raw_msg_timeout_on_close(36, 5)).unwrap();
msg_to_on_close.packet.sequence = 2.into();
msg_to_on_close.packet.timeout_height = msg_transfer_two.timeout_height;
msg_to_on_close.packet.timeout_timestamp = msg_transfer_two.timeout_timestamp;
let msg_recv_packet = MsgRecvPacket::try_from(get_dummy_raw_msg_recv_packet(35)).unwrap();
let res = dispatch(
Time::now(),
&mut ctx,
Ics26Envelope::Ics2Msg(ClientMsg::CreateClient(create_client_msg.clone())),
);
assert!(
res.is_ok(),
"ICS26 routing dispatch test 'client creation' failed for message {:?} with result: {:?}",
create_client_msg,
res
);
ctx.add_port(msg_chan_init.port_id().clone());
let mut events = res.unwrap().events;
let client_id_event = events.pop();
assert!(
client_id_event.is_some(),
"There was no event generated for client creation!"
);
let client_id = match client_id_event.unwrap() {
IbcEvent::CreateClient(create_client) => create_client.client_id().clone(),
event => panic!("unexpected IBC event: {:?}", event),
};
let tests: Vec<Test> = vec![
Test {
name: "Client update successful".to_string(),
msg: Ics26Envelope::Ics2Msg(ClientMsg::UpdateClient(MsgUpdateAnyClient {
client_id: client_id.clone(),
header: MockHeader::new(update_client_height)
.with_timestamp(Timestamp::now())
.into(),
signer: default_signer.clone(),
})),
want_pass: true,
},
Test {
name: "Client update fails due to stale header".to_string(),
msg: Ics26Envelope::Ics2Msg(ClientMsg::UpdateClient(MsgUpdateAnyClient {
client_id: client_id.clone(),
header: MockHeader::new(update_client_height).into(),
signer: default_signer.clone(),
})),
want_pass: false,
},
Test {
name: "Connection open init succeeds".to_string(),
msg: Ics26Envelope::Ics3Msg(ConnectionMsg::ConnectionOpenInit(
msg_conn_init.with_client_id(client_id.clone()),
)),
want_pass: true,
},
Test {
name: "Connection open try fails due to InvalidConsensusHeight (too high)"
.to_string(),
msg: Ics26Envelope::Ics3Msg(ConnectionMsg::ConnectionOpenTry(Box::new(
incorrect_msg_conn_try,
))),
want_pass: false,
},
Test {
name: "Connection open try succeeds".to_string(),
msg: Ics26Envelope::Ics3Msg(ConnectionMsg::ConnectionOpenTry(Box::new(
correct_msg_conn_try.with_client_id(client_id.clone()),
))),
want_pass: true,
},
Test {
name: "Connection open ack succeeds".to_string(),
msg: Ics26Envelope::Ics3Msg(ConnectionMsg::ConnectionOpenAck(Box::new(
msg_conn_ack,
))),
want_pass: true,
},
Test {
name: "Channel open init succeeds".to_string(),
msg: Ics26Envelope::Ics4ChannelMsg(ChannelMsg::ChannelOpenInit(msg_chan_init)),
want_pass: true,
},
Test {
name: "Channel open init fail due to missing connection".to_string(),
msg: Ics26Envelope::Ics4ChannelMsg(ChannelMsg::ChannelOpenInit(
incorrect_msg_chan_init,
)),
want_pass: false,
},
Test {
name: "Channel open try succeeds".to_string(),
msg: Ics26Envelope::Ics4ChannelMsg(ChannelMsg::ChannelOpenTry(msg_chan_try)),
want_pass: true,
},
Test {
name: "Channel open ack succeeds".to_string(),
msg: Ics26Envelope::Ics4ChannelMsg(ChannelMsg::ChannelOpenAck(msg_chan_ack)),
want_pass: true,
},
Test {
name: "Packet send".to_string(),
msg: Ics26Envelope::Ics20Msg(msg_transfer),
want_pass: true,
},
Test {
name: "Client update successful #2".to_string(),
msg: Ics26Envelope::Ics2Msg(ClientMsg::UpdateClient(MsgUpdateAnyClient {
client_id: client_id.clone(),
header: MockHeader::new(update_client_height_after_send)
.with_timestamp(Timestamp::now())
.into(),
signer: default_signer.clone(),
})),
want_pass: true,
},
Test {
name: "Receive packet".to_string(),
msg: Ics26Envelope::Ics4PacketMsg(PacketMsg::RecvPacket(msg_recv_packet.clone())),
want_pass: true,
},
Test {
name: "Re-Receive packet".to_string(),
msg: Ics26Envelope::Ics4PacketMsg(PacketMsg::RecvPacket(msg_recv_packet)),
want_pass: false,
},
Test {
name: "Packet send".to_string(),
msg: Ics26Envelope::Ics20Msg(msg_transfer_two),
want_pass: true,
},
Test {
name: "Client update successful".to_string(),
msg: Ics26Envelope::Ics2Msg(ClientMsg::UpdateClient(MsgUpdateAnyClient {
client_id: client_id.clone(),
header: MockHeader::new(update_client_height_after_second_send).into(),
signer: default_signer.clone(),
})),
want_pass: true,
},
Test {
name: "Channel close init succeeds".to_string(),
msg: Ics26Envelope::Ics4ChannelMsg(ChannelMsg::ChannelCloseInit(
msg_chan_close_init,
)),
want_pass: true,
},
Test {
name: "Channel close confirm fails cause channel is already closed".to_string(),
msg: Ics26Envelope::Ics4ChannelMsg(ChannelMsg::ChannelCloseConfirm(
msg_chan_close_confirm,
)),
want_pass: false,
},
Test {
name: "Timeout on close".to_string(),
msg: Ics26Envelope::Ics4PacketMsg(PacketMsg::ToClosePacket(msg_to_on_close)),
want_pass: true,
},
Test {
name: "Client upgrade successful".to_string(),
msg: Ics26Envelope::Ics2Msg(ClientMsg::UpgradeClient(MsgUpgradeAnyClient::new(
client_id.clone(),
AnyClientState::Mock(MockClientState::new(MockHeader::new(
upgrade_client_height,
))),
AnyConsensusState::Mock(MockConsensusState::new(MockHeader::new(
upgrade_client_height,
))),
get_dummy_merkle_proof(),
get_dummy_merkle_proof(),
default_signer.clone(),
))),
want_pass: true,
},
Test {
name: "Client upgrade un-successful".to_string(),
msg: Ics26Envelope::Ics2Msg(ClientMsg::UpgradeClient(MsgUpgradeAnyClient::new(
client_id,
AnyClientState::Mock(MockClientState::new(MockHeader::new(
upgrade_client_height_second,
))),
AnyConsensusState::Mock(MockConsensusState::new(MockHeader::new(
upgrade_client_height_second,
))),
get_dummy_merkle_proof(),
get_dummy_merkle_proof(),
default_signer,
))),
want_pass: false,
},
]
.into_iter()
.collect();
for test in tests {
let res = dispatch(Time::now(), &mut ctx, test.msg.clone());
assert_eq!(
test.want_pass,
res.is_ok(),
"ICS26 routing dispatch test '{}' failed for message {:?}\nwith result: {:?}",
test.name,
test.msg,
res
);
}
}
}