Skip to content

Commit

Permalink
Clean up QueryError and RelayerError types (#1321)
Browse files Browse the repository at this point in the history
* Clean up ibc-query QueryError type

* Clean up ibc-testkit RelayerError

* Clean up ibc-testkit RelayerError

* Change wording of a doc comment

* Remove RelayerError
  • Loading branch information
seanchen1991 authored Aug 27, 2024
1 parent e1b4db2 commit 466c13d
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 89 deletions.
20 changes: 10 additions & 10 deletions ibc-query/src/core/channel/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ where
let proof = ibc_ctx
.get_proof(proof_height, &Path::ChannelEnd(channel_end_path.clone()))
.ok_or_else(|| {
QueryError::proof_not_found(format!(
QueryError::missing_proof(format!(
"Proof not found for channel end path {channel_end_path:?}"
))
})?;
Expand Down Expand Up @@ -119,7 +119,7 @@ where
.first()
.map(|connection_id| ibc_ctx.connection_end(connection_id))
.ok_or_else(|| {
QueryError::proof_not_found(format!(
QueryError::missing_proof(format!(
"Channel {} does not have a connection",
request.channel_id
))
Expand All @@ -140,7 +140,7 @@ where
&Path::ClientState(ClientStatePath::new(connection_end.client_id().clone())),
)
.ok_or_else(|| {
QueryError::proof_not_found(format!(
QueryError::missing_proof(format!(
"Proof not found for client state path: {:?}",
connection_end.client_id()
))
Expand Down Expand Up @@ -172,7 +172,7 @@ where
.first()
.map(|connection_id| ibc_ctx.connection_end(connection_id))
.ok_or_else(|| {
QueryError::proof_not_found(format!(
QueryError::missing_proof(format!(
"Channel {} does not have a connection",
request.channel_id
))
Expand All @@ -198,7 +198,7 @@ where
&Path::ClientConsensusState(consensus_path.clone()),
)
.ok_or_else(|| {
QueryError::proof_not_found(format!(
QueryError::missing_proof(format!(
"Proof not found for client consensus state path: {consensus_path:?}"
))
})?;
Expand Down Expand Up @@ -233,7 +233,7 @@ where
let proof = ibc_ctx
.get_proof(proof_height, &Path::Commitment(commitment_path.clone()))
.ok_or_else(|| {
QueryError::proof_not_found(format!(
QueryError::missing_proof(format!(
"Proof not found for packet commitment path: {commitment_path:?}"
))
})?;
Expand Down Expand Up @@ -291,7 +291,7 @@ where
let proof = ibc_ctx
.get_proof(proof_height, &Path::Receipt(receipt_path.clone()))
.ok_or_else(|| {
QueryError::proof_not_found(format!(
QueryError::missing_proof(format!(
"Proof not found for packet receipt path: {receipt_path:?}"
))
})?;
Expand Down Expand Up @@ -325,7 +325,7 @@ where
let proof = ibc_ctx
.get_proof(proof_height, &Path::Ack(acknowledgement_path.clone()))
.ok_or_else(|| {
QueryError::proof_not_found(format!(
QueryError::missing_proof(format!(
"Proof not found for packet acknowledgement path: {acknowledgement_path:?}"
))
})?;
Expand Down Expand Up @@ -431,7 +431,7 @@ where
let proof = ibc_ctx
.get_proof(proof_height, &Path::SeqSend(next_seq_send_path))
.ok_or_else(|| {
QueryError::proof_not_found(format!(
QueryError::missing_proof(format!(
"Next sequence send proof not found for channel {}",
request.channel_id
))
Expand Down Expand Up @@ -464,7 +464,7 @@ where
let proof = ibc_ctx
.get_proof(proof_height, &Path::SeqRecv(next_seq_recv_path))
.ok_or_else(|| {
QueryError::proof_not_found(format!(
QueryError::missing_proof(format!(
"Next sequence receive proof not found for channel {}",
request.channel_id
))
Expand Down
10 changes: 5 additions & 5 deletions ibc-query/src/core/client/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ where
&Path::ClientState(ClientStatePath::new(client_id.clone())),
)
.ok_or_else(|| {
QueryError::proof_not_found(format!(
QueryError::missing_proof(format!(
"Proof not found for client state path: {client_id:?}"
))
})?;
Expand Down Expand Up @@ -109,7 +109,7 @@ where
.into_iter()
.max_by_key(|&(h, _)| h)
.ok_or_else(|| {
QueryError::proof_not_found(format!(
QueryError::missing_proof(format!(
"No consensus state found for client: {client_id:?}"
))
})?
Expand All @@ -130,7 +130,7 @@ where
)),
)
.ok_or_else(|| {
QueryError::proof_not_found(format!(
QueryError::missing_proof(format!(
"Proof not found for consensus state path: {client_id:?}"
))
})?;
Expand Down Expand Up @@ -242,7 +242,7 @@ where
&Path::UpgradeClientState(upgraded_client_state_path),
)
.ok_or_else(|| {
QueryError::proof_not_found(format!(
QueryError::missing_proof(format!(
"Proof not found for upgraded client state at: {proof_height:?}"
))
})?;
Expand Down Expand Up @@ -301,7 +301,7 @@ where
&Path::UpgradeConsensusState(upgraded_consensus_state_path),
)
.ok_or_else(|| {
QueryError::proof_not_found(format!(
QueryError::missing_proof(format!(
"Proof not found for upgraded consensus state at: {proof_height:?}"
))
})?;
Expand Down
8 changes: 4 additions & 4 deletions ibc-query/src/core/connection/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ where
&Path::Connection(ConnectionPath::new(&request.connection_id)),
)
.ok_or_else(|| {
QueryError::proof_not_found(format!(
QueryError::missing_proof(format!(
"Proof not found for connection path: {:?}",
request.connection_id
))
Expand Down Expand Up @@ -92,7 +92,7 @@ where
&Path::ClientConnection(ClientConnectionPath::new(request.client_id.clone())),
)
.ok_or_else(|| {
QueryError::proof_not_found(format!(
QueryError::missing_proof(format!(
"Proof not found for client connection path: {:?}",
request.client_id
))
Expand Down Expand Up @@ -130,7 +130,7 @@ where
&Path::ClientState(ClientStatePath::new(connection_end.client_id().clone())),
)
.ok_or_else(|| {
QueryError::proof_not_found(format!(
QueryError::missing_proof(format!(
"Proof not found for client state path: {:?}",
connection_end.client_id()
))
Expand Down Expand Up @@ -172,7 +172,7 @@ where
let proof = ibc_ctx
.get_proof(proof_height, &Path::ClientConsensusState(consensus_path))
.ok_or_else(|| {
QueryError::proof_not_found(format!(
QueryError::missing_proof(format!(
"Proof not found for consensus state path: {:?}",
connection_end.client_id()
))
Expand Down
19 changes: 11 additions & 8 deletions ibc-query/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,24 @@ use ibc::core::handler::types::error::ContextError;
use ibc::core::host::types::error::IdentifierError;
use tonic::Status;

/// The main error type of the ibc-query crate. This type mainly
/// serves to surface lower-level errors that occur when executing
/// ibc-query's codepaths.
#[derive(Debug, Display)]
pub enum QueryError {
/// Context error: {0}
/// context error: `{0}`
ContextError(ContextError),
/// Identifier error: {0}
/// identifier error: `{0}`
IdentifierError(IdentifierError),
/// Proof not found: {0}
ProofNotFound(String),
/// Missing field: {0}
/// missing proof: `{0}`
MissingProof(String),
/// missing field: `{0}`
MissingField(String),
}

impl QueryError {
pub fn proof_not_found<T: ToString>(description: T) -> Self {
Self::ProofNotFound(description.to_string())
pub fn missing_proof<T: ToString>(description: T) -> Self {
Self::MissingProof(description.to_string())
}

pub fn missing_field<T: ToString>(description: T) -> Self {
Expand All @@ -35,7 +38,7 @@ impl From<QueryError> for Status {
match e {
QueryError::ContextError(ctx_err) => Self::internal(ctx_err.to_string()),
QueryError::IdentifierError(id_err) => Self::internal(id_err.to_string()),
QueryError::ProofNotFound(description) => Self::not_found(description),
QueryError::MissingProof(description) => Self::not_found(description),
QueryError::MissingField(description) => Self::invalid_argument(description),
}
}
Expand Down
7 changes: 3 additions & 4 deletions ibc-testkit/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ use ibc::primitives::Timestamp;
use super::testapp::ibc::core::types::{LightClientState, MockIbcStore};
use crate::fixtures::core::context::TestContextConfig;
use crate::hosts::{HostClientState, MockHost, TendermintHost, TestBlock, TestHeader, TestHost};
use crate::relayer::error::RelayerError;
use crate::testapp::ibc::clients::{AnyClientState, AnyConsensusState};
use crate::testapp::ibc::core::router::MockRouter;
use crate::testapp::ibc::core::types::DEFAULT_BLOCK_TIME_SECS;
Expand Down Expand Up @@ -480,9 +479,9 @@ where
/// A datagram passes from the relayer to the IBC module (on host chain).
/// Alternative method to `Ics18Context::send` that does not exercise any serialization.
/// Used in testing the Ics18 algorithms, hence this may return an Ics18Error.
pub fn deliver(&mut self, msg: MsgEnvelope) -> Result<(), RelayerError> {
self.dispatch(msg)
.map_err(RelayerError::TransactionFailed)?;
pub fn deliver(&mut self, msg: MsgEnvelope) -> Result<(), ContextError> {
self.dispatch(msg)?;

// Create a new block.
self.advance_block_height();
Ok(())
Expand Down
38 changes: 0 additions & 38 deletions ibc-testkit/src/relayer/error.rs

This file was deleted.

1 change: 0 additions & 1 deletion ibc-testkit/src/relayer/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
pub mod context;
pub mod error;
pub mod integration;
pub mod utils;
29 changes: 10 additions & 19 deletions tests-integration/tests/core/ics02_client/update_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ use ibc_testkit::hosts::tendermint::BlockParams;
use ibc_testkit::hosts::{
HostClientState, MockHost, TendermintHost, TestBlock, TestHeader, TestHost,
};
use ibc_testkit::relayer::error::RelayerError;
use ibc_testkit::testapp::ibc::clients::mock::client_state::{
client_type as mock_client_type, MockClientState,
};
Expand Down Expand Up @@ -1527,7 +1526,7 @@ pub(crate) fn build_client_update_datagram<H: TestHeader, Dst: TestHost>(
dest: &TestContext<Dst>,
client_id: &ClientId,
src_header: &H,
) -> Result<ClientMsg, RelayerError>
) -> Option<ClientMsg>
where
HostClientState<Dst>: ClientStateValidation<DefaultIbcStore>,
{
Expand All @@ -1536,23 +1535,15 @@ where
let dest_client_latest_height = dest.light_client_latest_height(client_id);

if src_header.height() == dest_client_latest_height {
return Err(RelayerError::ClientAlreadyUpToDate {
client_id: client_id.clone(),
source_height: src_header.height(),
destination_height: dest_client_latest_height,
});
return None;
};

if dest_client_latest_height > src_header.height() {
return Err(RelayerError::ClientAtHigherHeight {
client_id: client_id.clone(),
source_height: src_header.height(),
destination_height: dest_client_latest_height,
});
return None;
};

// Client on destination chain can be updated.
Ok(ClientMsg::UpdateClient(MsgUpdateClient {
Some(ClientMsg::UpdateClient(MsgUpdateClient {
client_id: client_id.clone(),
client_message: src_header.clone().into(),
signer: dummy_account_id(),
Expand Down Expand Up @@ -1615,9 +1606,9 @@ fn client_update_ping_pong() {
);

assert!(
client_msg_b_res.is_ok(),
"create_client_update failed for context destination {ctx_b:?}, error: {client_msg_b_res:?}",
);
client_msg_b_res.is_some(),
"create_client_update failed for context destination {ctx_b:?}",
);

let client_msg_b = client_msg_b_res.unwrap();

Expand Down Expand Up @@ -1653,9 +1644,9 @@ fn client_update_ping_pong() {
build_client_update_datagram(&ctx_a, &client_on_a_for_b, &b_latest_header);

assert!(
client_msg_a_res.is_ok(),
"create_client_update failed for context destination {ctx_a:?}, error: {client_msg_a_res:?}",
);
client_msg_a_res.is_some(),
"create_client_update failed for context destination {ctx_a:?}",
);

let client_msg_a = client_msg_a_res.unwrap();

Expand Down

0 comments on commit 466c13d

Please sign in to comment.