prysm-pulse/beacon-chain/db/kv/validators_test.go
Raul Jordan 6bd8ae8f67
Implement Validator DB Methods (#3172)
* begin db interface

* define the database interface

* interface definition simplifications

* include latest message proto

* modify pbs

* rem kv folder

* add filter interface

* lint

* ctx package is great

* interface getting better

* ctx everywhere...it's everywhere!

* block roots method

* new kv store initialization

* comments

* gaz

* implement interface

* refactor for proper naming conventions

* add todos

* proper comments

* rem unused

* add schema

* implementation simplicity

* has validator latest vote func impl

* retrieve validator latest vote

* has idx

* implement missing validator methods

* missing validator methods and test helpers

* validator index crud tests

* validator tests

* all buckets

* refactor with ok bool

* all tests passing, fmt, imports
2019-08-12 14:33:07 -05:00

68 lines
1.7 KiB
Go

package kv
import (
"context"
"testing"
"github.com/gogo/protobuf/proto"
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
)
func TestStore_ValidatorIndexCRUD(t *testing.T) {
db := setupDB(t)
defer teardownDB(t, db)
validatorIdx := uint64(100)
pubKey := [48]byte{1, 2, 3, 4}
ctx := context.Background()
_, ok, err := db.ValidatorIndex(ctx, pubKey)
if err != nil {
t.Fatal(err)
}
if ok {
t.Fatal("Expected validator index to not exist")
}
if err := db.SaveValidatorIndex(ctx, pubKey, validatorIdx); err != nil {
t.Fatal(err)
}
retrievedIdx, ok, err := db.ValidatorIndex(ctx, pubKey)
if err != nil {
t.Fatal(err)
}
if !ok {
t.Fatal("Expected validator index to have been properly retrieved")
}
if retrievedIdx != validatorIdx {
t.Errorf("Wanted %d, received %d", validatorIdx, retrievedIdx)
}
if err := db.DeleteValidatorIndex(ctx, pubKey); err != nil {
t.Fatal(err)
}
if db.HasValidatorIndex(ctx, pubKey) {
t.Error("Expected validator index to have been deleted from the db")
}
}
func TestStore_ValidatorLatestVoteCRUD(t *testing.T) {
db := setupDB(t)
defer teardownDB(t, db)
validatorIdx := uint64(100)
latestVote := &pb.ValidatorLatestVote{
Epoch: 1,
Root: []byte("root"),
}
ctx := context.Background()
if err := db.SaveValidatorLatestVote(ctx, validatorIdx, latestVote); err != nil {
t.Fatal(err)
}
if !db.HasValidatorLatestVote(ctx, validatorIdx) {
t.Error("Expected validator latest vote to exist in the db")
}
retrievedVote, err := db.ValidatorLatestVote(ctx, validatorIdx)
if err != nil {
t.Fatal(err)
}
if !proto.Equal(latestVote, retrievedVote) {
t.Errorf("Wanted %d, received %d", latestVote, retrievedVote)
}
}