Fix electra light client types (#6361)

* persist light client updates

* update beacon chain to serve light client updates

* resolve todos

* cache best update

* extend cache parts

* is better light client update

* resolve merge conflict

* initial api changes

* add lc update db column

* fmt

* added tests

* add sim

* Merge branch 'unstable' of https://github.com/sigp/lighthouse into persist-light-client-updates

* fix some weird issues with the simulator

* tests

* Merge branch 'unstable' of https://github.com/sigp/lighthouse into persist-light-client-updates

* test changes

* merge conflict

* testing

* started work on ef tests and some code clean up

* update tests

* linting

* noop pre altair, were still failing on electra though

* allow for zeroed light client header

* Merge branch 'unstable' of https://github.com/sigp/lighthouse into persist-light-client-updates

* merge unstable

* remove unwraps

* remove unwraps

* fetch bootstrap without always querying for state

* storing bootstrap parts in db

* mroe code cleanup

* test

* prune sync committee branches from dropped chains

* Update light_client_update.rs

* merge unstable

* move functionality to helper methods

* refactor is best update fn

* refactor is best update fn

* improve organization of light client server cache logic

* fork diget calc, and only spawn as many blcoks as we need for the lc update test

* resovle merge conflict

* add electra bootstrap logic, add logic to cache current sync committee

* add latest sync committe branch cache

* fetch lc update from the cache if it exists

* fmt

* Fix beacon_chain tests

* Add debug code to update ranking_order ef test

* Fix compare code

* merge conflicts

* merge conflict

* add better error messaging

* resolve merge conflicts

* remove lc update from basicsim

* rename sync comittte variable and fix persist condition

* refactor get_light_client_update logic

* add better comments, return helpful error messages over http and rpc

* pruning canonical non checkpoint slots

* fix test

* rerun test

* update pruning logic, add tests

* fix tests

* fix imports

* fmt

* refactor db code

* Refactor db method

* Refactor db method

* lc electra changes

* Merge branch 'unstable' of https://github.com/sigp/lighthouse into light-client-electra

* add additional comments

* testing lc merkle changes

* lc electra

* update struct defs

* Merge branch 'unstable' of https://github.com/sigp/lighthouse into light-client-electra

* fix merge

* Merge branch 'unstable' of https://github.com/sigp/lighthouse into persist-light-client-bootstrap

* fix merge

* linting

* merge conflict

* prevent overflow

* enable lc server for http api tests

* Merge branch 'unstable' of https://github.com/sigp/lighthouse into light-client-electra

* get tests working:

* remove related TODOs

* fix test lint

* Merge branch 'persist-light-client-bootstrap' of https://github.com/eserilev/lighthouse into light-client-electra

* fix tests

* fix conflicts

* remove prints

* Merge branch 'persist-light-client-bootstrap' of https://github.com/eserilev/lighthouse into light-client-electra

* remove warning

* resolve conflicts

* merge conflicts

* linting

* remove comments

* cleanup

* linting

* Merge branch 'unstable' of https://github.com/sigp/lighthouse into light-client-electra

* pre/post electra light client cached data

* add proof type alias

* move is_empty_branch method out of impl

* add ssz tests for all forks

* refactor beacon state proof codepaths

* rename method

* fmt

* clean up proof logic

* refactor merkle proof api

* fmt

* Merge branch 'unstable' into light-client-electra

* Use superstruct mapping macros

* Merge branch 'unstable' of https://github.com/sigp/lighthouse into light-client-electra

* rename proof to merkleproof

* fmt

* Resolve merge conflicts

* merge conflicts
This commit is contained in:
Eitan Seri-Levi
2024-10-24 22:19:13 -07:00
committed by GitHub
parent 40d3423193
commit 9d069a9588
14 changed files with 490 additions and 185 deletions

View File

@@ -1,25 +1,19 @@
use crate::errors::BeaconChainError; use crate::errors::BeaconChainError;
use crate::{metrics, BeaconChainTypes, BeaconStore}; use crate::{metrics, BeaconChainTypes, BeaconStore};
use eth2::types::light_client_update::CurrentSyncCommitteeProofLen;
use parking_lot::{Mutex, RwLock}; use parking_lot::{Mutex, RwLock};
use safe_arith::SafeArith; use safe_arith::SafeArith;
use slog::{debug, Logger}; use slog::{debug, Logger};
use ssz::Decode; use ssz::Decode;
use ssz_types::FixedVector;
use std::num::NonZeroUsize; use std::num::NonZeroUsize;
use std::sync::Arc; use std::sync::Arc;
use store::DBColumn; use store::DBColumn;
use store::KeyValueStore; use store::KeyValueStore;
use tree_hash::TreeHash; use tree_hash::TreeHash;
use types::light_client_update::{
FinalizedRootProofLen, NextSyncCommitteeProofLen, CURRENT_SYNC_COMMITTEE_INDEX,
FINALIZED_ROOT_INDEX, NEXT_SYNC_COMMITTEE_INDEX,
};
use types::non_zero_usize::new_non_zero_usize; use types::non_zero_usize::new_non_zero_usize;
use types::{ use types::{
BeaconBlockRef, BeaconState, ChainSpec, Checkpoint, EthSpec, ForkName, Hash256, BeaconBlockRef, BeaconState, ChainSpec, Checkpoint, EthSpec, ForkName, Hash256,
LightClientBootstrap, LightClientFinalityUpdate, LightClientOptimisticUpdate, LightClientBootstrap, LightClientFinalityUpdate, LightClientOptimisticUpdate,
LightClientUpdate, Slot, SyncAggregate, SyncCommittee, LightClientUpdate, MerkleProof, Slot, SyncAggregate, SyncCommittee,
}; };
/// A prev block cache miss requires to re-generate the state of the post-parent block. Items in the /// A prev block cache miss requires to re-generate the state of the post-parent block. Items in the
@@ -69,17 +63,14 @@ impl<T: BeaconChainTypes> LightClientServerCache<T> {
block_post_state: &mut BeaconState<T::EthSpec>, block_post_state: &mut BeaconState<T::EthSpec>,
) -> Result<(), BeaconChainError> { ) -> Result<(), BeaconChainError> {
let _timer = metrics::start_timer(&metrics::LIGHT_CLIENT_SERVER_CACHE_STATE_DATA_TIMES); let _timer = metrics::start_timer(&metrics::LIGHT_CLIENT_SERVER_CACHE_STATE_DATA_TIMES);
let fork_name = spec.fork_name_at_slot::<T::EthSpec>(block.slot());
// Only post-altair // Only post-altair
if spec.fork_name_at_slot::<T::EthSpec>(block.slot()) == ForkName::Base { if fork_name.altair_enabled() {
return Ok(()); // Persist in memory cache for a descendent block
let cached_data = LightClientCachedData::from_state(block_post_state)?;
self.prev_block_cache.lock().put(block_root, cached_data);
} }
// Persist in memory cache for a descendent block
let cached_data = LightClientCachedData::from_state(block_post_state)?;
self.prev_block_cache.lock().put(block_root, cached_data);
Ok(()) Ok(())
} }
@@ -413,16 +404,12 @@ impl<T: BeaconChainTypes> Default for LightClientServerCache<T> {
} }
} }
type FinalityBranch = FixedVector<Hash256, FinalizedRootProofLen>;
type NextSyncCommitteeBranch = FixedVector<Hash256, NextSyncCommitteeProofLen>;
type CurrentSyncCommitteeBranch = FixedVector<Hash256, CurrentSyncCommitteeProofLen>;
#[derive(Clone)] #[derive(Clone)]
struct LightClientCachedData<E: EthSpec> { struct LightClientCachedData<E: EthSpec> {
finalized_checkpoint: Checkpoint, finalized_checkpoint: Checkpoint,
finality_branch: FinalityBranch, finality_branch: MerkleProof,
next_sync_committee_branch: NextSyncCommitteeBranch, next_sync_committee_branch: MerkleProof,
current_sync_committee_branch: CurrentSyncCommitteeBranch, current_sync_committee_branch: MerkleProof,
next_sync_committee: Arc<SyncCommittee<E>>, next_sync_committee: Arc<SyncCommittee<E>>,
current_sync_committee: Arc<SyncCommittee<E>>, current_sync_committee: Arc<SyncCommittee<E>>,
finalized_block_root: Hash256, finalized_block_root: Hash256,
@@ -430,17 +417,18 @@ struct LightClientCachedData<E: EthSpec> {
impl<E: EthSpec> LightClientCachedData<E> { impl<E: EthSpec> LightClientCachedData<E> {
fn from_state(state: &mut BeaconState<E>) -> Result<Self, BeaconChainError> { fn from_state(state: &mut BeaconState<E>) -> Result<Self, BeaconChainError> {
let (finality_branch, next_sync_committee_branch, current_sync_committee_branch) = (
state.compute_finalized_root_proof()?,
state.compute_current_sync_committee_proof()?,
state.compute_next_sync_committee_proof()?,
);
Ok(Self { Ok(Self {
finalized_checkpoint: state.finalized_checkpoint(), finalized_checkpoint: state.finalized_checkpoint(),
finality_branch: state.compute_merkle_proof(FINALIZED_ROOT_INDEX)?.into(), finality_branch,
next_sync_committee: state.next_sync_committee()?.clone(), next_sync_committee: state.next_sync_committee()?.clone(),
current_sync_committee: state.current_sync_committee()?.clone(), current_sync_committee: state.current_sync_committee()?.clone(),
next_sync_committee_branch: state next_sync_committee_branch,
.compute_merkle_proof(NEXT_SYNC_COMMITTEE_INDEX)? current_sync_committee_branch,
.into(),
current_sync_committee_branch: state
.compute_merkle_proof(CURRENT_SYNC_COMMITTEE_INDEX)?
.into(),
finalized_block_root: state.finalized_checkpoint().root, finalized_block_root: state.finalized_checkpoint().root,
}) })
} }

View File

@@ -206,13 +206,6 @@ async fn light_client_bootstrap_test() {
.build() .build()
.expect("should build"); .expect("should build");
let current_state = harness.get_current_state();
if ForkName::Electra == current_state.fork_name_unchecked() {
// TODO(electra) fix beacon state `compute_merkle_proof`
return;
}
let finalized_checkpoint = beacon_chain let finalized_checkpoint = beacon_chain
.canonical_head .canonical_head
.cached_head() .cached_head()
@@ -353,11 +346,6 @@ async fn light_client_updates_test() {
let current_state = harness.get_current_state(); let current_state = harness.get_current_state();
if ForkName::Electra == current_state.fork_name_unchecked() {
// TODO(electra) fix beacon state `compute_merkle_proof`
return;
}
// calculate the sync period from the previous slot // calculate the sync period from the previous slot
let sync_period = (current_state.slot() - Slot::new(1)) let sync_period = (current_state.slot() - Slot::new(1))
.epoch(E::slots_per_epoch()) .epoch(E::slots_per_epoch())

View File

@@ -44,7 +44,6 @@ use std::path::Path;
use std::sync::Arc; use std::sync::Arc;
use std::time::Duration; use std::time::Duration;
use types::data_column_sidecar::{ColumnIndex, DataColumnSidecar, DataColumnSidecarList}; use types::data_column_sidecar::{ColumnIndex, DataColumnSidecar, DataColumnSidecarList};
use types::light_client_update::CurrentSyncCommitteeProofLen;
use types::*; use types::*;
/// On-disk database that stores finalized states efficiently. /// On-disk database that stores finalized states efficiently.
@@ -641,15 +640,14 @@ impl<E: EthSpec, Hot: ItemStore<E>, Cold: ItemStore<E>> HotColdDB<E, Hot, Cold>
pub fn get_sync_committee_branch( pub fn get_sync_committee_branch(
&self, &self,
block_root: &Hash256, block_root: &Hash256,
) -> Result<Option<FixedVector<Hash256, CurrentSyncCommitteeProofLen>>, Error> { ) -> Result<Option<MerkleProof>, Error> {
let column = DBColumn::SyncCommitteeBranch; let column = DBColumn::SyncCommitteeBranch;
if let Some(bytes) = self if let Some(bytes) = self
.hot_db .hot_db
.get_bytes(column.into(), &block_root.as_ssz_bytes())? .get_bytes(column.into(), &block_root.as_ssz_bytes())?
{ {
let sync_committee_branch: FixedVector<Hash256, CurrentSyncCommitteeProofLen> = let sync_committee_branch = Vec::<Hash256>::from_ssz_bytes(&bytes)?;
FixedVector::from_ssz_bytes(&bytes)?;
return Ok(Some(sync_committee_branch)); return Ok(Some(sync_committee_branch));
} }
@@ -677,7 +675,7 @@ impl<E: EthSpec, Hot: ItemStore<E>, Cold: ItemStore<E>> HotColdDB<E, Hot, Cold>
pub fn store_sync_committee_branch( pub fn store_sync_committee_branch(
&self, &self,
block_root: Hash256, block_root: Hash256,
sync_committee_branch: &FixedVector<Hash256, CurrentSyncCommitteeProofLen>, sync_committee_branch: &MerkleProof,
) -> Result<(), Error> { ) -> Result<(), Error> {
let column = DBColumn::SyncCommitteeBranch; let column = DBColumn::SyncCommitteeBranch;
self.hot_db.put_bytes( self.hot_db.put_bytes(

View File

@@ -2506,33 +2506,64 @@ impl<E: EthSpec> BeaconState<E> {
Ok(()) Ok(())
} }
pub fn compute_merkle_proof(&self, generalized_index: usize) -> Result<Vec<Hash256>, Error> { pub fn compute_current_sync_committee_proof(&self) -> Result<Vec<Hash256>, Error> {
// 1. Convert generalized index to field index. // Sync committees are top-level fields, subtract off the generalized indices
let field_index = match generalized_index { // for the internal nodes. Result should be 22 or 23, the field offset of the committee
// in the `BeaconState`:
// https://github.com/ethereum/consensus-specs/blob/dev/specs/altair/beacon-chain.md#beaconstate
let field_index = if self.fork_name_unchecked().electra_enabled() {
light_client_update::CURRENT_SYNC_COMMITTEE_INDEX_ELECTRA
} else {
light_client_update::CURRENT_SYNC_COMMITTEE_INDEX light_client_update::CURRENT_SYNC_COMMITTEE_INDEX
| light_client_update::NEXT_SYNC_COMMITTEE_INDEX => {
// Sync committees are top-level fields, subtract off the generalized indices
// for the internal nodes. Result should be 22 or 23, the field offset of the committee
// in the `BeaconState`:
// https://github.com/ethereum/consensus-specs/blob/dev/specs/altair/beacon-chain.md#beaconstate
generalized_index
.checked_sub(self.num_fields_pow2())
.ok_or(Error::IndexNotSupported(generalized_index))?
}
light_client_update::FINALIZED_ROOT_INDEX => {
// Finalized root is the right child of `finalized_checkpoint`, divide by two to get
// the generalized index of `state.finalized_checkpoint`.
let finalized_checkpoint_generalized_index = generalized_index / 2;
// Subtract off the internal nodes. Result should be 105/2 - 32 = 20 which matches
// position of `finalized_checkpoint` in `BeaconState`.
finalized_checkpoint_generalized_index
.checked_sub(self.num_fields_pow2())
.ok_or(Error::IndexNotSupported(generalized_index))?
}
_ => return Err(Error::IndexNotSupported(generalized_index)),
}; };
let leaves = self.get_beacon_state_leaves();
self.generate_proof(field_index, &leaves)
}
// 2. Get all `BeaconState` leaves. pub fn compute_next_sync_committee_proof(&self) -> Result<Vec<Hash256>, Error> {
// Sync committees are top-level fields, subtract off the generalized indices
// for the internal nodes. Result should be 22 or 23, the field offset of the committee
// in the `BeaconState`:
// https://github.com/ethereum/consensus-specs/blob/dev/specs/altair/beacon-chain.md#beaconstate
let field_index = if self.fork_name_unchecked().electra_enabled() {
light_client_update::NEXT_SYNC_COMMITTEE_INDEX_ELECTRA
} else {
light_client_update::NEXT_SYNC_COMMITTEE_INDEX
};
let leaves = self.get_beacon_state_leaves();
self.generate_proof(field_index, &leaves)
}
pub fn compute_finalized_root_proof(&self) -> Result<Vec<Hash256>, Error> {
// Finalized root is the right child of `finalized_checkpoint`, divide by two to get
// the generalized index of `state.finalized_checkpoint`.
let field_index = if self.fork_name_unchecked().electra_enabled() {
// Index should be 169/2 - 64 = 20 which matches the position
// of `finalized_checkpoint` in `BeaconState`
light_client_update::FINALIZED_ROOT_INDEX_ELECTRA
} else {
// Index should be 105/2 - 32 = 20 which matches the position
// of `finalized_checkpoint` in `BeaconState`
light_client_update::FINALIZED_ROOT_INDEX
};
let leaves = self.get_beacon_state_leaves();
let mut proof = self.generate_proof(field_index, &leaves)?;
proof.insert(0, self.finalized_checkpoint().epoch.tree_hash_root());
Ok(proof)
}
fn generate_proof(
&self,
field_index: usize,
leaves: &[Hash256],
) -> Result<Vec<Hash256>, Error> {
let depth = self.num_fields_pow2().ilog2() as usize;
let tree = merkle_proof::MerkleTree::create(leaves, depth);
let (_, proof) = tree.generate_proof(field_index, depth)?;
Ok(proof)
}
fn get_beacon_state_leaves(&self) -> Vec<Hash256> {
let mut leaves = vec![]; let mut leaves = vec![];
#[allow(clippy::arithmetic_side_effects)] #[allow(clippy::arithmetic_side_effects)]
match self { match self {
@@ -2568,18 +2599,7 @@ impl<E: EthSpec> BeaconState<E> {
} }
}; };
// 3. Make deposit tree. leaves
// Use the depth of the `BeaconState` fields (i.e. `log2(32) = 5`).
let depth = light_client_update::CURRENT_SYNC_COMMITTEE_PROOF_LEN;
let tree = merkle_proof::MerkleTree::create(&leaves, depth);
let (_, mut proof) = tree.generate_proof(field_index, depth)?;
// 4. If we're proving the finalized root, patch in the finalized epoch to complete the proof.
if generalized_index == light_client_update::FINALIZED_ROOT_INDEX {
proof.insert(0, self.finalized_checkpoint().epoch.tree_hash_root());
}
Ok(proof)
} }
} }

View File

@@ -200,7 +200,7 @@ pub use crate::light_client_optimistic_update::{
}; };
pub use crate::light_client_update::{ pub use crate::light_client_update::{
Error as LightClientUpdateError, LightClientUpdate, LightClientUpdateAltair, Error as LightClientUpdateError, LightClientUpdate, LightClientUpdateAltair,
LightClientUpdateCapella, LightClientUpdateDeneb, LightClientUpdateElectra, LightClientUpdateCapella, LightClientUpdateDeneb, LightClientUpdateElectra, MerkleProof,
}; };
pub use crate::participation_flags::ParticipationFlags; pub use crate::participation_flags::ParticipationFlags;
pub use crate::payload::{ pub use crate::payload::{

View File

@@ -57,7 +57,16 @@ pub struct LightClientBootstrap<E: EthSpec> {
/// The `SyncCommittee` used in the requested period. /// The `SyncCommittee` used in the requested period.
pub current_sync_committee: Arc<SyncCommittee<E>>, pub current_sync_committee: Arc<SyncCommittee<E>>,
/// Merkle proof for sync committee /// Merkle proof for sync committee
#[superstruct(
only(Altair, Capella, Deneb),
partial_getter(rename = "current_sync_committee_branch_altair")
)]
pub current_sync_committee_branch: FixedVector<Hash256, CurrentSyncCommitteeProofLen>, pub current_sync_committee_branch: FixedVector<Hash256, CurrentSyncCommitteeProofLen>,
#[superstruct(
only(Electra),
partial_getter(rename = "current_sync_committee_branch_electra")
)]
pub current_sync_committee_branch: FixedVector<Hash256, CurrentSyncCommitteeProofLenElectra>,
} }
impl<E: EthSpec> LightClientBootstrap<E> { impl<E: EthSpec> LightClientBootstrap<E> {
@@ -115,7 +124,7 @@ impl<E: EthSpec> LightClientBootstrap<E> {
pub fn new( pub fn new(
block: &SignedBlindedBeaconBlock<E>, block: &SignedBlindedBeaconBlock<E>,
current_sync_committee: Arc<SyncCommittee<E>>, current_sync_committee: Arc<SyncCommittee<E>>,
current_sync_committee_branch: FixedVector<Hash256, CurrentSyncCommitteeProofLen>, current_sync_committee_branch: Vec<Hash256>,
chain_spec: &ChainSpec, chain_spec: &ChainSpec,
) -> Result<Self, Error> { ) -> Result<Self, Error> {
let light_client_bootstrap = match block let light_client_bootstrap = match block
@@ -126,22 +135,22 @@ impl<E: EthSpec> LightClientBootstrap<E> {
ForkName::Altair | ForkName::Bellatrix => Self::Altair(LightClientBootstrapAltair { ForkName::Altair | ForkName::Bellatrix => Self::Altair(LightClientBootstrapAltair {
header: LightClientHeaderAltair::block_to_light_client_header(block)?, header: LightClientHeaderAltair::block_to_light_client_header(block)?,
current_sync_committee, current_sync_committee,
current_sync_committee_branch, current_sync_committee_branch: current_sync_committee_branch.into(),
}), }),
ForkName::Capella => Self::Capella(LightClientBootstrapCapella { ForkName::Capella => Self::Capella(LightClientBootstrapCapella {
header: LightClientHeaderCapella::block_to_light_client_header(block)?, header: LightClientHeaderCapella::block_to_light_client_header(block)?,
current_sync_committee, current_sync_committee,
current_sync_committee_branch, current_sync_committee_branch: current_sync_committee_branch.into(),
}), }),
ForkName::Deneb => Self::Deneb(LightClientBootstrapDeneb { ForkName::Deneb => Self::Deneb(LightClientBootstrapDeneb {
header: LightClientHeaderDeneb::block_to_light_client_header(block)?, header: LightClientHeaderDeneb::block_to_light_client_header(block)?,
current_sync_committee, current_sync_committee,
current_sync_committee_branch, current_sync_committee_branch: current_sync_committee_branch.into(),
}), }),
ForkName::Electra => Self::Electra(LightClientBootstrapElectra { ForkName::Electra => Self::Electra(LightClientBootstrapElectra {
header: LightClientHeaderElectra::block_to_light_client_header(block)?, header: LightClientHeaderElectra::block_to_light_client_header(block)?,
current_sync_committee, current_sync_committee,
current_sync_committee_branch, current_sync_committee_branch: current_sync_committee_branch.into(),
}), }),
}; };
@@ -155,9 +164,7 @@ impl<E: EthSpec> LightClientBootstrap<E> {
) -> Result<Self, Error> { ) -> Result<Self, Error> {
let mut header = beacon_state.latest_block_header().clone(); let mut header = beacon_state.latest_block_header().clone();
header.state_root = beacon_state.update_tree_hash_cache()?; header.state_root = beacon_state.update_tree_hash_cache()?;
let current_sync_committee_branch = let current_sync_committee_branch = beacon_state.compute_current_sync_committee_proof()?;
FixedVector::new(beacon_state.compute_merkle_proof(CURRENT_SYNC_COMMITTEE_INDEX)?)?;
let current_sync_committee = beacon_state.current_sync_committee()?.clone(); let current_sync_committee = beacon_state.current_sync_committee()?.clone();
let light_client_bootstrap = match block let light_client_bootstrap = match block
@@ -168,22 +175,22 @@ impl<E: EthSpec> LightClientBootstrap<E> {
ForkName::Altair | ForkName::Bellatrix => Self::Altair(LightClientBootstrapAltair { ForkName::Altair | ForkName::Bellatrix => Self::Altair(LightClientBootstrapAltair {
header: LightClientHeaderAltair::block_to_light_client_header(block)?, header: LightClientHeaderAltair::block_to_light_client_header(block)?,
current_sync_committee, current_sync_committee,
current_sync_committee_branch, current_sync_committee_branch: current_sync_committee_branch.into(),
}), }),
ForkName::Capella => Self::Capella(LightClientBootstrapCapella { ForkName::Capella => Self::Capella(LightClientBootstrapCapella {
header: LightClientHeaderCapella::block_to_light_client_header(block)?, header: LightClientHeaderCapella::block_to_light_client_header(block)?,
current_sync_committee, current_sync_committee,
current_sync_committee_branch, current_sync_committee_branch: current_sync_committee_branch.into(),
}), }),
ForkName::Deneb => Self::Deneb(LightClientBootstrapDeneb { ForkName::Deneb => Self::Deneb(LightClientBootstrapDeneb {
header: LightClientHeaderDeneb::block_to_light_client_header(block)?, header: LightClientHeaderDeneb::block_to_light_client_header(block)?,
current_sync_committee, current_sync_committee,
current_sync_committee_branch, current_sync_committee_branch: current_sync_committee_branch.into(),
}), }),
ForkName::Electra => Self::Electra(LightClientBootstrapElectra { ForkName::Electra => Self::Electra(LightClientBootstrapElectra {
header: LightClientHeaderElectra::block_to_light_client_header(block)?, header: LightClientHeaderElectra::block_to_light_client_header(block)?,
current_sync_committee, current_sync_committee,
current_sync_committee_branch, current_sync_committee_branch: current_sync_committee_branch.into(),
}), }),
}; };
@@ -210,8 +217,28 @@ impl<E: EthSpec> ForkVersionDeserialize for LightClientBootstrap<E> {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; // `ssz_tests!` can only be defined once per namespace
use crate::MainnetEthSpec; #[cfg(test)]
mod altair {
use crate::{LightClientBootstrapAltair, MainnetEthSpec};
ssz_tests!(LightClientBootstrapAltair<MainnetEthSpec>);
}
ssz_tests!(LightClientBootstrapDeneb<MainnetEthSpec>); #[cfg(test)]
mod capella {
use crate::{LightClientBootstrapCapella, MainnetEthSpec};
ssz_tests!(LightClientBootstrapCapella<MainnetEthSpec>);
}
#[cfg(test)]
mod deneb {
use crate::{LightClientBootstrapDeneb, MainnetEthSpec};
ssz_tests!(LightClientBootstrapDeneb<MainnetEthSpec>);
}
#[cfg(test)]
mod electra {
use crate::{LightClientBootstrapElectra, MainnetEthSpec};
ssz_tests!(LightClientBootstrapElectra<MainnetEthSpec>);
}
} }

View File

@@ -63,8 +63,13 @@ pub struct LightClientFinalityUpdate<E: EthSpec> {
#[superstruct(only(Electra), partial_getter(rename = "finalized_header_electra"))] #[superstruct(only(Electra), partial_getter(rename = "finalized_header_electra"))]
pub finalized_header: LightClientHeaderElectra<E>, pub finalized_header: LightClientHeaderElectra<E>,
/// Merkle proof attesting finalized header. /// Merkle proof attesting finalized header.
#[test_random(default)] #[superstruct(
only(Altair, Capella, Deneb),
partial_getter(rename = "finality_branch_altair")
)]
pub finality_branch: FixedVector<Hash256, FinalizedRootProofLen>, pub finality_branch: FixedVector<Hash256, FinalizedRootProofLen>,
#[superstruct(only(Electra), partial_getter(rename = "finality_branch_electra"))]
pub finality_branch: FixedVector<Hash256, FinalizedRootProofLenElectra>,
/// current sync aggregate /// current sync aggregate
pub sync_aggregate: SyncAggregate<E>, pub sync_aggregate: SyncAggregate<E>,
/// Slot of the sync aggregated signature /// Slot of the sync aggregated signature
@@ -75,7 +80,7 @@ impl<E: EthSpec> LightClientFinalityUpdate<E> {
pub fn new( pub fn new(
attested_block: &SignedBlindedBeaconBlock<E>, attested_block: &SignedBlindedBeaconBlock<E>,
finalized_block: &SignedBlindedBeaconBlock<E>, finalized_block: &SignedBlindedBeaconBlock<E>,
finality_branch: FixedVector<Hash256, FinalizedRootProofLen>, finality_branch: Vec<Hash256>,
sync_aggregate: SyncAggregate<E>, sync_aggregate: SyncAggregate<E>,
signature_slot: Slot, signature_slot: Slot,
chain_spec: &ChainSpec, chain_spec: &ChainSpec,
@@ -92,7 +97,7 @@ impl<E: EthSpec> LightClientFinalityUpdate<E> {
finalized_header: LightClientHeaderAltair::block_to_light_client_header( finalized_header: LightClientHeaderAltair::block_to_light_client_header(
finalized_block, finalized_block,
)?, )?,
finality_branch, finality_branch: finality_branch.into(),
sync_aggregate, sync_aggregate,
signature_slot, signature_slot,
}) })
@@ -104,7 +109,7 @@ impl<E: EthSpec> LightClientFinalityUpdate<E> {
finalized_header: LightClientHeaderCapella::block_to_light_client_header( finalized_header: LightClientHeaderCapella::block_to_light_client_header(
finalized_block, finalized_block,
)?, )?,
finality_branch, finality_branch: finality_branch.into(),
sync_aggregate, sync_aggregate,
signature_slot, signature_slot,
}), }),
@@ -115,7 +120,7 @@ impl<E: EthSpec> LightClientFinalityUpdate<E> {
finalized_header: LightClientHeaderDeneb::block_to_light_client_header( finalized_header: LightClientHeaderDeneb::block_to_light_client_header(
finalized_block, finalized_block,
)?, )?,
finality_branch, finality_branch: finality_branch.into(),
sync_aggregate, sync_aggregate,
signature_slot, signature_slot,
}), }),
@@ -126,7 +131,7 @@ impl<E: EthSpec> LightClientFinalityUpdate<E> {
finalized_header: LightClientHeaderElectra::block_to_light_client_header( finalized_header: LightClientHeaderElectra::block_to_light_client_header(
finalized_block, finalized_block,
)?, )?,
finality_branch, finality_branch: finality_branch.into(),
sync_aggregate, sync_aggregate,
signature_slot, signature_slot,
}), }),
@@ -226,8 +231,28 @@ impl<E: EthSpec> ForkVersionDeserialize for LightClientFinalityUpdate<E> {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; // `ssz_tests!` can only be defined once per namespace
use crate::MainnetEthSpec; #[cfg(test)]
mod altair {
use crate::{LightClientFinalityUpdateAltair, MainnetEthSpec};
ssz_tests!(LightClientFinalityUpdateAltair<MainnetEthSpec>);
}
ssz_tests!(LightClientFinalityUpdateDeneb<MainnetEthSpec>); #[cfg(test)]
mod capella {
use crate::{LightClientFinalityUpdateCapella, MainnetEthSpec};
ssz_tests!(LightClientFinalityUpdateCapella<MainnetEthSpec>);
}
#[cfg(test)]
mod deneb {
use crate::{LightClientFinalityUpdateDeneb, MainnetEthSpec};
ssz_tests!(LightClientFinalityUpdateDeneb<MainnetEthSpec>);
}
#[cfg(test)]
mod electra {
use crate::{LightClientFinalityUpdateElectra, MainnetEthSpec};
ssz_tests!(LightClientFinalityUpdateElectra<MainnetEthSpec>);
}
} }

View File

@@ -307,3 +307,31 @@ impl<E: EthSpec> ForkVersionDeserialize for LightClientHeader<E> {
} }
} }
} }
#[cfg(test)]
mod tests {
// `ssz_tests!` can only be defined once per namespace
#[cfg(test)]
mod altair {
use crate::{LightClientHeaderAltair, MainnetEthSpec};
ssz_tests!(LightClientHeaderAltair<MainnetEthSpec>);
}
#[cfg(test)]
mod capella {
use crate::{LightClientHeaderCapella, MainnetEthSpec};
ssz_tests!(LightClientHeaderCapella<MainnetEthSpec>);
}
#[cfg(test)]
mod deneb {
use crate::{LightClientHeaderDeneb, MainnetEthSpec};
ssz_tests!(LightClientHeaderDeneb<MainnetEthSpec>);
}
#[cfg(test)]
mod electra {
use crate::{LightClientHeaderElectra, MainnetEthSpec};
ssz_tests!(LightClientHeaderElectra<MainnetEthSpec>);
}
}

View File

@@ -214,8 +214,28 @@ impl<E: EthSpec> ForkVersionDeserialize for LightClientOptimisticUpdate<E> {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; // `ssz_tests!` can only be defined once per namespace
use crate::MainnetEthSpec; #[cfg(test)]
mod altair {
use crate::{LightClientOptimisticUpdateAltair, MainnetEthSpec};
ssz_tests!(LightClientOptimisticUpdateAltair<MainnetEthSpec>);
}
ssz_tests!(LightClientOptimisticUpdateDeneb<MainnetEthSpec>); #[cfg(test)]
mod capella {
use crate::{LightClientOptimisticUpdateCapella, MainnetEthSpec};
ssz_tests!(LightClientOptimisticUpdateCapella<MainnetEthSpec>);
}
#[cfg(test)]
mod deneb {
use crate::{LightClientOptimisticUpdateDeneb, MainnetEthSpec};
ssz_tests!(LightClientOptimisticUpdateDeneb<MainnetEthSpec>);
}
#[cfg(test)]
mod electra {
use crate::{LightClientOptimisticUpdateElectra, MainnetEthSpec};
ssz_tests!(LightClientOptimisticUpdateElectra<MainnetEthSpec>);
}
} }

View File

@@ -14,7 +14,7 @@ use serde_json::Value;
use ssz::{Decode, Encode}; use ssz::{Decode, Encode};
use ssz_derive::Decode; use ssz_derive::Decode;
use ssz_derive::Encode; use ssz_derive::Encode;
use ssz_types::typenum::{U4, U5, U6}; use ssz_types::typenum::{U4, U5, U6, U7};
use std::sync::Arc; use std::sync::Arc;
use superstruct::superstruct; use superstruct::superstruct;
use test_random_derive::TestRandom; use test_random_derive::TestRandom;
@@ -25,24 +25,39 @@ pub const CURRENT_SYNC_COMMITTEE_INDEX: usize = 54;
pub const NEXT_SYNC_COMMITTEE_INDEX: usize = 55; pub const NEXT_SYNC_COMMITTEE_INDEX: usize = 55;
pub const EXECUTION_PAYLOAD_INDEX: usize = 25; pub const EXECUTION_PAYLOAD_INDEX: usize = 25;
pub const FINALIZED_ROOT_INDEX_ELECTRA: usize = 169;
pub const CURRENT_SYNC_COMMITTEE_INDEX_ELECTRA: usize = 86;
pub const NEXT_SYNC_COMMITTEE_INDEX_ELECTRA: usize = 87;
pub type FinalizedRootProofLen = U6; pub type FinalizedRootProofLen = U6;
pub type CurrentSyncCommitteeProofLen = U5; pub type CurrentSyncCommitteeProofLen = U5;
pub type ExecutionPayloadProofLen = U4; pub type ExecutionPayloadProofLen = U4;
pub type NextSyncCommitteeProofLen = U5; pub type NextSyncCommitteeProofLen = U5;
pub type FinalizedRootProofLenElectra = U7;
pub type CurrentSyncCommitteeProofLenElectra = U6;
pub type NextSyncCommitteeProofLenElectra = U6;
pub const FINALIZED_ROOT_PROOF_LEN: usize = 6; pub const FINALIZED_ROOT_PROOF_LEN: usize = 6;
pub const CURRENT_SYNC_COMMITTEE_PROOF_LEN: usize = 5; pub const CURRENT_SYNC_COMMITTEE_PROOF_LEN: usize = 5;
pub const NEXT_SYNC_COMMITTEE_PROOF_LEN: usize = 5; pub const NEXT_SYNC_COMMITTEE_PROOF_LEN: usize = 5;
pub const EXECUTION_PAYLOAD_PROOF_LEN: usize = 4; pub const EXECUTION_PAYLOAD_PROOF_LEN: usize = 4;
pub const FINALIZED_ROOT_PROOF_LEN_ELECTRA: usize = 7;
pub const NEXT_SYNC_COMMITTEE_PROOF_LEN_ELECTRA: usize = 6;
pub const CURRENT_SYNC_COMMITTEE_PROOF_LEN_ELECTRA: usize = 6;
pub type MerkleProof = Vec<Hash256>;
// Max light client updates by range request limits // Max light client updates by range request limits
// spec: https://github.com/ethereum/consensus-specs/blob/dev/specs/altair/light-client/p2p-interface.md#configuration // spec: https://github.com/ethereum/consensus-specs/blob/dev/specs/altair/light-client/p2p-interface.md#configuration
pub const MAX_REQUEST_LIGHT_CLIENT_UPDATES: u64 = 128; pub const MAX_REQUEST_LIGHT_CLIENT_UPDATES: u64 = 128;
type FinalityBranch = FixedVector<Hash256, FinalizedRootProofLen>; type FinalityBranch = FixedVector<Hash256, FinalizedRootProofLen>;
type FinalityBranchElectra = FixedVector<Hash256, FinalizedRootProofLenElectra>;
type NextSyncCommitteeBranch = FixedVector<Hash256, NextSyncCommitteeProofLen>; type NextSyncCommitteeBranch = FixedVector<Hash256, NextSyncCommitteeProofLen>;
type NextSyncCommitteeBranchElectra = FixedVector<Hash256, NextSyncCommitteeProofLenElectra>;
#[derive(Debug, PartialEq, Clone)] #[derive(Debug, PartialEq, Clone)]
pub enum Error { pub enum Error {
SszTypesError(ssz_types::Error), SszTypesError(ssz_types::Error),
@@ -124,8 +139,17 @@ pub struct LightClientUpdate<E: EthSpec> {
pub attested_header: LightClientHeaderElectra<E>, pub attested_header: LightClientHeaderElectra<E>,
/// The `SyncCommittee` used in the next period. /// The `SyncCommittee` used in the next period.
pub next_sync_committee: Arc<SyncCommittee<E>>, pub next_sync_committee: Arc<SyncCommittee<E>>,
/// Merkle proof for next sync committee // Merkle proof for next sync committee
#[superstruct(
only(Altair, Capella, Deneb),
partial_getter(rename = "next_sync_committee_branch_altair")
)]
pub next_sync_committee_branch: NextSyncCommitteeBranch, pub next_sync_committee_branch: NextSyncCommitteeBranch,
#[superstruct(
only(Electra),
partial_getter(rename = "next_sync_committee_branch_electra")
)]
pub next_sync_committee_branch: NextSyncCommitteeBranchElectra,
/// The last `BeaconBlockHeader` from the last attested finalized block (end of epoch). /// The last `BeaconBlockHeader` from the last attested finalized block (end of epoch).
#[superstruct(only(Altair), partial_getter(rename = "finalized_header_altair"))] #[superstruct(only(Altair), partial_getter(rename = "finalized_header_altair"))]
pub finalized_header: LightClientHeaderAltair<E>, pub finalized_header: LightClientHeaderAltair<E>,
@@ -136,7 +160,13 @@ pub struct LightClientUpdate<E: EthSpec> {
#[superstruct(only(Electra), partial_getter(rename = "finalized_header_electra"))] #[superstruct(only(Electra), partial_getter(rename = "finalized_header_electra"))]
pub finalized_header: LightClientHeaderElectra<E>, pub finalized_header: LightClientHeaderElectra<E>,
/// Merkle proof attesting finalized header. /// Merkle proof attesting finalized header.
#[superstruct(
only(Altair, Capella, Deneb),
partial_getter(rename = "finality_branch_altair")
)]
pub finality_branch: FinalityBranch, pub finality_branch: FinalityBranch,
#[superstruct(only(Electra), partial_getter(rename = "finality_branch_electra"))]
pub finality_branch: FinalityBranchElectra,
/// current sync aggreggate /// current sync aggreggate
pub sync_aggregate: SyncAggregate<E>, pub sync_aggregate: SyncAggregate<E>,
/// Slot of the sync aggregated signature /// Slot of the sync aggregated signature
@@ -165,8 +195,8 @@ impl<E: EthSpec> LightClientUpdate<E> {
sync_aggregate: &SyncAggregate<E>, sync_aggregate: &SyncAggregate<E>,
block_slot: Slot, block_slot: Slot,
next_sync_committee: Arc<SyncCommittee<E>>, next_sync_committee: Arc<SyncCommittee<E>>,
next_sync_committee_branch: FixedVector<Hash256, NextSyncCommitteeProofLen>, next_sync_committee_branch: Vec<Hash256>,
finality_branch: FixedVector<Hash256, FinalizedRootProofLen>, finality_branch: Vec<Hash256>,
attested_block: &SignedBlindedBeaconBlock<E>, attested_block: &SignedBlindedBeaconBlock<E>,
finalized_block: Option<&SignedBlindedBeaconBlock<E>>, finalized_block: Option<&SignedBlindedBeaconBlock<E>>,
chain_spec: &ChainSpec, chain_spec: &ChainSpec,
@@ -189,9 +219,9 @@ impl<E: EthSpec> LightClientUpdate<E> {
Self::Altair(LightClientUpdateAltair { Self::Altair(LightClientUpdateAltair {
attested_header, attested_header,
next_sync_committee, next_sync_committee,
next_sync_committee_branch, next_sync_committee_branch: next_sync_committee_branch.into(),
finalized_header, finalized_header,
finality_branch, finality_branch: finality_branch.into(),
sync_aggregate: sync_aggregate.clone(), sync_aggregate: sync_aggregate.clone(),
signature_slot: block_slot, signature_slot: block_slot,
}) })
@@ -209,9 +239,9 @@ impl<E: EthSpec> LightClientUpdate<E> {
Self::Capella(LightClientUpdateCapella { Self::Capella(LightClientUpdateCapella {
attested_header, attested_header,
next_sync_committee, next_sync_committee,
next_sync_committee_branch, next_sync_committee_branch: next_sync_committee_branch.into(),
finalized_header, finalized_header,
finality_branch, finality_branch: finality_branch.into(),
sync_aggregate: sync_aggregate.clone(), sync_aggregate: sync_aggregate.clone(),
signature_slot: block_slot, signature_slot: block_slot,
}) })
@@ -229,9 +259,9 @@ impl<E: EthSpec> LightClientUpdate<E> {
Self::Deneb(LightClientUpdateDeneb { Self::Deneb(LightClientUpdateDeneb {
attested_header, attested_header,
next_sync_committee, next_sync_committee,
next_sync_committee_branch, next_sync_committee_branch: next_sync_committee_branch.into(),
finalized_header, finalized_header,
finality_branch, finality_branch: finality_branch.into(),
sync_aggregate: sync_aggregate.clone(), sync_aggregate: sync_aggregate.clone(),
signature_slot: block_slot, signature_slot: block_slot,
}) })
@@ -249,9 +279,9 @@ impl<E: EthSpec> LightClientUpdate<E> {
Self::Electra(LightClientUpdateElectra { Self::Electra(LightClientUpdateElectra {
attested_header, attested_header,
next_sync_committee, next_sync_committee,
next_sync_committee_branch, next_sync_committee_branch: next_sync_committee_branch.into(),
finalized_header, finalized_header,
finality_branch, finality_branch: finality_branch.into(),
sync_aggregate: sync_aggregate.clone(), sync_aggregate: sync_aggregate.clone(),
signature_slot: block_slot, signature_slot: block_slot,
}) })
@@ -391,22 +421,18 @@ impl<E: EthSpec> LightClientUpdate<E> {
return Ok(new.signature_slot() < self.signature_slot()); return Ok(new.signature_slot() < self.signature_slot());
} }
fn is_next_sync_committee_branch_empty(&self) -> bool { fn is_next_sync_committee_branch_empty<'a>(&'a self) -> bool {
for index in self.next_sync_committee_branch().iter() { map_light_client_update_ref!(&'a _, self.to_ref(), |update, cons| {
if *index != Hash256::default() { cons(update);
return false; is_empty_branch(update.next_sync_committee_branch.as_ref())
} })
}
true
} }
pub fn is_finality_branch_empty(&self) -> bool { pub fn is_finality_branch_empty<'a>(&'a self) -> bool {
for index in self.finality_branch().iter() { map_light_client_update_ref!(&'a _, self.to_ref(), |update, cons| {
if *index != Hash256::default() { cons(update);
return false; is_empty_branch(update.finality_branch.as_ref())
} })
}
true
} }
// A `LightClientUpdate` has two `LightClientHeader`s // A `LightClientUpdate` has two `LightClientHeader`s
@@ -436,6 +462,15 @@ impl<E: EthSpec> LightClientUpdate<E> {
} }
} }
fn is_empty_branch(branch: &[Hash256]) -> bool {
for index in branch.iter() {
if *index != Hash256::default() {
return false;
}
}
true
}
fn compute_sync_committee_period_at_slot<E: EthSpec>( fn compute_sync_committee_period_at_slot<E: EthSpec>(
slot: Slot, slot: Slot,
chain_spec: &ChainSpec, chain_spec: &ChainSpec,
@@ -447,16 +482,53 @@ fn compute_sync_committee_period_at_slot<E: EthSpec>(
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use crate::MainnetEthSpec;
use ssz_types::typenum::Unsigned; use ssz_types::typenum::Unsigned;
ssz_tests!(LightClientUpdateDeneb<MainnetEthSpec>); // `ssz_tests!` can only be defined once per namespace
#[cfg(test)]
mod altair {
use super::*;
use crate::MainnetEthSpec;
ssz_tests!(LightClientUpdateAltair<MainnetEthSpec>);
}
#[cfg(test)]
mod capella {
use super::*;
use crate::MainnetEthSpec;
ssz_tests!(LightClientUpdateCapella<MainnetEthSpec>);
}
#[cfg(test)]
mod deneb {
use super::*;
use crate::MainnetEthSpec;
ssz_tests!(LightClientUpdateDeneb<MainnetEthSpec>);
}
#[cfg(test)]
mod electra {
use super::*;
use crate::MainnetEthSpec;
ssz_tests!(LightClientUpdateElectra<MainnetEthSpec>);
}
#[test] #[test]
fn finalized_root_params() { fn finalized_root_params() {
assert!(2usize.pow(FINALIZED_ROOT_PROOF_LEN as u32) <= FINALIZED_ROOT_INDEX); assert!(2usize.pow(FINALIZED_ROOT_PROOF_LEN as u32) <= FINALIZED_ROOT_INDEX);
assert!(2usize.pow(FINALIZED_ROOT_PROOF_LEN as u32 + 1) > FINALIZED_ROOT_INDEX); assert!(2usize.pow(FINALIZED_ROOT_PROOF_LEN as u32 + 1) > FINALIZED_ROOT_INDEX);
assert_eq!(FinalizedRootProofLen::to_usize(), FINALIZED_ROOT_PROOF_LEN); assert_eq!(FinalizedRootProofLen::to_usize(), FINALIZED_ROOT_PROOF_LEN);
assert!(
2usize.pow(FINALIZED_ROOT_PROOF_LEN_ELECTRA as u32) <= FINALIZED_ROOT_INDEX_ELECTRA
);
assert!(
2usize.pow(FINALIZED_ROOT_PROOF_LEN_ELECTRA as u32 + 1) > FINALIZED_ROOT_INDEX_ELECTRA
);
assert_eq!(
FinalizedRootProofLenElectra::to_usize(),
FINALIZED_ROOT_PROOF_LEN_ELECTRA
);
} }
#[test] #[test]
@@ -471,6 +543,19 @@ mod tests {
CurrentSyncCommitteeProofLen::to_usize(), CurrentSyncCommitteeProofLen::to_usize(),
CURRENT_SYNC_COMMITTEE_PROOF_LEN CURRENT_SYNC_COMMITTEE_PROOF_LEN
); );
assert!(
2usize.pow(CURRENT_SYNC_COMMITTEE_PROOF_LEN_ELECTRA as u32)
<= CURRENT_SYNC_COMMITTEE_INDEX_ELECTRA
);
assert!(
2usize.pow(CURRENT_SYNC_COMMITTEE_PROOF_LEN_ELECTRA as u32 + 1)
> CURRENT_SYNC_COMMITTEE_INDEX_ELECTRA
);
assert_eq!(
CurrentSyncCommitteeProofLenElectra::to_usize(),
CURRENT_SYNC_COMMITTEE_PROOF_LEN_ELECTRA
);
} }
#[test] #[test]
@@ -481,5 +566,18 @@ mod tests {
NextSyncCommitteeProofLen::to_usize(), NextSyncCommitteeProofLen::to_usize(),
NEXT_SYNC_COMMITTEE_PROOF_LEN NEXT_SYNC_COMMITTEE_PROOF_LEN
); );
assert!(
2usize.pow(NEXT_SYNC_COMMITTEE_PROOF_LEN_ELECTRA as u32)
<= NEXT_SYNC_COMMITTEE_INDEX_ELECTRA
);
assert!(
2usize.pow(NEXT_SYNC_COMMITTEE_PROOF_LEN_ELECTRA as u32 + 1)
> NEXT_SYNC_COMMITTEE_INDEX_ELECTRA
);
assert_eq!(
NextSyncCommitteeProofLenElectra::to_usize(),
NEXT_SYNC_COMMITTEE_PROOF_LEN_ELECTRA
);
} }
} }

View File

@@ -48,11 +48,6 @@ excluded_paths = [
"tests/.*/eip6110", "tests/.*/eip6110",
"tests/.*/whisk", "tests/.*/whisk",
"tests/.*/eip7594", "tests/.*/eip7594",
# TODO(electra) re-enable once https://github.com/sigp/lighthouse/issues/6002 is resolved
"tests/.*/electra/ssz_static/LightClientUpdate",
"tests/.*/electra/ssz_static/LightClientFinalityUpdate",
"tests/.*/electra/ssz_static/LightClientBootstrap",
"tests/.*/electra/merkle_proof",
] ]

View File

@@ -3,8 +3,8 @@ use crate::decode::{ssz_decode_file, ssz_decode_state, yaml_decode_file};
use serde::Deserialize; use serde::Deserialize;
use tree_hash::Hash256; use tree_hash::Hash256;
use types::{ use types::{
BeaconBlockBody, BeaconBlockBodyDeneb, BeaconBlockBodyElectra, BeaconState, FixedVector, light_client_update, BeaconBlockBody, BeaconBlockBodyCapella, BeaconBlockBodyDeneb,
FullPayload, Unsigned, BeaconBlockBodyElectra, BeaconState, FixedVector, FullPayload, Unsigned,
}; };
#[derive(Debug, Clone, Deserialize)] #[derive(Debug, Clone, Deserialize)]
@@ -22,13 +22,13 @@ pub struct MerkleProof {
#[derive(Debug, Clone, Deserialize)] #[derive(Debug, Clone, Deserialize)]
#[serde(bound = "E: EthSpec")] #[serde(bound = "E: EthSpec")]
pub struct MerkleProofValidity<E: EthSpec> { pub struct BeaconStateMerkleProofValidity<E: EthSpec> {
pub metadata: Option<Metadata>, pub metadata: Option<Metadata>,
pub state: BeaconState<E>, pub state: BeaconState<E>,
pub merkle_proof: MerkleProof, pub merkle_proof: MerkleProof,
} }
impl<E: EthSpec> LoadCase for MerkleProofValidity<E> { impl<E: EthSpec> LoadCase for BeaconStateMerkleProofValidity<E> {
fn load_from_dir(path: &Path, fork_name: ForkName) -> Result<Self, Error> { fn load_from_dir(path: &Path, fork_name: ForkName) -> Result<Self, Error> {
let spec = &testing_spec::<E>(fork_name); let spec = &testing_spec::<E>(fork_name);
let state = ssz_decode_state(&path.join("object.ssz_snappy"), spec)?; let state = ssz_decode_state(&path.join("object.ssz_snappy"), spec)?;
@@ -49,11 +49,30 @@ impl<E: EthSpec> LoadCase for MerkleProofValidity<E> {
} }
} }
impl<E: EthSpec> Case for MerkleProofValidity<E> { impl<E: EthSpec> Case for BeaconStateMerkleProofValidity<E> {
fn result(&self, _case_index: usize, _fork_name: ForkName) -> Result<(), Error> { fn result(&self, _case_index: usize, _fork_name: ForkName) -> Result<(), Error> {
let mut state = self.state.clone(); let mut state = self.state.clone();
state.update_tree_hash_cache().unwrap(); state.update_tree_hash_cache().unwrap();
let Ok(proof) = state.compute_merkle_proof(self.merkle_proof.leaf_index) else {
let proof = match self.merkle_proof.leaf_index {
light_client_update::CURRENT_SYNC_COMMITTEE_INDEX_ELECTRA
| light_client_update::CURRENT_SYNC_COMMITTEE_INDEX => {
state.compute_current_sync_committee_proof()
}
light_client_update::NEXT_SYNC_COMMITTEE_INDEX_ELECTRA
| light_client_update::NEXT_SYNC_COMMITTEE_INDEX => {
state.compute_next_sync_committee_proof()
}
light_client_update::FINALIZED_ROOT_INDEX_ELECTRA
| light_client_update::FINALIZED_ROOT_INDEX => state.compute_finalized_root_proof(),
_ => {
return Err(Error::FailedToParseTest(
"Could not retrieve merkle proof, invalid index".to_string(),
));
}
};
let Ok(proof) = proof else {
return Err(Error::FailedToParseTest( return Err(Error::FailedToParseTest(
"Could not retrieve merkle proof".to_string(), "Could not retrieve merkle proof".to_string(),
)); ));
@@ -198,3 +217,81 @@ impl<E: EthSpec> Case for KzgInclusionMerkleProofValidity<E> {
} }
} }
} }
#[derive(Debug, Clone, Deserialize)]
#[serde(bound = "E: EthSpec")]
pub struct BeaconBlockBodyMerkleProofValidity<E: EthSpec> {
pub metadata: Option<Metadata>,
pub block_body: BeaconBlockBody<E, FullPayload<E>>,
pub merkle_proof: MerkleProof,
}
impl<E: EthSpec> LoadCase for BeaconBlockBodyMerkleProofValidity<E> {
fn load_from_dir(path: &Path, fork_name: ForkName) -> Result<Self, Error> {
let block_body: BeaconBlockBody<E, FullPayload<E>> = match fork_name {
ForkName::Base | ForkName::Altair | ForkName::Bellatrix => {
return Err(Error::InternalError(format!(
"Beacon block body merkle proof validity test skipped for {:?}",
fork_name
)))
}
ForkName::Capella => {
ssz_decode_file::<BeaconBlockBodyCapella<E>>(&path.join("object.ssz_snappy"))?
.into()
}
ForkName::Deneb => {
ssz_decode_file::<BeaconBlockBodyDeneb<E>>(&path.join("object.ssz_snappy"))?.into()
}
ForkName::Electra => {
ssz_decode_file::<BeaconBlockBodyElectra<E>>(&path.join("object.ssz_snappy"))?
.into()
}
};
let merkle_proof = yaml_decode_file(&path.join("proof.yaml"))?;
// Metadata does not exist in these tests but it is left like this just in case.
let meta_path = path.join("meta.yaml");
let metadata = if meta_path.exists() {
Some(yaml_decode_file(&meta_path)?)
} else {
None
};
Ok(Self {
metadata,
block_body,
merkle_proof,
})
}
}
impl<E: EthSpec> Case for BeaconBlockBodyMerkleProofValidity<E> {
fn result(&self, _case_index: usize, _fork_name: ForkName) -> Result<(), Error> {
let binding = self.block_body.clone();
let block_body = binding.to_ref();
let Ok(proof) = block_body.block_body_merkle_proof(self.merkle_proof.leaf_index) else {
return Err(Error::FailedToParseTest(
"Could not retrieve merkle proof".to_string(),
));
};
let proof_len = proof.len();
let branch_len = self.merkle_proof.branch.len();
if proof_len != branch_len {
return Err(Error::NotEqual(format!(
"Branches not equal in length computed: {}, expected {}",
proof_len, branch_len
)));
}
for (i, proof_leaf) in proof.iter().enumerate().take(proof_len) {
let expected_leaf = self.merkle_proof.branch[i];
if *proof_leaf != expected_leaf {
return Err(Error::NotEqual(format!(
"Leaves not equal in merke proof computed: {}, expected: {}",
hex::encode(proof_leaf),
hex::encode(expected_leaf)
)));
}
}
Ok(())
}
}

View File

@@ -921,10 +921,10 @@ impl<E: EthSpec> Handler for KZGRecoverCellsAndKZGProofHandler<E> {
#[derive(Derivative)] #[derive(Derivative)]
#[derivative(Default(bound = ""))] #[derivative(Default(bound = ""))]
pub struct MerkleProofValidityHandler<E>(PhantomData<E>); pub struct BeaconStateMerkleProofValidityHandler<E>(PhantomData<E>);
impl<E: EthSpec + TypeName> Handler for MerkleProofValidityHandler<E> { impl<E: EthSpec + TypeName> Handler for BeaconStateMerkleProofValidityHandler<E> {
type Case = cases::MerkleProofValidity<E>; type Case = cases::BeaconStateMerkleProofValidity<E>;
fn config_name() -> &'static str { fn config_name() -> &'static str {
E::name() E::name()
@@ -935,15 +935,11 @@ impl<E: EthSpec + TypeName> Handler for MerkleProofValidityHandler<E> {
} }
fn handler_name(&self) -> String { fn handler_name(&self) -> String {
"single_merkle_proof".into() "single_merkle_proof/BeaconState".into()
} }
fn is_enabled_for_fork(&self, _fork_name: ForkName) -> bool { fn is_enabled_for_fork(&self, fork_name: ForkName) -> bool {
// Test is skipped due to some changes in the Capella light client fork_name.altair_enabled()
// spec.
//
// https://github.com/sigp/lighthouse/issues/4022
false
} }
} }
@@ -967,8 +963,32 @@ impl<E: EthSpec + TypeName> Handler for KzgInclusionMerkleProofValidityHandler<E
} }
fn is_enabled_for_fork(&self, fork_name: ForkName) -> bool { fn is_enabled_for_fork(&self, fork_name: ForkName) -> bool {
// TODO(electra) re-enable for electra once merkle proof issues for electra are resolved // Enabled in Deneb
fork_name.deneb_enabled() && !fork_name.electra_enabled() fork_name.deneb_enabled()
}
}
#[derive(Derivative)]
#[derivative(Default(bound = ""))]
pub struct BeaconBlockBodyMerkleProofValidityHandler<E>(PhantomData<E>);
impl<E: EthSpec + TypeName> Handler for BeaconBlockBodyMerkleProofValidityHandler<E> {
type Case = cases::BeaconBlockBodyMerkleProofValidity<E>;
fn config_name() -> &'static str {
E::name()
}
fn runner_name() -> &'static str {
"light_client"
}
fn handler_name(&self) -> String {
"single_merkle_proof/BeaconBlockBody".into()
}
fn is_enabled_for_fork(&self, fork_name: ForkName) -> bool {
fork_name.capella_enabled()
} }
} }
@@ -993,8 +1013,7 @@ impl<E: EthSpec + TypeName> Handler for LightClientUpdateHandler<E> {
fn is_enabled_for_fork(&self, fork_name: ForkName) -> bool { fn is_enabled_for_fork(&self, fork_name: ForkName) -> bool {
// Enabled in Altair // Enabled in Altair
// TODO(electra) re-enable once https://github.com/sigp/lighthouse/issues/6002 is resolved fork_name.altair_enabled()
fork_name.altair_enabled() && fork_name != ForkName::Electra
} }
} }

View File

@@ -396,11 +396,10 @@ mod ssz_static {
.run(); .run();
SszStaticHandler::<LightClientBootstrapDeneb<MainnetEthSpec>, MainnetEthSpec>::deneb_only() SszStaticHandler::<LightClientBootstrapDeneb<MainnetEthSpec>, MainnetEthSpec>::deneb_only()
.run(); .run();
// TODO(electra) re-enable once https://github.com/sigp/lighthouse/issues/6002 is resolved SszStaticHandler::<LightClientBootstrapElectra<MinimalEthSpec>, MinimalEthSpec>::electra_only()
// SszStaticHandler::<LightClientBootstrapElectra<MinimalEthSpec>, MinimalEthSpec>::electra_only() .run();
// .run(); SszStaticHandler::<LightClientBootstrapElectra<MainnetEthSpec>, MainnetEthSpec>::electra_only()
// SszStaticHandler::<LightClientBootstrapElectra<MainnetEthSpec>, MainnetEthSpec>::electra_only() .run();
// .run();
} }
// LightClientHeader has no internal indicator of which fork it is for, so we test it separately. // LightClientHeader has no internal indicator of which fork it is for, so we test it separately.
@@ -476,13 +475,12 @@ mod ssz_static {
SszStaticHandler::<LightClientFinalityUpdateDeneb<MainnetEthSpec>, MainnetEthSpec>::deneb_only( SszStaticHandler::<LightClientFinalityUpdateDeneb<MainnetEthSpec>, MainnetEthSpec>::deneb_only(
) )
.run(); .run();
// TODO(electra) re-enable once https://github.com/sigp/lighthouse/issues/6002 is resolved SszStaticHandler::<LightClientFinalityUpdateElectra<MinimalEthSpec>, MinimalEthSpec>::electra_only(
// SszStaticHandler::<LightClientFinalityUpdateElectra<MinimalEthSpec>, MinimalEthSpec>::electra_only( )
// ) .run();
// .run(); SszStaticHandler::<LightClientFinalityUpdateElectra<MainnetEthSpec>, MainnetEthSpec>::electra_only(
// SszStaticHandler::<LightClientFinalityUpdateElectra<MainnetEthSpec>, MainnetEthSpec>::electra_only( )
// ) .run();
// .run();
} }
// LightClientUpdate has no internal indicator of which fork it is for, so we test it separately. // LightClientUpdate has no internal indicator of which fork it is for, so we test it separately.
@@ -506,13 +504,12 @@ mod ssz_static {
.run(); .run();
SszStaticHandler::<LightClientUpdateDeneb<MainnetEthSpec>, MainnetEthSpec>::deneb_only() SszStaticHandler::<LightClientUpdateDeneb<MainnetEthSpec>, MainnetEthSpec>::deneb_only()
.run(); .run();
// TODO(electra) re-enable once https://github.com/sigp/lighthouse/issues/6002 is resolved SszStaticHandler::<LightClientUpdateElectra<MinimalEthSpec>, MinimalEthSpec>::electra_only(
// SszStaticHandler::<LightClientUpdateElectra<MinimalEthSpec>, MinimalEthSpec>::electra_only( )
// ) .run();
// .run(); SszStaticHandler::<LightClientUpdateElectra<MainnetEthSpec>, MainnetEthSpec>::electra_only(
// SszStaticHandler::<LightClientUpdateElectra<MainnetEthSpec>, MainnetEthSpec>::electra_only( )
// ) .run();
// .run();
} }
#[test] #[test]
@@ -922,8 +919,13 @@ fn kzg_recover_cells_and_proofs() {
} }
#[test] #[test]
fn merkle_proof_validity() { fn beacon_state_merkle_proof_validity() {
MerkleProofValidityHandler::<MainnetEthSpec>::default().run(); BeaconStateMerkleProofValidityHandler::<MainnetEthSpec>::default().run();
}
#[test]
fn beacon_block_body_merkle_proof_validity() {
BeaconBlockBodyMerkleProofValidityHandler::<MainnetEthSpec>::default().run();
} }
#[test] #[test]