Skip to content

Commit

Permalink
Unify is_optimistic_candidate_block
Browse files Browse the repository at this point in the history
  • Loading branch information
paulhauner committed Jun 28, 2022
1 parent 858db36 commit b5043b9
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 60 deletions.
36 changes: 5 additions & 31 deletions beacon_node/beacon_chain/src/block_verification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@
//!
//! ```
use crate::execution_payload::{
validate_execution_payload_for_gossip, validate_merge_block, PayloadNotifier,
is_optimistic_candidate_block, validate_execution_payload_for_gossip, validate_merge_block,
PayloadNotifier,
};
use crate::snapshot_cache::PreProcessingSnapshot;
use crate::validator_monitor::HISTORIC_EPOCHS as VALIDATOR_MONITOR_HISTORIC_EPOCHS;
Expand Down Expand Up @@ -1195,42 +1196,15 @@ impl<T: BeaconChainTypes> ExecutionPendingBlock<T> {
// If the payload did not validate or invalidate the block, check to see if this block is
// valid for optimistic import.
if payload_verification_status.is_optimistic() {
let current_slot = chain
.slot_clock
.now()
.ok_or(BeaconChainError::UnableToReadSlot)?;

// Use a blocking task to check if the block is an optimistic candidate. Interacting
// with the `canonical_head` lock in an async task can block the core executor.
let inner_chain = chain.clone();
let block_parent_root = block.parent_root();
let block_slot = block.slot();
let is_optimistic_candidate = chain
.spawn_blocking_handle(
move || {
inner_chain
.canonical_head
.fork_choice_read_lock()
.is_optimistic_candidate_block(
current_slot,
block_slot,
&block_parent_root,
&inner_chain.spec,
)
},
"validate_merge_block_optimistic_candidate",
)
.await
.map_err(BeaconChainError::from)?
.map_err(BeaconChainError::from)?;

let block_hash_opt = block
.message()
.body()
.execution_payload()
.map(|full_payload| full_payload.execution_payload.block_hash);

// Ensure the block is a candidate for optimistic import.
if !is_optimistic_candidate {
if !is_optimistic_candidate_block(&chain, block.slot(), block.parent_root()).await?
{
info!(
chain.log,
"Optimistically accepting terminal block";
Expand Down
60 changes: 31 additions & 29 deletions beacon_node/beacon_chain/src/execution_payload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,35 +188,7 @@ pub async fn validate_merge_block<'a, T: BeaconChainTypes>(
}
.into()),
None => {
let current_slot = chain
.slot_clock
.now()
.ok_or(BeaconChainError::UnableToReadSlot)?;
// Use a blocking task to check if the block is an optimistic candidate. Interacting
// with the `fork_choice` lock in an async task can block the core executor.
let inner_chain = chain.clone();
let block_parent_root = block.parent_root();
let block_slot = block.slot();
let is_optimistic_candidate = chain
.spawn_blocking_handle(
move || {
inner_chain
.canonical_head
.fork_choice_read_lock()
.is_optimistic_candidate_block(
current_slot,
block_slot,
&block_parent_root,
&inner_chain.spec,
)
},
"validate_merge_block_optimistic_candidate",
)
.await
.map_err(BeaconChainError::from)?
.map_err(BeaconChainError::from)?;

if is_optimistic_candidate {
if is_optimistic_candidate_block(chain, block.slot(), block.parent_root()).await? {
debug!(
chain.log,
"Optimistically accepting terminal block";
Expand All @@ -231,6 +203,36 @@ pub async fn validate_merge_block<'a, T: BeaconChainTypes>(
}
}

/// Check to see if a block with the given parameters is valid to be imported optimistically.
pub async fn is_optimistic_candidate_block<T: BeaconChainTypes>(
chain: &Arc<BeaconChain<T>>,
block_slot: Slot,
block_parent_root: Hash256,
) -> Result<bool, BeaconChainError> {
let current_slot = chain.slot()?;
let inner_chain = chain.clone();

// Use a blocking task to check if the block is an optimistic candidate. Interacting
// with the `fork_choice` lock in an async task can block the core executor.
chain
.spawn_blocking_handle(
move || {
inner_chain
.canonical_head
.fork_choice_read_lock()
.is_optimistic_candidate_block(
current_slot,
block_slot,
&block_parent_root,
&inner_chain.spec,
)
},
"validate_merge_block_optimistic_candidate",
)
.await?
.map_err(BeaconChainError::from)
}

/// Validate the gossip block's execution_payload according to the checks described here:
/// https://github.com/ethereum/consensus-specs/blob/dev/specs/merge/p2p-interface.md#beacon_block
pub fn validate_execution_payload_for_gossip<T: BeaconChainTypes>(
Expand Down

0 comments on commit b5043b9

Please sign in to comment.