Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: introduce convenience Status::verify_is_active method #1005

Merged
merged 3 commits into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- Add ibc::core::client::types::Status::verify_active method.
mina86 marked this conversation as resolved.
Show resolved Hide resolved
([#1005](https://github.com/cosmos/ibc-rs/pull/1005))
9 changes: 3 additions & 6 deletions ibc-core/ics02-client/src/handler/update_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,9 @@ where
// Read client state from the host chain store. The client should already exist.
let client_state = ctx.client_state(&client_id)?;

{
let status = client_state.status(ctx.get_client_validation_context(), &client_id)?;
if !status.is_active() {
return Err(ClientError::ClientNotActive { status }.into());
}
}
client_state
.status(ctx.get_client_validation_context(), &client_id)?
.verify_active()?;

let client_message = msg.client_message();

Expand Down
9 changes: 3 additions & 6 deletions ibc-core/ics02-client/src/handler/upgrade_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,9 @@ where
let old_client_state = ctx.client_state(&client_id)?;

// Check if the client is active.
{
let status = old_client_state.status(ctx.get_client_validation_context(), &client_id)?;
if !status.is_active() {
return Err(ClientError::ClientNotActive { status }.into());
}
}
old_client_state
.status(ctx.get_client_validation_context(), &client_id)?
.verify_active()?;

// Read the latest consensus state from the host chain store.
let old_client_cons_state_path = ClientConsensusStatePath::new(
Expand Down
12 changes: 11 additions & 1 deletion ibc-core/ics02-client/types/src/status.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use core::fmt::{Debug, Display, Formatter};

use crate::error::ClientError;

/// `UpdateKind` represents the 2 ways that a client can be updated
/// in IBC: either through a `MsgUpdateClient`, or a `MsgSubmitMisbehaviour`.
#[derive(Clone, Debug, PartialEq, Eq)]
Expand All @@ -14,7 +16,7 @@ pub enum UpdateKind {
}

/// Represents the status of a client
#[derive(Debug, PartialEq, Eq)]
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Status {
/// The client is active and allowed to be used
Active,
Expand All @@ -38,6 +40,14 @@ impl Status {
pub fn is_expired(&self) -> bool {
*self == Status::Expired
}

/// Checks whether the status is active; returns `Err` if not.
pub fn verify_active(&self) -> Result<(), ClientError> {
mina86 marked this conversation as resolved.
Show resolved Hide resolved
match self.clone() {
Self::Active => Ok(()),
mina86 marked this conversation as resolved.
Show resolved Hide resolved
status => Err(ClientError::ClientNotActive { status }),
}
}
}

impl Display for Status {
Expand Down
11 changes: 3 additions & 8 deletions ibc-core/ics03-connection/src/handler/conn_open_ack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

use ibc_core_client::context::client_state::{ClientStateCommon, ClientStateValidation};
use ibc_core_client::context::consensus_state::ConsensusState;
use ibc_core_client::types::error::ClientError;
use ibc_core_connection_types::error::ConnectionError;
use ibc_core_connection_types::events::OpenAck;
use ibc_core_connection_types::msgs::MsgConnectionOpenAck;
Expand Down Expand Up @@ -56,13 +55,9 @@ where
{
let client_state_of_b_on_a = ctx_a.client_state(vars.client_id_on_a())?;

{
let status = client_state_of_b_on_a
.status(ctx_a.get_client_validation_context(), vars.client_id_on_a())?;
if !status.is_active() {
return Err(ClientError::ClientNotActive { status }.into());
}
}
client_state_of_b_on_a
.status(ctx_a.get_client_validation_context(), vars.client_id_on_a())?
.verify_active()?;
client_state_of_b_on_a.validate_proof_height(msg.proofs_height_on_b)?;

let client_cons_state_path_on_a = ClientConsensusStatePath::new(
Expand Down
11 changes: 3 additions & 8 deletions ibc-core/ics03-connection/src/handler/conn_open_confirm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

use ibc_core_client::context::client_state::{ClientStateCommon, ClientStateValidation};
use ibc_core_client::context::consensus_state::ConsensusState;
use ibc_core_client::types::error::ClientError;
use ibc_core_connection_types::error::ConnectionError;
use ibc_core_connection_types::events::OpenConfirm;
use ibc_core_connection_types::msgs::MsgConnectionOpenConfirm;
Expand Down Expand Up @@ -45,13 +44,9 @@ where
{
let client_state_of_a_on_b = ctx_b.client_state(client_id_on_b)?;

{
let status = client_state_of_a_on_b
.status(ctx_b.get_client_validation_context(), client_id_on_b)?;
if !status.is_active() {
return Err(ClientError::ClientNotActive { status }.into());
}
}
client_state_of_a_on_b
.status(ctx_b.get_client_validation_context(), client_id_on_b)?
.verify_active()?;
client_state_of_a_on_b.validate_proof_height(msg.proof_height_on_a)?;

let client_cons_state_path_on_b = ClientConsensusStatePath::new(
Expand Down
11 changes: 3 additions & 8 deletions ibc-core/ics03-connection/src/handler/conn_open_init.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
//! Protocol logic specific to ICS3 messages of type `MsgConnectionOpenInit`.
use ibc_core_client::context::client_state::ClientStateValidation;
use ibc_core_client::types::error::ClientError;
use ibc_core_connection_types::events::OpenInit;
use ibc_core_connection_types::msgs::MsgConnectionOpenInit;
use ibc_core_connection_types::{ConnectionEnd, Counterparty, State};
Expand All @@ -20,13 +19,9 @@ where
// An IBC client running on the local (host) chain should exist.
let client_state_of_b_on_a = ctx_a.client_state(&msg.client_id_on_a)?;

{
let status = client_state_of_b_on_a
.status(ctx_a.get_client_validation_context(), &msg.client_id_on_a)?;
if !status.is_active() {
return Err(ClientError::ClientNotActive { status }.into());
}
}
client_state_of_b_on_a
.status(ctx_a.get_client_validation_context(), &msg.client_id_on_a)?
.verify_active()?;

if let Some(version) = msg.version {
version.verify_is_supported(&ctx_a.get_compatible_versions())?;
Expand Down
11 changes: 3 additions & 8 deletions ibc-core/ics03-connection/src/handler/conn_open_try.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
//! Protocol logic specific to processing ICS3 messages of type `MsgConnectionOpenTry`.;
use ibc_core_client::context::client_state::{ClientStateCommon, ClientStateValidation};
use ibc_core_client::context::consensus_state::ConsensusState;
use ibc_core_client::types::error::ClientError;
use ibc_core_connection_types::error::ConnectionError;
use ibc_core_connection_types::events::OpenTry;
use ibc_core_connection_types::msgs::MsgConnectionOpenTry;
Expand Down Expand Up @@ -55,13 +54,9 @@ where
{
let client_state_of_a_on_b = ctx_b.client_state(vars.conn_end_on_b.client_id())?;

{
let status = client_state_of_a_on_b
.status(ctx_b.get_client_validation_context(), &msg.client_id_on_b)?;
if !status.is_active() {
return Err(ClientError::ClientNotActive { status }.into());
}
}
client_state_of_a_on_b
.status(ctx_b.get_client_validation_context(), &msg.client_id_on_b)?
.verify_active()?;
client_state_of_a_on_b.validate_proof_height(msg.proofs_height_on_a)?;

let client_cons_state_path_on_b = ClientConsensusStatePath::new(
Expand Down
11 changes: 3 additions & 8 deletions ibc-core/ics04-channel/src/handler/acknowledgement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use ibc_core_channel_types::events::AcknowledgePacket;
use ibc_core_channel_types::msgs::MsgAcknowledgement;
use ibc_core_client::context::client_state::{ClientStateCommon, ClientStateValidation};
use ibc_core_client::context::consensus_state::ConsensusState;
use ibc_core_client::types::error::ClientError;
use ibc_core_connection::delay::verify_conn_delay_passed;
use ibc_core_connection::types::State as ConnectionState;
use ibc_core_handler_types::error::ContextError;
Expand Down Expand Up @@ -178,13 +177,9 @@ where
let client_id_on_a = conn_end_on_a.client_id();
let client_state_of_b_on_a = ctx_a.client_state(client_id_on_a)?;

{
let status = client_state_of_b_on_a
.status(ctx_a.get_client_validation_context(), client_id_on_a)?;
if !status.is_active() {
return Err(ClientError::ClientNotActive { status }.into());
}
}
client_state_of_b_on_a
.status(ctx_a.get_client_validation_context(), client_id_on_a)?
.verify_active()?;
client_state_of_b_on_a.validate_proof_height(msg.proof_height_on_b)?;

let client_cons_state_path_on_a = ClientConsensusStatePath::new(
Expand Down
11 changes: 3 additions & 8 deletions ibc-core/ics04-channel/src/handler/chan_close_confirm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use ibc_core_channel_types::events::CloseConfirm;
use ibc_core_channel_types::msgs::MsgChannelCloseConfirm;
use ibc_core_client::context::client_state::{ClientStateCommon, ClientStateValidation};
use ibc_core_client::context::consensus_state::ConsensusState;
use ibc_core_client::types::error::ClientError;
use ibc_core_connection::types::State as ConnectionState;
use ibc_core_handler_types::error::ContextError;
use ibc_core_handler_types::events::{IbcEvent, MessageEvent};
Expand Down Expand Up @@ -115,13 +114,9 @@ where
let client_id_on_b = conn_end_on_b.client_id();
let client_state_of_a_on_b = ctx_b.client_state(client_id_on_b)?;

{
let status = client_state_of_a_on_b
.status(ctx_b.get_client_validation_context(), client_id_on_b)?;
if !status.is_active() {
return Err(ClientError::ClientNotActive { status }.into());
}
}
client_state_of_a_on_b
.status(ctx_b.get_client_validation_context(), client_id_on_b)?
.verify_active()?;
client_state_of_a_on_b.validate_proof_height(msg.proof_height_on_a)?;

let client_cons_state_path_on_b = ClientConsensusStatePath::new(
Expand Down
11 changes: 3 additions & 8 deletions ibc-core/ics04-channel/src/handler/chan_close_init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use ibc_core_channel_types::error::ChannelError;
use ibc_core_channel_types::events::CloseInit;
use ibc_core_channel_types::msgs::MsgChannelCloseInit;
use ibc_core_client::context::client_state::ClientStateValidation;
use ibc_core_client::types::error::ClientError;
use ibc_core_connection::types::State as ConnectionState;
use ibc_core_handler_types::error::ContextError;
use ibc_core_handler_types::events::{IbcEvent, MessageEvent};
Expand Down Expand Up @@ -112,13 +111,9 @@ where

let client_id_on_a = conn_end_on_a.client_id();
let client_state_of_b_on_a = ctx_a.client_state(client_id_on_a)?;
{
let status =
client_state_of_b_on_a.status(ctx_a.get_client_validation_context(), client_id_on_a)?;
if !status.is_active() {
return Err(ClientError::ClientNotActive { status }.into());
}
}
client_state_of_b_on_a
.status(ctx_a.get_client_validation_context(), client_id_on_a)?
.verify_active()?;

Ok(())
}
11 changes: 3 additions & 8 deletions ibc-core/ics04-channel/src/handler/chan_open_ack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use ibc_core_channel_types::events::OpenAck;
use ibc_core_channel_types::msgs::MsgChannelOpenAck;
use ibc_core_client::context::client_state::{ClientStateCommon, ClientStateValidation};
use ibc_core_client::context::consensus_state::ConsensusState;
use ibc_core_client::types::error::ClientError;
use ibc_core_connection::types::State as ConnectionState;
use ibc_core_handler_types::error::ContextError;
use ibc_core_handler_types::events::{IbcEvent, MessageEvent};
Expand Down Expand Up @@ -112,13 +111,9 @@ where
let client_id_on_a = conn_end_on_a.client_id();
let client_state_of_b_on_a = ctx_a.client_state(client_id_on_a)?;

{
let status = client_state_of_b_on_a
.status(ctx_a.get_client_validation_context(), client_id_on_a)?;
if !status.is_active() {
return Err(ClientError::ClientNotActive { status }.into());
}
}
client_state_of_b_on_a
.status(ctx_a.get_client_validation_context(), client_id_on_a)?
.verify_active()?;
client_state_of_b_on_a.validate_proof_height(msg.proof_height_on_b)?;

let client_cons_state_path_on_a = ClientConsensusStatePath::new(
Expand Down
11 changes: 3 additions & 8 deletions ibc-core/ics04-channel/src/handler/chan_open_confirm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use ibc_core_channel_types::events::OpenConfirm;
use ibc_core_channel_types::msgs::MsgChannelOpenConfirm;
use ibc_core_client::context::client_state::{ClientStateCommon, ClientStateValidation};
use ibc_core_client::context::consensus_state::ConsensusState;
use ibc_core_client::types::error::ClientError;
use ibc_core_connection::types::State as ConnectionState;
use ibc_core_handler_types::error::ContextError;
use ibc_core_handler_types::events::{IbcEvent, MessageEvent};
Expand Down Expand Up @@ -117,13 +116,9 @@ where
let client_id_on_b = conn_end_on_b.client_id();
let client_state_of_a_on_b = ctx_b.client_state(client_id_on_b)?;

{
let status = client_state_of_a_on_b
.status(ctx_b.get_client_validation_context(), client_id_on_b)?;
if !status.is_active() {
return Err(ClientError::ClientNotActive { status }.into());
}
}
client_state_of_a_on_b
.status(ctx_b.get_client_validation_context(), client_id_on_b)?
.verify_active()?;
client_state_of_a_on_b.validate_proof_height(msg.proof_height_on_a)?;

let client_cons_state_path_on_b = ClientConsensusStatePath::new(
Expand Down
11 changes: 3 additions & 8 deletions ibc-core/ics04-channel/src/handler/chan_open_init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use ibc_core_channel_types::channel::{ChannelEnd, Counterparty, State};
use ibc_core_channel_types::events::OpenInit;
use ibc_core_channel_types::msgs::MsgChannelOpenInit;
use ibc_core_client::context::client_state::ClientStateValidation;
use ibc_core_client::types::error::ClientError;
use ibc_core_handler_types::error::ContextError;
use ibc_core_handler_types::events::{IbcEvent, MessageEvent};
use ibc_core_host::types::identifiers::ChannelId;
Expand Down Expand Up @@ -123,13 +122,9 @@ where
let client_id_on_a = conn_end_on_a.client_id();
let client_state_of_b_on_a = ctx_a.client_state(client_id_on_a)?;

{
let status =
client_state_of_b_on_a.status(ctx_a.get_client_validation_context(), client_id_on_a)?;
if !status.is_active() {
return Err(ClientError::ClientNotActive { status }.into());
}
}
client_state_of_b_on_a
.status(ctx_a.get_client_validation_context(), client_id_on_a)?
.verify_active()?;

let conn_version = conn_end_on_a.versions();

Expand Down
12 changes: 4 additions & 8 deletions ibc-core/ics04-channel/src/handler/chan_open_try.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use ibc_core_channel_types::events::OpenTry;
use ibc_core_channel_types::msgs::MsgChannelOpenTry;
use ibc_core_client::context::client_state::{ClientStateCommon, ClientStateValidation};
use ibc_core_client::context::consensus_state::ConsensusState;
use ibc_core_client::types::error::ClientError;
use ibc_core_connection::types::State as ConnectionState;
use ibc_core_handler_types::error::ContextError;
use ibc_core_handler_types::events::{IbcEvent, MessageEvent};
Expand Down Expand Up @@ -138,13 +137,10 @@ where
let client_id_on_b = conn_end_on_b.client_id();
let client_state_of_a_on_b = ctx_b.client_state(client_id_on_b)?;

{
let status = client_state_of_a_on_b
.status(ctx_b.get_client_validation_context(), client_id_on_b)?;
if !status.is_active() {
return Err(ClientError::ClientNotActive { status }.into());
}
}
client_state_of_a_on_b
.status(ctx_b.get_client_validation_context(), client_id_on_b)?
.verify_active()?;

client_state_of_a_on_b.validate_proof_height(msg.proof_height_on_a)?;

let client_cons_state_path_on_b = ClientConsensusStatePath::new(
Expand Down
12 changes: 4 additions & 8 deletions ibc-core/ics04-channel/src/handler/recv_packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use ibc_core_channel_types::msgs::MsgRecvPacket;
use ibc_core_channel_types::packet::Receipt;
use ibc_core_client::context::client_state::{ClientStateCommon, ClientStateValidation};
use ibc_core_client::context::consensus_state::ConsensusState;
use ibc_core_client::types::error::ClientError;
use ibc_core_connection::delay::verify_conn_delay_passed;
use ibc_core_connection::types::State as ConnectionState;
use ibc_core_handler_types::error::ContextError;
Expand Down Expand Up @@ -182,13 +181,10 @@ where
let client_id_on_b = conn_end_on_b.client_id();
let client_state_of_a_on_b = ctx_b.client_state(client_id_on_b)?;

{
let status = client_state_of_a_on_b
.status(ctx_b.get_client_validation_context(), client_id_on_b)?;
if !status.is_active() {
return Err(ClientError::ClientNotActive { status }.into());
}
}
client_state_of_a_on_b
.status(ctx_b.get_client_validation_context(), client_id_on_b)?
.verify_active()?;

client_state_of_a_on_b.validate_proof_height(msg.proof_height_on_a)?;

let client_cons_state_path_on_b = ClientConsensusStatePath::new(
Expand Down
11 changes: 3 additions & 8 deletions ibc-core/ics04-channel/src/handler/send_packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use ibc_core_channel_types::events::SendPacket;
use ibc_core_channel_types::packet::Packet;
use ibc_core_client::context::client_state::{ClientStateCommon, ClientStateValidation};
use ibc_core_client::context::consensus_state::ConsensusState;
use ibc_core_client::types::error::ClientError;
use ibc_core_handler_types::error::ContextError;
use ibc_core_handler_types::events::{IbcEvent, MessageEvent};
use ibc_core_host::types::path::{
Expand Down Expand Up @@ -54,13 +53,9 @@ pub fn send_packet_validate(

let client_state_of_b_on_a = ctx_a.client_state(client_id_on_a)?;

{
let status =
client_state_of_b_on_a.status(ctx_a.get_client_validation_context(), client_id_on_a)?;
if !status.is_active() {
return Err(ClientError::ClientNotActive { status }.into());
}
}
client_state_of_b_on_a
.status(ctx_a.get_client_validation_context(), client_id_on_a)?
.verify_active()?;

let latest_height_on_a = client_state_of_b_on_a.latest_height();

Expand Down
Loading
Loading