Skip to content

Commit

Permalink
Merge pull request #9 from datachainlab/update-ibc
Browse files Browse the repository at this point in the history
Update ethereum-ibc version

Signed-off-by: Jun Kimura <[email protected]>
  • Loading branch information
bluele authored Jan 15, 2024
2 parents 55873f3 + c14f692 commit 57e503f
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 31 deletions.
8 changes: 4 additions & 4 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ displaydoc = { version = "0.2", default-features = false }
tiny-keccak = { version = "1.4" }

light-client = { git = "https://github.com/datachainlab/lcp", rev = "v0.2.4", default-features = false, features = ["ibc"] }
ethereum-ibc = { git = "https://github.com/datachainlab/ethereum-ibc-rs", rev = "v0.0.5", default-features = false }
ethereum-ibc = { git = "https://github.com/datachainlab/ethereum-ibc-rs", rev = "v0.0.6", default-features = false }
37 changes: 25 additions & 12 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,14 @@ use light_client::{
};
use tiny_keccak::Keccak;

pub struct EthereumLightClient<const SYNC_COMMITTEE_SIZE: usize>;
pub struct EthereumLightClient<
const SYNC_COMMITTEE_SIZE: usize,
const EXECUTION_PAYLOAD_TREE_DEPTH: usize,
>;

impl<const SYNC_COMMITTEE_SIZE: usize> LightClient for EthereumLightClient<SYNC_COMMITTEE_SIZE> {
impl<const SYNC_COMMITTEE_SIZE: usize, const EXECUTION_PAYLOAD_TREE_DEPTH: usize> LightClient
for EthereumLightClient<SYNC_COMMITTEE_SIZE, EXECUTION_PAYLOAD_TREE_DEPTH>
{
fn client_type(&self) -> String {
eth_client_type().as_str().into()
}
Expand All @@ -40,7 +45,7 @@ impl<const SYNC_COMMITTEE_SIZE: usize> LightClient for EthereumLightClient<SYNC_
ctx: &dyn HostClientReader,
client_id: &ClientId,
) -> Result<Height, light_client::Error> {
let client_state: ClientState<SYNC_COMMITTEE_SIZE> =
let client_state: ClientState<SYNC_COMMITTEE_SIZE, EXECUTION_PAYLOAD_TREE_DEPTH> =
ctx.client_state(client_id)?.try_into()?;
Ok(client_state.latest_height().into())
}
Expand All @@ -51,7 +56,10 @@ impl<const SYNC_COMMITTEE_SIZE: usize> LightClient for EthereumLightClient<SYNC_
any_client_state: Any,
any_consensus_state: Any,
) -> Result<light_client::CreateClientResult, light_client::Error> {
let client_state = ClientState::<SYNC_COMMITTEE_SIZE>::try_from(any_client_state.clone())?;
let client_state =
ClientState::<SYNC_COMMITTEE_SIZE, EXECUTION_PAYLOAD_TREE_DEPTH>::try_from(
any_client_state.clone(),
)?;
let consensus_state = ConsensusState::try_from(any_consensus_state)?;
let _ = client_state
.initialise(consensus_state.0.clone().into())
Expand Down Expand Up @@ -84,7 +92,7 @@ impl<const SYNC_COMMITTEE_SIZE: usize> LightClient for EthereumLightClient<SYNC_
) -> Result<light_client::UpdateClientResult, light_client::Error> {
let header = Header::<SYNC_COMMITTEE_SIZE>::try_from(any_header.clone())?;

let client_state: ClientState<SYNC_COMMITTEE_SIZE> =
let client_state: ClientState<SYNC_COMMITTEE_SIZE, EXECUTION_PAYLOAD_TREE_DEPTH> =
ctx.client_state(&client_id)?.try_into()?;

let height = header.height().into();
Expand All @@ -109,7 +117,10 @@ impl<const SYNC_COMMITTEE_SIZE: usize> LightClient for EthereumLightClient<SYNC_
consensus_state: new_consensus_state,
} = client_state
.check_header_and_update_state(
&IBCContext::<EthereumClientState<SYNC_COMMITTEE_SIZE>, EthereumConsensusState>::new(ctx),
&IBCContext::<
EthereumClientState<SYNC_COMMITTEE_SIZE, EXECUTION_PAYLOAD_TREE_DEPTH>,
EthereumConsensusState,
>::new(ctx),
client_id.into(),
any_header.into(),
)
Expand All @@ -120,9 +131,9 @@ impl<const SYNC_COMMITTEE_SIZE: usize> LightClient for EthereumLightClient<SYNC_
})?;

let new_client_state = ClientState(
downcast_client_state::<EthereumClientState<SYNC_COMMITTEE_SIZE>>(
new_client_state.as_ref(),
)
downcast_client_state::<
EthereumClientState<SYNC_COMMITTEE_SIZE, EXECUTION_PAYLOAD_TREE_DEPTH>,
>(new_client_state.as_ref())
.unwrap()
.clone(),
);
Expand Down Expand Up @@ -238,7 +249,9 @@ impl<const SYNC_COMMITTEE_SIZE: usize> LightClient for EthereumLightClient<SYNC_
}
}

impl<const SYNC_COMMITTEE_SIZE: usize> EthereumLightClient<SYNC_COMMITTEE_SIZE> {
impl<const SYNC_COMMITTEE_SIZE: usize, const EXECUTION_PAYLOAD_TREE_DEPTH: usize>
EthereumLightClient<SYNC_COMMITTEE_SIZE, EXECUTION_PAYLOAD_TREE_DEPTH>
{
fn validate_args(
ctx: &dyn HostClientReader,
client_id: ClientId,
Expand All @@ -248,15 +261,15 @@ impl<const SYNC_COMMITTEE_SIZE: usize> EthereumLightClient<SYNC_COMMITTEE_SIZE>
proof: Vec<u8>,
) -> Result<
(
ClientState<SYNC_COMMITTEE_SIZE>,
ClientState<SYNC_COMMITTEE_SIZE, EXECUTION_PAYLOAD_TREE_DEPTH>,
ConsensusState,
CommitmentPrefix,
Path,
CommitmentProofBytes,
),
light_client::Error,
> {
let client_state: ClientState<SYNC_COMMITTEE_SIZE> =
let client_state: ClientState<SYNC_COMMITTEE_SIZE, EXECUTION_PAYLOAD_TREE_DEPTH> =
ctx.client_state(&client_id)?.try_into()?;

if client_state.is_frozen() {
Expand Down
27 changes: 25 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@

use client::EthereumLightClient;
use ethereum_ibc::client_state::ETHEREUM_CLIENT_STATE_TYPE_URL;
use ethereum_ibc::consensus::{bellatrix, deneb};
use light_client::LightClientRegistry;
extern crate alloc;

pub mod client;
pub mod errors;
pub mod header;
pub mod state;
pub use ethereum_ibc as ibc;

mod internal_prelude {
pub use alloc::boxed::Box;
Expand All @@ -20,13 +22,34 @@ mod internal_prelude {
}
use internal_prelude::*;

pub fn register_implementations<const SYNC_COMMITTEE_SIZE: usize>(
pub fn register_implementations<
const SYNC_COMMITTEE_SIZE: usize,
const EXECUTION_PAYLOAD_TREE_DEPTH: usize,
>(
registry: &mut dyn LightClientRegistry,
) {
registry
.put_light_client(
ETHEREUM_CLIENT_STATE_TYPE_URL.to_string(),
Box::new(EthereumLightClient::<SYNC_COMMITTEE_SIZE>),
Box::new(EthereumLightClient::<SYNC_COMMITTEE_SIZE, EXECUTION_PAYLOAD_TREE_DEPTH>),
)
.unwrap()
}

/// Register the light client implementations for the bellatrix and capella testnets
pub fn register_bellatrix_implementations<const SYNC_COMMITTEE_SIZE: usize>(
registry: &mut dyn LightClientRegistry,
) {
register_implementations::<SYNC_COMMITTEE_SIZE, { bellatrix::EXECUTION_PAYLOAD_TREE_DEPTH }>(
registry,
);
}

/// Register the light client implementations for the deneb testnet
pub fn register_deneb_implementations<const SYNC_COMMITTEE_SIZE: usize>(
registry: &mut dyn LightClientRegistry,
) {
register_implementations::<SYNC_COMMITTEE_SIZE, { deneb::EXECUTION_PAYLOAD_TREE_DEPTH }>(
registry,
);
}
33 changes: 21 additions & 12 deletions src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,23 @@ use light_client::types::proto::google::protobuf::Any as IBCAny;
use light_client::types::Any;

#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
pub struct ClientState<const SYNC_COMMITTEE_SIZE: usize>(
pub(crate) EthereumClientState<SYNC_COMMITTEE_SIZE>,
pub struct ClientState<const SYNC_COMMITTEE_SIZE: usize, const EXECUTION_PAYLOAD_TREE_DEPTH: usize>(
pub(crate) EthereumClientState<SYNC_COMMITTEE_SIZE, EXECUTION_PAYLOAD_TREE_DEPTH>,
);

impl<const SYNC_COMMITTEE_SIZE: usize> Deref for ClientState<SYNC_COMMITTEE_SIZE> {
type Target = EthereumClientState<SYNC_COMMITTEE_SIZE>;
impl<const SYNC_COMMITTEE_SIZE: usize, const EXECUTION_PAYLOAD_TREE_DEPTH: usize> Deref
for ClientState<SYNC_COMMITTEE_SIZE, EXECUTION_PAYLOAD_TREE_DEPTH>
{
type Target = EthereumClientState<SYNC_COMMITTEE_SIZE, EXECUTION_PAYLOAD_TREE_DEPTH>;

fn deref(&self) -> &Self::Target {
&self.0
}
}

impl<const SYNC_COMMITTEE_SIZE: usize> TryFrom<Any> for ClientState<SYNC_COMMITTEE_SIZE> {
impl<const SYNC_COMMITTEE_SIZE: usize, const EXECUTION_PAYLOAD_TREE_DEPTH: usize> TryFrom<Any>
for ClientState<SYNC_COMMITTEE_SIZE, EXECUTION_PAYLOAD_TREE_DEPTH>
{
type Error = Error;

fn try_from(value: Any) -> Result<Self, Self::Error> {
Expand All @@ -38,8 +42,10 @@ impl<const SYNC_COMMITTEE_SIZE: usize> TryFrom<Any> for ClientState<SYNC_COMMITT
}
}

impl<const SYNC_COMMITTEE_SIZE: usize> From<ClientState<SYNC_COMMITTEE_SIZE>> for Any {
fn from(value: ClientState<SYNC_COMMITTEE_SIZE>) -> Self {
impl<const SYNC_COMMITTEE_SIZE: usize, const EXECUTION_PAYLOAD_TREE_DEPTH: usize>
From<ClientState<SYNC_COMMITTEE_SIZE, EXECUTION_PAYLOAD_TREE_DEPTH>> for Any
{
fn from(value: ClientState<SYNC_COMMITTEE_SIZE, EXECUTION_PAYLOAD_TREE_DEPTH>) -> Self {
IBCAny::from(value.0).into()
}
}
Expand Down Expand Up @@ -78,9 +84,12 @@ impl From<ConsensusState> for Any {

// canonicalize_client_state canonicalizes some fields of specified client state
// target fields: latest_slot, latest_execution_block_number, frozen_height
pub fn canonicalize_client_state<const SYNC_COMMITTEE_SIZE: usize>(
client_state: ClientState<SYNC_COMMITTEE_SIZE>,
) -> ClientState<SYNC_COMMITTEE_SIZE> {
pub fn canonicalize_client_state<
const SYNC_COMMITTEE_SIZE: usize,
const EXECUTION_PAYLOAD_TREE_DEPTH: usize,
>(
client_state: ClientState<SYNC_COMMITTEE_SIZE, EXECUTION_PAYLOAD_TREE_DEPTH>,
) -> ClientState<SYNC_COMMITTEE_SIZE, EXECUTION_PAYLOAD_TREE_DEPTH> {
let mut client_state = client_state.0;
client_state.latest_slot = 0u64.into();
client_state.latest_execution_block_number = 0u64.into();
Expand All @@ -96,8 +105,8 @@ pub fn canonicalize_consensus_state(consensus_state: ConsensusState) -> Consensu
ConsensusState(consensus_state)
}

pub fn gen_state_id<const SYNC_COMMITTEE_SIZE: usize>(
client_state: ClientState<SYNC_COMMITTEE_SIZE>,
pub fn gen_state_id<const SYNC_COMMITTEE_SIZE: usize, const EXECUTION_PAYLOAD_TREE_DEPTH: usize>(
client_state: ClientState<SYNC_COMMITTEE_SIZE, EXECUTION_PAYLOAD_TREE_DEPTH>,
consensus_state: ConsensusState,
) -> Result<StateID, Error> {
Ok(gen_state_id_from_any(
Expand Down

0 comments on commit 57e503f

Please sign in to comment.