prysm-pulse/slasher/db/kv/validator_id_pubkey_test.go
terence tsao 6b82873737
Use validatorIndex instead of validatorID (#8498)
* Use ValidtorIndex across Prysm. Build ok

* First take at fixing tests

* Clean up e2e, fuzz... etc

* Fix new lines

* Update beacon-chain/cache/proposer_indices_test.go

Co-authored-by: Raul Jordan <raul@prysmaticlabs.com>

* Update beacon-chain/core/helpers/rewards_penalties.go

Co-authored-by: Raul Jordan <raul@prysmaticlabs.com>

* Update beacon-chain/core/helpers/shuffle.go

Co-authored-by: Raul Jordan <raul@prysmaticlabs.com>

* Update validator/graffiti/parse_graffiti_test.go

Co-authored-by: Raul Jordan <raul@prysmaticlabs.com>

* Raul's feedback

* Fix downcast int -> uint64

* Victor's feedback

* Replace validator id with validator index

* Replace validator id with validator index

* Typo

Co-authored-by: Raul Jordan <raul@prysmaticlabs.com>
2021-02-22 19:40:58 -06:00

85 lines
2.0 KiB
Go

package kv
import (
"context"
"testing"
types "github.com/prysmaticlabs/eth2-types"
"github.com/prysmaticlabs/prysm/shared/testutil/require"
)
type publicKeyTestStruct struct {
validatorIndex types.ValidatorIndex
pk []byte
}
var pkTests []publicKeyTestStruct
func init() {
pkTests = []publicKeyTestStruct{
{
validatorIndex: 1,
pk: []byte{1, 2, 3},
},
{
validatorIndex: 2,
pk: []byte{4, 5, 6},
},
{
validatorIndex: 3,
pk: []byte{7, 8, 9},
},
}
}
func TestNilDBValidatorPublicKey(t *testing.T) {
db := setupDB(t)
ctx := context.Background()
validatorIndex := types.ValidatorIndex(1)
pk, err := db.ValidatorPubKey(ctx, validatorIndex)
require.NoError(t, err, "Nil ValidatorPubKey should not return error")
require.DeepEqual(t, []uint8(nil), pk)
}
func TestSavePubKey(t *testing.T) {
db := setupDB(t)
ctx := context.Background()
for _, tt := range pkTests {
err := db.SavePubKey(ctx, tt.validatorIndex, tt.pk)
require.NoError(t, err, "Save validator public key failed")
pk, err := db.ValidatorPubKey(ctx, tt.validatorIndex)
require.NoError(t, err, "Failed to get validator public key")
require.NotNil(t, pk)
require.DeepEqual(t, tt.pk, pk, "Should return validator public key")
}
}
func TestDeletePublicKey(t *testing.T) {
db := setupDB(t)
ctx := context.Background()
for _, tt := range pkTests {
require.NoError(t, db.SavePubKey(ctx, tt.validatorIndex, tt.pk), "Save validator public key failed")
}
for _, tt := range pkTests {
pk, err := db.ValidatorPubKey(ctx, tt.validatorIndex)
require.NoError(t, err, "Failed to get validator public key")
require.NotNil(t, pk)
require.DeepEqual(t, tt.pk, pk, "Should return validator public key")
err = db.DeletePubKey(ctx, tt.validatorIndex)
require.NoError(t, err, "Delete validator public key")
pk, err = db.ValidatorPubKey(ctx, tt.validatorIndex)
require.NoError(t, err)
require.DeepEqual(t, []byte(nil), pk, "Expected validator public key to be deleted")
}
}