Skip to content

Commit

Permalink
More test fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
paulhauner committed Jun 28, 2022
1 parent 211e4cc commit 3438ac2
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 75 deletions.
12 changes: 0 additions & 12 deletions beacon_node/beacon_chain/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1331,18 +1331,6 @@ where
(deposits, state)
}

pub fn recompute_head_blocking(&self) -> Result<(), BeaconChainError> {
let chain = self.chain.clone();
self.chain
.task_executor
.clone()
.block_on_dangerous(
chain.recompute_head_at_current_slot(),
"recompute_head_blocking",
)
.ok_or(BeaconChainError::RuntimeShutdown)?
}

pub async fn process_block(
&self,
slot: Slot,
Expand Down
102 changes: 49 additions & 53 deletions beacon_node/network/src/beacon_processor/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ impl Drop for TestRig {
}

impl TestRig {
pub fn new(chain_length: u64) -> Self {
pub async fn new(chain_length: u64) -> Self {
// This allows for testing voluntary exits without building out a massive chain.
let mut spec = E::default_spec();
spec.shard_committee_period = 2;
Expand All @@ -84,11 +84,13 @@ impl TestRig {
harness.advance_slot();

for _ in 0..chain_length {
harness.extend_chain(
1,
BlockStrategy::OnCanonicalHead,
AttestationStrategy::AllValidators,
);
harness
.extend_chain(
1,
BlockStrategy::OnCanonicalHead,
AttestationStrategy::AllValidators,
)
.await;

harness.advance_slot();
}
Expand Down Expand Up @@ -223,14 +225,8 @@ impl TestRig {
}
}

pub fn recompute_head_blocking(&self) {
let chain = self.chain.clone();
self.chain
.task_executor
.clone()
.block_on_dangerous(chain.recompute_head(), "recompute_head_blocking")
.unwrap()
.unwrap();
pub async fn recompute_head(&self) {
self.chain.recompute_head_at_current_slot().await.unwrap()
}

pub fn head_root(&self) -> Hash256 {
Expand Down Expand Up @@ -465,9 +461,9 @@ fn junk_message_id() -> MessageId {
}

/// Blocks that arrive early should be queued for later processing.
#[test]
fn import_gossip_block_acceptably_early() {
let mut rig = TestRig::new(SMALL_CHAIN);
#[tokio::test]
async fn import_gossip_block_acceptably_early() {
let mut rig = TestRig::new(SMALL_CHAIN).await;

let slot_start = rig
.chain
Expand Down Expand Up @@ -512,9 +508,9 @@ fn import_gossip_block_acceptably_early() {
}

/// Blocks that are *too* early shouldn't get into the delay queue.
#[test]
fn import_gossip_block_unacceptably_early() {
let mut rig = TestRig::new(SMALL_CHAIN);
#[tokio::test]
async fn import_gossip_block_unacceptably_early() {
let mut rig = TestRig::new(SMALL_CHAIN).await;

let slot_start = rig
.chain
Expand Down Expand Up @@ -547,9 +543,9 @@ fn import_gossip_block_unacceptably_early() {
}

/// Blocks that arrive on-time should be processed normally.
#[test]
fn import_gossip_block_at_current_slot() {
let mut rig = TestRig::new(SMALL_CHAIN);
#[tokio::test]
async fn import_gossip_block_at_current_slot() {
let mut rig = TestRig::new(SMALL_CHAIN).await;

assert_eq!(
rig.chain.slot().unwrap(),
Expand All @@ -569,9 +565,9 @@ fn import_gossip_block_at_current_slot() {
}

/// Ensure a valid attestation can be imported.
#[test]
fn import_gossip_attestation() {
let mut rig = TestRig::new(SMALL_CHAIN);
#[tokio::test]
async fn import_gossip_attestation() {
let mut rig = TestRig::new(SMALL_CHAIN).await;

let initial_attns = rig.chain.naive_aggregation_pool.read().num_items();

Expand All @@ -593,8 +589,8 @@ enum BlockImportMethod {

/// Ensure that attestations that reference an unknown block get properly re-queued and
/// re-processed upon importing the block.
fn attestation_to_unknown_block_processed(import_method: BlockImportMethod) {
let mut rig = TestRig::new(SMALL_CHAIN);
async fn attestation_to_unknown_block_processed(import_method: BlockImportMethod) {
let mut rig = TestRig::new(SMALL_CHAIN).await;

// Send the attestation but not the block, and check that it was not imported.

Expand Down Expand Up @@ -627,7 +623,7 @@ fn attestation_to_unknown_block_processed(import_method: BlockImportMethod) {

// Run fork choice, since it isn't run when processing an RPC block. At runtime it is the
// responsibility of the sync manager to do this.
rig.recompute_head_blocking();
rig.recompute_head().await;

assert_eq!(
rig.head_root(),
Expand All @@ -642,20 +638,20 @@ fn attestation_to_unknown_block_processed(import_method: BlockImportMethod) {
);
}

#[test]
fn attestation_to_unknown_block_processed_after_gossip_block() {
attestation_to_unknown_block_processed(BlockImportMethod::Gossip)
#[tokio::test]
async fn attestation_to_unknown_block_processed_after_gossip_block() {
attestation_to_unknown_block_processed(BlockImportMethod::Gossip).await
}

#[test]
fn attestation_to_unknown_block_processed_after_rpc_block() {
attestation_to_unknown_block_processed(BlockImportMethod::Rpc)
#[tokio::test]
async fn attestation_to_unknown_block_processed_after_rpc_block() {
attestation_to_unknown_block_processed(BlockImportMethod::Rpc).await
}

/// Ensure that attestations that reference an unknown block get properly re-queued and
/// re-processed upon importing the block.
fn aggregate_attestation_to_unknown_block(import_method: BlockImportMethod) {
let mut rig = TestRig::new(SMALL_CHAIN);
async fn aggregate_attestation_to_unknown_block(import_method: BlockImportMethod) {
let mut rig = TestRig::new(SMALL_CHAIN).await;

// Empty the op pool.
rig.chain
Expand Down Expand Up @@ -694,7 +690,7 @@ fn aggregate_attestation_to_unknown_block(import_method: BlockImportMethod) {

// Run fork choice, since it isn't run when processing an RPC block. At runtime it is the
// responsibility of the sync manager to do this.
rig.recompute_head_blocking();
rig.recompute_head().await;

assert_eq!(
rig.head_root(),
Expand All @@ -709,21 +705,21 @@ fn aggregate_attestation_to_unknown_block(import_method: BlockImportMethod) {
);
}

#[test]
fn aggregate_attestation_to_unknown_block_processed_after_gossip_block() {
aggregate_attestation_to_unknown_block(BlockImportMethod::Gossip)
#[tokio::test]
async fn aggregate_attestation_to_unknown_block_processed_after_gossip_block() {
aggregate_attestation_to_unknown_block(BlockImportMethod::Gossip).await
}

#[test]
fn aggregate_attestation_to_unknown_block_processed_after_rpc_block() {
aggregate_attestation_to_unknown_block(BlockImportMethod::Rpc)
#[tokio::test]
async fn aggregate_attestation_to_unknown_block_processed_after_rpc_block() {
aggregate_attestation_to_unknown_block(BlockImportMethod::Rpc).await
}

/// Ensure that attestations that reference an unknown block get properly re-queued and re-processed
/// when the block is not seen.
#[test]
fn requeue_unknown_block_gossip_attestation_without_import() {
let mut rig = TestRig::new(SMALL_CHAIN);
#[tokio::test]
async fn requeue_unknown_block_gossip_attestation_without_import() {
let mut rig = TestRig::new(SMALL_CHAIN).await;

// Send the attestation but not the block, and check that it was not imported.

Expand Down Expand Up @@ -755,9 +751,9 @@ fn requeue_unknown_block_gossip_attestation_without_import() {

/// Ensure that aggregate that reference an unknown block get properly re-queued and re-processed
/// when the block is not seen.
#[test]
fn requeue_unknown_block_gossip_aggregated_attestation_without_import() {
let mut rig = TestRig::new(SMALL_CHAIN);
#[tokio::test]
async fn requeue_unknown_block_gossip_aggregated_attestation_without_import() {
let mut rig = TestRig::new(SMALL_CHAIN).await;

// Send the attestation but not the block, and check that it was not imported.

Expand Down Expand Up @@ -788,10 +784,10 @@ fn requeue_unknown_block_gossip_aggregated_attestation_without_import() {
}

/// Ensure a bunch of valid operations can be imported.
#[test]
fn import_misc_gossip_ops() {
#[tokio::test]
async fn import_misc_gossip_ops() {
// Exits need the long chain so validators aren't too young to exit.
let mut rig = TestRig::new(LONG_CHAIN);
let mut rig = TestRig::new(LONG_CHAIN).await;

/*
* Attester slashing
Expand Down
17 changes: 9 additions & 8 deletions testing/state_transition_vectors/src/exit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,12 @@ impl ExitTest {
}

#[cfg(all(test, not(debug_assertions)))]
fn run(self) -> BeaconState<E> {
async fn run(self) -> BeaconState<E> {
let spec = &E::default_spec();
let expected = self.expected.clone();
assert_eq!(STATE_EPOCH, spec.shard_committee_period);

let (block, mut state) = self.block_and_pre_state();
let (block, mut state) = self.block_and_pre_state().await;

let result = Self::process(&block, &mut state);

Expand Down Expand Up @@ -335,22 +335,23 @@ mod custom_tests {
);
}

#[test]
fn valid() {
let state = ExitTest::default().run();
#[tokio::test]
async fn valid() {
let state = ExitTest::default().run().await;
assert_exited(&state, VALIDATOR_INDEX as usize);
}

#[test]
fn valid_three() {
#[tokio::test]
async fn valid_three() {
let state = ExitTest {
block_modifier: Box::new(|harness, block| {
harness.add_voluntary_exit(block, 1, STATE_EPOCH);
harness.add_voluntary_exit(block, 2, STATE_EPOCH);
}),
..ExitTest::default()
}
.run();
.run()
.await;

for i in &[VALIDATOR_INDEX, 1, 2] {
assert_exited(&state, *i as usize);
Expand Down
3 changes: 1 addition & 2 deletions testing/state_transition_vectors/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,11 @@ macro_rules! vectors_and_tests {
}

#[cfg(all(test, not(debug_assertions)))]
#[tokio::test]
mod tests {
use super::*;
$(
#[tokio::test]
fn $name() {
async fn $name() {
$test.run().await;
}
)*
Expand Down

0 comments on commit 3438ac2

Please sign in to comment.