Skip to content

Commit

Permalink
Add back height parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
soareschen committed Jun 14, 2021
1 parent 30f6757 commit a7ad50b
Show file tree
Hide file tree
Showing 15 changed files with 122 additions and 79 deletions.
3 changes: 1 addition & 2 deletions relayer-cli/src/commands/create/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,8 @@ impl CreateChannelCommand {
// Query the connection end.
let height = Height::new(chain_a.id().version(), 0);
let conn_end = chain_a
.query_connection(connection_a_id)
.query_connection(connection_a_id, height)
.unwrap_or_else(exit_with_unrecoverable_error);

// Query the client state, obtain the identifier of chain b.
let chain_b_id = chain_a
.query_client_state(conn_end.client_id(), height)
Expand Down
7 changes: 6 additions & 1 deletion relayer-cli/src/commands/query/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ pub struct QueryConnectionEndCmd {

#[options(free, required, help = "identifier of the connection to query")]
connection_id: ConnectionId,

#[options(help = "height of the state to query", short = "h")]
height: Option<u64>,
}

// cargo run --bin hermes -- query connection end ibc-test connectionidone --height 3
Expand All @@ -45,7 +48,9 @@ impl Runnable for QueryConnectionEndCmd {
let rt = Arc::new(TokioRuntime::new().unwrap());
let chain = CosmosSdkChain::bootstrap(chain_config.clone(), rt).unwrap();

let res = chain.query_connection(&self.connection_id);
let height = ibc::Height::new(chain.id().version(), self.height.unwrap_or(0_u64));
let res = chain.query_connection(&self.connection_id, height);

match res {
Ok(connection_end) => {
if connection_end.state_matches(&State::Uninitialized) {
Expand Down
6 changes: 5 additions & 1 deletion relayer-cli/src/commands/tx/channel.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use abscissa_core::{Command, Options, Runnable};

use ibc::events::IbcEvent;
use ibc::ics02_client::height::Height;
use ibc::ics03_connection::connection::ConnectionEnd;
use ibc::ics04_channel::channel::Order;
use ibc::ics24_host::identifier::{ChainId, ChannelId, ClientId, ConnectionId, PortId};
Expand All @@ -22,7 +23,10 @@ macro_rules! tx_chan_cmd {
};

// Retrieve the connection
let dst_connection = match chains.dst.query_connection(&$self.dst_conn_id) {
let dst_connection = match chains
.dst
.query_connection(&$self.dst_conn_id, Height::zero())
{
Ok(connection) => connection,
Err(e) => return Output::error(format!("{}", e)).exit(),
};
Expand Down
2 changes: 1 addition & 1 deletion relayer-cli/src/commands/tx/transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ impl Runnable for TxIcs20MsgTransferCmd {
};

let conn_end = src_chain
.query_connection(conn_id)
.query_connection(conn_id, Height::zero())
.unwrap_or_else(exit_with_unrecoverable_error);

debug!("connection hop underlying the channel: {:?}", conn_end);
Expand Down
6 changes: 5 additions & 1 deletion relayer/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,11 @@ pub trait Chain: Sized {
request: QueryClientConnectionsRequest,
) -> Result<Vec<ConnectionId>, Error>;

fn query_connection(&self, connection_id: &ConnectionId) -> Result<ConnectionEnd, Error>;
fn query_connection(
&self,
connection_id: &ConnectionId,
height: ICSHeight,
) -> Result<ConnectionEnd, Error>;

/// Performs a query to retrieve the identifiers of all channels associated with a connection.
fn query_connection_channels(
Expand Down
100 changes: 57 additions & 43 deletions relayer/src/chain/cosmos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,47 +316,6 @@ impl CosmosSdkChain {

Ok((proof, height))
}

async fn do_query_connection(
&self,
connection_id: &ConnectionId,
) -> Result<ConnectionEnd, Error> {
use ibc_proto::ibc::core::connection::v1 as connection;

let mut client = connection::query_client::QueryClient::connect(self.grpc_addr.clone())
.await
.map_err(|e| Kind::Grpc.context(e))?;

let response = client
.connection(connection::QueryConnectionRequest {
connection_id: connection_id.to_string(),
})
.await
.map_err(|e| {
if e.code() == Code::NotFound {
Kind::ConnNotFound(connection_id.clone()).into()
} else {
Kind::Grpc.context(e)
}
})?;

match response.into_inner().connection {
Some(raw_connection) => {
let connection_end = raw_connection
.try_into()
.map_err(|e| Kind::Grpc.context(e))?;

Ok(connection_end)
}
None => {
// When no connection is found, the GRPC call itself should return
// the NotFound error code. Nevertheless even if the call is successful,
// the connection field may not be present, because in protobuf3
// everything is optional.
Err(Kind::ConnNotFound(connection_id.clone()).into())
}
}
}
}

impl Chain for CosmosSdkChain {
Expand Down Expand Up @@ -788,8 +747,63 @@ impl Chain for CosmosSdkChain {
Ok(ids)
}

fn query_connection(&self, connection_id: &ConnectionId) -> Result<ConnectionEnd, Error> {
self.block_on(async { self.do_query_connection(connection_id).await })
fn query_connection(
&self,
connection_id: &ConnectionId,
height: ICSHeight,
) -> Result<ConnectionEnd, Error> {
async fn do_query_connection(
chain: &CosmosSdkChain,
connection_id: &ConnectionId,
height: ICSHeight,
) -> Result<ConnectionEnd, Error> {
use ibc_proto::ibc::core::connection::v1 as connection;
use tonic::{metadata::MetadataValue, IntoRequest};

let mut client =
connection::query_client::QueryClient::connect(chain.grpc_addr.clone())
.await
.map_err(|e| Kind::Grpc.context(e))?;

let mut request = connection::QueryConnectionRequest {
connection_id: connection_id.to_string(),
}
.into_request();

let height_param = MetadataValue::from_str(&height.revision_height.to_string())
.map_err(|e| Kind::Grpc.context(e))?;

request
.metadata_mut()
.insert("x-cosmos-block-height", height_param);

let response = client.connection(request).await.map_err(|e| {
if e.code() == Code::NotFound {
Kind::ConnNotFound(connection_id.clone()).into()
} else {
Kind::Grpc.context(e)
}
})?;

match response.into_inner().connection {
Some(raw_connection) => {
let connection_end = raw_connection
.try_into()
.map_err(|e| Kind::Grpc.context(e))?;

Ok(connection_end)
}
None => {
// When no connection is found, the GRPC call itself should return
// the NotFound error code. Nevertheless even if the call is successful,
// the connection field may not be present, because in protobuf3
// everything is optional.
Err(Kind::ConnNotFound(connection_id.clone()).into())
}
}
}

self.block_on(async { do_query_connection(self, connection_id, height).await })
}

fn query_connection_channels(
Expand Down
2 changes: 1 addition & 1 deletion relayer/src/chain/counterparty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ pub fn channel_connection_client(
.ok_or_else(|| Error::MissingConnectionHops(channel_id.clone(), chain.id()))?;

let connection_end = chain
.query_connection(&connection_id)
.query_connection(&connection_id, Height::zero())
.map_err(|e| Error::QueryFailed(format!("{}", e)))?;

if !connection_end.is_open() {
Expand Down
7 changes: 6 additions & 1 deletion relayer/src/chain/handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ pub enum ChainRequest {

QueryConnection {
connection_id: ConnectionId,
height: Height,
reply_to: ReplyTo<ConnectionEnd>,
},

Expand Down Expand Up @@ -354,7 +355,11 @@ pub trait ChainHandle: DynClone + Send + Sync + Debug {

fn query_compatible_versions(&self) -> Result<Vec<Version>, Error>;

fn query_connection(&self, connection_id: &ConnectionId) -> Result<ConnectionEnd, Error>;
fn query_connection(
&self,
connection_id: &ConnectionId,
height: Height,
) -> Result<ConnectionEnd, Error>;

fn query_connection_channels(
&self,
Expand Down
7 changes: 6 additions & 1 deletion relayer/src/chain/handle/prod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,14 @@ impl ChainHandle for ProdChainHandle {
self.send(|reply_to| ChainRequest::QueryCompatibleVersions { reply_to })
}

fn query_connection(&self, connection_id: &ConnectionId) -> Result<ConnectionEnd, Error> {
fn query_connection(
&self,
connection_id: &ConnectionId,
height: Height,
) -> Result<ConnectionEnd, Error> {
self.send(|reply_to| ChainRequest::QueryConnection {
connection_id: connection_id.clone(),
height,
reply_to,
})
}
Expand Down
7 changes: 6 additions & 1 deletion relayer/src/chain/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use ibc::downcast;
use ibc::events::IbcEvent;
use ibc::ics02_client::client_consensus::{AnyConsensusState, AnyConsensusStateWithHeight};
use ibc::ics02_client::client_state::{AnyClientState, IdentifiedAnyClientState};
use ibc::ics02_client::height::Height as ICSHeight;
use ibc::ics03_connection::connection::ConnectionEnd;
use ibc::ics04_channel::channel::{ChannelEnd, IdentifiedChannelEnd};
use ibc::ics04_channel::packet::{PacketMsgType, Sequence};
Expand Down Expand Up @@ -160,7 +161,11 @@ impl Chain for MockChain {
unimplemented!()
}

fn query_connection(&self, _connection_id: &ConnectionId) -> Result<ConnectionEnd, Error> {
fn query_connection(
&self,
_connection_id: &ConnectionId,
_height: ICSHeight,
) -> Result<ConnectionEnd, Error> {
unimplemented!()
}

Expand Down
7 changes: 4 additions & 3 deletions relayer/src/chain/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,8 +267,8 @@ impl<C: Chain + Send + 'static> ChainRuntime<C> {
self.query_compatible_versions(reply_to)?
},

Ok(ChainRequest::QueryConnection { connection_id, reply_to }) => {
self.query_connection(connection_id, reply_to)?
Ok(ChainRequest::QueryConnection { connection_id, height, reply_to }) => {
self.query_connection(connection_id, height, reply_to)?
},

Ok(ChainRequest::QueryConnectionChannels { request, reply_to }) => {
Expand Down Expand Up @@ -606,9 +606,10 @@ impl<C: Chain + Send + 'static> ChainRuntime<C> {
fn query_connection(
&self,
connection_id: ConnectionId,
height: Height,
reply_to: ReplyTo<ConnectionEnd>,
) -> Result<(), Error> {
let connection_end = self.chain.query_connection(&connection_id);
let connection_end = self.chain.query_connection(&connection_id, height);

reply_to.send(connection_end).map_err(Kind::channel)?;

Expand Down
12 changes: 6 additions & 6 deletions relayer/src/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ impl Channel {
.map_err(|e| ChannelError::QueryError(counterparty_chain.id(), e))?;

let connection_id = channel_event_attributes.connection_id.clone();
let connection = chain.query_connection(&connection_id)?;
let connection = chain.query_connection(&connection_id, Height::zero())?;
let connection_counterparty = connection.counterparty();

let counterparty_connection_id = connection_counterparty
Expand Down Expand Up @@ -238,7 +238,7 @@ impl Channel {
WorkerChannelError::MissingConnectionHops(channel.src_channel_id.clone(), chain.id())
})?;

let a_connection = chain.query_connection(&a_connection_id)?;
let a_connection = chain.query_connection(&a_connection_id, Height::zero())?;
let b_connection_id = a_connection
.counterparty()
.connection_id()
Expand Down Expand Up @@ -742,7 +742,7 @@ impl Channel {

// Connection must exist on destination
self.dst_chain()
.query_connection(self.dst_connection_id())
.query_connection(self.dst_connection_id(), Height::zero())
.map_err(|e| ChannelError::QueryError(self.dst_chain().id(), e))?;

let query_height = self
Expand Down Expand Up @@ -845,7 +845,7 @@ impl Channel {

// Connection must exist on destination
self.dst_chain()
.query_connection(self.dst_connection_id())
.query_connection(self.dst_connection_id(), Height::zero())
.map_err(|e| ChannelError::QueryError(self.dst_chain().id(), e))?;

let query_height = self
Expand Down Expand Up @@ -936,7 +936,7 @@ impl Channel {

// Connection must exist on destination
self.dst_chain()
.query_connection(self.dst_connection_id())
.query_connection(self.dst_connection_id(), Height::zero())
.map_err(|e| ChannelError::QueryError(self.dst_chain().id(), e))?;

let query_height = self
Expand Down Expand Up @@ -1078,7 +1078,7 @@ impl Channel {

// Connection must exist on destination
self.dst_chain()
.query_connection(self.dst_connection_id())
.query_connection(self.dst_connection_id(), Height::zero())
.map_err(|e| ChannelError::QueryError(self.dst_chain().id(), e))?;

let query_height = self
Expand Down
12 changes: 6 additions & 6 deletions relayer/src/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,11 +289,11 @@ impl Connection {
counter += 1;

// Continue loop if query error
let a_connection = a_chain.query_connection(&self.src_connection_id());
let a_connection = a_chain.query_connection(&self.src_connection_id(), Height::zero());
if a_connection.is_err() {
continue;
}
let b_connection = b_chain.query_connection(&self.dst_connection_id());
let b_connection = b_chain.query_connection(&self.dst_connection_id(), Height::zero());
if b_connection.is_err() {
continue;
}
Expand Down Expand Up @@ -388,7 +388,7 @@ impl Connection {
// Retrieve existing connection if any
let dst_connection = self
.dst_chain()
.query_connection(self.dst_connection_id())
.query_connection(self.dst_connection_id(), Height::zero())
.map_err(|e| ConnectionError::QueryError(self.dst_chain().id(), e))?;

// Check if a connection is expected to exist on destination chain
Expand Down Expand Up @@ -493,7 +493,7 @@ impl Connection {
pub fn build_conn_try(&self) -> Result<Vec<Any>, ConnectionError> {
let src_connection = self
.src_chain()
.query_connection(self.src_connection_id())
.query_connection(self.src_connection_id(), Height::zero())
.map_err(|e| ConnectionError::QueryError(self.src_chain().id(), e))?;

// TODO - check that the src connection is consistent with the try options
Expand Down Expand Up @@ -625,7 +625,7 @@ impl Connection {

let src_connection = self
.src_chain()
.query_connection(self.src_connection_id())
.query_connection(self.src_connection_id(), Height::zero())
.map_err(|e| ConnectionError::QueryError(self.src_chain().id(), e))?;

// TODO - check that the src connection is consistent with the ack options
Expand Down Expand Up @@ -726,7 +726,7 @@ impl Connection {

let _src_connection = self
.src_chain()
.query_connection(self.src_connection_id())
.query_connection(self.src_connection_id(), query_height)
.map_err(|_| {
ConnectionError::Failed(format!(
"missing connection {} on source chain",
Expand Down
2 changes: 1 addition & 1 deletion relayer/src/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1635,7 +1635,7 @@ impl Link {
}

let a_connection_id = a_channel.connection_hops()[0].clone();
let a_connection = a_chain.query_connection(&a_connection_id)?;
let a_connection = a_chain.query_connection(&a_connection_id, Height::zero())?;

if !a_connection.state_matches(&ConnectionState::Open) {
return Err(LinkError::Failed(format!(
Expand Down
Loading

0 comments on commit a7ad50b

Please sign in to comment.