prysm-pulse/slasher/db/kv/validator_id_pubkey.go

55 lines
1.8 KiB
Go
Raw Normal View History

package kv
2019-11-05 13:54:27 +00:00
import (
"context"
2019-11-05 13:54:27 +00:00
"github.com/pkg/errors"
types "github.com/prysmaticlabs/eth2-types"
2019-11-05 13:54:27 +00:00
"github.com/prysmaticlabs/prysm/shared/bytesutil"
bolt "go.etcd.io/bbolt"
"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.
func (s *Store) ValidatorPubKey(ctx context.Context, validatorIndex types.ValidatorIndex) ([]byte, error) {
ctx, span := trace.StartSpan(ctx, "SlasherDB.ValidatorPubKey")
defer span.End()
2019-11-05 13:54:27 +00:00
var pk []byte
err := s.view(func(tx *bolt.Tx) error {
2019-11-05 13:54:27 +00:00
b := tx.Bucket(validatorsPublicKeysBucket)
pk = b.Get(bytesutil.Bytes4(uint64(validatorIndex)))
2019-11-05 13:54:27 +00:00
return nil
})
return pk, err
}
// SavePubKey accepts a validator id and its public key and writes it to disk.
func (s *Store) SavePubKey(ctx context.Context, validatorIndex types.ValidatorIndex, pubKey []byte) error {
ctx, span := trace.StartSpan(ctx, "SlasherDB.SavePubKey")
defer span.End()
err := s.update(func(tx *bolt.Tx) error {
2019-11-05 13:54:27 +00:00
bucket := tx.Bucket(validatorsPublicKeysBucket)
key := bytesutil.Bytes4(uint64(validatorIndex))
2019-11-05 13:54:27 +00:00
if err := bucket.Put(key, pubKey); err != nil {
return errors.Wrap(err, "failed to add validator public key to slasher s.")
2019-11-05 13:54:27 +00:00
}
return nil
})
return err
}
// DeletePubKey deletes a public key of a validator id.
func (s *Store) DeletePubKey(ctx context.Context, validatorIndex types.ValidatorIndex) error {
ctx, span := trace.StartSpan(ctx, "SlasherDB.DeletePubKey")
defer span.End()
return s.update(func(tx *bolt.Tx) error {
2019-11-05 13:54:27 +00:00
bucket := tx.Bucket(validatorsPublicKeysBucket)
key := bytesutil.Bytes4(uint64(validatorIndex))
2019-11-05 13:54:27 +00:00
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)
})
}