Tree-sync friendly lookup sync tests (#8592)

- Step 0 of the tree-sync roadmap https://github.com/sigp/lighthouse/issues/7678

Current lookup sync tests are written in an explicit way that assume how the internals of lookup sync work. For example the test would do:

- Emit unknown block parent message
- Expect block request for X
- Respond with successful block request
- Expect block processing request for X
- Response with successful processing request
- etc..

This is unnecessarily verbose. And it will requires a complete re-write when something changes in the internals of lookup sync (has happened a few times, mostly for deneb and fulu).

What we really want to assert is:

- WHEN: we receive an unknown block parent message
- THEN: Lookup sync can sync that block
- ASSERT: Without penalizing peers, without unnecessary retries


  Keep all existing tests and add new cases but written in the new style described above. The logic to serve and respond to request is in this function `fn simulate` 2288a3aeb1/beacon_node/network/src/sync/tests/lookups.rs (L301)
- It controls peer behavior based on a `CompleteStrategy` where you can set for example "respond to BlocksByRoot requests with empty"
- It actually runs beacon processor messages running their clousures. Now sync tests actually import blocks, increasing the test coverage to the interaction of sync and the da_checker.
- To achieve the above the tests create real blocks with the test harness. To make the tests as fast as before, I disabled crypto with `TestConfig`

Along the way I found a couple bugs, which I documented on the diff.


Co-Authored-By: dapplion <35266934+dapplion@users.noreply.github.com>
This commit is contained in:
Lion - dapplion
2026-02-12 21:24:51 -07:00
committed by GitHub
parent c59e4a0cee
commit f4a6b8d9b9
27 changed files with 2298 additions and 2381 deletions

View File

@@ -1,13 +1,19 @@
use crate::NetworkMessage;
use crate::sync::SyncMessage;
use crate::sync::block_lookups::BlockLookupsMetrics;
use crate::sync::manager::SyncManager;
use crate::sync::range_sync::RangeSyncType;
use crate::sync::tests::lookups::SimulateConfig;
use beacon_chain::block_verification_types::RpcBlock;
use beacon_chain::builder::Witness;
use beacon_chain::custody_context::NodeCustodyType;
use beacon_chain::test_utils::{BeaconChainHarness, EphemeralHarnessType};
use beacon_processor::WorkEvent;
use lighthouse_network::NetworkGlobals;
use lighthouse_network::rpc::RequestType;
use lighthouse_network::service::api_types::{AppRequestId, Id};
use lighthouse_network::{NetworkGlobals, PeerId};
use rand_chacha::ChaCha20Rng;
use slot_clock::ManualSlotClock;
use std::collections::{HashMap, HashSet};
use std::fs::OpenOptions;
use std::io::Write;
use std::sync::{Arc, Once};
@@ -16,7 +22,7 @@ use tokio::sync::mpsc;
use tracing_subscriber::fmt::MakeWriter;
use tracing_subscriber::layer::SubscriberExt;
use tracing_subscriber::util::SubscriberInitExt;
use types::{ForkName, MinimalEthSpec as E};
use types::{ForkName, Hash256, MinimalEthSpec as E, Slot};
mod lookups;
mod range;
@@ -58,6 +64,8 @@ struct TestRig {
network_rx_queue: Vec<NetworkMessage<E>>,
/// Receiver for `SyncMessage` from the network
sync_rx: mpsc::UnboundedReceiver<SyncMessage<E>>,
/// Stores all `SyncMessage`s received from `sync_rx`
sync_rx_queue: Vec<SyncMessage<E>>,
/// To send `SyncMessage`. For sending RPC responses or block processing results to sync.
sync_manager: SyncManager<T>,
/// To manipulate sync state and peer connection status
@@ -68,6 +76,65 @@ struct TestRig {
rng_08: rand_chacha_03::ChaCha20Rng,
rng: ChaCha20Rng,
fork_name: ForkName,
/// Blocks that will be used in the test but may not be known to `harness` yet.
network_blocks_by_root: HashMap<Hash256, RpcBlock<E>>,
network_blocks_by_slot: HashMap<Slot, RpcBlock<E>>,
penalties: Vec<ReportedPenalty>,
/// All seen lookups through the test run
seen_lookups: HashMap<Id, SeenLookup>,
/// Registry of all requests done by the test
requests: Vec<(RequestType<E>, AppRequestId)>,
/// Persistent config on how to complete request
complete_strategy: SimulateConfig,
/// Metrics values to allow a reset
initial_block_lookups_metrics: BlockLookupsMetrics,
/// Fulu test type
fulu_test_type: FuluTestType,
}
enum FuluTestType {
WeSupernodeThemSupernode,
WeSupernodeThemFullnodes,
WeFullnodeThemSupernode,
WeFullnodeThemFullnodes,
}
impl FuluTestType {
fn we_node_custody_type(&self) -> NodeCustodyType {
match self {
Self::WeSupernodeThemSupernode | Self::WeSupernodeThemFullnodes => {
NodeCustodyType::Supernode
}
Self::WeFullnodeThemSupernode | Self::WeFullnodeThemFullnodes => {
NodeCustodyType::Fullnode
}
}
}
fn them_node_custody_type(&self) -> NodeCustodyType {
match self {
Self::WeSupernodeThemSupernode | Self::WeFullnodeThemSupernode => {
NodeCustodyType::Supernode
}
Self::WeSupernodeThemFullnodes | Self::WeFullnodeThemFullnodes => {
NodeCustodyType::Fullnode
}
}
}
}
#[derive(Debug)]
struct SeenLookup {
/// Lookup's Id
id: Id,
block_root: Hash256,
seen_peers: HashSet<PeerId>,
}
#[derive(Debug)]
struct ReportedPenalty {
pub peer_id: PeerId,
pub msg: &'static str,
}
// Environment variable to read if `fork_from_env` feature is enabled.