-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Natively implement Cosmos UpdateClient and Comet light client (#479)
* Add comet-light-client-components crate * Implement DoVerifyToTarget * Add CanVerifyUpdateHeader * Draft implementation of verify_forward * Move comet crate to separate sub-directory * Implement UpdateHeaderVerifier * Remove VerifierState * Rename Chain to Client * Implement BisectHeight * Implement FetchTendermintLightBlock * Implement FetchTendermintLightBlockWithStatus * Implement TraceTendermintVerification * Implement DoUpdateVerifactionStatus * Implement ValidateTendermintLightBlock * Implement light client context and fields * Implement QueryHighestTrustedOrVerifiedBefore * Implement methods for CometLightClient * Try using new light client * Implement CanUpdateVerificationStatus<TrustedStatus> * Implement CanBuildLightBlocksForUpdateClient * Build UpdateClient headers * Use back Hermes v1 types for now * Use new UpdateClient payload builder * New UpdateClient is working * Remove old UpdateClient payload builder
- Loading branch information
1 parent
4b50f6d
commit 923643a
Showing
58 changed files
with
1,412 additions
and
35 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
[package] | ||
name = "hermes-comet-light-client-components" | ||
version = { workspace = true } | ||
edition = { workspace = true } | ||
license = { workspace = true } | ||
keywords = { workspace = true } | ||
repository = { workspace = true } | ||
authors = { workspace = true } | ||
rust-version = { workspace = true } | ||
description = """ | ||
Implementation of an IBC Relayer in Rust, as a library | ||
""" | ||
|
||
[dependencies] | ||
cgp = { workspace = true } | ||
hermes-chain-type-components = { workspace = true } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub mod verify_target_height; |
2 changes: 2 additions & 0 deletions
2
crates/comet/comet-light-client-components/src/impls/verify_target_height/mod.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
pub mod verify_forward; | ||
pub mod verify_to_target; |
106 changes: 106 additions & 0 deletions
106
crates/comet/comet-light-client-components/src/impls/verify_target_height/verify_forward.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
use core::fmt::Debug; | ||
|
||
use cgp::core::Async; | ||
use cgp::prelude::CanRaiseError; | ||
use hermes_chain_type_components::traits::types::height::HasHeightType; | ||
|
||
use crate::traits::compute_verification_height::CanComputeNextVerificationHeight; | ||
use crate::traits::fetch_light_block::CanFetchLightBlockWithStatus; | ||
use crate::traits::light_block::height::HasLightBlockHeight; | ||
use crate::traits::query_light_block::{CanQueryLightBlock, GetHighestTrustedOrVerifiedBefore}; | ||
use crate::traits::trace_verification_height::CanTraceVerificationHeight; | ||
use crate::traits::types::status::HasVerificationStatusType; | ||
use crate::traits::types::verdict::HasVerdictType; | ||
use crate::traits::update_verification_status::{CanUpdateVerificationStatus, VerifiedStatus}; | ||
use crate::traits::validate_light_block::{CanValidateLightBlock, IsWithinTrustingPeriod}; | ||
use crate::traits::verify_target_height::{NoInitialTrustedState, TargetHeightVerifier}; | ||
use crate::traits::verify_update_header::CanVerifyUpdateHeader; | ||
use crate::types::status::VerificationStatus; | ||
use crate::types::verdict::Verdict; | ||
|
||
pub struct DoVerifyForward; | ||
|
||
pub struct TargetLowerThanTrustedHeight<'a, Client> | ||
where | ||
Client: HasHeightType, | ||
{ | ||
pub target_height: &'a Client::Height, | ||
pub trusted_height: &'a Client::Height, | ||
} | ||
|
||
impl<Client, Mode> TargetHeightVerifier<Client, Mode> for DoVerifyForward | ||
where | ||
Client: HasLightBlockHeight | ||
+ HasVerdictType<Verdict = Verdict> | ||
+ HasVerificationStatusType<VerificationStatus = VerificationStatus> | ||
+ CanVerifyUpdateHeader | ||
+ CanTraceVerificationHeight | ||
+ CanFetchLightBlockWithStatus | ||
+ CanComputeNextVerificationHeight | ||
+ CanUpdateVerificationStatus<VerifiedStatus> | ||
+ CanValidateLightBlock<IsWithinTrustingPeriod> | ||
+ CanQueryLightBlock<GetHighestTrustedOrVerifiedBefore> | ||
+ CanRaiseError<NoInitialTrustedState> | ||
+ for<'a> CanRaiseError<TargetLowerThanTrustedHeight<'a, Client>>, | ||
Mode: Async, | ||
{ | ||
async fn verify_target_height( | ||
client: &mut Client, | ||
_mode: Mode, | ||
target_height: &Client::Height, | ||
) -> Result<Client::LightBlock, Client::Error> { | ||
let mut current_height = target_height.clone(); | ||
|
||
loop { | ||
let trusted_block = client | ||
.query_light_block(GetHighestTrustedOrVerifiedBefore, target_height) | ||
.ok_or_else(|| Client::raise_error(NoInitialTrustedState))?; | ||
|
||
let trusted_height = Client::light_block_height(&trusted_block); | ||
|
||
if target_height < trusted_height { | ||
return Err(Client::raise_error(TargetLowerThanTrustedHeight { | ||
target_height, | ||
trusted_height, | ||
})); | ||
} | ||
|
||
client.validate_light_block(IsWithinTrustingPeriod, &trusted_block)?; | ||
|
||
client.trace_verification_height(target_height, ¤t_height); | ||
|
||
if target_height == trusted_height { | ||
return Ok(trusted_block); | ||
} | ||
|
||
let (current_block, current_status) = client | ||
.fetch_light_block_with_status(¤t_height) | ||
.await?; | ||
|
||
let verdict = client.verify_update_header(¤t_block, &trusted_block)?; | ||
|
||
if verdict == Verdict::Success { | ||
if current_status == VerificationStatus::Unverified { | ||
client.update_verification_status(VerifiedStatus, ¤t_block); | ||
} | ||
|
||
client.trace_verification_height(¤t_height, trusted_height); | ||
} | ||
|
||
current_height = | ||
client.compute_next_verification_height(¤t_height, target_height)?; | ||
} | ||
} | ||
} | ||
|
||
impl<'a, Client> Debug for TargetLowerThanTrustedHeight<'a, Client> | ||
where | ||
Client: HasHeightType<Height: Debug>, | ||
{ | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
f.debug_struct("TargetLowerThanTrustedHeight") | ||
.field("target_height", &self.target_height) | ||
.field("trusted_height", &self.trusted_height) | ||
.finish() | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
...es/comet/comet-light-client-components/src/impls/verify_target_height/verify_to_target.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
use cgp::prelude::CanRaiseError; | ||
|
||
use crate::traits::light_block::height::HasLightBlockHeight; | ||
use crate::traits::query_light_block::{ | ||
CanQueryLightBlock, GetHighestTrustedOrVerifiedBefore, GetLowestTrustedOrVerified, | ||
GetTrustedOrVerified, | ||
}; | ||
use crate::traits::verify_target_height::{ | ||
CanVerifyTargetHeight, NoInitialTrustedState, TargetHeightVerifier, VerifyBackward, | ||
VerifyForward, VerifyToTarget, | ||
}; | ||
|
||
pub struct DoVerifyToTarget; | ||
|
||
impl<Client> TargetHeightVerifier<Client, VerifyToTarget> for DoVerifyToTarget | ||
where | ||
Client: HasLightBlockHeight | ||
+ CanVerifyTargetHeight<VerifyForward> | ||
+ CanVerifyTargetHeight<VerifyBackward> | ||
+ CanQueryLightBlock<GetTrustedOrVerified> | ||
+ CanQueryLightBlock<GetHighestTrustedOrVerifiedBefore> | ||
+ CanQueryLightBlock<GetLowestTrustedOrVerified> | ||
+ CanRaiseError<NoInitialTrustedState>, | ||
{ | ||
async fn verify_target_height( | ||
client: &mut Client, | ||
_mode: VerifyToTarget, | ||
target_height: &Client::Height, | ||
) -> Result<Client::LightBlock, Client::Error> { | ||
if let Some(block) = client.query_light_block(GetTrustedOrVerified, target_height) { | ||
return Ok(block); | ||
} | ||
|
||
let highest_block = client | ||
.query_light_block(GetHighestTrustedOrVerifiedBefore, target_height) | ||
.or_else(|| client.query_light_block(GetHighestTrustedOrVerifiedBefore, target_height)) | ||
.ok_or_else(|| Client::raise_error(NoInitialTrustedState))?; | ||
|
||
let highest_height = Client::light_block_height(&highest_block); | ||
|
||
if target_height >= highest_height { | ||
client | ||
.verify_target_height(VerifyForward, target_height) | ||
.await | ||
} else { | ||
client | ||
.verify_target_height(VerifyBackward, target_height) | ||
.await | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
pub mod impls; | ||
pub mod traits; | ||
pub mod types; |
11 changes: 11 additions & 0 deletions
11
crates/comet/comet-light-client-components/src/traits/compute_verification_height.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
use cgp::prelude::*; | ||
use hermes_chain_type_components::traits::types::height::HasHeightType; | ||
|
||
#[derive_component(NextVerificationHeightComputerComponent, NextVerificationHeightComputer<Client>)] | ||
pub trait CanComputeNextVerificationHeight: HasHeightType + HasErrorType { | ||
fn compute_next_verification_height( | ||
&self, | ||
current_height: &Self::Height, | ||
target_height: &Self::Height, | ||
) -> Result<Self::Height, Self::Error>; | ||
} |
25 changes: 25 additions & 0 deletions
25
crates/comet/comet-light-client-components/src/traits/fetch_light_block.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
use cgp::prelude::*; | ||
use hermes_chain_type_components::traits::types::height::HasHeightType; | ||
|
||
use crate::traits::types::light_block::HasLightBlockType; | ||
use crate::traits::types::status::HasVerificationStatusType; | ||
|
||
#[derive_component(LightBlockFetcherComponent, LightBlockFetcher<Client>)] | ||
#[async_trait] | ||
pub trait CanFetchLightBlock: HasHeightType + HasLightBlockType + HasErrorType { | ||
async fn fetch_light_block( | ||
&self, | ||
height: &Self::Height, | ||
) -> Result<Self::LightBlock, Self::Error>; | ||
} | ||
|
||
#[derive_component(LightBlockWithStatusFetcherComponent, LightBlockWithStatusFetcher<Client>)] | ||
#[async_trait] | ||
pub trait CanFetchLightBlockWithStatus: | ||
HasHeightType + HasLightBlockType + HasVerificationStatusType + HasErrorType | ||
{ | ||
async fn fetch_light_block_with_status( | ||
&mut self, | ||
height: &Self::Height, | ||
) -> Result<(Self::LightBlock, Self::VerificationStatus), Self::Error>; | ||
} |
9 changes: 9 additions & 0 deletions
9
crates/comet/comet-light-client-components/src/traits/light_block/height.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
use cgp::prelude::*; | ||
use hermes_chain_type_components::traits::types::height::HasHeightType; | ||
|
||
use crate::traits::types::light_block::HasLightBlockType; | ||
|
||
#[derive_component(LightBlockHeightGetterComponent, LightBlockHeightGetter<Client>)] | ||
pub trait HasLightBlockHeight: HasLightBlockType + HasHeightType { | ||
fn light_block_height(light_block: &Self::LightBlock) -> &Self::Height; | ||
} |
1 change: 1 addition & 0 deletions
1
crates/comet/comet-light-client-components/src/traits/light_block/mod.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub mod height; |
11 changes: 11 additions & 0 deletions
11
crates/comet/comet-light-client-components/src/traits/mod.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
pub mod compute_verification_height; | ||
pub mod fetch_light_block; | ||
pub mod light_block; | ||
pub mod query_light_block; | ||
pub mod trace_verification_height; | ||
pub mod types; | ||
pub mod update_client; | ||
pub mod update_verification_status; | ||
pub mod validate_light_block; | ||
pub mod verify_target_height; | ||
pub mod verify_update_header; |
15 changes: 15 additions & 0 deletions
15
crates/comet/comet-light-client-components/src/traits/query_light_block.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
use cgp::prelude::*; | ||
use hermes_chain_type_components::traits::types::height::HasHeightType; | ||
|
||
use crate::traits::types::light_block::HasLightBlockType; | ||
|
||
#[derive_component(LightBlockQuerierComponent, LightBlockQuerier<Client>)] | ||
pub trait CanQueryLightBlock<Mode>: HasHeightType + HasLightBlockType { | ||
fn query_light_block(&self, _mode: Mode, height: &Self::Height) -> Option<Self::LightBlock>; | ||
} | ||
|
||
pub struct GetTrustedOrVerified; | ||
|
||
pub struct GetHighestTrustedOrVerifiedBefore; | ||
|
||
pub struct GetLowestTrustedOrVerified; |
11 changes: 11 additions & 0 deletions
11
crates/comet/comet-light-client-components/src/traits/trace_verification_height.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
use cgp::prelude::*; | ||
use hermes_chain_type_components::traits::types::height::HasHeightType; | ||
|
||
#[derive_component(VerificationHeightTracerComponent, VerificationHeightTracer<Client>)] | ||
pub trait CanTraceVerificationHeight: HasHeightType { | ||
fn trace_verification_height( | ||
&mut self, | ||
target_height: &Self::Height, | ||
current_height: &Self::Height, | ||
); | ||
} |
6 changes: 6 additions & 0 deletions
6
crates/comet/comet-light-client-components/src/traits/types/light_block.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
use cgp::prelude::*; | ||
|
||
#[derive_component(LightBlockTypeComponent, ProvideLightBlockType<Client>)] | ||
pub trait HasLightBlockType: Async { | ||
type LightBlock: Async; | ||
} |
3 changes: 3 additions & 0 deletions
3
crates/comet/comet-light-client-components/src/traits/types/mod.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
pub mod light_block; | ||
pub mod status; | ||
pub mod verdict; |
Oops, something went wrong.