diff --git a/beacon_node/beacon_chain/src/beacon_chain.rs b/beacon_node/beacon_chain/src/beacon_chain.rs index 534cffae132..49a970c1780 100644 --- a/beacon_node/beacon_chain/src/beacon_chain.rs +++ b/beacon_node/beacon_chain/src/beacon_chain.rs @@ -1347,11 +1347,12 @@ impl BeaconChain { (parent_root, slot, sync_aggregate): LightClientProducerEvent, ) -> Result<(), Error> { self.light_client_server_cache.recompute_and_cache_updates( - &self.log, self.store.clone(), &parent_root, slot, &sync_aggregate, + &self.log, + &self.spec, ) } @@ -6635,13 +6636,17 @@ impl BeaconChain { &self, block_root: &Hash256, ) -> Result, ForkName)>, Error> { - let Some((state_root, slot)) = self - .get_blinded_block(block_root)? - .map(|block| (block.state_root(), block.slot())) - else { + let handle = self + .task_executor + .handle() + .ok_or(BeaconChainError::RuntimeShutdown)?; + + let Some(block) = handle.block_on(async { self.get_block(block_root).await })? else { return Ok(None); }; + let (state_root, slot) = (block.state_root(), block.slot()); + let Some(mut state) = self.get_state(&state_root, Some(slot))? else { return Ok(None); }; @@ -6651,12 +6656,12 @@ impl BeaconChain { .map_err(Error::InconsistentFork)?; match fork_name { - ForkName::Altair | ForkName::Merge => { - LightClientBootstrap::from_beacon_state(&mut state) + ForkName::Altair | ForkName::Merge | ForkName::Capella | ForkName::Deneb => { + LightClientBootstrap::from_beacon_state(&mut state, &block, &self.spec) .map(|bootstrap| Some((bootstrap, fork_name))) .map_err(Error::LightClientError) } - ForkName::Base | ForkName::Capella | ForkName::Deneb => Err(Error::UnsupportedFork), + ForkName::Base => Err(Error::UnsupportedFork), } } } diff --git a/beacon_node/beacon_chain/src/errors.rs b/beacon_node/beacon_chain/src/errors.rs index 9c1ba06f853..bfa58c42e65 100644 --- a/beacon_node/beacon_chain/src/errors.rs +++ b/beacon_node/beacon_chain/src/errors.rs @@ -250,6 +250,7 @@ easy_from_to!(StateAdvanceError, BeaconChainError); easy_from_to!(BlockReplayError, BeaconChainError); easy_from_to!(InconsistentFork, BeaconChainError); easy_from_to!(AvailabilityCheckError, BeaconChainError); +easy_from_to!(LightClientError, BeaconChainError); #[derive(Debug)] pub enum BlockProductionError { diff --git a/beacon_node/beacon_chain/src/light_client_finality_update_verification.rs b/beacon_node/beacon_chain/src/light_client_finality_update_verification.rs index 35863aa05ff..879fa02f7d9 100644 --- a/beacon_node/beacon_chain/src/light_client_finality_update_verification.rs +++ b/beacon_node/beacon_chain/src/light_client_finality_update_verification.rs @@ -48,7 +48,7 @@ impl VerifiedLightClientFinalityUpdate { // verify that enough time has passed for the block to have been propagated let start_time = chain .slot_clock - .start_of(rcv_finality_update.signature_slot) + .start_of(*rcv_finality_update.signature_slot()) .ok_or(Error::SigSlotStartIsNone)?; let one_third_slot_duration = Duration::new(chain.spec.seconds_per_slot / 3, 0); if seen_timestamp + chain.spec.maximum_gossip_clock_disparity() diff --git a/beacon_node/beacon_chain/src/light_client_optimistic_update_verification.rs b/beacon_node/beacon_chain/src/light_client_optimistic_update_verification.rs index 813b112db5a..5665adc3ed9 100644 --- a/beacon_node/beacon_chain/src/light_client_optimistic_update_verification.rs +++ b/beacon_node/beacon_chain/src/light_client_optimistic_update_verification.rs @@ -52,7 +52,7 @@ impl VerifiedLightClientOptimisticUpdate { // verify that enough time has passed for the block to have been propagated let start_time = chain .slot_clock - .start_of(rcv_optimistic_update.signature_slot) + .start_of(*rcv_optimistic_update.signature_slot()) .ok_or(Error::SigSlotStartIsNone)?; let one_third_slot_duration = Duration::new(chain.spec.seconds_per_slot / 3, 0); if seen_timestamp + chain.spec.maximum_gossip_clock_disparity() @@ -65,10 +65,7 @@ impl VerifiedLightClientOptimisticUpdate { let head_block = &head.snapshot.beacon_block; // check if we can process the optimistic update immediately // otherwise queue - let canonical_root = rcv_optimistic_update - .attested_header - .beacon - .canonical_root(); + let canonical_root = rcv_optimistic_update.get_canonical_root(); if canonical_root != head_block.message().parent_root() { return Err(Error::UnknownBlockParentRoot(canonical_root)); @@ -84,7 +81,7 @@ impl VerifiedLightClientOptimisticUpdate { return Err(Error::InvalidLightClientOptimisticUpdate); } - let parent_root = rcv_optimistic_update.attested_header.beacon.parent_root; + let parent_root = rcv_optimistic_update.get_parent_root(); Ok(Self { light_client_optimistic_update: rcv_optimistic_update, parent_root, diff --git a/beacon_node/beacon_chain/src/light_client_server_cache.rs b/beacon_node/beacon_chain/src/light_client_server_cache.rs index 1397e3fc9df..d480a6b56b2 100644 --- a/beacon_node/beacon_chain/src/light_client_server_cache.rs +++ b/beacon_node/beacon_chain/src/light_client_server_cache.rs @@ -8,7 +8,7 @@ use types::light_client_update::{FinalizedRootProofLen, FINALIZED_ROOT_INDEX}; use types::non_zero_usize::new_non_zero_usize; use types::{ BeaconBlockRef, BeaconState, ChainSpec, EthSpec, ForkName, Hash256, LightClientFinalityUpdate, - LightClientHeader, LightClientOptimisticUpdate, Slot, SyncAggregate, + LightClientOptimisticUpdate, Slot, SyncAggregate, }; /// A prev block cache miss requires to re-generate the state of the post-parent block. Items in the @@ -71,11 +71,12 @@ impl LightClientServerCache { /// results are cached either on disk or memory to be served via p2p and rest API pub fn recompute_and_cache_updates( &self, - log: &Logger, store: BeaconStore, block_parent_root: &Hash256, block_slot: Slot, sync_aggregate: &SyncAggregate, + log: &Logger, + chain_spec: &ChainSpec, ) -> Result<(), BeaconChainError> { let _timer = metrics::start_timer(&metrics::LIGHT_CLIENT_SERVER_CACHE_RECOMPUTE_UPDATES_TIMES); @@ -83,12 +84,13 @@ impl LightClientServerCache { let signature_slot = block_slot; let attested_block_root = block_parent_root; - let attested_block = store.get_blinded_block(attested_block_root)?.ok_or( - BeaconChainError::DBInconsistent(format!( - "Block not available {:?}", - attested_block_root - )), - )?; + let attested_block = + store + .get_full_block(attested_block_root)? + .ok_or(BeaconChainError::DBInconsistent(format!( + "Block not available {:?}", + attested_block_root + )))?; let cached_parts = self.get_or_compute_prev_block_cache( store.clone(), @@ -109,11 +111,12 @@ impl LightClientServerCache { }; if is_latest_optimistic { // can create an optimistic update, that is more recent - *self.latest_optimistic_update.write() = Some(LightClientOptimisticUpdate { - attested_header: block_to_light_client_header(attested_block.message()), - sync_aggregate: sync_aggregate.clone(), + *self.latest_optimistic_update.write() = Some(LightClientOptimisticUpdate::new( + &attested_block, + sync_aggregate.clone(), signature_slot, - }); + chain_spec, + )?); }; // Spec: Full nodes SHOULD provide the LightClientFinalityUpdate with the highest @@ -127,17 +130,16 @@ impl LightClientServerCache { if is_latest_finality & !cached_parts.finalized_block_root.is_zero() { // Immediately after checkpoint sync the finalized block may not be available yet. if let Some(finalized_block) = - store.get_blinded_block(&cached_parts.finalized_block_root)? + store.get_full_block(&cached_parts.finalized_block_root)? { - *self.latest_finality_update.write() = Some(LightClientFinalityUpdate { - // TODO: may want to cache this result from latest_optimistic_update if producing a - // light_client header becomes expensive - attested_header: block_to_light_client_header(attested_block.message()), - finalized_header: block_to_light_client_header(finalized_block.message()), - finality_branch: cached_parts.finality_branch.clone(), - sync_aggregate: sync_aggregate.clone(), + *self.latest_finality_update.write() = Some(LightClientFinalityUpdate::new( + &attested_block, + &finalized_block, + cached_parts.finality_branch.clone(), + sync_aggregate.clone(), signature_slot, - }); + chain_spec, + )?); } else { debug!( log, @@ -214,7 +216,7 @@ impl LightClientCachedData { } } -// Implements spec priorization rules: +// Implements spec prioritization rules: // > Full nodes SHOULD provide the LightClientFinalityUpdate with the highest attested_header.beacon.slot (if multiple, highest signature_slot) // // ref: https://github.com/ethereum/consensus-specs/blob/113c58f9bf9c08867f6f5f633c4d98e0364d612a/specs/altair/light-client/full-node.md#create_light_client_finality_update @@ -223,14 +225,15 @@ fn is_latest_finality_update( attested_slot: Slot, signature_slot: Slot, ) -> bool { - if attested_slot > prev.attested_header.beacon.slot { + let prev_slot = prev.get_attested_header_slot(); + if attested_slot > prev_slot { true } else { - attested_slot == prev.attested_header.beacon.slot && signature_slot > prev.signature_slot + attested_slot == prev_slot && signature_slot > *prev.signature_slot() } } -// Implements spec priorization rules: +// Implements spec prioritization rules: // > Full nodes SHOULD provide the LightClientOptimisticUpdate with the highest attested_header.beacon.slot (if multiple, highest signature_slot) // // ref: https://github.com/ethereum/consensus-specs/blob/113c58f9bf9c08867f6f5f633c4d98e0364d612a/specs/altair/light-client/full-node.md#create_light_client_optimistic_update @@ -239,18 +242,10 @@ fn is_latest_optimistic_update( attested_slot: Slot, signature_slot: Slot, ) -> bool { - if attested_slot > prev.attested_header.beacon.slot { + let prev_slot = prev.get_slot(); + if attested_slot > prev_slot { true } else { - attested_slot == prev.attested_header.beacon.slot && signature_slot > prev.signature_slot - } -} - -fn block_to_light_client_header( - block: BeaconBlockRef>, -) -> LightClientHeader { - // TODO: make fork aware - LightClientHeader { - beacon: block.block_header(), + attested_slot == prev_slot && signature_slot > *prev.signature_slot() } } diff --git a/beacon_node/http_api/src/lib.rs b/beacon_node/http_api/src/lib.rs index 5a8d5cae070..6d421e7c456 100644 --- a/beacon_node/http_api/src/lib.rs +++ b/beacon_node/http_api/src/lib.rs @@ -2337,7 +2337,7 @@ pub fn serve( let fork_name = chain .spec - .fork_name_at_slot::(update.signature_slot); + .fork_name_at_slot::(*update.signature_slot()); match accept_header { Some(api_types::Accept::Ssz) => Response::builder() .status(200) @@ -2384,7 +2384,7 @@ pub fn serve( let fork_name = chain .spec - .fork_name_at_slot::(update.signature_slot); + .fork_name_at_slot::(*update.signature_slot()); match accept_header { Some(api_types::Accept::Ssz) => Response::builder() .status(200) diff --git a/beacon_node/http_api/tests/tests.rs b/beacon_node/http_api/tests/tests.rs index 098f9f10512..ae109f93241 100644 --- a/beacon_node/http_api/tests/tests.rs +++ b/beacon_node/http_api/tests/tests.rs @@ -1717,7 +1717,7 @@ impl ApiTester { }; let expected = block.slot(); - assert_eq!(result.header.beacon.slot, expected); + assert_eq!(result.get_slot(), expected); self } diff --git a/beacon_node/lighthouse_network/src/rpc/codec/ssz_snappy.rs b/beacon_node/lighthouse_network/src/rpc/codec/ssz_snappy.rs index 58a9d137a96..c3c198adc48 100644 --- a/beacon_node/lighthouse_network/src/rpc/codec/ssz_snappy.rs +++ b/beacon_node/lighthouse_network/src/rpc/codec/ssz_snappy.rs @@ -590,9 +590,18 @@ fn handle_rpc_response( SupportedProtocol::MetaDataV1 => Ok(Some(RPCResponse::MetaData(MetaData::V1( MetaDataV1::from_ssz_bytes(decoded_buffer)?, )))), - SupportedProtocol::LightClientBootstrapV1 => Ok(Some(RPCResponse::LightClientBootstrap( - LightClientBootstrap::from_ssz_bytes(decoded_buffer)?, - ))), + SupportedProtocol::LightClientBootstrapV1 => match fork_name { + Some(fork_name) => Ok(Some(RPCResponse::LightClientBootstrap(Arc::new( + LightClientBootstrap::from_ssz_bytes(decoded_buffer, fork_name)?, + )))), + None => Err(RPCError::ErrorResponse( + RPCResponseErrorCode::InvalidRequest, + format!( + "No context bytes provided for {:?} response", + versioned_protocol + ), + )), + }, // MetaData V2 responses have no context bytes, so behave similarly to V1 responses SupportedProtocol::MetaDataV2 => Ok(Some(RPCResponse::MetaData(MetaData::V2( MetaDataV2::from_ssz_bytes(decoded_buffer)?, diff --git a/beacon_node/lighthouse_network/src/rpc/methods.rs b/beacon_node/lighthouse_network/src/rpc/methods.rs index 1a9a3ea93f2..6c5bc527d61 100644 --- a/beacon_node/lighthouse_network/src/rpc/methods.rs +++ b/beacon_node/lighthouse_network/src/rpc/methods.rs @@ -388,7 +388,7 @@ pub enum RPCResponse { BlobsByRange(Arc>), /// A response to a get LIGHT_CLIENT_BOOTSTRAP request. - LightClientBootstrap(LightClientBootstrap), + LightClientBootstrap(Arc>), /// A response to a get BLOBS_BY_ROOT request. BlobsByRoot(Arc>), @@ -569,11 +569,7 @@ impl std::fmt::Display for RPCResponse { RPCResponse::Pong(ping) => write!(f, "Pong: {}", ping.data), RPCResponse::MetaData(metadata) => write!(f, "Metadata: {}", metadata.seq_number()), RPCResponse::LightClientBootstrap(bootstrap) => { - write!( - f, - "LightClientBootstrap Slot: {}", - bootstrap.header.beacon.slot - ) + write!(f, "LightClientBootstrap Slot: {}", bootstrap.get_slot()) } } } diff --git a/beacon_node/lighthouse_network/src/service/api_types.rs b/beacon_node/lighthouse_network/src/service/api_types.rs index 96c9d283327..1a16e4c70be 100644 --- a/beacon_node/lighthouse_network/src/service/api_types.rs +++ b/beacon_node/lighthouse_network/src/service/api_types.rs @@ -93,7 +93,7 @@ pub enum Response { /// A response to a get BLOBS_BY_ROOT request. BlobsByRoot(Option>>), /// A response to a LightClientUpdate request. - LightClientBootstrap(LightClientBootstrap), + LightClientBootstrap(Arc>), } impl std::convert::From> for RPCCodedResponse { diff --git a/beacon_node/lighthouse_network/src/types/pubsub.rs b/beacon_node/lighthouse_network/src/types/pubsub.rs index 6582e56f18d..304cd6d9a2b 100644 --- a/beacon_node/lighthouse_network/src/types/pubsub.rs +++ b/beacon_node/lighthouse_network/src/types/pubsub.rs @@ -264,17 +264,31 @@ impl PubsubMessage { ))) } GossipKind::LightClientFinalityUpdate => { - let light_client_finality_update = - LightClientFinalityUpdate::from_ssz_bytes(data) - .map_err(|e| format!("{:?}", e))?; + let light_client_finality_update = match fork_context.from_context_bytes(gossip_topic.fork_digest) { + Some(&fork_name) => { + LightClientFinalityUpdate::from_ssz_bytes(data, fork_name) + .map_err(|e| format!("{:?}", e))? + }, + None => return Err(format!( + "light_client_finality_update topic invalid for given fork digest {:?}", + gossip_topic.fork_digest + )), + }; Ok(PubsubMessage::LightClientFinalityUpdate(Box::new( light_client_finality_update, ))) } GossipKind::LightClientOptimisticUpdate => { - let light_client_optimistic_update = - LightClientOptimisticUpdate::from_ssz_bytes(data) - .map_err(|e| format!("{:?}", e))?; + let light_client_optimistic_update = match fork_context.from_context_bytes(gossip_topic.fork_digest) { + Some(&fork_name) => { + LightClientOptimisticUpdate::from_ssz_bytes(data, fork_name) + .map_err(|e| format!("{:?}", e))? + }, + None => return Err(format!( + "light_client_optimistic_update topic invalid for given fork digest {:?}", + gossip_topic.fork_digest + )), + }; Ok(PubsubMessage::LightClientOptimisticUpdate(Box::new( light_client_optimistic_update, ))) diff --git a/beacon_node/network/src/network_beacon_processor/rpc_methods.rs b/beacon_node/network/src/network_beacon_processor/rpc_methods.rs index c63afcdb2fe..34c72b324af 100644 --- a/beacon_node/network/src/network_beacon_processor/rpc_methods.rs +++ b/beacon_node/network/src/network_beacon_processor/rpc_methods.rs @@ -304,7 +304,7 @@ impl NetworkBeaconProcessor { match self.chain.get_light_client_bootstrap(&block_root) { Ok(Some((bootstrap, _))) => self.send_response( peer_id, - Response::LightClientBootstrap(bootstrap), + Response::LightClientBootstrap(Arc::new(bootstrap)), request_id, ), Ok(None) => self.send_error_response( diff --git a/consensus/types/src/beacon_block_body.rs b/consensus/types/src/beacon_block_body.rs index 38155a4a6a5..976d51a5c90 100644 --- a/consensus/types/src/beacon_block_body.rs +++ b/consensus/types/src/beacon_block_body.rs @@ -15,6 +15,13 @@ pub type KzgCommitments = pub type KzgCommitmentOpts = FixedVector, ::MaxBlobsPerBlock>; +/// The number of leaves (including padding) on the `BeaconBlockBody` Merkle tree. +/// +/// ## Note +/// +/// This constant is set with the assumption that there are `> 8` and `<= 16` fields on the +/// `BeaconBlockBody`. **Tree hashing will fail if this value is set incorrectly.** +pub const NUM_BEACON_BLOCK_BODY_HASH_TREE_ROOT_LEAVES: usize = 16; /// Index of the `blob_kzg_commitments` leaf in the `BeaconBlockBody` tree post-deneb. pub const BLOB_KZG_COMMITMENTS_INDEX: usize = 11; @@ -591,6 +598,56 @@ impl From>> } } +impl BeaconBlockBody { + pub fn block_body_merkle_proof(&self, generalized_index: usize) -> Result, Error> { + let field_index = match generalized_index { + light_client_update::EXECUTION_PAYLOAD_INDEX => { + // Execution payload is a top-level field, subtract off the generalized indices + // for the internal nodes. Result should be 9, the field offset of the execution + // payload in the `BeaconBlockBody`: + // https://github.com/ethereum/consensus-specs/blob/dev/specs/deneb/beacon-chain.md#beaconblockbody + generalized_index + .checked_sub(NUM_BEACON_BLOCK_BODY_HASH_TREE_ROOT_LEAVES) + .ok_or(Error::IndexNotSupported(generalized_index))? + } + _ => return Err(Error::IndexNotSupported(generalized_index)), + }; + + let mut leaves = vec![ + self.randao_reveal().tree_hash_root(), + self.eth1_data().tree_hash_root(), + self.graffiti().tree_hash_root(), + self.proposer_slashings().tree_hash_root(), + self.attester_slashings().tree_hash_root(), + self.attestations().tree_hash_root(), + self.deposits().tree_hash_root(), + self.voluntary_exits().tree_hash_root(), + ]; + + if let Ok(sync_aggregate) = self.sync_aggregate() { + leaves.push(sync_aggregate.tree_hash_root()) + } + + if let Ok(execution_payload) = self.execution_payload() { + leaves.push(execution_payload.tree_hash_root()) + } + + if let Ok(bls_to_execution_changes) = self.bls_to_execution_changes() { + leaves.push(bls_to_execution_changes.tree_hash_root()) + } + + if let Ok(blob_kzg_commitments) = self.blob_kzg_commitments() { + leaves.push(blob_kzg_commitments.tree_hash_root()) + } + + let depth = light_client_update::EXECUTION_PAYLOAD_PROOF_LEN; + let tree = merkle_proof::MerkleTree::create(&leaves, depth); + let (_, proof) = tree.generate_proof(field_index, depth)?; + + Ok(proof) + } +} + /// Util method helpful for logging. pub fn format_kzg_commitments(commitments: &[KzgCommitment]) -> String { let commitment_strings: Vec = commitments.iter().map(|x| x.to_string()).collect(); diff --git a/consensus/types/src/lib.rs b/consensus/types/src/lib.rs index b07b497a2ae..e216a5da66b 100644 --- a/consensus/types/src/lib.rs +++ b/consensus/types/src/lib.rs @@ -152,11 +152,25 @@ pub use crate::fork_versioned_response::{ForkVersionDeserialize, ForkVersionedRe pub use crate::graffiti::{Graffiti, GRAFFITI_BYTES_LEN}; pub use crate::historical_batch::HistoricalBatch; pub use crate::indexed_attestation::IndexedAttestation; -pub use crate::light_client_bootstrap::LightClientBootstrap; -pub use crate::light_client_finality_update::LightClientFinalityUpdate; -pub use crate::light_client_header::LightClientHeader; -pub use crate::light_client_optimistic_update::LightClientOptimisticUpdate; -pub use crate::light_client_update::{Error as LightClientError, LightClientUpdate}; +pub use crate::light_client_bootstrap::{ + LightClientBootstrap, LightClientBootstrapAltair, LightClientBootstrapCapella, + LightClientBootstrapDeneb, +}; +pub use crate::light_client_finality_update::{ + LightClientFinalityUpdate, LightClientFinalityUpdateAltair, LightClientFinalityUpdateCapella, + LightClientFinalityUpdateDeneb, +}; +pub use crate::light_client_header::{ + LightClientHeader, LightClientHeaderAltair, LightClientHeaderCapella, LightClientHeaderDeneb, +}; +pub use crate::light_client_optimistic_update::{ + LightClientOptimisticUpdate, LightClientOptimisticUpdateAltair, + LightClientOptimisticUpdateCapella, LightClientOptimisticUpdateDeneb, +}; +pub use crate::light_client_update::{ + Error as LightClientError, LightClientUpdate, LightClientUpdateAltair, + LightClientUpdateCapella, LightClientUpdateDeneb, +}; pub use crate::participation_flags::ParticipationFlags; pub use crate::participation_list::ParticipationList; pub use crate::payload::{ diff --git a/consensus/types/src/light_client_bootstrap.rs b/consensus/types/src/light_client_bootstrap.rs index 6660783abd1..d4e85a351d5 100644 --- a/consensus/types/src/light_client_bootstrap.rs +++ b/consensus/types/src/light_client_bootstrap.rs @@ -1,68 +1,147 @@ use super::{BeaconState, EthSpec, FixedVector, Hash256, SyncCommittee}; use crate::{ - light_client_update::*, test_utils::TestRandom, ForkName, ForkVersionDeserialize, - LightClientHeader, + light_client_update::*, test_utils::TestRandom, ChainSpec, ForkName, ForkVersionDeserialize, + LightClientHeaderAltair, LightClientHeaderCapella, LightClientHeaderDeneb, SignedBeaconBlock, + Slot, }; +use derivative::Derivative; use serde::{Deserialize, Deserializer, Serialize}; use serde_json::Value; +use ssz::Decode; use ssz_derive::{Decode, Encode}; use std::sync::Arc; +use superstruct::superstruct; use test_random_derive::TestRandom; +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), + variant_attributes( + derive( + Debug, + Clone, + PartialEq, + Serialize, + Deserialize, + Derivative, + Decode, + Encode, + TestRandom, + arbitrary::Arbitrary, + TreeHash, + ), + serde(bound = "E: EthSpec", deny_unknown_fields), + arbitrary(bound = "E: EthSpec"), + ) +)] #[derive( - Debug, - Clone, - PartialEq, - Serialize, - Deserialize, - Encode, - Decode, - TestRandom, - arbitrary::Arbitrary, + Debug, Clone, Serialize, TreeHash, Encode, Deserialize, arbitrary::Arbitrary, PartialEq, )] -#[serde(bound = "T: EthSpec")] -#[arbitrary(bound = "T: EthSpec")] -pub struct LightClientBootstrap { +#[serde(untagged)] +#[tree_hash(enum_behaviour = "transparent")] +#[ssz(enum_behaviour = "transparent")] +#[serde(bound = "E: EthSpec", deny_unknown_fields)] +#[arbitrary(bound = "E: EthSpec")] +pub struct LightClientBootstrap { /// The requested beacon block header. - pub header: LightClientHeader, + #[superstruct(only(Altair), partial_getter(rename = "header_altair"))] + pub header: LightClientHeaderAltair, + #[superstruct(only(Capella), partial_getter(rename = "header_capella"))] + pub header: LightClientHeaderCapella, + #[superstruct(only(Deneb), partial_getter(rename = "header_deneb"))] + pub header: LightClientHeaderDeneb, /// The `SyncCommittee` used in the requested period. - pub current_sync_committee: Arc>, + pub current_sync_committee: Arc>, /// Merkle proof for sync committee pub current_sync_committee_branch: FixedVector, } -impl LightClientBootstrap { - pub fn from_beacon_state(beacon_state: &mut BeaconState) -> Result { +impl LightClientBootstrap { + pub fn get_slot<'a>(&'a self) -> Slot { + map_light_client_bootstrap_ref!(&'a _, self.to_ref(), |inner, cons| { + cons(inner); + inner.header.beacon.slot + }) + } + + pub fn from_ssz_bytes(bytes: &[u8], fork_name: ForkName) -> Result { + let bootstrap = match fork_name { + ForkName::Altair | ForkName::Merge => { + let header = LightClientBootstrapAltair::from_ssz_bytes(bytes)?; + Self::Altair(header) + } + ForkName::Capella => { + let header = LightClientBootstrapCapella::from_ssz_bytes(bytes)?; + Self::Capella(header) + } + ForkName::Deneb => { + let header = LightClientBootstrapDeneb::from_ssz_bytes(bytes)?; + Self::Deneb(header) + } + ForkName::Base => { + return Err(ssz::DecodeError::BytesInvalid(format!( + "LightClientBootstrap decoding for {fork_name} not implemented" + ))) + } + }; + + Ok(bootstrap) + } + + pub fn from_beacon_state( + beacon_state: &mut BeaconState, + block: &SignedBeaconBlock, + chain_spec: &ChainSpec, + ) -> Result { let mut header = beacon_state.latest_block_header().clone(); header.state_root = beacon_state.update_tree_hash_cache()?; let current_sync_committee_branch = - beacon_state.compute_merkle_proof(CURRENT_SYNC_COMMITTEE_INDEX)?; - Ok(LightClientBootstrap { - header: header.into(), - current_sync_committee: beacon_state.current_sync_committee()?.clone(), - current_sync_committee_branch: FixedVector::new(current_sync_committee_branch)?, - }) + FixedVector::new(beacon_state.compute_merkle_proof(CURRENT_SYNC_COMMITTEE_INDEX)?)?; + + let current_sync_committee = beacon_state.current_sync_committee()?.clone(); + + let light_client_bootstrap = match block + .fork_name(chain_spec) + .map_err(|_| Error::InconsistentFork)? + { + ForkName::Base => return Err(Error::AltairForkNotActive), + ForkName::Altair | ForkName::Merge => Self::Altair(LightClientBootstrapAltair { + header: LightClientHeaderAltair::block_to_light_client_header(block)?, + current_sync_committee, + current_sync_committee_branch, + }), + ForkName::Capella => Self::Capella(LightClientBootstrapCapella { + header: LightClientHeaderCapella::block_to_light_client_header(block)?, + current_sync_committee, + current_sync_committee_branch, + }), + ForkName::Deneb => Self::Deneb(LightClientBootstrapDeneb { + header: LightClientHeaderDeneb::block_to_light_client_header(block)?, + current_sync_committee, + current_sync_committee_branch, + }), + }; + + Ok(light_client_bootstrap) } } -impl ForkVersionDeserialize for LightClientBootstrap { +impl ForkVersionDeserialize for LightClientBootstrap { fn deserialize_by_fork<'de, D: Deserializer<'de>>( value: Value, fork_name: ForkName, ) -> Result { match fork_name { - ForkName::Altair | ForkName::Merge => { - Ok(serde_json::from_value::>(value) + ForkName::Altair | ForkName::Merge | ForkName::Capella | ForkName::Deneb => { + Ok(serde_json::from_value::>(value) .map_err(serde::de::Error::custom))? } - ForkName::Base | ForkName::Capella | ForkName::Deneb => { - Err(serde::de::Error::custom(format!( - "LightClientBootstrap failed to deserialize: unsupported fork '{}'", - fork_name - ))) - } + ForkName::Base => Err(serde::de::Error::custom(format!( + "LightClientBootstrap failed to deserialize: unsupported fork '{}'", + fork_name + ))), } } } @@ -72,5 +151,5 @@ mod tests { use super::*; use crate::MainnetEthSpec; - ssz_tests!(LightClientBootstrap); + ssz_tests!(LightClientBootstrapDeneb); } diff --git a/consensus/types/src/light_client_finality_update.rs b/consensus/types/src/light_client_finality_update.rs index 494e68b63f5..247ec87cde1 100644 --- a/consensus/types/src/light_client_finality_update.rs +++ b/consensus/types/src/light_client_finality_update.rs @@ -1,101 +1,177 @@ -use super::{ - EthSpec, FixedVector, Hash256, SignedBeaconBlock, SignedBlindedBeaconBlock, Slot, SyncAggregate, -}; +use super::{EthSpec, FixedVector, Hash256, Slot, SyncAggregate}; +use crate::ChainSpec; use crate::{ - light_client_update::*, test_utils::TestRandom, BeaconState, ChainSpec, ForkName, - ForkVersionDeserialize, LightClientHeader, + light_client_update::*, test_utils::TestRandom, ForkName, ForkVersionDeserialize, + LightClientHeaderAltair, LightClientHeaderCapella, LightClientHeaderDeneb, SignedBeaconBlock, }; +use derivative::Derivative; use serde::{Deserialize, Deserializer, Serialize}; use serde_json::Value; -use ssz_derive::{Decode, Encode}; +use ssz::Decode; +use ssz_derive::Decode; +use ssz_derive::Encode; +use superstruct::superstruct; use test_random_derive::TestRandom; -use tree_hash::TreeHash; +use tree_hash_derive::TreeHash; -/// A LightClientFinalityUpdate is the update light_client request or received by a gossip that -/// signal a new finalized beacon block header for the light client sync protocol. +#[superstruct( + variants(Altair, Capella, Deneb), + variant_attributes( + derive( + Debug, + Clone, + PartialEq, + Serialize, + Deserialize, + Derivative, + Decode, + Encode, + TestRandom, + arbitrary::Arbitrary, + TreeHash, + ), + serde(bound = "E: EthSpec", deny_unknown_fields), + arbitrary(bound = "E: EthSpec"), + ) +)] #[derive( - Debug, - Clone, - PartialEq, - Serialize, - Deserialize, - Encode, - Decode, - TestRandom, - arbitrary::Arbitrary, + Debug, Clone, Serialize, Encode, TreeHash, Deserialize, arbitrary::Arbitrary, PartialEq, )] -#[serde(bound = "T: EthSpec")] -#[arbitrary(bound = "T: EthSpec")] -pub struct LightClientFinalityUpdate { +#[serde(untagged)] +#[tree_hash(enum_behaviour = "transparent")] +#[ssz(enum_behaviour = "transparent")] +#[serde(bound = "E: EthSpec", deny_unknown_fields)] +#[arbitrary(bound = "E: EthSpec")] +pub struct LightClientFinalityUpdate { /// The last `BeaconBlockHeader` from the last attested block by the sync committee. - pub attested_header: LightClientHeader, + #[superstruct(only(Altair), partial_getter(rename = "attested_header_altair"))] + pub attested_header: LightClientHeaderAltair, + #[superstruct(only(Capella), partial_getter(rename = "attested_header_capella"))] + pub attested_header: LightClientHeaderCapella, + #[superstruct(only(Deneb), partial_getter(rename = "attested_header_deneb"))] + pub attested_header: LightClientHeaderDeneb, /// The last `BeaconBlockHeader` from the last attested finalized block (end of epoch). - pub finalized_header: LightClientHeader, + #[superstruct(only(Altair), partial_getter(rename = "finalized_header_altair"))] + pub finalized_header: LightClientHeaderAltair, + #[superstruct(only(Capella), partial_getter(rename = "finalized_header_capella"))] + pub finalized_header: LightClientHeaderCapella, + #[superstruct(only(Deneb), partial_getter(rename = "finalized_header_deneb"))] + pub finalized_header: LightClientHeaderDeneb, /// Merkle proof attesting finalized header. pub finality_branch: FixedVector, /// current sync aggreggate - pub sync_aggregate: SyncAggregate, + pub sync_aggregate: SyncAggregate, /// Slot of the sync aggregated singature pub signature_slot: Slot, } -impl LightClientFinalityUpdate { +impl LightClientFinalityUpdate { pub fn new( + attested_block: &SignedBeaconBlock, + finalized_block: &SignedBeaconBlock, + finality_branch: FixedVector, + sync_aggregate: SyncAggregate, + signature_slot: Slot, chain_spec: &ChainSpec, - beacon_state: &BeaconState, - block: &SignedBeaconBlock, - attested_state: &mut BeaconState, - finalized_block: &SignedBlindedBeaconBlock, ) -> Result { - let altair_fork_epoch = chain_spec - .altair_fork_epoch - .ok_or(Error::AltairForkNotActive)?; - if beacon_state.slot().epoch(T::slots_per_epoch()) < altair_fork_epoch { - return Err(Error::AltairForkNotActive); - } + let finality_update = match attested_block + .fork_name(chain_spec) + .map_err(|_| Error::InconsistentFork)? + { + ForkName::Altair | ForkName::Merge => { + let finality_update = LightClientFinalityUpdateAltair { + attested_header: LightClientHeaderAltair::block_to_light_client_header( + attested_block, + )?, + finalized_header: LightClientHeaderAltair::block_to_light_client_header( + finalized_block, + )?, + 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 => { + 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::Base => return Err(Error::AltairForkNotActive), + }; - let sync_aggregate = block.message().body().sync_aggregate()?; - if sync_aggregate.num_set_bits() < chain_spec.min_sync_committee_participants as usize { - return Err(Error::NotEnoughSyncCommitteeParticipants); - } + Ok(finality_update) + } - // Compute and validate attested header. - let mut attested_header = attested_state.latest_block_header().clone(); - attested_header.state_root = attested_state.update_tree_hash_cache()?; - // Build finalized header from finalized block - let finalized_header = finalized_block.message().block_header(); + pub fn get_attested_header_slot<'a>(&'a self) -> Slot { + map_light_client_finality_update_ref!(&'a _, self.to_ref(), |inner, cons| { + cons(inner); + inner.attested_header.beacon.slot + }) + } - if finalized_header.tree_hash_root() != beacon_state.finalized_checkpoint().root { - return Err(Error::InvalidFinalizedBlock); - } + pub fn from_ssz_bytes(bytes: &[u8], fork_name: ForkName) -> Result { + let finality_update = match fork_name { + ForkName::Altair | ForkName::Merge => { + let finality_update = LightClientFinalityUpdateAltair::from_ssz_bytes(bytes)?; + Self::Altair(finality_update) + } + ForkName::Capella => { + let finality_update = LightClientFinalityUpdateCapella::from_ssz_bytes(bytes)?; + Self::Capella(finality_update) + } + ForkName::Deneb => { + let finality_update = LightClientFinalityUpdateDeneb::from_ssz_bytes(bytes)?; + Self::Deneb(finality_update) + } + ForkName::Base => { + return Err(ssz::DecodeError::BytesInvalid(format!( + "LightClientFinalityUpdate decoding for {fork_name} not implemented" + ))) + } + }; - let finality_branch = attested_state.compute_merkle_proof(FINALIZED_ROOT_INDEX)?; - Ok(Self { - attested_header: attested_header.into(), - finalized_header: finalized_header.into(), - finality_branch: FixedVector::new(finality_branch)?, - sync_aggregate: sync_aggregate.clone(), - signature_slot: block.slot(), - }) + Ok(finality_update) } } -impl ForkVersionDeserialize for LightClientFinalityUpdate { +impl ForkVersionDeserialize for LightClientFinalityUpdate { fn deserialize_by_fork<'de, D: Deserializer<'de>>( value: Value, fork_name: ForkName, ) -> Result { match fork_name { - ForkName::Altair | ForkName::Merge => Ok(serde_json::from_value::< - LightClientFinalityUpdate, - >(value) - .map_err(serde::de::Error::custom))?, - ForkName::Base | ForkName::Capella | ForkName::Deneb => { - Err(serde::de::Error::custom(format!( - "LightClientFinalityUpdate failed to deserialize: unsupported fork '{}'", - fork_name - ))) - } + ForkName::Altair | ForkName::Merge | ForkName::Capella | ForkName::Deneb => Ok( + serde_json::from_value::>(value) + .map_err(serde::de::Error::custom), + )?, + ForkName::Base => Err(serde::de::Error::custom(format!( + "LightClientFinalityUpdate failed to deserialize: unsupported fork '{}'", + fork_name + ))), } } } @@ -105,5 +181,5 @@ mod tests { use super::*; use crate::MainnetEthSpec; - ssz_tests!(LightClientFinalityUpdate); + ssz_tests!(LightClientFinalityUpdateDeneb); } diff --git a/consensus/types/src/light_client_header.rs b/consensus/types/src/light_client_header.rs index 8fe31f7af8c..ce640b94ab6 100644 --- a/consensus/types/src/light_client_header.rs +++ b/consensus/types/src/light_client_header.rs @@ -1,26 +1,210 @@ -use crate::test_utils::TestRandom; use crate::BeaconBlockHeader; +use crate::ChainSpec; +use crate::ForkName; +use crate::ForkVersionDeserialize; +use crate::{light_client_update::*, BeaconBlockBody}; +use crate::{ + test_utils::TestRandom, EthSpec, ExecutionPayloadHeaderCapella, ExecutionPayloadHeaderDeneb, + FixedVector, Hash256, SignedBeaconBlock, +}; +use derivative::Derivative; use serde::{Deserialize, Serialize}; +use serde_json; +use ssz::Decode; use ssz_derive::{Decode, Encode}; +use std::marker::PhantomData; +use superstruct::superstruct; use test_random_derive::TestRandom; +use tree_hash_derive::TreeHash; +#[superstruct( + variants(Altair, Capella, Deneb), + variant_attributes( + derive( + Debug, + Clone, + PartialEq, + Serialize, + Deserialize, + Derivative, + Decode, + Encode, + TestRandom, + arbitrary::Arbitrary, + TreeHash, + ), + serde(bound = "E: EthSpec", deny_unknown_fields), + arbitrary(bound = "E: EthSpec"), + ) +)] #[derive( - Debug, - Clone, - PartialEq, - Serialize, - Deserialize, - Encode, - Decode, - TestRandom, - arbitrary::Arbitrary, + Debug, Clone, Serialize, TreeHash, Encode, Deserialize, arbitrary::Arbitrary, PartialEq, )] -pub struct LightClientHeader { +#[serde(untagged)] +#[tree_hash(enum_behaviour = "transparent")] +#[ssz(enum_behaviour = "transparent")] +#[serde(bound = "E: EthSpec", deny_unknown_fields)] +#[arbitrary(bound = "E: EthSpec")] +pub struct LightClientHeader { pub beacon: BeaconBlockHeader, + + #[superstruct( + only(Capella), + partial_getter(rename = "execution_payload_header_capella") + )] + pub execution: ExecutionPayloadHeaderCapella, + #[superstruct(only(Deneb), partial_getter(rename = "execution_payload_header_deneb"))] + pub execution: ExecutionPayloadHeaderDeneb, + + #[superstruct(only(Capella, Deneb))] + pub execution_branch: FixedVector, + + #[ssz(skip_serializing, skip_deserializing)] + #[tree_hash(skip_hashing)] + #[serde(skip)] + #[arbitrary(default)] + pub _phantom_data: PhantomData, +} + +impl LightClientHeader { + pub fn block_to_light_client_header( + block: &SignedBeaconBlock, + chain_spec: &ChainSpec, + ) -> Result { + let header = match block + .fork_name(chain_spec) + .map_err(|_| Error::InconsistentFork)? + { + ForkName::Base => return Err(Error::AltairForkNotActive), + ForkName::Altair | ForkName::Merge => LightClientHeader::Altair( + LightClientHeaderAltair::block_to_light_client_header(block)?, + ), + ForkName::Capella => LightClientHeader::Capella( + LightClientHeaderCapella::block_to_light_client_header(block)?, + ), + ForkName::Deneb => LightClientHeader::Deneb( + LightClientHeaderDeneb::block_to_light_client_header(block)?, + ), + }; + Ok(header) + } + + pub fn from_ssz_bytes(bytes: &[u8], fork_name: ForkName) -> Result { + let header = match fork_name { + ForkName::Altair | ForkName::Merge => { + let header = LightClientHeaderAltair::from_ssz_bytes(bytes)?; + LightClientHeader::Altair(header) + } + ForkName::Capella => { + let header = LightClientHeaderCapella::from_ssz_bytes(bytes)?; + LightClientHeader::Capella(header) + } + ForkName::Deneb => { + let header = LightClientHeaderDeneb::from_ssz_bytes(bytes)?; + LightClientHeader::Deneb(header) + } + ForkName::Base => { + return Err(ssz::DecodeError::BytesInvalid(format!( + "LightClientHeader decoding for {fork_name} not implemented" + ))) + } + }; + + Ok(header) + } + + /// Custom SSZ decoder that takes a `ForkName` as context. + pub fn from_ssz_bytes_for_fork( + bytes: &[u8], + fork_name: ForkName, + ) -> Result { + Self::from_ssz_bytes(bytes, fork_name) + } +} + +impl LightClientHeaderAltair { + pub fn block_to_light_client_header(block: &SignedBeaconBlock) -> Result { + Ok(LightClientHeaderAltair { + beacon: block.message().block_header(), + _phantom_data: PhantomData, + }) + } +} + +impl LightClientHeaderCapella { + pub fn block_to_light_client_header(block: &SignedBeaconBlock) -> Result { + let payload = block + .message() + .execution_payload()? + .execution_payload_capella()?; + + let header = ExecutionPayloadHeaderCapella::from(payload); + let beacon_block_body = BeaconBlockBody::from( + block + .message() + .body_capella() + .map_err(|_| Error::BeaconBlockBodyError)? + .to_owned(), + ); + + let execution_branch = + beacon_block_body.block_body_merkle_proof(EXECUTION_PAYLOAD_INDEX)?; + + return Ok(LightClientHeaderCapella { + beacon: block.message().block_header(), + execution: header, + execution_branch: FixedVector::new(execution_branch)?, + _phantom_data: PhantomData, + }); + } +} + +impl LightClientHeaderDeneb { + pub fn block_to_light_client_header(block: &SignedBeaconBlock) -> Result { + let payload = block + .message() + .execution_payload()? + .execution_payload_deneb()?; + + let header = ExecutionPayloadHeaderDeneb::from(payload); + let beacon_block_body = BeaconBlockBody::from( + block + .message() + .body_deneb() + .map_err(|_| Error::BeaconBlockBodyError)? + .to_owned(), + ); + + let execution_branch = + beacon_block_body.block_body_merkle_proof(EXECUTION_PAYLOAD_INDEX)?; + + Ok(LightClientHeaderDeneb { + beacon: block.message().block_header(), + execution: header, + execution_branch: FixedVector::new(execution_branch)?, + _phantom_data: PhantomData, + }) + } } -impl From for LightClientHeader { - fn from(beacon: BeaconBlockHeader) -> Self { - LightClientHeader { beacon } +impl ForkVersionDeserialize for LightClientHeader { + fn deserialize_by_fork<'de, D: serde::Deserializer<'de>>( + value: serde_json::value::Value, + fork_name: ForkName, + ) -> Result { + match fork_name { + ForkName::Altair | ForkName::Merge => serde_json::from_value(value) + .map(|light_client_header| Self::Altair(light_client_header)) + .map_err(serde::de::Error::custom), + ForkName::Capella => serde_json::from_value(value) + .map(|light_client_header| Self::Capella(light_client_header)) + .map_err(serde::de::Error::custom), + ForkName::Deneb => serde_json::from_value(value) + .map(|light_client_header| Self::Deneb(light_client_header)) + .map_err(serde::de::Error::custom), + ForkName::Base => Err(serde::de::Error::custom(format!( + "LightClientHeader deserialization for {fork_name} not implemented" + ))), + } } } diff --git a/consensus/types/src/light_client_optimistic_update.rs b/consensus/types/src/light_client_optimistic_update.rs index d883d735f35..88f287d753e 100644 --- a/consensus/types/src/light_client_optimistic_update.rs +++ b/consensus/types/src/light_client_optimistic_update.rs @@ -1,65 +1,155 @@ use super::{EthSpec, ForkName, ForkVersionDeserialize, Slot, SyncAggregate}; -use crate::light_client_header::LightClientHeader; +use crate::test_utils::TestRandom; use crate::{ - light_client_update::Error, test_utils::TestRandom, BeaconState, ChainSpec, SignedBeaconBlock, + light_client_update::*, ChainSpec, LightClientHeaderAltair, LightClientHeaderCapella, + LightClientHeaderDeneb, SignedBeaconBlock, }; +use derivative::Derivative; use serde::{Deserialize, Deserializer, Serialize}; use serde_json::Value; -use ssz_derive::{Decode, Encode}; +use ssz::Decode; +use ssz_derive::Decode; +use ssz_derive::Encode; +use superstruct::superstruct; use test_random_derive::TestRandom; -use tree_hash::TreeHash; +use tree_hash::Hash256; +use tree_hash_derive::TreeHash; /// A LightClientOptimisticUpdate is the update we send on each slot, /// it is based off the current unfinalized epoch is verified only against BLS signature. +#[superstruct( + variants(Altair, Capella, Deneb), + variant_attributes( + derive( + Debug, + Clone, + PartialEq, + Serialize, + Deserialize, + Derivative, + Decode, + Encode, + TestRandom, + arbitrary::Arbitrary, + TreeHash, + ), + serde(bound = "E: EthSpec", deny_unknown_fields), + arbitrary(bound = "E: EthSpec"), + ) +)] #[derive( - Debug, - Clone, - PartialEq, - Serialize, - Deserialize, - Encode, - Decode, - TestRandom, - arbitrary::Arbitrary, + Debug, Clone, Serialize, Encode, TreeHash, Deserialize, arbitrary::Arbitrary, PartialEq, )] -#[serde(bound = "T: EthSpec")] -#[arbitrary(bound = "T: EthSpec")] -pub struct LightClientOptimisticUpdate { +#[serde(untagged)] +#[tree_hash(enum_behaviour = "transparent")] +#[ssz(enum_behaviour = "transparent")] +#[serde(bound = "E: EthSpec", deny_unknown_fields)] +#[arbitrary(bound = "E: EthSpec")] +pub struct LightClientOptimisticUpdate { /// The last `BeaconBlockHeader` from the last attested block by the sync committee. - pub attested_header: LightClientHeader, + #[superstruct(only(Altair), partial_getter(rename = "attested_header_altair"))] + pub attested_header: LightClientHeaderAltair, + #[superstruct(only(Capella), partial_getter(rename = "attested_header_capella"))] + pub attested_header: LightClientHeaderCapella, + #[superstruct(only(Deneb), partial_getter(rename = "attested_header_deneb"))] + pub attested_header: LightClientHeaderDeneb, /// current sync aggreggate - pub sync_aggregate: SyncAggregate, + pub sync_aggregate: SyncAggregate, /// Slot of the sync aggregated singature pub signature_slot: Slot, } -impl LightClientOptimisticUpdate { +impl LightClientOptimisticUpdate { pub fn new( + attested_block: &SignedBeaconBlock, + sync_aggregate: SyncAggregate, + signature_slot: Slot, chain_spec: &ChainSpec, - block: &SignedBeaconBlock, - attested_state: &BeaconState, ) -> Result { - let altair_fork_epoch = chain_spec - .altair_fork_epoch - .ok_or(Error::AltairForkNotActive)?; - if attested_state.slot().epoch(T::slots_per_epoch()) < altair_fork_epoch { - return Err(Error::AltairForkNotActive); - } + let optimistic_update = match attested_block + .fork_name(chain_spec) + .map_err(|_| Error::InconsistentFork)? + { + ForkName::Altair | ForkName::Merge => { + let optimistic_update = LightClientOptimisticUpdateAltair { + attested_header: LightClientHeaderAltair::block_to_light_client_header( + attested_block, + )?, + sync_aggregate, + signature_slot, + }; + Self::Altair(optimistic_update) + } + ForkName::Capella => { + let optimistic_update = LightClientOptimisticUpdateCapella { + attested_header: LightClientHeaderCapella::block_to_light_client_header( + attested_block, + )?, + sync_aggregate, + signature_slot, + }; + Self::Capella(optimistic_update) + } + ForkName::Deneb => { + let optimistic_update = LightClientOptimisticUpdateDeneb { + attested_header: LightClientHeaderDeneb::block_to_light_client_header( + attested_block, + )?, + sync_aggregate, + signature_slot, + }; + Self::Deneb(optimistic_update) + } + ForkName::Base => return Err(Error::AltairForkNotActive), + }; - let sync_aggregate = block.message().body().sync_aggregate()?; - if sync_aggregate.num_set_bits() < chain_spec.min_sync_committee_participants as usize { - return Err(Error::NotEnoughSyncCommitteeParticipants); - } + Ok(optimistic_update) + } + + pub fn get_slot<'a>(&'a self) -> Slot { + map_light_client_optimistic_update_ref!(&'a _, self.to_ref(), |inner, cons| { + cons(inner); + inner.attested_header.beacon.slot + }) + } - // Compute and validate attested header. - let mut attested_header = attested_state.latest_block_header().clone(); - attested_header.state_root = attested_state.tree_hash_root(); - Ok(Self { - attested_header: attested_header.into(), - sync_aggregate: sync_aggregate.clone(), - signature_slot: block.slot(), + pub fn get_canonical_root<'a>(&'a self) -> Hash256 { + map_light_client_optimistic_update_ref!(&'a _, self.to_ref(), |inner, cons| { + cons(inner); + inner.attested_header.beacon.canonical_root() }) } + + pub fn get_parent_root<'a>(&'a self) -> Hash256 { + map_light_client_optimistic_update_ref!(&'a _, self.to_ref(), |inner, cons| { + cons(inner); + inner.attested_header.beacon.parent_root + }) + } + + pub fn from_ssz_bytes(bytes: &[u8], fork_name: ForkName) -> Result { + let optimistic_update = match fork_name { + ForkName::Altair | ForkName::Merge => { + let optimistic_update = LightClientOptimisticUpdateAltair::from_ssz_bytes(bytes)?; + Self::Altair(optimistic_update) + } + ForkName::Capella => { + let optimistic_update = LightClientOptimisticUpdateCapella::from_ssz_bytes(bytes)?; + Self::Capella(optimistic_update) + } + ForkName::Deneb => { + let optimistic_update = LightClientOptimisticUpdateDeneb::from_ssz_bytes(bytes)?; + Self::Deneb(optimistic_update) + } + ForkName::Base => { + return Err(ssz::DecodeError::BytesInvalid(format!( + "LightClientOptimisticUpdate decoding for {fork_name} not implemented" + ))) + } + }; + + Ok(optimistic_update) + } } impl ForkVersionDeserialize for LightClientOptimisticUpdate { @@ -68,16 +158,14 @@ impl ForkVersionDeserialize for LightClientOptimisticUpdate { fork_name: ForkName, ) -> Result { match fork_name { - ForkName::Altair | ForkName::Merge => Ok(serde_json::from_value::< - LightClientOptimisticUpdate, - >(value) - .map_err(serde::de::Error::custom))?, - ForkName::Base | ForkName::Capella | ForkName::Deneb => { - Err(serde::de::Error::custom(format!( - "LightClientOptimisticUpdate failed to deserialize: unsupported fork '{}'", - fork_name - ))) - } + ForkName::Altair | ForkName::Merge | ForkName::Capella | ForkName::Deneb => Ok( + serde_json::from_value::>(value) + .map_err(serde::de::Error::custom), + )?, + ForkName::Base => Err(serde::de::Error::custom(format!( + "LightClientOptimisticUpdate failed to deserialize: unsupported fork '{}'", + fork_name + ))), } } } @@ -87,5 +175,5 @@ mod tests { use super::*; use crate::MainnetEthSpec; - ssz_tests!(LightClientOptimisticUpdate); + ssz_tests!(LightClientOptimisticUpdateDeneb); } diff --git a/consensus/types/src/light_client_update.rs b/consensus/types/src/light_client_update.rs index 718cd7553f9..09cc1950990 100644 --- a/consensus/types/src/light_client_update.rs +++ b/consensus/types/src/light_client_update.rs @@ -1,28 +1,38 @@ -use super::{BeaconBlockHeader, EthSpec, FixedVector, Hash256, Slot, SyncAggregate, SyncCommittee}; +use super::{EthSpec, FixedVector, Hash256, Slot, SyncAggregate, SyncCommittee}; use crate::{ - beacon_state, test_utils::TestRandom, BeaconBlock, BeaconState, ChainSpec, ForkName, - ForkVersionDeserialize, LightClientHeader, + beacon_state, test_utils::TestRandom, BeaconBlock, BeaconBlockHeader, BeaconState, ChainSpec, + ForkName, ForkVersionDeserialize, LightClientHeaderAltair, LightClientHeaderCapella, + LightClientHeaderDeneb, SignedBeaconBlock, }; +use derivative::Derivative; use safe_arith::ArithError; use serde::{Deserialize, Deserializer, Serialize}; use serde_json::Value; -use ssz_derive::{Decode, Encode}; -use ssz_types::typenum::{U5, U6}; +use ssz::Decode; +use ssz_derive::Decode; +use ssz_derive::Encode; +use ssz_types::typenum::{U4, U5, U6}; use std::sync::Arc; +use superstruct::superstruct; use test_random_derive::TestRandom; use tree_hash::TreeHash; +use tree_hash_derive::TreeHash; pub const FINALIZED_ROOT_INDEX: usize = 105; pub const CURRENT_SYNC_COMMITTEE_INDEX: usize = 54; pub const NEXT_SYNC_COMMITTEE_INDEX: usize = 55; +pub const EXECUTION_PAYLOAD_INDEX: usize = 25; pub type FinalizedRootProofLen = U6; pub type CurrentSyncCommitteeProofLen = U5; +pub type ExecutionPayloadProofLen = U4; + pub type NextSyncCommitteeProofLen = U5; 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; #[derive(Debug, PartialEq, Clone)] pub enum Error { @@ -33,6 +43,8 @@ pub enum Error { NotEnoughSyncCommitteeParticipants, MismatchingPeriods, InvalidFinalizedBlock, + BeaconBlockBodyError, + InconsistentFork, } impl From for Error { @@ -53,77 +65,114 @@ impl From for Error { } } -/// A LightClientUpdate is the update we request solely to either complete the bootstraping process, +/// A LightClientUpdate is the update we request solely to either complete the bootstrapping process, /// or to sync up to the last committee period, we need to have one ready for each ALTAIR period /// we go over, note: there is no need to keep all of the updates from [ALTAIR_PERIOD, CURRENT_PERIOD]. +#[superstruct( + variants(Altair, Capella, Deneb), + variant_attributes( + derive( + Debug, + Clone, + PartialEq, + Serialize, + Deserialize, + Derivative, + Decode, + Encode, + TestRandom, + arbitrary::Arbitrary, + TreeHash, + ), + serde(bound = "E: EthSpec", deny_unknown_fields), + arbitrary(bound = "E: EthSpec"), + ) +)] #[derive( - Debug, - Clone, - PartialEq, - Serialize, - Deserialize, - Encode, - Decode, - TestRandom, - arbitrary::Arbitrary, + Debug, Clone, Serialize, Encode, TreeHash, Deserialize, arbitrary::Arbitrary, PartialEq, )] -#[serde(bound = "T: EthSpec")] -#[arbitrary(bound = "T: EthSpec")] -pub struct LightClientUpdate { +#[serde(untagged)] +#[tree_hash(enum_behaviour = "transparent")] +#[ssz(enum_behaviour = "transparent")] +#[serde(bound = "E: EthSpec", deny_unknown_fields)] +#[arbitrary(bound = "E: EthSpec")] +pub struct LightClientUpdate { /// The last `BeaconBlockHeader` from the last attested block by the sync committee. - pub attested_header: LightClientHeader, + #[superstruct(only(Altair), partial_getter(rename = "attested_header_altair"))] + pub attested_header: LightClientHeaderAltair, + #[superstruct(only(Capella), partial_getter(rename = "attested_header_capella"))] + pub attested_header: LightClientHeaderCapella, + #[superstruct(only(Deneb), partial_getter(rename = "attested_header_deneb"))] + pub attested_header: LightClientHeaderDeneb, /// The `SyncCommittee` used in the next period. - pub next_sync_committee: Arc>, + pub next_sync_committee: Arc>, /// Merkle proof for next sync committee pub next_sync_committee_branch: FixedVector, /// The last `BeaconBlockHeader` from the last attested finalized block (end of epoch). - pub finalized_header: LightClientHeader, + #[superstruct(only(Altair), partial_getter(rename = "finalized_header_altair"))] + pub finalized_header: LightClientHeaderAltair, + #[superstruct(only(Capella), partial_getter(rename = "finalized_header_capella"))] + pub finalized_header: LightClientHeaderCapella, + #[superstruct(only(Deneb), partial_getter(rename = "finalized_header_deneb"))] + pub finalized_header: LightClientHeaderDeneb, /// Merkle proof attesting finalized header. pub finality_branch: FixedVector, /// current sync aggreggate - pub sync_aggregate: SyncAggregate, - /// Slot of the sync aggregated singature + pub sync_aggregate: SyncAggregate, + /// Slot of the sync aggregated signature pub signature_slot: Slot, } -impl LightClientUpdate { - pub fn new( - chain_spec: ChainSpec, - beacon_state: BeaconState, - block: BeaconBlock, - attested_state: &mut BeaconState, - finalized_block: BeaconBlock, - ) -> Result { - let altair_fork_epoch = chain_spec - .altair_fork_epoch - .ok_or(Error::AltairForkNotActive)?; - if attested_state.slot().epoch(T::slots_per_epoch()) < altair_fork_epoch { - return Err(Error::AltairForkNotActive); +impl ForkVersionDeserialize for LightClientUpdate { + fn deserialize_by_fork<'de, D: Deserializer<'de>>( + value: Value, + fork_name: ForkName, + ) -> Result { + match fork_name { + ForkName::Altair | ForkName::Merge | ForkName::Capella | ForkName::Deneb => { + Ok(serde_json::from_value::>(value) + .map_err(serde::de::Error::custom))? + } + ForkName::Base => Err(serde::de::Error::custom(format!( + "LightClientUpdate failed to deserialize: unsupported fork '{}'", + fork_name + ))), } + } +} +impl LightClientUpdate { + pub fn new( + beacon_state: BeaconState, + block: BeaconBlock, + attested_state: &mut BeaconState, + attested_block: &SignedBeaconBlock, + finalized_block: &SignedBeaconBlock, + chain_spec: &ChainSpec, + ) -> Result { let sync_aggregate = block.body().sync_aggregate()?; if sync_aggregate.num_set_bits() < chain_spec.min_sync_committee_participants as usize { return Err(Error::NotEnoughSyncCommitteeParticipants); } - let signature_period = block.epoch().sync_committee_period(&chain_spec)?; + let signature_period = block.epoch().sync_committee_period(chain_spec)?; // Compute and validate attested header. let mut attested_header = attested_state.latest_block_header().clone(); attested_header.state_root = attested_state.tree_hash_root(); let attested_period = attested_header .slot - .epoch(T::slots_per_epoch()) - .sync_committee_period(&chain_spec)?; + .epoch(E::slots_per_epoch()) + .sync_committee_period(chain_spec)?; if attested_period != signature_period { return Err(Error::MismatchingPeriods); } // Build finalized header from finalized block let finalized_header = BeaconBlockHeader { slot: finalized_block.slot(), - proposer_index: finalized_block.proposer_index(), + proposer_index: finalized_block.message().proposer_index(), parent_root: finalized_block.parent_root(), state_root: finalized_block.state_root(), - body_root: finalized_block.body_root(), + body_root: finalized_block.message().body_root(), }; if finalized_header.tree_hash_root() != beacon_state.finalized_checkpoint().root { return Err(Error::InvalidFinalizedBlock); @@ -131,35 +180,84 @@ impl LightClientUpdate { let next_sync_committee_branch = attested_state.compute_merkle_proof(NEXT_SYNC_COMMITTEE_INDEX)?; let finality_branch = attested_state.compute_merkle_proof(FINALIZED_ROOT_INDEX)?; - Ok(Self { - attested_header: attested_header.into(), - next_sync_committee: attested_state.next_sync_committee()?.clone(), - next_sync_committee_branch: FixedVector::new(next_sync_committee_branch)?, - finalized_header: finalized_header.into(), - finality_branch: FixedVector::new(finality_branch)?, - sync_aggregate: sync_aggregate.clone(), - signature_slot: block.slot(), - }) + + let light_client_update = match attested_block + .fork_name(chain_spec) + .map_err(|_| Error::InconsistentFork)? + { + ForkName::Base => return Err(Error::AltairForkNotActive), + ForkName::Altair | ForkName::Merge => { + let attested_header = + LightClientHeaderAltair::block_to_light_client_header(attested_block)?; + let finalized_header = + LightClientHeaderAltair::block_to_light_client_header(finalized_block)?; + Self::Altair(LightClientUpdateAltair { + attested_header, + next_sync_committee: attested_state.next_sync_committee()?.clone(), + next_sync_committee_branch: FixedVector::new(next_sync_committee_branch)?, + finalized_header, + finality_branch: FixedVector::new(finality_branch)?, + sync_aggregate: sync_aggregate.clone(), + signature_slot: block.slot(), + }) + } + ForkName::Capella => { + let attested_header = + LightClientHeaderCapella::block_to_light_client_header(attested_block)?; + let finalized_header = + LightClientHeaderCapella::block_to_light_client_header(finalized_block)?; + Self::Capella(LightClientUpdateCapella { + attested_header, + next_sync_committee: attested_state.next_sync_committee()?.clone(), + next_sync_committee_branch: FixedVector::new(next_sync_committee_branch)?, + finalized_header, + finality_branch: FixedVector::new(finality_branch)?, + sync_aggregate: sync_aggregate.clone(), + signature_slot: block.slot(), + }) + } + ForkName::Deneb => { + let attested_header = + LightClientHeaderDeneb::block_to_light_client_header(attested_block)?; + let finalized_header = + LightClientHeaderDeneb::block_to_light_client_header(finalized_block)?; + Self::Deneb(LightClientUpdateDeneb { + attested_header, + next_sync_committee: attested_state.next_sync_committee()?.clone(), + next_sync_committee_branch: FixedVector::new(next_sync_committee_branch)?, + finalized_header, + finality_branch: FixedVector::new(finality_branch)?, + sync_aggregate: sync_aggregate.clone(), + signature_slot: block.slot(), + }) + } + }; + + Ok(light_client_update) } -} -impl ForkVersionDeserialize for LightClientUpdate { - fn deserialize_by_fork<'de, D: Deserializer<'de>>( - value: Value, - fork_name: ForkName, - ) -> Result { - match fork_name { + pub fn from_ssz_bytes(bytes: &[u8], fork_name: ForkName) -> Result { + let update = match fork_name { ForkName::Altair | ForkName::Merge => { - Ok(serde_json::from_value::>(value) - .map_err(serde::de::Error::custom))? + let update = LightClientUpdateAltair::from_ssz_bytes(bytes)?; + Self::Altair(update) + } + ForkName::Capella => { + let update = LightClientUpdateCapella::from_ssz_bytes(bytes)?; + Self::Capella(update) } - ForkName::Base | ForkName::Capella | ForkName::Deneb => { - Err(serde::de::Error::custom(format!( - "LightClientUpdate failed to deserialize: unsupported fork '{}'", - fork_name + ForkName::Deneb => { + let update = LightClientUpdateDeneb::from_ssz_bytes(bytes)?; + Self::Deneb(update) + } + ForkName::Base => { + return Err(ssz::DecodeError::BytesInvalid(format!( + "LightClientUpdate decoding for {fork_name} not implemented" ))) } - } + }; + + Ok(update) } } @@ -169,7 +267,7 @@ mod tests { use crate::MainnetEthSpec; use ssz_types::typenum::Unsigned; - ssz_tests!(LightClientUpdate); + ssz_tests!(LightClientUpdateDeneb); #[test] fn finalized_root_params() { diff --git a/consensus/types/src/test_utils/macros.rs b/consensus/types/src/test_utils/macros.rs index 1e275a5760e..4fd7720689d 100644 --- a/consensus/types/src/test_utils/macros.rs +++ b/consensus/types/src/test_utils/macros.rs @@ -20,7 +20,6 @@ macro_rules! ssz_tests { let original = <$type>::random_for_test(&mut rng); let bytes = ssz_encode(&original); - println!("bytes length: {}", bytes.len()); let decoded = <$type>::from_ssz_bytes(&bytes).unwrap(); assert_eq!(original, decoded); diff --git a/testing/ef_tests/check_all_files_accessed.py b/testing/ef_tests/check_all_files_accessed.py index 1d1f2fa49a0..7629d61827f 100755 --- a/testing/ef_tests/check_all_files_accessed.py +++ b/testing/ef_tests/check_all_files_accessed.py @@ -29,18 +29,8 @@ "tests/.*/.*/light_client", # LightClientStore "tests/.*/.*/ssz_static/LightClientStore", - # LightClientUpdate - "tests/.*/.*/ssz_static/LightClientUpdate", # LightClientSnapshot "tests/.*/.*/ssz_static/LightClientSnapshot", - # LightClientBootstrap - "tests/.*/.*/ssz_static/LightClientBootstrap", - # LightClientOptimistic - "tests/.*/.*/ssz_static/LightClientOptimistic", - # LightClientFinalityUpdate - "tests/.*/.*/ssz_static/LightClientFinalityUpdate", - # LightClientHeader - "tests/.*/.*/ssz_static/LightClientHeader", # One of the EF researchers likes to pack the tarballs on a Mac ".*\.DS_Store.*", # More Mac weirdness. diff --git a/testing/ef_tests/src/type_name.rs b/testing/ef_tests/src/type_name.rs index 13121854acc..ef5d7eb001c 100644 --- a/testing/ef_tests/src/type_name.rs +++ b/testing/ef_tests/src/type_name.rs @@ -73,6 +73,38 @@ type_name!(Fork); type_name!(ForkData); type_name_generic!(HistoricalBatch); type_name_generic!(IndexedAttestation); +type_name_generic!(LightClientBootstrap); +type_name_generic!(LightClientBootstrapAltair, "LightClientBootstrap"); +type_name_generic!(LightClientBootstrapCapella, "LightClientBootstrap"); +type_name_generic!(LightClientBootstrapDeneb, "LightClientBootstrap"); +type_name_generic!(LightClientFinalityUpdate); +type_name_generic!(LightClientFinalityUpdateAltair, "LightClientFinalityUpdate"); +type_name_generic!( + LightClientFinalityUpdateCapella, + "LightClientFinalityUpdate" +); +type_name_generic!(LightClientFinalityUpdateDeneb, "LightClientFinalityUpdate"); +type_name_generic!(LightClientHeader); +type_name_generic!(LightClientHeaderDeneb, "LightClientHeader"); +type_name_generic!(LightClientHeaderCapella, "LightClientHeader"); +type_name_generic!(LightClientHeaderAltair, "LightClientHeader"); +type_name_generic!(LightClientOptimisticUpdate); +type_name_generic!( + LightClientOptimisticUpdateAltair, + "LightClientOptimisticUpdate" +); +type_name_generic!( + LightClientOptimisticUpdateCapella, + "LightClientOptimisticUpdate" +); +type_name_generic!( + LightClientOptimisticUpdateDeneb, + "LightClientOptimisticUpdate" +); +type_name_generic!(LightClientUpdate); +type_name_generic!(LightClientUpdateAltair, "LightClientUpdate"); +type_name_generic!(LightClientUpdateCapella, "LightClientUpdate"); +type_name_generic!(LightClientUpdateDeneb, "LightClientUpdate"); type_name_generic!(PendingAttestation); type_name!(ProposerSlashing); type_name_generic!(SignedAggregateAndProof); diff --git a/testing/ef_tests/tests/tests.rs b/testing/ef_tests/tests/tests.rs index 5ed657c6522..3093239f7fd 100644 --- a/testing/ef_tests/tests/tests.rs +++ b/testing/ef_tests/tests/tests.rs @@ -217,7 +217,7 @@ mod ssz_static { use ef_tests::{Handler, SszStaticHandler, SszStaticTHCHandler, SszStaticWithSpecHandler}; use types::blob_sidecar::BlobIdentifier; use types::historical_summary::HistoricalSummary; - use types::*; + use types::{LightClientBootstrapAltair, *}; ssz_static_test!(aggregate_and_proof, AggregateAndProof<_>); ssz_static_test!(attestation, Attestation<_>); @@ -236,7 +236,6 @@ mod ssz_static { ssz_static_test!(fork_data, ForkData); ssz_static_test!(historical_batch, HistoricalBatch<_>); ssz_static_test!(indexed_attestation, IndexedAttestation<_>); - // NOTE: LightClient* intentionally omitted ssz_static_test!(pending_attestation, PendingAttestation<_>); ssz_static_test!(proposer_slashing, ProposerSlashing); ssz_static_test!(signed_aggregate_and_proof, SignedAggregateAndProof<_>); @@ -250,7 +249,6 @@ mod ssz_static { ssz_static_test!(signing_data, SigningData); ssz_static_test!(validator, Validator); ssz_static_test!(voluntary_exit, VoluntaryExit); - // BeaconBlockBody has no internal indicator of which fork it is for, so we test it separately. #[test] fn beacon_block_body() { @@ -285,6 +283,135 @@ mod ssz_static { .run(); } + // LightClientBootstrap has no internal indicator of which fork it is for, so we test it separately. + #[test] + fn light_client_bootstrap() { + SszStaticHandler::, MinimalEthSpec>::altair_only() + .run(); + SszStaticHandler::, MainnetEthSpec>::altair_only() + .run(); + SszStaticHandler::, MinimalEthSpec>::merge_only( + ) + .run(); + SszStaticHandler::, MainnetEthSpec>::merge_only( + ) + .run(); + SszStaticHandler::, MinimalEthSpec>::capella_only() + .run(); + SszStaticHandler::, MainnetEthSpec>::capella_only() + .run(); + SszStaticHandler::, MinimalEthSpec>::deneb_only() + .run(); + SszStaticHandler::, MainnetEthSpec>::deneb_only() + .run(); + } + + // LightClientHeader has no internal indicator of which fork it is for, so we test it separately. + #[test] + fn light_client_header() { + SszStaticHandler::, MinimalEthSpec>::altair_only() + .run(); + SszStaticHandler::, MainnetEthSpec>::altair_only() + .run(); + SszStaticHandler::, MinimalEthSpec>::merge_only() + .run(); + SszStaticHandler::, MainnetEthSpec>::merge_only() + .run(); + + SszStaticHandler::, MinimalEthSpec>::capella_only( + ) + .run(); + SszStaticHandler::, MainnetEthSpec>::capella_only( + ) + .run(); + + SszStaticHandler::, MinimalEthSpec>::deneb_only() + .run(); + SszStaticHandler::, MainnetEthSpec>::deneb_only() + .run(); + } + + // LightClientOptimisticUpdate has no internal indicator of which fork it is for, so we test it separately. + #[test] + fn light_client_optimistic_update() { + SszStaticHandler::, MinimalEthSpec>::altair_only( + ) + .run(); + SszStaticHandler::, MainnetEthSpec>::altair_only( + ) + .run(); + SszStaticHandler::, MinimalEthSpec>::merge_only( + ) + .run(); + SszStaticHandler::, MainnetEthSpec>::merge_only( + ) + .run(); + SszStaticHandler::, MinimalEthSpec>::capella_only( + ) + .run(); + SszStaticHandler::, MainnetEthSpec>::capella_only( + ) + .run(); + SszStaticHandler::, MinimalEthSpec>::deneb_only( + ) + .run(); + SszStaticHandler::, MainnetEthSpec>::deneb_only( + ) + .run(); + } + + // LightClientFinalityUpdate has no internal indicator of which fork it is for, so we test it separately. + #[test] + fn light_client_finality_update() { + SszStaticHandler::, MinimalEthSpec>::altair_only( + ) + .run(); + SszStaticHandler::, MainnetEthSpec>::altair_only( + ) + .run(); + SszStaticHandler::, MinimalEthSpec>::merge_only( + ) + .run(); + SszStaticHandler::, MainnetEthSpec>::merge_only( + ) + .run(); + SszStaticHandler::, MinimalEthSpec>::capella_only( + ) + .run(); + SszStaticHandler::, MainnetEthSpec>::capella_only( + ) + .run(); + SszStaticHandler::, MinimalEthSpec>::deneb_only( + ) + .run(); + SszStaticHandler::, MainnetEthSpec>::deneb_only( + ) + .run(); + } + + // LightClientUpdate has no internal indicator of which fork it is for, so we test it separately. + #[test] + fn light_client_update() { + SszStaticHandler::, MinimalEthSpec>::altair_only() + .run(); + SszStaticHandler::, MainnetEthSpec>::altair_only() + .run(); + SszStaticHandler::, MinimalEthSpec>::merge_only() + .run(); + SszStaticHandler::, MainnetEthSpec>::merge_only() + .run(); + SszStaticHandler::, MinimalEthSpec>::capella_only( + ) + .run(); + SszStaticHandler::, MainnetEthSpec>::capella_only( + ) + .run(); + SszStaticHandler::, MinimalEthSpec>::deneb_only() + .run(); + SszStaticHandler::, MainnetEthSpec>::deneb_only() + .run(); + } + #[test] fn signed_contribution_and_proof() { SszStaticHandler::, MinimalEthSpec>::altair_and_later().run(); diff --git a/testing/simulator/src/checks.rs b/testing/simulator/src/checks.rs index f38eacc394a..d30e44a1174 100644 --- a/testing/simulator/src/checks.rs +++ b/testing/simulator/src/checks.rs @@ -287,13 +287,13 @@ pub(crate) async fn verify_light_client_updates( } // Verify light client optimistic update. `signature_slot_distance` should be 1 in the ideal scenario. - let signature_slot = client + let signature_slot = *client .get_beacon_light_client_optimistic_update::() .await .map_err(|e| format!("Error while getting light client updates: {:?}", e))? .ok_or(format!("Light client optimistic update not found {slot:?}"))? .data - .signature_slot; + .signature_slot(); let signature_slot_distance = slot - signature_slot; if signature_slot_distance > light_client_update_slot_tolerance { return Err(format!("Existing optimistic update too old: signature slot {signature_slot}, current slot {slot:?}")); @@ -316,13 +316,13 @@ pub(crate) async fn verify_light_client_updates( } continue; } - let signature_slot = client + let signature_slot = *client .get_beacon_light_client_finality_update::() .await .map_err(|e| format!("Error while getting light client updates: {:?}", e))? .ok_or(format!("Light client finality update not found {slot:?}"))? .data - .signature_slot; + .signature_slot(); let signature_slot_distance = slot - signature_slot; if signature_slot_distance > light_client_update_slot_tolerance { return Err(format!(