Skip to content

Commit

Permalink
Override the light client store with the in-memory one in all command…
Browse files Browse the repository at this point in the history
…s except the relayer loop (informalsystems#562)

* Formatting

* Override the light client store with the in-memory one in all commands except the relayer loop

* Update changelog

* Cleanup
  • Loading branch information
romac authored Jan 27, 2021
1 parent da406c3 commit d301e44
Show file tree
Hide file tree
Showing 19 changed files with 286 additions and 137 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
- Replace `ChannelConfig` in `Channel::new` ([#511])
- Add `packet-send` CLI ([#470])
- UX improvements for relayer txs ([#536, #540, #554])
- Allow running standalone commands concurrently to the main relayer loop ([#501])

- [relayer]
- Performance improvements ([#514], [#537])
Expand All @@ -49,6 +50,7 @@
[#306]: https://github.com/informalsystems/ibc-rs/issues/306
[#470]: https://github.com/informalsystems/ibc-rs/issues/470
[#500]: https://github.com/informalsystems/ibc-rs/issues/500
[#501]: https://github.com/informalsystems/ibc-rs/issues/501
[#505]: https://github.com/informalsystems/ibc-rs/issues/505
[#507]: https://github.com/informalsystems/ibc-rs/issues/507
[#511]: https://github.com/informalsystems/ibc-rs/pull/511
Expand Down
23 changes: 14 additions & 9 deletions relayer-cli/src/commands/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ use abscissa_core::{Command, Help, Options, Runnable};

use ibc::ics04_channel::channel::Order;
use ibc::ics24_host::identifier::{ChainId, PortId};
use relayer::config::{RelayPath, StoreConfig};
use relayer::relay::connect_with_new_channel;

use crate::application::app_config;
use crate::commands::cli_utils::chain_handlers_from_chain_id;
use crate::commands::cli_utils::{ChainHandlePair, SpawnOptions};
use crate::conclude::Output;
use relayer::config::RelayPath;

/// `channel` subcommand
#[derive(Command, Debug, Options, Runnable)]
Expand Down Expand Up @@ -44,13 +44,18 @@ impl Runnable for ChannelHandshakeCommand {
fn run(&self) {
let config = app_config();

let chains =
match chain_handlers_from_chain_id(&config, &self.src_chain_id, &self.dst_chain_id) {
Ok(chains) => chains,
Err(e) => {
return Output::error(format!("{}", e)).exit();
}
};
let spawn_options = SpawnOptions::override_store_config(StoreConfig::memory());
let chains = match ChainHandlePair::spawn_with(
spawn_options,
&config,
&self.src_chain_id,
&self.dst_chain_id,
) {
Ok(chains) => chains,
Err(e) => {
return Output::error(format!("{}", e)).exit();
}
};

let res = connect_with_new_channel(
chains.src,
Expand Down
110 changes: 86 additions & 24 deletions relayer-cli/src/commands/cli_utils.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use abscissa_core::config;

use ibc::ics24_host::identifier::ChainId;
use relayer::chain::handle::ChainHandle;
use relayer::chain::runtime::ChainRuntime;
use relayer::chain::CosmosSDKChain;
use relayer::{chain::handle::ChainHandle, config::StoreConfig};
use relayer::{chain::runtime::ChainRuntime, config::ChainConfig};

use crate::application::CliApp;
use crate::error::{Error, Kind};
Expand All @@ -16,44 +16,106 @@ pub struct ChainHandlePair {
pub dst: Box<dyn ChainHandle>,
}

/// Create the source and destination chain handlers from the configuration and chain identifiers
pub fn chain_handlers_from_chain_id(
impl ChainHandlePair {
/// Spawn the source and destination chain runtime from the configuration and chain identifiers,
/// and return the pair of associated handles.
pub fn spawn(
config: &config::Reader<CliApp>,
src_chain_id: &ChainId,
dst_chain_id: &ChainId,
) -> Result<Self, Error> {
Self::spawn_with(Default::default(), config, src_chain_id, dst_chain_id)
}

/// Spawn the source and destination chain runtime from the configuration and chain identifiers,
/// and return the pair of associated handles. Accepts a `SpawnOptions` argument, which
/// is used to override each chain configuration before spawning its runtime.
pub fn spawn_with(
options: SpawnOptions,
config: &config::Reader<CliApp>,
src_chain_id: &ChainId,
dst_chain_id: &ChainId,
) -> Result<Self, Error> {
spawn_chain_runtimes(options, config, src_chain_id, dst_chain_id)
}
}

/// Spawn the source and destination chain runtime from the configuration and chain identifiers,
/// and return the pair of associated handles.
fn spawn_chain_runtimes(
spawn_options: SpawnOptions,
config: &config::Reader<CliApp>,
src_chain_id: &ChainId,
dst_chain_id: &ChainId,
) -> Result<ChainHandlePair, Error> {
let src_config = config
.find_chain(src_chain_id)
.cloned()
.ok_or_else(|| "missing source chain in configuration file".to_string());

let dst_config = config
.find_chain(dst_chain_id)
.cloned()
.ok_or_else(|| "missing destination chain configuration file".to_string());

let (src_chain_config, dst_chain_config) = match (src_config, dst_config) {
(Ok(s), Ok(d)) => (s, d),
(Err(e), _) | (_, Err(e)) => {
return Err(Kind::Config.context(e).into());
}
};
let (mut src_chain_config, mut dst_chain_config) =
zip_result(src_config, dst_config).map_err(|e| Kind::Config.context(e))?;

spawn_options.apply(&mut src_chain_config);
spawn_options.apply(&mut dst_chain_config);

let src_chain_res = ChainRuntime::<CosmosSDKChain>::spawn(src_chain_config.clone())
let src_chain_res = ChainRuntime::<CosmosSDKChain>::spawn(src_chain_config)
.map_err(|e| Kind::Runtime.context(e));
let src = match src_chain_res {
Ok((handle, _)) => handle,
Err(e) => {
return Err(e.into());
}
};

let dst_chain_res = ChainRuntime::<CosmosSDKChain>::spawn(dst_chain_config.clone())
let src = src_chain_res.map(|(handle, _)| handle)?;

let dst_chain_res = ChainRuntime::<CosmosSDKChain>::spawn(dst_chain_config)
.map_err(|e| Kind::Runtime.context(e));
let dst = match dst_chain_res {
Ok((handle, _)) => handle,
Err(e) => {
return Err(e.into());
}
};

let dst = dst_chain_res.map(|(handle, _)| handle)?;

Ok(ChainHandlePair { src, dst })
}

/// Allows override the chain configuration just before
/// spawning a new runtime instance.
///
/// This is currently only used to override the configured
/// light client store for one-off commands which do not
/// need the disk-based store.
#[derive(Clone, Debug, Default)]
pub struct SpawnOptions {
override_store_config: Option<StoreConfig>,
}

impl SpawnOptions {
/// Override the light client store config with the provided config.
pub fn override_store_config(store_config: StoreConfig) -> Self {
Self {
override_store_config: Some(store_config),
}
}

/// Apply these options to the provided chain configuration.
pub fn apply(&self, chain_config: &mut ChainConfig) {
if let Some(store_config) = &self.override_store_config {
Self::apply_store_config(chain_config, &store_config)
}
}

fn apply_store_config(chain_config: &mut ChainConfig, store_config: &StoreConfig) {
if let Some(peer_config) = chain_config.peers.as_mut() {
for light_client in &mut peer_config.light_clients {
light_client.store = store_config.clone();
}
}
}
}

/// Zip two results together.
fn zip_result<A, B, E>(a: Result<A, E>, b: Result<B, E>) -> Result<(A, B), E> {
match (a, b) {
(Ok(a), Ok(b)) => Ok((a, b)),
(Err(e), _) | (_, Err(e)) => Err(e),
}
}
4 changes: 1 addition & 3 deletions relayer-cli/src/commands/keys/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,7 @@ impl Runnable for KeysAddCmd {
let config = app_config();

let opts = match self.validate_options(&config) {
Err(err) => {
return Output::error(err).exit();
}
Err(err) => return Output::error(err).exit(),
Ok(result) => result,
};

Expand Down
4 changes: 1 addition & 3 deletions relayer-cli/src/commands/keys/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@ impl Runnable for KeysListCmd {
let config = app_config();

let opts = match self.validate_options(&config) {
Err(err) => {
return Output::error(err).exit();
}
Err(err) => return Output::error(err).exit(),
Ok(result) => result,
};

Expand Down
4 changes: 1 addition & 3 deletions relayer-cli/src/commands/keys/restore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,7 @@ impl Runnable for KeyRestoreCmd {
let config = app_config();

let opts = match self.validate_options(&config) {
Err(err) => {
return Output::error(err).exit();
}
Err(err) => return Output::error(err).exit(),
Ok(result) => result,
};

Expand Down
4 changes: 1 addition & 3 deletions relayer-cli/src/commands/query/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,7 @@ impl Runnable for QueryChannelEndCmd {
let config = app_config();

let (chain_config, opts) = match self.validate_options(&config) {
Err(err) => {
return Output::error(err).exit();
}
Err(err) => return Output::error(err).exit(),
Ok(result) => result,
};
info!("Options {:?}", opts);
Expand Down
12 changes: 3 additions & 9 deletions relayer-cli/src/commands/query/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,7 @@ impl Runnable for QueryClientStateCmd {
let config = app_config();

let (chain_config, opts) = match self.validate_options(&config) {
Err(err) => {
return Output::error(err).exit();
}
Err(err) => return Output::error(err).exit(),
Ok(result) => result,
};
info!("Options {:?}", opts);
Expand Down Expand Up @@ -162,9 +160,7 @@ impl Runnable for QueryClientConsensusCmd {
let config = app_config();

let (chain_config, opts) = match self.validate_options(&config) {
Err(err) => {
return Output::error(err).exit();
}
Err(err) => return Output::error(err).exit(),
Ok(result) => result,
};
info!("Options {:?}", opts);
Expand Down Expand Up @@ -271,9 +267,7 @@ impl Runnable for QueryClientConnectionsCmd {
let config = app_config();

let (chain_config, opts) = match self.validate_options(&config) {
Err(err) => {
return Output::error(err).exit();
}
Err(err) => return Output::error(err).exit(),
Ok(result) => result,
};
info!("Options {:?}", opts);
Expand Down
8 changes: 2 additions & 6 deletions relayer-cli/src/commands/query/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,7 @@ impl Runnable for QueryConnectionEndCmd {
let config = app_config();

let (chain_config, opts) = match self.validate_options(&config) {
Err(err) => {
return Output::error(err).exit();
}
Err(err) => return Output::error(err).exit(),
Ok(result) => result,
};
info!("Options {:?}", opts);
Expand Down Expand Up @@ -144,9 +142,7 @@ impl Runnable for QueryConnectionChannelsCmd {
let config = app_config();

let (chain_config, opts) = match self.validate_options(&config) {
Err(err) => {
return Output::error(err).exit();
}
Err(err) => return Output::error(err).exit(),
Ok(result) => result,
};
info!("Options {:?}", opts);
Expand Down
24 changes: 6 additions & 18 deletions relayer-cli/src/commands/query/packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,7 @@ impl Runnable for QueryPacketCommitmentsCmd {
let config = app_config();

let (chain_config, opts) = match self.validate_options(&config) {
Err(err) => {
return Output::error(err).exit();
}
Err(err) => return Output::error(err).exit(),
Ok(result) => result,
};
info!("Options {:?}", opts);
Expand Down Expand Up @@ -133,9 +131,7 @@ impl Runnable for QueryPacketCommitmentCmd {
let config = app_config();

let (chain_config, opts, sequence) = match self.validate_options(&config) {
Err(err) => {
return Output::error(err).exit();
}
Err(err) => return Output::error(err).exit(),
Ok(result) => result,
};
info!("Options {:?}", opts);
Expand Down Expand Up @@ -223,9 +219,7 @@ impl Runnable for QueryUnreceivedPacketsCmd {
let config = app_config();

let (dst_chain_config, src_chain_config, opts) = match self.validate_options(&config) {
Err(err) => {
return Output::error(err).exit();
}
Err(err) => return Output::error(err).exit(),
Ok(result) => result,
};
info!("Options {:?}", opts);
Expand Down Expand Up @@ -357,9 +351,7 @@ impl Runnable for QueryPacketAcknowledgementsCmd {
let config = app_config();

let (chain_config, opts) = match self.validate_options(&config) {
Err(err) => {
return Output::error(err).exit();
}
Err(err) => return Output::error(err).exit(),
Ok(result) => result,
};
info!("Options {:?}", opts);
Expand Down Expand Up @@ -431,9 +423,7 @@ impl Runnable for QueryPacketAcknowledgmentCmd {
let config = app_config();

let (chain_config, opts, sequence) = match self.validate_options(&config) {
Err(err) => {
return Output::error(err).exit();
}
Err(err) => return Output::error(err).exit(),
Ok(result) => result,
};
info!("Options {:?}", opts);
Expand Down Expand Up @@ -521,9 +511,7 @@ impl Runnable for QueryUnreceivedAcknowledgementCmd {
let config = app_config();

let (dst_chain_config, src_chain_config, opts) = match self.validate_options(&config) {
Err(err) => {
return Output::error(err).exit();
}
Err(err) => return Output::error(err).exit(),
Ok(result) => result,
};
info!("Options {:?}", opts);
Expand Down
17 changes: 7 additions & 10 deletions relayer-cli/src/commands/start.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use abscissa_core::{Command, Options, Runnable};

use ibc::ics04_channel::channel::Order;
use ibc::ics24_host::identifier::{ChainId, ChannelId, PortId};
use relayer::link::LinkParameters;
use relayer::relay::{channel_relay, relay_on_new_link};

use crate::commands::cli_utils::chain_handlers_from_chain_id;
use crate::commands::cli_utils::ChainHandlePair;
use crate::conclude::Output;
use crate::prelude::*;
use ibc::ics24_host::identifier::{ChainId, ChannelId, PortId};
use relayer::link::LinkParameters;

#[derive(Clone, Command, Debug, Options)]
pub struct StartCmd {
Expand All @@ -28,13 +28,10 @@ impl Runnable for StartCmd {
fn run(&self) {
let config = app_config();

let chains =
match chain_handlers_from_chain_id(&config, &self.src_chain_id, &self.dst_chain_id) {
Ok(chains) => chains,
Err(e) => {
return Output::error(format!("{}", e)).exit();
}
};
let chains = match ChainHandlePair::spawn(&config, &self.src_chain_id, &self.dst_chain_id) {
Ok(chains) => chains,
Err(e) => return Output::error(format!("{}", e)).exit(),
};

match (&self.src_port_id, &self.src_channel_id) {
(Some(src_port_id), Some(src_channel_id)) => {
Expand Down
Loading

0 comments on commit d301e44

Please sign in to comment.