Skip to content

Commit

Permalink
Use newly-define HostError helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
seanchen1991 committed Sep 13, 2024
1 parent 96b4797 commit ee93e43
Show file tree
Hide file tree
Showing 9 changed files with 180 additions and 216 deletions.
6 changes: 3 additions & 3 deletions ibc-core/ics02-client/src/handler/update_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ where
{
let event = {
let consensus_height =
consensus_heights.first().ok_or(HostError::MissingState {
description: "missing updated height in client update state".to_string(),
})?;
consensus_heights.first().ok_or(HostError::missing_state(
"missing updated height in client update state".to_string(),
))?;

IbcEvent::UpdateClient(UpdateClient::new(
client_id,
Expand Down
6 changes: 3 additions & 3 deletions ibc-core/ics02-client/src/handler/upgrade_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ where
);
let old_consensus_state = client_val_ctx
.consensus_state(&old_client_cons_state_path)
.map_err(|_| HostError::MissingState {
description: format!(
.map_err(|_| {
HostError::missing_state(format!(
"missing consensus state for client {} at height {}",
client_id,
old_client_state.latest_height()
),
))

Check warning on line 46 in ibc-core/ics02-client/src/handler/upgrade_client.rs

View check run for this annotation

Codecov / codecov/patch

ibc-core/ics02-client/src/handler/upgrade_client.rs#L42-L46

Added lines #L42 - L46 were not covered by tests
})?;

// Validate the upgraded client state and consensus state and verify proofs against the root
Expand Down
80 changes: 33 additions & 47 deletions ibc-core/ics24-host/cosmos/src/validate_self_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,53 +25,45 @@ pub trait ValidateSelfClientContext {
})?;

Check warning on line 25 in ibc-core/ics24-host/cosmos/src/validate_self_client.rs

View check run for this annotation

Codecov / codecov/patch

ibc-core/ics24-host/cosmos/src/validate_self_client.rs#L23-L25

Added lines #L23 - L25 were not covered by tests

if client_state_of_host_on_counterparty.is_frozen() {
return Err(HostError::UnexpectedData {
return Err(HostError::UnexpectedState {
description: "client unexpectedly frozen".to_string(),
});

Check warning on line 30 in ibc-core/ics24-host/cosmos/src/validate_self_client.rs

View check run for this annotation

Codecov / codecov/patch

ibc-core/ics24-host/cosmos/src/validate_self_client.rs#L28-L30

Added lines #L28 - L30 were not covered by tests
}

let self_chain_id = self.chain_id();

Check warning on line 34 in ibc-core/ics24-host/cosmos/src/validate_self_client.rs

View check run for this annotation

Codecov / codecov/patch

ibc-core/ics24-host/cosmos/src/validate_self_client.rs#L34

Added line #L34 was not covered by tests
if self_chain_id != &client_state_of_host_on_counterparty.chain_id {
return Err(HostError::InvalidState {
description: format!(
"invalid chain ID: expected {}, actual {}",
self_chain_id, client_state_of_host_on_counterparty.chain_id
),
});
return Err(HostError::invalid_state(format!(
"invalid chain ID: expected {}, actual {}",
self_chain_id, client_state_of_host_on_counterparty.chain_id
)));

Check warning on line 39 in ibc-core/ics24-host/cosmos/src/validate_self_client.rs

View check run for this annotation

Codecov / codecov/patch

ibc-core/ics24-host/cosmos/src/validate_self_client.rs#L36-L39

Added lines #L36 - L39 were not covered by tests
}

let latest_height = client_state_of_host_on_counterparty.latest_height;
let self_revision_number = self_chain_id.revision_number();

Check warning on line 44 in ibc-core/ics24-host/cosmos/src/validate_self_client.rs

View check run for this annotation

Codecov / codecov/patch

ibc-core/ics24-host/cosmos/src/validate_self_client.rs#L44

Added line #L44 was not covered by tests
if self_revision_number != latest_height.revision_number() {
return Err(HostError::InvalidState {
description: format!(
"mismatched client revision numbers; expected {}, actual {}",
self_revision_number,
latest_height.revision_number()
),
});
return Err(HostError::invalid_state(format!(
"mismatched client revision numbers; expected {}, actual {}",
self_revision_number,
latest_height.revision_number()
)));

Check warning on line 50 in ibc-core/ics24-host/cosmos/src/validate_self_client.rs

View check run for this annotation

Codecov / codecov/patch

ibc-core/ics24-host/cosmos/src/validate_self_client.rs#L46-L50

Added lines #L46 - L50 were not covered by tests
}

if latest_height >= self.host_current_height() {
return Err(HostError::InvalidState {
description: format!(
"client latest height {} should be less than chain height {}",
latest_height,
self.host_current_height()
),
});
return Err(HostError::invalid_state(format!(
"client latest height {} should be less than chain height {}",
latest_height,
self.host_current_height()
)));

Check warning on line 58 in ibc-core/ics24-host/cosmos/src/validate_self_client.rs

View check run for this annotation

Codecov / codecov/patch

ibc-core/ics24-host/cosmos/src/validate_self_client.rs#L54-L58

Added lines #L54 - L58 were not covered by tests
}

if self.proof_specs() != &client_state_of_host_on_counterparty.proof_specs {
return Err(HostError::InvalidState {
description: format!(
"invalid client proof specs; expected {:?}, actual {:?}",
self.proof_specs(),
client_state_of_host_on_counterparty.proof_specs
),
});
return Err(HostError::invalid_state(format!(
"invalid client proof specs; expected {:?}, actual {:?}",
self.proof_specs(),
client_state_of_host_on_counterparty.proof_specs
)));

Check warning on line 66 in ibc-core/ics24-host/cosmos/src/validate_self_client.rs

View check run for this annotation

Codecov / codecov/patch

ibc-core/ics24-host/cosmos/src/validate_self_client.rs#L62-L66

Added lines #L62 - L66 were not covered by tests
}

let _ = {
Expand All @@ -81,41 +73,35 @@ pub trait ValidateSelfClientContext {
trust_level.numerator(),
trust_level.denominator(),
)
.map_err(|e| HostError::InvalidState {
description: e.to_string(),
})?
.map_err(|e| HostError::invalid_state(e.to_string()))?

Check warning on line 76 in ibc-core/ics24-host/cosmos/src/validate_self_client.rs

View check run for this annotation

Codecov / codecov/patch

ibc-core/ics24-host/cosmos/src/validate_self_client.rs#L76

Added line #L76 was not covered by tests
};

if self.unbonding_period() != client_state_of_host_on_counterparty.unbonding_period {
return Err(HostError::InvalidState {
description: format!(
"invalid unbonding period; expected {:?}, actual {:?}",
self.unbonding_period(),
client_state_of_host_on_counterparty.unbonding_period,
),
});
return Err(HostError::invalid_state(format!(
"invalid unbonding period; expected {:?}, actual {:?}",
self.unbonding_period(),
client_state_of_host_on_counterparty.unbonding_period,
)));

Check warning on line 84 in ibc-core/ics24-host/cosmos/src/validate_self_client.rs

View check run for this annotation

Codecov / codecov/patch

ibc-core/ics24-host/cosmos/src/validate_self_client.rs#L80-L84

Added lines #L80 - L84 were not covered by tests
}

if client_state_of_host_on_counterparty.unbonding_period
< client_state_of_host_on_counterparty.trusting_period
{
return Err(HostError::InvalidState { description: format!(
return Err(HostError::invalid_state(format!(
"invalid counterparty client state: unbonding period must be greater than trusting period; unbonding period ({:?}) < trusting period ({:?})",

Check warning on line 91 in ibc-core/ics24-host/cosmos/src/validate_self_client.rs

View check run for this annotation

Codecov / codecov/patch

ibc-core/ics24-host/cosmos/src/validate_self_client.rs#L90-L91

Added lines #L90 - L91 were not covered by tests
client_state_of_host_on_counterparty.unbonding_period,
client_state_of_host_on_counterparty.trusting_period
)});
)));

Check warning on line 94 in ibc-core/ics24-host/cosmos/src/validate_self_client.rs

View check run for this annotation

Codecov / codecov/patch

ibc-core/ics24-host/cosmos/src/validate_self_client.rs#L94

Added line #L94 was not covered by tests
}

if !client_state_of_host_on_counterparty.upgrade_path.is_empty()
&& self.upgrade_path() != client_state_of_host_on_counterparty.upgrade_path
{
return Err(HostError::InvalidState {
description: format!(
"invalid upgrade path; expected {:?}, actual {:?}",
self.upgrade_path(),
client_state_of_host_on_counterparty.upgrade_path
),
});
return Err(HostError::invalid_state(format!(
"invalid upgrade path; expected {:?}, actual {:?}",
self.upgrade_path(),
client_state_of_host_on_counterparty.upgrade_path
)));

Check warning on line 104 in ibc-core/ics24-host/cosmos/src/validate_self_client.rs

View check run for this annotation

Codecov / codecov/patch

ibc-core/ics24-host/cosmos/src/validate_self_client.rs#L100-L104

Added lines #L100 - L104 were not covered by tests
}

Ok(())
Expand Down
4 changes: 1 addition & 3 deletions ibc-core/ics24-host/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,7 @@ pub trait ValidationContext {
&self.get_compatible_versions(),
counterparty_candidate_versions,
)
.map_err(|e| HostError::MissingState {
description: e.to_string(),
})
.map_err(|e| HostError::missing_state(e.to_string()))
}

/// Returns the `ChannelEnd` for the given `port_id` and `chan_id`.
Expand Down
4 changes: 2 additions & 2 deletions ibc-core/ics24-host/types/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ pub enum HostError {
FailedToValidateClient { description: String },
/// non-existent type: `{description}`
NonexistentType { description: String },
/// unexpected data: `{description}`
UnexpectedData { description: String },
/// unexpected state: `{description}`
UnexpectedState { description: String },
/// unknown resource: `{description}`
UnknownResource { description: String },
/// other error: `{description}`
Expand Down
71 changes: 29 additions & 42 deletions ibc-testkit/src/testapp/ibc/core/client_ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,7 @@ where
consensus_path.revision_number,
consensus_path.revision_height,
)
.map_err(|e| HostError::InvalidState {
description: e.to_string(),
})
.map_err(|e| HostError::invalid_state(e.to_string()))
})
.collect::<Result<Vec<_>, _>>()
}
Expand Down Expand Up @@ -106,18 +104,16 @@ where
.map(|path| {
self.consensus_state_store
.get(StoreHeight::Pending, &path)
.ok_or_else(|| HostError::FailedToRetrieveFromStore {
description: format!(
.ok_or_else(|| {
HostError::failed_to_retrieve_from_store(format!(
"missing consensus state for client {} at height {}",
client_id.clone(),
*height
),
))

Check warning on line 112 in ibc-testkit/src/testapp/ibc/core/client_ctx.rs

View check run for this annotation

Codecov / codecov/patch

ibc-testkit/src/testapp/ibc/core/client_ctx.rs#L108-L112

Added lines #L108 - L112 were not covered by tests
})
})
.transpose()
.map_err(|e| HostError::MissingState {
description: e.to_string(),
})?;
.map_err(|e| HostError::missing_state(e.to_string()))?;

Ok(consensus_state)
}
Expand Down Expand Up @@ -145,18 +141,16 @@ where
.map(|path| {
self.consensus_state_store
.get(StoreHeight::Pending, &path)
.ok_or_else(|| HostError::FailedToRetrieveFromStore {
description: format!(
.ok_or_else(|| {
HostError::failed_to_retrieve_from_store(format!(
"missing consensus state for client {} at height {}",
client_id.clone(),
*height
),
))

Check warning on line 149 in ibc-testkit/src/testapp/ibc/core/client_ctx.rs

View check run for this annotation

Codecov / codecov/patch

ibc-testkit/src/testapp/ibc/core/client_ctx.rs#L145-L149

Added lines #L145 - L149 were not covered by tests
})
})
.transpose()
.map_err(|e| HostError::MissingState {
description: e.to_string(),
})?;
.map_err(|e| HostError::missing_state(e.to_string()))?;

Ok(consensus_state)
}
Expand All @@ -172,9 +166,10 @@ where
fn client_state(&self, client_id: &ClientId) -> Result<Self::ClientStateRef, HostError> {
self.client_state_store
.get(StoreHeight::Pending, &ClientStatePath(client_id.clone()))
.ok_or(HostError::FailedToRetrieveFromStore {
description: format!("missing client state for client {}", client_id.clone()),
})
.ok_or(HostError::failed_to_retrieve_from_store(format!(
"missing client state for client {}",
client_id.clone()
)))
}

fn consensus_state(
Expand All @@ -185,19 +180,15 @@ where
client_cons_state_path.revision_number,
client_cons_state_path.revision_height,
)
.map_err(|e| HostError::InvalidState {
description: e.to_string(),
})?;
.map_err(|e| HostError::invalid_state(e.to_string()))?;
let consensus_state = self
.consensus_state_store
.get(StoreHeight::Pending, client_cons_state_path)
.ok_or(HostError::FailedToRetrieveFromStore {
description: format!(
"missing consensus state for client {} at height {}",
client_cons_state_path.client_id.clone(),
height
),
})?;
.ok_or(HostError::failed_to_retrieve_from_store(format!(
"missing consensus state for client {} at height {}",
client_cons_state_path.client_id.clone(),
height
)))?;

Ok(consensus_state)
}
Expand All @@ -217,13 +208,11 @@ where
let processed_timestamp = self
.client_processed_times
.get(StoreHeight::Pending, &client_update_time_path)
.ok_or(HostError::FailedToRetrieveFromStore {
description: format!(
"missing client update metadata for client {} at height {}",
client_id.clone(),
*height,
),
})?;
.ok_or(HostError::failed_to_retrieve_from_store(format!(
"missing client update metadata for client {} at height {}",
client_id.clone(),
*height,
)))?;
let client_update_height_path = ClientUpdateHeightPath::new(
client_id.clone(),
height.revision_number(),
Expand All @@ -232,13 +221,11 @@ where
let processed_height = self
.client_processed_heights
.get(StoreHeight::Pending, &client_update_height_path)
.ok_or(HostError::FailedToRetrieveFromStore {
description: format!(
"missing client update metadata for client {} at height {}",
client_id.clone(),
*height,
),
})?;
.ok_or(HostError::failed_to_retrieve_from_store(format!(
"missing client update metadata for client {} at height {}",
client_id.clone(),
*height,
)))?;

Ok((processed_timestamp, processed_height))
}
Expand Down
Loading

0 comments on commit ee93e43

Please sign in to comment.