Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Moved query parameters into the command parameters with Into. #155

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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