Skip to content

Commit

Permalink
add ClientUpgradeProofVerifier
Browse files Browse the repository at this point in the history
  • Loading branch information
avahowell committed Oct 6, 2023
1 parent 8dc50e6 commit d5afb6a
Showing 1 changed file with 64 additions and 1 deletion.
65 changes: 64 additions & 1 deletion crates/core/component/ibc/src/component/proof_verification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::component::client::StateReadExt;

use core::time::Duration;
use ibc_proto::protobuf::Protobuf;
use ibc_types::path::ClientConsensusStatePath;
use ibc_types::path::{ClientConsensusStatePath, ClientUpgradePath};
use ibc_types::DomainType;
use ibc_types::{
core::{
Expand Down Expand Up @@ -107,6 +107,69 @@ fn verify_merkle_proof(
Ok(())
}

#[async_trait]
pub trait ClientUpgradeProofVerifier: StateReadExt {
async fn verify_client_upgrade_proof(
&self,
connection: &ConnectionEnd,
proof: &MerkleProof,
proof_height: &Height,
upgraded_tm_consensus_state: TendermintConsensusState,
upgraded_tm_client_state: TendermintClientState,
) -> anyhow::Result<()> {
// get the stored client state for the counterparty
let trusted_client_state = self.get_client_state(&connection.client_id).await?;

// Check to see if the upgrade path is set
let mut upgrade_path = trusted_client_state.upgrade_path.clone();
if upgrade_path.pop().is_none() {
anyhow::bail!("upgrade path is not set");
};

let upgrade_path_prefix = MerklePrefix::try_from(upgrade_path[0].clone().into_bytes())
.map_err(|_| {
anyhow::anyhow!("couldnt create commitment prefix from client upgrade path")
})?;

// check if the client is frozen
// TODO: should we also check if the client is expired here?
if trusted_client_state.is_frozen() {
anyhow::bail!("client is frozen");
}

// get the stored consensus state for the counterparty
let trusted_consensus_state = self
.get_verified_consensus_state(*proof_height, connection.client_id.clone())
.await?;

trusted_client_state.verify_height(*proof_height)?;

verify_merkle_proof(
&trusted_client_state.proof_specs,
&upgrade_path_prefix,
proof,
&trusted_consensus_state.root,
ClientUpgradePath::UpgradedClientState(
trusted_client_state.latest_height().revision_height(),
),
upgraded_tm_client_state.encode_to_vec(),
)?;

verify_merkle_proof(
&trusted_client_state.proof_specs,
&upgrade_path_prefix,
proof,
&trusted_consensus_state.root,
ClientUpgradePath::UpgradedClientConsensusState(
trusted_client_state.latest_height().revision_height(),
),
upgraded_tm_consensus_state.encode_to_vec(),
)?;

Ok(())
}
}

#[async_trait]
pub trait ChannelProofVerifier: StateReadExt {
async fn verify_channel_proof(
Expand Down

0 comments on commit d5afb6a

Please sign in to comment.