prysm-pulse/validator/db/attestation_history.go
Ivan Martinez a4db560e55 Prepare validator DB for attester protection implementation (#4584)
* Add flag for attester protection
* Merge branch 'master' of https://github.com/prysmaticlabs/Prysm into protecc-attester-db
* Merge branch 'master' of https://github.com/prysmaticlabs/Prysm into protecc-attester-db
* Remove flags
* Add attestation history DB functions to validator client
* Fix comments
* Update interface to new funcs
* Merge branch 'master' of https://github.com/prysmaticlabs/Prysm into protecc-attester-db
* Fix test
* Merge branch 'master' into protecc-attester-db
2020-01-19 22:05:48 +00: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 unmarshalAttestationHistory(enc []byte) (*slashpb.AttestationHistory, error) {
history := &slashpb.AttestationHistory{}
err := proto.Unmarshal(enc, history)
if err != nil {
return nil, errors.Wrap(err, "failed to unmarshal encoding")
}
return history, nil
}
// AttestationHistory accepts a validator public key and returns the corresponding attestation history.
// Returns nil if there is no attestation history for the validator.
func (db *Store) AttestationHistory(ctx context.Context, publicKey []byte) (*slashpb.AttestationHistory, error) {
ctx, span := trace.StartSpan(ctx, "Validator.AttestationHistory")
defer span.End()
var err error
var attestationHistory *slashpb.AttestationHistory
err = db.view(func(tx *bolt.Tx) error {
bucket := tx.Bucket(historicAttestationsBucket)
enc := bucket.Get(publicKey)
if enc == nil {
return nil
}
attestationHistory, err = unmarshalAttestationHistory(enc)
return err
})
return attestationHistory, err
}
// SaveAttestationHistory returns the attestation history for the requested validator public key.
func (db *Store) SaveAttestationHistory(ctx context.Context, pubKey []byte, attestationHistory *slashpb.AttestationHistory) error {
ctx, span := trace.StartSpan(ctx, "Validator.SaveAttestationHistory")
defer span.End()
enc, err := proto.Marshal(attestationHistory)
if err != nil {
return errors.Wrap(err, "failed to encode attestation history")
}
err = db.update(func(tx *bolt.Tx) error {
bucket := tx.Bucket(historicAttestationsBucket)
return bucket.Put(pubKey, enc)
})
return err
}
// DeleteAttestationHistory deletes the attestation history for the corresponding validator public key.
func (db *Store) DeleteAttestationHistory(ctx context.Context, pubkey []byte) error {
ctx, span := trace.StartSpan(ctx, "Validator.DeleteAttestationHistory")
defer span.End()
return db.update(func(tx *bolt.Tx) error {
bucket := tx.Bucket(historicAttestationsBucket)
if err := bucket.Delete(pubkey); err != nil {
return errors.Wrap(err, "failed to delete the attestation history")
}
return nil
})
}