2019-02-25 17:37:02 +00:00
|
|
|
package db
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/binary"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/boltdb/bolt"
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/hashutil"
|
|
|
|
)
|
|
|
|
|
|
|
|
// SaveValidatorIndex accepts a public key and validator index and writes them to disk.
|
|
|
|
func (db *BeaconDB) SaveValidatorIndex(pubKey []byte, index int) error {
|
|
|
|
h := hashutil.Hash(pubKey)
|
|
|
|
|
|
|
|
return db.update(func(tx *bolt.Tx) error {
|
|
|
|
bucket := tx.Bucket(validatorBucket)
|
|
|
|
|
|
|
|
buf := make([]byte, binary.MaxVarintLen64)
|
|
|
|
n := binary.PutUvarint(buf, uint64(index))
|
|
|
|
|
|
|
|
return bucket.Put(h[:], buf[:n])
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-02-28 05:14:52 +00:00
|
|
|
// SaveValidatorIndexBatch accepts a public key and validator index and writes them to disk.
|
|
|
|
func (db *BeaconDB) SaveValidatorIndexBatch(pubKey []byte, index int) error {
|
|
|
|
h := hashutil.Hash(pubKey)
|
|
|
|
|
|
|
|
return db.batch(func(tx *bolt.Tx) error {
|
|
|
|
bucket := tx.Bucket(validatorBucket)
|
|
|
|
buf := make([]byte, binary.MaxVarintLen64)
|
|
|
|
n := binary.PutUvarint(buf, uint64(index))
|
|
|
|
return bucket.Put(h[:], buf[:n])
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-02-25 17:37:02 +00:00
|
|
|
// ValidatorIndex accepts a public key and returns the corresponding validator index.
|
|
|
|
func (db *BeaconDB) ValidatorIndex(pubKey []byte) (uint64, error) {
|
|
|
|
if !db.HasValidator(pubKey) {
|
2019-03-26 21:11:21 +00:00
|
|
|
return 0, fmt.Errorf("validator %#x does not exist", pubKey)
|
2019-02-25 17:37:02 +00:00
|
|
|
}
|
|
|
|
var index uint64
|
|
|
|
h := hashutil.Hash(pubKey)
|
|
|
|
|
|
|
|
err := db.view(func(tx *bolt.Tx) error {
|
|
|
|
bucket := tx.Bucket(validatorBucket)
|
|
|
|
|
|
|
|
enc := bucket.Get(h[:])
|
|
|
|
if enc == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
var err error
|
|
|
|
buf := bytes.NewBuffer(enc)
|
|
|
|
index, err = binary.ReadUvarint(buf)
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
|
|
|
|
return index, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteValidatorIndex deletes the validator index map record.
|
|
|
|
func (db *BeaconDB) DeleteValidatorIndex(pubKey []byte) error {
|
|
|
|
h := hashutil.Hash(pubKey)
|
|
|
|
|
|
|
|
return db.update(func(tx *bolt.Tx) error {
|
2019-04-14 10:02:14 +00:00
|
|
|
bkt := tx.Bucket(validatorBucket)
|
2019-02-25 17:37:02 +00:00
|
|
|
|
2019-04-14 10:02:14 +00:00
|
|
|
return bkt.Delete(h[:])
|
2019-02-25 17:37:02 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// HasValidator checks if a validator index map exists.
|
|
|
|
func (db *BeaconDB) HasValidator(pubKey []byte) bool {
|
|
|
|
exists := false
|
|
|
|
h := hashutil.Hash(pubKey)
|
|
|
|
// #nosec G104, similar to HasBlock, HasAttestation... etc
|
|
|
|
db.view(func(tx *bolt.Tx) error {
|
2019-04-14 10:02:14 +00:00
|
|
|
bkt := tx.Bucket(validatorBucket)
|
2019-02-25 17:37:02 +00:00
|
|
|
|
2019-04-14 10:02:14 +00:00
|
|
|
exists = bkt.Get(h[:]) != nil
|
2019-02-25 17:37:02 +00:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
return exists
|
|
|
|
}
|
2019-04-14 10:02:14 +00:00
|
|
|
|
|
|
|
// HasAllValidators returns true if all validators in a list of public keys
|
|
|
|
// are in the bucket.
|
|
|
|
func (db *BeaconDB) HasAllValidators(pubKeys [][]byte) bool {
|
|
|
|
return db.hasValidators(pubKeys, true /* requireAll */)
|
|
|
|
}
|
|
|
|
|
|
|
|
// HasAnyValidators returns true if any validator in a list of public keys
|
|
|
|
// are in the bucket.
|
|
|
|
func (db *BeaconDB) HasAnyValidators(pubKeys [][]byte) bool {
|
|
|
|
return db.hasValidators(pubKeys, false /* requireAll */)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *BeaconDB) hasValidators(pubKeys [][]byte, requireAll bool) bool {
|
|
|
|
exists := false
|
|
|
|
// #nosec G104, similar to HasBlock, HasAttestation... etc
|
|
|
|
db.view(func(tx *bolt.Tx) error {
|
|
|
|
bkt := tx.Bucket(validatorBucket)
|
|
|
|
for _, pk := range pubKeys {
|
|
|
|
h := hashutil.Hash(pk)
|
|
|
|
exists = bkt.Get(h[:]) != nil
|
|
|
|
if !exists && requireAll {
|
|
|
|
break
|
|
|
|
} else if exists && !requireAll {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
return exists
|
|
|
|
}
|