mirror of
https://github.com/sigp/lighthouse.git
synced 2026-05-30 04:37:13 +00:00
Prevent fd leak in random slasher tests (#6254)
* Prevent fd leak in random slasher tests * Clippy
This commit is contained in:
@@ -7,10 +7,11 @@ use slasher::{
|
||||
block, chain_spec, indexed_att, slashed_validators_from_attestations,
|
||||
slashed_validators_from_slashings, E,
|
||||
},
|
||||
Config, Slasher,
|
||||
Config, Slasher, SlasherDB,
|
||||
};
|
||||
use std::cmp::max;
|
||||
use tempfile::tempdir;
|
||||
use std::sync::Arc;
|
||||
use tempfile::{tempdir, TempDir};
|
||||
use types::{Epoch, EthSpec};
|
||||
|
||||
#[derive(Debug)]
|
||||
@@ -32,7 +33,16 @@ impl Default for TestConfig {
|
||||
}
|
||||
}
|
||||
|
||||
fn random_test(seed: u64, test_config: TestConfig) {
|
||||
fn make_db() -> (TempDir, SlasherDB<E>) {
|
||||
let tempdir = tempdir().unwrap();
|
||||
let initial_config = Arc::new(Config::new(tempdir.path().into()));
|
||||
let logger = test_logger();
|
||||
let spec = chain_spec();
|
||||
let db = SlasherDB::open(initial_config.clone(), spec, logger).unwrap();
|
||||
(tempdir, db)
|
||||
}
|
||||
|
||||
fn random_test(seed: u64, mut db: SlasherDB<E>, test_config: TestConfig) -> SlasherDB<E> {
|
||||
let check_slashings = test_config.check_slashings;
|
||||
let num_validators = test_config.num_validators;
|
||||
let max_attestations = test_config.max_attestations;
|
||||
@@ -40,18 +50,17 @@ fn random_test(seed: u64, test_config: TestConfig) {
|
||||
println!("Running with seed {}", seed);
|
||||
let mut rng = StdRng::seed_from_u64(seed);
|
||||
|
||||
let tempdir = tempdir().unwrap();
|
||||
|
||||
let mut config = Config::new(tempdir.path().into());
|
||||
let mut config = Config::new(db.get_config().database_path.clone());
|
||||
config.validator_chunk_size = 1 << rng.gen_range(1..4);
|
||||
|
||||
let chunk_size_exponent = rng.gen_range(1..4);
|
||||
config.chunk_size = 1 << chunk_size_exponent;
|
||||
config.history_length = 1 << rng.gen_range(chunk_size_exponent..chunk_size_exponent + 3);
|
||||
|
||||
let spec = chain_spec();
|
||||
let config = Arc::new(config);
|
||||
db.update_config(config.clone());
|
||||
|
||||
let slasher = Slasher::<E>::open(config.clone(), spec, test_logger()).unwrap();
|
||||
let slasher = Slasher::<E>::from_config_and_db(config.clone(), db, test_logger()).unwrap();
|
||||
|
||||
let validators = (0..num_validators as u64).collect::<Vec<u64>>();
|
||||
|
||||
@@ -121,7 +130,7 @@ fn random_test(seed: u64, test_config: TestConfig) {
|
||||
}
|
||||
|
||||
if !check_slashings {
|
||||
return;
|
||||
return slasher.into_reset_db().unwrap();
|
||||
}
|
||||
|
||||
slasher.process_queued(current_epoch).unwrap();
|
||||
@@ -131,6 +140,9 @@ fn random_test(seed: u64, test_config: TestConfig) {
|
||||
let slashed_validators = slashed_validators_from_slashings(&slashings);
|
||||
let expected_slashed_validators = slashed_validators_from_attestations(&attestations);
|
||||
assert_eq!(slashed_validators, expected_slashed_validators);
|
||||
|
||||
// Return the database for reuse.
|
||||
slasher.into_reset_db().unwrap()
|
||||
}
|
||||
|
||||
// Fuzz-like test that runs forever on different seeds looking for crashes.
|
||||
@@ -138,8 +150,9 @@ fn random_test(seed: u64, test_config: TestConfig) {
|
||||
#[ignore]
|
||||
fn no_crash() {
|
||||
let mut rng = thread_rng();
|
||||
let (_tempdir, mut db) = make_db();
|
||||
loop {
|
||||
random_test(rng.gen(), TestConfig::default());
|
||||
db = random_test(rng.gen(), db, TestConfig::default());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -148,9 +161,11 @@ fn no_crash() {
|
||||
#[ignore]
|
||||
fn no_crash_with_blocks() {
|
||||
let mut rng = thread_rng();
|
||||
let (_tempdir, mut db) = make_db();
|
||||
loop {
|
||||
random_test(
|
||||
db = random_test(
|
||||
rng.gen(),
|
||||
db,
|
||||
TestConfig {
|
||||
add_blocks: true,
|
||||
..TestConfig::default()
|
||||
@@ -164,9 +179,11 @@ fn no_crash_with_blocks() {
|
||||
#[ignore]
|
||||
fn check_slashings() {
|
||||
let mut rng = thread_rng();
|
||||
let (_tempdir, mut db) = make_db();
|
||||
loop {
|
||||
random_test(
|
||||
db = random_test(
|
||||
rng.gen(),
|
||||
db,
|
||||
TestConfig {
|
||||
check_slashings: true,
|
||||
..TestConfig::default()
|
||||
@@ -177,8 +194,10 @@ fn check_slashings() {
|
||||
|
||||
#[test]
|
||||
fn check_slashings_example1() {
|
||||
let (_tempdir, db) = make_db();
|
||||
random_test(
|
||||
1,
|
||||
db,
|
||||
TestConfig {
|
||||
check_slashings: true,
|
||||
..TestConfig::default()
|
||||
@@ -188,8 +207,10 @@ fn check_slashings_example1() {
|
||||
|
||||
#[test]
|
||||
fn check_slashings_example2() {
|
||||
let (_tempdir, db) = make_db();
|
||||
random_test(
|
||||
2,
|
||||
db,
|
||||
TestConfig {
|
||||
check_slashings: true,
|
||||
max_attestations: 3,
|
||||
@@ -200,8 +221,10 @@ fn check_slashings_example2() {
|
||||
|
||||
#[test]
|
||||
fn check_slashings_example3() {
|
||||
let (_tempdir, db) = make_db();
|
||||
random_test(
|
||||
3,
|
||||
db,
|
||||
TestConfig {
|
||||
check_slashings: true,
|
||||
max_attestations: 100,
|
||||
@@ -212,23 +235,28 @@ fn check_slashings_example3() {
|
||||
|
||||
#[test]
|
||||
fn no_crash_example1() {
|
||||
random_test(1, TestConfig::default());
|
||||
let (_tempdir, db) = make_db();
|
||||
random_test(1, db, TestConfig::default());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn no_crash_example2() {
|
||||
random_test(2, TestConfig::default());
|
||||
let (_tempdir, db) = make_db();
|
||||
random_test(2, db, TestConfig::default());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn no_crash_example3() {
|
||||
random_test(3, TestConfig::default());
|
||||
let (_tempdir, db) = make_db();
|
||||
random_test(3, db, TestConfig::default());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn no_crash_blocks_example1() {
|
||||
let (_tempdir, db) = make_db();
|
||||
random_test(
|
||||
1,
|
||||
db,
|
||||
TestConfig {
|
||||
add_blocks: true,
|
||||
..TestConfig::default()
|
||||
@@ -238,5 +266,6 @@ fn no_crash_blocks_example1() {
|
||||
|
||||
#[test]
|
||||
fn no_crash_aug_24() {
|
||||
random_test(13519442335106054152, TestConfig::default())
|
||||
let (_tempdir, db) = make_db();
|
||||
random_test(13519442335106054152, db, TestConfig::default());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user