Skip to content

Commit

Permalink
Add ChainId to Starknet and Comet client state (#173)
Browse files Browse the repository at this point in the history
* Add ChainId to CometClientState

* use MutEncoder and MutDecoder

* Fix clippy

* Update CometClientState in Cairo to add chain_id field

* Add chain ID type to StarknetClientState as Felt type

* Use String in Rust code for Chain ID in Starknet client state

* Remove unnecessary Felt to Protobuf wiring

* Replace remaining Felt chainID types to String

* use ChainId for chain_id type

* debug: this compiles

* wire ChainId encoding

* add cgp trait bounds

* comment ChainId cgp bound which does not work

* Fix chain id encoding

* Fix ChainId encoding in Starknet client state

* Update ibc-starknet-cw nix

* Remove unwrap

* Use correct chain ID in ClientState update_state

---------

Co-authored-by: Ranadeep Biswas <[email protected]>
Co-authored-by: Soares Chen <[email protected]>
  • Loading branch information
3 people authored Jan 15, 2025
1 parent 3684271 commit 7e16860
Show file tree
Hide file tree
Showing 22 changed files with 147 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ use core::num::traits::Zero;
use starknet_ibc_clients::cometbft::CometErrors;
use starknet_ibc_core::client::{Height, HeightPartialOrd, Status, StatusTrait};

#[derive(Clone, Debug, Drop, Hash, PartialEq, Serde, starknet::Store)]
#[derive(Clone, Debug, Drop, PartialEq, Serde, starknet::Store)]
pub struct CometClientState {
pub latest_height: Height,
pub trusting_period: u64,
pub unbonding_period: u64,
pub status: Status,
pub chain_id: ByteArray,
}

#[generate_trait]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ pub impl CometClientConfigImpl of CometClientConfigTrait {
trusting_period: *self.trusting_period,
unbonding_period: *self.unbonding_period,
status: Status::Active,
chain_id: "dummy_chain",
};

Serde::serialize(@client_state, ref serialized_client_state);
Expand Down
6 changes: 3 additions & 3 deletions light-client/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions light-client/ibc-client-starknet-types/src/client_state.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use cgp::prelude::*;
use ibc_core::client::types::Height;
use ibc_core::host::types::identifiers::ChainId;

pub const STARKNET_CLIENT_STATE_TYPE_URL: &str = "/StarknetClientState";

#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Clone, Debug, PartialEq, derive_more::From, HasField)]
pub struct StarknetClientState {
pub latest_height: Height,
pub chain_id: ChainId,
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use cgp::core::component::UseContext;
use cgp::prelude::*;
use hermes_cosmos_encoding_components::impls::chain_id::EncodeChainIdField;
use hermes_encoding_components::impls::encode_mut::combine::CombineEncoders;
use hermes_encoding_components::impls::encode_mut::field::EncodeField;
use hermes_encoding_components::impls::encode_mut::from::DecodeFrom;
Expand All @@ -8,6 +9,7 @@ use hermes_protobuf_encoding_components::components::{MutDecoderComponent, MutEn
use hermes_protobuf_encoding_components::impls::encode_mut::proto_field::decode_required::DecodeRequiredProtoField;
use hermes_protobuf_encoding_components::impls::encode_mut::proto_field::encode::EncodeLengthDelimitedProtoField;
use ibc_core::client::types::Height;
use ibc_core::host::types::identifiers::ChainId;

use crate::StarknetClientState;

Expand All @@ -21,22 +23,30 @@ delegate_components! {
symbol!("latest_height"),
EncodeLengthDelimitedProtoField<1, UseContext>,
>,
EncodeField<
symbol!("chain_id"),
EncodeChainIdField<2>,
>,
]>,
MutDecoderComponent: DecodeFrom<
Self,
CombineEncoders<Product![
DecodeRequiredProtoField<1, UseContext>,
EncodeChainIdField<2>,
]>
>,
}
}

impl Transformer for EncodeStarknetClientState {
type From = Product![Height];
type From = Product![Height, ChainId];

type To = StarknetClientState;

fn transform(product![latest_height]: Self::From) -> Self::To {
StarknetClientState { latest_height }
fn transform(product![latest_height, chain_id]: Self::From) -> Self::To {
StarknetClientState {
latest_height,
chain_id,
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ where

let new_client_state = ClientStateType {
latest_height: header.height,
chain_id: self.0.chain_id.clone(),
}
.into();

Expand Down
12 changes: 11 additions & 1 deletion light-client/ibc-client-starknet/src/encoding/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use ibc_client_starknet_types::{StarknetClientState, StarknetConsensusState};
use ibc_core::client::types::error::ClientError;
use ibc_core::client::types::Height;
use ibc_core::commitment_types::commitment::CommitmentRoot;
use ibc_core::host::types::error::DecodingError;
use ibc_core::host::types::error::{DecodingError, IdentifierError};
use ibc_core::primitives::{Timestamp, TimestampError};
use prost::DecodeError;

Expand Down Expand Up @@ -175,6 +175,16 @@ impl ErrorRaiser<StarknetLightClientEncoding, TypeUrlMismatchError>
}
}

impl ErrorRaiser<StarknetLightClientEncoding, IdentifierError>
for StarknetLightClientEncodingContextComponents
{
fn raise_error(e: IdentifierError) -> ClientError {
ClientError::ClientSpecific {
description: format!("{e:?}"),
}
}
}

pub trait CanUseStarknetLightClientEncoding:
Async
+ CanEncodeAndDecode<ViaProtobuf, Any>
Expand Down
2 changes: 1 addition & 1 deletion nix/ibc-starknet-cw.nix
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ let
cargoLock = {
lockFile = ./../light-client/Cargo.lock;
outputHashes = {
"hermes-cosmos-encoding-components-0.1.0" = "sha256-OrNFK6UQjQ7cpj8HqPipG2YcqCV4/KjiV5oRLnoUUyY=";
"hermes-cosmos-encoding-components-0.1.0" = "sha256-Lkk0bxna5bhXuOifLgMqNHaJO65bFg7oMoLBTIOVhYE=";
};
};

Expand Down
Loading

0 comments on commit 7e16860

Please sign in to comment.