Skip to content

Commit

Permalink
Remove custom block body root and block root methods (#8069)
Browse files Browse the repository at this point in the history
* Remove custom block body root and block root methods

* Add nil checks and fix tests

* Fmt

* Typo
  • Loading branch information
terencechain committed Dec 9, 2020
1 parent 92736d0 commit 00dacbd
Show file tree
Hide file tree
Showing 10 changed files with 11 additions and 201 deletions.
2 changes: 0 additions & 2 deletions beacon-chain/state/stateutil/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ go_library(
"@com_github_dgraph_io_ristretto//:go_default_library",
"@com_github_pkg_errors//:go_default_library",
"@com_github_prysmaticlabs_ethereumapis//eth/v1alpha1:go_default_library",
"@com_github_prysmaticlabs_go_ssz//:go_default_library",
],
)

Expand All @@ -44,7 +43,6 @@ go_test(
srcs = [
"attestations_test.go",
"benchmark_test.go",
"blocks_test.go",
"state_root_test.go",
"stateutil_test.go",
"trie_helpers_test.go",
Expand Down
36 changes: 0 additions & 36 deletions beacon-chain/state/stateutil/benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,47 +3,11 @@ package stateutil_test
import (
"testing"

ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
"github.com/prysmaticlabs/prysm/beacon-chain/state/stateutil"
"github.com/prysmaticlabs/prysm/shared/hashutil"
"github.com/prysmaticlabs/prysm/shared/htrutils"
"github.com/prysmaticlabs/prysm/shared/testutil"
"github.com/prysmaticlabs/prysm/shared/testutil/require"
)

func BenchmarkBlockHTR(b *testing.B) {
genState, keys := testutil.DeterministicGenesisState(b, 200)
conf := testutil.DefaultBlockGenConfig()
blk, err := testutil.GenerateFullBlock(genState, keys, conf, 10)
require.NoError(b, err)
atts := make([]*ethpb.Attestation, 0, 128)
for i := 0; i < 128; i++ {
atts = append(atts, blk.Block.Body.Attestations[0])
}
deposits, _, err := testutil.DeterministicDepositsAndKeys(16)
require.NoError(b, err)
blk.Block.Body.Attestations = atts
blk.Block.Body.Deposits = deposits

b.Run("SSZ_HTR", func(b *testing.B) {
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_, err := stateutil.BlockRoot(blk.Block)
require.NoError(b, err)
}
})

b.Run("Custom_SSZ_HTR", func(b *testing.B) {
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_, err := stateutil.BlockRoot(blk.Block)
require.NoError(b, err)
}
})
}

func BenchmarkMerkleize_Buffered(b *testing.B) {
roots := make([][32]byte, 8192)
for i := 0; i < 8192; i++ {
Expand Down
100 changes: 0 additions & 100 deletions beacon-chain/state/stateutil/blocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (

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

// BlockRoot returns the block hash tree root of the provided block.
func BlockRoot(blk *ethpb.BeaconBlock) ([32]byte, error) {
fieldRoots := make([][32]byte, 5)
if blk != nil {
headerSlotBuf := make([]byte, 8)
binary.LittleEndian.PutUint64(headerSlotBuf, blk.Slot)
headerSlotRoot := bytesutil.ToBytes32(headerSlotBuf)
fieldRoots[0] = headerSlotRoot
proposerIdxBuf := make([]byte, 8)
binary.LittleEndian.PutUint64(proposerIdxBuf, blk.ProposerIndex)
proposerIndexRoot := bytesutil.ToBytes32(proposerIdxBuf)
fieldRoots[1] = proposerIndexRoot
parentRoot := bytesutil.ToBytes32(blk.ParentRoot)
fieldRoots[2] = parentRoot
stateRoot := bytesutil.ToBytes32(blk.StateRoot)
fieldRoots[3] = stateRoot
bodyRoot, err := BlockBodyRoot(blk.Body)
if err != nil {
return [32]byte{}, err
}
fieldRoots[4] = bodyRoot
}
return htrutils.BitwiseMerkleizeArrays(hashutil.CustomSHA256Hasher(), fieldRoots, uint64(len(fieldRoots)), uint64(len(fieldRoots)))
}

// BlockBodyRoot returns the hash tree root of the block body.
func BlockBodyRoot(body *ethpb.BeaconBlockBody) ([32]byte, error) {
if body == nil {
// Treat nil body to be the same as empty. This is mostly for test setup purposes and would
// be very unlikely to happen in production workflow.
emptyRoot := make([]byte, 32)
emptyRandao := make([]byte, 96)
body = &ethpb.BeaconBlockBody{
RandaoReveal: emptyRandao,
Eth1Data: &ethpb.Eth1Data{
DepositRoot: emptyRoot,
DepositCount: 0,
BlockHash: emptyRoot,
},
Graffiti: emptyRoot,
ProposerSlashings: make([]*ethpb.ProposerSlashing, 0),
AttesterSlashings: make([]*ethpb.AttesterSlashing, 0),
Attestations: make([]*ethpb.Attestation, 0),
Deposits: make([]*ethpb.Deposit, 0),
VoluntaryExits: make([]*ethpb.SignedVoluntaryExit, 0),
}
}
hasher := hashutil.CustomSHA256Hasher()
fieldRoots := make([][32]byte, 8)
rawRandao := bytesutil.ToBytes96(body.RandaoReveal)
packedRandao, err := htrutils.Pack([][]byte{rawRandao[:]})
if err != nil {
return [32]byte{}, err
}
randaoRoot, err := htrutils.BitwiseMerkleize(hasher, packedRandao, uint64(len(packedRandao)), uint64(len(packedRandao)))
if err != nil {
return [32]byte{}, err
}
fieldRoots[0] = randaoRoot

eth1Root, err := Eth1Root(hasher, body.Eth1Data)
if err != nil {
return [32]byte{}, err
}
fieldRoots[1] = eth1Root

graffitiRoot := bytesutil.ToBytes32(body.Graffiti)
fieldRoots[2] = graffitiRoot

proposerSlashingsRoot, err := ssz.HashTreeRootWithCapacity(body.ProposerSlashings, params.BeaconConfig().MaxProposerSlashings)
if err != nil {
return [32]byte{}, err
}
fieldRoots[3] = proposerSlashingsRoot
attesterSlashingsRoot, err := ssz.HashTreeRootWithCapacity(body.AttesterSlashings, params.BeaconConfig().MaxAttesterSlashings)
if err != nil {
return [32]byte{}, err
}
fieldRoots[4] = attesterSlashingsRoot
attsRoot, err := blockAttestationRoot(body.Attestations)
if err != nil {
return [32]byte{}, err
}
fieldRoots[5] = attsRoot

depositRoot, err := ssz.HashTreeRootWithCapacity(body.Deposits, params.BeaconConfig().MaxDeposits)
if err != nil {
return [32]byte{}, err
}
fieldRoots[6] = depositRoot

exitRoot, err := ssz.HashTreeRootWithCapacity(body.VoluntaryExits, params.BeaconConfig().MaxVoluntaryExits)
if err != nil {
return [32]byte{}, err
}
fieldRoots[7] = exitRoot
return htrutils.BitwiseMerkleizeArrays(hasher, fieldRoots, uint64(len(fieldRoots)), uint64(len(fieldRoots)))
}

// Eth1Root computes the HashTreeRoot Merkleization of
// a BeaconBlockHeader struct according to the eth2
// Simple Serialize specification.
Expand Down
45 changes: 0 additions & 45 deletions beacon-chain/state/stateutil/blocks_test.go

This file was deleted.

1 change: 0 additions & 1 deletion proto/migration/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ go_library(
importpath = "github.com/prysmaticlabs/prysm/proto/migration",
visibility = ["//visibility:public"],
deps = [
"//beacon-chain/state/stateutil:go_default_library",
"@com_github_golang_protobuf//proto:go_default_library",
"@com_github_pkg_errors//:go_default_library",
"@com_github_prysmaticlabs_ethereumapis//eth/v1:go_default_library",
Expand Down
3 changes: 1 addition & 2 deletions proto/migration/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@ import (
"github.com/pkg/errors"
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1"
ethpb_alpha "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
"github.com/prysmaticlabs/prysm/beacon-chain/state/stateutil"
)

// V1Alpha1BlockToV1BlockHeader converts a v1alpha1 SignedBeaconBlock proto to a v1 SignedBeaconBlockHeader proto.
func V1Alpha1BlockToV1BlockHeader(block *ethpb_alpha.SignedBeaconBlock) (*ethpb.SignedBeaconBlockHeader, error) {
bodyRoot, err := stateutil.BlockBodyRoot(block.Block.Body)
bodyRoot, err := block.Block.Body.HashTreeRoot()
if err != nil {
return nil, errors.Wrap(err, "failed to get body root of block")
}
Expand Down
2 changes: 0 additions & 2 deletions shared/blockutil/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ go_library(
importpath = "github.com/prysmaticlabs/prysm/shared/blockutil",
visibility = ["//visibility:public"],
deps = [
"//beacon-chain/state/stateutil:go_default_library",
"@com_github_pkg_errors//:go_default_library",
"@com_github_prysmaticlabs_ethereumapis//eth/v1alpha1:go_default_library",
],
Expand All @@ -18,7 +17,6 @@ go_test(
srcs = ["block_utils_test.go"],
embed = [":go_default_library"],
deps = [
"//beacon-chain/state/stateutil:go_default_library",
"//shared/bytesutil:go_default_library",
"//shared/params:go_default_library",
"//shared/testutil/assert:go_default_library",
Expand Down
7 changes: 4 additions & 3 deletions shared/blockutil/block_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ package blockutil
import (
"github.com/pkg/errors"
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
"github.com/prysmaticlabs/prysm/beacon-chain/state/stateutil"
)

// SignedBeaconBlockHeaderFromBlock function to retrieve signed block header from block.
func SignedBeaconBlockHeaderFromBlock(block *ethpb.SignedBeaconBlock) (*ethpb.SignedBeaconBlockHeader, error) {
if block.Block == nil || block.Block.Body == nil {
return nil, errors.New("nil block")
}
bodyRoot, err := stateutil.BlockBodyRoot(block.Block.Body)

bodyRoot, err := block.Block.Body.HashTreeRoot()
if err != nil {
return nil, errors.Wrap(err, "failed to get body root of block")
}
Expand All @@ -32,7 +32,8 @@ func BeaconBlockHeaderFromBlock(block *ethpb.BeaconBlock) (*ethpb.BeaconBlockHea
if block.Body == nil {
return nil, errors.New("nil block body")
}
bodyRoot, err := stateutil.BlockBodyRoot(block.Body)

bodyRoot, err := block.Body.HashTreeRoot()
if err != nil {
return nil, errors.Wrap(err, "failed to get body root of block")
}
Expand Down
5 changes: 2 additions & 3 deletions shared/blockutil/block_utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"testing"

eth "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
"github.com/prysmaticlabs/prysm/beacon-chain/state/stateutil"
"github.com/prysmaticlabs/prysm/shared/bytesutil"
"github.com/prysmaticlabs/prysm/shared/params"
"github.com/prysmaticlabs/prysm/shared/testutil/assert"
Expand Down Expand Up @@ -33,7 +32,7 @@ func TestBeaconBlockHeaderFromBlock(t *testing.T) {
VoluntaryExits: []*eth.SignedVoluntaryExit{},
},
}
bodyRoot, err := stateutil.BlockBodyRoot(blk.Body)
bodyRoot, err := blk.Body.HashTreeRoot()
require.NoError(t, err)
want := &eth.BeaconBlockHeader{
Slot: blk.Slot,
Expand Down Expand Up @@ -84,7 +83,7 @@ func TestSignedBeaconBlockHeaderFromBlock(t *testing.T) {
},
Signature: bytesutil.PadTo([]byte("signature"), params.BeaconConfig().BLSSignatureLength),
}
bodyRoot, err := stateutil.BlockBodyRoot(blk.Block.Body)
bodyRoot, err := blk.Block.Body.HashTreeRoot()
require.NoError(t, err)
want := &eth.SignedBeaconBlockHeader{Header: &eth.BeaconBlockHeader{
Slot: blk.Block.Slot,
Expand Down
11 changes: 4 additions & 7 deletions validator/client/propose_protect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,14 @@ func TestPreBlockSignValidation(t *testing.T) {
pubKey := [48]byte{}
copy(pubKey[:], validatorKey.PublicKey().Marshal())

block := &ethpb.BeaconBlock{
Slot: 10,
ProposerIndex: 0,
Body: &ethpb.BeaconBlockBody{},
}
block := testutil.NewBeaconBlock()
block.Block.Slot = 10
mockProtector := &mockSlasher.MockProtector{AllowBlock: false}
validator.protector = mockProtector
err := validator.preBlockSignValidations(context.Background(), pubKey, block)
err := validator.preBlockSignValidations(context.Background(), pubKey, block.Block)
require.ErrorContains(t, failedPreBlockSignExternalErr, err)
mockProtector.AllowBlock = true
err = validator.preBlockSignValidations(context.Background(), pubKey, block)
err = validator.preBlockSignValidations(context.Background(), pubKey, block.Block)
require.NoError(t, err, "Expected allowed attestation not to throw error")
}

Expand Down

0 comments on commit 00dacbd

Please sign in to comment.