Skip to content
9 changes: 9 additions & 0 deletions beacon_node/beacon_chain/src/beacon_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6804,6 +6804,15 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
.enr_fork_id::<T::EthSpec>(slot, self.genesis_validators_root)
}

/// Returns the fork_digest corresponding to an epoch.
/// See [`ChainSpec::compute_fork_digest`]
pub fn compute_fork_digest(&self, epoch: Epoch) -> [u8; 4] {
self.spec
.compute_fork_digest(self.genesis_validators_root, epoch)
}

/// Calculates the `Duration` to the next fork digest (this could be either a regular or BPO
/// hard fork) if it exists and returns it with its corresponding `Epoch`.
pub fn duration_to_next_digest(&self) -> Option<(Epoch, Duration)> {
// If we are unable to read the slot clock we assume that it is prior to genesis and
// therefore use the genesis slot.
Expand Down
10 changes: 4 additions & 6 deletions beacon_node/http_api/src/light_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,12 +150,10 @@ fn map_light_client_update_to_ssz_chunk<T: BeaconChainTypes>(
chain: &BeaconChain<T>,
light_client_update: &LightClientUpdate<T::EthSpec>,
) -> LightClientUpdateResponseChunk {
let fork_digest = chain.spec.compute_fork_digest(
chain.genesis_validators_root,
light_client_update
.attested_header_slot()
.epoch(T::EthSpec::slots_per_epoch()),
);
let epoch = light_client_update
.attested_header_slot()
.epoch(T::EthSpec::slots_per_epoch());
let fork_digest = chain.compute_fork_digest(epoch);

let payload = light_client_update.as_ssz_bytes();
let response_chunk_len = fork_digest.len() + payload.len();
Expand Down
2 changes: 1 addition & 1 deletion beacon_node/lighthouse_network/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ pub fn gossipsub_config(
) -> Vec<u8> {
let topic_bytes = message.topic.as_str().as_bytes();

if fork_context.current_fork().altair_enabled() {
if fork_context.current_fork_name().altair_enabled() {
let topic_len_bytes = topic_bytes.len().to_le_bytes();
let mut vec = Vec::with_capacity(
prefix.len() + topic_len_bytes.len() + topic_bytes.len() + message.data.len(),
Expand Down
31 changes: 16 additions & 15 deletions beacon_node/lighthouse_network/src/discovery/enr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
pub use discv5::enr::CombinedKey;

use super::enr_ext::CombinedKeyExt;
use super::enr_ext::{EnrExt, QUIC6_ENR_KEY, QUIC_ENR_KEY};
use super::ENR_FILENAME;
use crate::types::{Enr, EnrAttestationBitfield, EnrSyncCommitteeBitfield};
use crate::NetworkConfig;
Expand All @@ -18,8 +19,6 @@ use std::str::FromStr;
use tracing::{debug, warn};
use types::{ChainSpec, EnrForkId, EthSpec};

use super::enr_ext::{EnrExt, QUIC6_ENR_KEY, QUIC_ENR_KEY};

/// The ENR field specifying the fork id.
pub const ETH2_ENR_KEY: &str = "eth2";
/// The ENR field specifying the next fork digest.
Expand Down Expand Up @@ -87,8 +86,7 @@ impl Eth2Enr for Enr {
}

fn next_fork_digest(&self) -> Result<[u8; 4], &'static str> {
self
.get_decodable::<[u8; 4]>(NEXT_FORK_DIGEST_ENR_KEY)
self.get_decodable::<[u8; 4]>(NEXT_FORK_DIGEST_ENR_KEY)
.ok_or("ENR next fork digest non-existent")?
.map_err(|_| "Could not decode the ENR next fork digest")
}
Expand Down Expand Up @@ -161,14 +159,14 @@ pub fn build_or_load_enr<E: EthSpec>(
local_key: Keypair,
config: &NetworkConfig,
enr_fork_id: &EnrForkId,
next_fork_digest: [u8; 4],
spec: &ChainSpec,
nfd: [u8; 4],
) -> Result<Enr, String> {
// Build the local ENR.
// Note: Discovery should update the ENR record's IP to the external IP as seen by the
// majority of our peers, if the CLI doesn't expressly forbid it.
let enr_key = CombinedKey::from_libp2p(local_key)?;
let mut local_enr = build_enr::<E>(&enr_key, config, enr_fork_id, spec, nfd)?;
let mut local_enr = build_enr::<E>(&enr_key, config, enr_fork_id, next_fork_digest, spec)?;

use_or_load_enr(&enr_key, &mut local_enr, config)?;
Ok(local_enr)
Expand All @@ -179,8 +177,8 @@ pub fn build_enr<E: EthSpec>(
enr_key: &CombinedKey,
config: &NetworkConfig,
enr_fork_id: &EnrForkId,
next_fork_digest: [u8; 4],
spec: &ChainSpec,
nfd: [u8; 4],
) -> Result<Enr, String> {
let mut builder = discv5::enr::Enr::builder();
let (maybe_ipv4_address, maybe_ipv6_address) = &config.enr_address;
Expand Down Expand Up @@ -271,7 +269,7 @@ pub fn build_enr<E: EthSpec>(
&bitfield.as_ssz_bytes().into(),
);

// only set `cgc` if PeerDAS fork epoch has been scheduled
// only set `cgc` and `nfd` if PeerDAS fork (Fulu) epoch has been scheduled
if spec.is_peer_das_scheduled() {
let custody_group_count =
if let Some(false_cgc) = config.advertise_false_custody_group_count {
Expand All @@ -282,11 +280,7 @@ pub fn build_enr<E: EthSpec>(
spec.custody_requirement
};
builder.add_value(PEERDAS_CUSTODY_GROUP_COUNT_ENR_KEY, &custody_group_count);
}

// only set `nfd` if peer das is scheduled
if spec.is_peer_das_scheduled() {
builder.add_value(NEXT_FORK_DIGEST_ENR_KEY, &nfd);
builder.add_value(NEXT_FORK_DIGEST_ENR_KEY, &next_fork_digest);
}

builder
Expand Down Expand Up @@ -359,6 +353,7 @@ mod test {
use types::{Epoch, MainnetEthSpec};

type E = MainnetEthSpec;
const TEST_NFD: [u8; 4] = [0x01, 0x02, 0x03, 0x04];

fn make_fulu_spec() -> ChainSpec {
let mut spec = E::default_spec();
Expand All @@ -370,11 +365,17 @@ mod test {
let keypair = libp2p::identity::secp256k1::Keypair::generate();
let enr_key = CombinedKey::from_secp256k1(&keypair);
let enr_fork_id = EnrForkId::default();
let nfd = [0; 4]; // placeholder
let enr = build_enr::<E>(&enr_key, &config, &enr_fork_id, spec, nfd).unwrap();
let enr = build_enr::<E>(&enr_key, &config, &enr_fork_id, TEST_NFD, spec).unwrap();
(enr, enr_key)
}

#[test]
fn test_nfd_enr_encoding() {
let spec = make_fulu_spec();
let enr = build_enr_with_config(NetworkConfig::default(), &spec).0;
assert_eq!(enr.next_fork_digest().unwrap(), TEST_NFD);
}

#[test]
fn custody_group_count_default() {
let config = NetworkConfig {
Expand Down
12 changes: 9 additions & 3 deletions beacon_node/lighthouse_network/src/discovery/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1230,9 +1230,15 @@ mod tests {
config.set_listening_addr(crate::ListenAddress::unused_v4_ports());
let config = Arc::new(config);
let enr_key: CombinedKey = CombinedKey::from_secp256k1(&keypair);
let nfd = [0; 4]; // placeholder
let enr: Enr =
build_enr::<E>(&enr_key, &config, &EnrForkId::default(), &spec, nfd).unwrap();
let next_fork_digest = [0; 4];
let enr: Enr = build_enr::<E>(
&enr_key,
&config,
&EnrForkId::default(),
next_fork_digest,
&spec,
)
.unwrap();
let globals = NetworkGlobals::new(
enr,
MetaData::V2(MetaDataV2 {
Expand Down
Loading
Loading