Skip to content

Commit

Permalink
Make tests *finally* compile with async FC
Browse files Browse the repository at this point in the history
  • Loading branch information
paulhauner committed Jun 28, 2022
1 parent 3438ac2 commit 9aa796f
Show file tree
Hide file tree
Showing 19 changed files with 999 additions and 768 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion beacon_node/beacon_chain/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ mod persisted_beacon_chain;
mod persisted_fork_choice;
mod pre_finalization_cache;
pub mod proposer_prep_service;
mod recompute_head;
pub mod recompute_head;
pub mod schema_change;
mod shuffling_cache;
mod snapshot_cache;
Expand Down
2 changes: 1 addition & 1 deletion beacon_node/beacon_chain/src/recompute_head.rs
Original file line number Diff line number Diff line change
Expand Up @@ -608,7 +608,7 @@ fn detect_reorg<E: EthSpec>(
/// Iterate through the current chain to find the slot intersecting with the given beacon state.
/// The maximum depth this will search is `SLOTS_PER_HISTORICAL_ROOT`, and if that depth is reached
/// and no intersection is found, the finalized slot will be returned.
fn find_reorg_slot<E: EthSpec>(
pub fn find_reorg_slot<E: EthSpec>(
old_state: &BeaconState<E>,
old_block_root: Hash256,
new_state: &BeaconState<E>,
Expand Down
16 changes: 16 additions & 0 deletions beacon_node/beacon_chain/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,22 @@ where
(head.beacon_state, state_root)
}

pub fn head_slot(&self) -> Slot {
self.chain.canonical_head.read().head_slot()
}

pub fn head_block_root(&self) -> Hash256 {
self.chain.canonical_head.read().head_block_root()
}

pub fn finalized_checkpoint(&self) -> Checkpoint {
self.chain.canonical_head.read().finalized_checkpoint()
}

pub fn justified_checkpoint(&self) -> Checkpoint {
self.chain.canonical_head.read().justified_checkpoint()
}

pub fn get_current_slot(&self) -> Slot {
self.chain.slot().unwrap()
}
Expand Down
42 changes: 26 additions & 16 deletions beacon_node/beacon_chain/tests/attestation_production.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ lazy_static! {
/// attestation at each slot from genesis through to three epochs past the head.
///
/// It checks the produced attestation against some locally computed values.
#[test]
fn produces_attestations() {
#[tokio::test]
async fn produces_attestations() {
let num_blocks_produced = MainnetEthSpec::slots_per_epoch() * 4;
let additional_slots_tested = MainnetEthSpec::slots_per_epoch() * 3;

Expand All @@ -37,11 +37,13 @@ fn produces_attestations() {
if slot > 0 && slot <= num_blocks_produced {
harness.advance_slot();

harness.extend_chain(
1,
BlockStrategy::OnCanonicalHead,
AttestationStrategy::AllValidators,
);
harness
.extend_chain(
1,
BlockStrategy::OnCanonicalHead,
AttestationStrategy::AllValidators,
)
.await;
}

let slot = Slot::from(slot);
Expand Down Expand Up @@ -129,7 +131,12 @@ fn produces_attestations() {
assert_eq!(data.target.root, target_root, "bad target root");

let early_attestation = {
let proto_block = chain.fork_choice.read().get_block(&block_root).unwrap();
let proto_block = chain
.canonical_head
.read()
.fork_choice
.get_block(&block_root)
.unwrap();
chain
.early_attester_cache
.add_head_block(block_root, block.clone(), proto_block, &state, &chain.spec)
Expand All @@ -151,8 +158,8 @@ fn produces_attestations() {

/// Ensures that the early attester cache wont create an attestation to a block in a later slot than
/// the one requested.
#[test]
fn early_attester_cache_old_request() {
#[tokio::test]
async fn early_attester_cache_old_request() {
let harness = BeaconChainHarness::builder(MainnetEthSpec)
.default_spec()
.keypairs(KEYPAIRS[..].to_vec())
Expand All @@ -162,18 +169,21 @@ fn early_attester_cache_old_request() {

harness.advance_slot();

harness.extend_chain(
2,
BlockStrategy::OnCanonicalHead,
AttestationStrategy::AllValidators,
);
harness
.extend_chain(
2,
BlockStrategy::OnCanonicalHead,
AttestationStrategy::AllValidators,
)
.await;

let head = harness.chain.head().unwrap();
assert_eq!(head.beacon_block.slot(), 2);
let head_proto_block = harness
.chain
.fork_choice
.canonical_head
.read()
.fork_choice
.get_block(&head.beacon_block_root)
.unwrap();

Expand Down
90 changes: 51 additions & 39 deletions beacon_node/beacon_chain/tests/attestation_verification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,15 +213,17 @@ struct GossipTester {
}

impl GossipTester {
pub fn new() -> Self {
pub async fn new() -> Self {
let harness = get_harness(VALIDATOR_COUNT);

// Extend the chain out a few epochs so we have some chain depth to play with.
harness.extend_chain(
MainnetEthSpec::slots_per_epoch() as usize * 3 - 1,
BlockStrategy::OnCanonicalHead,
AttestationStrategy::AllValidators,
);
harness
.extend_chain(
MainnetEthSpec::slots_per_epoch() as usize * 3 - 1,
BlockStrategy::OnCanonicalHead,
AttestationStrategy::AllValidators,
)
.await;

// Advance into a slot where there have not been blocks or attestations produced.
harness.advance_slot();
Expand Down Expand Up @@ -395,9 +397,10 @@ impl GossipTester {
}
}
/// Tests verification of `SignedAggregateAndProof` from the gossip network.
#[test]
fn aggregated_gossip_verification() {
#[tokio::test]
async fn aggregated_gossip_verification() {
GossipTester::new()
.await
/*
* The following two tests ensure:
*
Expand Down Expand Up @@ -612,7 +615,7 @@ fn aggregated_gossip_verification() {
tester.valid_aggregate.message.aggregate.clone(),
None,
&sk,
&chain.head_info().unwrap().fork,
&chain.canonical_head.read().head_fork(),
chain.genesis_validators_root,
&chain.spec,
)
Expand Down Expand Up @@ -669,9 +672,10 @@ fn aggregated_gossip_verification() {
}

/// Tests the verification conditions for an unaggregated attestation on the gossip network.
#[test]
fn unaggregated_gossip_verification() {
#[tokio::test]
async fn unaggregated_gossip_verification() {
GossipTester::new()
.await
/*
* The following test ensures:
*
Expand Down Expand Up @@ -924,16 +928,18 @@ fn unaggregated_gossip_verification() {
/// Ensures that an attestation that skips epochs can still be processed.
///
/// This also checks that we can do a state lookup if we don't get a hit from the shuffling cache.
#[test]
fn attestation_that_skips_epochs() {
#[tokio::test]
async fn attestation_that_skips_epochs() {
let harness = get_harness(VALIDATOR_COUNT);

// Extend the chain out a few epochs so we have some chain depth to play with.
harness.extend_chain(
MainnetEthSpec::slots_per_epoch() as usize * 3 + 1,
BlockStrategy::OnCanonicalHead,
AttestationStrategy::SomeValidators(vec![]),
);
harness
.extend_chain(
MainnetEthSpec::slots_per_epoch() as usize * 3 + 1,
BlockStrategy::OnCanonicalHead,
AttestationStrategy::SomeValidators(vec![]),
)
.await;

let current_slot = harness.chain.slot().expect("should get slot");
let current_epoch = harness.chain.epoch().expect("should get epoch");
Expand Down Expand Up @@ -992,16 +998,18 @@ fn attestation_that_skips_epochs() {
.expect("should gossip verify attestation that skips slots");
}

#[test]
fn attestation_to_finalized_block() {
#[tokio::test]
async fn attestation_to_finalized_block() {
let harness = get_harness(VALIDATOR_COUNT);

// Extend the chain out a few epochs so we have some chain depth to play with.
harness.extend_chain(
MainnetEthSpec::slots_per_epoch() as usize * 4 + 1,
BlockStrategy::OnCanonicalHead,
AttestationStrategy::AllValidators,
);
harness
.extend_chain(
MainnetEthSpec::slots_per_epoch() as usize * 4 + 1,
BlockStrategy::OnCanonicalHead,
AttestationStrategy::AllValidators,
)
.await;

let finalized_checkpoint = harness
.chain
Expand Down Expand Up @@ -1067,16 +1075,18 @@ fn attestation_to_finalized_block() {
.contains(earlier_block_root));
}

#[test]
fn verify_aggregate_for_gossip_doppelganger_detection() {
#[tokio::test]
async fn verify_aggregate_for_gossip_doppelganger_detection() {
let harness = get_harness(VALIDATOR_COUNT);

// Extend the chain out a few epochs so we have some chain depth to play with.
harness.extend_chain(
MainnetEthSpec::slots_per_epoch() as usize * 3 - 1,
BlockStrategy::OnCanonicalHead,
AttestationStrategy::AllValidators,
);
harness
.extend_chain(
MainnetEthSpec::slots_per_epoch() as usize * 3 - 1,
BlockStrategy::OnCanonicalHead,
AttestationStrategy::AllValidators,
)
.await;

// Advance into a slot where there have not been blocks or attestations produced.
harness.advance_slot();
Expand Down Expand Up @@ -1124,16 +1134,18 @@ fn verify_aggregate_for_gossip_doppelganger_detection() {
.expect("should check if gossip aggregator was observed"));
}

#[test]
fn verify_attestation_for_gossip_doppelganger_detection() {
#[tokio::test]
async fn verify_attestation_for_gossip_doppelganger_detection() {
let harness = get_harness(VALIDATOR_COUNT);

// Extend the chain out a few epochs so we have some chain depth to play with.
harness.extend_chain(
MainnetEthSpec::slots_per_epoch() as usize * 3 - 1,
BlockStrategy::OnCanonicalHead,
AttestationStrategy::AllValidators,
);
harness
.extend_chain(
MainnetEthSpec::slots_per_epoch() as usize * 3 - 1,
BlockStrategy::OnCanonicalHead,
AttestationStrategy::AllValidators,
)
.await;

// Advance into a slot where there have not been blocks or attestations produced.
harness.advance_slot();
Expand Down
Loading

0 comments on commit 9aa796f

Please sign in to comment.