2020-02-13 16:19:46 +00:00
|
|
|
package kv
|
2019-11-05 13:54:27 +00:00
|
|
|
|
|
|
|
import (
|
2020-02-13 19:51:30 +00:00
|
|
|
"context"
|
|
|
|
|
2019-11-05 13:54:27 +00:00
|
|
|
"github.com/boltdb/bolt"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/bytesutil"
|
2020-02-13 19:51:30 +00:00
|
|
|
"go.opencensus.io/trace"
|
2019-11-05 13:54:27 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// ValidatorPubKey accepts validator id and returns the corresponding pubkey.
|
|
|
|
// Returns nil if the pubkey for this validator id does not exist.
|
2020-02-13 19:51:30 +00:00
|
|
|
func (db *Store) ValidatorPubKey(ctx context.Context, validatorID uint64) ([]byte, error) {
|
|
|
|
ctx, span := trace.StartSpan(ctx, "SlasherDB.ValidatorPubKey")
|
|
|
|
defer span.End()
|
2019-11-05 13:54:27 +00:00
|
|
|
var pk []byte
|
|
|
|
err := db.view(func(tx *bolt.Tx) error {
|
|
|
|
b := tx.Bucket(validatorsPublicKeysBucket)
|
|
|
|
pk = b.Get(bytesutil.Bytes4(validatorID))
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
return pk, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// SavePubKey accepts a validator id and its public key and writes it to disk.
|
2020-02-13 19:51:30 +00:00
|
|
|
func (db *Store) SavePubKey(ctx context.Context, validatorID uint64, pubKey []byte) error {
|
|
|
|
ctx, span := trace.StartSpan(ctx, "SlasherDB.SavePubKey")
|
|
|
|
defer span.End()
|
2019-11-05 13:54:27 +00:00
|
|
|
err := db.update(func(tx *bolt.Tx) error {
|
|
|
|
bucket := tx.Bucket(validatorsPublicKeysBucket)
|
|
|
|
key := bytesutil.Bytes4(validatorID)
|
|
|
|
if err := bucket.Put(key, pubKey); err != nil {
|
|
|
|
return errors.Wrap(err, "failed to add validator public key to slasher db.")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeletePubKey deletes a public key of a validator id.
|
2020-02-13 19:51:30 +00:00
|
|
|
func (db *Store) DeletePubKey(ctx context.Context, validatorID uint64) error {
|
|
|
|
ctx, span := trace.StartSpan(ctx, "SlasherDB.DeletePubKey")
|
|
|
|
defer span.End()
|
2019-11-05 13:54:27 +00:00
|
|
|
return db.update(func(tx *bolt.Tx) error {
|
|
|
|
bucket := tx.Bucket(validatorsPublicKeysBucket)
|
|
|
|
key := bytesutil.Bytes4(validatorID)
|
|
|
|
if err := bucket.Delete(key); err != nil {
|
|
|
|
return errors.Wrap(err, "failed to delete public key from validators public key bucket")
|
|
|
|
}
|
|
|
|
return bucket.Delete(key)
|
|
|
|
})
|
|
|
|
}
|