Skip to content

Commit 00dacbd

Browse files
authored
Remove custom block body root and block root methods (#8069)
* Remove custom block body root and block root methods * Add nil checks and fix tests * Fmt * Typo
1 parent 92736d0 commit 00dacbd

File tree

10 files changed

+11
-201
lines changed

10 files changed

+11
-201
lines changed

beacon-chain/state/stateutil/BUILD.bazel

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ go_library(
3535
"@com_github_dgraph_io_ristretto//:go_default_library",
3636
"@com_github_pkg_errors//:go_default_library",
3737
"@com_github_prysmaticlabs_ethereumapis//eth/v1alpha1:go_default_library",
38-
"@com_github_prysmaticlabs_go_ssz//:go_default_library",
3938
],
4039
)
4140

@@ -44,7 +43,6 @@ go_test(
4443
srcs = [
4544
"attestations_test.go",
4645
"benchmark_test.go",
47-
"blocks_test.go",
4846
"state_root_test.go",
4947
"stateutil_test.go",
5048
"trie_helpers_test.go",

beacon-chain/state/stateutil/benchmark_test.go

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,47 +3,11 @@ package stateutil_test
33
import (
44
"testing"
55

6-
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
7-
"github.com/prysmaticlabs/prysm/beacon-chain/state/stateutil"
86
"github.com/prysmaticlabs/prysm/shared/hashutil"
97
"github.com/prysmaticlabs/prysm/shared/htrutils"
10-
"github.com/prysmaticlabs/prysm/shared/testutil"
118
"github.com/prysmaticlabs/prysm/shared/testutil/require"
129
)
1310

14-
func BenchmarkBlockHTR(b *testing.B) {
15-
genState, keys := testutil.DeterministicGenesisState(b, 200)
16-
conf := testutil.DefaultBlockGenConfig()
17-
blk, err := testutil.GenerateFullBlock(genState, keys, conf, 10)
18-
require.NoError(b, err)
19-
atts := make([]*ethpb.Attestation, 0, 128)
20-
for i := 0; i < 128; i++ {
21-
atts = append(atts, blk.Block.Body.Attestations[0])
22-
}
23-
deposits, _, err := testutil.DeterministicDepositsAndKeys(16)
24-
require.NoError(b, err)
25-
blk.Block.Body.Attestations = atts
26-
blk.Block.Body.Deposits = deposits
27-
28-
b.Run("SSZ_HTR", func(b *testing.B) {
29-
b.ResetTimer()
30-
b.ReportAllocs()
31-
for i := 0; i < b.N; i++ {
32-
_, err := stateutil.BlockRoot(blk.Block)
33-
require.NoError(b, err)
34-
}
35-
})
36-
37-
b.Run("Custom_SSZ_HTR", func(b *testing.B) {
38-
b.ResetTimer()
39-
b.ReportAllocs()
40-
for i := 0; i < b.N; i++ {
41-
_, err := stateutil.BlockRoot(blk.Block)
42-
require.NoError(b, err)
43-
}
44-
})
45-
}
46-
4711
func BenchmarkMerkleize_Buffered(b *testing.B) {
4812
roots := make([][32]byte, 8192)
4913
for i := 0; i < 8192; i++ {

beacon-chain/state/stateutil/blocks.go

Lines changed: 0 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66

77
"github.com/pkg/errors"
88
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
9-
"github.com/prysmaticlabs/go-ssz"
109
"github.com/prysmaticlabs/prysm/shared/bytesutil"
1110
"github.com/prysmaticlabs/prysm/shared/featureconfig"
1211
"github.com/prysmaticlabs/prysm/shared/hashutil"
@@ -38,105 +37,6 @@ func BlockHeaderRoot(header *ethpb.BeaconBlockHeader) ([32]byte, error) {
3837
return htrutils.BitwiseMerkleize(hashutil.CustomSHA256Hasher(), fieldRoots, uint64(len(fieldRoots)), uint64(len(fieldRoots)))
3938
}
4039

41-
// BlockRoot returns the block hash tree root of the provided block.
42-
func BlockRoot(blk *ethpb.BeaconBlock) ([32]byte, error) {
43-
fieldRoots := make([][32]byte, 5)
44-
if blk != nil {
45-
headerSlotBuf := make([]byte, 8)
46-
binary.LittleEndian.PutUint64(headerSlotBuf, blk.Slot)
47-
headerSlotRoot := bytesutil.ToBytes32(headerSlotBuf)
48-
fieldRoots[0] = headerSlotRoot
49-
proposerIdxBuf := make([]byte, 8)
50-
binary.LittleEndian.PutUint64(proposerIdxBuf, blk.ProposerIndex)
51-
proposerIndexRoot := bytesutil.ToBytes32(proposerIdxBuf)
52-
fieldRoots[1] = proposerIndexRoot
53-
parentRoot := bytesutil.ToBytes32(blk.ParentRoot)
54-
fieldRoots[2] = parentRoot
55-
stateRoot := bytesutil.ToBytes32(blk.StateRoot)
56-
fieldRoots[3] = stateRoot
57-
bodyRoot, err := BlockBodyRoot(blk.Body)
58-
if err != nil {
59-
return [32]byte{}, err
60-
}
61-
fieldRoots[4] = bodyRoot
62-
}
63-
return htrutils.BitwiseMerkleizeArrays(hashutil.CustomSHA256Hasher(), fieldRoots, uint64(len(fieldRoots)), uint64(len(fieldRoots)))
64-
}
65-
66-
// BlockBodyRoot returns the hash tree root of the block body.
67-
func BlockBodyRoot(body *ethpb.BeaconBlockBody) ([32]byte, error) {
68-
if body == nil {
69-
// Treat nil body to be the same as empty. This is mostly for test setup purposes and would
70-
// be very unlikely to happen in production workflow.
71-
emptyRoot := make([]byte, 32)
72-
emptyRandao := make([]byte, 96)
73-
body = &ethpb.BeaconBlockBody{
74-
RandaoReveal: emptyRandao,
75-
Eth1Data: &ethpb.Eth1Data{
76-
DepositRoot: emptyRoot,
77-
DepositCount: 0,
78-
BlockHash: emptyRoot,
79-
},
80-
Graffiti: emptyRoot,
81-
ProposerSlashings: make([]*ethpb.ProposerSlashing, 0),
82-
AttesterSlashings: make([]*ethpb.AttesterSlashing, 0),
83-
Attestations: make([]*ethpb.Attestation, 0),
84-
Deposits: make([]*ethpb.Deposit, 0),
85-
VoluntaryExits: make([]*ethpb.SignedVoluntaryExit, 0),
86-
}
87-
}
88-
hasher := hashutil.CustomSHA256Hasher()
89-
fieldRoots := make([][32]byte, 8)
90-
rawRandao := bytesutil.ToBytes96(body.RandaoReveal)
91-
packedRandao, err := htrutils.Pack([][]byte{rawRandao[:]})
92-
if err != nil {
93-
return [32]byte{}, err
94-
}
95-
randaoRoot, err := htrutils.BitwiseMerkleize(hasher, packedRandao, uint64(len(packedRandao)), uint64(len(packedRandao)))
96-
if err != nil {
97-
return [32]byte{}, err
98-
}
99-
fieldRoots[0] = randaoRoot
100-
101-
eth1Root, err := Eth1Root(hasher, body.Eth1Data)
102-
if err != nil {
103-
return [32]byte{}, err
104-
}
105-
fieldRoots[1] = eth1Root
106-
107-
graffitiRoot := bytesutil.ToBytes32(body.Graffiti)
108-
fieldRoots[2] = graffitiRoot
109-
110-
proposerSlashingsRoot, err := ssz.HashTreeRootWithCapacity(body.ProposerSlashings, params.BeaconConfig().MaxProposerSlashings)
111-
if err != nil {
112-
return [32]byte{}, err
113-
}
114-
fieldRoots[3] = proposerSlashingsRoot
115-
attesterSlashingsRoot, err := ssz.HashTreeRootWithCapacity(body.AttesterSlashings, params.BeaconConfig().MaxAttesterSlashings)
116-
if err != nil {
117-
return [32]byte{}, err
118-
}
119-
fieldRoots[4] = attesterSlashingsRoot
120-
attsRoot, err := blockAttestationRoot(body.Attestations)
121-
if err != nil {
122-
return [32]byte{}, err
123-
}
124-
fieldRoots[5] = attsRoot
125-
126-
depositRoot, err := ssz.HashTreeRootWithCapacity(body.Deposits, params.BeaconConfig().MaxDeposits)
127-
if err != nil {
128-
return [32]byte{}, err
129-
}
130-
fieldRoots[6] = depositRoot
131-
132-
exitRoot, err := ssz.HashTreeRootWithCapacity(body.VoluntaryExits, params.BeaconConfig().MaxVoluntaryExits)
133-
if err != nil {
134-
return [32]byte{}, err
135-
}
136-
fieldRoots[7] = exitRoot
137-
return htrutils.BitwiseMerkleizeArrays(hasher, fieldRoots, uint64(len(fieldRoots)), uint64(len(fieldRoots)))
138-
}
139-
14040
// Eth1Root computes the HashTreeRoot Merkleization of
14141
// a BeaconBlockHeader struct according to the eth2
14242
// Simple Serialize specification.

beacon-chain/state/stateutil/blocks_test.go

Lines changed: 0 additions & 45 deletions
This file was deleted.

proto/migration/BUILD.bazel

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ go_library(
66
importpath = "github.com/prysmaticlabs/prysm/proto/migration",
77
visibility = ["//visibility:public"],
88
deps = [
9-
"//beacon-chain/state/stateutil:go_default_library",
109
"@com_github_golang_protobuf//proto:go_default_library",
1110
"@com_github_pkg_errors//:go_default_library",
1211
"@com_github_prysmaticlabs_ethereumapis//eth/v1:go_default_library",

proto/migration/migration.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,11 @@ import (
55
"github.com/pkg/errors"
66
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1"
77
ethpb_alpha "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
8-
"github.com/prysmaticlabs/prysm/beacon-chain/state/stateutil"
98
)
109

1110
// V1Alpha1BlockToV1BlockHeader converts a v1alpha1 SignedBeaconBlock proto to a v1 SignedBeaconBlockHeader proto.
1211
func V1Alpha1BlockToV1BlockHeader(block *ethpb_alpha.SignedBeaconBlock) (*ethpb.SignedBeaconBlockHeader, error) {
13-
bodyRoot, err := stateutil.BlockBodyRoot(block.Block.Body)
12+
bodyRoot, err := block.Block.Body.HashTreeRoot()
1413
if err != nil {
1514
return nil, errors.Wrap(err, "failed to get body root of block")
1615
}

shared/blockutil/BUILD.bazel

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ go_library(
77
importpath = "github.com/prysmaticlabs/prysm/shared/blockutil",
88
visibility = ["//visibility:public"],
99
deps = [
10-
"//beacon-chain/state/stateutil:go_default_library",
1110
"@com_github_pkg_errors//:go_default_library",
1211
"@com_github_prysmaticlabs_ethereumapis//eth/v1alpha1:go_default_library",
1312
],
@@ -18,7 +17,6 @@ go_test(
1817
srcs = ["block_utils_test.go"],
1918
embed = [":go_default_library"],
2019
deps = [
21-
"//beacon-chain/state/stateutil:go_default_library",
2220
"//shared/bytesutil:go_default_library",
2321
"//shared/params:go_default_library",
2422
"//shared/testutil/assert:go_default_library",

shared/blockutil/block_utils.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@ package blockutil
33
import (
44
"github.com/pkg/errors"
55
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
6-
"github.com/prysmaticlabs/prysm/beacon-chain/state/stateutil"
76
)
87

98
// SignedBeaconBlockHeaderFromBlock function to retrieve signed block header from block.
109
func SignedBeaconBlockHeaderFromBlock(block *ethpb.SignedBeaconBlock) (*ethpb.SignedBeaconBlockHeader, error) {
1110
if block.Block == nil || block.Block.Body == nil {
1211
return nil, errors.New("nil block")
1312
}
14-
bodyRoot, err := stateutil.BlockBodyRoot(block.Block.Body)
13+
14+
bodyRoot, err := block.Block.Body.HashTreeRoot()
1515
if err != nil {
1616
return nil, errors.Wrap(err, "failed to get body root of block")
1717
}
@@ -32,7 +32,8 @@ func BeaconBlockHeaderFromBlock(block *ethpb.BeaconBlock) (*ethpb.BeaconBlockHea
3232
if block.Body == nil {
3333
return nil, errors.New("nil block body")
3434
}
35-
bodyRoot, err := stateutil.BlockBodyRoot(block.Body)
35+
36+
bodyRoot, err := block.Body.HashTreeRoot()
3637
if err != nil {
3738
return nil, errors.Wrap(err, "failed to get body root of block")
3839
}

shared/blockutil/block_utils_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"testing"
55

66
eth "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
7-
"github.com/prysmaticlabs/prysm/beacon-chain/state/stateutil"
87
"github.com/prysmaticlabs/prysm/shared/bytesutil"
98
"github.com/prysmaticlabs/prysm/shared/params"
109
"github.com/prysmaticlabs/prysm/shared/testutil/assert"
@@ -33,7 +32,7 @@ func TestBeaconBlockHeaderFromBlock(t *testing.T) {
3332
VoluntaryExits: []*eth.SignedVoluntaryExit{},
3433
},
3534
}
36-
bodyRoot, err := stateutil.BlockBodyRoot(blk.Body)
35+
bodyRoot, err := blk.Body.HashTreeRoot()
3736
require.NoError(t, err)
3837
want := &eth.BeaconBlockHeader{
3938
Slot: blk.Slot,
@@ -84,7 +83,7 @@ func TestSignedBeaconBlockHeaderFromBlock(t *testing.T) {
8483
},
8584
Signature: bytesutil.PadTo([]byte("signature"), params.BeaconConfig().BLSSignatureLength),
8685
}
87-
bodyRoot, err := stateutil.BlockBodyRoot(blk.Block.Body)
86+
bodyRoot, err := blk.Block.Body.HashTreeRoot()
8887
require.NoError(t, err)
8988
want := &eth.SignedBeaconBlockHeader{Header: &eth.BeaconBlockHeader{
9089
Slot: blk.Block.Slot,

validator/client/propose_protect_test.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,17 +66,14 @@ func TestPreBlockSignValidation(t *testing.T) {
6666
pubKey := [48]byte{}
6767
copy(pubKey[:], validatorKey.PublicKey().Marshal())
6868

69-
block := &ethpb.BeaconBlock{
70-
Slot: 10,
71-
ProposerIndex: 0,
72-
Body: &ethpb.BeaconBlockBody{},
73-
}
69+
block := testutil.NewBeaconBlock()
70+
block.Block.Slot = 10
7471
mockProtector := &mockSlasher.MockProtector{AllowBlock: false}
7572
validator.protector = mockProtector
76-
err := validator.preBlockSignValidations(context.Background(), pubKey, block)
73+
err := validator.preBlockSignValidations(context.Background(), pubKey, block.Block)
7774
require.ErrorContains(t, failedPreBlockSignExternalErr, err)
7875
mockProtector.AllowBlock = true
79-
err = validator.preBlockSignValidations(context.Background(), pubKey, block)
76+
err = validator.preBlockSignValidations(context.Background(), pubKey, block.Block)
8077
require.NoError(t, err, "Expected allowed attestation not to throw error")
8178
}
8279

0 commit comments

Comments
 (0)