github.com/prysmaticlabs/prysm@v1.4.4/slasher/db/kv/validator_id_pubkey.go (about)

     1  package kv
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/pkg/errors"
     7  	types "github.com/prysmaticlabs/eth2-types"
     8  	"github.com/prysmaticlabs/prysm/shared/bytesutil"
     9  	bolt "go.etcd.io/bbolt"
    10  	"go.opencensus.io/trace"
    11  )
    12  
    13  // ValidatorPubKey accepts validator id and returns the corresponding pubkey.
    14  // Returns nil if the pubkey for this validator id does not exist.
    15  func (s *Store) ValidatorPubKey(ctx context.Context, validatorIndex types.ValidatorIndex) ([]byte, error) {
    16  	ctx, span := trace.StartSpan(ctx, "SlasherDB.ValidatorPubKey")
    17  	defer span.End()
    18  	var pk []byte
    19  	err := s.view(func(tx *bolt.Tx) error {
    20  		b := tx.Bucket(validatorsPublicKeysBucket)
    21  		pk = b.Get(bytesutil.Bytes4(uint64(validatorIndex)))
    22  		return nil
    23  	})
    24  	return pk, err
    25  }
    26  
    27  // SavePubKey accepts a validator id and its public key  and writes it to disk.
    28  func (s *Store) SavePubKey(ctx context.Context, validatorIndex types.ValidatorIndex, pubKey []byte) error {
    29  	ctx, span := trace.StartSpan(ctx, "SlasherDB.SavePubKey")
    30  	defer span.End()
    31  	err := s.update(func(tx *bolt.Tx) error {
    32  		bucket := tx.Bucket(validatorsPublicKeysBucket)
    33  		key := bytesutil.Bytes4(uint64(validatorIndex))
    34  		if err := bucket.Put(key, pubKey); err != nil {
    35  			return errors.Wrap(err, "failed to add validator public key to slasher s.")
    36  		}
    37  		return nil
    38  	})
    39  	return err
    40  }
    41  
    42  // DeletePubKey deletes a public key of a validator id.
    43  func (s *Store) DeletePubKey(ctx context.Context, validatorIndex types.ValidatorIndex) error {
    44  	ctx, span := trace.StartSpan(ctx, "SlasherDB.DeletePubKey")
    45  	defer span.End()
    46  	return s.update(func(tx *bolt.Tx) error {
    47  		bucket := tx.Bucket(validatorsPublicKeysBucket)
    48  		key := bytesutil.Bytes4(uint64(validatorIndex))
    49  		if err := bucket.Delete(key); err != nil {
    50  			return errors.Wrap(err, "failed to delete public key from validators public key bucket")
    51  		}
    52  		return bucket.Delete(key)
    53  	})
    54  }