mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2025-01-05 09:14:28 +00:00
78a865eb0b
* Replaced. Debugging missing strict dependencies... * Merge branch 'master' into bbolt-import * Update import path * Merge branch 'bbolt-import' of github.com:prysmaticlabs/prysm into bbolt-import * use forked prombbolt * Merge branch 'bbolt-import' of github.com:prysmaticlabs/prysm into bbolt-import * fix * remove old boltdb reference * Use correct bolt for pk manager * Merge branch 'bbolt-import' of github.com:prysmaticlabs/prysm into bbolt-import * fix for docker build * gaz, oops
72 lines
2.2 KiB
Go
72 lines
2.2 KiB
Go
package db
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/gogo/protobuf/proto"
|
|
"github.com/pkg/errors"
|
|
slashpb "github.com/prysmaticlabs/prysm/proto/slashing"
|
|
bolt "go.etcd.io/bbolt"
|
|
"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
|
|
})
|
|
}
|