2020-06-02 14:41:21 +00:00
|
|
|
package kv
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/hex"
|
|
|
|
"flag"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/prysmaticlabs/prysm/slasher/detection/attestations/types"
|
|
|
|
"github.com/urfave/cli/v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
type spansTestStruct struct {
|
|
|
|
name string
|
|
|
|
epoch uint64
|
|
|
|
spansHex string
|
|
|
|
spansResultHex string
|
|
|
|
validator1Span types.Span
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
|
|
|
var spanNewTests []spansTestStruct
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
spanNewTests = []spansTestStruct{
|
|
|
|
{
|
|
|
|
name: "span too small",
|
|
|
|
epoch: 1,
|
|
|
|
spansHex: "00000000",
|
|
|
|
spansResultHex: "",
|
|
|
|
validator1Span: types.Span{},
|
2020-06-11 18:17:44 +00:00
|
|
|
err: types.ErrWrongSize,
|
2020-06-02 14:41:21 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "no validator 1 in spans",
|
|
|
|
epoch: 2,
|
|
|
|
spansHex: "00000000000000",
|
|
|
|
spansResultHex: "00000000000000",
|
|
|
|
validator1Span: types.Span{},
|
|
|
|
err: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "validator 1 in spans",
|
|
|
|
epoch: 3,
|
|
|
|
spansHex: "0000000000000001000000000000",
|
|
|
|
spansResultHex: "0000000000000001000000000000",
|
|
|
|
validator1Span: types.Span{MinSpan: 1},
|
|
|
|
err: nil,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestValidatorSpans_NilDB(t *testing.T) {
|
|
|
|
app := cli.App{}
|
|
|
|
set := flag.NewFlagSet("test", 0)
|
|
|
|
db := setupDB(t, cli.NewContext(&app, set, nil))
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
validatorIdx := uint64(1)
|
|
|
|
es, err := db.EpochSpans(ctx, validatorIdx)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Nil EpochSpansMap should not return error: %v", err)
|
|
|
|
}
|
2020-06-11 18:17:44 +00:00
|
|
|
cleanStore, err := types.NewEpochStore([]byte{})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(es, cleanStore) {
|
2020-06-02 14:41:21 +00:00
|
|
|
t.Fatal("EpochSpans should return empty byte array if no record exists in the db")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStore_SaveReadEpochSpans(t *testing.T) {
|
|
|
|
app := cli.App{}
|
|
|
|
set := flag.NewFlagSet("test", 0)
|
|
|
|
db := setupDB(t, cli.NewContext(&app, set, nil))
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
for _, tt := range spanNewTests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
spans, err := hex.DecodeString(tt.spansHex)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-06-11 18:17:44 +00:00
|
|
|
es, err := types.NewEpochStore(spans)
|
2020-06-02 14:41:21 +00:00
|
|
|
if err != tt.err {
|
|
|
|
t.Fatalf("Failed to get the right error expected: %v got: %v", tt.err, err)
|
|
|
|
}
|
2020-06-11 18:17:44 +00:00
|
|
|
if err = db.SaveEpochSpans(ctx, tt.epoch, es); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-06-02 14:41:21 +00:00
|
|
|
sm, err := db.EpochSpans(ctx, tt.epoch)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to get validator spans: %v", err)
|
|
|
|
}
|
|
|
|
spansResult, err := hex.DecodeString(tt.spansResultHex)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-06-11 18:17:44 +00:00
|
|
|
esr, err := types.NewEpochStore(spansResult)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-06-02 14:41:21 +00:00
|
|
|
if !reflect.DeepEqual(sm, esr) {
|
|
|
|
t.Fatalf("Get should return validator spans: %v got: %v", spansResult, sm)
|
|
|
|
}
|
|
|
|
|
2020-06-11 18:17:44 +00:00
|
|
|
s, err := es.GetValidatorSpan(1)
|
|
|
|
if err != nil {
|
2020-06-02 14:41:21 +00:00
|
|
|
t.Fatalf("Failed to get validator 1 span: %v", err)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(s, tt.validator1Span) {
|
|
|
|
t.Fatalf("Get should return validator span for validator 2: %v got: %v", tt.validator1Span, s)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|