Skip to content

Commit

Permalink
Fix build for latest tendermint-rs master (informalsystems#299)
Browse files Browse the repository at this point in the history
  • Loading branch information
romac committed Oct 9, 2020
1 parent e889ff3 commit f4d226d
Show file tree
Hide file tree
Showing 11 changed files with 67 additions and 40 deletions.
6 changes: 3 additions & 3 deletions relayer-cli/src/commands/light/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ struct InitOptions {

impl InitOptions {
fn from_init_cmd(cmd: &InitCmd) -> Result<InitOptions, &'static str> {
match (cmd.chain_id, cmd.hash, cmd.height) {
match (cmd.chain_id.clone(), cmd.hash, cmd.height) {
(Some(chain_id), Some(hash), Some(height)) => Ok(Self {
chain_id,
trusted_hash: hash,
Expand Down Expand Up @@ -72,11 +72,11 @@ impl InitCmd {
)?;

let mut config = LightConfig::load(LIGHT_CONFIG_PATH)?;
config.chains.insert(chain_config.id, trust_options);
config.chains.insert(chain_config.id.clone(), trust_options);
config.save(LIGHT_CONFIG_PATH)?;

status_ok!(
chain_config.id,
chain_config.id.clone(),
"Set trusted options: hash={} height={}",
options.trusted_hash,
options.trusted_height
Expand Down
5 changes: 4 additions & 1 deletion relayer-cli/src/commands/query/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ use relayer::chain::{Chain, CosmosSDKChain};
use tendermint::chain::Id as ChainId;
use tendermint_proto::DomainType;

use std::convert::TryInto;

#[derive(Clone, Command, Debug, Options)]
pub struct QueryChannelEndCmd {
#[options(free, help = "identifier of the chain to query")]
Expand Down Expand Up @@ -46,6 +48,7 @@ impl QueryChannelEndCmd {
) -> Result<(ChainConfig, QueryChannelOptions), String> {
let chain_id = self
.chain_id
.clone()
.ok_or_else(|| "missing chain identifier".to_string())?;
let chain_config = config
.chains
Expand Down Expand Up @@ -102,7 +105,7 @@ impl Runnable for QueryChannelEndCmd {
let res: Result<ChannelEnd, Error> = chain
.query(
ChannelEnds(opts.port_id, opts.channel_id),
opts.height,
opts.height.try_into().unwrap(),
opts.proof,
)
.map_err(|e| Kind::Query.context(e).into())
Expand Down
25 changes: 19 additions & 6 deletions relayer-cli/src/commands/query/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ use relayer::chain::CosmosSDKChain;
use tendermint::chain::Id as ChainId;
use tendermint_proto::DomainType;

use std::convert::TryInto;

/// Query client state command
#[derive(Clone, Command, Debug, Options)]
pub struct QueryClientStateCmd {
Expand Down Expand Up @@ -82,7 +84,11 @@ impl Runnable for QueryClientStateCmd {
let chain = CosmosSDKChain::from_config(chain_config).unwrap();

let res: Result<AnyClientState, Error> = chain
.query(ClientState(opts.client_id), opts.height, opts.proof)
.query(
ClientState(opts.client_id),
opts.height.try_into().unwrap(),
opts.proof,
)
.map_err(|e| Kind::Query.context(e).into())
.and_then(|v| {
AnyClientState::decode_vec(&v).map_err(|e| Kind::Query.context(e).into())
Expand Down Expand Up @@ -184,7 +190,7 @@ impl Runnable for QueryClientConsensusCmd {
epoch: opts.consensus_epoch,
height: opts.consensus_height,
},
opts.height,
opts.height.try_into().unwrap(),
opts.proof,
)
.map_err(|e| Kind::Query.context(e).into())
Expand All @@ -204,7 +210,9 @@ fn validate_common_options(
client_id: &Option<String>,
config: &Config,
) -> Result<(ChainConfig, ClientId), String> {
let chain_id = chain_id.ok_or_else(|| "missing chain parameter".to_string())?;
let chain_id = chain_id
.clone()
.ok_or_else(|| "missing chain parameter".to_string())?;
let chain_config = config
.chains
.iter()
Expand Down Expand Up @@ -250,6 +258,7 @@ impl QueryClientConnectionsCmd {
) -> Result<(ChainConfig, QueryClientConnectionsOptions), String> {
let chain_id = self
.chain_id
.clone()
.ok_or_else(|| "missing chain identifier".to_string())?;
let chain_config = config
.chains
Expand Down Expand Up @@ -297,7 +306,11 @@ impl Runnable for QueryClientConnectionsCmd {

let chain = CosmosSDKChain::from_config(chain_config).unwrap();
let res: Result<ConnectionIDs, Error> = chain
.query(ClientConnections(opts.client_id), opts.height, opts.proof)
.query(
ClientConnections(opts.client_id),
opts.height.try_into().unwrap(),
opts.proof,
)
.map_err(|e| Kind::Query.context(e).into())
.and_then(|v| ConnectionIDs::decode_vec(&v).map_err(|e| Kind::Query.context(e).into()));
match res {
Expand Down Expand Up @@ -354,7 +367,7 @@ mod tests {
name: "No client id specified".to_string(),
params: QueryClientStateCmd {
client_id: None,
..default_params
..default_params.clone()
},
want_pass: false,
},
Expand Down Expand Up @@ -440,7 +453,7 @@ mod tests {
name: "No client id specified".to_string(),
params: QueryClientConnectionsCmd {
client_id: None,
..default_params
..default_params.clone()
},
want_pass: false,
},
Expand Down
14 changes: 10 additions & 4 deletions relayer-cli/src/commands/query/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ use abscissa_core::{Command, Options, Runnable};
use relayer::config::{ChainConfig, Config};

use crate::error::{Error, Kind};
use ibc::ics03_connection::connection::ConnectionEnd;
use ibc::ics24_host::error::ValidationError;
use ibc::ics24_host::identifier::ConnectionId;
use ibc::ics24_host::Path::Connections;
use relayer::chain::{Chain, CosmosSDKChain};
use tendermint::chain::Id as ChainId;
use tendermint_proto::DomainType;

use ibc::ics03_connection::connection::ConnectionEnd;
use std::convert::TryInto;

#[derive(Clone, Command, Debug, Options)]
pub struct QueryConnectionEndCmd {
Expand Down Expand Up @@ -42,6 +43,7 @@ impl QueryConnectionEndCmd {
) -> Result<(ChainConfig, QueryConnectionOptions), String> {
let chain_id = self
.chain_id
.clone()
.ok_or_else(|| "missing chain identifier".to_string())?;
let chain_config = config
.chains
Expand Down Expand Up @@ -88,7 +90,11 @@ impl Runnable for QueryConnectionEndCmd {
// run without proof:
// cargo run --bin relayer -- -c relayer/tests/config/fixtures/simple_config.toml query connection end ibc-test connectionidone --height 3 -p false
let res: Result<ConnectionEnd, Error> = chain
.query(Connections(opts.connection_id), opts.height, opts.proof)
.query(
Connections(opts.connection_id),
opts.height.try_into().unwrap(),
opts.proof,
)
.map_err(|e| Kind::Query.context(e).into())
.and_then(|v| ConnectionEnd::decode_vec(&v).map_err(|e| Kind::Query.context(e).into()));

Expand Down Expand Up @@ -145,15 +151,15 @@ mod tests {
name: "No connection id specified".to_string(),
params: QueryConnectionEndCmd {
connection_id: None,
..default_params
..default_params.clone()
},
want_pass: false,
},
Test {
name: "Bad connection, non-alpha".to_string(),
params: QueryConnectionEndCmd {
connection_id: Some("conn01".to_string()),
..default_params
..default_params.clone()
},
want_pass: false,
},
Expand Down
4 changes: 2 additions & 2 deletions relayer-cli/src/commands/start.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ async fn create_client_task(
let supervisor = create_client(chain, trust_options, reset).await?;
let handle = supervisor.handle();

let task = client_task(*chain.id(), supervisor);
let task = client_task(chain.id().clone(), supervisor);

let light_client = client::tendermint::LightClient::new(handle);
chain.set_light_client(light_client);
Expand Down Expand Up @@ -176,7 +176,7 @@ async fn create_client(
reset: bool,
) -> Result<Supervisor, BoxError> {
let chain_config = chain.config();
let id = chain_config.id;
let id = chain_config.id.clone();

let db_path = format!("store_{}.db", chain.id());
let store = store::sled::SledStore::new(sled::open(db_path)?);
Expand Down
11 changes: 6 additions & 5 deletions relayer-cli/tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,17 @@ use ibc::ics24_host::identifier::{ChannelId, ClientId, ConnectionId, PortId};
use ibc::ics24_host::Path::{ChannelEnds, ClientConnections};
use relayer::chain::{Chain, CosmosSDKChain};
use relayer::config::{ChainConfig, Config};
use std::str::FromStr;
use tendermint::chain::Id;
use tendermint::net::Address;
use tendermint_proto::DomainType;

use std::convert::TryInto;
use std::str::FromStr;

/// Configuration that connects to the informaldev/simd DockerHub image running on localhost.
fn simd_config() -> Config {
let mut config = Config::default();
config.chains = vec![ChainConfig {
id: Id::from("ibc-test"),
id: "ibc-test".try_into().unwrap(),
rpc_addr: Address::from_str("127.0.0.1:26657").unwrap(),
account_prefix: "cosmos".to_string(),
key_name: "testkey".to_string(),
Expand Down Expand Up @@ -84,7 +85,7 @@ fn query_channel_id() {
PortId::from_str("firstport").unwrap(),
ChannelId::from_str("firstchannel").unwrap(),
),
0,
0_u64.try_into().unwrap(),
false,
)
.unwrap(),
Expand All @@ -108,7 +109,7 @@ fn query_client_id() {
&chain
.query(
ClientConnections(ClientId::from_str("clientidone").unwrap()),
0,
0_u64.try_into().unwrap(),
false,
)
.unwrap(),
Expand Down
2 changes: 1 addition & 1 deletion relayer/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pub trait Chain {
type Error: Into<Box<dyn Error + Send + Sync + 'static>>;

/// Perform a generic `query`, and return the corresponding response data.
fn query(&self, data: Path, height: u64, prove: bool) -> Result<Vec<u8>, Self::Error>;
fn query(&self, data: Path, height: Height, prove: bool) -> Result<Vec<u8>, Self::Error>;

/// send a transaction with `msgs` to chain.
fn send(&self, _msgs: &[Any]) -> Result<(), Self::Error>;
Expand Down
22 changes: 11 additions & 11 deletions relayer/src/chain/cosmos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,15 @@ impl Chain for CosmosSDKChain {
type ClientState = ClientState;
type Error = Error;

fn query(&self, data: Path, height: u64, prove: bool) -> Result<Vec<u8>, Self::Error> {
fn query(&self, data: Path, height: Height, prove: bool) -> Result<Vec<u8>, Self::Error> {
let path = TendermintABCIPath::from_str(IBC_QUERY_PATH).unwrap();

if !data.is_provable() & prove {
return Err(Kind::Store
.context("requested proof for a path in the privateStore")
.into());
}

let response = block_on(abci_query(&self, path, data.to_string(), height, prove))?;

// Verify response proof, if requested.
Expand Down Expand Up @@ -219,21 +221,19 @@ async fn abci_query(
chain: &CosmosSDKChain,
path: TendermintABCIPath,
data: String,
height: u64,
height: Height,
prove: bool,
) -> Result<Vec<u8>, anomaly::Error<Kind>> {
let height = if height.value() == 0 {
None
} else {
Some(height)
};

// Use the Tendermint-rs RPC client to do the query.
let response = chain
.rpc_client()
.abci_query(
Some(path),
data.into_bytes(),
match height {
0 => None,
_ => Some(Height::from(height)),
},
prove,
)
.abci_query(Some(path), data.into_bytes(), height, prove)
.await
.map_err(|e| Kind::Rpc.context(e))?;

Expand Down
2 changes: 1 addition & 1 deletion relayer/src/event_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ impl EventHandler {
loop {
if let Some(events) = self.channel_from_monitors.recv().await {
for event in events.1 {
self.handle(events.0, event);
self.handle(events.0.clone(), event);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion relayer/src/event_monitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ impl EventMonitor {
if let Ok(ibc_events) = ibc::events::get_all_events(event) {
// TODO - send_timeout()?
self.channel_to_handler
.send((self.chain_id, ibc_events))
.send((self.chain_id.clone(), ibc_events))
.await?;
}
}
Expand Down
14 changes: 9 additions & 5 deletions relayer/src/tx/client.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use prost_types::Any;
use std::convert::TryInto;
use std::time::Duration;

use ibc::ics02_client::client_def::{AnyClientState, AnyConsensusState};
Expand Down Expand Up @@ -27,10 +28,13 @@ pub fn create_client(opts: CreateClientOptions) -> Result<(), Error> {
let dest_chain = CosmosSDKChain::from_config(opts.clone().dest_chain_config)?;

// Query the client state on destination chain.
if dest_chain
.query(ClientStatePath(opts.clone().dest_client_id), 0, false)
.is_ok()
{
let response = dest_chain.query(
ClientStatePath(opts.clone().dest_client_id),
0_u64.try_into().unwrap(),
false,
);

if response.is_ok() {
return Err(Into::<Error>::into(Kind::CreateClient(
opts.dest_client_id,
"client already exists".into(),
Expand Down Expand Up @@ -63,7 +67,7 @@ pub fn create_client(opts: CreateClientOptions) -> Result<(), Error> {
src_chain.unbonding_period(),
Duration::from_millis(3000),
height,
Height(0),
0_u64.try_into().unwrap(),
false,
false,
)
Expand Down

0 comments on commit f4d226d

Please sign in to comment.