Skip to content

Commit

Permalink
Fix Bad Merge in V4 (#12183)
Browse files Browse the repository at this point in the history
  • Loading branch information
nisdas authored Mar 24, 2023
1 parent 76c729f commit 191e603
Show file tree
Hide file tree
Showing 11 changed files with 19 additions and 36 deletions.
1 change: 1 addition & 0 deletions beacon-chain/node/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ go_library(
"@com_github_ethereum_go_ethereum//common:go_default_library",
"@com_github_pkg_errors//:go_default_library",
"@com_github_prometheus_client_golang//prometheus:go_default_library",
"@com_github_prysmaticlabs_fastssz//:go_default_library",
"@com_github_sirupsen_logrus//:go_default_library",
"@com_github_urfave_cli_v2//:go_default_library",
],
Expand Down
5 changes: 5 additions & 0 deletions beacon-chain/node/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"

"github.com/ethereum/go-ethereum/common"
fastssz "github.com/prysmaticlabs/fastssz"
"github.com/prysmaticlabs/prysm/v4/cmd"
"github.com/prysmaticlabs/prysm/v4/cmd/beacon-chain/flags"
"github.com/prysmaticlabs/prysm/v4/config/params"
Expand Down Expand Up @@ -189,3 +190,7 @@ func configureExecutionSetting(cliCtx *cli.Context) error {
" Default fee recipient will be used as a fall back", checksumAddress.Hex())
return params.SetActive(c)
}

func configureFastSSZHashingAlgorithm() {
fastssz.EnableVectorizedHTR = true
}
1 change: 1 addition & 0 deletions beacon-chain/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ func New(cliCtx *cli.Context, opts ...Option) (*BeaconNode, error) {
if err := configureExecutionSetting(cliCtx); err != nil {
return nil, err
}
configureFastSSZHashingAlgorithm()

// Initializes any forks here.
params.BeaconConfig().InitializeForkSchedule()
Expand Down
1 change: 0 additions & 1 deletion beacon-chain/state/fieldtrie/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ go_library(
"//beacon-chain/state/state-native/custom-types:go_default_library",
"//beacon-chain/state/state-native/types:go_default_library",
"//beacon-chain/state/stateutil:go_default_library",
"//crypto/hash:go_default_library",
"//encoding/bytesutil:go_default_library",
"//math:go_default_library",
"//proto/prysm/v1alpha1:go_default_library",
Expand Down
4 changes: 1 addition & 3 deletions beacon-chain/state/fieldtrie/field_trie_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
customtypes "github.com/prysmaticlabs/prysm/v4/beacon-chain/state/state-native/custom-types"
"github.com/prysmaticlabs/prysm/v4/beacon-chain/state/state-native/types"
"github.com/prysmaticlabs/prysm/v4/beacon-chain/state/stateutil"
"github.com/prysmaticlabs/prysm/v4/crypto/hash"
"github.com/prysmaticlabs/prysm/v4/encoding/bytesutil"
pmath "github.com/prysmaticlabs/prysm/v4/math"
ethpb "github.com/prysmaticlabs/prysm/v4/proto/prysm/v1alpha1"
Expand Down Expand Up @@ -250,9 +249,8 @@ func handleEth1DataSlice(val []*ethpb.Eth1Data, indices []uint64, convertAll boo
length = len(val)
}
roots := make([][32]byte, 0, length)
hasher := hash.CustomSHA256Hasher()
rootCreator := func(input *ethpb.Eth1Data) error {
newRoot, err := stateutil.Eth1DataRootWithHasher(hasher, input)
newRoot, err := stateutil.Eth1DataRootWithHasher(input)
if err != nil {
return err
}
Expand Down
4 changes: 1 addition & 3 deletions beacon-chain/state/state-native/hasher.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/prysmaticlabs/prysm/v4/beacon-chain/state/stateutil"
fieldparams "github.com/prysmaticlabs/prysm/v4/config/fieldparams"
"github.com/prysmaticlabs/prysm/v4/config/params"
"github.com/prysmaticlabs/prysm/v4/crypto/hash"
"github.com/prysmaticlabs/prysm/v4/encoding/bytesutil"
"github.com/prysmaticlabs/prysm/v4/encoding/ssz"
"github.com/prysmaticlabs/prysm/v4/runtime/version"
Expand All @@ -24,7 +23,6 @@ func ComputeFieldRootsWithHasher(ctx context.Context, state *BeaconState) ([][]b
if state == nil {
return nil, errors.New("nil state")
}
hasher := hash.CustomSHA256Hasher()
var fieldRoots [][]byte
switch state.version {
case version.Phase0:
Expand Down Expand Up @@ -98,7 +96,7 @@ func ComputeFieldRootsWithHasher(ctx context.Context, state *BeaconState) ([][]b
fieldRoots[types.HistoricalRoots.RealPosition()] = historicalRootsRt[:]

// Eth1Data data structure root.
eth1HashTreeRoot, err := stateutil.Eth1Root(hasher, state.eth1Data)
eth1HashTreeRoot, err := stateutil.Eth1Root(state.eth1Data)
if err != nil {
return nil, errors.Wrap(err, "could not compute eth1data merkleization")
}
Expand Down
4 changes: 1 addition & 3 deletions beacon-chain/state/state-native/state_trie.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
fieldparams "github.com/prysmaticlabs/prysm/v4/config/fieldparams"
"github.com/prysmaticlabs/prysm/v4/config/params"
"github.com/prysmaticlabs/prysm/v4/container/slice"
"github.com/prysmaticlabs/prysm/v4/crypto/hash"
"github.com/prysmaticlabs/prysm/v4/encoding/bytesutil"
"github.com/prysmaticlabs/prysm/v4/encoding/ssz"
ethpb "github.com/prysmaticlabs/prysm/v4/proto/prysm/v1alpha1"
Expand Down Expand Up @@ -694,7 +693,6 @@ func (b *BeaconState) rootSelector(ctx context.Context, field types.FieldIndex)
defer span.End()
span.AddAttributes(trace.StringAttribute("field", field.String(b.version)))

hasher := hash.CustomSHA256Hasher()
switch field {
case types.GenesisTime:
return ssz.Uint64Root(b.genesisTime), nil
Expand Down Expand Up @@ -735,7 +733,7 @@ func (b *BeaconState) rootSelector(ctx context.Context, field types.FieldIndex)
}
return ssz.ByteArrayRootWithLimit(hRoots, fieldparams.HistoricalRootsLength)
case types.Eth1Data:
return stateutil.Eth1Root(hasher, b.eth1Data)
return stateutil.Eth1Root(b.eth1Data)
case types.Eth1DataVotes:
if b.rebuildTrie[field] {
err := b.resetFieldTrie(
Expand Down
12 changes: 3 additions & 9 deletions beacon-chain/state/stateutil/eth1_root.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@ import (

"github.com/pkg/errors"
params "github.com/prysmaticlabs/prysm/v4/config/params"
"github.com/prysmaticlabs/prysm/v4/crypto/hash"
"github.com/prysmaticlabs/prysm/v4/encoding/bytesutil"
"github.com/prysmaticlabs/prysm/v4/encoding/ssz"
ethpb "github.com/prysmaticlabs/prysm/v4/proto/prysm/v1alpha1"
)

// Eth1DataRootWithHasher returns the hash tree root of input `eth1Data`.
func Eth1DataRootWithHasher(hasher ssz.HashFn, eth1Data *ethpb.Eth1Data) ([32]byte, error) {
func Eth1DataRootWithHasher(eth1Data *ethpb.Eth1Data) ([32]byte, error) {
if eth1Data == nil {
return [32]byte{}, errors.New("nil eth1 data")
}
Expand Down Expand Up @@ -42,21 +41,16 @@ func Eth1DataRootWithHasher(hasher ssz.HashFn, eth1Data *ethpb.Eth1Data) ([32]by

// Eth1DatasRoot returns the hash tree root of input `eth1Datas`.
func Eth1DatasRoot(eth1Datas []*ethpb.Eth1Data) ([32]byte, error) {
hasher := hash.CustomSHA256Hasher()
eth1VotesRoots := make([][32]byte, 0, len(eth1Datas))
for i := 0; i < len(eth1Datas); i++ {
eth1, err := Eth1DataRootWithHasher(hasher, eth1Datas[i])
eth1, err := Eth1DataRootWithHasher(eth1Datas[i])
if err != nil {
return [32]byte{}, errors.Wrap(err, "could not compute eth1data merkleization")
}
eth1VotesRoots = append(eth1VotesRoots, eth1)
}

eth1VotesRootsRoot, err := ssz.BitwiseMerkleize(
eth1VotesRoots,
uint64(len(eth1VotesRoots)),
params.BeaconConfig().Eth1DataVotesLength(),
)
eth1VotesRootsRoot, err := ssz.BitwiseMerkleize(eth1VotesRoots, uint64(len(eth1VotesRoots)), params.BeaconConfig().Eth1DataVotesLength())
if err != nil {
return [32]byte{}, errors.Wrap(err, "could not compute eth1data votes merkleization")
}
Expand Down
12 changes: 3 additions & 9 deletions beacon-chain/state/stateutil/field_root_attestation.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (

"github.com/pkg/errors"
params "github.com/prysmaticlabs/prysm/v4/config/params"
"github.com/prysmaticlabs/prysm/v4/crypto/hash"
"github.com/prysmaticlabs/prysm/v4/encoding/ssz"
ethpb "github.com/prysmaticlabs/prysm/v4/proto/prysm/v1alpha1"
)
Expand All @@ -24,21 +23,16 @@ func EpochAttestationsRoot(atts []*ethpb.PendingAttestation) ([32]byte, error) {
return [32]byte{}, fmt.Errorf("epoch attestation exceeds max length %d", max)
}

hasher := hash.CustomSHA256Hasher()
roots := make([][32]byte, len(atts))
for i := 0; i < len(atts); i++ {
pendingRoot, err := pendingAttestationRoot(hasher, atts[i])
pendingRoot, err := pendingAttestationRoot(atts[i])
if err != nil {
return [32]byte{}, errors.Wrap(err, "could not attestation merkleization")
}
roots[i] = pendingRoot
}

attsRootsRoot, err := ssz.BitwiseMerkleize(
roots,
uint64(len(roots)),
params.BeaconConfig().CurrentEpochAttestationsLength(),
)
attsRootsRoot, err := ssz.BitwiseMerkleize(roots, uint64(len(roots)), params.BeaconConfig().CurrentEpochAttestationsLength())
if err != nil {
return [32]byte{}, errors.Wrap(err, "could not compute epoch attestations merkleization")
}
Expand All @@ -53,7 +47,7 @@ func EpochAttestationsRoot(atts []*ethpb.PendingAttestation) ([32]byte, error) {
return res, nil
}

func pendingAttestationRoot(hasher ssz.HashFn, att *ethpb.PendingAttestation) ([32]byte, error) {
func pendingAttestationRoot(att *ethpb.PendingAttestation) ([32]byte, error) {
if att == nil {
return [32]byte{}, errors.New("nil pending attestation")
}
Expand Down
5 changes: 2 additions & 3 deletions beacon-chain/state/stateutil/field_root_eth1.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,17 @@ package stateutil

import (
"github.com/pkg/errors"
"github.com/prysmaticlabs/prysm/v4/encoding/ssz"
ethpb "github.com/prysmaticlabs/prysm/v4/proto/prysm/v1alpha1"
)

// Eth1Root computes the HashTreeRoot Merkleization of
// a BeaconBlockHeader struct according to the eth2
// Simple Serialize specification.
func Eth1Root(hasher ssz.HashFn, eth1Data *ethpb.Eth1Data) ([32]byte, error) {
func Eth1Root(eth1Data *ethpb.Eth1Data) ([32]byte, error) {
if eth1Data == nil {
return [32]byte{}, errors.New("nil eth1 data")
}
return Eth1DataRootWithHasher(hasher, eth1Data)
return Eth1DataRootWithHasher(eth1Data)
}

// Eth1DataVotesRoot computes the HashTreeRoot Merkleization of
Expand Down
6 changes: 1 addition & 5 deletions beacon-chain/state/stateutil/historical_summaries_root.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,7 @@ func HistoricalSummariesRoot(summaries []*ethpb.HistoricalSummary) ([32]byte, er
roots[i] = r
}

summariesRoot, err := ssz.BitwiseMerkleize(
roots,
uint64(len(roots)),
fieldparams.HistoricalRootsLength,
)
summariesRoot, err := ssz.BitwiseMerkleize(roots, uint64(len(roots)), fieldparams.HistoricalRootsLength)
if err != nil {
return [32]byte{}, errors.Wrap(err, "could not compute historical summaries merkleization")
}
Expand Down

0 comments on commit 191e603

Please sign in to comment.