|
| 1 | +use std::future::Future; |
| 2 | + |
| 3 | +// use crate::application::APPLICATION; |
| 4 | +use crate::prelude::*; |
| 5 | + |
| 6 | +use abscissa_core::{Command, Options, Runnable}; |
| 7 | + |
| 8 | +use tendermint::chain::Id as ChainId; |
| 9 | +use tendermint::hash::Hash; |
| 10 | +use tendermint::lite::Height; |
| 11 | + |
| 12 | +use relayer::chain::tendermint::TendermintChain; |
| 13 | +use relayer::client::trust_options::TrustOptions; |
| 14 | +use relayer::config::{ChainConfig, Config}; |
| 15 | +use relayer::store::{sled::SledStore, Store}; |
| 16 | + |
| 17 | +#[derive(Command, Debug, Options)] |
| 18 | +pub struct InitCmd { |
| 19 | + #[options(free, help = "identifier of the chain to initialize light client for")] |
| 20 | + chain_id: Option<ChainId>, |
| 21 | + |
| 22 | + #[options(help = "trusted header hash", short = "x")] |
| 23 | + hash: Option<Hash>, |
| 24 | + |
| 25 | + #[options(help = "trusted header height", short = "h")] |
| 26 | + height: Option<Height>, |
| 27 | +} |
| 28 | + |
| 29 | +#[derive(Clone, Debug)] |
| 30 | +struct InitOptions { |
| 31 | + /// identifier of chain to initialize light client for |
| 32 | + chain_id: ChainId, |
| 33 | + |
| 34 | + /// trusted header hash |
| 35 | + trusted_hash: Hash, |
| 36 | + |
| 37 | + /// trusted header height |
| 38 | + trusted_height: Height, |
| 39 | +} |
| 40 | + |
| 41 | +impl InitCmd { |
| 42 | + fn get_chain_config_and_options( |
| 43 | + &self, |
| 44 | + config: &Config, |
| 45 | + ) -> Result<(ChainConfig, InitOptions), String> { |
| 46 | + match (&self.chain_id, &self.hash, self.height) { |
| 47 | + (Some(chain_id), Some(trusted_hash), Some(trusted_height)) => { |
| 48 | + let chain_config = config.chains.iter().find(|c| c.id == *chain_id); |
| 49 | + |
| 50 | + match chain_config { |
| 51 | + Some(chain_config) => { |
| 52 | + let opts = InitOptions { |
| 53 | + chain_id: *chain_id, |
| 54 | + trusted_hash: *trusted_hash, |
| 55 | + trusted_height, |
| 56 | + }; |
| 57 | + |
| 58 | + Ok((chain_config.clone(), opts)) |
| 59 | + } |
| 60 | + None => Err(format!("cannot find chain {} in config", chain_id)), |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + (None, _, _) => Err("missing chain identifier".to_string()), |
| 65 | + (_, None, _) => Err("missing trusted hash".to_string()), |
| 66 | + (_, _, None) => Err("missing trusted height".to_string()), |
| 67 | + } |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +impl Runnable for InitCmd { |
| 72 | + /// Initialize the light client for the given chain |
| 73 | + fn run(&self) { |
| 74 | + // FIXME: This just hangs and never runs the given future |
| 75 | + // abscissa_tokio::run(&APPLICATION, ...).unwrap(); |
| 76 | + |
| 77 | + let config = app_config(); |
| 78 | + |
| 79 | + let (chain_config, opts) = match self.get_chain_config_and_options(&config) { |
| 80 | + Err(err) => { |
| 81 | + status_err!("invalid options: {}", err); |
| 82 | + return; |
| 83 | + } |
| 84 | + Ok(result) => result, |
| 85 | + }; |
| 86 | + |
| 87 | + block_on(async { |
| 88 | + let trust_options = TrustOptions::new( |
| 89 | + opts.trusted_hash, |
| 90 | + opts.trusted_height, |
| 91 | + chain_config.trusting_period, |
| 92 | + Default::default(), |
| 93 | + ) |
| 94 | + .unwrap(); |
| 95 | + |
| 96 | + let mut store: SledStore<TendermintChain> = |
| 97 | + relayer::store::persistent(format!("store_{}.db", chain_config.id)); |
| 98 | + |
| 99 | + store.set_trust_options(trust_options).unwrap(); // FIXME: unwrap |
| 100 | + |
| 101 | + status_ok!( |
| 102 | + chain_config.id, |
| 103 | + "Set trusted options: hash={} height={}", |
| 104 | + opts.trusted_hash, |
| 105 | + opts.trusted_height |
| 106 | + ); |
| 107 | + }); |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +fn block_on<F: Future>(future: F) -> F::Output { |
| 112 | + tokio::runtime::Builder::new() |
| 113 | + .basic_scheduler() |
| 114 | + .enable_all() |
| 115 | + .build() |
| 116 | + .unwrap() |
| 117 | + .block_on(future) |
| 118 | +} |
0 commit comments