mirror of
https://github.com/OffchainLabs/prysm.git
synced 2026-01-09 13:28:01 -05:00
Rm unused HashTreeRootState (#6697)
* Remove hash tree root implementations * Delete benchmark tests * Update existing tests * Gaz * Remove outdated ssz cache fuzz test Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com>
This commit is contained in:
@@ -22,6 +22,7 @@ go_library(
|
||||
"//tools/benchmark-files-gen:__pkg__",
|
||||
"//tools/pcli:__pkg__",
|
||||
"//shared/aggregation:__subpackages__",
|
||||
"//proto/testing:__subpackages__",
|
||||
],
|
||||
deps = [
|
||||
"//beacon-chain/core/state/stateutils:go_default_library",
|
||||
|
||||
@@ -44,7 +44,6 @@ go_test(
|
||||
"attestations_test.go",
|
||||
"benchmark_test.go",
|
||||
"blocks_test.go",
|
||||
"state_root_cache_fuzz_test.go",
|
||||
"state_root_test.go",
|
||||
"stateutil_test.go",
|
||||
"trie_helpers_test.go",
|
||||
@@ -57,12 +56,10 @@ go_test(
|
||||
"//shared/hashutil:go_default_library",
|
||||
"//shared/htrutils:go_default_library",
|
||||
"//shared/interop:go_default_library",
|
||||
"//shared/mputil:go_default_library",
|
||||
"//shared/params:go_default_library",
|
||||
"//shared/testutil:go_default_library",
|
||||
"//shared/testutil/assert:go_default_library",
|
||||
"//shared/testutil/require:go_default_library",
|
||||
"@com_github_google_gofuzz//:go_default_library",
|
||||
"@com_github_prysmaticlabs_ethereumapis//eth/v1alpha1:go_default_library",
|
||||
"@com_github_prysmaticlabs_go_ssz//:go_default_library",
|
||||
],
|
||||
|
||||
@@ -42,18 +42,6 @@ type stateRootHasher struct {
|
||||
rootsCache *ristretto.Cache
|
||||
}
|
||||
|
||||
// HashTreeRootState provides a fully-customized version of ssz.HashTreeRoot
|
||||
// for the BeaconState type of the official Ethereum Serenity specification.
|
||||
// The reason for this particular function is to optimize for speed and memory allocation
|
||||
// at the expense of complete specificity (that is, this function can only be used
|
||||
// on the Prysm BeaconState data structure).
|
||||
func HashTreeRootState(state *pb.BeaconState) ([32]byte, error) {
|
||||
if featureconfig.Get().EnableSSZCache {
|
||||
return cachedHasher.hashTreeRootState(state)
|
||||
}
|
||||
return nocachedHasher.hashTreeRootState(state)
|
||||
}
|
||||
|
||||
// ComputeFieldRoots returns the hash tree root computations of every field in
|
||||
// the beacon state as a list of 32 byte roots.
|
||||
func ComputeFieldRoots(state *pb.BeaconState) ([][]byte, error) {
|
||||
@@ -63,23 +51,6 @@ func ComputeFieldRoots(state *pb.BeaconState) ([][]byte, error) {
|
||||
return nocachedHasher.computeFieldRoots(state)
|
||||
}
|
||||
|
||||
func (h *stateRootHasher) hashTreeRootState(state *pb.BeaconState) ([32]byte, error) {
|
||||
var fieldRoots [][]byte
|
||||
var err error
|
||||
if featureconfig.Get().EnableSSZCache {
|
||||
fieldRoots, err = cachedHasher.computeFieldRoots(state)
|
||||
if err != nil {
|
||||
return [32]byte{}, err
|
||||
}
|
||||
} else {
|
||||
fieldRoots, err = nocachedHasher.computeFieldRoots(state)
|
||||
if err != nil {
|
||||
return [32]byte{}, err
|
||||
}
|
||||
}
|
||||
return htrutils.BitwiseMerkleize(hashutil.CustomSHA256Hasher(), fieldRoots, uint64(len(fieldRoots)), uint64(len(fieldRoots)))
|
||||
}
|
||||
|
||||
func (h *stateRootHasher) computeFieldRoots(state *pb.BeaconState) ([][]byte, error) {
|
||||
if state == nil {
|
||||
return nil, errors.New("nil state")
|
||||
|
||||
@@ -1,130 +0,0 @@
|
||||
package stateutil
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
fuzz "github.com/google/gofuzz"
|
||||
ethereum_beacon_p2p_v1 "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
|
||||
"github.com/prysmaticlabs/prysm/shared/mputil"
|
||||
"github.com/prysmaticlabs/prysm/shared/params"
|
||||
)
|
||||
|
||||
func init() {
|
||||
params.UseMinimalConfig()
|
||||
}
|
||||
|
||||
func TestStateRootCacheFuzz_10(t *testing.T) {
|
||||
fuzzStateRootCache(t, 0, 10)
|
||||
}
|
||||
|
||||
func TestStateRootCacheFuzz_100(t *testing.T) {
|
||||
fuzzStateRootCache(t, 1, 100)
|
||||
}
|
||||
|
||||
func TestStateRootCacheFuzz_1000(t *testing.T) {
|
||||
fuzzStateRootCache(t, 2, 1000)
|
||||
}
|
||||
|
||||
func fuzzStateRootCache(t *testing.T, seed int64, iterations uint64) {
|
||||
fuzzer := fuzz.NewWithSeed(seed)
|
||||
|
||||
hasher := &stateRootHasher{}
|
||||
hasherWithCache := cachedHasher
|
||||
|
||||
mismatch := 0
|
||||
mismatchedIndices := make([]uint64, 0)
|
||||
|
||||
// Use scatter to run tests in parallel.
|
||||
if _, err := mputil.Scatter(int(iterations), func(start int, length int, _ *sync.RWMutex) (i interface{}, err error) {
|
||||
state := ðereum_beacon_p2p_v1.BeaconState{}
|
||||
for i := start; i < start+length; i++ {
|
||||
func() {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
// Ignore fuzzing panics for out of range values
|
||||
}
|
||||
}()
|
||||
fuzzer.Fuzz(state)
|
||||
}()
|
||||
var a, b [32]byte
|
||||
func() {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
t.Errorf("Non-cached HTR panicked on iteration %d", i)
|
||||
panic(r)
|
||||
}
|
||||
}()
|
||||
var err error
|
||||
a, err = hasher.hashTreeRootState(state)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}()
|
||||
|
||||
func() {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
t.Errorf("Cached HTR panicked on iteration %d", i)
|
||||
panic(r)
|
||||
}
|
||||
}()
|
||||
var err error
|
||||
b, err = hasherWithCache.hashTreeRootState(state)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}()
|
||||
|
||||
if a != b {
|
||||
mismatch++
|
||||
mismatchedIndices = append(mismatchedIndices, uint64(i))
|
||||
}
|
||||
}
|
||||
return nil, nil
|
||||
}); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
if mismatch > 0 {
|
||||
t.Errorf("Mismatched indices: %v", mismatchedIndices)
|
||||
t.Fatalf("%d of %d random states had different roots", mismatch, iterations)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHashTreeRootState_ElementsChanged_RecomputeBranch(t *testing.T) {
|
||||
hasher := &stateRootHasher{}
|
||||
hasherWithCache := cachedHasher
|
||||
state := ðereum_beacon_p2p_v1.BeaconState{}
|
||||
initialRoots := make([][]byte, 5)
|
||||
for i := 0; i < len(initialRoots); i++ {
|
||||
var someRt [32]byte
|
||||
copy(someRt[:], "hello")
|
||||
initialRoots[i] = someRt[:]
|
||||
}
|
||||
state.RandaoMixes = initialRoots
|
||||
if _, err := hasherWithCache.hashTreeRootState(state); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
badRoots := make([][]byte, 5)
|
||||
for i := 0; i < len(badRoots); i++ {
|
||||
var someRt [32]byte
|
||||
copy(someRt[:], strconv.Itoa(i))
|
||||
badRoots[i] = someRt[:]
|
||||
}
|
||||
|
||||
state.RandaoMixes = badRoots
|
||||
r1, err := hasher.hashTreeRootState(state)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
r2, err := hasherWithCache.hashTreeRootState(state)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if r1 != r2 {
|
||||
t.Errorf("Wanted %#x (nocache), received %#x (withcache)", r1, r2)
|
||||
}
|
||||
}
|
||||
@@ -8,7 +8,6 @@ import (
|
||||
|
||||
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
|
||||
"github.com/prysmaticlabs/go-ssz"
|
||||
"github.com/prysmaticlabs/prysm/beacon-chain/state/stateutil"
|
||||
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
|
||||
"github.com/prysmaticlabs/prysm/shared/interop"
|
||||
"github.com/prysmaticlabs/prysm/shared/params"
|
||||
@@ -29,36 +28,6 @@ func TestState_FieldCount(t *testing.T) {
|
||||
assert.Equal(t, count, numFields)
|
||||
}
|
||||
|
||||
func BenchmarkHashTreeRootState_Custom_512(b *testing.B) {
|
||||
b.StopTimer()
|
||||
genesisState := setupGenesisState(b, 512)
|
||||
b.StartTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
_, err := stateutil.HashTreeRootState(genesisState)
|
||||
require.NoError(b, err)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkHashTreeRootState_Custom_16384(b *testing.B) {
|
||||
b.StopTimer()
|
||||
genesisState := setupGenesisState(b, 16384)
|
||||
b.StartTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
_, err := stateutil.HashTreeRootState(genesisState)
|
||||
require.NoError(b, err)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkHashTreeRootState_Custom_300000(b *testing.B) {
|
||||
b.StopTimer()
|
||||
genesisState := setupGenesisState(b, 300000)
|
||||
b.StartTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
_, err := stateutil.HashTreeRootState(genesisState)
|
||||
require.NoError(b, err)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkHashTreeRoot_Generic_512(b *testing.B) {
|
||||
b.StopTimer()
|
||||
genesisState := setupGenesisState(b, 512)
|
||||
|
||||
@@ -10,7 +10,6 @@ import (
|
||||
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
|
||||
"github.com/prysmaticlabs/go-ssz"
|
||||
stateTrie "github.com/prysmaticlabs/prysm/beacon-chain/state"
|
||||
"github.com/prysmaticlabs/prysm/beacon-chain/state/stateutil"
|
||||
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
|
||||
"github.com/prysmaticlabs/prysm/shared/bytesutil"
|
||||
"github.com/prysmaticlabs/prysm/shared/interop"
|
||||
@@ -35,7 +34,9 @@ func TestBeaconState_ProtoBeaconStateCompatibility(t *testing.T) {
|
||||
|
||||
r1, err := customState.HashTreeRoot(ctx)
|
||||
require.NoError(t, err)
|
||||
r2, err := stateutil.HashTreeRootState(genesis)
|
||||
beaconState, err := stateTrie.InitializeFromProto(genesis)
|
||||
require.NoError(t, err)
|
||||
r2, err := beaconState.HashTreeRoot(context.Background())
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, r1, r2, "Mismatched roots")
|
||||
|
||||
@@ -46,7 +47,9 @@ func TestBeaconState_ProtoBeaconStateCompatibility(t *testing.T) {
|
||||
r1, err = customState.HashTreeRoot(ctx)
|
||||
require.NoError(t, err)
|
||||
genesis.Balances = balances
|
||||
r2, err = stateutil.HashTreeRootState(genesis)
|
||||
beaconState, err = stateTrie.InitializeFromProto(genesis)
|
||||
require.NoError(t, err)
|
||||
r2, err = beaconState.HashTreeRoot(context.Background())
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, r1, r2, "Mismatched roots")
|
||||
}
|
||||
|
||||
@@ -71,7 +71,6 @@ test_suite(
|
||||
":deposit_fuzz_test_with_libfuzzer",
|
||||
":proposer_slashing_fuzz_test_with_libfuzzer",
|
||||
":rpc_status_fuzz_test_with_libfuzzer",
|
||||
":ssz_cache_fuzz_test_with_libfuzzer",
|
||||
":voluntary_exit_fuzz_test_with_libfuzzer",
|
||||
],
|
||||
)
|
||||
@@ -199,21 +198,6 @@ go_fuzz_test(
|
||||
] + COMMON_DEPS,
|
||||
)
|
||||
|
||||
go_fuzz_test(
|
||||
name = "ssz_cache_fuzz_test",
|
||||
srcs = [
|
||||
"ssz_cache_fuzz.go",
|
||||
] + COMMON_SRCS,
|
||||
corpus = "@sigp_beacon_fuzz_corpora//:0_11_0_mainnet_beaconstate",
|
||||
corpus_path = "external/sigp_beacon_fuzz_corpora/0-11-0/mainnet/beaconstate",
|
||||
func = "BeaconFuzzSSZCache",
|
||||
importpath = IMPORT_PATH,
|
||||
deps = [
|
||||
"//proto/beacon/p2p/v1:go_default_library",
|
||||
"//beacon-chain/state/stateutil:go_default_library",
|
||||
] + COMMON_DEPS,
|
||||
)
|
||||
|
||||
go_fuzz_test(
|
||||
name = "voluntary_exit_fuzz_test",
|
||||
srcs = [
|
||||
@@ -242,7 +226,6 @@ go_library(
|
||||
"deposit_fuzz.go",
|
||||
"inputs.go",
|
||||
"rpc_status_fuzz.go",
|
||||
"ssz_cache_fuzz.go",
|
||||
"voluntary_exit_fuzz.go",
|
||||
":ssz_generated_files", # keep
|
||||
],
|
||||
|
||||
@@ -1,36 +0,0 @@
|
||||
package fuzz
|
||||
|
||||
import (
|
||||
"github.com/prysmaticlabs/prysm/beacon-chain/state/stateutil"
|
||||
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
|
||||
"github.com/prysmaticlabs/prysm/shared/featureconfig"
|
||||
)
|
||||
|
||||
// BeaconFuzzSSZCache for testing critical paths along the ssz cache for beacon state HTR.
|
||||
func BeaconFuzzSSZCache(input []byte) {
|
||||
s := &pb.BeaconState{}
|
||||
if err := s.UnmarshalSSZ(input); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
fc := featureconfig.Get()
|
||||
fc.EnableSSZCache = true
|
||||
featureconfig.Init(fc)
|
||||
|
||||
a, err := stateutil.HashTreeRootState(s)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
fc.EnableSSZCache = false
|
||||
featureconfig.Init(fc)
|
||||
|
||||
b, err := stateutil.HashTreeRootState(s)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if a != b {
|
||||
panic("Cached and non cached hash tree root hashers produced different results")
|
||||
}
|
||||
}
|
||||
@@ -46,11 +46,13 @@ go_test(
|
||||
embed = [":go_default_library"],
|
||||
tags = ["spectest"],
|
||||
deps = [
|
||||
"//beacon-chain/state:go_default_library",
|
||||
"//beacon-chain/state/stateutil:go_default_library",
|
||||
"//proto/beacon/p2p/v1:go_default_library",
|
||||
"//shared/bytesutil:go_default_library",
|
||||
"//shared/params/spectest:go_default_library",
|
||||
"//shared/testutil:go_default_library",
|
||||
"//shared/testutil/require:go_default_library",
|
||||
"@com_github_ferranbt_fastssz//:go_default_library",
|
||||
"@com_github_ghodss_yaml//:go_default_library",
|
||||
"@com_github_prysmaticlabs_ethereumapis//eth/v1alpha1:go_default_library",
|
||||
@@ -77,11 +79,13 @@ go_test(
|
||||
"spectest",
|
||||
],
|
||||
deps = [
|
||||
"//beacon-chain/state:go_default_library",
|
||||
"//beacon-chain/state/stateutil:go_default_library",
|
||||
"//proto/beacon/p2p/v1:go_default_library",
|
||||
"//shared/bytesutil:go_default_library",
|
||||
"//shared/params/spectest:go_default_library",
|
||||
"//shared/testutil:go_default_library",
|
||||
"//shared/testutil/require:go_default_library",
|
||||
"@com_github_ferranbt_fastssz//:go_default_library",
|
||||
"@com_github_ghodss_yaml//:go_default_library",
|
||||
"@com_github_prysmaticlabs_ethereumapis//eth/v1alpha1:go_default_library",
|
||||
@@ -103,11 +107,13 @@ go_test(
|
||||
],
|
||||
tags = ["spectest"],
|
||||
deps = [
|
||||
"//beacon-chain/state:go_default_library",
|
||||
"//beacon-chain/state/stateutil:go_default_library",
|
||||
"//proto/beacon/p2p/v1:go_default_library",
|
||||
"//shared/bytesutil:go_default_library",
|
||||
"//shared/params/spectest:go_default_library",
|
||||
"//shared/testutil:go_default_library",
|
||||
"//shared/testutil/require:go_default_library",
|
||||
"@com_github_ferranbt_fastssz//:go_default_library",
|
||||
"@com_github_ghodss_yaml//:go_default_library",
|
||||
"@com_github_prysmaticlabs_ethereumapis//eth/v1alpha1:go_default_library",
|
||||
|
||||
@@ -2,6 +2,7 @@ package testing
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"path"
|
||||
@@ -10,10 +11,11 @@ import (
|
||||
fssz "github.com/ferranbt/fastssz"
|
||||
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
|
||||
"github.com/prysmaticlabs/go-ssz"
|
||||
"github.com/prysmaticlabs/prysm/beacon-chain/state/stateutil"
|
||||
"github.com/prysmaticlabs/prysm/beacon-chain/state"
|
||||
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
|
||||
"github.com/prysmaticlabs/prysm/shared/params/spectest"
|
||||
"github.com/prysmaticlabs/prysm/shared/testutil"
|
||||
"github.com/prysmaticlabs/prysm/shared/testutil/require"
|
||||
)
|
||||
|
||||
// SSZRoots --
|
||||
@@ -56,7 +58,9 @@ func runSSZStaticTests(t *testing.T, config string) {
|
||||
var htr func(interface{}) ([32]byte, error)
|
||||
if _, ok := object.(*pb.BeaconState); ok {
|
||||
htr = func(s interface{}) ([32]byte, error) {
|
||||
return stateutil.HashTreeRootState(s.(*pb.BeaconState))
|
||||
beaconState, err := state.InitializeFromProto(s.(*pb.BeaconState))
|
||||
require.NoError(t, err)
|
||||
return beaconState.HashTreeRoot(context.Background())
|
||||
}
|
||||
} else {
|
||||
htr = ssz.HashTreeRoot
|
||||
|
||||
Reference in New Issue
Block a user