Skip to content

Commit

Permalink
ibc: ⛅ hoist ics02 validation out of client_counter
Browse files Browse the repository at this point in the history
nb: this commit strictly contains plain code motion. it does not make any
changes to the code being moved.

`ics02_validation` contains some important code for interfacing with
tendermint/cometbft. while this submodule lives beneath the client
counter logic, its contents are used in our message handling code
related to e.g. misbehavior.

this hoists this submodule up one level, to be a child of
`penumbra_ibc::component`.
  • Loading branch information
cratelyn committed Jan 10, 2024
1 parent 29b7cb2 commit e1a1bda
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 94 deletions.
1 change: 1 addition & 0 deletions crates/core/component/ibc/src/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ mod client;
mod client_counter;
mod connection;
mod connection_counter;
mod ics02_validation;

#[cfg(feature = "rpc")]
pub mod rpc;
Expand Down
87 changes: 1 addition & 86 deletions crates/core/component/ibc/src/component/client_counter.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::IBC_PROOF_SPECS;
use crate::{component::ics02_validation, IBC_PROOF_SPECS};
use ibc_proto::google::protobuf::Any;
use ibc_types::core::client::Height;
use ibc_types::core::connection::{ChainId, ConnectionId};
Expand Down Expand Up @@ -90,91 +90,6 @@ impl From<ClientConnections> for pb::ClientConnections {
}
}

pub(crate) mod ics02_validation {
use anyhow::{anyhow, Result};
use ibc_proto::google::protobuf::Any;
use ibc_types::lightclients::tendermint::client_state::{
ClientState as TendermintClientState, TENDERMINT_CLIENT_STATE_TYPE_URL,
};
use ibc_types::lightclients::tendermint::consensus_state::{
ConsensusState as TendermintConsensusState, TENDERMINT_CONSENSUS_STATE_TYPE_URL,
};
use ibc_types::lightclients::tendermint::header::{
Header as TendermintHeader, TENDERMINT_HEADER_TYPE_URL,
};
use ibc_types::lightclients::tendermint::misbehaviour::{
Misbehaviour as TendermintMisbehavior, TENDERMINT_MISBEHAVIOUR_TYPE_URL,
};

pub fn is_tendermint_header_state(header: &Any) -> bool {
header.type_url.as_str() == TENDERMINT_HEADER_TYPE_URL
}
pub fn is_tendermint_consensus_state(consensus_state: &Any) -> bool {
consensus_state.type_url.as_str() == TENDERMINT_CONSENSUS_STATE_TYPE_URL
}
pub fn is_tendermint_client_state(client_state: &Any) -> bool {
client_state.type_url.as_str() == TENDERMINT_CLIENT_STATE_TYPE_URL
}
pub fn is_tendermint_misbehavior(misbehavior: &Any) -> bool {
misbehavior.type_url.as_str() == TENDERMINT_MISBEHAVIOUR_TYPE_URL
}

pub fn get_tendermint_misbehavior(misbehavior: Any) -> Result<TendermintMisbehavior> {
if is_tendermint_misbehavior(&misbehavior) {
TendermintMisbehavior::try_from(misbehavior)
.map_err(|e| anyhow!(format!("failed to deserialize tendermint misbehavior: {e}")))
} else {
anyhow::bail!(format!(
"expected a tendermint light client misbehavior, got: {}",
misbehavior.type_url.as_str()
))
}
}

pub fn get_tendermint_header(header: Any) -> Result<TendermintHeader> {
if is_tendermint_header_state(&header) {
TendermintHeader::try_from(header)
.map_err(|e| anyhow!(format!("failed to deserialize tendermint header: {e}")))
} else {
anyhow::bail!(format!(
"expected a tendermint light client header, got: {}",
header.type_url.as_str()
))
}
}

pub fn get_tendermint_consensus_state(
consensus_state: Any,
) -> Result<TendermintConsensusState> {
if is_tendermint_consensus_state(&consensus_state) {
TendermintConsensusState::try_from(consensus_state).map_err(|e| {
anyhow!(format!(
"failed to deserialize tendermint consensus state: {e}"
))
})
} else {
anyhow::bail!(format!(
"expected tendermint consensus state, got: {}",
consensus_state.type_url.as_str()
))
}
}
pub fn get_tendermint_client_state(client_state: Any) -> Result<TendermintClientState> {
if is_tendermint_client_state(&client_state) {
TendermintClientState::try_from(client_state).map_err(|e| {
anyhow!(format!(
"failed to deserialize tendermint client state: {e}"
))
})
} else {
anyhow::bail!(format!(
"expected tendermint client state, got: {}",
client_state.type_url.as_str()
))
}
}
}

// Check that the trust threshold is:
//
// a) non-zero
Expand Down
80 changes: 80 additions & 0 deletions crates/core/component/ibc/src/component/ics02_validation.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
use anyhow::{anyhow, Result};
use ibc_proto::google::protobuf::Any;
use ibc_types::lightclients::tendermint::client_state::{
ClientState as TendermintClientState, TENDERMINT_CLIENT_STATE_TYPE_URL,
};
use ibc_types::lightclients::tendermint::consensus_state::{
ConsensusState as TendermintConsensusState, TENDERMINT_CONSENSUS_STATE_TYPE_URL,
};
use ibc_types::lightclients::tendermint::header::{
Header as TendermintHeader, TENDERMINT_HEADER_TYPE_URL,
};
use ibc_types::lightclients::tendermint::misbehaviour::{
Misbehaviour as TendermintMisbehavior, TENDERMINT_MISBEHAVIOUR_TYPE_URL,
};

pub fn is_tendermint_header_state(header: &Any) -> bool {
header.type_url.as_str() == TENDERMINT_HEADER_TYPE_URL
}
pub fn is_tendermint_consensus_state(consensus_state: &Any) -> bool {
consensus_state.type_url.as_str() == TENDERMINT_CONSENSUS_STATE_TYPE_URL
}
pub fn is_tendermint_client_state(client_state: &Any) -> bool {
client_state.type_url.as_str() == TENDERMINT_CLIENT_STATE_TYPE_URL
}
pub fn is_tendermint_misbehavior(misbehavior: &Any) -> bool {
misbehavior.type_url.as_str() == TENDERMINT_MISBEHAVIOUR_TYPE_URL
}

pub fn get_tendermint_misbehavior(misbehavior: Any) -> Result<TendermintMisbehavior> {
if is_tendermint_misbehavior(&misbehavior) {
TendermintMisbehavior::try_from(misbehavior)
.map_err(|e| anyhow!(format!("failed to deserialize tendermint misbehavior: {e}")))
} else {
anyhow::bail!(format!(
"expected a tendermint light client misbehavior, got: {}",
misbehavior.type_url.as_str()
))
}
}

pub fn get_tendermint_header(header: Any) -> Result<TendermintHeader> {
if is_tendermint_header_state(&header) {
TendermintHeader::try_from(header)
.map_err(|e| anyhow!(format!("failed to deserialize tendermint header: {e}")))
} else {
anyhow::bail!(format!(
"expected a tendermint light client header, got: {}",
header.type_url.as_str()
))
}
}

pub fn get_tendermint_consensus_state(consensus_state: Any) -> Result<TendermintConsensusState> {
if is_tendermint_consensus_state(&consensus_state) {
TendermintConsensusState::try_from(consensus_state).map_err(|e| {
anyhow!(format!(
"failed to deserialize tendermint consensus state: {e}"
))
})
} else {
anyhow::bail!(format!(
"expected tendermint consensus state, got: {}",
consensus_state.type_url.as_str()
))
}
}
pub fn get_tendermint_client_state(client_state: Any) -> Result<TendermintClientState> {
if is_tendermint_client_state(&client_state) {
TendermintClientState::try_from(client_state).map_err(|e| {
anyhow!(format!(
"failed to deserialize tendermint client state: {e}"
))
})
} else {
anyhow::bail!(format!(
"expected tendermint client state, got: {}",
client_state.type_url.as_str()
))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ use ibc_types::{

use crate::component::{
client::{StateReadExt as _, StateWriteExt as _},
client_counter::{ics02_validation, ClientCounter},
MsgHandler,
client_counter::ClientCounter,
ics02_validation, MsgHandler,
};

#[async_trait]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,9 @@ use tendermint_light_client_verifier::{
ProdVerifier, Verdict, Verifier,
};

use crate::component::client::StateWriteExt as _;
use crate::component::client_counter::ics02_validation;
use crate::component::ClientStateReadExt as _;

use super::update_client::verify_header_validator_set;
use super::MsgHandler;
use crate::component::{client::StateWriteExt as _, ics02_validation, ClientStateReadExt as _};

#[async_trait]
impl MsgHandler for MsgSubmitMisbehaviour {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ use tendermint_light_client_verifier::{

use crate::component::{
client::{Ics2ClientExt as _, StateReadExt as _, StateWriteExt as _},
client_counter::ics02_validation,
MsgHandler,
ics02_validation, MsgHandler,
};

#[async_trait]
Expand Down

0 comments on commit e1a1bda

Please sign in to comment.