lighthouse/slasher/tests/proposer_slashings.rs
Wink Saville 58870fc6d3 Add test_logger as feature to logging (#2586)
## Issue Addressed

Fix #2585

## Proposed Changes

Provide a canonical version of test_logger that can be used
throughout lighthouse.

## Additional Info

This allows tests to conditionally emit logging data by adding
test_logger as the default logger. And then when executing
`cargo test --features logging/test_logger` log output
will be visible:

  wink@3900x:~/lighthouse/common/logging/tests/test-feature-test_logger (Add-test_logger-as-feature-to-logging)
  $ cargo test --features logging/test_logger
      Finished test [unoptimized + debuginfo] target(s) in 0.02s
       Running unittests (target/debug/deps/test_logger-e20115db6a5e3714)

  running 1 test
  Sep 10 12:53:45.212 INFO hi, module: test_logger:8
  test tests::test_fn_with_logging ... ok

  test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s

     Doc-tests test-logger

  running 0 tests

  test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s

Or, in normal scenarios where logging isn't needed, executing
`cargo test` the log output will not be visible:

  wink@3900x:~/lighthouse/common/logging/tests/test-feature-test_logger (Add-test_logger-as-feature-to-logging)
  $ cargo test
      Finished test [unoptimized + debuginfo] target(s) in 0.02s
       Running unittests (target/debug/deps/test_logger-02e02f8d41e8cf8a)

  running 1 test
  test tests::test_fn_with_logging ... ok

  test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s

     Doc-tests test-logger

  running 0 tests

  test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
2021-10-06 00:46:07 +00:00

63 lines
2.2 KiB
Rust

use logging::test_logger;
use slasher::{
test_utils::{block as test_block, E},
Config, Slasher,
};
use tempfile::tempdir;
use types::{Epoch, EthSpec};
#[test]
fn empty_pruning() {
let tempdir = tempdir().unwrap();
let config = Config::new(tempdir.path().into()).for_testing();
let slasher = Slasher::<E>::open(config, test_logger()).unwrap();
slasher.prune_database(Epoch::new(0)).unwrap();
}
#[test]
fn block_pruning() {
let slots_per_epoch = E::slots_per_epoch();
let tempdir = tempdir().unwrap();
let mut config = Config::new(tempdir.path().into()).for_testing();
config.chunk_size = 2;
config.history_length = 2;
let slasher = Slasher::<E>::open(config.clone(), test_logger()).unwrap();
let current_epoch = Epoch::from(2 * config.history_length);
// Pruning the empty database should be safe.
slasher.prune_database(Epoch::new(0)).unwrap();
slasher.prune_database(current_epoch).unwrap();
// Add blocks in excess of the history length and prune them away.
let proposer_index = 100_000; // high to check sorting by slot
for slot in 1..=current_epoch.as_u64() * slots_per_epoch {
slasher.accept_block_header(test_block(slot, proposer_index, 0));
}
slasher.process_queued(current_epoch).unwrap();
slasher.prune_database(current_epoch).unwrap();
// Add more conflicting blocks, and check that only the ones within the non-pruned
// section are detected as slashable.
for slot in 1..=current_epoch.as_u64() * slots_per_epoch {
slasher.accept_block_header(test_block(slot, proposer_index, 1));
}
slasher.process_queued(current_epoch).unwrap();
let proposer_slashings = slasher.get_proposer_slashings();
// Check number of proposer slashings, accounting for single block in current epoch.
assert_eq!(
proposer_slashings.len(),
(config.history_length - 1) * slots_per_epoch as usize + 1
);
// Check epochs of all slashings are from within range.
assert!(proposer_slashings.iter().all(|slashing| slashing
.signed_header_1
.message
.slot
.epoch(slots_per_epoch)
> current_epoch - config.history_length as u64));
}