-
Notifications
You must be signed in to change notification settings - Fork 327
/
channel.rs
176 lines (146 loc) · 5.95 KB
/
channel.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
use abscissa_core::{Clap, Command, Runnable};
use clap::AppSettings;
use ibc::core::ics02_client::client_state::ClientState;
use ibc::core::ics03_connection::connection::IdentifiedConnectionEnd;
use ibc::core::ics04_channel::channel::Order;
use ibc::core::ics24_host::identifier::{ChainId, ConnectionId, PortId};
use ibc::Height;
use ibc_relayer::chain::handle::ChainHandle;
use ibc_relayer::channel::Channel;
use ibc_relayer::connection::Connection;
use ibc_relayer::foreign_client::ForeignClient;
use crate::cli_utils::{spawn_chain_runtime, ChainHandlePair};
use crate::conclude::{exit_with_unrecoverable_error, Output};
use crate::prelude::*;
use ibc_relayer::config::default::connection_delay;
#[derive(Clone, Command, Debug, Clap)]
#[clap(setting(AppSettings::DisableVersionFlag))]
pub struct CreateChannelCommand {
#[clap(
required = true,
about = "identifier of the side `a` chain for the new channel"
)]
chain_a_id: ChainId,
#[clap(about = "identifier of the side `b` chain for the new channel (optional)")]
chain_b_id: Option<ChainId>,
#[clap(
short,
long,
about = "identifier of the connection on chain `a` to use in creating the new channel"
)]
connection_a: Option<ConnectionId>,
#[clap(
long,
required = true,
about = "identifier of the side `a` port for the new channel"
)]
port_a: PortId,
#[clap(
long,
required = true,
about = "identifier of the side `b` port for the new channel"
)]
port_b: PortId,
#[clap(
short,
long,
about = "the channel ordering, valid options 'unordered' (default) and 'ordered'",
default_value_t
)]
order: Order,
// FIXME: rename to avoid confusion with the common --version flag?
#[clap(short, long, about = "the version for the new channel")]
version: Option<String>,
}
impl Runnable for CreateChannelCommand {
fn run(&self) {
match &self.chain_b_id {
None => self.run_reusing_connection(),
Some(chain_b) => self.run_using_new_connection(chain_b),
}
}
}
impl CreateChannelCommand {
// Creates a new channel, as well as a new underlying connection and clients.
fn run_using_new_connection(&self, chain_b_id: &ChainId) {
let config = app_config();
// Bail with an explicit error. The user might be expecting to use this connection.
if self.connection_a.is_some() {
return Output::error(
"Option `<connection-a>` is incompatible with `<chain-b-id>`".to_string(),
)
.exit();
}
let chains = ChainHandlePair::spawn(&config, &self.chain_a_id, chain_b_id)
.unwrap_or_else(exit_with_unrecoverable_error);
// let version = self.chain_a_id.version();
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) {
let config = app_config();
// Validate & spawn runtime for side a.
let chain_a = spawn_chain_runtime(&config, &self.chain_a_id)
.unwrap_or_else(exit_with_unrecoverable_error);
// Unwrap the identifier of the connection on side a.
let connection_a_id =
match &self.connection_a {
Some(c) => c,
None => return Output::error(
"Option `--connection-a` is necessary when <chain-b-id> argument is missing"
.to_string(),
)
.exit(),
};
// Query the connection end.
let height = Height::new(chain_a.id().version(), 0);
let conn_end = chain_a
.query_connection(connection_a_id, height)
.unwrap_or_else(exit_with_unrecoverable_error);
// Query the client state, obtain the identifier of chain b.
let chain_b_id = chain_a
.query_client_state(conn_end.client_id(), height)
.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_id).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_id.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();
}
}