mirror of
https://github.com/sigp/lighthouse.git
synced 2026-05-29 20:27:14 +00:00
Retrospective invalidation of exec. payloads for opt. sync (#2837)
## Issue Addressed
NA
## Proposed Changes
Adds the functionality to allow blocks to be validated/invalidated after their import as per the [optimistic sync spec](https://github.com/ethereum/consensus-specs/blob/dev/sync/optimistic.md#how-to-optimistically-import-blocks). This means:
- Updating `ProtoArray` to allow flipping the `execution_status` of ancestors/descendants based on payload validity updates.
- Creating separation between `execution_layer` and the `beacon_chain` by creating a `PayloadStatus` struct.
- Refactoring how the `execution_layer` selects a `PayloadStatus` from the multiple statuses returned from multiple EEs.
- Adding testing framework for optimistic imports.
- Add `ExecutionBlockHash(Hash256)` new-type struct to avoid confusion between *beacon block roots* and *execution payload hashes*.
- Add `merge` to [`FORKS`](c3a793fd73/Makefile (L17)) in the `Makefile` to ensure we test the beacon chain with merge settings.
- Fix some tests here that were failing due to a missing execution layer.
## TODO
- [ ] Balance tests
Co-authored-by: Mark Mackey <mark@sigmaprime.io>
This commit is contained in:
@@ -52,7 +52,7 @@ use crate::{metrics, BeaconChainError};
|
||||
use eth2::types::{
|
||||
EventKind, SseBlock, SseChainReorg, SseFinalizedCheckpoint, SseHead, SseLateHead, SyncDuty,
|
||||
};
|
||||
use execution_layer::{ExecutionLayer, PayloadStatusV1Status};
|
||||
use execution_layer::{ExecutionLayer, PayloadStatus};
|
||||
use fork_choice::{AttestationFromBlock, ForkChoice};
|
||||
use futures::channel::mpsc::Sender;
|
||||
use itertools::process_results;
|
||||
@@ -112,6 +112,10 @@ pub const FORK_CHOICE_DB_KEY: Hash256 = Hash256::zero();
|
||||
/// Defines how old a block can be before it's no longer a candidate for the early attester cache.
|
||||
const EARLY_ATTESTER_CACHE_HISTORIC_SLOTS: u64 = 4;
|
||||
|
||||
/// Reported to the user when the justified block has an invalid execution payload.
|
||||
pub const INVALID_JUSTIFIED_PAYLOAD_SHUTDOWN_REASON: &str =
|
||||
"Justified block has an invalid execution payload.";
|
||||
|
||||
/// Defines the behaviour when a block/block-root for a skipped slot is requested.
|
||||
pub enum WhenSlotSkipped {
|
||||
/// If the slot is a skip slot, return `None`.
|
||||
@@ -201,7 +205,7 @@ pub struct HeadInfo {
|
||||
pub genesis_validators_root: Hash256,
|
||||
pub proposer_shuffling_decision_root: Hash256,
|
||||
pub is_merge_transition_complete: bool,
|
||||
pub execution_payload_block_hash: Option<Hash256>,
|
||||
pub execution_payload_block_hash: Option<ExecutionBlockHash>,
|
||||
}
|
||||
|
||||
pub trait BeaconChainTypes: Send + Sync + 'static {
|
||||
@@ -220,15 +224,15 @@ pub enum HeadSafetyStatus {
|
||||
///
|
||||
/// If the block is post-terminal-block, `Some(execution_payload.block_hash)` is included with
|
||||
/// the variant.
|
||||
Safe(Option<Hash256>),
|
||||
Safe(Option<ExecutionBlockHash>),
|
||||
/// The head block execution payload has not yet been verified by an EL.
|
||||
///
|
||||
/// The `execution_payload.block_hash` of the head block is returned.
|
||||
Unsafe(Hash256),
|
||||
Unsafe(ExecutionBlockHash),
|
||||
/// The head block execution payload was deemed to be invalid by an EL.
|
||||
///
|
||||
/// The `execution_payload.block_hash` of the head block is returned.
|
||||
Invalid(Hash256),
|
||||
Invalid(ExecutionBlockHash),
|
||||
}
|
||||
|
||||
pub type BeaconForkChoice<T> = ForkChoice<
|
||||
@@ -3173,6 +3177,101 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
|
||||
Ok((block, state))
|
||||
}
|
||||
|
||||
/// This method must be called whenever an execution engine indicates that a payload is
|
||||
/// invalid.
|
||||
///
|
||||
/// If the `latest_root` is known to fork-choice it will be invalidated. If it is not known, an
|
||||
/// error will be returned.
|
||||
///
|
||||
/// If `latest_valid_hash` is `None` or references a block unknown to fork choice, no other
|
||||
/// blocks will be invalidated. If `latest_valid_hash` is a block known to fork choice, all
|
||||
/// blocks between the `latest_root` and the `latest_valid_hash` will be invalidated (which may
|
||||
/// cause further, second-order invalidations).
|
||||
///
|
||||
/// ## Notes
|
||||
///
|
||||
/// Use these rules to set `latest_root`:
|
||||
///
|
||||
/// - When `forkchoiceUpdated` indicates an invalid block, set `latest_root` to be the
|
||||
/// block root that was the head of the chain when `forkchoiceUpdated` was called.
|
||||
/// - When `executePayload` returns an invalid block *during* block import, set
|
||||
/// `latest_root` to be the parent of the beacon block containing the invalid
|
||||
/// payload (because the block containing the payload is not present in fork choice).
|
||||
/// - When `executePayload` returns an invalid block *after* block import, set
|
||||
/// `latest_root` to be root of the beacon block containing the invalid payload.
|
||||
pub fn process_invalid_execution_payload(
|
||||
&self,
|
||||
latest_root: Hash256,
|
||||
latest_valid_hash: Option<ExecutionBlockHash>,
|
||||
) -> Result<(), Error> {
|
||||
debug!(
|
||||
self.log,
|
||||
"Invalid execution payload in block";
|
||||
"latest_valid_hash" => ?latest_valid_hash,
|
||||
"latest_root" => ?latest_root,
|
||||
);
|
||||
|
||||
// Update fork choice.
|
||||
if let Err(e) = self
|
||||
.fork_choice
|
||||
.write()
|
||||
.on_invalid_execution_payload(latest_root, latest_valid_hash)
|
||||
{
|
||||
crit!(
|
||||
self.log,
|
||||
"Failed to process invalid payload";
|
||||
"error" => ?e,
|
||||
"latest_valid_hash" => ?latest_valid_hash,
|
||||
"latest_root" => ?latest_root,
|
||||
);
|
||||
}
|
||||
|
||||
// Run fork choice since it's possible that the payload invalidation might result in a new
|
||||
// head.
|
||||
//
|
||||
// Don't return early though, since invalidating the justified checkpoint might cause an
|
||||
// error here.
|
||||
if let Err(e) = self.fork_choice() {
|
||||
crit!(
|
||||
self.log,
|
||||
"Failed to run fork choice routine";
|
||||
"error" => ?e,
|
||||
);
|
||||
}
|
||||
|
||||
// Atomically obtain the justified root from fork choice.
|
||||
let justified_block = self.fork_choice.read().get_justified_block()?;
|
||||
|
||||
if justified_block.execution_status.is_invalid() {
|
||||
crit!(
|
||||
self.log,
|
||||
"The justified checkpoint is invalid";
|
||||
"msg" => "ensure you are not connected to a malicious network. This error is not \
|
||||
recoverable, please reach out to the lighthouse developers for assistance."
|
||||
);
|
||||
|
||||
let mut shutdown_sender = self.shutdown_sender();
|
||||
if let Err(e) = shutdown_sender.try_send(ShutdownReason::Failure(
|
||||
INVALID_JUSTIFIED_PAYLOAD_SHUTDOWN_REASON,
|
||||
)) {
|
||||
crit!(
|
||||
self.log,
|
||||
"Unable to trigger client shut down";
|
||||
"msg" => "shut down may already be under way",
|
||||
"error" => ?e
|
||||
);
|
||||
}
|
||||
|
||||
// Return an error here to try and prevent progression by upstream functions.
|
||||
return Err(Error::JustifiedPayloadInvalid {
|
||||
justified_root: justified_block.root,
|
||||
execution_block_hash: justified_block.execution_status.block_hash(),
|
||||
});
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Execute the fork choice algorithm and enthrone the result as the canonical head.
|
||||
pub fn fork_choice(&self) -> Result<(), Error> {
|
||||
metrics::inc_counter(&metrics::FORK_CHOICE_REQUESTS);
|
||||
@@ -3188,19 +3287,47 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
|
||||
}
|
||||
|
||||
fn fork_choice_internal(&self) -> Result<(), Error> {
|
||||
// Determine the root of the block that is the head of the chain.
|
||||
let beacon_block_root = self
|
||||
.fork_choice
|
||||
.write()
|
||||
.get_head(self.slot()?, &self.spec)?;
|
||||
// Atomically obtain the head block root and the finalized block.
|
||||
let (beacon_block_root, finalized_block) = {
|
||||
let mut fork_choice = self.fork_choice.write();
|
||||
|
||||
// Determine the root of the block that is the head of the chain.
|
||||
let beacon_block_root = fork_choice.get_head(self.slot()?, &self.spec)?;
|
||||
|
||||
(beacon_block_root, fork_choice.get_finalized_block()?)
|
||||
};
|
||||
|
||||
let current_head = self.head_info()?;
|
||||
let old_finalized_checkpoint = current_head.finalized_checkpoint;
|
||||
|
||||
// Exit early if the head hasn't changed.
|
||||
if beacon_block_root == current_head.block_root {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
// Check to ensure that this finalized block hasn't been marked as invalid.
|
||||
if let ExecutionStatus::Invalid(block_hash) = finalized_block.execution_status {
|
||||
crit!(
|
||||
self.log,
|
||||
"Finalized block has an invalid payload";
|
||||
"msg" => "You must use the `--purge-db` flag to clear the database and restart sync. \
|
||||
You may be on a hostile network.",
|
||||
"block_hash" => ?block_hash
|
||||
);
|
||||
let mut shutdown_sender = self.shutdown_sender();
|
||||
shutdown_sender
|
||||
.try_send(ShutdownReason::Failure(
|
||||
"Finalized block has an invalid execution payload.",
|
||||
))
|
||||
.map_err(BeaconChainError::InvalidFinalizedPayloadShutdownError)?;
|
||||
|
||||
// Exit now, the node is in an invalid state.
|
||||
return Err(Error::InvalidFinalizedPayload {
|
||||
finalized_root: finalized_block.root,
|
||||
execution_block_hash: block_hash,
|
||||
});
|
||||
}
|
||||
|
||||
let lag_timer = metrics::start_timer(&metrics::FORK_CHOICE_SET_HEAD_LAG_TIMES);
|
||||
|
||||
// At this point we know that the new head block is not the same as the previous one
|
||||
@@ -3448,33 +3575,6 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
|
||||
}
|
||||
|
||||
if new_finalized_checkpoint.epoch != old_finalized_checkpoint.epoch {
|
||||
// Check to ensure that this finalized block hasn't been marked as invalid.
|
||||
let finalized_block = self
|
||||
.fork_choice
|
||||
.read()
|
||||
.get_block(&new_finalized_checkpoint.root)
|
||||
.ok_or(BeaconChainError::FinalizedBlockMissingFromForkChoice(
|
||||
new_finalized_checkpoint.root,
|
||||
))?;
|
||||
if let ExecutionStatus::Invalid(block_hash) = finalized_block.execution_status {
|
||||
crit!(
|
||||
self.log,
|
||||
"Finalized block has an invalid payload";
|
||||
"msg" => "You must use the `--purge-db` flag to clear the database and restart sync. \
|
||||
You may be on a hostile network.",
|
||||
"block_hash" => ?block_hash
|
||||
);
|
||||
let mut shutdown_sender = self.shutdown_sender();
|
||||
shutdown_sender
|
||||
.try_send(ShutdownReason::Failure(
|
||||
"Finalized block has an invalid execution payload.",
|
||||
))
|
||||
.map_err(BeaconChainError::InvalidFinalizedPayloadShutdownError)?;
|
||||
|
||||
// Exit now, the node is in an invalid state.
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
// Due to race conditions, it's technically possible that the head we load here is
|
||||
// different to the one earlier in this function.
|
||||
//
|
||||
@@ -3575,64 +3675,59 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
|
||||
// If this is a post-merge block, update the execution layer.
|
||||
if let Some(new_head_execution_block_hash) = new_head_execution_block_hash_opt {
|
||||
if is_merge_transition_complete {
|
||||
let execution_layer = self
|
||||
.execution_layer
|
||||
.clone()
|
||||
.ok_or(Error::ExecutionLayerMissing)?;
|
||||
let store = self.store.clone();
|
||||
let log = self.log.clone();
|
||||
|
||||
// Spawn the update task, without waiting for it to complete.
|
||||
execution_layer.spawn(
|
||||
move |execution_layer| async move {
|
||||
if let Err(e) = Self::update_execution_engine_forkchoice(
|
||||
execution_layer,
|
||||
store,
|
||||
new_finalized_checkpoint.root,
|
||||
new_head_execution_block_hash,
|
||||
&log,
|
||||
)
|
||||
.await
|
||||
{
|
||||
crit!(
|
||||
log,
|
||||
"Failed to update execution head";
|
||||
"error" => ?e
|
||||
);
|
||||
}
|
||||
},
|
||||
"update_execution_engine_forkchoice",
|
||||
)
|
||||
let finalized_execution_block_hash = finalized_block
|
||||
.execution_status
|
||||
.block_hash()
|
||||
.unwrap_or_else(ExecutionBlockHash::zero);
|
||||
if let Err(e) = self.update_execution_engine_forkchoice_blocking(
|
||||
finalized_execution_block_hash,
|
||||
beacon_block_root,
|
||||
new_head_execution_block_hash,
|
||||
) {
|
||||
crit!(
|
||||
self.log,
|
||||
"Failed to update execution head";
|
||||
"error" => ?e
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn update_execution_engine_forkchoice(
|
||||
execution_layer: ExecutionLayer,
|
||||
store: BeaconStore<T>,
|
||||
finalized_beacon_block_root: Hash256,
|
||||
head_execution_block_hash: Hash256,
|
||||
log: &Logger,
|
||||
pub fn update_execution_engine_forkchoice_blocking(
|
||||
&self,
|
||||
finalized_execution_block_hash: ExecutionBlockHash,
|
||||
head_block_root: Hash256,
|
||||
head_execution_block_hash: ExecutionBlockHash,
|
||||
) -> Result<(), Error> {
|
||||
// Loading the finalized block from the store is not ideal. Perhaps it would be better to
|
||||
// store it on fork-choice so we can do a lookup without hitting the database.
|
||||
//
|
||||
// See: https://github.com/sigp/lighthouse/pull/2627#issuecomment-927537245
|
||||
let finalized_block = store
|
||||
.get_block(&finalized_beacon_block_root)?
|
||||
.ok_or(Error::MissingBeaconBlock(finalized_beacon_block_root))?;
|
||||
let execution_layer = self
|
||||
.execution_layer
|
||||
.as_ref()
|
||||
.ok_or(Error::ExecutionLayerMissing)?;
|
||||
|
||||
let finalized_execution_block_hash = finalized_block
|
||||
.message()
|
||||
.body()
|
||||
.execution_payload()
|
||||
.ok()
|
||||
.map(|ep| ep.block_hash)
|
||||
.unwrap_or_else(Hash256::zero);
|
||||
execution_layer
|
||||
.block_on_generic(|_| {
|
||||
self.update_execution_engine_forkchoice_async(
|
||||
finalized_execution_block_hash,
|
||||
head_block_root,
|
||||
head_execution_block_hash,
|
||||
)
|
||||
})
|
||||
.map_err(Error::ForkchoiceUpdate)?
|
||||
}
|
||||
|
||||
let forkchoice_updated_response = execution_layer
|
||||
pub async fn update_execution_engine_forkchoice_async(
|
||||
&self,
|
||||
finalized_execution_block_hash: ExecutionBlockHash,
|
||||
head_block_root: Hash256,
|
||||
head_execution_block_hash: ExecutionBlockHash,
|
||||
) -> Result<(), Error> {
|
||||
let forkchoice_updated_response = self
|
||||
.execution_layer
|
||||
.as_ref()
|
||||
.ok_or(Error::ExecutionLayerMissing)?
|
||||
.notify_forkchoice_updated(
|
||||
head_execution_block_hash,
|
||||
finalized_execution_block_hash,
|
||||
@@ -3642,14 +3737,14 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
|
||||
.map_err(Error::ExecutionForkChoiceUpdateFailed);
|
||||
|
||||
match forkchoice_updated_response {
|
||||
Ok((status, latest_valid_hash)) => match status {
|
||||
PayloadStatusV1Status::Valid | PayloadStatusV1Status::Syncing => Ok(()),
|
||||
Ok(status) => match &status {
|
||||
PayloadStatus::Valid | PayloadStatus::Syncing => Ok(()),
|
||||
// The specification doesn't list `ACCEPTED` as a valid response to a fork choice
|
||||
// update. This response *seems* innocent enough, so we won't return early with an
|
||||
// error. However, we create a log to bring attention to the issue.
|
||||
PayloadStatusV1Status::Accepted => {
|
||||
PayloadStatus::Accepted => {
|
||||
warn!(
|
||||
log,
|
||||
self.log,
|
||||
"Fork choice update received ACCEPTED";
|
||||
"msg" => "execution engine provided an unexpected response to a fork \
|
||||
choice update. although this is not a serious issue, please raise \
|
||||
@@ -3657,16 +3752,38 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
PayloadStatusV1Status::Invalid
|
||||
| PayloadStatusV1Status::InvalidTerminalBlock
|
||||
| PayloadStatusV1Status::InvalidBlockHash => {
|
||||
// TODO(bellatrix): process the invalid payload.
|
||||
PayloadStatus::Invalid {
|
||||
latest_valid_hash, ..
|
||||
} => {
|
||||
warn!(
|
||||
self.log,
|
||||
"Fork choice update invalidated payload";
|
||||
"status" => ?status
|
||||
);
|
||||
// The execution engine has stated that all blocks between the
|
||||
// `head_execution_block_hash` and `latest_valid_hash` are invalid.
|
||||
self.process_invalid_execution_payload(
|
||||
head_block_root,
|
||||
Some(*latest_valid_hash),
|
||||
)?;
|
||||
|
||||
Err(BeaconChainError::ExecutionForkChoiceUpdateInvalid { status })
|
||||
}
|
||||
PayloadStatus::InvalidTerminalBlock { .. }
|
||||
| PayloadStatus::InvalidBlockHash { .. } => {
|
||||
warn!(
|
||||
self.log,
|
||||
"Fork choice update invalidated payload";
|
||||
"status" => ?status
|
||||
);
|
||||
// The execution engine has stated that the head block is invalid, however it
|
||||
// hasn't returned a latest valid ancestor.
|
||||
//
|
||||
// See: https://github.com/sigp/lighthouse/pull/2837
|
||||
Err(BeaconChainError::ExecutionForkChoiceUpdateInvalid {
|
||||
status,
|
||||
latest_valid_hash,
|
||||
})
|
||||
// Using a `None` latest valid ancestor will result in only the head block
|
||||
// being invalidated (no ancestors).
|
||||
self.process_invalid_execution_payload(head_block_root, None)?;
|
||||
|
||||
Err(BeaconChainError::ExecutionForkChoiceUpdateInvalid { status })
|
||||
}
|
||||
},
|
||||
Err(e) => Err(e),
|
||||
|
||||
@@ -54,7 +54,7 @@ use crate::{
|
||||
metrics, BeaconChain, BeaconChainError, BeaconChainTypes,
|
||||
};
|
||||
use eth2::types::EventKind;
|
||||
use execution_layer::PayloadStatusV1Status;
|
||||
use execution_layer::PayloadStatus;
|
||||
use fork_choice::{ForkChoice, ForkChoiceStore, PayloadVerificationStatus};
|
||||
use parking_lot::RwLockReadGuard;
|
||||
use proto_array::Block as ProtoBlock;
|
||||
@@ -76,9 +76,9 @@ use std::time::Duration;
|
||||
use store::{Error as DBError, HotColdDB, HotStateSummary, KeyValueStore, StoreOp};
|
||||
use tree_hash::TreeHash;
|
||||
use types::{
|
||||
BeaconBlockRef, BeaconState, BeaconStateError, ChainSpec, CloneConfig, Epoch, EthSpec, Hash256,
|
||||
InconsistentFork, PublicKey, PublicKeyBytes, RelativeEpoch, SignedBeaconBlock,
|
||||
SignedBeaconBlockHeader, Slot,
|
||||
BeaconBlockRef, BeaconState, BeaconStateError, ChainSpec, CloneConfig, Epoch, EthSpec,
|
||||
ExecutionBlockHash, Hash256, InconsistentFork, PublicKey, PublicKeyBytes, RelativeEpoch,
|
||||
SignedBeaconBlock, SignedBeaconBlockHeader, Slot,
|
||||
};
|
||||
|
||||
/// Maximum block slot number. Block with slots bigger than this constant will NOT be processed.
|
||||
@@ -270,10 +270,7 @@ pub enum ExecutionPayloadError {
|
||||
/// ## Peer scoring
|
||||
///
|
||||
/// The block is invalid and the peer is faulty
|
||||
RejectedByExecutionEngine {
|
||||
status: PayloadStatusV1Status,
|
||||
latest_valid_hash: Option<Vec<Hash256>>,
|
||||
},
|
||||
RejectedByExecutionEngine { status: PayloadStatus },
|
||||
/// The execution payload timestamp does not match the slot
|
||||
///
|
||||
/// ## Peer scoring
|
||||
@@ -286,7 +283,7 @@ pub enum ExecutionPayloadError {
|
||||
///
|
||||
/// The block is invalid and the peer sent us a block that passes gossip propagation conditions,
|
||||
/// but is invalid upon further verification.
|
||||
InvalidTerminalPoWBlock { parent_hash: Hash256 },
|
||||
InvalidTerminalPoWBlock { parent_hash: ExecutionBlockHash },
|
||||
/// The `TERMINAL_BLOCK_HASH` is set, but the block has not reached the
|
||||
/// `TERMINAL_BLOCK_HASH_ACTIVATION_EPOCH`.
|
||||
///
|
||||
@@ -305,8 +302,8 @@ pub enum ExecutionPayloadError {
|
||||
/// The block is invalid and the peer sent us a block that passes gossip propagation conditions,
|
||||
/// but is invalid upon further verification.
|
||||
InvalidTerminalBlockHash {
|
||||
terminal_block_hash: Hash256,
|
||||
payload_parent_hash: Hash256,
|
||||
terminal_block_hash: ExecutionBlockHash,
|
||||
payload_parent_hash: ExecutionBlockHash,
|
||||
},
|
||||
/// The execution node failed to provide a parent block to a known block. This indicates an
|
||||
/// issue with the execution node.
|
||||
@@ -314,7 +311,7 @@ pub enum ExecutionPayloadError {
|
||||
/// ## Peer scoring
|
||||
///
|
||||
/// The peer is not necessarily invalid.
|
||||
PoWParentMissing(Hash256),
|
||||
PoWParentMissing(ExecutionBlockHash),
|
||||
}
|
||||
|
||||
impl From<execution_layer::Error> for ExecutionPayloadError {
|
||||
|
||||
@@ -8,7 +8,7 @@ use crate::naive_aggregation_pool::Error as NaiveAggregationError;
|
||||
use crate::observed_aggregates::Error as ObservedAttestationsError;
|
||||
use crate::observed_attesters::Error as ObservedAttestersError;
|
||||
use crate::observed_block_producers::Error as ObservedBlockProducersError;
|
||||
use execution_layer::PayloadStatusV1Status;
|
||||
use execution_layer::PayloadStatus;
|
||||
use futures::channel::mpsc::TrySendError;
|
||||
use operation_pool::OpPoolError;
|
||||
use safe_arith::ArithError;
|
||||
@@ -139,15 +139,27 @@ pub enum BeaconChainError {
|
||||
ExecutionLayerMissing,
|
||||
ExecutionForkChoiceUpdateFailed(execution_layer::Error),
|
||||
ExecutionForkChoiceUpdateInvalid {
|
||||
status: PayloadStatusV1Status,
|
||||
latest_valid_hash: Option<Vec<Hash256>>,
|
||||
status: PayloadStatus,
|
||||
},
|
||||
BlockRewardSlotError,
|
||||
BlockRewardAttestationError,
|
||||
BlockRewardSyncError,
|
||||
HeadMissingFromForkChoice(Hash256),
|
||||
FinalizedBlockMissingFromForkChoice(Hash256),
|
||||
InvalidFinalizedPayload {
|
||||
finalized_root: Hash256,
|
||||
execution_block_hash: ExecutionBlockHash,
|
||||
},
|
||||
InvalidFinalizedPayloadShutdownError(TrySendError<ShutdownReason>),
|
||||
JustifiedPayloadInvalid {
|
||||
justified_root: Hash256,
|
||||
execution_block_hash: Option<ExecutionBlockHash>,
|
||||
},
|
||||
ForkchoiceUpdate(execution_layer::Error),
|
||||
FinalizedCheckpointMismatch {
|
||||
head_state: Checkpoint,
|
||||
fork_choice: Hash256,
|
||||
},
|
||||
}
|
||||
|
||||
easy_from_to!(SlotProcessingError, BeaconChainError);
|
||||
|
||||
@@ -11,7 +11,7 @@ use crate::{
|
||||
BeaconChain, BeaconChainError, BeaconChainTypes, BlockError, BlockProductionError,
|
||||
ExecutionPayloadError,
|
||||
};
|
||||
use execution_layer::PayloadStatusV1Status;
|
||||
use execution_layer::PayloadStatus;
|
||||
use fork_choice::PayloadVerificationStatus;
|
||||
use proto_array::{Block as ProtoBlock, ExecutionStatus};
|
||||
use slog::debug;
|
||||
@@ -57,22 +57,26 @@ pub fn notify_new_payload<T: BeaconChainTypes>(
|
||||
.block_on(|execution_layer| execution_layer.notify_new_payload(execution_payload));
|
||||
|
||||
match new_payload_response {
|
||||
Ok((status, latest_valid_hash)) => match status {
|
||||
PayloadStatusV1Status::Valid => Ok(PayloadVerificationStatus::Verified),
|
||||
PayloadStatusV1Status::Syncing | PayloadStatusV1Status::Accepted => {
|
||||
Ok(status) => match status {
|
||||
PayloadStatus::Valid => Ok(PayloadVerificationStatus::Verified),
|
||||
PayloadStatus::Syncing | PayloadStatus::Accepted => {
|
||||
Ok(PayloadVerificationStatus::NotVerified)
|
||||
}
|
||||
PayloadStatusV1Status::Invalid
|
||||
| PayloadStatusV1Status::InvalidTerminalBlock
|
||||
| PayloadStatusV1Status::InvalidBlockHash => {
|
||||
// TODO(bellatrix): process the invalid payload.
|
||||
//
|
||||
// See: https://github.com/sigp/lighthouse/pull/2837
|
||||
Err(ExecutionPayloadError::RejectedByExecutionEngine {
|
||||
status,
|
||||
latest_valid_hash,
|
||||
}
|
||||
.into())
|
||||
PayloadStatus::Invalid {
|
||||
latest_valid_hash, ..
|
||||
} => {
|
||||
// This block has not yet been applied to fork choice, so the latest block that was
|
||||
// imported to fork choice was the parent.
|
||||
let latest_root = block.parent_root();
|
||||
chain.process_invalid_execution_payload(latest_root, Some(latest_valid_hash))?;
|
||||
|
||||
Err(ExecutionPayloadError::RejectedByExecutionEngine { status }.into())
|
||||
}
|
||||
PayloadStatus::InvalidTerminalBlock { .. } | PayloadStatus::InvalidBlockHash { .. } => {
|
||||
// Returning an error here should be sufficient to invalidate the block. We have no
|
||||
// information to indicate its parent is invalid, so no need to run
|
||||
// `BeaconChain::process_invalid_execution_payload`.
|
||||
Err(ExecutionPayloadError::RejectedByExecutionEngine { status }.into())
|
||||
}
|
||||
},
|
||||
Err(e) => Err(ExecutionPayloadError::RequestFailed(e).into()),
|
||||
@@ -99,7 +103,7 @@ pub fn validate_merge_block<T: BeaconChainTypes>(
|
||||
let block_epoch = block.slot().epoch(T::EthSpec::slots_per_epoch());
|
||||
let execution_payload = block.execution_payload()?;
|
||||
|
||||
if spec.terminal_block_hash != Hash256::zero() {
|
||||
if spec.terminal_block_hash != ExecutionBlockHash::zero() {
|
||||
if block_epoch < spec.terminal_block_hash_activation_epoch {
|
||||
return Err(ExecutionPayloadError::InvalidActivationEpoch {
|
||||
activation_epoch: spec.terminal_block_hash_activation_epoch,
|
||||
@@ -263,7 +267,7 @@ pub async fn prepare_execution_payload<T: BeaconChainTypes>(
|
||||
.ok_or(BlockProductionError::ExecutionLayerMissing)?;
|
||||
|
||||
let parent_hash = if !is_merge_transition_complete(state) {
|
||||
let is_terminal_block_hash_set = spec.terminal_block_hash != Hash256::zero();
|
||||
let is_terminal_block_hash_set = spec.terminal_block_hash != ExecutionBlockHash::zero();
|
||||
let is_activation_epoch_reached =
|
||||
state.current_epoch() >= spec.terminal_block_hash_activation_epoch;
|
||||
|
||||
@@ -314,7 +318,7 @@ pub async fn prepare_execution_payload<T: BeaconChainTypes>(
|
||||
parent_hash,
|
||||
timestamp,
|
||||
random,
|
||||
finalized_block_hash.unwrap_or_else(Hash256::zero),
|
||||
finalized_block_hash.unwrap_or_else(ExecutionBlockHash::zero),
|
||||
proposer_index,
|
||||
)
|
||||
.await
|
||||
|
||||
@@ -41,7 +41,7 @@ mod validator_pubkey_cache;
|
||||
pub use self::beacon_chain::{
|
||||
AttestationProcessingOutcome, BeaconChain, BeaconChainTypes, BeaconStore, ChainSegmentResult,
|
||||
ForkChoiceError, HeadInfo, HeadSafetyStatus, StateSkipConfig, WhenSlotSkipped,
|
||||
MAXIMUM_GOSSIP_CLOCK_DISPARITY,
|
||||
INVALID_JUSTIFIED_PAYLOAD_SHUTDOWN_REASON, MAXIMUM_GOSSIP_CLOCK_DISPARITY,
|
||||
};
|
||||
pub use self::beacon_snapshot::BeaconSnapshot;
|
||||
pub use self::chain_config::ChainConfig;
|
||||
|
||||
@@ -432,7 +432,7 @@ where
|
||||
spec: chain.spec.clone(),
|
||||
chain: Arc::new(chain),
|
||||
validator_keypairs,
|
||||
shutdown_receiver,
|
||||
shutdown_receiver: Arc::new(Mutex::new(shutdown_receiver)),
|
||||
mock_execution_layer: self.mock_execution_layer,
|
||||
execution_layer_runtime: self.execution_layer_runtime,
|
||||
rng: make_rng(),
|
||||
@@ -449,7 +449,7 @@ pub struct BeaconChainHarness<T: BeaconChainTypes> {
|
||||
|
||||
pub chain: Arc<BeaconChain<T>>,
|
||||
pub spec: ChainSpec,
|
||||
pub shutdown_receiver: Receiver<ShutdownReason>,
|
||||
pub shutdown_receiver: Arc<Mutex<Receiver<ShutdownReason>>>,
|
||||
|
||||
pub mock_execution_layer: Option<MockExecutionLayer<T::EthSpec>>,
|
||||
pub execution_layer_runtime: Option<ExecutionLayerRuntime>,
|
||||
@@ -502,6 +502,17 @@ where
|
||||
epoch.start_slot(E::slots_per_epoch()).into()
|
||||
}
|
||||
|
||||
pub fn shutdown_reasons(&self) -> Vec<ShutdownReason> {
|
||||
let mutex = self.shutdown_receiver.clone();
|
||||
let mut receiver = mutex.lock();
|
||||
std::iter::from_fn(move || match receiver.try_next() {
|
||||
Ok(Some(s)) => Some(s),
|
||||
Ok(None) => panic!("shutdown sender dropped"),
|
||||
Err(_) => None,
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
pub fn get_current_state(&self) -> BeaconState<E> {
|
||||
self.chain.head().unwrap().beacon_state
|
||||
}
|
||||
|
||||
@@ -26,6 +26,7 @@ fn produces_attestations() {
|
||||
.default_spec()
|
||||
.keypairs(KEYPAIRS[..].to_vec())
|
||||
.fresh_ephemeral_store()
|
||||
.mock_execution_layer()
|
||||
.build();
|
||||
|
||||
let chain = &harness.chain;
|
||||
|
||||
@@ -42,6 +42,7 @@ fn get_harness(validator_count: usize) -> BeaconChainHarness<EphemeralHarnessTyp
|
||||
.spec(spec)
|
||||
.keypairs(KEYPAIRS[0..validator_count].to_vec())
|
||||
.fresh_ephemeral_store()
|
||||
.mock_execution_layer()
|
||||
.build();
|
||||
|
||||
harness.advance_slot();
|
||||
|
||||
@@ -54,6 +54,7 @@ fn get_harness(validator_count: usize) -> BeaconChainHarness<EphemeralHarnessTyp
|
||||
.default_spec()
|
||||
.keypairs(KEYPAIRS[0..validator_count].to_vec())
|
||||
.fresh_ephemeral_store()
|
||||
.mock_execution_layer()
|
||||
.build();
|
||||
|
||||
harness.advance_slot();
|
||||
@@ -839,6 +840,7 @@ fn verify_block_for_gossip_slashing_detection() {
|
||||
.keypairs(KEYPAIRS.to_vec())
|
||||
.fresh_ephemeral_store()
|
||||
.initial_mutator(Box::new(move |builder| builder.slasher(inner_slasher)))
|
||||
.mock_execution_layer()
|
||||
.build();
|
||||
harness.advance_slot();
|
||||
|
||||
@@ -918,6 +920,7 @@ fn add_base_block_to_altair_chain() {
|
||||
.spec(spec)
|
||||
.keypairs(KEYPAIRS[..].to_vec())
|
||||
.fresh_ephemeral_store()
|
||||
.mock_execution_layer()
|
||||
.build();
|
||||
|
||||
// Move out of the genesis slot.
|
||||
@@ -1036,6 +1039,7 @@ fn add_altair_block_to_base_chain() {
|
||||
.spec(spec)
|
||||
.keypairs(KEYPAIRS[..].to_vec())
|
||||
.fresh_ephemeral_store()
|
||||
.mock_execution_layer()
|
||||
.build();
|
||||
|
||||
// Move out of the genesis slot.
|
||||
|
||||
@@ -3,6 +3,7 @@ mod attestation_verification;
|
||||
mod block_verification;
|
||||
mod merge;
|
||||
mod op_verification;
|
||||
mod payload_invalidation;
|
||||
mod store_tests;
|
||||
mod sync_committee_verification;
|
||||
mod tests;
|
||||
|
||||
@@ -13,7 +13,7 @@ fn verify_execution_payload_chain<T: EthSpec>(chain: &[ExecutionPayload<T>]) {
|
||||
|
||||
for ep in chain {
|
||||
assert!(*ep != ExecutionPayload::default());
|
||||
assert!(ep.block_hash != Hash256::zero());
|
||||
assert!(ep.block_hash != ExecutionBlockHash::zero());
|
||||
|
||||
// Check against previous `ExecutionPayload`.
|
||||
if let Some(prev_ep) = prev_ep {
|
||||
@@ -40,7 +40,7 @@ fn merge_with_terminal_block_hash_override() {
|
||||
spec.terminal_total_difficulty,
|
||||
DEFAULT_TERMINAL_BLOCK,
|
||||
0,
|
||||
Hash256::zero(),
|
||||
ExecutionBlockHash::zero(),
|
||||
)
|
||||
.unwrap()
|
||||
.block_hash;
|
||||
|
||||
@@ -40,6 +40,7 @@ fn get_harness(store: Arc<HotColdDB>, validator_count: usize) -> TestHarness {
|
||||
.default_spec()
|
||||
.keypairs(KEYPAIRS[0..validator_count].to_vec())
|
||||
.fresh_disk_store(store)
|
||||
.mock_execution_layer()
|
||||
.build();
|
||||
harness.advance_slot();
|
||||
harness
|
||||
|
||||
601
beacon_node/beacon_chain/tests/payload_invalidation.rs
Normal file
601
beacon_node/beacon_chain/tests/payload_invalidation.rs
Normal file
@@ -0,0 +1,601 @@
|
||||
#![cfg(not(debug_assertions))]
|
||||
|
||||
use beacon_chain::{
|
||||
test_utils::{BeaconChainHarness, EphemeralHarnessType},
|
||||
BeaconChainError, BlockError, ExecutionPayloadError, HeadInfo, StateSkipConfig,
|
||||
WhenSlotSkipped, INVALID_JUSTIFIED_PAYLOAD_SHUTDOWN_REASON,
|
||||
};
|
||||
use proto_array::ExecutionStatus;
|
||||
use task_executor::ShutdownReason;
|
||||
use types::*;
|
||||
|
||||
const VALIDATOR_COUNT: usize = 32;
|
||||
|
||||
type E = MainnetEthSpec;
|
||||
|
||||
#[derive(PartialEq, Clone)]
|
||||
enum Payload {
|
||||
Valid,
|
||||
Invalid {
|
||||
latest_valid_hash: Option<ExecutionBlockHash>,
|
||||
},
|
||||
Syncing,
|
||||
}
|
||||
|
||||
struct InvalidPayloadRig {
|
||||
harness: BeaconChainHarness<EphemeralHarnessType<E>>,
|
||||
enable_attestations: bool,
|
||||
}
|
||||
|
||||
impl InvalidPayloadRig {
|
||||
fn new() -> Self {
|
||||
let mut spec = E::default_spec();
|
||||
spec.altair_fork_epoch = Some(Epoch::new(0));
|
||||
spec.bellatrix_fork_epoch = Some(Epoch::new(0));
|
||||
|
||||
let harness = BeaconChainHarness::builder(MainnetEthSpec)
|
||||
.spec(spec)
|
||||
.deterministic_keypairs(VALIDATOR_COUNT)
|
||||
.mock_execution_layer()
|
||||
.fresh_ephemeral_store()
|
||||
.build();
|
||||
|
||||
// Move to slot 1.
|
||||
harness.advance_slot();
|
||||
|
||||
Self {
|
||||
harness,
|
||||
enable_attestations: false,
|
||||
}
|
||||
}
|
||||
|
||||
fn enable_attestations(mut self) -> Self {
|
||||
self.enable_attestations = true;
|
||||
self
|
||||
}
|
||||
|
||||
fn block_hash(&self, block_root: Hash256) -> ExecutionBlockHash {
|
||||
self.harness
|
||||
.chain
|
||||
.get_block(&block_root)
|
||||
.unwrap()
|
||||
.unwrap()
|
||||
.message()
|
||||
.body()
|
||||
.execution_payload()
|
||||
.unwrap()
|
||||
.block_hash
|
||||
}
|
||||
|
||||
fn execution_status(&self, block_root: Hash256) -> ExecutionStatus {
|
||||
self.harness
|
||||
.chain
|
||||
.fork_choice
|
||||
.read()
|
||||
.get_block(&block_root)
|
||||
.unwrap()
|
||||
.execution_status
|
||||
}
|
||||
|
||||
fn fork_choice(&self) {
|
||||
self.harness.chain.fork_choice().unwrap();
|
||||
}
|
||||
|
||||
fn head_info(&self) -> HeadInfo {
|
||||
self.harness.chain.head_info().unwrap()
|
||||
}
|
||||
|
||||
fn move_to_terminal_block(&self) {
|
||||
let mock_execution_layer = self.harness.mock_execution_layer.as_ref().unwrap();
|
||||
mock_execution_layer
|
||||
.server
|
||||
.execution_block_generator()
|
||||
.move_to_terminal_block()
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
fn build_blocks(&mut self, num_blocks: u64, is_valid: Payload) -> Vec<Hash256> {
|
||||
(0..num_blocks)
|
||||
.map(|_| self.import_block(is_valid.clone()))
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn move_to_first_justification(&mut self, is_valid: Payload) {
|
||||
let slots_till_justification = E::slots_per_epoch() * 3;
|
||||
self.build_blocks(slots_till_justification, is_valid);
|
||||
|
||||
let justified_checkpoint = self.head_info().current_justified_checkpoint;
|
||||
assert_eq!(justified_checkpoint.epoch, 2);
|
||||
}
|
||||
|
||||
fn import_block(&mut self, is_valid: Payload) -> Hash256 {
|
||||
self.import_block_parametric(is_valid, |error| {
|
||||
matches!(
|
||||
error,
|
||||
BlockError::ExecutionPayloadError(
|
||||
ExecutionPayloadError::RejectedByExecutionEngine { .. }
|
||||
)
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
fn block_root_at_slot(&self, slot: Slot) -> Option<Hash256> {
|
||||
self.harness
|
||||
.chain
|
||||
.block_root_at_slot(slot, WhenSlotSkipped::None)
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
fn import_block_parametric<F: Fn(&BlockError<E>) -> bool>(
|
||||
&mut self,
|
||||
is_valid: Payload,
|
||||
evaluate_error: F,
|
||||
) -> Hash256 {
|
||||
let mock_execution_layer = self.harness.mock_execution_layer.as_ref().unwrap();
|
||||
|
||||
let head = self.harness.chain.head().unwrap();
|
||||
let state = head.beacon_state;
|
||||
let slot = state.slot() + 1;
|
||||
let (block, post_state) = self.harness.make_block(state, slot);
|
||||
let block_root = block.canonical_root();
|
||||
|
||||
match is_valid {
|
||||
Payload::Valid | Payload::Syncing => {
|
||||
if is_valid == Payload::Syncing {
|
||||
// Importing a payload whilst returning `SYNCING` simulates an EE that obtains
|
||||
// the block via it's own means (e.g., devp2p).
|
||||
let should_import_payload = true;
|
||||
mock_execution_layer
|
||||
.server
|
||||
.all_payloads_syncing(should_import_payload);
|
||||
} else {
|
||||
mock_execution_layer.server.full_payload_verification();
|
||||
}
|
||||
let root = self.harness.process_block(slot, block.clone()).unwrap();
|
||||
|
||||
if self.enable_attestations {
|
||||
let all_validators: Vec<usize> = (0..VALIDATOR_COUNT).collect();
|
||||
self.harness.attest_block(
|
||||
&post_state,
|
||||
block.state_root(),
|
||||
block_root.into(),
|
||||
&block,
|
||||
&all_validators,
|
||||
);
|
||||
}
|
||||
|
||||
let execution_status = self.execution_status(root.into());
|
||||
|
||||
match is_valid {
|
||||
Payload::Syncing => assert!(execution_status.is_not_verified()),
|
||||
Payload::Valid => assert!(execution_status.is_valid()),
|
||||
Payload::Invalid { .. } => unreachable!(),
|
||||
}
|
||||
|
||||
assert_eq!(
|
||||
self.harness.chain.get_block(&block_root).unwrap().unwrap(),
|
||||
block,
|
||||
"block from db must match block imported"
|
||||
);
|
||||
}
|
||||
Payload::Invalid { latest_valid_hash } => {
|
||||
let latest_valid_hash = latest_valid_hash
|
||||
.unwrap_or_else(|| self.block_hash(block.message().parent_root()));
|
||||
|
||||
mock_execution_layer
|
||||
.server
|
||||
.all_payloads_invalid(latest_valid_hash);
|
||||
|
||||
match self.harness.process_block(slot, block) {
|
||||
Err(error) if evaluate_error(&error) => (),
|
||||
Err(other) => {
|
||||
panic!("evaluate_error returned false with {:?}", other)
|
||||
}
|
||||
Ok(_) => panic!("block with invalid payload was imported"),
|
||||
};
|
||||
|
||||
assert!(
|
||||
self.harness
|
||||
.chain
|
||||
.fork_choice
|
||||
.read()
|
||||
.get_block(&block_root)
|
||||
.is_none(),
|
||||
"invalid block must not exist in fork choice"
|
||||
);
|
||||
assert!(
|
||||
self.harness.chain.get_block(&block_root).unwrap().is_none(),
|
||||
"invalid block cannot be accessed via get_block"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
block_root
|
||||
}
|
||||
}
|
||||
|
||||
/// Simple test of the different import types.
|
||||
#[test]
|
||||
fn valid_invalid_syncing() {
|
||||
let mut rig = InvalidPayloadRig::new();
|
||||
rig.move_to_terminal_block();
|
||||
|
||||
rig.import_block(Payload::Valid);
|
||||
rig.import_block(Payload::Invalid {
|
||||
latest_valid_hash: None,
|
||||
});
|
||||
rig.import_block(Payload::Syncing);
|
||||
}
|
||||
|
||||
/// Ensure that an invalid payload can invalidate its parent too (given the right
|
||||
/// `latest_valid_hash`.
|
||||
#[test]
|
||||
fn invalid_payload_invalidates_parent() {
|
||||
let mut rig = InvalidPayloadRig::new();
|
||||
rig.move_to_terminal_block();
|
||||
|
||||
let roots = vec![
|
||||
rig.import_block(Payload::Syncing),
|
||||
rig.import_block(Payload::Syncing),
|
||||
rig.import_block(Payload::Syncing),
|
||||
];
|
||||
|
||||
let latest_valid_hash = rig.block_hash(roots[0]);
|
||||
|
||||
rig.import_block(Payload::Invalid {
|
||||
latest_valid_hash: Some(latest_valid_hash),
|
||||
});
|
||||
|
||||
assert!(rig.execution_status(roots[0]).is_valid());
|
||||
assert!(rig.execution_status(roots[1]).is_invalid());
|
||||
assert!(rig.execution_status(roots[2]).is_invalid());
|
||||
|
||||
assert_eq!(rig.head_info().block_root, roots[0]);
|
||||
}
|
||||
|
||||
/// Ensure the client tries to exit when the justified checkpoint is invalidated.
|
||||
#[test]
|
||||
fn justified_checkpoint_becomes_invalid() {
|
||||
let mut rig = InvalidPayloadRig::new().enable_attestations();
|
||||
rig.move_to_terminal_block();
|
||||
rig.move_to_first_justification(Payload::Syncing);
|
||||
|
||||
let justified_checkpoint = rig.head_info().current_justified_checkpoint;
|
||||
let parent_root_of_justified = rig
|
||||
.harness
|
||||
.chain
|
||||
.get_block(&justified_checkpoint.root)
|
||||
.unwrap()
|
||||
.unwrap()
|
||||
.parent_root();
|
||||
let parent_hash_of_justified = rig.block_hash(parent_root_of_justified);
|
||||
|
||||
// No service should have triggered a shutdown, yet.
|
||||
assert!(rig.harness.shutdown_reasons().is_empty());
|
||||
|
||||
// Import a block that will invalidate the justified checkpoint.
|
||||
rig.import_block_parametric(
|
||||
Payload::Invalid {
|
||||
latest_valid_hash: Some(parent_hash_of_justified),
|
||||
},
|
||||
|error| {
|
||||
matches!(
|
||||
error,
|
||||
// The block import should fail since the beacon chain knows the justified payload
|
||||
// is invalid.
|
||||
BlockError::BeaconChainError(BeaconChainError::JustifiedPayloadInvalid { .. })
|
||||
)
|
||||
},
|
||||
);
|
||||
|
||||
// The beacon chain should have triggered a shutdown.
|
||||
assert_eq!(
|
||||
rig.harness.shutdown_reasons(),
|
||||
vec![ShutdownReason::Failure(
|
||||
INVALID_JUSTIFIED_PAYLOAD_SHUTDOWN_REASON
|
||||
)]
|
||||
);
|
||||
}
|
||||
|
||||
/// Ensure that a `latest_valid_hash` for a pre-finality block only reverts a single block.
|
||||
#[test]
|
||||
fn pre_finalized_latest_valid_hash() {
|
||||
let num_blocks = E::slots_per_epoch() * 4;
|
||||
let finalized_epoch = 2;
|
||||
|
||||
let mut rig = InvalidPayloadRig::new().enable_attestations();
|
||||
rig.move_to_terminal_block();
|
||||
let blocks = rig.build_blocks(num_blocks, Payload::Syncing);
|
||||
|
||||
assert_eq!(rig.head_info().finalized_checkpoint.epoch, finalized_epoch);
|
||||
|
||||
let pre_finalized_block_root = rig.block_root_at_slot(Slot::new(1)).unwrap();
|
||||
let pre_finalized_block_hash = rig.block_hash(pre_finalized_block_root);
|
||||
|
||||
// No service should have triggered a shutdown, yet.
|
||||
assert!(rig.harness.shutdown_reasons().is_empty());
|
||||
|
||||
// Import a pre-finalized block.
|
||||
rig.import_block(Payload::Invalid {
|
||||
latest_valid_hash: Some(pre_finalized_block_hash),
|
||||
});
|
||||
|
||||
// The latest imported block should be the head.
|
||||
assert_eq!(rig.head_info().block_root, *blocks.last().unwrap());
|
||||
|
||||
// The beacon chain should *not* have triggered a shutdown.
|
||||
assert_eq!(rig.harness.shutdown_reasons(), vec![]);
|
||||
|
||||
// All blocks should still be unverified.
|
||||
for i in E::slots_per_epoch() * finalized_epoch..num_blocks {
|
||||
let slot = Slot::new(i);
|
||||
let root = rig.block_root_at_slot(slot).unwrap();
|
||||
assert!(rig.execution_status(root).is_not_verified());
|
||||
}
|
||||
}
|
||||
|
||||
/// Ensure that a `latest_valid_hash` will:
|
||||
///
|
||||
/// - Invalidate descendants of `latest_valid_root`.
|
||||
/// - Validate `latest_valid_root` and its ancestors.
|
||||
#[test]
|
||||
fn latest_valid_hash_will_validate() {
|
||||
const LATEST_VALID_SLOT: u64 = 3;
|
||||
|
||||
let mut rig = InvalidPayloadRig::new().enable_attestations();
|
||||
rig.move_to_terminal_block();
|
||||
let blocks = rig.build_blocks(4, Payload::Syncing);
|
||||
|
||||
let latest_valid_root = rig
|
||||
.block_root_at_slot(Slot::new(LATEST_VALID_SLOT))
|
||||
.unwrap();
|
||||
let latest_valid_hash = rig.block_hash(latest_valid_root);
|
||||
|
||||
rig.import_block(Payload::Invalid {
|
||||
latest_valid_hash: Some(latest_valid_hash),
|
||||
});
|
||||
|
||||
assert_eq!(rig.head_info().slot, LATEST_VALID_SLOT);
|
||||
|
||||
for slot in 0..=4 {
|
||||
let slot = Slot::new(slot);
|
||||
let root = if slot > 0 {
|
||||
// If not the genesis slot, check the blocks we just produced.
|
||||
blocks[slot.as_usize() - 1]
|
||||
} else {
|
||||
// Genesis slot
|
||||
rig.block_root_at_slot(slot).unwrap()
|
||||
};
|
||||
let execution_status = rig.execution_status(root);
|
||||
|
||||
if slot > LATEST_VALID_SLOT {
|
||||
assert!(execution_status.is_invalid())
|
||||
} else if slot == 0 {
|
||||
assert!(execution_status.is_irrelevant())
|
||||
} else {
|
||||
assert!(execution_status.is_valid())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Check behaviour when the `latest_valid_hash` is a junk value.
|
||||
#[test]
|
||||
fn latest_valid_hash_is_junk() {
|
||||
let num_blocks = E::slots_per_epoch() * 5;
|
||||
let finalized_epoch = 3;
|
||||
|
||||
let mut rig = InvalidPayloadRig::new().enable_attestations();
|
||||
rig.move_to_terminal_block();
|
||||
let blocks = rig.build_blocks(num_blocks, Payload::Syncing);
|
||||
|
||||
assert_eq!(rig.head_info().finalized_checkpoint.epoch, finalized_epoch);
|
||||
|
||||
// No service should have triggered a shutdown, yet.
|
||||
assert!(rig.harness.shutdown_reasons().is_empty());
|
||||
|
||||
let junk_hash = ExecutionBlockHash::repeat_byte(42);
|
||||
rig.import_block(Payload::Invalid {
|
||||
latest_valid_hash: Some(junk_hash),
|
||||
});
|
||||
|
||||
// The latest imported block should be the head.
|
||||
assert_eq!(rig.head_info().block_root, *blocks.last().unwrap());
|
||||
|
||||
// The beacon chain should *not* have triggered a shutdown.
|
||||
assert_eq!(rig.harness.shutdown_reasons(), vec![]);
|
||||
|
||||
// All blocks should still be unverified.
|
||||
for i in E::slots_per_epoch() * finalized_epoch..num_blocks {
|
||||
let slot = Slot::new(i);
|
||||
let root = rig.block_root_at_slot(slot).unwrap();
|
||||
assert!(rig.execution_status(root).is_not_verified());
|
||||
}
|
||||
}
|
||||
|
||||
/// Check that descendants of invalid blocks are also invalidated.
|
||||
#[test]
|
||||
fn invalidates_all_descendants() {
|
||||
let num_blocks = E::slots_per_epoch() * 4 + E::slots_per_epoch() / 2;
|
||||
let finalized_epoch = 2;
|
||||
let finalized_slot = E::slots_per_epoch() * 2;
|
||||
|
||||
let mut rig = InvalidPayloadRig::new().enable_attestations();
|
||||
rig.move_to_terminal_block();
|
||||
let blocks = rig.build_blocks(num_blocks, Payload::Syncing);
|
||||
|
||||
assert_eq!(rig.head_info().finalized_checkpoint.epoch, finalized_epoch);
|
||||
assert_eq!(rig.head_info().block_root, *blocks.last().unwrap());
|
||||
|
||||
// Apply a block which conflicts with the canonical chain.
|
||||
let fork_slot = Slot::new(4 * E::slots_per_epoch() + 3);
|
||||
let fork_parent_slot = fork_slot - 1;
|
||||
let fork_parent_state = rig
|
||||
.harness
|
||||
.chain
|
||||
.state_at_slot(fork_parent_slot, StateSkipConfig::WithStateRoots)
|
||||
.unwrap();
|
||||
assert_eq!(fork_parent_state.slot(), fork_parent_slot);
|
||||
let (fork_block, _fork_post_state) = rig.harness.make_block(fork_parent_state, fork_slot);
|
||||
let fork_block_root = rig.harness.chain.process_block(fork_block).unwrap();
|
||||
rig.fork_choice();
|
||||
|
||||
// The latest valid hash will be set to the grandparent of the fork block. This means that the
|
||||
// parent of the fork block will become invalid.
|
||||
let latest_valid_slot = fork_parent_slot - 1;
|
||||
let latest_valid_root = rig
|
||||
.harness
|
||||
.chain
|
||||
.block_root_at_slot(latest_valid_slot, WhenSlotSkipped::None)
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
assert!(blocks.contains(&latest_valid_root));
|
||||
let latest_valid_hash = rig.block_hash(latest_valid_root);
|
||||
|
||||
// The new block should not become the head, the old head should remain.
|
||||
assert_eq!(rig.head_info().block_root, *blocks.last().unwrap());
|
||||
|
||||
rig.import_block(Payload::Invalid {
|
||||
latest_valid_hash: Some(latest_valid_hash),
|
||||
});
|
||||
|
||||
// The block before the fork should become the head.
|
||||
assert_eq!(rig.head_info().block_root, latest_valid_root);
|
||||
|
||||
// The fork block should be invalidated, even though it's not an ancestor of the block that
|
||||
// triggered the INVALID response from the EL.
|
||||
assert!(rig.execution_status(fork_block_root).is_invalid());
|
||||
|
||||
for root in blocks {
|
||||
let slot = rig.harness.chain.get_block(&root).unwrap().unwrap().slot();
|
||||
|
||||
// Fork choice doesn't have info about pre-finalization, nothing to check here.
|
||||
if slot < finalized_slot {
|
||||
continue;
|
||||
}
|
||||
|
||||
let execution_status = rig.execution_status(root);
|
||||
if slot <= latest_valid_slot {
|
||||
// Blocks prior to the latest valid hash are valid.
|
||||
assert!(execution_status.is_valid());
|
||||
} else {
|
||||
// Blocks after the latest valid hash are invalid.
|
||||
assert!(execution_status.is_invalid());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Check that the head will switch after the canonical branch is invalidated.
|
||||
#[test]
|
||||
fn switches_heads() {
|
||||
let num_blocks = E::slots_per_epoch() * 4 + E::slots_per_epoch() / 2;
|
||||
let finalized_epoch = 2;
|
||||
let finalized_slot = E::slots_per_epoch() * 2;
|
||||
|
||||
let mut rig = InvalidPayloadRig::new().enable_attestations();
|
||||
rig.move_to_terminal_block();
|
||||
let blocks = rig.build_blocks(num_blocks, Payload::Syncing);
|
||||
|
||||
assert_eq!(rig.head_info().finalized_checkpoint.epoch, finalized_epoch);
|
||||
assert_eq!(rig.head_info().block_root, *blocks.last().unwrap());
|
||||
|
||||
// Apply a block which conflicts with the canonical chain.
|
||||
let fork_slot = Slot::new(4 * E::slots_per_epoch() + 3);
|
||||
let fork_parent_slot = fork_slot - 1;
|
||||
let fork_parent_state = rig
|
||||
.harness
|
||||
.chain
|
||||
.state_at_slot(fork_parent_slot, StateSkipConfig::WithStateRoots)
|
||||
.unwrap();
|
||||
assert_eq!(fork_parent_state.slot(), fork_parent_slot);
|
||||
let (fork_block, _fork_post_state) = rig.harness.make_block(fork_parent_state, fork_slot);
|
||||
let fork_parent_root = fork_block.parent_root();
|
||||
let fork_block_root = rig.harness.chain.process_block(fork_block).unwrap();
|
||||
rig.fork_choice();
|
||||
|
||||
let latest_valid_slot = fork_parent_slot;
|
||||
let latest_valid_hash = rig.block_hash(fork_parent_root);
|
||||
|
||||
// The new block should not become the head, the old head should remain.
|
||||
assert_eq!(rig.head_info().block_root, *blocks.last().unwrap());
|
||||
|
||||
rig.import_block(Payload::Invalid {
|
||||
latest_valid_hash: Some(latest_valid_hash),
|
||||
});
|
||||
|
||||
// The fork block should become the head.
|
||||
assert_eq!(rig.head_info().block_root, fork_block_root);
|
||||
|
||||
// The fork block has not yet been validated.
|
||||
assert!(rig.execution_status(fork_block_root).is_not_verified());
|
||||
|
||||
for root in blocks {
|
||||
let slot = rig.harness.chain.get_block(&root).unwrap().unwrap().slot();
|
||||
|
||||
// Fork choice doesn't have info about pre-finalization, nothing to check here.
|
||||
if slot < finalized_slot {
|
||||
continue;
|
||||
}
|
||||
|
||||
let execution_status = rig.execution_status(root);
|
||||
if slot <= latest_valid_slot {
|
||||
// Blocks prior to the latest valid hash are valid.
|
||||
assert!(execution_status.is_valid());
|
||||
} else {
|
||||
// Blocks after the latest valid hash are invalid.
|
||||
assert!(execution_status.is_invalid());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn invalid_during_processing() {
|
||||
let mut rig = InvalidPayloadRig::new();
|
||||
rig.move_to_terminal_block();
|
||||
|
||||
let roots = &[
|
||||
rig.import_block(Payload::Valid),
|
||||
rig.import_block(Payload::Invalid {
|
||||
latest_valid_hash: None,
|
||||
}),
|
||||
rig.import_block(Payload::Valid),
|
||||
];
|
||||
|
||||
// 0 should be present in the chain.
|
||||
assert!(rig.harness.chain.get_block(&roots[0]).unwrap().is_some());
|
||||
// 1 should *not* be present in the chain.
|
||||
assert_eq!(rig.harness.chain.get_block(&roots[1]).unwrap(), None);
|
||||
// 2 should be the head.
|
||||
let head = rig.harness.chain.head_info().unwrap();
|
||||
assert_eq!(head.block_root, roots[2]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn invalid_after_optimistic_sync() {
|
||||
let mut rig = InvalidPayloadRig::new();
|
||||
rig.move_to_terminal_block();
|
||||
|
||||
let mut roots = vec![
|
||||
rig.import_block(Payload::Syncing),
|
||||
rig.import_block(Payload::Syncing),
|
||||
rig.import_block(Payload::Syncing),
|
||||
];
|
||||
|
||||
for root in &roots {
|
||||
assert!(rig.harness.chain.get_block(root).unwrap().is_some());
|
||||
}
|
||||
|
||||
// 2 should be the head.
|
||||
let head = rig.harness.chain.head_info().unwrap();
|
||||
assert_eq!(head.block_root, roots[2]);
|
||||
|
||||
roots.push(rig.import_block(Payload::Invalid {
|
||||
latest_valid_hash: Some(rig.block_hash(roots[1])),
|
||||
}));
|
||||
|
||||
// Running fork choice is necessary since a block has been invalidated.
|
||||
rig.fork_choice();
|
||||
|
||||
// 1 should be the head, since 2 was invalidated.
|
||||
let head = rig.harness.chain.head_info().unwrap();
|
||||
assert_eq!(head.block_root, roots[1]);
|
||||
}
|
||||
@@ -66,6 +66,7 @@ fn get_harness(
|
||||
.default_spec()
|
||||
.keypairs(KEYPAIRS[0..validator_count].to_vec())
|
||||
.fresh_disk_store(store)
|
||||
.mock_execution_layer()
|
||||
.build();
|
||||
harness.advance_slot();
|
||||
harness
|
||||
@@ -554,6 +555,7 @@ fn delete_blocks_and_states() {
|
||||
.default_spec()
|
||||
.keypairs(validators_keypairs)
|
||||
.fresh_disk_store(store.clone())
|
||||
.mock_execution_layer()
|
||||
.build();
|
||||
|
||||
let unforked_blocks: u64 = 4 * E::slots_per_epoch();
|
||||
@@ -680,6 +682,7 @@ fn multi_epoch_fork_valid_blocks_test(
|
||||
.default_spec()
|
||||
.keypairs(validators_keypairs)
|
||||
.fresh_disk_store(store)
|
||||
.mock_execution_layer()
|
||||
.build();
|
||||
|
||||
let num_fork1_blocks: u64 = num_fork1_blocks_.try_into().unwrap();
|
||||
@@ -974,6 +977,7 @@ fn prunes_abandoned_fork_between_two_finalized_checkpoints() {
|
||||
.default_spec()
|
||||
.keypairs(validators_keypairs)
|
||||
.fresh_ephemeral_store()
|
||||
.mock_execution_layer()
|
||||
.build();
|
||||
let slots_per_epoch = rig.slots_per_epoch();
|
||||
let (mut state, state_root) = rig.get_current_state_and_root();
|
||||
@@ -1083,6 +1087,7 @@ fn pruning_does_not_touch_abandoned_block_shared_with_canonical_chain() {
|
||||
.default_spec()
|
||||
.keypairs(validators_keypairs)
|
||||
.fresh_ephemeral_store()
|
||||
.mock_execution_layer()
|
||||
.build();
|
||||
let slots_per_epoch = rig.slots_per_epoch();
|
||||
let (state, state_root) = rig.get_current_state_and_root();
|
||||
@@ -1212,6 +1217,7 @@ fn pruning_does_not_touch_blocks_prior_to_finalization() {
|
||||
.default_spec()
|
||||
.keypairs(validators_keypairs)
|
||||
.fresh_ephemeral_store()
|
||||
.mock_execution_layer()
|
||||
.build();
|
||||
let slots_per_epoch = rig.slots_per_epoch();
|
||||
let (mut state, state_root) = rig.get_current_state_and_root();
|
||||
@@ -1306,6 +1312,7 @@ fn prunes_fork_growing_past_youngest_finalized_checkpoint() {
|
||||
.default_spec()
|
||||
.keypairs(validators_keypairs)
|
||||
.fresh_ephemeral_store()
|
||||
.mock_execution_layer()
|
||||
.build();
|
||||
let (state, state_root) = rig.get_current_state_and_root();
|
||||
|
||||
@@ -1448,6 +1455,7 @@ fn prunes_skipped_slots_states() {
|
||||
.default_spec()
|
||||
.keypairs(validators_keypairs)
|
||||
.fresh_ephemeral_store()
|
||||
.mock_execution_layer()
|
||||
.build();
|
||||
let (state, state_root) = rig.get_current_state_and_root();
|
||||
|
||||
@@ -1571,6 +1579,7 @@ fn finalizes_non_epoch_start_slot() {
|
||||
.default_spec()
|
||||
.keypairs(validators_keypairs)
|
||||
.fresh_ephemeral_store()
|
||||
.mock_execution_layer()
|
||||
.build();
|
||||
let (state, state_root) = rig.get_current_state_and_root();
|
||||
|
||||
@@ -2140,6 +2149,7 @@ fn finalizes_after_resuming_from_db() {
|
||||
.default_spec()
|
||||
.keypairs(KEYPAIRS[0..validator_count].to_vec())
|
||||
.fresh_disk_store(store.clone())
|
||||
.mock_execution_layer()
|
||||
.build();
|
||||
|
||||
harness.advance_slot();
|
||||
@@ -2183,6 +2193,7 @@ fn finalizes_after_resuming_from_db() {
|
||||
.default_spec()
|
||||
.keypairs(KEYPAIRS[0..validator_count].to_vec())
|
||||
.resumed_disk_store(store)
|
||||
.mock_execution_layer()
|
||||
.build();
|
||||
|
||||
assert_chains_pretty_much_the_same(&original_chain, &resumed_harness.chain);
|
||||
@@ -2254,6 +2265,7 @@ fn revert_minority_fork_on_resume() {
|
||||
.spec(spec1)
|
||||
.keypairs(KEYPAIRS[0..validator_count].to_vec())
|
||||
.fresh_disk_store(store1)
|
||||
.mock_execution_layer()
|
||||
.build();
|
||||
|
||||
// Chain with fork epoch configured.
|
||||
@@ -2263,6 +2275,7 @@ fn revert_minority_fork_on_resume() {
|
||||
.spec(spec2.clone())
|
||||
.keypairs(KEYPAIRS[0..validator_count].to_vec())
|
||||
.fresh_disk_store(store2)
|
||||
.mock_execution_layer()
|
||||
.build();
|
||||
|
||||
// Apply the same blocks to both chains initially.
|
||||
@@ -2358,6 +2371,7 @@ fn revert_minority_fork_on_resume() {
|
||||
.set_slot(end_slot.as_u64());
|
||||
builder
|
||||
}))
|
||||
.mock_execution_layer()
|
||||
.build();
|
||||
|
||||
// Head should now be just before the fork.
|
||||
|
||||
@@ -30,6 +30,7 @@ fn get_harness(validator_count: usize) -> BeaconChainHarness<EphemeralHarnessTyp
|
||||
.spec(spec)
|
||||
.keypairs(KEYPAIRS[0..validator_count].to_vec())
|
||||
.fresh_ephemeral_store()
|
||||
.mock_execution_layer()
|
||||
.build();
|
||||
|
||||
harness.advance_slot();
|
||||
|
||||
@@ -28,6 +28,7 @@ fn get_harness(validator_count: usize) -> BeaconChainHarness<EphemeralHarnessTyp
|
||||
.default_spec()
|
||||
.keypairs(KEYPAIRS[0..validator_count].to_vec())
|
||||
.fresh_ephemeral_store()
|
||||
.mock_execution_layer()
|
||||
.build();
|
||||
|
||||
harness.advance_slot();
|
||||
|
||||
Reference in New Issue
Block a user