prysm-pulse/beacon-chain/db/validator.go
shayzluf 0732012459 Validator-multiple key (#2069)
* first version - broken

* working proto changes

* resolve review remarks

* fix goimport issues

* fix service issues

* first logic version-broken

* first running version - no new tests

* fix validator client test

* add wait group to goroutines

* remove unused var in function call

* fix review remarks and tests

* merge master changes and fix conflicts

* gazzele fix

* fix prestonvanloon requested changes

* merge and some of terenc3t remarks addressed

* _,pk bug fix in log

* fix account file name suffix and filter not active validator out

* merge with master and fix missing parameters

* run over all public keys in hasvalidators

* add test for error when no all the validators has index in the db and hasvalidators is called

* fix runner tests fail due to timing issues

* goimports

* smaller sleep time in proposer tests

* fix UpdateAssignments loging

* fix goimports

* added && false commented TestUpdateAssignments_DoesNothingWhenNotEpochStartAndAlreadyExistingAssignments

* hasvalidators without missing publickeys list

* fix some of prestone review remarks

* fixes for prestone comments

* review changes applied

* expect context call in TestWaitForActivation_ValidatorOriginallyExists

* changed hasvalidators to return true if one validator exists

* fix init problem to getkeys

* hasvalidators requiers all validators to be in db

* validator attest assignments update

* fix ap var name

* Change name to hasallvalidators

* fix tests

* update script, fix any vs all validator calls

* fix wait for activation

* filter validator

* reuse the reply block

* fix imports

* Remove dup

* better lookup of active validators

* better filter active vlaidators, still need to fix committee assignment tests

* lint

* use activated keys

* fix for postchainstart

* fix logging

* move state transitions

* hasanyvalidator and hasallvalidators

* fix tests with updatechainhead missing

* add tests

* fix TestCommitteeAssignment_OK

* fix test

* fix validator tests

* fix TestCommitteeAssignment_multipleKeys_OK and TestWaitForActivation_ValidatorOriginallyExists

* fix goimports

* removed unused param from assignment

* change string(pk) to hex.EncodeString(pk) fix change requests

* add inactive validator status to assignments

* fix logging mess due to multi validator setup

* set no assignment to debug level

* log assignments every epoch

* logging fixes

* fixed runtime by using the right assignments

* correct activation request

* fix the validator panic

* correct assignment

* fix test fail and waitforactivation

* performance log issue fix

* fix goimports

* add log message with truncated pk for attest

* add truncated pk to attest and propose logs

* Add comment to script, change 9 to 8

* Update assignment log

* Add comment, report number of assignments

* Use WithError, add validator as field, merge block proposal log

* Update validator_propose.go

* fix

* use entry.String()

* fix fmt
2019-04-18 12:23:38 -05:00

119 lines
3.0 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 {
bkt := tx.Bucket(validatorBucket)
return bkt.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 {
bkt := tx.Bucket(validatorBucket)
exists = bkt.Get(h[:]) != nil
return nil
})
return exists
}
// 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
}