Unimplement TreeHash for BeaconState (#6083)

* Unimplement `TreeHash` for `BeaconState`
This commit is contained in:
Michael Sproul
2024-07-12 23:06:08 +10:00
committed by GitHub
parent 0c0b56d9e8
commit 2f0af2be89
16 changed files with 117 additions and 70 deletions

View File

@@ -7,7 +7,6 @@ use eth2::types::{BroadcastValidation, PublishBlockRequest};
use http_api::test_utils::InteractiveTester;
use http_api::{publish_blinded_block, publish_block, reconstruct_block, ProvenancedBlock};
use std::sync::Arc;
use tree_hash::TreeHash;
use types::{Epoch, EthSpec, ForkName, Hash256, MainnetEthSpec, Slot};
use warp::Rejection;
use warp_utils::reject::CustomBadRequest;
@@ -353,13 +352,20 @@ pub async fn consensus_partial_pass_only_consensus() {
let slot_b = slot_a + 1;
let state_a = tester.harness.get_current_state();
let ((block_a, _), state_after_a) = tester.harness.make_block(state_a.clone(), slot_b).await;
let ((block_b, blobs_b), state_after_b) = tester.harness.make_block(state_a, slot_b).await;
let ((block_a, _), mut state_after_a) =
tester.harness.make_block(state_a.clone(), slot_b).await;
let ((block_b, blobs_b), mut state_after_b) = tester.harness.make_block(state_a, slot_b).await;
let block_b_root = block_b.canonical_root();
/* check for `make_block` curios */
assert_eq!(block_a.state_root(), state_after_a.tree_hash_root());
assert_eq!(block_b.state_root(), state_after_b.tree_hash_root());
assert_eq!(
block_a.state_root(),
state_after_a.canonical_root().unwrap()
);
assert_eq!(
block_b.state_root(),
state_after_b.canonical_root().unwrap()
);
assert_ne!(block_a.state_root(), block_b.state_root());
let gossip_block_contents_b = PublishBlockRequest::new(block_b, blobs_b)
@@ -516,13 +522,19 @@ pub async fn equivocation_consensus_early_equivocation() {
let slot_b = slot_a + 1;
let state_a = tester.harness.get_current_state();
let ((block_a, blobs_a), state_after_a) =
let ((block_a, blobs_a), mut state_after_a) =
tester.harness.make_block(state_a.clone(), slot_b).await;
let ((block_b, blobs_b), state_after_b) = tester.harness.make_block(state_a, slot_b).await;
let ((block_b, blobs_b), mut state_after_b) = tester.harness.make_block(state_a, slot_b).await;
/* check for `make_block` curios */
assert_eq!(block_a.state_root(), state_after_a.tree_hash_root());
assert_eq!(block_b.state_root(), state_after_b.tree_hash_root());
assert_eq!(
block_a.state_root(),
state_after_a.canonical_root().unwrap()
);
assert_eq!(
block_b.state_root(),
state_after_b.canonical_root().unwrap()
);
assert_ne!(block_a.state_root(), block_b.state_root());
/* submit `block_a` as valid */
@@ -642,13 +654,19 @@ pub async fn equivocation_consensus_late_equivocation() {
let slot_b = slot_a + 1;
let state_a = tester.harness.get_current_state();
let ((block_a, blobs_a), state_after_a) =
let ((block_a, blobs_a), mut state_after_a) =
tester.harness.make_block(state_a.clone(), slot_b).await;
let ((block_b, blobs_b), state_after_b) = tester.harness.make_block(state_a, slot_b).await;
let ((block_b, blobs_b), mut state_after_b) = tester.harness.make_block(state_a, slot_b).await;
/* check for `make_block` curios */
assert_eq!(block_a.state_root(), state_after_a.tree_hash_root());
assert_eq!(block_b.state_root(), state_after_b.tree_hash_root());
assert_eq!(
block_a.state_root(),
state_after_a.canonical_root().unwrap()
);
assert_eq!(
block_b.state_root(),
state_after_b.canonical_root().unwrap()
);
assert_ne!(block_a.state_root(), block_b.state_root());
let gossip_block_contents_b = PublishBlockRequest::new(block_b, blobs_b)
@@ -1135,15 +1153,21 @@ pub async fn blinded_equivocation_consensus_early_equivocation() {
let slot_b = slot_a + 1;
let state_a = tester.harness.get_current_state();
let (block_a, state_after_a) = tester
let (block_a, mut state_after_a) = tester
.harness
.make_blinded_block(state_a.clone(), slot_b)
.await;
let (block_b, state_after_b) = tester.harness.make_blinded_block(state_a, slot_b).await;
let (block_b, mut state_after_b) = tester.harness.make_blinded_block(state_a, slot_b).await;
/* check for `make_blinded_block` curios */
assert_eq!(block_a.state_root(), state_after_a.tree_hash_root());
assert_eq!(block_b.state_root(), state_after_b.tree_hash_root());
assert_eq!(
block_a.state_root(),
state_after_a.canonical_root().unwrap()
);
assert_eq!(
block_b.state_root(),
state_after_b.canonical_root().unwrap()
);
assert_ne!(block_a.state_root(), block_b.state_root());
/* submit `block_a` as valid */
@@ -1259,16 +1283,22 @@ pub async fn blinded_equivocation_consensus_late_equivocation() {
let slot_b = slot_a + 1;
let state_a = tester.harness.get_current_state();
let (block_a, state_after_a) = tester
let (block_a, mut state_after_a) = tester
.harness
.make_blinded_block(state_a.clone(), slot_b)
.await;
let (block_b, state_after_b) = tester.harness.make_blinded_block(state_a, slot_b).await;
let (block_b, mut state_after_b) = tester.harness.make_blinded_block(state_a, slot_b).await;
let block_b = Arc::new(block_b);
/* check for `make_blinded_block` curios */
assert_eq!(block_a.state_root(), state_after_a.tree_hash_root());
assert_eq!(block_b.state_root(), state_after_b.tree_hash_root());
assert_eq!(
block_a.state_root(),
state_after_a.canonical_root().unwrap()
);
assert_eq!(
block_b.state_root(),
state_after_b.canonical_root().unwrap()
);
assert_ne!(block_a.state_root(), block_b.state_root());
let unblinded_block_a = reconstruct_block(

View File

@@ -55,7 +55,7 @@ async fn sync_committee_duties_across_fork() {
// though the head state hasn't transitioned yet.
let fork_slot = fork_epoch.start_slot(E::slots_per_epoch());
let (genesis_state, genesis_state_root) = harness.get_current_state_and_root();
let (_, state) = harness
let (_, mut state) = harness
.add_attested_block_at_slot(
fork_slot - 1,
genesis_state,
@@ -76,7 +76,7 @@ async fn sync_committee_duties_across_fork() {
assert_eq!(sync_duties.len(), E::sync_committee_size());
// After applying a block at the fork slot the duties should remain unchanged.
let state_root = state.canonical_root();
let state_root = state.canonical_root().unwrap();
harness
.add_attested_block_at_slot(fork_slot, state, state_root, &all_validators)
.await
@@ -257,7 +257,7 @@ async fn sync_committee_indices_across_fork() {
// applied.
let fork_slot = fork_epoch.start_slot(E::slots_per_epoch());
let (genesis_state, genesis_state_root) = harness.get_current_state_and_root();
let (_, state) = harness
let (_, mut state) = harness
.add_attested_block_at_slot(
fork_slot - 1,
genesis_state,
@@ -295,7 +295,7 @@ async fn sync_committee_indices_across_fork() {
// Once the head is updated it should be useable for requests, including in the next sync
// committee period.
let state_root = state.canonical_root();
let state_root = state.canonical_root().unwrap();
harness
.add_attested_block_at_slot(fork_slot + 1, state, state_root, &all_validators)
.await

View File

@@ -17,7 +17,6 @@ use state_processing::{
use std::collections::HashMap;
use std::sync::Arc;
use std::time::Duration;
use tree_hash::TreeHash;
use types::{
Address, Epoch, EthSpec, ExecPayload, ExecutionBlockHash, ForkName, MainnetEthSpec,
MinimalEthSpec, ProposerPreparationData, Slot,
@@ -515,16 +514,17 @@ pub async fn proposer_boost_re_org_test(
}
harness.advance_slot();
let (block_a_root, block_a, state_a) = harness
let (block_a_root, block_a, mut state_a) = harness
.add_block_at_slot(slot_a, harness.get_current_state())
.await
.unwrap();
let state_a_root = state_a.canonical_root().unwrap();
// Attest to block A during slot A.
let (block_a_parent_votes, _) = harness.make_attestations_with_limit(
&all_validators,
&state_a,
state_a.canonical_root(),
state_a_root,
block_a_root,
slot_a,
num_parent_votes,
@@ -538,7 +538,7 @@ pub async fn proposer_boost_re_org_test(
let (block_a_empty_votes, block_a_attesters) = harness.make_attestations_with_limit(
&all_validators,
&state_a,
state_a.canonical_root(),
state_a_root,
block_a_root,
slot_b,
num_empty_votes,
@@ -553,6 +553,7 @@ pub async fn proposer_boost_re_org_test(
// Produce block B and process it halfway through the slot.
let (block_b, mut state_b) = harness.make_block(state_a.clone(), slot_b).await;
let state_b_root = state_b.canonical_root().unwrap();
let block_b_root = block_b.0.canonical_root();
let obs_time = slot_clock.start_of(slot_b).unwrap() + slot_clock.slot_duration() / 2;
@@ -570,7 +571,7 @@ pub async fn proposer_boost_re_org_test(
let (block_b_head_votes, _) = harness.make_attestations_with_limit(
&remaining_attesters,
&state_b,
state_b.canonical_root(),
state_b_root,
block_b_root.into(),
slot_b,
num_head_votes,
@@ -774,32 +775,34 @@ 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).await;
let (block_b, mut state_b) = harness.make_block(state_a.clone(), slot_b).await;
let block_root_b = harness
.process_block(slot_b, block_b.0.canonical_root(), block_b)
.await
.unwrap();
let state_root_b = state_b.canonical_root().unwrap();
// Create attestations to B but keep them in reserve until after C has been processed.
let attestations_b = harness.make_attestations(
&all_validators,
&state_b,
state_b.tree_hash_root(),
state_root_b,
block_root_b,
slot_b,
);
let (block_c, state_c) = harness.make_block(state_a, slot_c).await;
let (block_c, mut state_c) = harness.make_block(state_a, slot_c).await;
let block_root_c = harness
.process_block(slot_c, block_c.0.canonical_root(), block_c.clone())
.await
.unwrap();
let state_root_c = state_c.canonical_root().unwrap();
// Create attestations to C from a small number of validators and process them immediately.
let attestations_c = harness.make_attestations(
&all_validators[..validator_count / 2],
&state_c,
state_c.tree_hash_root(),
state_root_c,
block_root_c,
slot_c,
);