Skip to content

Commit

Permalink
Add {fork_name}_enabled functions (#5951)
Browse files Browse the repository at this point in the history
* add fork_name_enabled fn to Forkname impl

* refactor codebase to use new fork_enabled fn

* fmt

* Merge branch 'unstable' of https://github.com/sigp/lighthouse into fork-ord-impl

* small code cleanup

* resolve merge conflicts

* fix beacon chain test

* merge conflicts

* fix ef test issue

* resolve merge conflicts
  • Loading branch information
eserilev authored Oct 3, 2024
1 parent dd08ebb commit 82faf97
Show file tree
Hide file tree
Showing 22 changed files with 223 additions and 251 deletions.
9 changes: 5 additions & 4 deletions beacon_node/beacon_chain/src/attestation_verification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1144,13 +1144,14 @@ pub fn verify_propagation_slot_range<S: SlotClock, E: EthSpec>(

let current_fork =
spec.fork_name_at_slot::<E>(slot_clock.now().ok_or(BeaconChainError::UnableToReadSlot)?);
let earliest_permissible_slot = if !current_fork.deneb_enabled() {
one_epoch_prior
// EIP-7045
} else {

let earliest_permissible_slot = if current_fork.deneb_enabled() {
// EIP-7045
one_epoch_prior
.epoch(E::slots_per_epoch())
.start_slot(E::slots_per_epoch())
} else {
one_epoch_prior
};

if attestation_slot < earliest_permissible_slot {
Expand Down
87 changes: 40 additions & 47 deletions beacon_node/beacon_chain/src/beacon_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2619,11 +2619,7 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
/// Check if the current slot is greater than or equal to the Capella fork epoch.
pub fn current_slot_is_post_capella(&self) -> Result<bool, Error> {
let current_fork = self.spec.fork_name_at_slot::<T::EthSpec>(self.slot()?);
if let ForkName::Base | ForkName::Altair | ForkName::Bellatrix = current_fork {
Ok(false)
} else {
Ok(true)
}
Ok(current_fork.capella_enabled())
}

/// Import a BLS to execution change to the op pool.
Expand Down Expand Up @@ -5945,26 +5941,23 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
payload_attributes
} else {
let prepare_slot_fork = self.spec.fork_name_at_slot::<T::EthSpec>(prepare_slot);
let withdrawals = match prepare_slot_fork {
ForkName::Base | ForkName::Altair | ForkName::Bellatrix => None,
ForkName::Capella | ForkName::Deneb | ForkName::Electra => {
let chain = self.clone();
self.spawn_blocking_handle(
move || {
chain.get_expected_withdrawals(&forkchoice_update_params, prepare_slot)
},
"prepare_beacon_proposer_withdrawals",
)
.await?
.map(Some)?
}

let withdrawals = if prepare_slot_fork.capella_enabled() {
let chain = self.clone();
self.spawn_blocking_handle(
move || chain.get_expected_withdrawals(&forkchoice_update_params, prepare_slot),
"prepare_beacon_proposer_withdrawals",
)
.await?
.map(Some)?
} else {
None
};

let parent_beacon_block_root = match prepare_slot_fork {
ForkName::Base | ForkName::Altair | ForkName::Bellatrix | ForkName::Capella => None,
ForkName::Deneb | ForkName::Electra => {
Some(pre_payload_attributes.parent_beacon_block_root)
}
let parent_beacon_block_root = if prepare_slot_fork.deneb_enabled() {
Some(pre_payload_attributes.parent_beacon_block_root)
} else {
None
};

let payload_attributes = PayloadAttributes::new(
Expand Down Expand Up @@ -6110,27 +6103,27 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
// `execution_engine_forkchoice_lock` apart from the one here.
let forkchoice_lock = execution_layer.execution_engine_forkchoice_lock().await;

let (head_block_root, head_hash, justified_hash, finalized_hash) = if let Some(head_hash) =
params.head_hash
{
(
params.head_root,
head_hash,
params
.justified_hash
.unwrap_or_else(ExecutionBlockHash::zero),
params
.finalized_hash
.unwrap_or_else(ExecutionBlockHash::zero),
)
} else {
// The head block does not have an execution block hash. We must check to see if we
// happen to be the proposer of the transition block, in which case we still need to
// send forkchoice_updated.
match self.spec.fork_name_at_slot::<T::EthSpec>(next_slot) {
// We are pre-bellatrix; no need to update the EL.
ForkName::Base | ForkName::Altair => return Ok(()),
_ => {
let (head_block_root, head_hash, justified_hash, finalized_hash) =
if let Some(head_hash) = params.head_hash {
(
params.head_root,
head_hash,
params
.justified_hash
.unwrap_or_else(ExecutionBlockHash::zero),
params
.finalized_hash
.unwrap_or_else(ExecutionBlockHash::zero),
)
} else {
// The head block does not have an execution block hash. We must check to see if we
// happen to be the proposer of the transition block, in which case we still need to
// send forkchoice_updated.
if self
.spec
.fork_name_at_slot::<T::EthSpec>(next_slot)
.bellatrix_enabled()
{
// We are post-bellatrix
if let Some(payload_attributes) = execution_layer
.payload_attributes(next_slot, params.head_root)
Expand Down Expand Up @@ -6164,9 +6157,10 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
// We are not a proposer, no need to update the EL.
return Ok(());
}
} else {
return Ok(());
}
}
};
};

let forkchoice_updated_response = execution_layer
.notify_forkchoice_updated(
Expand Down Expand Up @@ -7009,7 +7003,6 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
.finalized_checkpoint()
.epoch
.sync_committee_period(&self.spec)?;

self.light_client_server_cache.get_light_client_bootstrap(
&self.store,
block_root,
Expand Down
30 changes: 16 additions & 14 deletions beacon_node/beacon_chain/tests/attestation_verification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -359,22 +359,24 @@ impl GossipTester {
}

pub fn earliest_valid_attestation_slot(&self) -> Slot {
let offset = match self.harness.spec.fork_name_at_epoch(self.epoch()) {
ForkName::Base | ForkName::Altair | ForkName::Bellatrix | ForkName::Capella => {
// Subtract an additional slot since the harness will be exactly on the start of the
// slot and the propagation tolerance will allow an extra slot.
E::slots_per_epoch() + 1
}
let offset = if self
.harness
.spec
.fork_name_at_epoch(self.epoch())
.deneb_enabled()
{
// EIP-7045
ForkName::Deneb | ForkName::Electra => {
let epoch_slot_offset = (self.slot() % E::slots_per_epoch()).as_u64();
if epoch_slot_offset != 0 {
E::slots_per_epoch() + epoch_slot_offset
} else {
// Here the propagation tolerance will cause the cutoff to be an entire epoch earlier
2 * E::slots_per_epoch()
}
let epoch_slot_offset = (self.slot() % E::slots_per_epoch()).as_u64();
if epoch_slot_offset != 0 {
E::slots_per_epoch() + epoch_slot_offset
} else {
// Here the propagation tolerance will cause the cutoff to be an entire epoch earlier
2 * E::slots_per_epoch()
}
} else {
// Subtract an additional slot since the harness will be exactly on the start of the
// slot and the propagation tolerance will allow an extra slot.
E::slots_per_epoch() + 1
};

self.slot()
Expand Down
4 changes: 2 additions & 2 deletions beacon_node/client/src/notifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ async fn capella_readiness_logging<T: BeaconChainTypes>(
.snapshot
.beacon_state
.fork_name_unchecked()
>= ForkName::Capella;
.capella_enabled();

let has_execution_layer = beacon_chain.execution_layer.is_some();

Expand Down Expand Up @@ -496,7 +496,7 @@ async fn deneb_readiness_logging<T: BeaconChainTypes>(
.snapshot
.beacon_state
.fork_name_unchecked()
>= ForkName::Deneb;
.deneb_enabled();

let has_execution_layer = beacon_chain.execution_layer.is_some();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -661,21 +661,18 @@ impl<E: EthSpec> ExecutionBlockGenerator<E> {
},
};

match execution_payload.fork_name() {
ForkName::Base | ForkName::Altair | ForkName::Bellatrix | ForkName::Capella => {}
ForkName::Deneb | ForkName::Electra => {
// get random number between 0 and Max Blobs
let mut rng = self.rng.lock();
let num_blobs = rng.gen::<usize>() % (E::max_blobs_per_block() + 1);
let (bundle, transactions) = generate_blobs(num_blobs)?;
for tx in Vec::from(transactions) {
execution_payload
.transactions_mut()
.push(tx)
.map_err(|_| "transactions are full".to_string())?;
}
self.blobs_bundles.insert(id, bundle);
if execution_payload.fork_name().deneb_enabled() {
// get random number between 0 and Max Blobs
let mut rng = self.rng.lock();
let num_blobs = rng.gen::<usize>() % (E::max_blobs_per_block() + 1);
let (bundle, transactions) = generate_blobs(num_blobs)?;
for tx in Vec::from(transactions) {
execution_payload
.transactions_mut()
.push(tx)
.map_err(|_| "transactions are full".to_string())?;
}
self.blobs_bundles.insert(id, bundle);
}

*execution_payload.block_hash_mut() =
Expand Down
10 changes: 6 additions & 4 deletions beacon_node/execution_layer/src/test_utils/mock_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -479,16 +479,18 @@ pub fn serve<E: EthSpec>(
let prev_randao = head_state
.get_randao_mix(head_state.current_epoch())
.map_err(|_| reject("couldn't get prev randao"))?;
let expected_withdrawals = match fork {
ForkName::Base | ForkName::Altair | ForkName::Bellatrix => None,
ForkName::Capella | ForkName::Deneb | ForkName::Electra => Some(

let expected_withdrawals = if fork.capella_enabled() {
Some(
builder
.beacon_client
.get_expected_withdrawals(&StateId::Head)
.await
.unwrap()
.data,
),
)
} else {
None
};

let payload_attributes = match fork {
Expand Down
14 changes: 8 additions & 6 deletions beacon_node/http_api/src/build_block_contents.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,9 @@ pub fn build_block_contents<E: EthSpec>(
BeaconBlockResponseWrapper::Blinded(block) => {
Ok(ProduceBlockV3Response::Blinded(block.block))
}
BeaconBlockResponseWrapper::Full(block) => match fork_name {
ForkName::Base | ForkName::Altair | ForkName::Bellatrix | ForkName::Capella => Ok(
ProduceBlockV3Response::Full(FullBlockContents::Block(block.block)),
),
ForkName::Deneb | ForkName::Electra => {

BeaconBlockResponseWrapper::Full(block) => {
if fork_name.deneb_enabled() {
let BeaconBlockResponse {
block,
state: _,
Expand All @@ -37,7 +35,11 @@ pub fn build_block_contents<E: EthSpec>(
blobs,
}),
))
} else {
Ok(ProduceBlockV3Response::Full(FullBlockContents::Block(
block.block,
)))
}
},
}
}
}
5 changes: 3 additions & 2 deletions beacon_node/http_api/src/builder_states.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use safe_arith::SafeArith;
use state_processing::per_block_processing::get_expected_withdrawals;
use state_processing::state_advance::partial_state_advance;
use std::sync::Arc;
use types::{BeaconState, EthSpec, ForkName, Slot, Withdrawals};
use types::{BeaconState, EthSpec, Slot, Withdrawals};

const MAX_EPOCH_LOOKAHEAD: u64 = 2;

Expand Down Expand Up @@ -53,7 +53,8 @@ fn get_next_withdrawals_sanity_checks<T: BeaconChainTypes>(
}

let fork = chain.spec.fork_name_at_slot::<T::EthSpec>(proposal_slot);
if let ForkName::Base | ForkName::Altair | ForkName::Bellatrix = fork {

if !fork.capella_enabled() {
return Err(warp_utils::reject::custom_bad_request(
"the specified state is a pre-capella state.".to_string(),
));
Expand Down
38 changes: 16 additions & 22 deletions beacon_node/lighthouse_network/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -440,28 +440,22 @@ pub fn gossipsub_config(
fork_context: Arc<ForkContext>,
) -> Vec<u8> {
let topic_bytes = message.topic.as_str().as_bytes();
match fork_context.current_fork() {
ForkName::Altair
| ForkName::Bellatrix
| ForkName::Capella
| ForkName::Deneb
| ForkName::Electra => {
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(),
);
vec.extend_from_slice(&prefix);
vec.extend_from_slice(&topic_len_bytes);
vec.extend_from_slice(topic_bytes);
vec.extend_from_slice(&message.data);
vec
}
ForkName::Base => {
let mut vec = Vec::with_capacity(prefix.len() + message.data.len());
vec.extend_from_slice(&prefix);
vec.extend_from_slice(&message.data);
vec
}

if fork_context.current_fork().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(),
);
vec.extend_from_slice(&prefix);
vec.extend_from_slice(&topic_len_bytes);
vec.extend_from_slice(topic_bytes);
vec.extend_from_slice(&message.data);
vec
} else {
let mut vec = Vec::with_capacity(prefix.len() + message.data.len());
vec.extend_from_slice(&prefix);
vec.extend_from_slice(&message.data);
vec
}
}
let message_domain_valid_snappy = gossipsub_config_params.message_domain_valid_snappy;
Expand Down
10 changes: 4 additions & 6 deletions beacon_node/lighthouse_network/src/rpc/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,12 +151,10 @@ const REQUEST_TIMEOUT: u64 = 15;

/// Returns the maximum bytes that can be sent across the RPC.
pub fn max_rpc_size(fork_context: &ForkContext, max_chunk_size: usize) -> usize {
match fork_context.current_fork() {
ForkName::Altair | ForkName::Base => max_chunk_size / 10,
ForkName::Bellatrix => max_chunk_size,
ForkName::Capella => max_chunk_size,
ForkName::Deneb => max_chunk_size,
ForkName::Electra => max_chunk_size,
if fork_context.current_fork().bellatrix_enabled() {
max_chunk_size
} else {
max_chunk_size / 10
}
}

Expand Down
Loading

0 comments on commit 82faf97

Please sign in to comment.