prysm-pulse/validator/db/proposal_history.go
Ivan Martinez 4ab0a91e51
Validator Slashing Protection DB (#4389)
* Begin adding DB to validator client

Begin adding ValidatorProposalHistory

Implement most of proposal history

Finish tests

Fix marking a proposal for the first time

Change proposalhistory to not using bit shifting

Add pb.go

Change after proto/slashing added

Finally fix protos

Fix most tests

Fix all tests for double proposal protection

Start initialiing DB in validator client

Add db to validator struct

Add DB to ProposeBlock

Fix test errors and begin mocking

Fix test formatting and pass test for validator protection!

Fix merge issues

Fix renames

Fix tests

* Fix tests

* Fix first startup on DB

* Fix nil check tests

* Fix E2E

* Fix e2e flag

* Fix comments

* Fix for comments

* Move proposal hepers to validator/client to keep DB clean

* Add clear-db flag to validator client

* Fix formatting

* Clear out unintended changes

* Fix build issues

* Fix build issues

* Gazelle

* Fix mock test

* Remove proposal history

* Add terminal confirmation to DB clearing

* Add interface for validatorDB, add context to DB functions

* Add force-clear-db flag

* Cleanup

* Update validator/node/node.go

Co-Authored-By: Raul Jordan <raul@prysmaticlabs.com>

* Change db to clear file, not whole folder

* Fix db test

* Fix teardown test

Co-authored-by: Raul Jordan <raul@prysmaticlabs.com>
2020-01-08 13:16:17 -05:00

72 lines
2.2 KiB
Go

package db
import (
"context"
"github.com/boltdb/bolt"
"github.com/gogo/protobuf/proto"
"github.com/pkg/errors"
slashpb "github.com/prysmaticlabs/prysm/proto/slashing"
"go.opencensus.io/trace"
)
func unmarshalProposalHistory(enc []byte) (*slashpb.ProposalHistory, error) {
history := &slashpb.ProposalHistory{}
err := proto.Unmarshal(enc, history)
if err != nil {
return nil, errors.Wrap(err, "failed to unmarshal encoding")
}
return history, nil
}
// ProposalHistory accepts a validator public key and returns the corresponding proposal history.
// Returns nil if there is no proposal history for the validator.
func (db *Store) ProposalHistory(ctx context.Context, publicKey []byte) (*slashpb.ProposalHistory, error) {
ctx, span := trace.StartSpan(ctx, "Validator.ProposalHistory")
defer span.End()
var err error
var proposalHistory *slashpb.ProposalHistory
err = db.view(func(tx *bolt.Tx) error {
bucket := tx.Bucket(historicProposalsBucket)
enc := bucket.Get(publicKey)
if enc == nil {
return nil
}
proposalHistory, err = unmarshalProposalHistory(enc)
return err
})
return proposalHistory, err
}
// SaveProposalHistory returns the proposal history for the requested validator public key.
func (db *Store) SaveProposalHistory(ctx context.Context, pubKey []byte, proposalHistory *slashpb.ProposalHistory) error {
ctx, span := trace.StartSpan(ctx, "Validator.SaveProposalHistory")
defer span.End()
enc, err := proto.Marshal(proposalHistory)
if err != nil {
return errors.Wrap(err, "failed to encode proposal history")
}
err = db.update(func(tx *bolt.Tx) error {
bucket := tx.Bucket(historicProposalsBucket)
return bucket.Put(pubKey, enc)
})
return err
}
// DeleteProposalHistory deletes the proposal history for the corresponding validator public key.
func (db *Store) DeleteProposalHistory(ctx context.Context, pubkey []byte) error {
ctx, span := trace.StartSpan(ctx, "Validator.DeleteProposalHistory")
defer span.End()
return db.update(func(tx *bolt.Tx) error {
bucket := tx.Bucket(historicProposalsBucket)
if err := bucket.Delete(pubkey); err != nil {
return errors.Wrap(err, "failed to delete the proposal history")
}
return nil
})
}