-
Notifications
You must be signed in to change notification settings - Fork 370
/
Copy pathinit.rs
111 lines (90 loc) · 3.27 KB
/
init.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
use std::future::Future;
// use crate::application::APPLICATION;
use crate::prelude::*;
use abscissa_core::{Command, Options, Runnable};
use tendermint::chain::Id as ChainId;
use tendermint::hash::Hash;
use tendermint::lite::Height;
use relayer::chain::tendermint::TendermintChain;
use relayer::client::trust_options::TrustOptions;
use relayer::config::{ChainConfig, Config};
use relayer::store::{sled::SledStore, Store};
#[derive(Command, Debug, Options)]
pub struct InitCmd {
#[options(free, help = "identifier of the chain to initialize light client for")]
chain_id: Option<ChainId>,
#[options(help = "trusted header hash", short = "x")]
hash: Option<Hash>,
#[options(help = "trusted header height", short = "h")]
height: Option<Height>,
}
#[derive(Clone, Debug)]
struct InitOptions {
/// identifier of chain to initialize light client for
chain_id: ChainId,
/// trusted header hash
trusted_hash: Hash,
/// trusted header height
trusted_height: Height,
}
impl InitCmd {
fn get_chain_config_and_options(
&self,
config: &Config,
) -> Result<(ChainConfig, InitOptions), String> {
match (&self.chain_id, &self.hash, self.height) {
(Some(chain_id), Some(trusted_hash), Some(trusted_height)) => {
let chain_config = config.chains.iter().find(|c| c.id == *chain_id);
match chain_config {
Some(chain_config) => {
let opts = InitOptions {
chain_id: *chain_id,
trusted_hash: *trusted_hash,
trusted_height,
};
Ok((chain_config.clone(), opts))
}
None => Err(format!("cannot find chain {} in config", chain_id)),
}
}
(None, _, _) => Err("missing chain identifier".to_string()),
(_, None, _) => Err("missing trusted hash".to_string()),
(_, _, None) => Err("missing trusted height".to_string()),
}
}
}
impl Runnable for InitCmd {
/// Initialize the light client for the given chain
fn run(&self) {
// FIXME: This just hangs and never runs the given future
// abscissa_tokio::run(&APPLICATION, ...).unwrap();
let config = app_config();
let (chain_config, opts) = match self.get_chain_config_and_options(&config) {
Err(err) => {
status_err!("invalid options: {}", err);
return;
}
Ok(result) => result,
};
block_on(async {
let trust_options = TrustOptions::new(
opts.trusted_hash,
opts.trusted_height,
chain_config.trusting_period,
Default::default(),
)
.unwrap();
let mut store: SledStore<TendermintChain> =
relayer::store::persistent(format!("store_{}.db", chain_config.id));
store.set_trust_options(trust_options).unwrap(); // FIXME: unwrap
});
}
}
fn block_on<F: Future>(future: F) -> F::Output {
tokio::runtime::Builder::new()
.basic_scheduler()
.enable_all()
.build()
.unwrap()
.block_on(future)
}