Skip to content

Commit

Permalink
Eliminate a bunch of redundant map_err calls
Browse files Browse the repository at this point in the history
  • Loading branch information
seanchen1991 committed Sep 19, 2024
1 parent 8add32b commit 0b00504
Show file tree
Hide file tree
Showing 30 changed files with 196 additions and 226 deletions.
4 changes: 1 addition & 3 deletions ibc-apps/ics20-transfer/types/src/amount.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,7 @@ impl FromStr for Amount {

fn from_str(s: &str) -> Result<Self, Self::Err> {
let amount = U256::from_dec_str(s).map_err(|e| {
DecodingError::invalid_raw_data(format!(
"invalid amount that could not be parsed as a U256: {e}"
))
DecodingError::invalid_raw_data(format!("amount could not be parsed as a U256: {e}"))
})?;

Ok(Self(amount))
Expand Down
2 changes: 1 addition & 1 deletion ibc-apps/ics20-transfer/types/src/coin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ where
.all(|x| x.is_alphanumeric() || VALID_DENOM_CHARACTERS.contains(x))
})
.ok_or(DecodingError::invalid_raw_data(format!(
"invalid coin: {coin_str}"
"coin str: {coin_str}"
)))?;

Ok(Coin {
Expand Down
4 changes: 1 addition & 3 deletions ibc-apps/ics20-transfer/types/src/denom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,9 +218,7 @@ impl FromStr for TracePath {
remaining_parts
.is_none()
.then_some(trace_path)
.ok_or(DecodingError::invalid_raw_data(format!(
"invalid trace path: {s}"
)))
.ok_or(DecodingError::invalid_raw_data(format!("trace path: {s}")))
}
}

Expand Down
4 changes: 2 additions & 2 deletions ibc-apps/ics20-transfer/types/src/msgs/transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ impl TryFrom<RawMsgTransfer> for MsgTransfer {
// Packet timeout height and packet timeout timestamp cannot both be unset.
if !timeout_height_on_b.is_set() && !timeout_timestamp_on_b.is_set() {
return Err(DecodingError::missing_raw_data(
"missing timeout height or timeout timestamp",
"msg transfer timeout height or timeout timestamp",
));
}

Expand All @@ -65,7 +65,7 @@ impl TryFrom<RawMsgTransfer> for MsgTransfer {
packet_data: PacketData {
token: raw_msg
.token
.ok_or(DecodingError::missing_raw_data("missing token"))?
.ok_or(DecodingError::missing_raw_data("msg transfer token"))?
.try_into()?,
sender: raw_msg.sender.into(),
receiver: raw_msg.receiver.into(),
Expand Down
4 changes: 1 addition & 3 deletions ibc-apps/ics721-nft-transfer/types/src/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,9 +247,7 @@ impl FromStr for ClassUri {
fn from_str(class_uri: &str) -> Result<Self, Self::Err> {
match Uri::from_str(class_uri) {
Ok(uri) => Ok(Self(uri)),
Err(err) => Err(DecodingError::invalid_raw_data(format!(
"invalid class URI: {err}"
))),
Err(err) => Err(DecodingError::invalid_raw_data(format!("class URI: {err}"))),
}
}
}
Expand Down
10 changes: 4 additions & 6 deletions ibc-clients/ics07-tendermint/src/client_state/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ use ibc_client_tendermint_types::{
use ibc_core_client::context::prelude::*;
use ibc_core_client::types::error::ClientError;
use ibc_core_client::types::Height;
use ibc_core_host::types::error::DecodingError;
use ibc_core_host::types::identifiers::ClientId;
use ibc_core_host::types::path::{ClientConsensusStatePath, ClientStatePath};
use ibc_primitives::prelude::*;
use ibc_primitives::proto::Any;
use ibc_primitives::{prelude::*, TimestampError};

use super::ClientState;

Expand Down Expand Up @@ -340,10 +339,9 @@ where
let host_timestamp = ctx.host_timestamp()?.into_tm_time();

let tm_consensus_state_timestamp = tm_consensus_state.timestamp();
let tm_consensus_state_expiry =
(tm_consensus_state_timestamp + client_state.trusting_period).map_err(|e| {
DecodingError::invalid_raw_data(format!("failed to parse TmConsensusState: {e}"))
})?;
let tm_consensus_state_expiry = (tm_consensus_state_timestamp
+ client_state.trusting_period)
.map_err(|_| TimestampError::TimestampOverflow)?;

if tm_consensus_state_expiry > host_timestamp {
break;
Expand Down
49 changes: 25 additions & 24 deletions ibc-clients/ics07-tendermint/types/src/client_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,56 +236,58 @@ impl TryFrom<RawTmClientState> for ClientState {
let chain_id = ChainId::from_str(raw.chain_id.as_str())?;

let trust_level = {
let trust_level = raw
.trust_level
.ok_or(DecodingError::missing_raw_data("missing trust level"))?;
trust_level
.try_into()
.map_err(DecodingError::invalid_raw_data)?
let trust_level = raw.trust_level.ok_or(DecodingError::missing_raw_data(
"tm client state trust level",
))?;
trust_level.try_into()?
};

let trusting_period = raw
.trusting_period
.ok_or(DecodingError::missing_raw_data("missing trusting period"))?
.ok_or(DecodingError::missing_raw_data(
"tm client state trusting period",
))?
.try_into()
.map_err(|d| {
DecodingError::invalid_raw_data(format!("invalid trusting period: {d:?}"))
DecodingError::invalid_raw_data(format!("tm client state trusting period: {d:?}"))
})?;

let unbonding_period = raw
.unbonding_period
.ok_or(DecodingError::missing_raw_data("missing unbonding period"))?
.ok_or(DecodingError::missing_raw_data(
"tm client state unbonding period",
))?
.try_into()
.map_err(|d| {
DecodingError::invalid_raw_data(format!("invalid unbonding period: {d:?}"))
DecodingError::invalid_raw_data(format!("tm client state unbonding period: {d:?}"))
})?;

let max_clock_drift = raw
.max_clock_drift
.ok_or(DecodingError::missing_raw_data("missing max clock drift"))?
.ok_or(DecodingError::missing_raw_data(
"tm client state max clock drift",
))?
.try_into()
.map_err(|d| {
DecodingError::invalid_raw_data(format!("invalid max clock drift: {d:?}"))
DecodingError::invalid_raw_data(format!("tm client state max clock drift: {d:?}"))
})?;

let latest_height = raw
.latest_height
.ok_or(DecodingError::missing_raw_data("missing latest height"))?
.ok_or(DecodingError::missing_raw_data(
"tm client state latest height",
))?
.try_into()?;

let proof_specs = raw
.proof_specs
.try_into()
.map_err(|e| DecodingError::invalid_raw_data(format!("invalid proof specs: {e:?}")))?;
let proof_specs = raw.proof_specs.try_into()?;

// NOTE: In `RawClientState`, a `frozen_height` of `0` means "not
// frozen". See:
// https://github.com/cosmos/ibc-go/blob/8422d0c4c35ef970539466c5bdec1cd27369bab3/modules/light-clients/07-tendermint/types/client_state.go#L74
let frozen_height =
Height::try_from(raw.frozen_height.ok_or(DecodingError::MissingRawData {
description: "frozen height not set".to_string(),
})?)
.ok();
let frozen_height = Height::try_from(raw.frozen_height.ok_or(
DecodingError::missing_raw_data("tm client state frozen height"),
)?)
.ok();

// We use set this deprecated field just so that we can properly convert
// it back in its raw form
Expand Down Expand Up @@ -346,8 +348,7 @@ impl TryFrom<Any> for ClientState {

fn try_from(raw: Any) -> Result<Self, Self::Error> {
fn decode_client_state(value: &[u8]) -> Result<ClientState, DecodingError> {
let client_state =
Protobuf::<RawTmClientState>::decode(value).map_err(DecodingError::Protobuf)?;
let client_state = Protobuf::<RawTmClientState>::decode(value)?;
Ok(client_state)
}

Expand Down
21 changes: 9 additions & 12 deletions ibc-clients/ics07-tendermint/types/src/consensus_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,27 +51,24 @@ impl TryFrom<RawConsensusState> for ConsensusState {
fn try_from(raw: RawConsensusState) -> Result<Self, Self::Error> {
let proto_root = raw
.root
.ok_or(DecodingError::MissingRawData {
description: "no commitment root set".into(),
})?
.ok_or(DecodingError::missing_raw_data(
"consensus state commitment root",
))?
.hash;

let ibc_proto::google::protobuf::Timestamp { seconds, nanos } =
raw.timestamp.ok_or(DecodingError::MissingRawData {
description: "no timestamp set".into(),
})?;
let ibc_proto::google::protobuf::Timestamp { seconds, nanos } = raw
.timestamp
.ok_or(DecodingError::missing_raw_data("consensus state timestamp"))?;
// FIXME: shunts like this are necessary due to
// https://github.com/informalsystems/tendermint-rs/issues/1053
let proto_timestamp = tpb::Timestamp { seconds, nanos };
let timestamp = proto_timestamp
.try_into()
.map_err(|e| DecodingError::InvalidRawData {
description: format!("invalid timestamp: {e}"),
})?;
.map_err(|e| DecodingError::invalid_raw_data(format!("timestamp: {e}")))?;

let next_validators_hash = Hash::from_bytes(Algorithm::Sha256, &raw.next_validators_hash)
.map_err(|e| DecodingError::InvalidHash {
description: e.to_string(),
.map_err(|e| {
DecodingError::invalid_raw_data(format!("next validators hash: {e}"))
})?;

Ok(Self {
Expand Down
11 changes: 4 additions & 7 deletions ibc-clients/ics08-wasm/types/src/client_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,10 @@ impl TryFrom<RawClientState> for ClientState {
fn try_from(raw: RawClientState) -> Result<Self, Self::Error> {
let latest_height = raw
.latest_height
.ok_or(DecodingError::MissingRawData {
description: "latest height not set".to_string(),
})?
.try_into()
.map_err(|e| DecodingError::InvalidRawData {
description: format!("failed to decode latest height: {e}"),
})?;
.ok_or(DecodingError::missing_raw_data(
"client state latest height",
))?
.try_into()?;
Ok(Self {
data: raw.data,
checksum: raw.checksum,
Expand Down
11 changes: 10 additions & 1 deletion ibc-core/ics02-client/types/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use displaydoc::Display;
use ibc_core_commitment_types::error::CommitmentError;
use ibc_core_host_types::error::{DecodingError, HostError, IdentifierError};
use ibc_core_host_types::identifiers::ClientId;
use ibc_primitives::prelude::*;
use ibc_primitives::Timestamp;
use ibc_primitives::{prelude::*, TimestampError};

use crate::height::Height;
use crate::Status;
Expand All @@ -19,6 +19,8 @@ pub enum ClientError {
Upgrade(UpgradeClientError),
/// decoding error: {0}
Decoding(DecodingError),
/// timestamp error: {0}
Timestamp(TimestampError),
/// invalid trust threshold `{numerator}`/`{denominator}`
InvalidTrustThreshold { numerator: u64, denominator: u64 },
/// invalid client state type `{0}`
Expand Down Expand Up @@ -80,6 +82,12 @@ impl From<IdentifierError> for ClientError {
}
}

impl From<TimestampError> for ClientError {
fn from(e: TimestampError) -> Self {
Self::Timestamp(e)
}
}

#[cfg(feature = "std")]
impl std::error::Error for ClientError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
Expand All @@ -88,6 +96,7 @@ impl std::error::Error for ClientError {
Self::Decoding(e) => Some(e),
Self::Upgrade(e) => Some(e),
Self::Host(e) => Some(e),
Self::Timestamp(e) => Some(e),
_ => None,
}
}
Expand Down
Loading

0 comments on commit 0b00504

Please sign in to comment.