Skip to content

Commit 4aecaec

Browse files
Add HostError contexts to module-level error types (#1345)
* Add ConnectionError::Host variant and clean up some other variants * Migrate some ics03 handlers to return HostErrors * Revert "Migrate some ics03 handlers to return HostErrors" This reverts commit f48a1a6. * Add HostError variant to ChannelError * Remove HandlerError::Host variant * Map client handler HostErrors to ClientError::Host * Revert "Map client handler HostErrors to ClientError::Host" This reverts commit 247be8a. * Change ics02-client handlers to return ClientError * Change ics03-connection handlers to return ConnectionError * Change ics04-channel handlers to return ChannelError * Make necessary changes in ics25 dispatch * imp: misc improvements and fixes * imp: use derive_more::From for ChannelError * fix: rename InvalidClientState under ConnectionError * nit: remove redundant import --------- Co-authored-by: Farhad Shabani <[email protected]>
1 parent a4e409a commit 4aecaec

File tree

35 files changed

+362
-493
lines changed

35 files changed

+362
-493
lines changed

ibc-apps/ics20-transfer/src/module.rs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use ibc_core::channel::types::acknowledgement::{Acknowledgement, Acknowledgement
66
use ibc_core::channel::types::channel::{Counterparty, Order};
77
use ibc_core::channel::types::packet::Packet;
88
use ibc_core::channel::types::Version;
9-
use ibc_core::handler::types::error::HandlerError;
109
use ibc_core::host::types::error::DecodingError;
1110
use ibc_core::host::types::identifiers::{ChannelId, ConnectionId, PortId};
1211
use ibc_core::primitives::prelude::*;
@@ -42,9 +41,7 @@ pub fn on_chan_open_init_validate(
4241
}
4342

4443
if !version.is_empty() {
45-
version
46-
.verify_is_expected(Version::new(VERSION.to_string()))
47-
.map_err(HandlerError::from)?;
44+
version.verify_is_expected(Version::new(VERSION.to_string()))?;
4845
}
4946

5047
Ok(())
@@ -78,9 +75,7 @@ pub fn on_chan_open_try_validate(
7875
});
7976
}
8077

81-
counterparty_version
82-
.verify_is_expected(Version::new(VERSION.to_string()))
83-
.map_err(HandlerError::from)?;
78+
counterparty_version.verify_is_expected(Version::new(VERSION.to_string()))?;
8479

8580
Ok(())
8681
}
@@ -103,9 +98,7 @@ pub fn on_chan_open_ack_validate(
10398
_channel_id: &ChannelId,
10499
counterparty_version: &Version,
105100
) -> Result<(), TokenTransferError> {
106-
counterparty_version
107-
.verify_is_expected(Version::new(VERSION.to_string()))
108-
.map_err(HandlerError::from)?;
101+
counterparty_version.verify_is_expected(Version::new(VERSION.to_string()))?;
109102

110103
Ok(())
111104
}

ibc-apps/ics20-transfer/types/src/error.rs

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,19 @@
22
use displaydoc::Display;
33
use ibc_core::channel::types::acknowledgement::StatusValue;
44
use ibc_core::channel::types::channel::Order;
5-
use ibc_core::handler::types::error::HandlerError;
5+
use ibc_core::channel::types::error::ChannelError;
66
use ibc_core::host::types::error::{DecodingError, HostError};
77
use ibc_core::host::types::identifiers::{ChannelId, PortId};
88
use ibc_core::primitives::prelude::*;
99

10-
#[derive(Display, Debug)]
10+
#[derive(Display, Debug, derive_more::From)]
1111
pub enum TokenTransferError {
1212
/// host error: `{0}`
13-
Handler(HandlerError),
13+
Host(HostError),
1414
/// decoding error: `{0}`
1515
Decoding(DecodingError),
16+
/// channel error: `{0}`
17+
Channel(ChannelError),
1618
/// missing destination channel `{channel_id}` on port `{port_id}`
1719
MissingDestinationChannel {
1820
port_id: PortId,
@@ -36,31 +38,14 @@ pub enum TokenTransferError {
3638
impl std::error::Error for TokenTransferError {
3739
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
3840
match &self {
39-
Self::Handler(e) => Some(e),
41+
Self::Host(e) => Some(e),
4042
Self::Decoding(e) => Some(e),
43+
Self::Channel(e) => Some(e),
4144
_ => None,
4245
}
4346
}
4447
}
4548

46-
impl From<HandlerError> for TokenTransferError {
47-
fn from(e: HandlerError) -> Self {
48-
Self::Handler(e)
49-
}
50-
}
51-
52-
impl From<HostError> for TokenTransferError {
53-
fn from(e: HostError) -> Self {
54-
Self::Handler(HandlerError::Host(e))
55-
}
56-
}
57-
58-
impl From<DecodingError> for TokenTransferError {
59-
fn from(e: DecodingError) -> Self {
60-
Self::Decoding(e)
61-
}
62-
}
63-
6449
impl From<TokenTransferError> for StatusValue {
6550
fn from(e: TokenTransferError) -> Self {
6651
StatusValue::new(e.to_string()).expect("error message must not be empty")

ibc-apps/ics721-nft-transfer/src/module.rs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use ibc_core::channel::types::acknowledgement::{Acknowledgement, Acknowledgement
33
use ibc_core::channel::types::channel::{Counterparty, Order};
44
use ibc_core::channel::types::packet::Packet;
55
use ibc_core::channel::types::Version;
6-
use ibc_core::handler::types::error::HandlerError;
76
use ibc_core::host::types::identifiers::{ChannelId, ConnectionId, PortId};
87
use ibc_core::primitives::prelude::*;
98
use ibc_core::primitives::Signer;
@@ -42,9 +41,7 @@ pub fn on_chan_open_init_validate(
4241
}
4342

4443
if !version.is_empty() {
45-
version
46-
.verify_is_expected(Version::new(VERSION.to_string()))
47-
.map_err(HandlerError::from)?;
44+
version.verify_is_expected(Version::new(VERSION.to_string()))?;
4845
}
4946

5047
Ok(())
@@ -78,9 +75,7 @@ pub fn on_chan_open_try_validate(
7875
});
7976
}
8077

81-
counterparty_version
82-
.verify_is_expected(Version::new(VERSION.to_string()))
83-
.map_err(HandlerError::from)?;
78+
counterparty_version.verify_is_expected(Version::new(VERSION.to_string()))?;
8479

8580
Ok(())
8681
}
@@ -103,10 +98,7 @@ pub fn on_chan_open_ack_validate(
10398
_channel_id: &ChannelId,
10499
counterparty_version: &Version,
105100
) -> Result<(), NftTransferError> {
106-
counterparty_version
107-
.verify_is_expected(Version::new(VERSION.to_string()))
108-
.map_err(HandlerError::from)?;
109-
101+
counterparty_version.verify_is_expected(Version::new(VERSION.to_string()))?;
110102
Ok(())
111103
}
112104

ibc-apps/ics721-nft-transfer/types/src/error.rs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,17 @@ use derive_more::From;
33
use displaydoc::Display;
44
use ibc_core::channel::types::acknowledgement::StatusValue;
55
use ibc_core::channel::types::channel::Order;
6-
use ibc_core::handler::types::error::HandlerError;
6+
use ibc_core::channel::types::error::ChannelError;
77
use ibc_core::host::types::error::{DecodingError, HostError};
88
use ibc_core::host::types::identifiers::{ChannelId, PortId};
99
use ibc_core::primitives::prelude::*;
1010

1111
#[derive(Display, Debug, From)]
1212
pub enum NftTransferError {
13-
/// handler error: `{0}`
14-
Handler(HandlerError),
13+
/// host error: `{0}`
14+
Host(HostError),
15+
/// channel error: `{0}`
16+
Channel(ChannelError),
1517
/// decoding error: `{0}`
1618
Decoding(DecodingError),
1719
/// missing destination channel `{channel_id}` on port `{port_id}`
@@ -41,19 +43,14 @@ pub enum NftTransferError {
4143
impl std::error::Error for NftTransferError {
4244
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
4345
match &self {
44-
Self::Handler(e) => Some(e),
46+
Self::Channel(e) => Some(e),
47+
Self::Host(e) => Some(e),
4548
Self::Decoding(e) => Some(e),
4649
_ => None,
4750
}
4851
}
4952
}
5053

51-
impl From<HostError> for NftTransferError {
52-
fn from(e: HostError) -> Self {
53-
Self::Handler(HandlerError::Host(e))
54-
}
55-
}
56-
5754
impl From<NftTransferError> for StatusValue {
5855
fn from(err: NftTransferError) -> Self {
5956
StatusValue::new(err.to_string()).expect("error message must not be empty")

ibc-core/ics02-client/src/handler/create_client.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,12 @@ use ibc_core_client_types::error::ClientError;
55
use ibc_core_client_types::events::CreateClient;
66
use ibc_core_client_types::msgs::MsgCreateClient;
77
use ibc_core_client_types::Status;
8-
use ibc_core_handler_types::error::HandlerError;
98
use ibc_core_handler_types::events::{IbcEvent, MessageEvent};
109
use ibc_core_host::{ClientStateMut, ClientStateRef, ExecutionContext, ValidationContext};
1110
use ibc_primitives::prelude::*;
1211
use ibc_primitives::proto::Any;
1312

14-
pub fn validate<Ctx>(ctx: &Ctx, msg: MsgCreateClient) -> Result<(), HandlerError>
13+
pub fn validate<Ctx>(ctx: &Ctx, msg: MsgCreateClient) -> Result<(), ClientError>
1514
where
1615
Ctx: ValidationContext,
1716
<ClientStateRef<Ctx> as TryFrom<Any>>::Error: Into<ClientError>,
@@ -36,21 +35,21 @@ where
3635
let status = client_state.status(client_val_ctx, &client_id)?;
3736

3837
if status.is_frozen() {
39-
return Err(ClientError::UnexpectedStatus(Status::Frozen).into());
38+
return Err(ClientError::UnexpectedStatus(Status::Frozen));
4039
};
4140

4241
let host_timestamp = ctx.host_timestamp()?;
4342

4443
client_state.verify_consensus_state(consensus_state, &host_timestamp)?;
4544

4645
if client_val_ctx.client_state(&client_id).is_ok() {
47-
return Err(ClientError::DuplicateClientState(client_id).into());
46+
return Err(ClientError::DuplicateClientState(client_id));
4847
};
4948

5049
Ok(())
5150
}
5251

53-
pub fn execute<Ctx>(ctx: &mut Ctx, msg: MsgCreateClient) -> Result<(), HandlerError>
52+
pub fn execute<Ctx>(ctx: &mut Ctx, msg: MsgCreateClient) -> Result<(), ClientError>
5453
where
5554
Ctx: ExecutionContext,
5655
<ClientStateMut<Ctx> as TryFrom<Any>>::Error: Into<ClientError>,

ibc-core/ics02-client/src/handler/recover_client.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,14 @@
33
use ibc_core_client_context::prelude::*;
44
use ibc_core_client_types::error::ClientError;
55
use ibc_core_client_types::msgs::MsgRecoverClient;
6-
use ibc_core_handler_types::error::HandlerError;
76
use ibc_core_host::types::path::ClientConsensusStatePath;
87
use ibc_core_host::{ExecutionContext, ValidationContext};
98

109
/// Performs the validation steps associated with the client recovery process. This
1110
/// includes validating that the parameters of the subject and substitute clients match,
1211
/// as well as validating that the substitute client *is* active and that the subject
1312
/// client is *not* active.
14-
pub fn validate<Ctx>(ctx: &Ctx, msg: MsgRecoverClient) -> Result<(), HandlerError>
13+
pub fn validate<Ctx>(ctx: &Ctx, msg: MsgRecoverClient) -> Result<(), ClientError>
1514
where
1615
Ctx: ValidationContext,
1716
{
@@ -33,8 +32,7 @@ where
3332
return Err(ClientError::NotAllowedClientRecoveryHeights {
3433
subject_height,
3534
substitute_height,
36-
}
37-
.into());
35+
});
3836
}
3937

4038
substitute_client_state
@@ -62,7 +60,7 @@ where
6260
/// - copying the substitute client's consensus state as the subject's consensus state
6361
/// - setting the subject client's processed height and processed time values to match the substitute client's
6462
/// - setting the subject client's latest height, trusting period, and chain ID values to match the substitute client's
65-
pub fn execute<Ctx>(ctx: &mut Ctx, msg: MsgRecoverClient) -> Result<(), HandlerError>
63+
pub fn execute<Ctx>(ctx: &mut Ctx, msg: MsgRecoverClient) -> Result<(), ClientError>
6664
where
6765
Ctx: ExecutionContext,
6866
{

ibc-core/ics02-client/src/handler/update_client.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,13 @@ use ibc_core_client_types::error::ClientError;
55
use ibc_core_client_types::events::{ClientMisbehaviour, UpdateClient};
66
use ibc_core_client_types::msgs::MsgUpdateOrMisbehaviour;
77
use ibc_core_client_types::UpdateKind;
8-
use ibc_core_handler_types::error::HandlerError;
98
use ibc_core_handler_types::events::{IbcEvent, MessageEvent};
109
use ibc_core_host::types::error::HostError;
1110
use ibc_core_host::{ExecutionContext, ValidationContext};
1211
use ibc_primitives::prelude::*;
1312
use ibc_primitives::ToVec;
1413

15-
pub fn validate<Ctx>(ctx: &Ctx, msg: MsgUpdateOrMisbehaviour) -> Result<(), HandlerError>
14+
pub fn validate<Ctx>(ctx: &Ctx, msg: MsgUpdateOrMisbehaviour) -> Result<(), ClientError>
1615
where
1716
Ctx: ValidationContext,
1817
{
@@ -36,7 +35,7 @@ where
3635
Ok(())
3736
}
3837

39-
pub fn execute<Ctx>(ctx: &mut Ctx, msg: MsgUpdateOrMisbehaviour) -> Result<(), HandlerError>
38+
pub fn execute<Ctx>(ctx: &mut Ctx, msg: MsgUpdateOrMisbehaviour) -> Result<(), ClientError>
4039
where
4140
Ctx: ExecutionContext,
4241
{
@@ -67,8 +66,7 @@ where
6766
if !matches!(update_kind, UpdateKind::UpdateClient) {
6867
return Err(ClientError::FailedMisbehaviourHandling {
6968
description: "misbehaviour submitted, but none found".to_string(),
70-
}
71-
.into());
69+
});
7270
}
7371

7472
let header = client_message;

ibc-core/ics02-client/src/handler/upgrade_client.rs

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
//! Protocol logic specific to processing ICS2 messages of type `MsgUpgradeAnyClient`.
22
//!
33
use ibc_core_client_context::prelude::*;
4+
use ibc_core_client_types::error::ClientError;
45
use ibc_core_client_types::events::UpgradeClient;
56
use ibc_core_client_types::msgs::MsgUpgradeClient;
6-
use ibc_core_handler_types::error::HandlerError;
77
use ibc_core_handler_types::events::{IbcEvent, MessageEvent};
8-
use ibc_core_host::types::error::HostError;
98
use ibc_core_host::types::path::ClientConsensusStatePath;
109
use ibc_core_host::{ExecutionContext, ValidationContext};
1110
use ibc_primitives::prelude::*;
1211

13-
pub fn validate<Ctx>(ctx: &Ctx, msg: MsgUpgradeClient) -> Result<(), HandlerError>
12+
pub fn validate<Ctx>(ctx: &Ctx, msg: MsgUpgradeClient) -> Result<(), ClientError>
1413
where
1514
Ctx: ValidationContext,
1615
{
@@ -36,15 +35,7 @@ where
3635
old_client_state.latest_height().revision_number(),
3736
old_client_state.latest_height().revision_height(),
3837
);
39-
let old_consensus_state = client_val_ctx
40-
.consensus_state(&old_client_cons_state_path)
41-
.map_err(|_| {
42-
HostError::missing_state(format!(
43-
"missing consensus state for client {} at height {}",
44-
client_id,
45-
old_client_state.latest_height()
46-
))
47-
})?;
38+
let old_consensus_state = client_val_ctx.consensus_state(&old_client_cons_state_path)?;
4839

4940
// Validate the upgraded client state and consensus state and verify proofs against the root
5041
old_client_state.verify_upgrade_client(
@@ -58,7 +49,7 @@ where
5849
Ok(())
5950
}
6051

61-
pub fn execute<Ctx>(ctx: &mut Ctx, msg: MsgUpgradeClient) -> Result<(), HandlerError>
52+
pub fn execute<Ctx>(ctx: &mut Ctx, msg: MsgUpgradeClient) -> Result<(), ClientError>
6253
where
6354
Ctx: ExecutionContext,
6455
{

ibc-core/ics03-connection/src/delay.rs

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@ use ibc_core_client::context::ClientValidationContext;
22
use ibc_core_client::types::Height;
33
use ibc_core_connection_types::error::ConnectionError;
44
use ibc_core_connection_types::ConnectionEnd;
5-
use ibc_core_handler_types::error::HandlerError;
65
use ibc_core_host::ValidationContext;
76

87
pub fn verify_conn_delay_passed<Ctx>(
98
ctx: &Ctx,
109
packet_proof_height: Height,
1110
connection_end: &ConnectionEnd,
12-
) -> Result<(), HandlerError>
11+
) -> Result<(), ConnectionError>
1312
where
1413
Ctx: ValidationContext,
1514
{
@@ -31,23 +30,19 @@ where
3130
let earliest_valid_time = (last_client_update.0 + conn_delay_time_period)
3231
.map_err(ConnectionError::OverflowedTimestamp)?;
3332
if current_host_time < earliest_valid_time {
34-
return Err(HandlerError::Connection(
35-
ConnectionError::InsufficientTimeElapsed {
36-
current_host_time,
37-
earliest_valid_time,
38-
},
39-
));
33+
return Err(ConnectionError::InsufficientTimeElapsed {
34+
current_host_time,
35+
earliest_valid_time,
36+
});
4037
}
4138

4239
// Verify that the current host chain height is later than the last client update height
4340
let earliest_valid_height = last_client_update.1.add(conn_delay_height_period);
4441
if current_host_height < earliest_valid_height {
45-
return Err(HandlerError::Connection(
46-
ConnectionError::InsufficientBlocksElapsed {
47-
current_host_height,
48-
earliest_valid_height,
49-
},
50-
));
42+
return Err(ConnectionError::InsufficientBlocksElapsed {
43+
current_host_height,
44+
earliest_valid_height,
45+
});
5146
};
5247

5348
Ok(())

0 commit comments

Comments
 (0)