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

Implement ADR 009: ChainEndpoint and ChainHandle methods standardization #2200

Merged
merged 21 commits into from
May 19, 2022
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- Implement ADR 9: add domain type for request messages that are passed to query
functions ([#2192](https://github.com/informalsystems/ibc-rs/issues/2192))
10 changes: 10 additions & 0 deletions modules/src/core/ics23_commitment/commitment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ use ibc_proto::ibc::core::commitment::v1::MerkleProof as RawMerkleProof;
use serde::{Deserialize, Serialize};
use subtle_encoding::{Encoding, Hex};

use super::merkle::MerkleProof;

#[derive(Clone, PartialEq, Eq, Serialize)]
#[serde(transparent)]
pub struct CommitmentRoot {
Expand Down Expand Up @@ -88,6 +90,14 @@ impl TryFrom<RawMerkleProof> for CommitmentProofBytes {
}
}

impl TryFrom<MerkleProof> for CommitmentProofBytes {
type Error = ProofError;

fn try_from(value: MerkleProof) -> Result<Self, Self::Error> {
Self::try_from(RawMerkleProof::from(value))
}
}

impl TryFrom<CommitmentProofBytes> for RawMerkleProof {
type Error = Error;

Expand Down
20 changes: 18 additions & 2 deletions modules/src/core/ics23_commitment/merkle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,22 @@ impl From<RawMerkleProof> for MerkleProof {
}
}

impl From<MerkleProof> for RawMerkleProof {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question: What's a RawMerkleProof and when is it appropriate to use it vs. a "regular" MerkleProof?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The RawMerkleProof is the type that is defined in ibc-go's protobuf files. MerkleProof here is our own domain type for it, and hence we should use it everywhere in our codebase except when it's time to interact with a gRPC endpoint (where we convert our MerkleProof back into a RawMerkleProof to be sent on the wire)

fn from(proof: MerkleProof) -> Self {
Self {
proofs: proof
.proofs
.into_iter()
.map(|p| {
let mut encoded = Vec::new();
prost::Message::encode(&p, &mut encoded).unwrap();
prost::Message::decode(&*encoded).unwrap()
})
.collect(),
}
}
}

impl MerkleProof {
pub fn verify_membership(
&self,
Expand Down Expand Up @@ -216,7 +232,7 @@ fn calculate_non_existence_root(proof: &NonExistenceProof) -> Result<Vec<u8>, Er
// }
// }

pub fn convert_tm_to_ics_merkle_proof(tm_proof: &TendermintProof) -> Result<RawMerkleProof, Error> {
pub fn convert_tm_to_ics_merkle_proof(tm_proof: &TendermintProof) -> Result<MerkleProof, Error> {
let mut proofs = Vec::new();

for op in &tm_proof.ops {
Expand All @@ -227,5 +243,5 @@ pub fn convert_tm_to_ics_merkle_proof(tm_proof: &TendermintProof) -> Result<RawM
proofs.push(parsed);
}

Ok(RawMerkleProof { proofs })
Ok(MerkleProof::from(RawMerkleProof { proofs }))
}
1 change: 1 addition & 0 deletions proto/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ pub mod cosmos {
include_proto!("cosmos.base.query.v1beta1.rs");
}

// TODO (BEFORE MERGING PR): Remove
pub mod pagination {
use super::v1beta1::PageRequest;

Expand Down
11 changes: 9 additions & 2 deletions relayer-cli/src/commands/create/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use ibc::core::ics04_channel::Version;
use ibc::core::ics24_host::identifier::{ChainId, ConnectionId, PortId};
use ibc::Height;
use ibc_relayer::chain::handle::ChainHandle;
use ibc_relayer::chain::requests::{QueryClientStateRequest, QueryConnectionRequest};
use ibc_relayer::channel::Channel;
use ibc_relayer::connection::Connection;
use ibc_relayer::foreign_client::ForeignClient;
Expand Down Expand Up @@ -187,12 +188,18 @@ impl CreateChannelCommand {
// Query the connection end.
let height = Height::new(chain_a.id().version(), 0);
let conn_end = chain_a
.query_connection(connection_a, height)
.query_connection(QueryConnectionRequest {
connection_id: connection_a.clone(),
height,
})
.unwrap_or_else(exit_with_unrecoverable_error);

// Query the client state, obtain the identifier of chain b.
let chain_b = chain_a
.query_client_state(conn_end.client_id(), height)
.query_client_state(QueryClientStateRequest {
client_id: conn_end.client_id().clone(),
height,
})
.map(|cs| cs.chain_id())
.unwrap_or_else(exit_with_unrecoverable_error);

Expand Down
6 changes: 5 additions & 1 deletion relayer-cli/src/commands/create/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use ibc::core::ics02_client::client_state::ClientState;
use ibc::core::ics24_host::identifier::{ChainId, ClientId};
use ibc::Height;
use ibc_relayer::chain::handle::ChainHandle;
use ibc_relayer::chain::requests::QueryClientStateRequest;
use ibc_relayer::connection::Connection;
use ibc_relayer::foreign_client::ForeignClient;

Expand Down Expand Up @@ -114,7 +115,10 @@ impl CreateConnectionCommand {

// Query client state. Extract the target chain (chain_id which this client is verifying).
let height = Height::new(chain_a.id().version(), 0);
let chain_b_id = match chain_a.query_client_state(client_a_id, height) {
let chain_b_id = match chain_a.query_client_state(QueryClientStateRequest {
client_id: client_a_id.clone(),
height,
}) {
Ok(cs) => cs.chain_id(),
Err(e) => Output::error(format!(
"failed while querying client '{}' on chain '{}' with error: {}",
Expand Down
6 changes: 5 additions & 1 deletion relayer-cli/src/commands/misbehaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use ibc::core::ics02_client::height::Height;
use ibc::core::ics24_host::identifier::{ChainId, ClientId};
use ibc::events::IbcEvent;
use ibc_relayer::chain::handle::ChainHandle;
use ibc_relayer::chain::requests::QueryClientStateRequest;
use ibc_relayer::config::Config;
use ibc_relayer::foreign_client::{ForeignClient, MisbehaviourResults};
use std::ops::Deref;
Expand Down Expand Up @@ -99,7 +100,10 @@ fn misbehaviour_handling<Chain: ChainHandle>(
update: Option<UpdateClient>,
) -> Result<(), Box<dyn std::error::Error>> {
let client_state = chain
.query_client_state(&client_id, Height::zero())
.query_client_state(QueryClientStateRequest {
client_id: client_id.clone(),
height: Height::zero(),
})
.map_err(|e| format!("could not query client state for {}: {}", client_id, e))?;

if client_state.is_frozen() {
Expand Down
8 changes: 6 additions & 2 deletions relayer-cli/src/commands/query/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use tokio::runtime::Runtime as TokioRuntime;

use ibc::core::ics24_host::identifier::ChainId;
use ibc::core::ics24_host::identifier::{ChannelId, PortId};
use ibc_relayer::chain::requests::QueryChannelRequest;
use ibc_relayer::chain::{ChainEndpoint, CosmosSdkChain};

use crate::conclude::{exit_with_unrecoverable_error, Output};
Expand Down Expand Up @@ -46,8 +47,11 @@ impl Runnable for QueryChannelEndCmd {
let chain = CosmosSdkChain::bootstrap(chain_config.clone(), rt)
.unwrap_or_else(exit_with_unrecoverable_error);

let height = ibc::Height::new(chain.id().version(), self.height.unwrap_or(0_u64));
let res = chain.query_channel(&self.port_id, &self.channel_id, height);
let res = chain.query_channel(QueryChannelRequest {
port_id: self.port_id.clone(),
channel_id: self.channel_id,
height: ibc::Height::new(chain.id().version(), self.height.unwrap_or(0_u64)),
});
match res {
Ok(channel_end) => {
if channel_end.state_matches(&State::Uninitialized) {
Expand Down
47 changes: 33 additions & 14 deletions relayer-cli/src/commands/query/channel_ends.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ use ibc::core::ics24_host::identifier::ChainId;
use ibc::core::ics24_host::identifier::{ChannelId, ClientId, ConnectionId, PortId};
use ibc::Height;
use ibc_relayer::chain::handle::{BaseChainHandle, ChainHandle};
use ibc_relayer::chain::requests::{
QueryChannelRequest, QueryClientStateRequest, QueryConnectionRequest,
};
use ibc_relayer::registry::Registry;

use crate::conclude::Output;
Expand Down Expand Up @@ -78,7 +81,11 @@ fn do_run<Chain: ChainHandle>(cmd: &QueryChannelEndsCmd) -> Result<(), Box<dyn s
None => chain.query_latest_height()?,
};

let channel_end = chain.query_channel(&port_id, &channel_id, chain_height)?;
let channel_end = chain.query_channel(QueryChannelRequest {
port_id: port_id.clone(),
channel_id,
height: chain_height,
})?;
if channel_end.state_matches(&State::Uninitialized) {
return Err(format!(
"{}/{} on chain {} @ {:?} is uninitialized",
Expand All @@ -98,11 +105,17 @@ fn do_run<Chain: ChainHandle>(cmd: &QueryChannelEndsCmd) -> Result<(), Box<dyn s
})?
.clone();

let connection_end = chain.query_connection(&connection_id, chain_height)?;
let connection_end = chain.query_connection(QueryConnectionRequest {
connection_id: connection_id.clone(),
height: chain_height,
})?;

let client_id = connection_end.client_id().clone();

let client_state = chain.query_client_state(&client_id, chain_height)?;
let client_state = chain.query_client_state(QueryClientStateRequest {
client_id: client_id.clone(),
height: chain_height,
})?;

let channel_counterparty = channel_end.counterparty().clone();
let connection_counterparty = connection_end.counterparty().clone();
Expand Down Expand Up @@ -132,17 +145,23 @@ fn do_run<Chain: ChainHandle>(cmd: &QueryChannelEndsCmd) -> Result<(), Box<dyn s
let counterparty_chain = registry.get_or_spawn(&counterparty_chain_id)?;
let counterparty_chain_height = counterparty_chain.query_latest_height()?;

let counterparty_connection_end = counterparty_chain
.query_connection(&counterparty_connection_id, counterparty_chain_height)?;

let counterparty_client_state = counterparty_chain
.query_client_state(&counterparty_client_id, counterparty_chain_height)?;

let counterparty_channel_end = counterparty_chain.query_channel(
&counterparty_port_id,
&counterparty_channel_id,
counterparty_chain_height,
)?;
let counterparty_connection_end =
counterparty_chain.query_connection(QueryConnectionRequest {
connection_id: counterparty_connection_id.clone(),
height: counterparty_chain_height,
})?;

let counterparty_client_state =
counterparty_chain.query_client_state(QueryClientStateRequest {
client_id: counterparty_client_id.clone(),
height: counterparty_chain_height,
})?;

let counterparty_channel_end = counterparty_chain.query_channel(QueryChannelRequest {
port_id: counterparty_port_id.clone(),
channel_id: counterparty_channel_id,
height: counterparty_chain_height,
})?;

if cmd.verbose {
let res = ChannelEnds {
Expand Down
51 changes: 32 additions & 19 deletions relayer-cli/src/commands/query/channels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ use ibc::core::ics02_client::client_state::ClientState;
use ibc::core::ics04_channel::channel::{ChannelEnd, State};
use ibc::core::ics24_host::identifier::{ChainId, ChannelId, ConnectionId, PortChannelId, PortId};
use ibc::Height;
use ibc_proto::ibc::core::channel::v1::QueryChannelsRequest;
use ibc_relayer::chain::handle::{BaseChainHandle, ChainHandle};
use ibc_relayer::chain::requests::{
PageRequest, QueryChannelRequest, QueryChannelsRequest, QueryClientStateRequest,
QueryConnectionRequest,
};
use ibc_relayer::registry::Registry;

use crate::commands::query::channel_ends::ChannelEnds;
Expand Down Expand Up @@ -54,11 +57,9 @@ fn run_query_channels<Chain: ChainHandle>(
let chain = registry.get_or_spawn(&cmd.chain_id)?;
let chain_height = chain.query_latest_height()?;

let req = QueryChannelsRequest {
pagination: ibc_proto::cosmos::base::query::pagination::all(),
};

let identified_channels = chain.query_channels(req)?;
let identified_channels = chain.query_channels(QueryChannelsRequest {
pagination: Some(PageRequest::all()),
})?;

for identified_channel in identified_channels {
let port_id = identified_channel.port_id;
Expand Down Expand Up @@ -125,9 +126,15 @@ fn query_channel_ends<Chain: ChainHandle>(
channel_id: ChannelId,
chain_height: Height,
) -> Result<ChannelEnds, Box<dyn std::error::Error>> {
let connection_end = chain.query_connection(&connection_id, chain_height)?;
let connection_end = chain.query_connection(QueryConnectionRequest {
connection_id: connection_id.clone(),
height: chain_height,
})?;
let client_id = connection_end.client_id().clone();
let client_state = chain.query_client_state(&client_id, chain_height)?;
let client_state = chain.query_client_state(QueryClientStateRequest {
client_id,
height: chain_height,
})?;
let counterparty_chain_id = client_state.chain_id();

if let Some(dst_chain_id) = destination_chain {
Expand Down Expand Up @@ -166,17 +173,23 @@ fn query_channel_ends<Chain: ChainHandle>(
let counterparty_chain = registry.get_or_spawn(&counterparty_chain_id)?;
let counterparty_chain_height = counterparty_chain.query_latest_height()?;

let counterparty_connection_end = counterparty_chain
.query_connection(&counterparty_connection_id, counterparty_chain_height)?;

let counterparty_client_state = counterparty_chain
.query_client_state(&counterparty_client_id, counterparty_chain_height)?;

let counterparty_channel_end = counterparty_chain.query_channel(
&counterparty_port_id,
&counterparty_channel_id,
counterparty_chain_height,
)?;
let counterparty_connection_end =
counterparty_chain.query_connection(QueryConnectionRequest {
connection_id: counterparty_connection_id,
height: counterparty_chain_height,
})?;

let counterparty_client_state =
counterparty_chain.query_client_state(QueryClientStateRequest {
client_id: counterparty_client_id,
height: counterparty_chain_height,
})?;

let counterparty_channel_end = counterparty_chain.query_channel(QueryChannelRequest {
port_id: counterparty_port_id,
channel_id: counterparty_channel_id,
height: counterparty_chain_height,
})?;

Ok(ChannelEnds {
channel_end,
Expand Down
Loading