Use stateutil AttestationDataRoot instead of go-ssz (#6136)

* Change usages of ssz.HashTreeRoot(att.Data) to stateutil
* Gaz
* Tests
* Imports
* Merge branch 'master' into use-stateutil-attdata
* Fix tests
* Merge branch 'use-stateutil-attdata' of github.com:prysmaticlabs/prysm into use-stateutil-attdata
* Merge refs/heads/master into use-stateutil-attdata
* Merge refs/heads/master into use-stateutil-attdata
This commit is contained in:
Ivan Martinez
2020-06-04 21:32:08 -04:00
committed by GitHub
parent edbeb9022b
commit a4f4ab2b1a
10 changed files with 58 additions and 24 deletions

View File

@@ -39,6 +39,8 @@ func ComputeSigningRoot(object interface{}, domain []byte) ([32]byte, error) {
switch object.(type) {
case *ethpb.BeaconBlock:
return stateutil.BlockRoot(object.(*ethpb.BeaconBlock))
case *ethpb.AttestationData:
return stateutil.AttestationDataRoot(object.(*ethpb.AttestationData))
default:
// utilise generic ssz library
return ssz.HashTreeRoot(object)

View File

@@ -6,8 +6,8 @@ import (
"github.com/pkg/errors"
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
"github.com/prysmaticlabs/go-ssz"
"github.com/prysmaticlabs/prysm/beacon-chain/db/filters"
"github.com/prysmaticlabs/prysm/beacon-chain/state/stateutil"
dbpb "github.com/prysmaticlabs/prysm/proto/beacon/db"
"github.com/prysmaticlabs/prysm/shared/bytesutil"
"github.com/prysmaticlabs/prysm/shared/sliceutil"
@@ -153,7 +153,7 @@ func (k *Store) SaveAttestation(ctx context.Context, att *ethpb.Attestation) err
return err
}
attDataRoot, err := ssz.HashTreeRoot(att.Data)
attDataRoot, err := stateutil.AttestationDataRoot(att.Data)
if err != nil {
return err
}
@@ -196,7 +196,7 @@ func (k *Store) SaveAttestations(ctx context.Context, atts []*ethpb.Attestation)
err := k.db.Update(func(tx *bolt.Tx) error {
for _, att := range atts {
attDataRoot, err := ssz.HashTreeRoot(att.Data)
attDataRoot, err := stateutil.AttestationDataRoot(att.Data)
if err != nil {
return err
}

View File

@@ -8,21 +8,33 @@ import (
"sync"
"testing"
"github.com/prysmaticlabs/prysm/shared/bytesutil"
"github.com/gogo/protobuf/proto"
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
"github.com/prysmaticlabs/go-bitfield"
"github.com/prysmaticlabs/go-ssz"
"github.com/prysmaticlabs/prysm/beacon-chain/db/filters"
"github.com/prysmaticlabs/prysm/beacon-chain/state/stateutil"
)
func TestStore_AttestationCRUD(t *testing.T) {
db := setupDB(t)
att := &ethpb.Attestation{
Data: &ethpb.AttestationData{Slot: 10},
Data: &ethpb.AttestationData{
Slot: 10,
BeaconBlockRoot: make([]byte, 32),
Source: &ethpb.Checkpoint{
Root: make([]byte, 32),
},
Target: &ethpb.Checkpoint{
Root: make([]byte, 32),
},
},
AggregationBits: bitfield.Bitlist{0b00000001, 0b1},
}
ctx := context.Background()
attDataRoot, err := ssz.HashTreeRoot(att.Data)
attDataRoot, err := stateutil.AttestationDataRoot(att.Data)
if err != nil {
t.Fatal(err)
}
@@ -58,6 +70,7 @@ func TestStore_AttestationsBatchDelete(t *testing.T) {
db := setupDB(t)
ctx := context.Background()
numAtts := 10
blockRoot := bytesutil.PadTo([]byte("head"), 32)
totalAtts := make([]*ethpb.Attestation, numAtts)
// We track the data roots for the even indexed attestations.
attDataRoots := make([][32]byte, 0)
@@ -65,13 +78,19 @@ func TestStore_AttestationsBatchDelete(t *testing.T) {
for i := 0; i < len(totalAtts); i++ {
totalAtts[i] = &ethpb.Attestation{
Data: &ethpb.AttestationData{
BeaconBlockRoot: []byte("head"),
Slot: uint64(i),
BeaconBlockRoot: blockRoot,
Source: &ethpb.Checkpoint{
Root: make([]byte, 32),
},
Target: &ethpb.Checkpoint{
Root: make([]byte, 32),
},
},
AggregationBits: bitfield.Bitlist{0b00000001, 0b1},
}
if i%2 == 0 {
r, err := ssz.HashTreeRoot(totalAtts[i].Data)
r, err := stateutil.AttestationDataRoot(totalAtts[i].Data)
if err != nil {
t.Fatal(err)
}
@@ -83,7 +102,7 @@ func TestStore_AttestationsBatchDelete(t *testing.T) {
if err := db.SaveAttestations(ctx, totalAtts); err != nil {
t.Fatal(err)
}
retrieved, err := db.Attestations(ctx, filters.NewFilter().SetHeadBlockRoot([]byte("head")))
retrieved, err := db.Attestations(ctx, filters.NewFilter().SetHeadBlockRoot(blockRoot))
if err != nil {
t.Fatal(err)
}
@@ -95,7 +114,7 @@ func TestStore_AttestationsBatchDelete(t *testing.T) {
t.Fatal(err)
}
// When we retrieve the data, only the odd indexed attestations should remain.
retrieved, err = db.Attestations(ctx, filters.NewFilter().SetHeadBlockRoot([]byte("head")))
retrieved, err = db.Attestations(ctx, filters.NewFilter().SetHeadBlockRoot(blockRoot))
if err != nil {
t.Fatal(err)
}
@@ -114,14 +133,18 @@ func TestStore_BoltDontPanic(t *testing.T) {
for i := 0; i <= 100; i++ {
att := &ethpb.Attestation{
Data: &ethpb.AttestationData{
Slot: uint64(i),
Source: &ethpb.Checkpoint{},
Target: &ethpb.Checkpoint{},
Slot: uint64(i),
BeaconBlockRoot: make([]byte, 32),
Source: &ethpb.Checkpoint{
Root: make([]byte, 32)},
Target: &ethpb.Checkpoint{
Root: make([]byte, 32),
},
},
AggregationBits: bitfield.Bitlist{0b11},
}
ctx := context.Background()
attDataRoot, err := ssz.HashTreeRoot(att.Data)
attDataRoot, err := stateutil.AttestationDataRoot(att.Data)
if err != nil {
t.Fatal(err)
}
@@ -142,11 +165,19 @@ func TestStore_BoltDontPanic(t *testing.T) {
wg.Add(1)
go func() {
att := &ethpb.Attestation{
Data: &ethpb.AttestationData{Slot: uint64(startEpoch)},
Data: &ethpb.AttestationData{
Slot: uint64(startEpoch),
BeaconBlockRoot: make([]byte, 32),
Source: &ethpb.Checkpoint{
Root: make([]byte, 32)},
Target: &ethpb.Checkpoint{
Root: make([]byte, 32),
},
},
AggregationBits: bitfield.Bitlist{0b11},
}
ctx := context.Background()
attDataRoot, err := ssz.HashTreeRoot(att.Data)
attDataRoot, err := stateutil.AttestationDataRoot(att.Data)
if err != nil {
t.Fatal(err)
}

View File

@@ -17,6 +17,7 @@ go_library(
"//beacon-chain/core/helpers:go_default_library",
"//beacon-chain/operations/attestations/kv:go_default_library",
"//beacon-chain/state:go_default_library",
"//beacon-chain/state/stateutil:go_default_library",
"//shared/hashutil:go_default_library",
"//shared/params:go_default_library",
"//shared/roughtime:go_default_library",
@@ -26,7 +27,6 @@ go_library(
"@com_github_prometheus_client_golang//prometheus/promauto:go_default_library",
"@com_github_prysmaticlabs_ethereumapis//eth/v1alpha1:go_default_library",
"@com_github_prysmaticlabs_go_bitfield//:go_default_library",
"@com_github_prysmaticlabs_go_ssz//:go_default_library",
"@com_github_sirupsen_logrus//:go_default_library",
"@io_opencensus_go//trace:go_default_library",
],

View File

@@ -15,6 +15,7 @@ go_library(
deps = [
"//beacon-chain/core/helpers:go_default_library",
"//beacon-chain/state:go_default_library",
"//beacon-chain/state/stateutil:go_default_library",
"//shared/hashutil:go_default_library",
"@com_github_pkg_errors//:go_default_library",
"@com_github_prysmaticlabs_ethereumapis//eth/v1alpha1:go_default_library",

View File

@@ -6,6 +6,7 @@ import (
"github.com/prysmaticlabs/go-ssz"
"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers"
stateTrie "github.com/prysmaticlabs/prysm/beacon-chain/state"
"github.com/prysmaticlabs/prysm/beacon-chain/state/stateutil"
)
// AggregateUnaggregatedAttestations aggregates the unaggregated attestations and save the
@@ -16,7 +17,7 @@ func (p *AttCaches) AggregateUnaggregatedAttestations() error {
attsByDataRoot := make(map[[32]byte][]*ethpb.Attestation)
unaggregatedAtts := p.UnaggregatedAttestations()
for _, att := range unaggregatedAtts {
attDataRoot, err := ssz.HashTreeRoot(att.Data)
attDataRoot, err := stateutil.AttestationDataRoot(att.Data)
if err != nil {
return err
}

View File

@@ -8,9 +8,9 @@ import (
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
"github.com/prysmaticlabs/go-bitfield"
"github.com/prysmaticlabs/go-ssz"
"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers"
stateTrie "github.com/prysmaticlabs/prysm/beacon-chain/state"
"github.com/prysmaticlabs/prysm/beacon-chain/state/stateutil"
"github.com/prysmaticlabs/prysm/shared/hashutil"
"github.com/prysmaticlabs/prysm/shared/slotutil"
"go.opencensus.io/trace"
@@ -62,7 +62,7 @@ func (s *Service) batchForkChoiceAtts(ctx context.Context) error {
continue
}
attDataRoot, err := ssz.HashTreeRoot(att.Data)
attDataRoot, err := stateutil.AttestationDataRoot(att.Data)
if err != nil {
return err
}

View File

@@ -54,7 +54,6 @@ go_library(
"@com_github_prometheus_client_golang//prometheus:go_default_library",
"@com_github_prometheus_client_golang//prometheus/promauto:go_default_library",
"@com_github_prysmaticlabs_ethereumapis//eth/v1alpha1:go_default_library",
"@com_github_prysmaticlabs_go_ssz//:go_default_library",
"@com_github_sirupsen_logrus//:go_default_library",
"@org_golang_google_grpc//codes:go_default_library",
"@org_golang_google_grpc//status:go_default_library",

View File

@@ -8,12 +8,12 @@ import (
ptypes "github.com/gogo/protobuf/types"
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
"github.com/prysmaticlabs/go-ssz"
"github.com/prysmaticlabs/prysm/beacon-chain/core/feed"
"github.com/prysmaticlabs/prysm/beacon-chain/core/feed/operation"
"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers"
"github.com/prysmaticlabs/prysm/beacon-chain/db/filters"
"github.com/prysmaticlabs/prysm/beacon-chain/flags"
"github.com/prysmaticlabs/prysm/beacon-chain/state/stateutil"
"github.com/prysmaticlabs/prysm/shared/attestationutil"
"github.com/prysmaticlabs/prysm/shared/bytesutil"
"github.com/prysmaticlabs/prysm/shared/featureconfig"
@@ -355,7 +355,7 @@ func (bs *Server) collectReceivedAttestations(ctx context.Context) {
bs.CollectedAttestationsBuffer <- atts
}
case att := <-bs.ReceivedAttestationsBuffer:
attDataRoot, err := ssz.HashTreeRoot(att.Data)
attDataRoot, err := stateutil.AttestationDataRoot(att.Data)
if err != nil {
log.Errorf("Could not hash tree root data: %v", err)
continue

View File

@@ -5,13 +5,13 @@ import (
ptypes "github.com/gogo/protobuf/types"
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
"github.com/prysmaticlabs/go-ssz"
"github.com/prysmaticlabs/prysm/beacon-chain/cache"
"github.com/prysmaticlabs/prysm/beacon-chain/core/feed"
"github.com/prysmaticlabs/prysm/beacon-chain/core/feed/operation"
"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers"
"github.com/prysmaticlabs/prysm/beacon-chain/core/state"
stateTrie "github.com/prysmaticlabs/prysm/beacon-chain/state"
"github.com/prysmaticlabs/prysm/beacon-chain/state/stateutil"
"github.com/prysmaticlabs/prysm/shared/bls"
"github.com/prysmaticlabs/prysm/shared/bytesutil"
"github.com/prysmaticlabs/prysm/shared/featureconfig"
@@ -152,7 +152,7 @@ func (vs *Server) ProposeAttestation(ctx context.Context, att *ethpb.Attestation
return nil, status.Error(codes.InvalidArgument, "Incorrect attestation signature")
}
root, err := ssz.HashTreeRoot(att.Data)
root, err := stateutil.AttestationDataRoot(att.Data)
if err != nil {
return nil, status.Errorf(codes.Internal, "Could not tree hash attestation: %v", err)
}