Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add electra lightclient types #5980

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions consensus/types/src/execution_payload_header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,8 @@ impl<E: EthSpec> ExecutionPayloadHeader<E> {
pub fn ssz_max_var_len_for_fork(fork_name: ForkName) -> usize {
// Matching here in case variable fields are added in future forks.
match fork_name {
ForkName::Base
| ForkName::Altair
| ForkName::Bellatrix
| ForkName::Capella
| ForkName::Deneb
| ForkName::Electra => {
ForkName::Base | ForkName::Altair => 0,
ForkName::Bellatrix | ForkName::Capella | ForkName::Deneb | ForkName::Electra => {
// Max size of variable length `extra_data` field
E::max_extra_data_bytes() * <u8 as Encode>::ssz_fixed_len()
}
Expand Down
8 changes: 5 additions & 3 deletions consensus/types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,22 +181,24 @@ pub use crate::indexed_attestation::{
};
pub use crate::light_client_bootstrap::{
LightClientBootstrap, LightClientBootstrapAltair, LightClientBootstrapCapella,
LightClientBootstrapDeneb,
LightClientBootstrapDeneb, LightClientBootstrapElectra,
};
pub use crate::light_client_finality_update::{
LightClientFinalityUpdate, LightClientFinalityUpdateAltair, LightClientFinalityUpdateCapella,
LightClientFinalityUpdateDeneb,
LightClientFinalityUpdateDeneb, LightClientFinalityUpdateElectra,
};
pub use crate::light_client_header::{
LightClientHeader, LightClientHeaderAltair, LightClientHeaderCapella, LightClientHeaderDeneb,
LightClientHeaderElectra,
};
pub use crate::light_client_optimistic_update::{
LightClientOptimisticUpdate, LightClientOptimisticUpdateAltair,
LightClientOptimisticUpdateCapella, LightClientOptimisticUpdateDeneb,
LightClientOptimisticUpdateElectra,
};
pub use crate::light_client_update::{
Error as LightClientError, LightClientUpdate, LightClientUpdateAltair,
LightClientUpdateCapella, LightClientUpdateDeneb,
LightClientUpdateCapella, LightClientUpdateDeneb, LightClientUpdateElectra,
};
pub use crate::participation_flags::ParticipationFlags;
pub use crate::participation_list::ParticipationList;
Expand Down
36 changes: 21 additions & 15 deletions consensus/types/src/light_client_bootstrap.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use crate::{
light_client_update::*, test_utils::TestRandom, BeaconState, ChainSpec, EthSpec, FixedVector,
ForkName, ForkVersionDeserialize, Hash256, LightClientHeader, LightClientHeaderAltair,
LightClientHeaderCapella, LightClientHeaderDeneb, SignedBeaconBlock, Slot, SyncCommittee,
LightClientHeaderCapella, LightClientHeaderDeneb, LightClientHeaderElectra, SignedBeaconBlock,
Slot, SyncCommittee,
};
use derivative::Derivative;
use serde::{Deserialize, Deserializer, Serialize};
Expand All @@ -16,7 +17,7 @@ use tree_hash_derive::TreeHash;
/// A LightClientBootstrap is the initializer we send over to light_client nodes
/// that are trying to generate their basic storage when booting up.
#[superstruct(
variants(Altair, Capella, Deneb),
variants(Altair, Capella, Deneb, Electra),
variant_attributes(
derive(
Debug,
Expand Down Expand Up @@ -51,6 +52,8 @@ pub struct LightClientBootstrap<E: EthSpec> {
pub header: LightClientHeaderCapella<E>,
#[superstruct(only(Deneb), partial_getter(rename = "header_deneb"))]
pub header: LightClientHeaderDeneb<E>,
#[superstruct(only(Electra), partial_getter(rename = "header_electra"))]
pub header: LightClientHeaderElectra<E>,
/// The `SyncCommittee` used in the requested period.
pub current_sync_committee: Arc<SyncCommittee<E>>,
/// Merkle proof for sync committee
Expand All @@ -66,6 +69,7 @@ impl<E: EthSpec> LightClientBootstrap<E> {
Self::Altair(_) => func(ForkName::Altair),
Self::Capella(_) => func(ForkName::Capella),
Self::Deneb(_) => func(ForkName::Deneb),
Self::Electra(_) => func(ForkName::Electra),
}
}

Expand All @@ -82,9 +86,8 @@ impl<E: EthSpec> LightClientBootstrap<E> {
Self::Altair(LightClientBootstrapAltair::from_ssz_bytes(bytes)?)
}
ForkName::Capella => Self::Capella(LightClientBootstrapCapella::from_ssz_bytes(bytes)?),
ForkName::Deneb | ForkName::Electra => {
Self::Deneb(LightClientBootstrapDeneb::from_ssz_bytes(bytes)?)
}
ForkName::Deneb => Self::Deneb(LightClientBootstrapDeneb::from_ssz_bytes(bytes)?),
ForkName::Electra => Self::Electra(LightClientBootstrapElectra::from_ssz_bytes(bytes)?),
ForkName::Base => {
return Err(ssz::DecodeError::BytesInvalid(format!(
"LightClientBootstrap decoding for {fork_name} not implemented"
Expand All @@ -97,18 +100,16 @@ impl<E: EthSpec> LightClientBootstrap<E> {

#[allow(clippy::arithmetic_side_effects)]
pub fn ssz_max_len_for_fork(fork_name: ForkName) -> usize {
// TODO(electra): review electra changes
match fork_name {
let fixed_len = match fork_name {
ForkName::Base => 0,
ForkName::Altair
| ForkName::Bellatrix
| ForkName::Capella
| ForkName::Deneb
| ForkName::Electra => {
ForkName::Altair | ForkName::Bellatrix => {
<LightClientBootstrapAltair<E> as Encode>::ssz_fixed_len()
+ LightClientHeader::<E>::ssz_max_var_len_for_fork(fork_name)
}
}
ForkName::Capella => <LightClientBootstrapCapella<E> as Encode>::ssz_fixed_len(),
ForkName::Deneb => <LightClientBootstrapDeneb<E> as Encode>::ssz_fixed_len(),
ForkName::Electra => <LightClientBootstrapElectra<E> as Encode>::ssz_fixed_len(),
};
fixed_len + LightClientHeader::<E>::ssz_max_var_len_for_fork(fork_name)
}

pub fn from_beacon_state(
Expand Down Expand Up @@ -138,11 +139,16 @@ impl<E: EthSpec> LightClientBootstrap<E> {
current_sync_committee,
current_sync_committee_branch,
}),
ForkName::Deneb | ForkName::Electra => Self::Deneb(LightClientBootstrapDeneb {
ForkName::Deneb => Self::Deneb(LightClientBootstrapDeneb {
header: LightClientHeaderDeneb::block_to_light_client_header(block)?,
current_sync_committee,
current_sync_committee_branch,
}),
ForkName::Electra => Self::Electra(LightClientBootstrapElectra {
header: LightClientHeaderElectra::block_to_light_client_header(block)?,
current_sync_committee,
current_sync_committee_branch,
}),
};

Ok(light_client_bootstrap)
Expand Down
99 changes: 55 additions & 44 deletions consensus/types/src/light_client_finality_update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ use super::{EthSpec, FixedVector, Hash256, LightClientHeader, Slot, SyncAggregat
use crate::ChainSpec;
use crate::{
light_client_update::*, test_utils::TestRandom, ForkName, ForkVersionDeserialize,
LightClientHeaderAltair, LightClientHeaderCapella, LightClientHeaderDeneb, SignedBeaconBlock,
LightClientHeaderAltair, LightClientHeaderCapella, LightClientHeaderDeneb,
LightClientHeaderElectra, SignedBeaconBlock,
};
use derivative::Derivative;
use serde::{Deserialize, Deserializer, Serialize};
Expand All @@ -15,7 +16,7 @@ use test_random_derive::TestRandom;
use tree_hash_derive::TreeHash;

#[superstruct(
variants(Altair, Capella, Deneb),
variants(Altair, Capella, Deneb, Electra),
variant_attributes(
derive(
Debug,
Expand Down Expand Up @@ -50,13 +51,17 @@ pub struct LightClientFinalityUpdate<E: EthSpec> {
pub attested_header: LightClientHeaderCapella<E>,
#[superstruct(only(Deneb), partial_getter(rename = "attested_header_deneb"))]
pub attested_header: LightClientHeaderDeneb<E>,
#[superstruct(only(Electra), partial_getter(rename = "attested_header_electra"))]
pub attested_header: LightClientHeaderElectra<E>,
/// 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>,
#[superstruct(only(Capella), partial_getter(rename = "finalized_header_capella"))]
pub finalized_header: LightClientHeaderCapella<E>,
#[superstruct(only(Deneb), partial_getter(rename = "finalized_header_deneb"))]
pub finalized_header: LightClientHeaderDeneb<E>,
#[superstruct(only(Electra), partial_getter(rename = "finalized_header_electra"))]
pub finalized_header: LightClientHeaderElectra<E>,
/// Merkle proof attesting finalized header.
#[test_random(default)]
pub finality_branch: FixedVector<Hash256, FinalizedRootProofLen>,
Expand All @@ -80,7 +85,7 @@ impl<E: EthSpec> LightClientFinalityUpdate<E> {
.map_err(|_| Error::InconsistentFork)?
{
ForkName::Altair | ForkName::Bellatrix => {
let finality_update = LightClientFinalityUpdateAltair {
Self::Altair(LightClientFinalityUpdateAltair {
attested_header: LightClientHeaderAltair::block_to_light_client_header(
attested_block,
)?,
Expand All @@ -90,37 +95,42 @@ impl<E: EthSpec> LightClientFinalityUpdate<E> {
finality_branch,
sync_aggregate,
signature_slot,
};
Self::Altair(finality_update)
}
ForkName::Capella => {
let finality_update = LightClientFinalityUpdateCapella {
attested_header: LightClientHeaderCapella::block_to_light_client_header(
attested_block,
)?,
finalized_header: LightClientHeaderCapella::block_to_light_client_header(
finalized_block,
)?,
finality_branch,
sync_aggregate,
signature_slot,
};
Self::Capella(finality_update)
}
ForkName::Deneb | ForkName::Electra => {
let finality_update = LightClientFinalityUpdateDeneb {
attested_header: LightClientHeaderDeneb::block_to_light_client_header(
attested_block,
)?,
finalized_header: LightClientHeaderDeneb::block_to_light_client_header(
finalized_block,
)?,
finality_branch,
sync_aggregate,
signature_slot,
};
Self::Deneb(finality_update)
})
}
ForkName::Capella => Self::Capella(LightClientFinalityUpdateCapella {
attested_header: LightClientHeaderCapella::block_to_light_client_header(
attested_block,
)?,
finalized_header: LightClientHeaderCapella::block_to_light_client_header(
finalized_block,
)?,
finality_branch,
sync_aggregate,
signature_slot,
}),
ForkName::Deneb => Self::Deneb(LightClientFinalityUpdateDeneb {
attested_header: LightClientHeaderDeneb::block_to_light_client_header(
attested_block,
)?,
finalized_header: LightClientHeaderDeneb::block_to_light_client_header(
finalized_block,
)?,
finality_branch,
sync_aggregate,
signature_slot,
}),
ForkName::Electra => Self::Electra(LightClientFinalityUpdateElectra {
attested_header: LightClientHeaderElectra::block_to_light_client_header(
attested_block,
)?,
finalized_header: LightClientHeaderElectra::block_to_light_client_header(
finalized_block,
)?,
finality_branch,
sync_aggregate,
signature_slot,
}),

ForkName::Base => return Err(Error::AltairForkNotActive),
};

Expand All @@ -135,6 +145,7 @@ impl<E: EthSpec> LightClientFinalityUpdate<E> {
Self::Altair(_) => func(ForkName::Altair),
Self::Capella(_) => func(ForkName::Capella),
Self::Deneb(_) => func(ForkName::Deneb),
Self::Electra(_) => func(ForkName::Electra),
}
}

Expand All @@ -153,8 +164,9 @@ impl<E: EthSpec> LightClientFinalityUpdate<E> {
ForkName::Capella => {
Self::Capella(LightClientFinalityUpdateCapella::from_ssz_bytes(bytes)?)
}
ForkName::Deneb | ForkName::Electra => {
Self::Deneb(LightClientFinalityUpdateDeneb::from_ssz_bytes(bytes)?)
ForkName::Deneb => Self::Deneb(LightClientFinalityUpdateDeneb::from_ssz_bytes(bytes)?),
ForkName::Electra => {
Self::Electra(LightClientFinalityUpdateElectra::from_ssz_bytes(bytes)?)
}
ForkName::Base => {
return Err(ssz::DecodeError::BytesInvalid(format!(
Expand All @@ -168,18 +180,17 @@ impl<E: EthSpec> LightClientFinalityUpdate<E> {

#[allow(clippy::arithmetic_side_effects)]
pub fn ssz_max_len_for_fork(fork_name: ForkName) -> usize {
// TODO(electra): review electra changes
match fork_name {
let fixed_size = match fork_name {
ForkName::Base => 0,
ForkName::Altair
| ForkName::Bellatrix
| ForkName::Capella
| ForkName::Deneb
| ForkName::Electra => {
ForkName::Altair | ForkName::Bellatrix => {
<LightClientFinalityUpdateAltair<E> as Encode>::ssz_fixed_len()
+ 2 * LightClientHeader::<E>::ssz_max_var_len_for_fork(fork_name)
}
}
ForkName::Capella => <LightClientFinalityUpdateCapella<E> as Encode>::ssz_fixed_len(),
ForkName::Deneb => <LightClientFinalityUpdateDeneb<E> as Encode>::ssz_fixed_len(),
ForkName::Electra => <LightClientFinalityUpdateElectra<E> as Encode>::ssz_fixed_len(),
};
// `2 *` because there are two headers in the update
fixed_size + 2 * LightClientHeader::<E>::ssz_max_var_len_for_fork(fork_name)
}
}

Expand Down
Loading
Loading