Skip to content

Commit

Permalink
refactor: nits from nightly clippy (#962)
Browse files Browse the repository at this point in the history
* use enum tuple variants directly

* use first() over get(0)

* use infallible conversion

* rm redundant export

* cargo fmt
  • Loading branch information
rnbguy authored Nov 14, 2023
1 parent fe6b5b0 commit 95fc7d7
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 82 deletions.
34 changes: 18 additions & 16 deletions crates/ibc-testkit/src/testapp/ibc/core/core_ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,26 +144,28 @@ impl ValidationContext for MockContext {
let self_chain_id = &self.host_chain_id;
let self_revision_number = self_chain_id.revision_number();
if self_revision_number != mock_client_state.latest_height().revision_number() {
return Err(ConnectionError::InvalidClientState {
reason: format!(
"client is not in the same revision as the chain. expected: {}, got: {}",
self_revision_number,
mock_client_state.latest_height().revision_number()
),
})
.map_err(ContextError::ConnectionError);
return Err(ContextError::ConnectionError(
ConnectionError::InvalidClientState {
reason: format!(
"client is not in the same revision as the chain. expected: {}, got: {}",
self_revision_number,
mock_client_state.latest_height().revision_number()
),
},
));
}

let host_current_height = self.latest_height().increment();
if mock_client_state.latest_height() >= host_current_height {
return Err(ConnectionError::InvalidClientState {
reason: format!(
"client has latest height {} greater than or equal to chain height {}",
mock_client_state.latest_height(),
host_current_height
),
})
.map_err(ContextError::ConnectionError);
return Err(ContextError::ConnectionError(
ConnectionError::InvalidClientState {
reason: format!(
"client has latest height {} greater than or equal to chain height {}",
mock_client_state.latest_height(),
host_current_height
),
},
));
}

Ok(())
Expand Down
1 change: 0 additions & 1 deletion crates/ibc-testkit/src/testapp/ibc/core/router/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
mod context;
mod types;

pub use context::*;
pub use types::*;
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub fn dummy_raw_msg_create_client() -> MsgCreateClient {

MsgCreateClient {
client_state: Some(Any::from(tm_client_state)),
consensus_state: Some(Any::from(TmConsensusState::try_from(tm_header).unwrap())),
consensus_state: Some(Any::from(TmConsensusState::from(tm_header))),
signer: dummy_bech32_account(),
}
}
2 changes: 1 addition & 1 deletion crates/ibc/src/applications/transfer/msgs/transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,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(PacketError::MissingTimeout).map_err(ContextError::from)?;
return Err(ContextError::from(PacketError::MissingTimeout))?;
}

Ok(MsgTransfer {
Expand Down
2 changes: 1 addition & 1 deletion crates/ibc/src/core/ics02_client/handler/update_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ where

{
let event = {
let consensus_height = consensus_heights.get(0).ok_or(ClientError::Other {
let consensus_height = consensus_heights.first().ok_or(ClientError::Other {
description: "client update state returned no updated height".to_string(),
})?;

Expand Down
22 changes: 12 additions & 10 deletions crates/ibc/src/core/ics03_connection/delay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,23 @@ where
let earliest_valid_time = (last_client_update_time + conn_delay_time_period)
.map_err(ConnectionError::TimestampOverflow)?;
if current_host_time < earliest_valid_time {
return Err(ConnectionError::NotEnoughTimeElapsed {
current_host_time,
earliest_valid_time,
})
.map_err(ContextError::ConnectionError);
return Err(ContextError::ConnectionError(
ConnectionError::NotEnoughTimeElapsed {
current_host_time,
earliest_valid_time,
},
));
}

// Verify that the current host chain height is later than the last client update height
let earliest_valid_height = last_client_update_height.add(conn_delay_height_period);
if current_host_height < earliest_valid_height {
return Err(ConnectionError::NotEnoughBlocksElapsed {
current_host_height,
earliest_valid_height,
})
.map_err(ContextError::ConnectionError);
return Err(ContextError::ConnectionError(
ConnectionError::NotEnoughBlocksElapsed {
current_host_height,
earliest_valid_height,
},
));
};

Ok(())
Expand Down
4 changes: 2 additions & 2 deletions crates/ibc/src/core/ics23_commitment/merkle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,10 @@ impl MerkleProof {
// verify the absence of key in lowest subtree
let proof = self
.proofs
.get(0)
.first()
.ok_or(CommitmentError::InvalidMerkleProof)?;
let spec = ics23_specs
.get(0)
.first()
.ok_or(CommitmentError::InvalidMerkleProof)?;
// keys are represented from root-to-leaf
let key = keys
Expand Down
105 changes: 55 additions & 50 deletions crates/ibc/src/hosts/tendermint/validate_self_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,47 +38,51 @@ pub trait ValidateSelfClientContext {

let self_chain_id = self.chain_id();
if self_chain_id != &tm_client_state.chain_id {
return Err(ConnectionError::InvalidClientState {
reason: format!(
"invalid chain-id. expected: {}, got: {}",
self_chain_id, tm_client_state.chain_id
),
})
.map_err(ContextError::ConnectionError);
return Err(ContextError::ConnectionError(
ConnectionError::InvalidClientState {
reason: format!(
"invalid chain-id. expected: {}, got: {}",
self_chain_id, tm_client_state.chain_id
),
},
));
}

let self_revision_number = self_chain_id.revision_number();
if self_revision_number != tm_client_state.latest_height().revision_number() {
return Err(ConnectionError::InvalidClientState {
reason: format!(
"client is not in the same revision as the chain. expected: {}, got: {}",
self_revision_number,
tm_client_state.latest_height().revision_number()
),
})
.map_err(ContextError::ConnectionError);
return Err(ContextError::ConnectionError(
ConnectionError::InvalidClientState {
reason: format!(
"client is not in the same revision as the chain. expected: {}, got: {}",
self_revision_number,
tm_client_state.latest_height().revision_number()
),
},
));
}

if tm_client_state.latest_height() >= self.host_current_height() {
return Err(ConnectionError::InvalidClientState {
reason: format!(
"client has latest height {} greater than or equal to chain height {}",
tm_client_state.latest_height(),
self.host_current_height()
),
})
.map_err(ContextError::ConnectionError);
return Err(ContextError::ConnectionError(
ConnectionError::InvalidClientState {
reason: format!(
"client has latest height {} greater than or equal to chain height {}",
tm_client_state.latest_height(),
self.host_current_height()
),
},
));
}

if self.proof_specs() != &tm_client_state.proof_specs {
return Err(ConnectionError::InvalidClientState {
reason: format!(
"client has invalid proof specs. expected: {:?}, got: {:?}",
self.proof_specs(),
tm_client_state.proof_specs
),
})
.map_err(ContextError::ConnectionError);
return Err(ContextError::ConnectionError(
ConnectionError::InvalidClientState {
reason: format!(
"client has invalid proof specs. expected: {:?}, got: {:?}",
self.proof_specs(),
tm_client_state.proof_specs
),
},
));
}

let _ = {
Expand All @@ -94,36 +98,37 @@ pub trait ValidateSelfClientContext {
};

if self.unbonding_period() != tm_client_state.unbonding_period {
return Err(ConnectionError::InvalidClientState {
reason: format!(
"invalid unbonding period. expected: {:?}, got: {:?}",
self.unbonding_period(),
tm_client_state.unbonding_period,
),
})
.map_err(ContextError::ConnectionError);
return Err(ContextError::ConnectionError(
ConnectionError::InvalidClientState {
reason: format!(
"invalid unbonding period. expected: {:?}, got: {:?}",
self.unbonding_period(),
tm_client_state.unbonding_period,
),
},
));
}

if tm_client_state.unbonding_period < tm_client_state.trusting_period {
return Err(ConnectionError::InvalidClientState{ reason: format!(
return Err(ContextError::ConnectionError(ConnectionError::InvalidClientState{ reason: format!(
"unbonding period must be greater than trusting period. unbonding period ({:?}) < trusting period ({:?})",
tm_client_state.unbonding_period,
tm_client_state.trusting_period
)})
.map_err(ContextError::ConnectionError);
)}));
}

if !tm_client_state.upgrade_path.is_empty()
&& self.upgrade_path() != tm_client_state.upgrade_path
{
return Err(ConnectionError::InvalidClientState {
reason: format!(
"invalid upgrade path. expected: {:?}, got: {:?}",
self.upgrade_path(),
tm_client_state.upgrade_path
),
})
.map_err(ContextError::ConnectionError);
return Err(ContextError::ConnectionError(
ConnectionError::InvalidClientState {
reason: format!(
"invalid upgrade path. expected: {:?}, got: {:?}",
self.upgrade_path(),
tm_client_state.upgrade_path
),
},
));
}

Ok(())
Expand Down

0 comments on commit 95fc7d7

Please sign in to comment.