-
Notifications
You must be signed in to change notification settings - Fork 332
/
connection.rs
125 lines (105 loc) · 4.13 KB
/
connection.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
use std::sync::Arc;
use abscissa_core::{Command, Options, Runnable};
use tokio::runtime::Runtime as TokioRuntime;
use ibc::{
ics03_connection::connection::State,
ics24_host::identifier::ConnectionId,
ics24_host::identifier::{ChainId, PortChannelId},
};
use ibc_proto::ibc::core::channel::v1::QueryConnectionChannelsRequest;
use ibc_relayer::chain::{Chain, CosmosSdkChain};
use crate::conclude::Output;
use crate::error::{Error, Kind};
use crate::prelude::*;
#[derive(Clone, Command, Debug, Options)]
pub struct QueryConnectionEndCmd {
#[options(free, required, help = "identifier of the chain to query")]
chain_id: ChainId,
#[options(free, required, help = "identifier of the connection to query")]
connection_id: ConnectionId,
#[options(help = "height of the state to query", short = "h")]
height: Option<u64>,
}
// cargo run --bin hermes -- query connection end ibc-test connectionidone --height 3
impl Runnable for QueryConnectionEndCmd {
fn run(&self) {
let config = app_config();
let chain_config = match config.find_chain(&self.chain_id) {
None => {
return Output::error(format!(
"chain '{}' not found in configuration file",
self.chain_id
))
.exit()
}
Some(chain_config) => chain_config,
};
debug!("Options: {:?}", self);
let rt = Arc::new(TokioRuntime::new().unwrap());
let chain = CosmosSdkChain::bootstrap(chain_config.clone(), rt).unwrap();
let height = ibc::Height::new(chain.id().version(), self.height.unwrap_or(0_u64));
let res = chain.query_connection(&self.connection_id, height);
match res {
Ok(connection_end) => {
if connection_end.state_matches(&State::Uninitialized) {
Output::error(format!(
"connection '{}' does not exist",
self.connection_id
))
.exit()
} else {
Output::success(connection_end).exit()
}
}
Err(e) => Output::error(format!("{}", e)).exit(),
}
}
}
/// Command for querying the channel identifiers associated with a connection.
/// Sample invocation:
/// `cargo run --bin hermes -- query connection channels ibc-0 connection-0`
#[derive(Clone, Command, Debug, Options)]
pub struct QueryConnectionChannelsCmd {
#[options(free, required, help = "identifier of the chain to query")]
chain_id: ChainId,
#[options(free, required, help = "identifier of the connection to query")]
connection_id: ConnectionId,
}
impl Runnable for QueryConnectionChannelsCmd {
fn run(&self) {
let config = app_config();
let chain_config = match config.find_chain(&self.chain_id) {
None => {
return Output::error(format!(
"chain '{}' not found in configuration file",
self.chain_id
))
.exit()
}
Some(chain_config) => chain_config,
};
debug!("Options: {:?}", self);
let rt = Arc::new(TokioRuntime::new().unwrap());
let chain = CosmosSdkChain::bootstrap(chain_config.clone(), rt).unwrap();
let req = QueryConnectionChannelsRequest {
connection: self.connection_id.to_string(),
pagination: ibc_proto::cosmos::base::query::pagination::all(),
};
let res: Result<_, Error> = chain
.query_connection_channels(req)
.map_err(|e| Kind::Query.context(e).into());
match res {
Ok(channels) => {
let ids: Vec<PortChannelId> = channels
.into_iter()
.map(|identified_channel| PortChannelId {
port_id: identified_channel.port_id,
channel_id: identified_channel.channel_id,
})
.collect();
Output::success(ids).exit()
}
Err(e) => Output::error(format!("{}", e)).exit(),
}
}
}