prysm-pulse/beacon-chain/db/kv/validators.go
Preston Van Loon 2c28e4e7a3 Improvements to Committee Assignments for multiple key requests (#4294)
* Add committees helper, benchmark, results show 62ms for 8k validators which was previously 4 minutes
* Add regression test with same data
* fix epoch conversion
* lint
* undo and lint
* Merge branch 'master' of github.com:prysmaticlabs/prysm into zoom-zoom-assignments
* remove validaotr index span
* fix comment, add test to test against spec definition method for consistency.
* Deprecate CommitteeAssignment, delete unused reference to CommitteeAssignment
* Merge branch 'master' of github.com:prysmaticlabs/prysm into zoom-zoom-assignments
* remove new line
* make test be more complicated with validators activated in an epoch transition
* add feature flag for fast-assignments
* Merge branch 'master' of github.com:prysmaticlabs/prysm into zoom-zoom-assignments
* gaz, gofmt, add deprecated code back
* Update beacon-chain/core/helpers/committee.go

Co-Authored-By: Ivan Martinez <ivanthegreatdev@gmail.com>
* Merge refs/heads/master into zoom-zoom-assignments
* Merge refs/heads/master into zoom-zoom-assignments
* Merge refs/heads/master into zoom-zoom-assignments
2019-12-17 03:05:26 +00:00

78 lines
2.2 KiB
Go

package kv
import (
"context"
"encoding/binary"
"github.com/boltdb/bolt"
"go.opencensus.io/trace"
)
// ValidatorIndex by public key.
func (k *Store) ValidatorIndex(ctx context.Context, publicKey [48]byte) (uint64, bool, error) {
// Return latest validatorIndex from cache if it exists.
if v, ok := k.validatorIndexCache.Get(string(publicKey[:])); v != nil && ok {
return v.(uint64), true, nil
}
var validatorIdx uint64
var ok bool
err := k.db.View(func(tx *bolt.Tx) error {
bkt := tx.Bucket(validatorsBucket)
enc := bkt.Get(publicKey[:])
if enc == nil {
return nil
}
validatorIdx = binary.LittleEndian.Uint64(enc)
ok = true
return nil
})
return validatorIdx, ok, err
}
// HasValidatorIndex verifies if a validator's index by public key exists in the db.
func (k *Store) HasValidatorIndex(ctx context.Context, publicKey [48]byte) bool {
ctx, span := trace.StartSpan(ctx, "BeaconDB.HasValidatorIndex")
defer span.End()
if v, ok := k.validatorIndexCache.Get(string(publicKey[:])); v != nil && ok {
return true
}
exists := false
// #nosec G104. Always returns nil.
k.db.View(func(tx *bolt.Tx) error {
bkt := tx.Bucket(validatorsBucket)
exists = bkt.Get(publicKey[:]) != nil
return nil
})
return exists
}
// DeleteValidatorIndex clears a validator index from the db by the validator's public key.
func (k *Store) DeleteValidatorIndex(ctx context.Context, publicKey [48]byte) error {
ctx, span := trace.StartSpan(ctx, "BeaconDB.DeleteValidatorIndex")
defer span.End()
return k.db.Update(func(tx *bolt.Tx) error {
bucket := tx.Bucket(validatorsBucket)
k.validatorIndexCache.Del(string(publicKey[:]))
return bucket.Delete(publicKey[:])
})
}
// SaveValidatorIndex by public key in the db.
func (k *Store) SaveValidatorIndex(ctx context.Context, publicKey [48]byte, validatorIdx uint64) error {
ctx, span := trace.StartSpan(ctx, "BeaconDB.SaveValidatorIndex")
defer span.End()
return k.db.Update(func(tx *bolt.Tx) error {
bucket := tx.Bucket(validatorsBucket)
buf := uint64ToBytes(validatorIdx)
k.validatorIndexCache.Set(string(publicKey[:]), validatorIdx, int64(len(buf)))
return bucket.Put(publicKey[:], buf)
})
}
func uint64ToBytes(i uint64) []byte {
buf := make([]byte, 8)
binary.LittleEndian.PutUint64(buf, i)
return buf
}