Fix range sync tests

This commit is contained in:
Emilia Hane 2023-01-26 17:40:21 +01:00
parent e9e198a2b6
commit 6beca6defc
No known key found for this signature in database
GPG Key ID: E73394F9C09206FA
4 changed files with 37 additions and 52 deletions

2
Cargo.lock generated
View File

@ -596,6 +596,8 @@ dependencies = [
"serde_json", "serde_json",
"slasher", "slasher",
"slog", "slog",
"slog-async",
"slog-term",
"sloggers", "sloggers",
"slot_clock", "slot_clock",
"smallvec", "smallvec",

View File

@ -68,6 +68,8 @@ hex = "0.4.2"
exit-future = "0.2.0" exit-future = "0.2.0"
unused_port = {path = "../../common/unused_port"} unused_port = {path = "../../common/unused_port"}
oneshot_broadcast = { path = "../../common/oneshot_broadcast" } oneshot_broadcast = { path = "../../common/oneshot_broadcast" }
slog-term = "2.6.0"
slog-async = "2.5.0"
[[test]] [[test]]
name = "beacon_chain_tests" name = "beacon_chain_tests"

View File

@ -34,7 +34,9 @@ use rand::Rng;
use rand::SeedableRng; use rand::SeedableRng;
use rayon::prelude::*; use rayon::prelude::*;
use sensitive_url::SensitiveUrl; use sensitive_url::SensitiveUrl;
use slog::Logger; use slog::{o, Drain, Logger};
use slog_async::Async;
use slog_term::{FullFormat, TermDecorator};
use slot_clock::{SlotClock, SystemTimeSlotClock, TestingSlotClock}; use slot_clock::{SlotClock, SystemTimeSlotClock, TestingSlotClock};
use state_processing::per_block_processing::compute_timestamp_at_slot; use state_processing::per_block_processing::compute_timestamp_at_slot;
use state_processing::{ use state_processing::{
@ -72,7 +74,6 @@ pub type DiskHarnessType<E, TSlotClock> = BaseHarnessType<E, LevelDB<E>, LevelDB
pub type EphemeralHarnessType<E, TSlotClock> = pub type EphemeralHarnessType<E, TSlotClock> =
BaseHarnessType<E, MemoryStore<E>, MemoryStore<E>, TSlotClock>; BaseHarnessType<E, MemoryStore<E>, MemoryStore<E>, TSlotClock>;
pub type EphemeralTestingSlotClockHarnessType<E> = pub type EphemeralTestingSlotClockHarnessType<E> =
BaseHarnessType<E, MemoryStore<E>, MemoryStore<E>, TestingSlotClock>; BaseHarnessType<E, MemoryStore<E>, MemoryStore<E>, TestingSlotClock>;
pub type EphemeralSystemTimeSlotClockHarnessType<E> = pub type EphemeralSystemTimeSlotClockHarnessType<E> =
@ -2129,3 +2130,15 @@ impl<T: BeaconChainTypes> fmt::Debug for BeaconChainHarness<T> {
write!(f, "BeaconChainHarness") write!(f, "BeaconChainHarness")
} }
} }
pub fn build_log(level: slog::Level, enabled: bool) -> Logger {
let decorator = TermDecorator::new().build();
let drain = FullFormat::new(decorator).build().fuse();
let drain = Async::new(drain).build().fuse();
if enabled {
Logger::root(drain.filter_level(level).fuse(), o!())
} else {
Logger::root(drain.filter(|_| false).fuse(), o!())
}
}

View File

@ -377,25 +377,26 @@ mod tests {
use crate::beacon_processor::WorkEvent as BeaconWorkEvent; use crate::beacon_processor::WorkEvent as BeaconWorkEvent;
use crate::service::RequestId; use crate::service::RequestId;
use crate::NetworkMessage; use crate::NetworkMessage;
use beacon_chain::{builder::BeaconChainBuilder, test_utils::test_spec};
use beacon_chain::{ use beacon_chain::{
builder::Witness, eth1_chain::CachingEth1Backend, parking_lot::RwLock, EngineState, builder::Witness,
eth1_chain::CachingEth1Backend,
parking_lot::RwLock,
test_utils::{
build_log, BeaconChainHarness, EphemeralSystemTimeSlotClockHarnessType as HarnessType,
},
EngineState,
}; };
use lighthouse_network::{ use lighthouse_network::{
rpc::{BlocksByRangeRequest, StatusMessage}, rpc::{BlocksByRangeRequest, StatusMessage},
NetworkGlobals, Request, NetworkGlobals, Request,
}; };
use slog::{o, Drain}; use slog::o;
use sloggers::{null::NullLoggerBuilder, Build}; use slot_clock::SystemTimeSlotClock;
use slot_clock::{SlotClock, SystemTimeSlotClock};
use std::time::{Duration, SystemTime};
use std::{collections::HashSet, sync::Arc}; use std::{collections::HashSet, sync::Arc};
use store::MemoryStore; use store::MemoryStore;
use tokio::sync::mpsc; use tokio::sync::mpsc;
use types::{Hash256, MinimalEthSpec as E}; use types::{Hash256, MinimalEthSpec as E};
const SLOT_DURATION_MILLIS: u64 = 400;
#[derive(Debug)] #[derive(Debug)]
struct FakeStorage { struct FakeStorage {
known_blocks: RwLock<HashSet<Hash256>>, known_blocks: RwLock<HashSet<Hash256>>,
@ -443,18 +444,6 @@ mod tests {
type TestBeaconChainType = type TestBeaconChainType =
Witness<SystemTimeSlotClock, CachingEth1Backend<E>, E, MemoryStore<E>, MemoryStore<E>>; Witness<SystemTimeSlotClock, CachingEth1Backend<E>, E, MemoryStore<E>, MemoryStore<E>>;
fn build_log(level: slog::Level, enabled: bool) -> slog::Logger {
let decorator = slog_term::TermDecorator::new().build();
let drain = slog_term::FullFormat::new(decorator).build().fuse();
let drain = slog_async::Async::new(drain).build().fuse();
if enabled {
slog::Logger::root(drain.filter_level(level).fuse(), o!())
} else {
slog::Logger::root(drain.filter(|_| false).fuse(), o!())
}
}
#[allow(unused)] #[allow(unused)]
struct TestRig { struct TestRig {
log: slog::Logger, log: slog::Logger,
@ -595,37 +584,16 @@ mod tests {
} }
fn range(log_enabled: bool) -> (TestRig, RangeSync<TestBeaconChainType, FakeStorage>) { fn range(log_enabled: bool) -> (TestRig, RangeSync<TestBeaconChainType, FakeStorage>) {
let builder = NullLoggerBuilder;
let db_log = builder.build().expect("should build logger");
let store = store::HotColdDB::open_ephemeral(
store::StoreConfig::default(),
E::default_spec(),
db_log,
)
.unwrap();
// Initialise a new beacon chain
let chain = Arc::new(
BeaconChainBuilder::new(E)
.custom_spec(test_spec::<E>())
.store(Arc::new(store))
.dummy_eth1_backend()
.expect("should build dummy backend")
.slot_clock(SystemTimeSlotClock::new(
types::Slot::new(0),
Duration::from_secs(
SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH)
.unwrap()
.as_secs(),
),
Duration::from_millis(SLOT_DURATION_MILLIS),
))
.build()
.expect("should build"),
);
let log = build_log(slog::Level::Trace, log_enabled); let log = build_log(slog::Level::Trace, log_enabled);
// Initialise a new beacon chain
let harness = BeaconChainHarness::<HarnessType<E>>::builder(E::default())
.default_spec()
.logger(log.clone())
.deterministic_keypairs(1)
.fresh_ephemeral_store()
.build();
let chain = harness.chain;
let fake_store = Arc::new(FakeStorage::default()); let fake_store = Arc::new(FakeStorage::default());
let (beacon_processor_tx, beacon_processor_rx) = mpsc::channel(10); let (beacon_processor_tx, beacon_processor_rx) = mpsc::channel(10);
let range_sync = RangeSync::<TestBeaconChainType, FakeStorage>::new( let range_sync = RangeSync::<TestBeaconChainType, FakeStorage>::new(