mirror of
https://github.com/sigp/lighthouse.git
synced 2026-04-16 20:39:10 +00:00
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:
@@ -2506,33 +2506,64 @@ impl<E: EthSpec> BeaconState<E> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn compute_merkle_proof(&self, generalized_index: usize) -> Result<Vec<Hash256>, Error> {
|
||||
// 1. Convert generalized index to field index.
|
||||
let field_index = match generalized_index {
|
||||
pub fn compute_current_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::CURRENT_SYNC_COMMITTEE_INDEX_ELECTRA
|
||||
} else {
|
||||
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![];
|
||||
#[allow(clippy::arithmetic_side_effects)]
|
||||
match self {
|
||||
@@ -2568,18 +2599,7 @@ impl<E: EthSpec> BeaconState<E> {
|
||||
}
|
||||
};
|
||||
|
||||
// 3. Make deposit tree.
|
||||
// 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)
|
||||
leaves
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -200,7 +200,7 @@ pub use crate::light_client_optimistic_update::{
|
||||
};
|
||||
pub use crate::light_client_update::{
|
||||
Error as LightClientUpdateError, LightClientUpdate, LightClientUpdateAltair,
|
||||
LightClientUpdateCapella, LightClientUpdateDeneb, LightClientUpdateElectra,
|
||||
LightClientUpdateCapella, LightClientUpdateDeneb, LightClientUpdateElectra, MerkleProof,
|
||||
};
|
||||
pub use crate::participation_flags::ParticipationFlags;
|
||||
pub use crate::payload::{
|
||||
|
||||
@@ -57,7 +57,16 @@ pub struct LightClientBootstrap<E: EthSpec> {
|
||||
/// The `SyncCommittee` used in the requested period.
|
||||
pub current_sync_committee: Arc<SyncCommittee<E>>,
|
||||
/// 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>,
|
||||
#[superstruct(
|
||||
only(Electra),
|
||||
partial_getter(rename = "current_sync_committee_branch_electra")
|
||||
)]
|
||||
pub current_sync_committee_branch: FixedVector<Hash256, CurrentSyncCommitteeProofLenElectra>,
|
||||
}
|
||||
|
||||
impl<E: EthSpec> LightClientBootstrap<E> {
|
||||
@@ -115,7 +124,7 @@ impl<E: EthSpec> LightClientBootstrap<E> {
|
||||
pub fn new(
|
||||
block: &SignedBlindedBeaconBlock<E>,
|
||||
current_sync_committee: Arc<SyncCommittee<E>>,
|
||||
current_sync_committee_branch: FixedVector<Hash256, CurrentSyncCommitteeProofLen>,
|
||||
current_sync_committee_branch: Vec<Hash256>,
|
||||
chain_spec: &ChainSpec,
|
||||
) -> Result<Self, Error> {
|
||||
let light_client_bootstrap = match block
|
||||
@@ -126,22 +135,22 @@ impl<E: EthSpec> LightClientBootstrap<E> {
|
||||
ForkName::Altair | ForkName::Bellatrix => Self::Altair(LightClientBootstrapAltair {
|
||||
header: LightClientHeaderAltair::block_to_light_client_header(block)?,
|
||||
current_sync_committee,
|
||||
current_sync_committee_branch,
|
||||
current_sync_committee_branch: current_sync_committee_branch.into(),
|
||||
}),
|
||||
ForkName::Capella => Self::Capella(LightClientBootstrapCapella {
|
||||
header: LightClientHeaderCapella::block_to_light_client_header(block)?,
|
||||
current_sync_committee,
|
||||
current_sync_committee_branch,
|
||||
current_sync_committee_branch: current_sync_committee_branch.into(),
|
||||
}),
|
||||
ForkName::Deneb => Self::Deneb(LightClientBootstrapDeneb {
|
||||
header: LightClientHeaderDeneb::block_to_light_client_header(block)?,
|
||||
current_sync_committee,
|
||||
current_sync_committee_branch,
|
||||
current_sync_committee_branch: current_sync_committee_branch.into(),
|
||||
}),
|
||||
ForkName::Electra => Self::Electra(LightClientBootstrapElectra {
|
||||
header: LightClientHeaderElectra::block_to_light_client_header(block)?,
|
||||
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> {
|
||||
let mut header = beacon_state.latest_block_header().clone();
|
||||
header.state_root = beacon_state.update_tree_hash_cache()?;
|
||||
let current_sync_committee_branch =
|
||||
FixedVector::new(beacon_state.compute_merkle_proof(CURRENT_SYNC_COMMITTEE_INDEX)?)?;
|
||||
|
||||
let current_sync_committee_branch = beacon_state.compute_current_sync_committee_proof()?;
|
||||
let current_sync_committee = beacon_state.current_sync_committee()?.clone();
|
||||
|
||||
let light_client_bootstrap = match block
|
||||
@@ -168,22 +175,22 @@ impl<E: EthSpec> LightClientBootstrap<E> {
|
||||
ForkName::Altair | ForkName::Bellatrix => Self::Altair(LightClientBootstrapAltair {
|
||||
header: LightClientHeaderAltair::block_to_light_client_header(block)?,
|
||||
current_sync_committee,
|
||||
current_sync_committee_branch,
|
||||
current_sync_committee_branch: current_sync_committee_branch.into(),
|
||||
}),
|
||||
ForkName::Capella => Self::Capella(LightClientBootstrapCapella {
|
||||
header: LightClientHeaderCapella::block_to_light_client_header(block)?,
|
||||
current_sync_committee,
|
||||
current_sync_committee_branch,
|
||||
current_sync_committee_branch: current_sync_committee_branch.into(),
|
||||
}),
|
||||
ForkName::Deneb => Self::Deneb(LightClientBootstrapDeneb {
|
||||
header: LightClientHeaderDeneb::block_to_light_client_header(block)?,
|
||||
current_sync_committee,
|
||||
current_sync_committee_branch,
|
||||
current_sync_committee_branch: current_sync_committee_branch.into(),
|
||||
}),
|
||||
ForkName::Electra => Self::Electra(LightClientBootstrapElectra {
|
||||
header: LightClientHeaderElectra::block_to_light_client_header(block)?,
|
||||
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)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::MainnetEthSpec;
|
||||
// `ssz_tests!` can only be defined once per namespace
|
||||
#[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>);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -63,8 +63,13 @@ pub struct LightClientFinalityUpdate<E: EthSpec> {
|
||||
#[superstruct(only(Electra), partial_getter(rename = "finalized_header_electra"))]
|
||||
pub finalized_header: LightClientHeaderElectra<E>,
|
||||
/// 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>,
|
||||
#[superstruct(only(Electra), partial_getter(rename = "finality_branch_electra"))]
|
||||
pub finality_branch: FixedVector<Hash256, FinalizedRootProofLenElectra>,
|
||||
/// current sync aggregate
|
||||
pub sync_aggregate: SyncAggregate<E>,
|
||||
/// Slot of the sync aggregated signature
|
||||
@@ -75,7 +80,7 @@ impl<E: EthSpec> LightClientFinalityUpdate<E> {
|
||||
pub fn new(
|
||||
attested_block: &SignedBlindedBeaconBlock<E>,
|
||||
finalized_block: &SignedBlindedBeaconBlock<E>,
|
||||
finality_branch: FixedVector<Hash256, FinalizedRootProofLen>,
|
||||
finality_branch: Vec<Hash256>,
|
||||
sync_aggregate: SyncAggregate<E>,
|
||||
signature_slot: Slot,
|
||||
chain_spec: &ChainSpec,
|
||||
@@ -92,7 +97,7 @@ impl<E: EthSpec> LightClientFinalityUpdate<E> {
|
||||
finalized_header: LightClientHeaderAltair::block_to_light_client_header(
|
||||
finalized_block,
|
||||
)?,
|
||||
finality_branch,
|
||||
finality_branch: finality_branch.into(),
|
||||
sync_aggregate,
|
||||
signature_slot,
|
||||
})
|
||||
@@ -104,7 +109,7 @@ impl<E: EthSpec> LightClientFinalityUpdate<E> {
|
||||
finalized_header: LightClientHeaderCapella::block_to_light_client_header(
|
||||
finalized_block,
|
||||
)?,
|
||||
finality_branch,
|
||||
finality_branch: finality_branch.into(),
|
||||
sync_aggregate,
|
||||
signature_slot,
|
||||
}),
|
||||
@@ -115,7 +120,7 @@ impl<E: EthSpec> LightClientFinalityUpdate<E> {
|
||||
finalized_header: LightClientHeaderDeneb::block_to_light_client_header(
|
||||
finalized_block,
|
||||
)?,
|
||||
finality_branch,
|
||||
finality_branch: finality_branch.into(),
|
||||
sync_aggregate,
|
||||
signature_slot,
|
||||
}),
|
||||
@@ -126,7 +131,7 @@ impl<E: EthSpec> LightClientFinalityUpdate<E> {
|
||||
finalized_header: LightClientHeaderElectra::block_to_light_client_header(
|
||||
finalized_block,
|
||||
)?,
|
||||
finality_branch,
|
||||
finality_branch: finality_branch.into(),
|
||||
sync_aggregate,
|
||||
signature_slot,
|
||||
}),
|
||||
@@ -226,8 +231,28 @@ impl<E: EthSpec> ForkVersionDeserialize for LightClientFinalityUpdate<E> {
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::MainnetEthSpec;
|
||||
// `ssz_tests!` can only be defined once per namespace
|
||||
#[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>);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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>);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -214,8 +214,28 @@ impl<E: EthSpec> ForkVersionDeserialize for LightClientOptimisticUpdate<E> {
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::MainnetEthSpec;
|
||||
// `ssz_tests!` can only be defined once per namespace
|
||||
#[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>);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@ use serde_json::Value;
|
||||
use ssz::{Decode, Encode};
|
||||
use ssz_derive::Decode;
|
||||
use ssz_derive::Encode;
|
||||
use ssz_types::typenum::{U4, U5, U6};
|
||||
use ssz_types::typenum::{U4, U5, U6, U7};
|
||||
use std::sync::Arc;
|
||||
use superstruct::superstruct;
|
||||
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 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 CurrentSyncCommitteeProofLen = U5;
|
||||
pub type ExecutionPayloadProofLen = U4;
|
||||
|
||||
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 CURRENT_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 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
|
||||
// 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;
|
||||
|
||||
type FinalityBranch = FixedVector<Hash256, FinalizedRootProofLen>;
|
||||
type FinalityBranchElectra = FixedVector<Hash256, FinalizedRootProofLenElectra>;
|
||||
type NextSyncCommitteeBranch = FixedVector<Hash256, NextSyncCommitteeProofLen>;
|
||||
|
||||
type NextSyncCommitteeBranchElectra = FixedVector<Hash256, NextSyncCommitteeProofLenElectra>;
|
||||
|
||||
#[derive(Debug, PartialEq, Clone)]
|
||||
pub enum Error {
|
||||
SszTypesError(ssz_types::Error),
|
||||
@@ -124,8 +139,17 @@ pub struct LightClientUpdate<E: EthSpec> {
|
||||
pub attested_header: LightClientHeaderElectra<E>,
|
||||
/// The `SyncCommittee` used in the next period.
|
||||
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,
|
||||
#[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).
|
||||
#[superstruct(only(Altair), partial_getter(rename = "finalized_header_altair"))]
|
||||
pub finalized_header: LightClientHeaderAltair<E>,
|
||||
@@ -136,7 +160,13 @@ pub struct LightClientUpdate<E: EthSpec> {
|
||||
#[superstruct(only(Electra), partial_getter(rename = "finalized_header_electra"))]
|
||||
pub finalized_header: LightClientHeaderElectra<E>,
|
||||
/// Merkle proof attesting finalized header.
|
||||
#[superstruct(
|
||||
only(Altair, Capella, Deneb),
|
||||
partial_getter(rename = "finality_branch_altair")
|
||||
)]
|
||||
pub finality_branch: FinalityBranch,
|
||||
#[superstruct(only(Electra), partial_getter(rename = "finality_branch_electra"))]
|
||||
pub finality_branch: FinalityBranchElectra,
|
||||
/// current sync aggreggate
|
||||
pub sync_aggregate: SyncAggregate<E>,
|
||||
/// Slot of the sync aggregated signature
|
||||
@@ -165,8 +195,8 @@ impl<E: EthSpec> LightClientUpdate<E> {
|
||||
sync_aggregate: &SyncAggregate<E>,
|
||||
block_slot: Slot,
|
||||
next_sync_committee: Arc<SyncCommittee<E>>,
|
||||
next_sync_committee_branch: FixedVector<Hash256, NextSyncCommitteeProofLen>,
|
||||
finality_branch: FixedVector<Hash256, FinalizedRootProofLen>,
|
||||
next_sync_committee_branch: Vec<Hash256>,
|
||||
finality_branch: Vec<Hash256>,
|
||||
attested_block: &SignedBlindedBeaconBlock<E>,
|
||||
finalized_block: Option<&SignedBlindedBeaconBlock<E>>,
|
||||
chain_spec: &ChainSpec,
|
||||
@@ -189,9 +219,9 @@ impl<E: EthSpec> LightClientUpdate<E> {
|
||||
Self::Altair(LightClientUpdateAltair {
|
||||
attested_header,
|
||||
next_sync_committee,
|
||||
next_sync_committee_branch,
|
||||
next_sync_committee_branch: next_sync_committee_branch.into(),
|
||||
finalized_header,
|
||||
finality_branch,
|
||||
finality_branch: finality_branch.into(),
|
||||
sync_aggregate: sync_aggregate.clone(),
|
||||
signature_slot: block_slot,
|
||||
})
|
||||
@@ -209,9 +239,9 @@ impl<E: EthSpec> LightClientUpdate<E> {
|
||||
Self::Capella(LightClientUpdateCapella {
|
||||
attested_header,
|
||||
next_sync_committee,
|
||||
next_sync_committee_branch,
|
||||
next_sync_committee_branch: next_sync_committee_branch.into(),
|
||||
finalized_header,
|
||||
finality_branch,
|
||||
finality_branch: finality_branch.into(),
|
||||
sync_aggregate: sync_aggregate.clone(),
|
||||
signature_slot: block_slot,
|
||||
})
|
||||
@@ -229,9 +259,9 @@ impl<E: EthSpec> LightClientUpdate<E> {
|
||||
Self::Deneb(LightClientUpdateDeneb {
|
||||
attested_header,
|
||||
next_sync_committee,
|
||||
next_sync_committee_branch,
|
||||
next_sync_committee_branch: next_sync_committee_branch.into(),
|
||||
finalized_header,
|
||||
finality_branch,
|
||||
finality_branch: finality_branch.into(),
|
||||
sync_aggregate: sync_aggregate.clone(),
|
||||
signature_slot: block_slot,
|
||||
})
|
||||
@@ -249,9 +279,9 @@ impl<E: EthSpec> LightClientUpdate<E> {
|
||||
Self::Electra(LightClientUpdateElectra {
|
||||
attested_header,
|
||||
next_sync_committee,
|
||||
next_sync_committee_branch,
|
||||
next_sync_committee_branch: next_sync_committee_branch.into(),
|
||||
finalized_header,
|
||||
finality_branch,
|
||||
finality_branch: finality_branch.into(),
|
||||
sync_aggregate: sync_aggregate.clone(),
|
||||
signature_slot: block_slot,
|
||||
})
|
||||
@@ -391,22 +421,18 @@ impl<E: EthSpec> LightClientUpdate<E> {
|
||||
return Ok(new.signature_slot() < self.signature_slot());
|
||||
}
|
||||
|
||||
fn is_next_sync_committee_branch_empty(&self) -> bool {
|
||||
for index in self.next_sync_committee_branch().iter() {
|
||||
if *index != Hash256::default() {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
true
|
||||
fn is_next_sync_committee_branch_empty<'a>(&'a self) -> bool {
|
||||
map_light_client_update_ref!(&'a _, self.to_ref(), |update, cons| {
|
||||
cons(update);
|
||||
is_empty_branch(update.next_sync_committee_branch.as_ref())
|
||||
})
|
||||
}
|
||||
|
||||
pub fn is_finality_branch_empty(&self) -> bool {
|
||||
for index in self.finality_branch().iter() {
|
||||
if *index != Hash256::default() {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
true
|
||||
pub fn is_finality_branch_empty<'a>(&'a self) -> bool {
|
||||
map_light_client_update_ref!(&'a _, self.to_ref(), |update, cons| {
|
||||
cons(update);
|
||||
is_empty_branch(update.finality_branch.as_ref())
|
||||
})
|
||||
}
|
||||
|
||||
// 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>(
|
||||
slot: Slot,
|
||||
chain_spec: &ChainSpec,
|
||||
@@ -447,16 +482,53 @@ fn compute_sync_committee_period_at_slot<E: EthSpec>(
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::MainnetEthSpec;
|
||||
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]
|
||||
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 + 1) > FINALIZED_ROOT_INDEX);
|
||||
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]
|
||||
@@ -471,6 +543,19 @@ mod tests {
|
||||
CurrentSyncCommitteeProofLen::to_usize(),
|
||||
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]
|
||||
@@ -481,5 +566,18 @@ mod tests {
|
||||
NextSyncCommitteeProofLen::to_usize(),
|
||||
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
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user