Skip to content

Commit

Permalink
remove type parameter EXECUTION_PAYLOAD_TREE_DEPTH from `SyncProtoc…
Browse files Browse the repository at this point in the history
…olVerifier`

Signed-off-by: Jun Kimura <[email protected]>
  • Loading branch information
bluele committed Oct 27, 2024
1 parent ff98f0b commit fcab95a
Show file tree
Hide file tree
Showing 14 changed files with 167 additions and 50 deletions.
3 changes: 2 additions & 1 deletion crates/consensus/src/compute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ pub fn compute_domain<C: ChainContext>(
genesis_validators_root: Option<Root>,
) -> Result<Domain, Error> {
let fork_data_root = compute_fork_data_root(
fork_version.unwrap_or(ctx.fork_parameters().genesis_version.clone()),
fork_version.unwrap_or(ctx.fork_parameters().genesis_version().clone()),
genesis_validators_root.unwrap_or_default(),
)?;
let mut domain: [u8; 32] = Default::default();
Expand Down Expand Up @@ -96,6 +96,7 @@ mod tests {
#[test]
fn test_compute_timestamp_at_slot() {
let ctx = DefaultChainContext::new_with_config(1729846322.into(), get_minimal_config());
assert!(ctx.validate().is_ok(), "context is invalid");
assert_eq!(compute_timestamp_at_slot(&ctx, 0.into()), 1729846322.into());
assert_eq!(compute_timestamp_at_slot(&ctx, 1.into()), 1729846328.into());
assert_eq!(compute_timestamp_at_slot(&ctx, 2.into()), 1729846334.into());
Expand Down
19 changes: 15 additions & 4 deletions crates/consensus/src/config/goerli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,24 @@ pub fn get_config() -> Config {
fork_parameters: ForkParameters::new(
Version([0, 0, 16, 32]),
vec![
ForkParameter::new(Version([5, 0, 16, 32]), U64(u64::MAX)),
ForkParameter::new(Version([4, 0, 16, 32]), U64(231680)),
ForkParameter::new(Version([3, 0, 16, 32]), U64(162304)),
ForkParameter::new(Version([2, 0, 16, 32]), U64(112260)),
ForkParameter::new(Version([1, 0, 16, 32]), U64(36660)),
ForkParameter::new(Version([2, 0, 16, 32]), U64(112260)),
ForkParameter::new(Version([3, 0, 16, 32]), U64(162304)),
ForkParameter::new(Version([4, 0, 16, 32]), U64(231680)),
ForkParameter::new(Version([5, 0, 16, 32]), U64(u64::MAX)),
],
),
min_genesis_time: U64(1614588812),
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_config_validation() {
let config = get_config();
assert!(config.fork_parameters.validate().is_ok());
}
}
19 changes: 15 additions & 4 deletions crates/consensus/src/config/mainnet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,24 @@ pub fn get_config() -> Config {
fork_parameters: ForkParameters::new(
Version([0, 0, 0, 0]),
vec![
ForkParameter::new(Version([5, 0, 0, 0]), U64(u64::MAX)),
ForkParameter::new(Version([4, 0, 0, 0]), U64(269568)),
ForkParameter::new(Version([3, 0, 0, 0]), U64(194048)),
ForkParameter::new(Version([2, 0, 0, 0]), U64(144896)),
ForkParameter::new(Version([1, 0, 0, 0]), U64(74240)),
ForkParameter::new(Version([2, 0, 0, 0]), U64(144896)),
ForkParameter::new(Version([3, 0, 0, 0]), U64(194048)),
ForkParameter::new(Version([4, 0, 0, 0]), U64(269568)),
ForkParameter::new(Version([5, 0, 0, 0]), U64(u64::MAX)),
],
),
min_genesis_time: U64(1606824000),
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_config_validation() {
let config = get_config();
assert!(config.fork_parameters.validate().is_ok());
}
}
23 changes: 17 additions & 6 deletions crates/consensus/src/config/minimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,27 @@ pub fn get_config() -> Config {
fork_parameters: ForkParameters::new(
Version([0, 0, 0, 1]),
vec![
// deneb
ForkParameter::new(Version([4, 0, 0, 1]), U64(0)),
// capella
ForkParameter::new(Version([3, 0, 0, 1]), U64(0)),
// belatrix
ForkParameter::new(Version([2, 0, 0, 1]), U64(0)),
// altair
ForkParameter::new(Version([1, 0, 0, 1]), U64(0)),
// belatrix
ForkParameter::new(Version([2, 0, 0, 1]), U64(0)),
// capella
ForkParameter::new(Version([3, 0, 0, 1]), U64(0)),
// deneb
ForkParameter::new(Version([4, 0, 0, 1]), U64(0)),
],
),
min_genesis_time: U64(1578009600),
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_config_validation() {
let config = get_config();
assert!(config.fork_parameters.validate().is_ok());
}
}
19 changes: 15 additions & 4 deletions crates/consensus/src/config/sepolia.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,24 @@ pub fn get_config() -> Config {
fork_parameters: ForkParameters::new(
Version([144, 0, 0, 105]),
vec![
ForkParameter::new(Version([144, 0, 0, 116]), U64(u64::MAX)),
ForkParameter::new(Version([144, 0, 0, 115]), U64(132608)),
ForkParameter::new(Version([144, 0, 0, 114]), U64(56832)),
ForkParameter::new(Version([144, 0, 0, 113]), U64(100)),
ForkParameter::new(Version([144, 0, 0, 112]), U64(50)),
ForkParameter::new(Version([144, 0, 0, 113]), U64(100)),
ForkParameter::new(Version([144, 0, 0, 114]), U64(56832)),
ForkParameter::new(Version([144, 0, 0, 115]), U64(132608)),
ForkParameter::new(Version([144, 0, 0, 116]), U64(u64::MAX)),
],
),
min_genesis_time: U64(1655647200),
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_config_validation() {
let config = get_config();
assert!(config.fork_parameters.validate().is_ok());
}
}
6 changes: 6 additions & 0 deletions crates/consensus/src/context.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::{
beacon::{Epoch, Slot},
config::Config,
errors::Error,
fork::ForkParameters,
types::U64,
};
Expand Down Expand Up @@ -47,6 +48,11 @@ impl DefaultChainContext {
config.preset.EPOCHS_PER_SYNC_COMMITTEE_PERIOD,
)
}

pub fn validate(&self) -> Result<(), Error> {
self.fork_parameters.validate()?;
Ok(())
}
}

impl ChainContext for DefaultChainContext {
Expand Down
12 changes: 11 additions & 1 deletion crates/consensus/src/errors.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
use crate::{beacon::Root, bls::PublicKey, fork::ForkParameters, internal_prelude::*, types::H256};
use crate::{
beacon::{Root, Version},
bls::PublicKey,
fork::ForkParameters,
internal_prelude::*,
types::{H256, U64},
};
use displaydoc::Display;

#[derive(Debug, Display)]
Expand All @@ -21,6 +27,10 @@ pub enum Error {
InvalidAddressLength(usize, usize),
/// invalid fork parameters order: `{0:?}`
InvalidForkParamersOrder(ForkParameters),
/// invalid fork version: `epoch={0:?} fork={1:?} index={2}`
UnknownFork(U64, U64, usize),
/// the fork version does not support execution payload: `{0:?}`
NotSupportedExecutionPayload(Version),
/// other error: `{description}`
Other { description: String },
}
Expand Down
50 changes: 46 additions & 4 deletions crates/consensus/src/fork.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,32 @@ use crate::errors::Error;
use crate::internal_prelude::*;
use crate::types::U64;

#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub enum Fork {
Genesis(Version),
Altair(ForkParameter),
Bellatrix(ForkParameter),
Capella(ForkParameter),
Deneb(ForkParameter),
}

impl Fork {
pub fn execution_payload_tree_depth(&self) -> Result<usize, Error> {
match self {
Fork::Genesis(v) => Err(Error::NotSupportedExecutionPayload(v.clone())),
Fork::Altair(f) => Err(Error::NotSupportedExecutionPayload(f.version.clone())),
Fork::Bellatrix(_) => Ok(bellatrix::EXECUTION_PAYLOAD_TREE_DEPTH),
Fork::Capella(_) => Ok(capella::EXECUTION_PAYLOAD_TREE_DEPTH),
Fork::Deneb(_) => Ok(deneb::EXECUTION_PAYLOAD_TREE_DEPTH),
}
}
}

#[derive(Debug, Default, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct ForkParameters {
pub genesis_version: Version,
pub forks: Vec<ForkParameter>,
genesis_version: Version,
/// Forks in order of increasing epoch
forks: Vec<ForkParameter>,
}

impl ForkParameters {
Expand All @@ -22,7 +44,7 @@ impl ForkParameters {
}

pub fn validate(&self) -> Result<(), Error> {
if self.forks.windows(2).all(|f| f[0].epoch >= f[1].epoch) {
if self.forks.windows(2).all(|f| f[0].epoch <= f[1].epoch) {
Ok(())
} else {
Err(Error::InvalidForkParamersOrder(self.clone()))
Expand All @@ -33,14 +55,34 @@ impl ForkParameters {
U64(0)
}

pub fn genesis_version(&self) -> &Version {
&self.genesis_version
}

pub fn compute_fork_version(&self, epoch: Epoch) -> Result<&Version, Error> {
for fork in self.forks.iter() {
for fork in self.forks.iter().rev() {
if epoch >= fork.epoch {
return Ok(&fork.version);
}
}
Ok(&self.genesis_version)
}

pub fn compute_fork(&self, epoch: Epoch) -> Result<Fork, Error> {
for (i, fork) in self.forks.iter().enumerate().rev() {
if epoch >= fork.epoch {
let fork = fork.clone();
return Ok(match i {
0 => Fork::Altair(fork),
1 => Fork::Bellatrix(fork),
2 => Fork::Capella(fork),
3 => Fork::Deneb(fork),
_ => return Err(Error::UnknownFork(epoch, fork.epoch, i)),
});
}
}
Ok(Fork::Genesis(self.genesis_version.clone()))
}
}

#[derive(Debug, Default, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
Expand Down
3 changes: 2 additions & 1 deletion crates/consensus/src/fork/bellatrix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ pub fn gen_execution_payload_field_proof<
}

#[cfg(test)]
mod test {
mod tests {
use super::{
gen_execution_payload_field_proof, gen_execution_payload_proof, BeaconBlockHeader,
};
Expand Down Expand Up @@ -495,6 +495,7 @@ mod test {
// ensure that signing_root calculation is correct

let ctx = DefaultChainContext::new_with_config(0.into(), config::mainnet::get_config());
assert!(ctx.validate().is_ok(), "context is invalid");
let fork_version =
compute_fork_version(&ctx, compute_epoch_at_slot(&ctx, update.signature_slot)).unwrap();
let domain = compute_domain(
Expand Down
2 changes: 1 addition & 1 deletion crates/consensus/src/fork/capella.rs
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ pub fn gen_execution_payload_field_proof<
}

#[cfg(test)]
mod test {
mod tests {
use super::*;
use crate::beacon::BLOCK_BODY_EXECUTION_PAYLOAD_LEAF_INDEX;
use crate::merkle::is_valid_merkle_branch;
Expand Down
2 changes: 1 addition & 1 deletion crates/consensus/src/fork/deneb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ pub fn gen_execution_payload_field_proof<
}

#[cfg(test)]
mod test {
mod tests {
use super::*;
use crate::beacon::BLOCK_BODY_EXECUTION_PAYLOAD_LEAF_INDEX;
use crate::merkle::is_valid_merkle_branch;
Expand Down
9 changes: 4 additions & 5 deletions crates/light-client-cli/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ use ethereum_consensus::{
BlockNumber, EXECUTION_PAYLOAD_BLOCK_NUMBER_LEAF_INDEX,
EXECUTION_PAYLOAD_STATE_ROOT_LEAF_INDEX,
},
fork::deneb::{self, LightClientUpdate, EXECUTION_PAYLOAD_TREE_DEPTH},
fork::deneb::{self, LightClientUpdate},
sync_protocol::SyncCommitteePeriod,
types::{H256, U64},
};
use ethereum_light_client_verifier::{
consensus::SyncProtocolVerifier,
context::{ConsensusVerificationContext, Fraction, LightClientContext},
context::{ChainConsensusVerificationContext, Fraction, LightClientContext},
state::should_update_sync_committees,
updates::{deneb::ConsensusUpdateInfo, ConsensusUpdate},
};
Expand All @@ -45,7 +45,6 @@ pub struct LightClient<
chain: Chain,
verifier: SyncProtocolVerifier<
SYNC_COMMITTEE_SIZE,
EXECUTION_PAYLOAD_TREE_DEPTH,
LightClientStore<SYNC_COMMITTEE_SIZE, BYTES_PER_LOGS_BLOOM, MAX_EXTRA_DATA_BYTES>,
>,
genesis_time: U64,
Expand Down Expand Up @@ -245,7 +244,7 @@ impl<

async fn process_light_client_update(
&self,
vctx: &(impl ChainContext + ConsensusVerificationContext),
vctx: &impl ChainConsensusVerificationContext,
update: LightClientUpdate<SYNC_COMMITTEE_SIZE, BYTES_PER_LOGS_BLOOM, MAX_EXTRA_DATA_BYTES>,
state: &LightClientStore<SYNC_COMMITTEE_SIZE, BYTES_PER_LOGS_BLOOM, MAX_EXTRA_DATA_BYTES>,
) -> Result<
Expand Down Expand Up @@ -304,7 +303,7 @@ impl<
}
}

fn build_verification_context(&self) -> impl ChainContext + ConsensusVerificationContext {
fn build_verification_context(&self) -> impl ChainConsensusVerificationContext {
LightClientContext::new_with_config(
self.ctx.config.clone(),
self.genesis_validators_root,
Expand Down
Loading

0 comments on commit fcab95a

Please sign in to comment.