Skip to content

Commit

Permalink
Fix test compilation errors
Browse files Browse the repository at this point in the history
  • Loading branch information
paulhauner committed Jun 28, 2022
1 parent 59351d2 commit f9634a6
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 42 deletions.
4 changes: 2 additions & 2 deletions beacon_node/http_api/tests/interactive_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ pub async fn fork_choice_before_proposal() {
let slot_d = slot_a + 3;

let state_a = harness.get_current_state();
let (block_b, state_b) = harness.make_block(state_a.clone(), slot_b);
let (block_b, state_b) = harness.make_block(state_a.clone(), slot_b).await;
let block_root_b = harness.process_block(slot_b, block_b).await.unwrap();

// Create attestations to B but keep them in reserve until after C has been processed.
Expand All @@ -78,7 +78,7 @@ pub async fn fork_choice_before_proposal() {
slot_b,
);

let (block_c, state_c) = harness.make_block(state_a, slot_c);
let (block_c, state_c) = harness.make_block(state_a, slot_c).await;
let block_root_c = harness
.process_block(slot_c, block_c.clone())
.await
Expand Down
20 changes: 12 additions & 8 deletions beacon_node/http_api/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,14 @@ impl ApiTester {
"precondition: current slot is one after head"
);

let (next_block, _next_state) =
harness.make_block(head.beacon_state.clone(), harness.chain.slot().unwrap());
let (next_block, _next_state) = harness
.make_block(head.beacon_state.clone(), harness.chain.slot().unwrap())
.await;

// `make_block` adds random graffiti, so this will produce an alternate block
let (reorg_block, _reorg_state) =
harness.make_block(head.beacon_state.clone(), harness.chain.slot().unwrap());
let (reorg_block, _reorg_state) = harness
.make_block(head.beacon_state.clone(), harness.chain.slot().unwrap())
.await;

let head_state_root = head.beacon_state_root();
let attestations = harness
Expand Down Expand Up @@ -228,12 +230,14 @@ impl ApiTester {

let head = harness.chain.head().unwrap();

let (next_block, _next_state) =
harness.make_block(head.beacon_state.clone(), harness.chain.slot().unwrap());
let (next_block, _next_state) = harness
.make_block(head.beacon_state.clone(), harness.chain.slot().unwrap())
.await;

// `make_block` adds random graffiti, so this will produce an alternate block
let (reorg_block, _reorg_state) =
harness.make_block(head.beacon_state.clone(), harness.chain.slot().unwrap());
let (reorg_block, _reorg_state) = harness
.make_block(head.beacon_state.clone(), harness.chain.slot().unwrap())
.await;

let head_state_root = head.beacon_state_root();
let attestations = harness
Expand Down
13 changes: 7 additions & 6 deletions beacon_node/network/src/beacon_processor/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const STANDARD_TIMEOUT: Duration = Duration::from_secs(10);
/// Provides utilities for testing the `BeaconProcessor`.
struct TestRig {
chain: Arc<BeaconChain<T>>,
next_block: SignedBeaconBlock<E>,
next_block: Arc<SignedBeaconBlock<E>>,
attestations: Vec<(Attestation<E>, SubnetId)>,
next_block_attestations: Vec<(Attestation<E>, SubnetId)>,
next_block_aggregate_attestations: Vec<SignedAggregateAndProof<E>>,
Expand Down Expand Up @@ -103,8 +103,9 @@ impl TestRig {
"precondition: current slot is one after head"
);

let (next_block, next_state) =
harness.make_block(head.beacon_state.clone(), harness.chain.slot().unwrap());
let (next_block, next_state) = harness
.make_block(head.beacon_state.clone(), harness.chain.slot().unwrap())
.await;

let head_state_root = head.beacon_state_root();
let attestations = harness
Expand Down Expand Up @@ -210,7 +211,7 @@ impl TestRig {

Self {
chain,
next_block,
next_block: Arc::new(next_block),
attestations,
next_block_attestations,
next_block_aggregate_attestations,
Expand Down Expand Up @@ -239,15 +240,15 @@ impl TestRig {
junk_message_id(),
junk_peer_id(),
Client::default(),
Box::new(self.next_block.clone()),
self.next_block.clone(),
Duration::from_secs(0),
))
.unwrap();
}

pub fn enqueue_rpc_block(&self) {
let event = WorkEvent::rpc_beacon_block(
Box::new(self.next_block.clone()),
self.next_block.clone(),
std::time::Duration::default(),
BlockProcessType::ParentLookup {
chain_hash: Hash256::random(),
Expand Down
57 changes: 31 additions & 26 deletions consensus/fork_choice/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@ use fork_choice::{
};
use store::MemoryStore;
use types::{
test_utils::generate_deterministic_keypair, BeaconBlock, BeaconBlockRef, BeaconState,
ChainSpec, Checkpoint, Epoch, EthSpec, Hash256, IndexedAttestation, MainnetEthSpec, Slot,
SubnetId,
test_utils::generate_deterministic_keypair, BeaconBlockRef, BeaconState, ChainSpec, Checkpoint,
Epoch, EthSpec, Hash256, IndexedAttestation, MainnetEthSpec, SignedBeaconBlock, Slot, SubnetId,
};

pub type E = MainnetEthSpec;
Expand Down Expand Up @@ -198,7 +197,7 @@ impl ForkChoiceTest {
let validators = self.harness.get_all_validators();
loop {
let slot = self.harness.get_current_slot();
let (block, state_) = self.harness.make_block(state, slot);
let (block, state_) = self.harness.make_block(state, slot).await;
state = state_;
if !predicate(block.message(), &state) {
break;
Expand Down Expand Up @@ -276,9 +275,9 @@ impl ForkChoiceTest {
/// Applies a block directly to fork choice, bypassing the beacon chain.
///
/// Asserts the block was applied successfully.
pub fn apply_block_directly_to_fork_choice<F>(self, mut func: F) -> Self
pub async fn apply_block_directly_to_fork_choice<F>(self, mut func: F) -> Self
where
F: FnMut(&mut BeaconBlock<E>, &mut BeaconState<E>),
F: FnMut(&mut SignedBeaconBlock<E>, &mut BeaconState<E>),
{
let state = self
.harness
Expand All @@ -289,9 +288,8 @@ impl ForkChoiceTest {
)
.unwrap();
let slot = self.harness.get_current_slot();
let (signed_block, mut state) = self.harness.make_block(state, slot);
let (mut block, _) = signed_block.deconstruct();
func(&mut block, &mut state);
let (mut signed_block, mut state) = self.harness.make_block(state, slot).await;
func(&mut signed_block, &mut state);
let current_slot = self.harness.get_current_slot();
self.harness
.chain
Expand All @@ -300,8 +298,8 @@ impl ForkChoiceTest {
.fork_choice
.on_block(
current_slot,
&block,
block.canonical_root(),
signed_block.message(),
signed_block.canonical_root(),
Duration::from_secs(0),
&state,
PayloadVerificationStatus::Verified,
Expand All @@ -314,13 +312,13 @@ impl ForkChoiceTest {
/// Applies a block directly to fork choice, bypassing the beacon chain.
///
/// Asserts that an error occurred and allows inspecting it via `comparison_func`.
pub fn apply_invalid_block_directly_to_fork_choice<F, G>(
pub async fn apply_invalid_block_directly_to_fork_choice<F, G>(
self,
mut mutation_func: F,
mut comparison_func: G,
) -> Self
where
F: FnMut(&mut BeaconBlock<E>, &mut BeaconState<E>),
F: FnMut(&mut SignedBeaconBlock<E>, &mut BeaconState<E>),
G: FnMut(ForkChoiceError),
{
let state = self
Expand All @@ -332,9 +330,8 @@ impl ForkChoiceTest {
)
.unwrap();
let slot = self.harness.get_current_slot();
let (signed_block, mut state) = self.harness.make_block(state, slot);
let (mut block, _) = signed_block.deconstruct();
mutation_func(&mut block, &mut state);
let (mut signed_block, mut state) = self.harness.make_block(state, slot).await;
mutation_func(&mut signed_block, &mut state);
let current_slot = self.harness.get_current_slot();
let err = self
.harness
Expand All @@ -344,8 +341,8 @@ impl ForkChoiceTest {
.fork_choice
.on_block(
current_slot,
&block,
block.canonical_root(),
signed_block.message(),
signed_block.canonical_root(),
Duration::from_secs(0),
&state,
PayloadVerificationStatus::Verified,
Expand Down Expand Up @@ -593,6 +590,7 @@ async fn justified_checkpoint_updates_with_non_descendent_inside_safe_slots_with
.get_block_root(Epoch::new(1).start_slot(E::slots_per_epoch()))
.unwrap();
})
.await
.assert_justified_epoch(3)
.assert_best_justified_epoch(3);
}
Expand Down Expand Up @@ -621,6 +619,7 @@ async fn justified_checkpoint_updates_with_non_descendent_outside_safe_slots_wit
.get_block_root(Epoch::new(1).start_slot(E::slots_per_epoch()))
.unwrap();
})
.await
.assert_justified_epoch(2)
.assert_best_justified_epoch(3);
}
Expand Down Expand Up @@ -649,6 +648,7 @@ async fn justified_checkpoint_updates_with_non_descendent_outside_safe_slots_wit
.get_block_root(Epoch::new(1).start_slot(E::slots_per_epoch()))
.unwrap();
})
.await
.assert_justified_epoch(3)
.assert_best_justified_epoch(3);
}
Expand Down Expand Up @@ -689,7 +689,7 @@ async fn invalid_block_unknown_parent() {
.await
.apply_invalid_block_directly_to_fork_choice(
|block, _| {
*block.parent_root_mut() = junk;
*block.message_mut().parent_root_mut() = junk;
},
|err| {
assert_invalid_block!(
Expand All @@ -698,7 +698,8 @@ async fn invalid_block_unknown_parent() {
if parent == junk
)
},
);
)
.await;
}

/// Specification v0.12.1
Expand All @@ -711,10 +712,11 @@ async fn invalid_block_future_slot() {
.await
.apply_invalid_block_directly_to_fork_choice(
|block, _| {
*block.slot_mut() += 1;
*block.message_mut().slot_mut() += 1;
},
|err| assert_invalid_block!(err, InvalidBlock::FutureSlot { .. }),
);
)
.await;
}

/// Specification v0.12.1
Expand All @@ -730,7 +732,8 @@ async fn invalid_block_finalized_slot() {
.await
.apply_invalid_block_directly_to_fork_choice(
|block, _| {
*block.slot_mut() = Epoch::new(2).start_slot(E::slots_per_epoch()) - 1;
*block.message_mut().slot_mut() =
Epoch::new(2).start_slot(E::slots_per_epoch()) - 1;
},
|err| {
assert_invalid_block!(
Expand All @@ -739,7 +742,8 @@ async fn invalid_block_finalized_slot() {
if finalized_slot == Epoch::new(2).start_slot(E::slots_per_epoch())
)
},
);
)
.await;
}

/// Specification v0.12.1
Expand All @@ -763,7 +767,7 @@ async fn invalid_block_finalized_descendant() {
.assert_finalized_epoch(2)
.apply_invalid_block_directly_to_fork_choice(
|block, state| {
*block.parent_root_mut() = *state
*block.message_mut().parent_root_mut() = *state
.get_block_root(Epoch::new(1).start_slot(E::slots_per_epoch()))
.unwrap();
*invalid_ancestor.lock().unwrap() = block.parent_root();
Expand All @@ -775,7 +779,8 @@ async fn invalid_block_finalized_descendant() {
if block_ancestor == Some(*invalid_ancestor.lock().unwrap())
)
},
);
)
.await;
}

macro_rules! assert_invalid_attestation {
Expand Down

0 comments on commit f9634a6

Please sign in to comment.