mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2024-12-28 14:17:17 +00:00
ec41d37cf4
* Revert "status err code" This reverts commit 27112ff2841ae7ccfb7d7eca665e06bbad81755d. * imports
88 lines
2.1 KiB
Go
88 lines
2.1 KiB
Go
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])
|
|
})
|
|
}
|
|
|
|
// 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])
|
|
})
|
|
|
|
}
|
|
|
|
// ValidatorIndex accepts a public key and returns the corresponding validator index.
|
|
func (db *BeaconDB) ValidatorIndex(pubKey []byte) (uint64, error) {
|
|
if !db.HasValidator(pubKey) {
|
|
return 0, fmt.Errorf("validator %#x does not exist", pubKey)
|
|
}
|
|
|
|
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 {
|
|
a := tx.Bucket(validatorBucket)
|
|
|
|
return a.Delete(h[:])
|
|
})
|
|
}
|
|
|
|
// 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 {
|
|
a := tx.Bucket(validatorBucket)
|
|
|
|
exists = a.Get(h[:]) != nil
|
|
return nil
|
|
})
|
|
return exists
|
|
}
|