Skip to content

Commit

Permalink
Merge branch 'feature/update-eth2-libp2p-unstable-october' into pectr…
Browse files Browse the repository at this point in the history
…a-devnet4
  • Loading branch information
Tumas committed Nov 6, 2024
2 parents bd507cb + 17ad54d commit af7c135
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 18 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,7 @@ git-version = '0.3'
glob = '0.3'
good_lp = { version = '1', default-features = false, features = ['highs'] }
hash_hasher = '2'
hashlink = '0.9'
hex = { version = '0.4', features = ['serde'] }
hex-literal = '0.4'
hex_fmt = '0.3'
Expand Down
2 changes: 1 addition & 1 deletion fork_choice_control/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ impl<P: Preset> Context<P> {
}

pub fn on_blob_sidecar(&mut self, blob_sidecar: BlobSidecar<P>) -> Option<P2pMessage<P>> {
let subnet_id = misc::compute_subnet_for_blob_sidecar(blob_sidecar.index);
let subnet_id = misc::compute_subnet_for_blob_sidecar(self.config(), blob_sidecar.index);

self.controller().on_gossip_blob_sidecar(
Arc::new(blob_sidecar),
Expand Down
3 changes: 2 additions & 1 deletion fork_choice_store/src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1722,7 +1722,8 @@ impl<P: Preset> Store<P> {

// [REJECT] The sidecar is for the correct subnet -- i.e. compute_subnet_for_blob_sidecar(blob_sidecar.index) == subnet_id.
if let Some(actual) = origin.subnet_id() {
let expected = misc::compute_subnet_for_blob_sidecar(blob_sidecar.index);
let expected =
misc::compute_subnet_for_blob_sidecar(&self.chain_config, blob_sidecar.index);

ensure!(
actual == expected,
Expand Down
6 changes: 3 additions & 3 deletions helper_functions/src/misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use types::{
combined::{Attestation, SignedBeaconBlock},
config::Config,
deneb::{
consts::{BlobCommitmentTreeDepth, BlobSidecarSubnetCount, VERSIONED_HASH_VERSION_KZG},
consts::{BlobCommitmentTreeDepth, VERSIONED_HASH_VERSION_KZG},
containers::BlobSidecar,
primitives::{
Blob, BlobCommitmentInclusionProof, BlobIndex, KzgCommitment, KzgProof, VersionedHash,
Expand Down Expand Up @@ -204,8 +204,8 @@ pub fn compute_subnet_for_attestation<P: Preset>(

/// [`compute_subnet_for_blob_sidecar`](https://github.com/ethereum/consensus-specs/blob/v1.4.0-beta.1/specs/deneb/validator.md#sidecar)
#[must_use]
pub fn compute_subnet_for_blob_sidecar(blob_index: BlobIndex) -> SubnetId {
blob_index.mod_typenum::<BlobSidecarSubnetCount>()
pub const fn compute_subnet_for_blob_sidecar(config: &Config, blob_index: BlobIndex) -> SubnetId {
blob_index % config.blob_sidecar_subnet_count
}

/// <https://github.com/ethereum/consensus-specs/blob/v1.1.0/specs/altair/validator.md#broadcast-sync-committee-message>
Expand Down
33 changes: 25 additions & 8 deletions p2p/src/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,11 @@ impl<P: Preset> Network<P> {
}

fn publish_blob_sidecar(&self, blob_sidecar: Arc<BlobSidecar<P>>) {
let subnet_id = misc::compute_subnet_for_blob_sidecar(blob_sidecar.index);
let subnet_id = misc::compute_subnet_for_blob_sidecar(
self.controller.chain_config(),
blob_sidecar.index,
);

let blob_identifier: BlobIdentifier = blob_sidecar.as_ref().into();

debug!("publishing blob sidecar: {blob_identifier:?}, subnet_id: {subnet_id}");
Expand Down Expand Up @@ -843,22 +847,32 @@ impl<P: Preset> Network<P> {
self.handle_blocks_by_root_request(peer_id, peer_request_id, request_id, request);
Ok(())
}
RequestType::DataColumnsByRange(_) => {
debug!("received DataColumnsByRange request (peer_id: {peer_id})");
Ok(())
}
RequestType::DataColumnsByRoot(_) => {
debug!("received DataColumnsByRoot request (peer_id: {peer_id})");
Ok(())
}
RequestType::LightClientBootstrap(_) => {
// TODO(Altair Light Client Sync Protocol)
debug!("received LightClientBootstrap request (peer_id: {peer_id})");

Ok(())
}
RequestType::LightClientFinalityUpdate => {
// TODO(Altair Light Client Sync Protocol)
debug!("received LightClientFinalityUpdate request (peer_id: {peer_id})");

Ok(())
}
RequestType::LightClientOptimisticUpdate => {
// TODO(Altair Light Client Sync Protocol)
debug!("received LightClientOptimisticUpdate request (peer_id: {peer_id})");

Ok(())
}
RequestType::LightClientUpdatesByRange(_) => {
// TODO(Altair Light Client Sync Protocol)
debug!("received LightClientUpdatesByRange request (peer_id: {peer_id})");
Ok(())
}
RequestType::BlobsByRange(request) => {
Expand Down Expand Up @@ -1290,6 +1304,7 @@ impl<P: Preset> Network<P> {
request_id: {request_id}",
);
}
Response::DataColumnsByRange(_) | Response::DataColumnsByRoot(_) => {}
Response::LightClientBootstrap(_) => {
// TODO(Altair Light Client Sync Protocol)
debug!("received LightClientBootstrap response chunk (peer_id: {peer_id})");
Expand All @@ -1302,6 +1317,10 @@ impl<P: Preset> Network<P> {
// TODO(Altair Light Client Sync Protocol)
debug!("received LightClientOptimisticUpdate response (peer_id: {peer_id})");
}
Response::LightClientUpdatesByRange(_) => {
// TODO(Altair Light Client Sync Protocol)
debug!("received LightClientUpdatesByRange response (peer_id: {peer_id})");
}
}
}

Expand Down Expand Up @@ -1372,9 +1391,7 @@ impl<P: Preset> Network<P> {
block_seen,
);
}
PubsubMessage::DataColumnSidecar(_) => {
// TODO
}
PubsubMessage::DataColumnSidecar(_) => {}
PubsubMessage::AggregateAndProofAttestation(aggregate_and_proof) => {
if let Some(metrics) = self.metrics.as_ref() {
metrics.register_gossip_object(&["aggregate_and_proof_attestation"]);
Expand Down Expand Up @@ -1725,7 +1742,7 @@ impl<P: Preset> Network<P> {

let current_phase = self.fork_context.current_fork();

for kind in core_topics_to_subscribe(current_phase)
for kind in core_topics_to_subscribe(self.controller.chain_config(), current_phase)
.iter()
.filter(|kind| !subscribed_topics.contains(kind))
.cloned()
Expand Down
72 changes: 72 additions & 0 deletions types/src/combined.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use crate::{
BeaconBlock as AltairBeaconBlock, LightClientBootstrap as AltairLightClientBootstrap,
LightClientFinalityUpdate as AltairLightClientFinalityUpdate,
LightClientOptimisticUpdate as AltairLightClientOptimisticUpdate,
LightClientUpdate as AltairLightClientUpdate,
SignedBeaconBlock as AltairSignedBeaconBlock,
},
},
Expand All @@ -41,6 +42,7 @@ use crate::{
LightClientBootstrap as CapellaLightClientBootstrap,
LightClientFinalityUpdate as CapellaLightClientFinalityUpdate,
LightClientOptimisticUpdate as CapellaLightClientOptimisticUpdate,
LightClientUpdate as CapellaLightClientUpdate,
SignedBeaconBlock as CapellaSignedBeaconBlock,
SignedBlindedBeaconBlock as CapellaSignedBlindedBeaconBlock,
},
Expand All @@ -55,6 +57,7 @@ use crate::{
LightClientBootstrap as DenebLightClientBootstrap,
LightClientFinalityUpdate as DenebLightClientFinalityUpdate,
LightClientOptimisticUpdate as DenebLightClientOptimisticUpdate,
LightClientUpdate as DenebLightClientUpdate,
SignedBeaconBlock as DenebSignedBeaconBlock,
SignedBlindedBeaconBlock as DenebSignedBlindedBeaconBlock,
},
Expand All @@ -69,6 +72,7 @@ use crate::{
LightClientBootstrap as ElectraLightClientBootstrap,
LightClientFinalityUpdate as ElectraLightClientFinalityUpdate,
LightClientOptimisticUpdate as ElectraLightClientOptimisticUpdate,
LightClientUpdate as ElectraLightClientUpdate,
SignedAggregateAndProof as ElectraSignedAggregateAndProof,
SignedBeaconBlock as ElectraSignedBeaconBlock,
SignedBlindedBeaconBlock as ElectraSignedBlindedBeaconBlock,
Expand Down Expand Up @@ -1214,6 +1218,64 @@ impl<P: Preset> SszWrite for LightClientOptimisticUpdate<P> {
}
}

#[derive(Clone, PartialEq, Eq, Debug)]
pub enum LightClientUpdate<P: Preset> {
// Boxed to pass `clippy::large_enum_variant`.
Altair(Box<AltairLightClientUpdate<P>>),
Capella(Box<CapellaLightClientUpdate<P>>),
Deneb(Box<DenebLightClientUpdate<P>>),
Electra(Box<ElectraLightClientUpdate<P>>),
}

// It is difficult to implement `SszRead` for the combined `LightClientUpdate`.
// `attested_header` starts with a slot, but `LightClientHeader` becomes variable-size in Capella.
// It is possible to distinguish variants of `LightClientUpdate` by length as of Capella,
// but it becomes more difficult in Deneb and may become impossible in later phases.
assert_not_impl_any!(LightClientUpdate<Mainnet>: SszRead<Config>);

impl<P: Preset> SszSize for LightClientUpdate<P> {
// The const parameter should be `Self::VARIANT_COUNT`, but `Self` refers to a generic type.
// Type parameters cannot be used in `const` contexts until `generic_const_exprs` is stable.
const SIZE: Size = Size::for_untagged_union::<{ Phase::CARDINALITY - 2 }>([
AltairLightClientUpdate::<P>::SIZE,
CapellaLightClientUpdate::<P>::SIZE,
DenebLightClientUpdate::<P>::SIZE,
ElectraLightClientUpdate::<P>::SIZE,
]);
}

impl<P: Preset> SszWrite for LightClientUpdate<P> {
fn write_variable(&self, bytes: &mut Vec<u8>) -> Result<(), WriteError> {
match self {
Self::Altair(update) => {
let size = AltairLightClientUpdate::<P>::SIZE.get();
let length_before = bytes.len();
let length_after = length_before + size;

bytes.resize(length_after, 0);
update.write_fixed(&mut bytes[length_before..]);

Ok(())
}
Self::Capella(update) => update.write_variable(bytes),
Self::Deneb(update) => update.write_variable(bytes),
Self::Electra(update) => update.write_variable(bytes),
}
}
}

impl<P: Preset> LightClientUpdate<P> {
#[must_use]
pub fn signature_slot(&self) -> Slot {
match self {
Self::Altair(update) => update.signature_slot,
Self::Capella(update) => update.signature_slot,
Self::Deneb(update) => update.signature_slot,
Self::Electra(update) => update.signature_slot,
}
}
}

#[derive(Clone, PartialEq, Eq, Debug, From, Deserialize, Serialize)]
#[serde(bound = "", untagged)]
pub enum AggregateAndProof<P: Preset> {
Expand Down Expand Up @@ -1591,30 +1653,40 @@ mod spec_tests {
["consensus-spec-tests/tests/minimal/altair/ssz_static/LightClientFinalityUpdate/*/*"] [altair_minimal_finality_update] [LightClientFinalityUpdate] [Minimal] [Altair];
["consensus-spec-tests/tests/mainnet/altair/ssz_static/LightClientOptimisticUpdate/*/*"] [altair_mainnet_optimistic_update] [LightClientOptimisticUpdate] [Mainnet] [Altair];
["consensus-spec-tests/tests/minimal/altair/ssz_static/LightClientOptimisticUpdate/*/*"] [altair_minimal_optimistic_update] [LightClientOptimisticUpdate] [Minimal] [Altair];
["consensus-spec-tests/tests/mainnet/altair/ssz_static/LightClientUpdate/*/*"] [altair_mainnet_update] [LightClientUpdate] [Mainnet] [Altair];
["consensus-spec-tests/tests/minimal/altair/ssz_static/LightClientUpdate/*/*"] [altair_minimal_update] [LightClientUpdate] [Minimal] [Altair];
["consensus-spec-tests/tests/mainnet/bellatrix/ssz_static/LightClientBootstrap/*/*"] [bellatrix_mainnet_bootstrap] [LightClientBootstrap] [Mainnet] [Altair];
["consensus-spec-tests/tests/minimal/bellatrix/ssz_static/LightClientBootstrap/*/*"] [bellatrix_minimal_bootstrap] [LightClientBootstrap] [Minimal] [Altair];
["consensus-spec-tests/tests/mainnet/bellatrix/ssz_static/LightClientFinalityUpdate/*/*"] [bellatrix_mainnet_finality_update] [LightClientFinalityUpdate] [Mainnet] [Altair];
["consensus-spec-tests/tests/minimal/bellatrix/ssz_static/LightClientFinalityUpdate/*/*"] [bellatrix_minimal_finality_update] [LightClientFinalityUpdate] [Minimal] [Altair];
["consensus-spec-tests/tests/mainnet/bellatrix/ssz_static/LightClientOptimisticUpdate/*/*"] [bellatrix_mainnet_optimistic_update] [LightClientOptimisticUpdate] [Mainnet] [Altair];
["consensus-spec-tests/tests/minimal/bellatrix/ssz_static/LightClientOptimisticUpdate/*/*"] [bellatrix_minimal_optimistic_update] [LightClientOptimisticUpdate] [Minimal] [Altair];
["consensus-spec-tests/tests/mainnet/bellatrix/ssz_static/LightClientUpdate/*/*"] [bellatrix_mainnet_update] [LightClientUpdate] [Mainnet] [Altair];
["consensus-spec-tests/tests/minimal/bellatrix/ssz_static/LightClientUpdate/*/*"] [bellatrix_minimal_update] [LightClientUpdate] [Minimal] [Altair];
["consensus-spec-tests/tests/mainnet/capella/ssz_static/LightClientBootstrap/*/*"] [capella_mainnet_bootstrap] [LightClientBootstrap] [Mainnet] [Capella];
["consensus-spec-tests/tests/minimal/capella/ssz_static/LightClientBootstrap/*/*"] [capella_minimal_bootstrap] [LightClientBootstrap] [Minimal] [Capella];
["consensus-spec-tests/tests/mainnet/capella/ssz_static/LightClientFinalityUpdate/*/*"] [capella_mainnet_finality_update] [LightClientFinalityUpdate] [Mainnet] [Capella];
["consensus-spec-tests/tests/minimal/capella/ssz_static/LightClientFinalityUpdate/*/*"] [capella_minimal_finality_update] [LightClientFinalityUpdate] [Minimal] [Capella];
["consensus-spec-tests/tests/mainnet/capella/ssz_static/LightClientOptimisticUpdate/*/*"] [capella_mainnet_optimistic_update] [LightClientOptimisticUpdate] [Mainnet] [Capella];
["consensus-spec-tests/tests/minimal/capella/ssz_static/LightClientOptimisticUpdate/*/*"] [capella_minimal_optimistic_update] [LightClientOptimisticUpdate] [Minimal] [Capella];
["consensus-spec-tests/tests/mainnet/capella/ssz_static/LightClientUpdate/*/*"] [capella_mainnet_update] [LightClientUpdate] [Mainnet] [Capella];
["consensus-spec-tests/tests/minimal/capella/ssz_static/LightClientUpdate/*/*"] [capella_minimal_update] [LightClientUpdate] [Minimal] [Capella];
["consensus-spec-tests/tests/mainnet/deneb/ssz_static/LightClientBootstrap/*/*"] [deneb_mainnet_bootstrap] [LightClientBootstrap] [Mainnet] [Deneb];
["consensus-spec-tests/tests/minimal/deneb/ssz_static/LightClientBootstrap/*/*"] [deneb_minimal_bootstrap] [LightClientBootstrap] [Minimal] [Deneb];
["consensus-spec-tests/tests/mainnet/deneb/ssz_static/LightClientFinalityUpdate/*/*"] [deneb_mainnet_finality_update] [LightClientFinalityUpdate] [Mainnet] [Deneb];
["consensus-spec-tests/tests/minimal/deneb/ssz_static/LightClientFinalityUpdate/*/*"] [deneb_minimal_finality_update] [LightClientFinalityUpdate] [Minimal] [Deneb];
["consensus-spec-tests/tests/mainnet/deneb/ssz_static/LightClientOptimisticUpdate/*/*"] [deneb_mainnet_optimistic_update] [LightClientOptimisticUpdate] [Mainnet] [Deneb];
["consensus-spec-tests/tests/minimal/deneb/ssz_static/LightClientOptimisticUpdate/*/*"] [deneb_minimal_optimistic_update] [LightClientOptimisticUpdate] [Minimal] [Deneb];
["consensus-spec-tests/tests/mainnet/deneb/ssz_static/LightClientUpdate/*/*"] [deneb_mainnet_update] [LightClientUpdate] [Mainnet] [Deneb];
["consensus-spec-tests/tests/minimal/deneb/ssz_static/LightClientUpdate/*/*"] [deneb_minimal_update] [LightClientUpdate] [Minimal] [Deneb];
["consensus-spec-tests/tests/mainnet/electra/ssz_static/LightClientBootstrap/*/*"] [electra_mainnet_bootstrap] [LightClientBootstrap] [Mainnet] [Electra];
["consensus-spec-tests/tests/minimal/electra/ssz_static/LightClientBootstrap/*/*"] [electra_minimal_bootstrap] [LightClientBootstrap] [Minimal] [Electra];
["consensus-spec-tests/tests/mainnet/electra/ssz_static/LightClientFinalityUpdate/*/*"] [electra_mainnet_finality_update] [LightClientFinalityUpdate] [Mainnet] [Electra];
["consensus-spec-tests/tests/minimal/electra/ssz_static/LightClientFinalityUpdate/*/*"] [electra_minimal_finality_update] [LightClientFinalityUpdate] [Minimal] [Electra];
["consensus-spec-tests/tests/mainnet/electra/ssz_static/LightClientOptimisticUpdate/*/*"] [electra_mainnet_optimistic_update] [LightClientOptimisticUpdate] [Mainnet] [Electra];
["consensus-spec-tests/tests/minimal/electra/ssz_static/LightClientOptimisticUpdate/*/*"] [electra_minimal_optimistic_update] [LightClientOptimisticUpdate] [Minimal] [Electra];
["consensus-spec-tests/tests/mainnet/electra/ssz_static/LightClientUpdate/*/*"] [electra_mainnet_update] [LightClientUpdate] [Mainnet] [Electra];
["consensus-spec-tests/tests/minimal/electra/ssz_static/LightClientUpdate/*/*"] [electra_minimal_update] [LightClientUpdate] [Minimal] [Electra];
)]
#[test_resources(glob)]
fn function_name(case: Case) {
Expand Down
5 changes: 1 addition & 4 deletions types/src/deneb/consts.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
use hex_literal::hex;
use ssz::MerkleElements;
use typenum::{U32, U6};
use typenum::U32;

use crate::{deneb::primitives::KzgCommitment, preset::Preset};

pub const VERSIONED_HASH_VERSION_KZG: &[u8] = &hex!("01");

// TODO(feature/deneb): Can `BlobSidecarSubnetCount` be a `const`?
// It's never used as a type even in `eth2_libp2p`.
pub type BlobSidecarSubnetCount = U6;
pub type BytesPerFieldElement = U32;

pub type BlobCommitmentTreeDepth<P> = <
Expand Down

0 comments on commit af7c135

Please sign in to comment.