Skip to content

Commit

Permalink
Moved query parameters into the command parameters with Into. (#155)
Browse files Browse the repository at this point in the history
* Moved query parameters into the command parameters with Into.
  • Loading branch information
greg-szabo authored Jul 17, 2020
1 parent b584b88 commit 7e8dc89
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 42 deletions.
29 changes: 17 additions & 12 deletions relayer/cli/src/commands/query/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ use relayer_modules::ics24_host::error::ValidationError;
use relayer_modules::path::{ChannelEndsPath, Path};
use std::str::FromStr;
use tendermint::abci::Path as TendermintPath;
use tendermint::block::Height;
use tendermint::chain::Id as ChainId;

#[derive(Clone, Command, Debug, Options)]
Expand Down Expand Up @@ -45,6 +44,17 @@ struct QueryChannelOptions {
proof: bool,
}

impl Into<Request> for QueryChannelOptions {
fn into(self) -> Request {
Request {
path: Some(TendermintPath::from_str(&"store/ibc/key").unwrap()),
data: ChannelEndsPath::new(self.port_id, self.channel_id).to_string(),
height: self.height,
prove: self.proof,
}
}
}

impl QueryChannelEndCmd {
fn validate_options(
&self,
Expand Down Expand Up @@ -111,17 +121,12 @@ impl Runnable for QueryChannelEndCmd {
// Note: currently both fail in amino_unmarshal_binary_length_prefixed().
// To test this start a Gaia node and configure a channel using the go relayer.
let chain = TendermintChain::from_config(chain_config).unwrap();
let res = block_on(query::<TendermintChain, RawChannel, ChannelEnd>(
&chain,
Request {
path: Some(TendermintPath::from_str(&"store/ibc/key").unwrap()),
data: ChannelEndsPath::new(opts.port_id, opts.channel_id)
.to_string()
.into_bytes(),
height: Some(Height::from(opts.height)),
prove: opts.proof,
},
));
let res = block_on(query::<
TendermintChain,
RawChannel,
ChannelEnd,
QueryChannelOptions,
>(&chain, opts));

match res {
Ok(cs) => status_info!("connection query result: ", "{:?}", cs),
Expand Down
29 changes: 17 additions & 12 deletions relayer/cli/src/commands/query/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ use ibc_proto::connection::ConnectionEnd as RawConnectionEnd;
use relayer_modules::ics03_connection::connection::ConnectionEnd;
use std::str::FromStr;
use tendermint::abci::Path as TendermintPath;
use tendermint::block::Height;

#[derive(Clone, Command, Debug, Options)]
pub struct QueryConnectionEndCmd {
Expand All @@ -39,6 +38,17 @@ struct QueryConnectionOptions {
proof: bool,
}

impl Into<Request> for QueryConnectionOptions {
fn into(self) -> Request {
Request {
path: Some(TendermintPath::from_str(&"store/ibc/key").unwrap()),
data: ConnectionPath::new(self.connection_id).to_string(),
height: self.height,
prove: self.proof,
}
}
}

impl QueryConnectionEndCmd {
fn validate_options(
&self,
Expand Down Expand Up @@ -97,17 +107,12 @@ impl Runnable for QueryConnectionEndCmd {
//
// Note: currently both fail in amino_unmarshal_binary_length_prefixed().
// To test this start a Gaia node and configure a client using the go relayer.
let res = block_on(query::<TendermintChain, RawConnectionEnd, ConnectionEnd>(
&chain,
Request {
path: Some(TendermintPath::from_str(&"store/ibc/key").unwrap()),
data: ConnectionPath::new(opts.connection_id)
.to_string()
.into_bytes(),
height: Some(Height::from(opts.height)),
prove: opts.proof,
},
));
let res = block_on(query::<
TendermintChain,
RawConnectionEnd,
ConnectionEnd,
QueryConnectionOptions,
>(&chain, opts));

match res {
Ok(cs) => status_info!("connection query result: ", "{:?}", cs),
Expand Down
40 changes: 22 additions & 18 deletions relayer/relay/src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,22 @@ use crate::chain::Chain;
use bytes::Bytes;
use prost::Message;
use relayer_modules::error;
use serde::{Deserialize, Serialize};
use std::convert::TryFrom;
use tendermint::abci::Path;
use tendermint::abci::Path as TendermintPath;
use tendermint::block;

pub mod client;

// This struct was copied form tenderint-rs rpc/source/endpoint/abci_query.rs and made public.
// Todo: Work on removing it by consolidating the Request part of every query function - maybe with a helper function.
/// Query the ABCI application for information
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct Request {
/// Path to the data
#[serde(skip_serializing_if = "Option::is_none")]
pub path: Option<Path>,
pub path: Option<TendermintPath>,

/// Data to query
//#[serde(with = "serializers::bytes::hexstring")]
pub data: Vec<u8>,
pub data: String,

/// Block height
#[serde(skip_serializing_if = "Option::is_none")]
pub height: Option<block::Height>,
pub height: u64,

/// Include proof in response
pub prove: bool,
Expand All @@ -34,24 +27,35 @@ pub struct Request {
///
/// is_query_store_with_proofxpects a format like /<queryType>/<storeName>/<subpath>,
/// where queryType must be "store" and subpath must be "key" to require a proof.
fn is_query_store_with_proof(_path: &Path) -> bool {
fn is_query_store_with_proof(_path: &TendermintPath) -> bool {
false
}

/// Perform a generic `abci_query` on the given `chain`, and return the corresponding deserialized response data.
pub async fn query<C, R, T>(chain: &C, request: Request) -> Result<T, error::Error>
pub async fn query<C, R, T, O>(chain: &C, request: O) -> Result<T, error::Error>
where
C: Chain,
R: Message + Default,
T: TryFrom<R>,
C: Chain, // Chain configuration
R: Message + Default, // Raw Struct type
T: TryFrom<R>, // Internal Struct type
O: Into<Request>, // Query Command configuration (opts)
{
// RPC Request

let path = request.path.clone().unwrap();
let request: Request = request.into();
let path = request.path.clone().unwrap(); // for the is_query_store_with_proof function

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

Expand Down

0 comments on commit 7e8dc89

Please sign in to comment.