From c77de769ca6c04a3e122a6039ca83c99f6706486 Mon Sep 17 00:00:00 2001 From: Jun Kimura Date: Wed, 10 May 2023 18:13:06 +0900 Subject: [PATCH] make fork parameters generic Signed-off-by: Jun Kimura --- crates/ibc/Cargo.toml | 4 +- crates/ibc/src/client_state.rs | 60 ++++++------------- .../lightclients/ethereum/v1/ethereum.proto | 7 +-- .../src/prost/ibc.lightclients.ethereum.v1.rs | 12 +--- 4 files changed, 24 insertions(+), 59 deletions(-) diff --git a/crates/ibc/Cargo.toml b/crates/ibc/Cargo.toml index 6518427..0340344 100644 --- a/crates/ibc/Cargo.toml +++ b/crates/ibc/Cargo.toml @@ -16,5 +16,5 @@ hex = { version = "0.4.3", default-features = false, features = ["alloc", "serde ssz-rs = { git = "https://github.com/bluele/ssz_rs", branch = "serde-no-std", default-features = false, features = ["serde"] } ethereum-ibc-proto = { path = "../../proto", default-features = false } -ethereum-consensus = { git = "https://github.com/datachainlab/ethereum-light-client-rs", rev = "1ac6905ad7020fd377e66e0ac116d41509cca1fe", default-features = false } -ethereum-light-client-verifier = { git = "https://github.com/datachainlab/ethereum-light-client-rs", rev = "1ac6905ad7020fd377e66e0ac116d41509cca1fe", default-features = false } +ethereum-consensus = { git = "https://github.com/datachainlab/ethereum-light-client-rs", rev = "aac304f27372e8a0fb950522ee9cd6f41e43f56f", default-features = false } +ethereum-light-client-verifier = { git = "https://github.com/datachainlab/ethereum-light-client-rs", rev = "aac304f27372e8a0fb950522ee9cd6f41e43f56f", default-features = false } diff --git a/crates/ibc/src/client_state.rs b/crates/ibc/src/client_state.rs index 2aee40c..281398d 100644 --- a/crates/ibc/src/client_state.rs +++ b/crates/ibc/src/client_state.rs @@ -8,7 +8,7 @@ use crate::{eth_client_type, internal_prelude::*}; use core::time::Duration; use ethereum_consensus::beacon::{Epoch, Root, Slot, Version}; use ethereum_consensus::context::ChainContext; -use ethereum_consensus::fork::ForkParameters; +use ethereum_consensus::fork::{ForkParameter, ForkParameters}; use ethereum_consensus::preset; use ethereum_consensus::types::{Address, H256, U64}; use ethereum_ibc_proto::ibc::lightclients::ethereum::v1::{ClientState as RawClientState, Fork}; @@ -90,7 +90,7 @@ impl ClientState { .unix_timestamp() as u64, ); let current_slot = (current_timestamp - self.genesis_time) / self.seconds_per_slot - + self.fork_parameters.genesis_slot; + + self.fork_parameters.genesis_slot(); LightClientContext::new( self.fork_parameters.clone(), self.seconds_per_slot, @@ -549,32 +549,22 @@ impl TryFrom version } - let fork_parameters = value.fork_parameters.unwrap(); + let raw_fork_parameters = value.fork_parameters.unwrap(); + let fork_parameters = ForkParameters::new( + bytes_to_version(raw_fork_parameters.genesis_fork_version), + raw_fork_parameters + .forks + .into_iter() + .map(|f| ForkParameter::new(bytes_to_version(f.version), f.epoch.into())) + .collect(), + ); + fork_parameters.validate()?; let trust_level = value.trust_level.unwrap(); Ok(Self { genesis_validators_root: H256::from_slice(&value.genesis_validators_root), min_sync_committee_participants: value.min_sync_committee_participants.into(), genesis_time: value.genesis_time.into(), - fork_parameters: ForkParameters { - genesis_fork_version: bytes_to_version(fork_parameters.genesis_fork_version), - genesis_slot: fork_parameters.genesis_slot.into(), - altair_fork_version: bytes_to_version( - fork_parameters.altair.clone().unwrap().version, - ), - altair_fork_epoch: fork_parameters.altair.unwrap().epoch.into(), - bellatrix_fork_version: bytes_to_version( - fork_parameters.bellatrix.clone().unwrap().version, - ), - bellatrix_fork_epoch: fork_parameters.bellatrix.unwrap().epoch.into(), - capella_fork_version: bytes_to_version( - fork_parameters.capella.clone().unwrap().version, - ), - capella_fork_epoch: fork_parameters.capella.unwrap().epoch.into(), - eip4844_fork_version: bytes_to_version( - fork_parameters.eip4844.clone().unwrap().version, - ), - eip4844_fork_epoch: fork_parameters.eip4844.unwrap().epoch.into(), - }, + fork_parameters, seconds_per_slot: value.seconds_per_slot.into(), slots_per_epoch: value.slots_per_epoch.into(), epochs_per_sync_committee_period: value.epochs_per_sync_committee_period.into(), @@ -618,24 +608,12 @@ impl From> fo min_sync_committee_participants: value.min_sync_committee_participants.into(), genesis_time: value.genesis_time.into(), fork_parameters: Some(ProtoForkParameters { - genesis_fork_version: version_to_bytes(fork_parameters.genesis_fork_version), - genesis_slot: fork_parameters.genesis_slot.into(), - altair: Some(make_fork( - fork_parameters.altair_fork_version, - fork_parameters.altair_fork_epoch, - )), - bellatrix: Some(make_fork( - fork_parameters.bellatrix_fork_version, - fork_parameters.bellatrix_fork_epoch, - )), - capella: Some(make_fork( - fork_parameters.capella_fork_version, - fork_parameters.capella_fork_epoch, - )), - eip4844: Some(make_fork( - fork_parameters.eip4844_fork_version, - fork_parameters.eip4844_fork_epoch, - )), + genesis_fork_version: version_to_bytes(fork_parameters.genesis_version), + forks: fork_parameters + .forks + .into_iter() + .map(|f| make_fork(f.version, f.epoch)) + .collect(), }), seconds_per_slot: value.seconds_per_slot.into(), slots_per_epoch: value.slots_per_epoch.into(), diff --git a/proto/definitions/ibc/lightclients/ethereum/v1/ethereum.proto b/proto/definitions/ibc/lightclients/ethereum/v1/ethereum.proto index baa5d11..3e4a51d 100644 --- a/proto/definitions/ibc/lightclients/ethereum/v1/ethereum.proto +++ b/proto/definitions/ibc/lightclients/ethereum/v1/ethereum.proto @@ -48,12 +48,7 @@ message TrustedSyncCommittee { message ForkParameters { bytes genesis_fork_version = 1; - uint64 genesis_slot = 2; - - Fork altair = 3; - Fork bellatrix = 4; - Fork capella = 5; - Fork eip4844 = 6; + repeated Fork forks = 2; } message Fraction { diff --git a/proto/src/prost/ibc.lightclients.ethereum.v1.rs b/proto/src/prost/ibc.lightclients.ethereum.v1.rs index e9bb95f..96e78d4 100644 --- a/proto/src/prost/ibc.lightclients.ethereum.v1.rs +++ b/proto/src/prost/ibc.lightclients.ethereum.v1.rs @@ -77,16 +77,8 @@ pub struct TrustedSyncCommittee { pub struct ForkParameters { #[prost(bytes = "vec", tag = "1")] pub genesis_fork_version: ::prost::alloc::vec::Vec, - #[prost(uint64, tag = "2")] - pub genesis_slot: u64, - #[prost(message, optional, tag = "3")] - pub altair: ::core::option::Option, - #[prost(message, optional, tag = "4")] - pub bellatrix: ::core::option::Option, - #[prost(message, optional, tag = "5")] - pub capella: ::core::option::Option, - #[prost(message, optional, tag = "6")] - pub eip4844: ::core::option::Option, + #[prost(message, repeated, tag = "2")] + pub forks: ::prost::alloc::vec::Vec, } #[allow(clippy::derive_partial_eq_without_eq)] #[derive(Clone, PartialEq, ::prost::Message)]