Skip to content

Commit

Permalink
ICS24 Path variants as types (informalsystems#1760)
Browse files Browse the repository at this point in the history
* Path variants as types

* Use derive_more crate

* Simplify Path usage

* Minor refactoring

* More cleanup

* Add .changelog entry
  • Loading branch information
hu55a1n1 authored Jan 17, 2022
1 parent 4c536a8 commit 6788ddf
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- Extract all `ics24_host::Path` variants into their separate types
([#1760](https://github.com/informalsystems/ibc-rs/issues/1760))
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

49 changes: 29 additions & 20 deletions relayer/src/chain/cosmos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ use ibc::core::ics04_channel::packet::{Packet, PacketMsgType, Sequence};
use ibc::core::ics23_commitment::commitment::CommitmentPrefix;
use ibc::core::ics23_commitment::merkle::convert_tm_to_ics_merkle_proof;
use ibc::core::ics24_host::identifier::{ChainId, ChannelId, ClientId, ConnectionId, PortId};
use ibc::core::ics24_host::Path::ClientConsensusState as ClientConsensusPath;
use ibc::core::ics24_host::Path::ClientState as ClientStatePath;
use ibc::core::ics24_host::{ClientUpgradePath, Path, IBC_QUERY_PATH, SDK_UPGRADE_QUERY_PATH};
use ibc::events::{from_tx_response_event, IbcEvent};
use ibc::query::{QueryTxHash, QueryTxRequest};
Expand Down Expand Up @@ -97,6 +95,10 @@ use crate::{
};

use super::{ChainEndpoint, HealthCheck};
use ibc::core::ics24_host::path::{
AcksPath, ChannelEndsPath, ClientConsensusStatePath, ClientStatePath, CommitmentsPath,
ConnectionsPath, ReceiptsPath, SeqRecvsPath,
};

mod compatibility;
pub mod version;
Expand Down Expand Up @@ -594,7 +596,12 @@ impl CosmosSdkChain {
self.config.max_tx_size.into()
}

fn query(&self, data: Path, height: ICSHeight, prove: bool) -> Result<QueryResponse, Error> {
fn query(
&self,
data: impl Into<Path>,
height: ICSHeight,
prove: bool,
) -> Result<QueryResponse, Error> {
crate::time!("query");

// SAFETY: Creating a Path from a constant; this should never fail
Expand All @@ -603,6 +610,7 @@ impl CosmosSdkChain {

let height = Height::try_from(height.revision_height).map_err(Error::invalid_height)?;

let data = data.into();
if !data.is_provable() & prove {
return Err(Error::private_store());
}
Expand Down Expand Up @@ -1546,7 +1554,7 @@ impl ChainEndpoint for CosmosSdkChain {
height: ICSHeight,
) -> Result<ChannelEnd, Error> {
let res = self.query(
Path::ChannelEnds(port_id.clone(), channel_id.clone()),
ChannelEndsPath(port_id.clone(), channel_id.clone()),
height,
false,
)?;
Expand Down Expand Up @@ -1926,7 +1934,7 @@ impl ChainEndpoint for CosmosSdkChain {
crate::time!("proven_client_consensus");

let res = self.query(
ClientConsensusPath {
ClientConsensusStatePath {
client_id: client_id.clone(),
epoch: consensus_height.revision_number,
height: consensus_height.revision_height,
Expand All @@ -1952,7 +1960,7 @@ impl ChainEndpoint for CosmosSdkChain {
connection_id: &ConnectionId,
height: ICSHeight,
) -> Result<(ConnectionEnd, MerkleProof), Error> {
let res = self.query(Path::Connections(connection_id.clone()), height, true)?;
let res = self.query(ConnectionsPath(connection_id.clone()), height, true)?;
let connection_end = ConnectionEnd::decode_vec(&res.value).map_err(Error::decode)?;

Ok((
Expand All @@ -1968,7 +1976,7 @@ impl ChainEndpoint for CosmosSdkChain {
height: ICSHeight,
) -> Result<(ChannelEnd, MerkleProof), Error> {
let res = self.query(
Path::ChannelEnds(port_id.clone(), channel_id.clone()),
ChannelEndsPath(port_id.clone(), channel_id.clone()),
height,
true,
)?;
Expand All @@ -1989,31 +1997,32 @@ impl ChainEndpoint for CosmosSdkChain {
sequence: Sequence,
height: ICSHeight,
) -> Result<(Vec<u8>, MerkleProof), Error> {
let data = match packet_type {
PacketMsgType::Recv => Path::Commitments {
let data: Path = match packet_type {
PacketMsgType::Recv => CommitmentsPath {
port_id,
channel_id,
sequence,
},
PacketMsgType::Ack => Path::Acks {
}
.into(),
PacketMsgType::Ack => AcksPath {
port_id,
channel_id,
sequence,
},
PacketMsgType::TimeoutUnordered => Path::Receipts {
}
.into(),
PacketMsgType::TimeoutUnordered => ReceiptsPath {
port_id,
channel_id,
sequence,
},
PacketMsgType::TimeoutOrdered => Path::SeqRecvs {
0: port_id,
1: channel_id,
},
PacketMsgType::TimeoutOnClose => Path::Receipts {
}
.into(),
PacketMsgType::TimeoutOrdered => SeqRecvsPath(port_id, channel_id).into(),
PacketMsgType::TimeoutOnClose => ReceiptsPath {
port_id,
channel_id,
sequence,
},
}
.into(),
};

let res = self.query(data, height, true)?;
Expand Down

0 comments on commit 6788ddf

Please sign in to comment.