## Overview
This rather extensive PR achieves two primary goals:
1. Uses the finalized/justified checkpoints of fork choice (FC), rather than that of the head state.
2. Refactors fork choice, block production and block processing to `async` functions.
Additionally, it achieves:
- Concurrent forkchoice updates to the EL and cache pruning after a new head is selected.
- Concurrent "block packing" (attestations, etc) and execution payload retrieval during block production.
- Concurrent per-block-processing and execution payload verification during block processing.
- The `Arc`-ification of `SignedBeaconBlock` during block processing (it's never mutated, so why not?):
- I had to do this to deal with sending blocks into spawned tasks.
- Previously we were cloning the beacon block at least 2 times during each block processing, these clones are either removed or turned into cheaper `Arc` clones.
- We were also `Box`-ing and un-`Box`-ing beacon blocks as they moved throughout the networking crate. This is not a big deal, but it's nice to avoid shifting things between the stack and heap.
- Avoids cloning *all the blocks* in *every chain segment* during sync.
- It also has the potential to clean up our code where we need to pass an *owned* block around so we can send it back in the case of an error (I didn't do much of this, my PR is already big enough 😅)
- The `BeaconChain::HeadSafetyStatus` struct was removed. It was an old relic from prior merge specs.
For motivation for this change, see https://github.com/sigp/lighthouse/pull/3244#issuecomment-1160963273
## Changes to `canonical_head` and `fork_choice`
Previously, the `BeaconChain` had two separate fields:
```
canonical_head: RwLock<Snapshot>,
fork_choice: RwLock<BeaconForkChoice>
```
Now, we have grouped these values under a single struct:
```
canonical_head: CanonicalHead {
cached_head: RwLock<Arc<Snapshot>>,
fork_choice: RwLock<BeaconForkChoice>
}
```
Apart from ergonomics, the only *actual* change here is wrapping the canonical head snapshot in an `Arc`. This means that we no longer need to hold the `cached_head` (`canonical_head`, in old terms) lock when we want to pull some values from it. This was done to avoid deadlock risks by preventing functions from acquiring (and holding) the `cached_head` and `fork_choice` locks simultaneously.
## Breaking Changes
### The `state` (root) field in the `finalized_checkpoint` SSE event
Consider the scenario where epoch `n` is just finalized, but `start_slot(n)` is skipped. There are two state roots we might in the `finalized_checkpoint` SSE event:
1. The state root of the finalized block, which is `get_block(finalized_checkpoint.root).state_root`.
4. The state root at slot of `start_slot(n)`, which would be the state from (1), but "skipped forward" through any skip slots.
Previously, Lighthouse would choose (2). However, we can see that when [Teku generates that event](de2b2801c8/data/beaconrestapi/src/main/java/tech/pegasys/teku/beaconrestapi/handlers/v1/events/EventSubscriptionManager.java (L171-L182)) it uses [`getStateRootFromBlockRoot`](de2b2801c8/data/provider/src/main/java/tech/pegasys/teku/api/ChainDataProvider.java (L336-L341)) which uses (1).
I have switched Lighthouse from (2) to (1). I think it's a somewhat arbitrary choice between the two, where (1) is easier to compute and is consistent with Teku.
## Notes for Reviewers
I've renamed `BeaconChain::fork_choice` to `BeaconChain::recompute_head`. Doing this helped ensure I broke all previous uses of fork choice and I also find it more descriptive. It describes an action and can't be confused with trying to get a reference to the `ForkChoice` struct.
I've changed the ordering of SSE events when a block is received. It used to be `[block, finalized, head]` and now it's `[block, head, finalized]`. It was easier this way and I don't think we were making any promises about SSE event ordering so it's not "breaking".
I've made it so fork choice will run when it's first constructed. I did this because I wanted to have a cached version of the last call to `get_head`. Ensuring `get_head` has been run *at least once* means that the cached values doesn't need to wrapped in an `Option`. This was fairly simple, it just involved passing a `slot` to the constructor so it knows *when* it's being run. When loading a fork choice from the store and a slot clock isn't handy I've just used the `slot` that was saved in the `fork_choice_store`. That seems like it would be a faithful representation of the slot when we saved it.
I added the `genesis_time: u64` to the `BeaconChain`. It's small, constant and nice to have around.
Since we're using FC for the fin/just checkpoints, we no longer get the `0x00..00` roots at genesis. You can see I had to remove a work-around in `ef-tests` here: b56be3bc2. I can't find any reason why this would be an issue, if anything I think it'll be better since the genesis-alias has caught us out a few times (0x00..00 isn't actually a real root). Edit: I did find a case where the `network` expected the 0x00..00 alias and patched it here: 3f26ac3e2.
You'll notice a lot of changes in tests. Generally, tests should be functionally equivalent. Here are the things creating the most diff-noise in tests:
- Changing tests to be `tokio::async` tests.
- Adding `.await` to fork choice, block processing and block production functions.
- Refactor of the `canonical_head` "API" provided by the `BeaconChain`. E.g., `chain.canonical_head.cached_head()` instead of `chain.canonical_head.read()`.
- Wrapping `SignedBeaconBlock` in an `Arc`.
- In the `beacon_chain/tests/block_verification`, we can't use the `lazy_static` `CHAIN_SEGMENT` variable anymore since it's generated with an async function. We just generate it in each test, not so efficient but hopefully insignificant.
I had to disable `rayon` concurrent tests in the `fork_choice` tests. This is because the use of `rayon` and `block_on` was causing a panic.
Co-authored-by: Mac L <mjladson@pm.me>
425 lines
13 KiB
Rust
425 lines
13 KiB
Rust
use crate::execution_engine::{ExecutionEngine, GenericExecutionEngine};
|
|
use execution_layer::{ExecutionLayer, PayloadAttributes, PayloadStatus};
|
|
use std::sync::Arc;
|
|
use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH};
|
|
use task_executor::TaskExecutor;
|
|
use tokio::time::sleep;
|
|
use types::{
|
|
Address, ChainSpec, EthSpec, ExecutionBlockHash, ExecutionPayload, FullPayload, Hash256,
|
|
MainnetEthSpec, Slot, Uint256,
|
|
};
|
|
|
|
const EXECUTION_ENGINE_START_TIMEOUT: Duration = Duration::from_secs(20);
|
|
|
|
struct ExecutionPair<E, T: EthSpec> {
|
|
/// The Lighthouse `ExecutionLayer` struct, connected to the `execution_engine` via HTTP.
|
|
execution_layer: ExecutionLayer<T>,
|
|
/// A handle to external EE process, once this is dropped the process will be killed.
|
|
#[allow(dead_code)]
|
|
execution_engine: ExecutionEngine<E>,
|
|
}
|
|
|
|
/// A rig that holds two EE processes for testing.
|
|
///
|
|
/// There are two EEs held here so that we can test out-of-order application of payloads, and other
|
|
/// edge-cases.
|
|
pub struct TestRig<E, T: EthSpec = MainnetEthSpec> {
|
|
#[allow(dead_code)]
|
|
runtime: Arc<tokio::runtime::Runtime>,
|
|
ee_a: ExecutionPair<E, T>,
|
|
ee_b: ExecutionPair<E, T>,
|
|
spec: ChainSpec,
|
|
_runtime_shutdown: exit_future::Signal,
|
|
}
|
|
|
|
impl<E: GenericExecutionEngine> TestRig<E> {
|
|
pub fn new(generic_engine: E) -> Self {
|
|
let log = environment::null_logger().unwrap();
|
|
let runtime = Arc::new(
|
|
tokio::runtime::Builder::new_multi_thread()
|
|
.enable_all()
|
|
.build()
|
|
.unwrap(),
|
|
);
|
|
let (runtime_shutdown, exit) = exit_future::signal();
|
|
let (shutdown_tx, _) = futures::channel::mpsc::channel(1);
|
|
let executor = TaskExecutor::new(Arc::downgrade(&runtime), exit, log.clone(), shutdown_tx);
|
|
|
|
let fee_recipient = None;
|
|
|
|
let ee_a = {
|
|
let execution_engine = ExecutionEngine::new(generic_engine.clone());
|
|
let urls = vec![execution_engine.http_auth_url()];
|
|
|
|
let config = execution_layer::Config {
|
|
execution_endpoints: urls,
|
|
secret_files: vec![],
|
|
suggested_fee_recipient: Some(Address::repeat_byte(42)),
|
|
default_datadir: execution_engine.datadir(),
|
|
..Default::default()
|
|
};
|
|
let execution_layer =
|
|
ExecutionLayer::from_config(config, executor.clone(), log.clone()).unwrap();
|
|
ExecutionPair {
|
|
execution_engine,
|
|
execution_layer,
|
|
}
|
|
};
|
|
|
|
let ee_b = {
|
|
let execution_engine = ExecutionEngine::new(generic_engine);
|
|
let urls = vec![execution_engine.http_auth_url()];
|
|
|
|
let config = execution_layer::Config {
|
|
execution_endpoints: urls,
|
|
secret_files: vec![],
|
|
suggested_fee_recipient: fee_recipient,
|
|
default_datadir: execution_engine.datadir(),
|
|
..Default::default()
|
|
};
|
|
let execution_layer =
|
|
ExecutionLayer::from_config(config, executor, log.clone()).unwrap();
|
|
ExecutionPair {
|
|
execution_engine,
|
|
execution_layer,
|
|
}
|
|
};
|
|
|
|
let mut spec = MainnetEthSpec::default_spec();
|
|
spec.terminal_total_difficulty = Uint256::zero();
|
|
|
|
Self {
|
|
runtime,
|
|
ee_a,
|
|
ee_b,
|
|
spec,
|
|
_runtime_shutdown: runtime_shutdown,
|
|
}
|
|
}
|
|
|
|
pub fn perform_tests_blocking(&self) {
|
|
self.runtime
|
|
.handle()
|
|
.block_on(async { self.perform_tests().await });
|
|
}
|
|
|
|
pub async fn wait_until_synced(&self) {
|
|
let start_instant = Instant::now();
|
|
|
|
for pair in [&self.ee_a, &self.ee_b] {
|
|
loop {
|
|
// Run the routine to check for online nodes.
|
|
pair.execution_layer.watchdog_task().await;
|
|
|
|
if pair.execution_layer.is_synced().await {
|
|
break;
|
|
} else if start_instant + EXECUTION_ENGINE_START_TIMEOUT > Instant::now() {
|
|
sleep(Duration::from_millis(500)).await;
|
|
} else {
|
|
panic!("timeout waiting for execution engines to come online")
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
pub async fn perform_tests(&self) {
|
|
self.wait_until_synced().await;
|
|
|
|
/*
|
|
* Check the transition config endpoint.
|
|
*/
|
|
for ee in [&self.ee_a, &self.ee_b] {
|
|
ee.execution_layer
|
|
.exchange_transition_configuration(&self.spec)
|
|
.await
|
|
.unwrap();
|
|
}
|
|
|
|
/*
|
|
* Read the terminal block hash from both pairs, check it's equal.
|
|
*/
|
|
|
|
let terminal_pow_block_hash = self
|
|
.ee_a
|
|
.execution_layer
|
|
.get_terminal_pow_block_hash(&self.spec)
|
|
.await
|
|
.unwrap()
|
|
.unwrap();
|
|
|
|
assert_eq!(
|
|
terminal_pow_block_hash,
|
|
self.ee_b
|
|
.execution_layer
|
|
.get_terminal_pow_block_hash(&self.spec)
|
|
.await
|
|
.unwrap()
|
|
.unwrap()
|
|
);
|
|
|
|
/*
|
|
* Execution Engine A:
|
|
*
|
|
* Produce a valid payload atop the terminal block.
|
|
*/
|
|
|
|
let parent_hash = terminal_pow_block_hash;
|
|
let timestamp = timestamp_now();
|
|
let prev_randao = Hash256::zero();
|
|
let finalized_block_hash = ExecutionBlockHash::zero();
|
|
let proposer_index = 0;
|
|
let valid_payload = self
|
|
.ee_a
|
|
.execution_layer
|
|
.get_payload::<FullPayload<MainnetEthSpec>>(
|
|
parent_hash,
|
|
timestamp,
|
|
prev_randao,
|
|
finalized_block_hash,
|
|
proposer_index,
|
|
None,
|
|
Slot::new(0),
|
|
)
|
|
.await
|
|
.unwrap()
|
|
.execution_payload;
|
|
|
|
/*
|
|
* Execution Engine A:
|
|
*
|
|
* Indicate that the payload is the head of the chain, before submitting a
|
|
* `notify_new_payload`.
|
|
*/
|
|
let head_block_hash = valid_payload.block_hash;
|
|
let finalized_block_hash = ExecutionBlockHash::zero();
|
|
let slot = Slot::new(42);
|
|
let head_block_root = Hash256::repeat_byte(42);
|
|
let status = self
|
|
.ee_a
|
|
.execution_layer
|
|
.notify_forkchoice_updated(head_block_hash, finalized_block_hash, slot, head_block_root)
|
|
.await
|
|
.unwrap();
|
|
assert_eq!(status, PayloadStatus::Syncing);
|
|
|
|
/*
|
|
* Execution Engine A:
|
|
*
|
|
* Provide the valid payload back to the EE again.
|
|
*/
|
|
|
|
let status = self
|
|
.ee_a
|
|
.execution_layer
|
|
.notify_new_payload(&valid_payload)
|
|
.await
|
|
.unwrap();
|
|
assert_eq!(status, PayloadStatus::Valid);
|
|
check_payload_reconstruction(&self.ee_a, &valid_payload).await;
|
|
|
|
/*
|
|
* Execution Engine A:
|
|
*
|
|
* Indicate that the payload is the head of the chain.
|
|
*
|
|
* Do not provide payload attributes (we'll test that later).
|
|
*/
|
|
let head_block_hash = valid_payload.block_hash;
|
|
let finalized_block_hash = ExecutionBlockHash::zero();
|
|
let slot = Slot::new(42);
|
|
let head_block_root = Hash256::repeat_byte(42);
|
|
let status = self
|
|
.ee_a
|
|
.execution_layer
|
|
.notify_forkchoice_updated(head_block_hash, finalized_block_hash, slot, head_block_root)
|
|
.await
|
|
.unwrap();
|
|
assert_eq!(status, PayloadStatus::Valid);
|
|
|
|
/*
|
|
* Execution Engine A:
|
|
*
|
|
* Provide an invalidated payload to the EE.
|
|
*/
|
|
|
|
let mut invalid_payload = valid_payload.clone();
|
|
invalid_payload.prev_randao = Hash256::from_low_u64_be(42);
|
|
let status = self
|
|
.ee_a
|
|
.execution_layer
|
|
.notify_new_payload(&invalid_payload)
|
|
.await
|
|
.unwrap();
|
|
assert!(matches!(status, PayloadStatus::InvalidBlockHash { .. }));
|
|
|
|
/*
|
|
* Execution Engine A:
|
|
*
|
|
* Produce another payload atop the previous one.
|
|
*/
|
|
|
|
let parent_hash = valid_payload.block_hash;
|
|
let timestamp = valid_payload.timestamp + 1;
|
|
let prev_randao = Hash256::zero();
|
|
let finalized_block_hash = ExecutionBlockHash::zero();
|
|
let proposer_index = 0;
|
|
let second_payload = self
|
|
.ee_a
|
|
.execution_layer
|
|
.get_payload::<FullPayload<MainnetEthSpec>>(
|
|
parent_hash,
|
|
timestamp,
|
|
prev_randao,
|
|
finalized_block_hash,
|
|
proposer_index,
|
|
None,
|
|
Slot::new(0),
|
|
)
|
|
.await
|
|
.unwrap()
|
|
.execution_payload;
|
|
|
|
/*
|
|
* Execution Engine A:
|
|
*
|
|
* Provide the second payload back to the EE again.
|
|
*/
|
|
|
|
let status = self
|
|
.ee_a
|
|
.execution_layer
|
|
.notify_new_payload(&second_payload)
|
|
.await
|
|
.unwrap();
|
|
assert_eq!(status, PayloadStatus::Valid);
|
|
check_payload_reconstruction(&self.ee_a, &second_payload).await;
|
|
|
|
/*
|
|
* Execution Engine A:
|
|
*
|
|
* Indicate that the payload is the head of the chain, providing payload attributes.
|
|
*/
|
|
let head_block_hash = valid_payload.block_hash;
|
|
let finalized_block_hash = ExecutionBlockHash::zero();
|
|
let payload_attributes = PayloadAttributes {
|
|
timestamp: second_payload.timestamp + 1,
|
|
prev_randao: Hash256::zero(),
|
|
suggested_fee_recipient: Address::zero(),
|
|
};
|
|
let slot = Slot::new(42);
|
|
let head_block_root = Hash256::repeat_byte(100);
|
|
let validator_index = 0;
|
|
self.ee_a
|
|
.execution_layer
|
|
.insert_proposer(slot, head_block_root, validator_index, payload_attributes)
|
|
.await;
|
|
let status = self
|
|
.ee_a
|
|
.execution_layer
|
|
.notify_forkchoice_updated(head_block_hash, finalized_block_hash, slot, head_block_root)
|
|
.await
|
|
.unwrap();
|
|
assert_eq!(status, PayloadStatus::Valid);
|
|
|
|
/*
|
|
* Execution Engine B:
|
|
*
|
|
* Provide the second payload, without providing the first.
|
|
*/
|
|
let status = self
|
|
.ee_b
|
|
.execution_layer
|
|
.notify_new_payload(&second_payload)
|
|
.await
|
|
.unwrap();
|
|
assert_eq!(status, PayloadStatus::Accepted);
|
|
|
|
/*
|
|
* Execution Engine B:
|
|
*
|
|
* Set the second payload as the head, without providing payload attributes.
|
|
*/
|
|
let head_block_hash = second_payload.block_hash;
|
|
let finalized_block_hash = ExecutionBlockHash::zero();
|
|
let slot = Slot::new(42);
|
|
let head_block_root = Hash256::repeat_byte(42);
|
|
let status = self
|
|
.ee_b
|
|
.execution_layer
|
|
.notify_forkchoice_updated(head_block_hash, finalized_block_hash, slot, head_block_root)
|
|
.await
|
|
.unwrap();
|
|
assert_eq!(status, PayloadStatus::Syncing);
|
|
|
|
/*
|
|
* Execution Engine B:
|
|
*
|
|
* Provide the first payload to the EE.
|
|
*/
|
|
|
|
let status = self
|
|
.ee_b
|
|
.execution_layer
|
|
.notify_new_payload(&valid_payload)
|
|
.await
|
|
.unwrap();
|
|
assert_eq!(status, PayloadStatus::Valid);
|
|
check_payload_reconstruction(&self.ee_b, &valid_payload).await;
|
|
|
|
/*
|
|
* Execution Engine B:
|
|
*
|
|
* Provide the second payload, now the first has been provided.
|
|
*/
|
|
let status = self
|
|
.ee_b
|
|
.execution_layer
|
|
.notify_new_payload(&second_payload)
|
|
.await
|
|
.unwrap();
|
|
assert_eq!(status, PayloadStatus::Valid);
|
|
check_payload_reconstruction(&self.ee_b, &second_payload).await;
|
|
|
|
/*
|
|
* Execution Engine B:
|
|
*
|
|
* Set the second payload as the head, without providing payload attributes.
|
|
*/
|
|
let head_block_hash = second_payload.block_hash;
|
|
let finalized_block_hash = ExecutionBlockHash::zero();
|
|
let slot = Slot::new(42);
|
|
let head_block_root = Hash256::repeat_byte(42);
|
|
let status = self
|
|
.ee_b
|
|
.execution_layer
|
|
.notify_forkchoice_updated(head_block_hash, finalized_block_hash, slot, head_block_root)
|
|
.await
|
|
.unwrap();
|
|
assert_eq!(status, PayloadStatus::Valid);
|
|
}
|
|
}
|
|
|
|
/// Check that the given payload can be re-constructed by fetching it from the EE.
|
|
///
|
|
/// Panic if payload reconstruction fails.
|
|
async fn check_payload_reconstruction<E: GenericExecutionEngine>(
|
|
ee: &ExecutionPair<E, MainnetEthSpec>,
|
|
payload: &ExecutionPayload<MainnetEthSpec>,
|
|
) {
|
|
let reconstructed = ee
|
|
.execution_layer
|
|
.get_payload_by_block_hash(payload.block_hash)
|
|
.await
|
|
.unwrap()
|
|
.unwrap();
|
|
assert_eq!(reconstructed, *payload);
|
|
}
|
|
|
|
/// Returns the duration since the unix epoch.
|
|
pub fn timestamp_now() -> u64 {
|
|
SystemTime::now()
|
|
.duration_since(UNIX_EPOCH)
|
|
.unwrap_or_else(|_| Duration::from_secs(0))
|
|
.as_secs()
|
|
}
|