Skip to content

Commit

Permalink
Change attester protection to return default if DB is empty (#5323)
Browse files Browse the repository at this point in the history
* Change how default values are set

* Remove unused imports

* Remove wasteful db call

* Fix db tests

* Fix db test
  • Loading branch information
0xKiwi committed Apr 6, 2020
1 parent 91b19b9 commit 894508f
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 59 deletions.
11 changes: 2 additions & 9 deletions validator/client/validator_attest.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,9 @@ func (v *validator) SubmitAttestation(ctx context.Context, slot uint64, pubKey [
return
}

var history *slashpb.AttestationHistory
if featureconfig.Get().ProtectAttester {
history, err := v.db.AttestationHistory(ctx, pubKey[:])
history, err = v.db.AttestationHistory(ctx, pubKey[:])
if err != nil {
log.Errorf("Could not get attestation history from DB: %v", err)
if v.emitAccountMetrics {
Expand Down Expand Up @@ -148,14 +149,6 @@ func (v *validator) SubmitAttestation(ctx context.Context, slot uint64, pubKey [
}

if featureconfig.Get().ProtectAttester {
history, err := v.db.AttestationHistory(ctx, pubKey[:])
if err != nil {
log.Errorf("Could not get attestation history from DB: %v", err)
if v.emitAccountMetrics {
validatorAttestFailVec.WithLabelValues(fmtKey).Inc()
}
return
}
history = markAttestationForTargetEpoch(history, data.Source.Epoch, data.Target.Epoch)
if err := v.db.SaveAttestationHistory(ctx, pubKey[:], history); err != nil {
log.Errorf("Could not save attestation history to DB: %v", err)
Expand Down
12 changes: 3 additions & 9 deletions validator/client/validator_propose.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
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"
"github.com/prysmaticlabs/prysm/shared/bls"
Expand Down Expand Up @@ -85,8 +86,9 @@ func (v *validator) ProposeBlock(ctx context.Context, slot uint64, pubKey [48]by
return
}

var slotBits bitfield.Bitlist
if featureconfig.Get().ProtectProposer {
slotBits, err := v.db.ProposalHistoryForEpoch(ctx, pubKey[:], epoch)
slotBits, err = v.db.ProposalHistoryForEpoch(ctx, pubKey[:], epoch)
if err != nil {
log.WithError(err).Error("Failed to get proposal history")
if v.emitAccountMetrics {
Expand Down Expand Up @@ -130,14 +132,6 @@ func (v *validator) ProposeBlock(ctx context.Context, slot uint64, pubKey [48]by
}

if featureconfig.Get().ProtectProposer {
slotBits, err := v.db.ProposalHistoryForEpoch(ctx, pubKey[:], epoch)
if err != nil {
log.WithError(err).Error("Failed to get proposal history")
if v.emitAccountMetrics {
validatorProposeFailVec.WithLabelValues(fmtKey).Inc()
}
return
}
slotBits.SetBitAt(slot%params.BeaconConfig().SlotsPerEpoch, true)
if err := v.db.SaveProposalHistoryForEpoch(ctx, pubKey[:], epoch, slotBits); err != nil {
log.WithError(err).Error("Failed to save updated proposal history")
Expand Down
6 changes: 6 additions & 0 deletions validator/db/attestation_history.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/gogo/protobuf/proto"
"github.com/pkg/errors"
slashpb "github.com/prysmaticlabs/prysm/proto/slashing"
"github.com/prysmaticlabs/prysm/shared/params"
bolt "go.etcd.io/bbolt"
"go.opencensus.io/trace"
)
Expand All @@ -31,6 +32,11 @@ func (db *Store) AttestationHistory(ctx context.Context, publicKey []byte) (*sla
bucket := tx.Bucket(historicAttestationsBucket)
enc := bucket.Get(publicKey)
if enc == nil {
newMap := make(map[uint64]uint64)
newMap[0] = params.BeaconConfig().FarFutureEpoch
attestationHistory = &slashpb.AttestationHistory{
TargetToSource: newMap,
}
return nil
}
attestationHistory, err = unmarshalAttestationHistory(enc)
Expand Down
27 changes: 8 additions & 19 deletions validator/db/attestation_history_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/prysmaticlabs/prysm/shared/params"
)

func TestAttestationHistory_InitializesNewPubKeys(t *testing.T) {
func TestAttestationHistory_EmptyVal(t *testing.T) {
pubkeys := [][48]byte{{30}, {25}, {20}}
db := SetupDB(t, pubkeys)
defer TeardownDB(t, db)
Expand All @@ -31,22 +31,6 @@ func TestAttestationHistory_InitializesNewPubKeys(t *testing.T) {
}
}

func TestAttestationHistory_NilDB(t *testing.T) {
db := SetupDB(t, [][48]byte{})
defer TeardownDB(t, db)

valPubkey := []byte{1, 2, 3}

attestationHistory, err := db.AttestationHistory(context.Background(), valPubkey)
if err != nil {
t.Fatal(err)
}

if attestationHistory != nil {
t.Fatalf("Expected attestation history to be nil, received: %v", attestationHistory)
}
}

func TestSaveAttestationHistory_OK(t *testing.T) {
db := SetupDB(t, [][48]byte{})
defer TeardownDB(t, db)
Expand Down Expand Up @@ -187,7 +171,12 @@ func TestDeleteAttestationHistory_OK(t *testing.T) {
if err != nil {
t.Fatalf("Failed to get attestation history: %v", err)
}
if savedHistory != nil {
t.Fatalf("Expected attestation history to be nil, received %v", savedHistory)
cleanMap := make(map[uint64]uint64)
cleanMap[0] = params.BeaconConfig().FarFutureEpoch
clean := &slashpb.AttestationHistory{
TargetToSource: cleanMap,
}
if !reflect.DeepEqual(savedHistory, clean) {
t.Fatalf("Expected attestation history to be %v, received %v", clean, savedHistory)
}
}
22 changes: 1 addition & 21 deletions validator/db/db.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
package db

import (
"context"
"os"
"path/filepath"
"time"

"github.com/pkg/errors"
slashpb "github.com/prysmaticlabs/prysm/proto/slashing"
"github.com/prysmaticlabs/prysm/shared/params"
"github.com/prysmaticlabs/prysm/validator/db/iface"
"github.com/sirupsen/logrus"
bolt "go.etcd.io/bbolt"
Expand Down Expand Up @@ -92,27 +89,10 @@ func NewKVStore(dirPath string, pubKeys [][48]byte) (*Store, error) {
return nil, err
}

// Initialize the required pubKeys into the DB to ensure they're not empty.
// Initialize the required public keys into the DB to ensure they're not empty.
if err := kv.initializeSubBuckets(pubKeys); err != nil {
return nil, err
}
for _, pubkey := range pubKeys {
attHistory, err := kv.AttestationHistory(context.Background(), pubkey[:])
if err != nil {
return nil, err
}
if attHistory == nil {
newMap := make(map[uint64]uint64)
newMap[0] = params.BeaconConfig().FarFutureEpoch
cleanHistory := &slashpb.AttestationHistory{
TargetToSource: newMap,
}
if err := kv.SaveAttestationHistory(context.Background(), pubkey[:], cleanHistory); err != nil {
return nil, err
}
}

}

return kv, err
}
Expand Down
2 changes: 1 addition & 1 deletion validator/db/proposal_history.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func (db *Store) initializeSubBuckets(pubKeys [][48]byte) error {
bucket := tx.Bucket(historicProposalsBucket)
for _, pubKey := range pubKeys {
if _, err := bucket.CreateBucketIfNotExists(pubKey[:]); err != nil {
return errors.Wrap(err, "failed to delete the proposal history")
return errors.Wrap(err, "failed to create proposal history bucket")
}
}
return nil
Expand Down

0 comments on commit 894508f

Please sign in to comment.