Event-based block lookup tests (#5534)

* WIP

* Initial working version of new sync tests.

* Remove sync traits and fix lints.

* Reduce internal method visibility and make test method instead. Remove extra beacon chain harness instance created in tests.

* Improve `SyncTester` api.

* Fix lint.

* Test example

* Lookup tests using rig

* Tests should interface with events only

* lint

* Skip deneb test pre-deneb

* Add more assertions

* Remove logging changes

* Address @jimmygchen comments

* Merge branch 'unstable' of https://github.com/sigp/lighthouse into bn-p2p-tests

* remove unused assertions

* fix lint
This commit is contained in:
Lion - dapplion
2024-04-10 21:05:18 +09:00
committed by GitHub
parent 72af6fb83a
commit b1f9751a69
8 changed files with 939 additions and 1016 deletions

View File

@@ -3,16 +3,13 @@ use crate::{
sync::{manager::BlockProcessType, SyncMessage},
};
use beacon_chain::block_verification_types::RpcBlock;
use beacon_chain::{
builder::Witness, eth1_chain::CachingEth1Backend, test_utils::BeaconChainHarness, BeaconChain,
};
use beacon_chain::{builder::Witness, eth1_chain::CachingEth1Backend, BeaconChain};
use beacon_chain::{BeaconChainTypes, NotifyExecutionLayer};
use beacon_processor::{
work_reprocessing_queue::ReprocessQueueMessage, BeaconProcessorChannels, BeaconProcessorSend,
DuplicateCache, GossipAggregatePackage, GossipAttestationPackage, Work,
WorkEvent as BeaconWorkEvent,
};
use environment::null_logger;
use lighthouse_network::rpc::methods::{BlobsByRangeRequest, BlobsByRootRequest};
use lighthouse_network::{
rpc::{BlocksByRangeRequest, BlocksByRootRequest, LightClientBootstrapRequest, StatusMessage},
@@ -24,7 +21,6 @@ use std::path::PathBuf;
use std::sync::Arc;
use std::time::Duration;
use store::MemoryStore;
use task_executor::test_utils::TestRuntime;
use task_executor::TaskExecutor;
use tokio::sync::mpsc::{self, error::TrySendError};
use types::*;
@@ -667,6 +663,9 @@ impl<E: EthSpec> NetworkBeaconProcessor<TestBeaconChainType<E>> {
// processor (but not much else).
pub fn null_for_testing(
network_globals: Arc<NetworkGlobals<E>>,
chain: Arc<BeaconChain<TestBeaconChainType<E>>>,
executor: TaskExecutor,
log: Logger,
) -> (Self, mpsc::Receiver<BeaconWorkEvent<E>>) {
let BeaconProcessorChannels {
beacon_processor_tx,
@@ -677,27 +676,17 @@ impl<E: EthSpec> NetworkBeaconProcessor<TestBeaconChainType<E>> {
let (network_tx, _network_rx) = mpsc::unbounded_channel();
let (sync_tx, _sync_rx) = mpsc::unbounded_channel();
let log = null_logger().unwrap();
let harness: BeaconChainHarness<TestBeaconChainType<E>> =
BeaconChainHarness::builder(E::default())
.spec(E::default_spec())
.deterministic_keypairs(8)
.logger(log.clone())
.fresh_ephemeral_store()
.mock_execution_layer()
.build();
let runtime = TestRuntime::default();
let network_beacon_processor = Self {
beacon_processor_send: beacon_processor_tx,
duplicate_cache: DuplicateCache::default(),
chain: harness.chain,
chain,
network_tx,
sync_tx,
reprocess_tx: work_reprocessing_tx,
network_globals,
invalid_block_storage: InvalidBlockStorage::Disabled,
executor: runtime.task_executor.clone(),
executor,
log,
};

View File

@@ -76,6 +76,24 @@ impl<T: BeaconChainTypes> BlockLookups<T> {
}
}
#[cfg(test)]
pub(crate) fn active_single_lookups(&self) -> Vec<Id> {
self.single_block_lookups.keys().cloned().collect()
}
#[cfg(test)]
pub(crate) fn active_parent_lookups(&self) -> Vec<Hash256> {
self.parent_lookups
.iter()
.map(|r| r.chain_hash())
.collect::<Vec<_>>()
}
#[cfg(test)]
pub(crate) fn failed_chains_contains(&mut self, chain_hash: &Hash256) -> bool {
self.failed_chains.contains(chain_hash)
}
/* Lookup requests */
/// Creates a lookup for the block with the given `block_root` and immediately triggers it.

File diff suppressed because it is too large Load Diff

View File

@@ -233,24 +233,13 @@ pub fn spawn<T: BeaconChainTypes>(
);
// create an instance of the SyncManager
let network_globals = beacon_processor.network_globals.clone();
let mut sync_manager = SyncManager {
chain: beacon_chain.clone(),
input_channel: sync_recv,
network: SyncNetworkContext::new(
network_send,
beacon_processor.clone(),
beacon_chain.clone(),
log.clone(),
),
range_sync: RangeSync::new(beacon_chain.clone(), log.clone()),
backfill_sync: BackFillSync::new(beacon_chain.clone(), network_globals, log.clone()),
block_lookups: BlockLookups::new(
beacon_chain.data_availability_checker.clone(),
log.clone(),
),
log: log.clone(),
};
let mut sync_manager = SyncManager::new(
beacon_chain,
network_send,
beacon_processor,
sync_recv,
log.clone(),
);
// spawn the sync manager thread
debug!(log, "Sync Manager started");
@@ -258,6 +247,48 @@ pub fn spawn<T: BeaconChainTypes>(
}
impl<T: BeaconChainTypes> SyncManager<T> {
pub(crate) fn new(
beacon_chain: Arc<BeaconChain<T>>,
network_send: mpsc::UnboundedSender<NetworkMessage<T::EthSpec>>,
beacon_processor: Arc<NetworkBeaconProcessor<T>>,
sync_recv: mpsc::UnboundedReceiver<SyncMessage<T::EthSpec>>,
log: slog::Logger,
) -> Self {
let network_globals = beacon_processor.network_globals.clone();
Self {
chain: beacon_chain.clone(),
input_channel: sync_recv,
network: SyncNetworkContext::new(
network_send,
beacon_processor.clone(),
beacon_chain.clone(),
log.clone(),
),
range_sync: RangeSync::new(beacon_chain.clone(), log.clone()),
backfill_sync: BackFillSync::new(beacon_chain.clone(), network_globals, log.clone()),
block_lookups: BlockLookups::new(
beacon_chain.data_availability_checker.clone(),
log.clone(),
),
log: log.clone(),
}
}
#[cfg(test)]
pub(crate) fn active_single_lookups(&self) -> Vec<Id> {
self.block_lookups.active_single_lookups()
}
#[cfg(test)]
pub(crate) fn active_parent_lookups(&self) -> Vec<Hash256> {
self.block_lookups.active_parent_lookups()
}
#[cfg(test)]
pub(crate) fn failed_chains_contains(&mut self, chain_hash: &Hash256) -> bool {
self.block_lookups.failed_chains_contains(chain_hash)
}
fn network_globals(&self) -> &NetworkGlobals<T::EthSpec> {
self.network.network_globals()
}
@@ -597,7 +628,7 @@ impl<T: BeaconChainTypes> SyncManager<T> {
}
}
fn handle_message(&mut self, sync_message: SyncMessage<T::EthSpec>) {
pub(crate) fn handle_message(&mut self, sync_message: SyncMessage<T::EthSpec>) {
match sync_message {
SyncMessage::AddPeer(peer_id, info) => {
self.add_peer(peer_id, info);
@@ -648,11 +679,12 @@ impl<T: BeaconChainTypes> SyncManager<T> {
ChildComponents::new(block_root, None, Some(blobs)),
);
}
SyncMessage::UnknownBlockHashFromAttestation(peer_id, block_hash) => {
SyncMessage::UnknownBlockHashFromAttestation(peer_id, block_root) => {
// If we are not synced, ignore this block.
if self.synced_and_connected(&peer_id) {
debug!(self.log, "Received sync_message"; "message" => "UnknownBlockHashFromAttestation", "block_root" => %block_root);
self.block_lookups
.search_block(block_hash, &[peer_id], &mut self.network);
.search_block(block_root, &[peer_id], &mut self.network);
}
}
SyncMessage::Disconnect(peer_id) => {

View File

@@ -637,7 +637,12 @@ mod tests {
let (network_tx, network_rx) = mpsc::unbounded_channel();
let globals = Arc::new(NetworkGlobals::new_test_globals(Vec::new(), &log));
let (network_beacon_processor, beacon_processor_rx) =
NetworkBeaconProcessor::null_for_testing(globals.clone());
NetworkBeaconProcessor::null_for_testing(
globals.clone(),
chain.clone(),
harness.runtime.task_executor.clone(),
log.clone(),
);
let cx = SyncNetworkContext::new(
network_tx,
Arc::new(network_beacon_processor),