-
Notifications
You must be signed in to change notification settings - Fork 329
/
client.rs
132 lines (108 loc) · 4.25 KB
/
client.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
use abscissa_core::{Command, Options, Runnable};
use serde_json::json;
use ibc::events::IBCEvent;
use ibc::ics24_host::identifier::ClientId;
use relayer::chain::runtime::ChainRuntime;
use relayer::chain::CosmosSDKChain;
use relayer::config::ChainConfig;
use relayer::foreign_client::{build_create_client_and_send, build_update_client_and_send};
use crate::application::app_config;
use crate::conclude::Output;
use crate::error::{Error, Kind};
use crate::prelude::*;
#[derive(Clone, Command, Debug, Options)]
pub struct TxCreateClientCmd {
#[options(free, help = "identifier of the destination chain")]
dst_chain_id: String,
#[options(free, help = "identifier of the source chain")]
src_chain_id: String,
}
impl Runnable for TxCreateClientCmd {
fn run(&self) {
let (dst_chain_config, src_chain_config) =
match validate_common_options(&self.dst_chain_id, &self.src_chain_id) {
Ok(result) => result,
Err(err) => {
return Output::with_error().with_result(json!(err)).exit();
}
};
status_info!(
"Message CreateClient",
"for source chain: {:?}, on destination chain: {:?}",
src_chain_config.id,
dst_chain_config.id
);
let (src_chain, _) = ChainRuntime::<CosmosSDKChain>::spawn(src_chain_config).unwrap();
let (dst_chain, _) = ChainRuntime::<CosmosSDKChain>::spawn(dst_chain_config).unwrap();
let res: Result<IBCEvent, Error> = build_create_client_and_send(dst_chain, src_chain)
.map_err(|e| Kind::Tx.context(e).into());
match res {
Ok(receipt) => Output::with_success().with_result(json!(receipt)).exit(),
Err(e) => Output::with_error()
.with_result(json!(format!("{}", e)))
.exit(),
}
}
}
#[derive(Clone, Command, Debug, Options)]
pub struct TxUpdateClientCmd {
#[options(free, help = "identifier of the destination chain")]
dst_chain_id: String,
#[options(free, help = "identifier of the source chain")]
src_chain_id: String,
#[options(
free,
help = "identifier of the client to be updated on destination chain"
)]
dst_client_id: ClientId,
}
impl Runnable for TxUpdateClientCmd {
fn run(&self) {
let opts = validate_common_options(&self.dst_chain_id, &self.src_chain_id);
let (dst_chain_config, src_chain_config) = match opts {
Ok(result) => result,
Err(err) => {
return Output::with_error().with_result(json!(err)).exit();
}
};
status_info!(
"Message UpdateClient",
"id: {:?}, for chain: {:?}, on chain: {:?}",
self.dst_client_id,
src_chain_config.id,
dst_chain_config.id
);
let (src_chain, _) = ChainRuntime::<CosmosSDKChain>::spawn(src_chain_config).unwrap();
let (dst_chain, _) = ChainRuntime::<CosmosSDKChain>::spawn(dst_chain_config).unwrap();
let res: Result<IBCEvent, Error> =
build_update_client_and_send(dst_chain, src_chain, &self.dst_client_id)
.map_err(|e| Kind::Tx.context(e).into());
match res {
Ok(receipt) => Output::with_success().with_result(json!(receipt)).exit(),
Err(e) => Output::with_error()
.with_result(json!(format!("{}", e)))
.exit(),
}
}
}
fn validate_common_options(
dst_chain_id: &str,
src_chain_id: &str,
) -> Result<(ChainConfig, ChainConfig), String> {
let config = app_config();
// Validate parameters
let dst_chain_id = dst_chain_id
.parse()
.map_err(|_| "bad destination chain identifier".to_string())?;
let src_chain_id = src_chain_id
.parse()
.map_err(|_| "bad source chain identifier".to_string())?;
// Get the source and destination chain configuration
let dst_chain_config = config
.find_chain(&dst_chain_id)
.ok_or_else(|| "missing destination chain configuration".to_string())?;
let src_chain_config = config
.find_chain(&src_chain_id)
.ok_or_else(|| "missing source chain configuration".to_string())?;
Ok((dst_chain_config.clone(), src_chain_config.clone()))
}