Skip to content

Commit

Permalink
ChainEndpoint: Use default implementations from CosmosSdkChain (#2807)
Browse files Browse the repository at this point in the history
* ChainEndpoint: Use default implementations from CosmosSdkChain

Fixes #2806.

This requires changing config() to return a &ChainConfig instead of a
ChainConfig, so the one call site using config() is also updated.

* Fix changelog link

Co-authored-by: Adi Seredinschi <[email protected]>
Signed-off-by: Kevin Ji <[email protected]>

* Undo `init_event_monitor` change

Signed-off-by: Kevin Ji <[email protected]>
Co-authored-by: Sean Chen <[email protected]>
Co-authored-by: Adi Seredinschi <[email protected]>
Co-authored-by: Romain Ruetschi <[email protected]>
  • Loading branch information
4 people authored Nov 15, 2022
1 parent 5f7e6c7 commit 027578f
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 54 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
- Move default implementations of `init_event_monitor`, `id`, `get_key`,
and `add_key` from CosmosSdkChain to ChainEndpoint, and change
`ChainEndpoint::config()` to return a `&ChainConfig` instead of
a `ChainConfig`. ([#2806](https://github.com/informalsystems/ibc-rs/issues/2806))
66 changes: 17 additions & 49 deletions crates/relayer/src/chain/cosmos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ use core::{
time::Duration,
};
use num_bigint::BigInt;
use std::cmp::Ordering;
use std::thread;
use std::{cmp::Ordering, thread};

use ibc_proto::protobuf::Protobuf;
use tendermint::block::Height as TmHeight;
Expand All @@ -23,15 +22,21 @@ use tonic::{codegen::http::Uri, metadata::AsciiMetadataValue};
use tracing::{error, instrument, trace, warn};

use ibc_proto::cosmos::staking::v1beta1::Params as StakingParams;
use ibc_relayer_types::clients::ics07_tendermint::client_state::{
AllowUpdate, ClientState as TmClientState,
};
use ibc_relayer_types::clients::ics07_tendermint::consensus_state::ConsensusState as TMConsensusState;
use ibc_relayer_types::clients::ics07_tendermint::header::Header as TmHeader;
use ibc_relayer_types::core::ics02_client::client_type::ClientType;
use ibc_relayer_types::core::ics02_client::error::Error as ClientError;
use ibc_relayer_types::core::ics02_client::events::UpdateClient;
use ibc_relayer_types::core::ics03_connection::connection::{
ConnectionEnd, IdentifiedConnectionEnd,
};
use ibc_relayer_types::core::ics04_channel::channel::{ChannelEnd, IdentifiedChannelEnd};
use ibc_relayer_types::core::ics04_channel::packet::Sequence;
use ibc_relayer_types::core::ics23_commitment::commitment::CommitmentPrefix;
use ibc_relayer_types::core::ics23_commitment::merkle::MerkleProof;
use ibc_relayer_types::core::ics24_host::identifier::{
ChainId, ChannelId, ClientId, ConnectionId, PortId,
};
Expand All @@ -44,19 +49,15 @@ use ibc_relayer_types::core::ics24_host::{
};
use ibc_relayer_types::signer::Signer;
use ibc_relayer_types::Height as ICSHeight;
use ibc_relayer_types::{
clients::ics07_tendermint::client_state::{AllowUpdate, ClientState as TmClientState},
core::ics23_commitment::merkle::MerkleProof,
};
use ibc_relayer_types::{
clients::ics07_tendermint::consensus_state::ConsensusState as TMConsensusState,
core::ics02_client::events::UpdateClient,
};

use crate::account::Balance;
use crate::chain::client::ClientSettings;
use crate::chain::cosmos::batch::sequential_send_batched_messages_and_wait_commit;
use crate::chain::cosmos::batch::{
send_batched_messages_and_wait_check_tx, send_batched_messages_and_wait_commit,
};
use crate::chain::cosmos::encode::key_entry_to_signer;
use crate::chain::cosmos::fee::maybe_register_counterparty_payee;
use crate::chain::cosmos::gas::{calculate_fee, mul_ceil};
use crate::chain::cosmos::query::account::get_or_fetch_account;
use crate::chain::cosmos::query::balance::{query_all_balances, query_balance};
use crate::chain::cosmos::query::denom_trace::query_denom_trace;
Expand All @@ -70,10 +71,6 @@ use crate::chain::cosmos::types::config::TxConfig;
use crate::chain::cosmos::types::gas::{
default_gas_from_config, gas_multiplier_from_config, max_gas_from_config,
};
use crate::chain::cosmos::{
batch::sequential_send_batched_messages_and_wait_commit,
gas::{calculate_fee, mul_ceil},
};
use crate::chain::endpoint::{ChainEndpoint, ChainStatus, HealthCheck};
use crate::chain::requests::{Qualified, QueryPacketEventDataRequest};
use crate::chain::tracking::TrackedMsgs;
Expand All @@ -82,19 +79,15 @@ use crate::config::ChainConfig;
use crate::consensus_state::{AnyConsensusState, AnyConsensusStateWithHeight};
use crate::denom::DenomTrace;
use crate::error::Error;
use crate::event::monitor::{EventMonitor, EventReceiver, TxMonitorCmd};
use crate::event::monitor::{EventReceiver, TxMonitorCmd};
use crate::event::IbcEventWithHeight;
use crate::keyring::{KeyEntry, KeyRing};
use crate::light_client::tendermint::LightClient as TmLightClient;
use crate::light_client::{LightClient, Verified};
use crate::misbehaviour::MisbehaviourEvidence;
use crate::util::pretty::{PrettyConsensusStateWithHeight, PrettyIdentifiedChannel};
use crate::{
chain::cosmos::batch::{
send_batched_messages_and_wait_check_tx, send_batched_messages_and_wait_commit,
},
util::pretty::{PrettyIdentifiedClientState, PrettyIdentifiedConnection},
};
use crate::util::pretty::{PrettyIdentifiedClientState, PrettyIdentifiedConnection};
use crate::{account::Balance, event::monitor::EventMonitor};

use super::requests::{
IncludeProof, QueryChannelClientStateRequest, QueryChannelRequest, QueryChannelsRequest,
Expand Down Expand Up @@ -682,10 +675,6 @@ impl ChainEndpoint for CosmosSdkChain {
Ok(())
}

fn id(&self) -> &ChainId {
&self.config().id
}

fn keybase(&self) -> &KeyRing {
&self.keybase
}
Expand Down Expand Up @@ -788,29 +777,8 @@ impl ChainEndpoint for CosmosSdkChain {
}

/// Get the chain configuration
fn config(&self) -> ChainConfig {
self.config.clone()
}

/// Get the signing key
fn get_key(&mut self) -> Result<KeyEntry, Error> {
crate::time!("get_key");

// Get the key from key seed file
let key = self
.keybase()
.get_key(&self.config.key_name)
.map_err(|e| Error::key_not_found(self.config.key_name.clone(), e))?;

Ok(key)
}

fn add_key(&mut self, key_name: &str, key: KeyEntry) -> Result<(), Error> {
self.keybase_mut()
.add_key(key_name, key)
.map_err(Error::key_base)?;

Ok(())
fn config(&self) -> &ChainConfig {
&self.config
}

fn ibc_version(&self) -> Result<Option<semver::Version>, Error> {
Expand Down
27 changes: 23 additions & 4 deletions crates/relayer/src/chain/endpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,12 @@ pub trait ChainEndpoint: Sized {
type ClientState: ClientState + Into<AnyClientState>;

/// Returns the chain's identifier
fn id(&self) -> &ChainId;
fn id(&self) -> &ChainId {
&self.config().id
}

/// Returns the chain configuration
fn config(&self) -> ChainConfig;
fn config(&self) -> &ChainConfig;

// Life cycle

Expand Down Expand Up @@ -109,9 +111,26 @@ pub trait ChainEndpoint: Sized {

fn get_signer(&self) -> Result<Signer, Error>;

fn get_key(&mut self) -> Result<KeyEntry, Error>;
/// Get the signing key
fn get_key(&mut self) -> Result<KeyEntry, Error> {
crate::time!("get_key");

// Get the key from key seed file
let key = self
.keybase()
.get_key(&self.config().key_name)
.map_err(|e| Error::key_not_found(self.config().key_name.clone(), e))?;

Ok(key)
}

fn add_key(&mut self, key_name: &str, key: KeyEntry) -> Result<(), Error> {
self.keybase_mut()
.add_key(key_name, key)
.map_err(Error::key_base)?;

fn add_key(&mut self, key_name: &str, key: KeyEntry) -> Result<(), Error>;
Ok(())
}

// Versioning

Expand Down
2 changes: 1 addition & 1 deletion crates/relayer/src/chain/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,7 @@ where
}

fn get_config(&self, reply_to: ReplyTo<ChainConfig>) -> Result<(), Error> {
let result = Ok(self.chain.config());
let result = Ok(self.chain.config().clone());
reply_to.send(result).map_err(Error::send)
}

Expand Down

0 comments on commit 027578f

Please sign in to comment.