ibc-relayer-cli 1.13.2

Hermes is an IBC Relayer written in Rust
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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
use abscissa_core::clap::Parser;

use console::style;
use dialoguer::Confirm;

use ibc_relayer::chain::handle::ChainHandle;
use ibc_relayer::chain::requests::{
    IncludeProof, QueryClientStateRequest, QueryConnectionRequest, QueryHeight,
};
use ibc_relayer::channel::Channel;
use ibc_relayer::config::default::connection_delay;
use ibc_relayer::connection::Connection;
use ibc_relayer::foreign_client::ForeignClient;
use ibc_relayer_types::core::ics03_connection::connection::IdentifiedConnectionEnd;
use ibc_relayer_types::core::ics04_channel::channel::Ordering;
use ibc_relayer_types::core::ics04_channel::version::Version;
use ibc_relayer_types::core::ics24_host::identifier::{ChainId, ConnectionId, PortId};

use crate::cli_utils::{spawn_chain_runtime, ChainHandlePair};
use crate::conclude::{exit_with_unrecoverable_error, Output};
use crate::prelude::*;

static PROMPT: &str = "Are you sure you want a new connection & clients to be created? Hermes will use default security parameters.";
static HINT: &str = "Consider using the default invocation\n\nhermes create channel --a-port <PORT-ID> --b-port <PORT-ID> --a-chain <CHAIN-A-ID> --a-connection <CONNECTION-A-ID>\n\nto reuse a pre-existing connection.";

/// The data structure that represents all the possible options when invoking
/// the `create channel` CLI command.
///
/// There are two possible ways to invoke this command:
///
/// `create channel --a-port <A_PORT_ID> --b-port <B_PORT_ID> --a-chain <A_CHAIN_ID> --a-connection <A_CONNECTION_ID>`
/// is the default way in which this command should be used, specifying a `Connection-ID`
/// associated with chain A for this new channel to reuse.
///
/// `create channel --a-port <A_PORT_ID> --b-port <B_PORT_ID> --a-chain <A_CHAIN_ID> --b-chain <B_CHAIN_ID> --new-client-connection`
/// can alternatively be used to indicate that a new connection/client pair is being
/// created as part of this new channel. This brings up an interactive yes/no prompt
/// to ensure that the operator at least considers the fact that they're initializing a
/// new connection with the channel. This prompt can be skipped by appending the `--yes`
/// flag to the command.
///
/// Note that `Connection-ID`s have to be considered based off of the chain's perspective. Although
/// chain A and chain B might refer to the connection with different names, they are actually referring
/// to the same connection.
#[derive(Clone, Command, Debug, Parser, PartialEq, Eq)]
#[clap(
    override_usage = "hermes create channel [OPTIONS] --a-chain <A_CHAIN_ID> --a-connection <A_CONNECTION_ID> --a-port <A_PORT_ID> --b-port <B_PORT_ID>

    hermes create channel [OPTIONS] --a-chain <A_CHAIN_ID> --b-chain <B_CHAIN_ID> --a-port <A_PORT_ID> --b-port <B_PORT_ID> --new-client-connection"
)]
pub struct CreateChannelCommand {
    #[clap(
        long = "a-chain",
        required = true,
        value_name = "A_CHAIN_ID",
        help_heading = "FLAGS",
        help = "Identifier of the side `a` chain for the new channel"
    )]
    chain_a: ChainId,

    #[clap(
        long = "b-chain",
        value_name = "B_CHAIN_ID",
        required = true,
        requires = "new-client-connection",
        group = "b_chain_group",
        help_heading = "FLAGS",
        help = "Identifier of the side `b` chain for the new channel"
    )]
    chain_b: Option<ChainId>,

    #[clap(
        long = "a-connection",
        visible_alias = "a-conn",
        value_name = "A_CONNECTION_ID",
        required = true,
        groups = &["b_chain_group", "new_client_group"],
        help_heading = "FLAGS",
        help = "Identifier of the connection on chain `a` to use in creating the new channel"
    )]
    connection_a: Option<ConnectionId>,

    #[clap(
        long = "a-port",
        required = true,
        value_name = "A_PORT_ID",
        help_heading = "FLAGS",
        help = "Identifier of the side `a` port for the new channel"
    )]
    port_a: PortId,

    #[clap(
        long = "b-port",
        required = true,
        value_name = "B_PORT_ID",
        help_heading = "FLAGS",
        help = "Identifier of the side `b` port for the new channel"
    )]
    port_b: PortId,

    #[clap(
        long = "order",
        value_name = "ORDER",
        help = "The channel ordering, valid options 'unordered' (default) and 'ordered'",
        default_value_t
    )]
    order: Ordering,

    #[clap(
        long = "channel-version",
        visible_alias = "chan-version",
        value_name = "VERSION",
        help = "The version for the new channel"
    )]
    version: Option<Version>,

    #[clap(
        long = "new-client-connection",
        visible_alias = "new-client-conn",
        required = true,
        group = "new_client_group",
        help = "Indicates that a new client and connection will be created underlying the new channel"
    )]
    new_client_connection: bool,

    #[clap(
        long = "yes",
        requires = "new-client-connection",
        help = "Skip new_client_connection confirmation"
    )]
    yes: bool,
}

impl Runnable for CreateChannelCommand {
    fn run(&self) {
        match &self.connection_a {
            Some(conn) => self.run_reusing_connection(conn),
            None => {
                if let Some(chain_b) = &self.chain_b {
                    if self.new_client_connection {
                        if self.yes {
                            self.run_using_new_connection(chain_b);
                        } else {
                            match Confirm::new()
                                .with_prompt(format!(
                                    "{}: {}\n{}: {}",
                                    style("WARN").yellow(),
                                    PROMPT,
                                    style("Hint").cyan(),
                                    HINT
                                ))
                                .interact()
                            {
                                Ok(confirm) => {
                                    if confirm {
                                        self.run_using_new_connection(chain_b);
                                    } else {
                                        Output::error("You elected not to create new clients and connections. Please re-invoke `create channel` with a pre-existing connection ID".to_string()).exit();
                                    }
                                }
                                Err(e) => {
                                    Output::error(format!(
                                        "An error occurred while waiting for user input: {e}"
                                    ));
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

impl CreateChannelCommand {
    /// Creates a new channel, as well as a new underlying connection and clients.
    fn run_using_new_connection(&self, chain_b: &ChainId) {
        let config = app_config();

        let chains = ChainHandlePair::spawn(&config, &self.chain_a, chain_b)
            .unwrap_or_else(exit_with_unrecoverable_error);

        info!(
            "Creating new clients, new connection, and a new channel with order {}",
            self.order
        );

        let client_a = ForeignClient::new(chains.src.clone(), chains.dst.clone())
            .unwrap_or_else(exit_with_unrecoverable_error);
        let client_b = ForeignClient::new(chains.dst.clone(), chains.src)
            .unwrap_or_else(exit_with_unrecoverable_error);

        // Create the connection.
        let con = Connection::new(client_a, client_b, connection_delay())
            .unwrap_or_else(exit_with_unrecoverable_error);

        // Finally create the channel.
        let channel = Channel::new(
            con,
            self.order,
            self.port_a.clone(),
            self.port_b.clone(),
            self.version.clone(),
        )
        .unwrap_or_else(exit_with_unrecoverable_error);

        Output::success(channel).exit();
    }

    /// Creates a new channel, reusing an already existing connection and its clients.
    fn run_reusing_connection(&self, connection_a: &ConnectionId) {
        let config = app_config();

        // Validate & spawn runtime for side a.
        let chain_a = spawn_chain_runtime(&config, &self.chain_a)
            .unwrap_or_else(exit_with_unrecoverable_error);

        // Query the connection end.
        let (conn_end, _) = chain_a
            .query_connection(
                QueryConnectionRequest {
                    connection_id: connection_a.clone(),
                    height: QueryHeight::Latest,
                },
                IncludeProof::No,
            )
            .unwrap_or_else(exit_with_unrecoverable_error);

        // Query the client state, obtain the identifier of chain b.
        let chain_b = chain_a
            .query_client_state(
                QueryClientStateRequest {
                    client_id: conn_end.client_id().clone(),
                    height: QueryHeight::Latest,
                },
                IncludeProof::No,
            )
            .map(|(cs, _)| cs.chain_id())
            .unwrap_or_else(exit_with_unrecoverable_error);

        // Spawn the runtime for side b.
        let chain_b =
            spawn_chain_runtime(&config, &chain_b).unwrap_or_else(exit_with_unrecoverable_error);

        // Create the foreign client handles.
        let client_a = ForeignClient::find(chain_b.clone(), chain_a.clone(), conn_end.client_id())
            .unwrap_or_else(exit_with_unrecoverable_error);
        let client_b = ForeignClient::find(chain_a, chain_b, conn_end.counterparty().client_id())
            .unwrap_or_else(exit_with_unrecoverable_error);

        let identified_end = IdentifiedConnectionEnd::new(connection_a.clone(), conn_end);

        let connection = Connection::find(client_a, client_b, &identified_end)
            .unwrap_or_else(exit_with_unrecoverable_error);

        let channel = Channel::new(
            connection,
            self.order,
            self.port_a.clone(),
            self.port_b.clone(),
            self.version.clone(),
        )
        .unwrap_or_else(exit_with_unrecoverable_error);

        Output::success(channel).exit();
    }
}

#[cfg(test)]
mod tests {
    use std::str::FromStr;

    use super::CreateChannelCommand;
    use abscissa_core::clap::Parser;

    use ibc_relayer_types::core::ics04_channel::channel::Ordering;
    use ibc_relayer_types::core::ics04_channel::version::Version;
    use ibc_relayer_types::core::ics24_host::identifier::{ChainId, ConnectionId, PortId};

    #[test]
    fn test_create_channel_a_conn_required() {
        assert_eq!(
            CreateChannelCommand {
                chain_a: ChainId::from_string("chain_a"),
                chain_b: None,
                connection_a: Some(ConnectionId::from_str("connection_a").unwrap()),
                port_a: PortId::from_str("port_id_a").unwrap(),
                port_b: PortId::from_str("port_id_b").unwrap(),
                order: Ordering::Unordered,
                version: None,
                new_client_connection: false,
                yes: false
            },
            CreateChannelCommand::parse_from([
                "test",
                "--a-chain",
                "chain_a",
                "--a-connection",
                "connection_a",
                "--a-port",
                "port_id_a",
                "--b-port",
                "port_id_b"
            ])
        )
    }

    #[test]
    fn test_create_channel_version() {
        assert_eq!(
            CreateChannelCommand {
                chain_a: ChainId::from_string("chain_a"),
                chain_b: None,
                connection_a: Some(ConnectionId::from_str("connection_a").unwrap()),
                port_a: PortId::from_str("port_id_a").unwrap(),
                port_b: PortId::from_str("port_id_b").unwrap(),
                order: Ordering::Unordered,
                version: Some(Version::new("v1".to_owned())),
                new_client_connection: false,
                yes: false
            },
            CreateChannelCommand::parse_from([
                "test",
                "--a-chain",
                "chain_a",
                "--a-connection",
                "connection_a",
                "--a-port",
                "port_id_a",
                "--b-port",
                "port_id_b",
                "--channel-version",
                "v1"
            ])
        )
    }

    #[test]
    fn test_create_channel_order() {
        assert_eq!(
            CreateChannelCommand {
                chain_a: ChainId::from_string("chain_a"),
                chain_b: None,
                connection_a: Some(ConnectionId::from_str("connection_a").unwrap()),
                port_a: PortId::from_str("port_id_a").unwrap(),
                port_b: PortId::from_str("port_id_b").unwrap(),
                order: Ordering::Ordered,
                version: None,
                new_client_connection: false,
                yes: false
            },
            CreateChannelCommand::parse_from([
                "test",
                "--a-chain",
                "chain_a",
                "--a-connection",
                "connection_a",
                "--a-port",
                "port_id_a",
                "--b-port",
                "port_id_b",
                "--order",
                "ordered"
            ])
        )
    }

    #[test]
    fn test_create_channel_a_conn_alias() {
        assert_eq!(
            CreateChannelCommand {
                chain_a: ChainId::from_string("chain_a"),
                chain_b: None,
                connection_a: Some(ConnectionId::from_str("connection_a").unwrap()),
                port_a: PortId::from_str("port_id_a").unwrap(),
                port_b: PortId::from_str("port_id_b").unwrap(),
                order: Ordering::Unordered,
                version: None,
                new_client_connection: false,
                yes: false
            },
            CreateChannelCommand::parse_from([
                "test",
                "--a-chain",
                "chain_a",
                "--a-conn",
                "connection_a",
                "--a-port",
                "port_id_a",
                "--b-port",
                "port_id_b"
            ])
        )
    }

    #[test]
    fn test_create_channel_a_conn_with_new_client_conn() {
        assert!(CreateChannelCommand::try_parse_from([
            "test",
            "--a-chain",
            "chain_a",
            "--a-connection",
            "connection_a",
            "--a-port",
            "port_id_a",
            "--b-port",
            "port_id_b",
            "--new-client-connection"
        ])
        .is_err())
    }

    #[test]
    fn test_create_channel_a_conn_with_yes() {
        assert!(CreateChannelCommand::try_parse_from([
            "test",
            "--a-chain",
            "chain_a",
            "--b-chain",
            "chain_b",
            "--a-connection",
            "connection_a",
            "--a-port",
            "port_id_a",
            "--b-port",
            "port_id_b",
            "--order",
            "ordered",
            "--yes"
        ])
        .is_err())
    }

    #[test]
    fn test_create_channel_a_conn_with_b_chain() {
        assert!(CreateChannelCommand::try_parse_from([
            "test",
            "--a-chain",
            "chain_a",
            "--b-chain",
            "chain_b",
            "--a-connection",
            "connection_a",
            "--a-port",
            "port_id_a",
            "--b-port",
            "port_id_b",
            "--order",
            "ordered"
        ])
        .is_err())
    }

    #[test]
    fn test_create_channel_b_chain() {
        assert_eq!(
            CreateChannelCommand {
                chain_a: ChainId::from_string("chain_a"),
                chain_b: Some(ChainId::from_string("chain_b")),
                connection_a: None,
                port_a: PortId::from_str("port_id_a").unwrap(),
                port_b: PortId::from_str("port_id_b").unwrap(),
                order: Ordering::Unordered,
                version: None,
                new_client_connection: true,
                yes: false
            },
            CreateChannelCommand::parse_from([
                "test",
                "--a-chain",
                "chain_a",
                "--b-chain",
                "chain_b",
                "--a-port",
                "port_id_a",
                "--b-port",
                "port_id_b",
                "--new-client-connection"
            ])
        )
    }

    #[test]
    fn test_create_channel_b_chain_yes() {
        assert_eq!(
            CreateChannelCommand {
                chain_a: ChainId::from_string("chain_a"),
                chain_b: Some(ChainId::from_string("chain_b")),
                connection_a: None,
                port_a: PortId::from_str("port_id_a").unwrap(),
                port_b: PortId::from_str("port_id_b").unwrap(),
                order: Ordering::Unordered,
                version: None,
                new_client_connection: true,
                yes: true
            },
            CreateChannelCommand::parse_from([
                "test",
                "--a-chain",
                "chain_a",
                "--b-chain",
                "chain_b",
                "--a-port",
                "port_id_a",
                "--b-port",
                "port_id_b",
                "--new-client-connection",
                "--yes"
            ])
        )
    }

    #[test]
    fn test_create_channel_b_new_client_conn_alias() {
        assert_eq!(
            CreateChannelCommand {
                chain_a: ChainId::from_string("chain_a"),
                chain_b: Some(ChainId::from_string("chain_b")),
                connection_a: None,
                port_a: PortId::from_str("port_id_a").unwrap(),
                port_b: PortId::from_str("port_id_b").unwrap(),
                order: Ordering::Unordered,
                version: None,
                new_client_connection: true,
                yes: false
            },
            CreateChannelCommand::parse_from([
                "test",
                "--a-chain",
                "chain_a",
                "--b-chain",
                "chain_b",
                "--a-port",
                "port_id_a",
                "--b-port",
                "port_id_b",
                "--new-client-conn"
            ])
        )
    }

    #[test]
    fn test_create_channel_b_chain_without_new_client() {
        assert!(CreateChannelCommand::try_parse_from([
            "test",
            "--a-chain",
            "chain_a",
            "--b-chain",
            "chain_b",
            "--a-port",
            "port_id_a",
            "--b-port",
            "port_id_b"
        ])
        .is_err())
    }

    #[test]
    fn test_create_channel_no_b_port() {
        assert!(CreateChannelCommand::try_parse_from([
            "test",
            "--a-chain",
            "chain_a",
            "--b-chain",
            "chain_b",
            "--a-connection",
            "connection_a",
            "--a-port",
            "port_id_a"
        ])
        .is_err())
    }

    #[test]
    fn test_create_channel_no_a_port() {
        assert!(CreateChannelCommand::try_parse_from([
            "test",
            "--a-chain",
            "chain_a",
            "--b-chain",
            "chain_b",
            "--a-connection",
            "connection_a",
            "--b-port",
            "port_id_b"
        ])
        .is_err())
    }

    #[test]
    fn test_create_channel_no_b_chain_nor_a_conn() {
        assert!(CreateChannelCommand::try_parse_from([
            "test",
            "--a-chain",
            "chain_a",
            "--a-port",
            "port_id_a",
            "--b-port",
            "port_id_b"
        ])
        .is_err())
    }

    #[test]
    fn test_create_channel_no_a_chain() {
        assert!(CreateChannelCommand::try_parse_from([
            "test",
            "--b-chain",
            "chain_b",
            "--a-connection",
            "connection_a",
            "--a-port",
            "port_id_a",
            "--b-port",
            "port_id_b"
        ])
        .is_err())
    }
}