2020-02-13 16:19:46 +00:00
|
|
|
package kv
|
2019-11-06 20:08:20 +00:00
|
|
|
|
|
|
|
import (
|
2020-02-13 19:51:30 +00:00
|
|
|
"context"
|
2020-01-22 05:39:21 +00:00
|
|
|
"flag"
|
2019-11-12 17:24:56 +00:00
|
|
|
"reflect"
|
2019-11-06 20:08:20 +00:00
|
|
|
"testing"
|
2020-01-22 05:39:21 +00:00
|
|
|
"time"
|
2019-11-06 20:08:20 +00:00
|
|
|
|
2020-03-05 05:35:14 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/slasher/detection/attestations/types"
|
2020-05-31 06:44:34 +00:00
|
|
|
"github.com/urfave/cli/v2"
|
2019-11-06 20:08:20 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type spanMapTestStruct struct {
|
2020-03-05 05:35:14 +00:00
|
|
|
epoch uint64
|
|
|
|
spanMap map[uint64]types.Span
|
2019-11-06 20:08:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var spanTests []spanMapTestStruct
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
spanTests = []spanMapTestStruct{
|
|
|
|
{
|
2020-03-05 05:35:14 +00:00
|
|
|
epoch: 1,
|
|
|
|
spanMap: map[uint64]types.Span{
|
|
|
|
1: {MinSpan: 10, MaxSpan: 20, HasAttested: false, SigBytes: [2]byte{1, 1}},
|
|
|
|
2: {MinSpan: 11, MaxSpan: 21, HasAttested: true, SigBytes: [2]byte{1, 1}},
|
|
|
|
3: {MinSpan: 12, MaxSpan: 22, HasAttested: false, SigBytes: [2]byte{1, 1}},
|
2019-11-06 20:08:20 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2020-03-05 05:35:14 +00:00
|
|
|
epoch: 2,
|
|
|
|
spanMap: map[uint64]types.Span{
|
|
|
|
1: {MinSpan: 10, MaxSpan: 20, HasAttested: false, SigBytes: [2]byte{1, 1}},
|
|
|
|
2: {MinSpan: 11, MaxSpan: 21, HasAttested: true, SigBytes: [2]byte{1, 1}},
|
|
|
|
3: {MinSpan: 12, MaxSpan: 22, HasAttested: true, SigBytes: [2]byte{1, 1}},
|
2019-11-06 20:08:20 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2020-03-05 05:35:14 +00:00
|
|
|
epoch: 3,
|
|
|
|
spanMap: map[uint64]types.Span{
|
|
|
|
1: {MinSpan: 10, MaxSpan: 20, HasAttested: true, SigBytes: [2]byte{1, 1}},
|
|
|
|
2: {MinSpan: 11, MaxSpan: 21, SigBytes: [2]byte{1, 1}},
|
|
|
|
3: {MinSpan: 12, MaxSpan: 22, SigBytes: [2]byte{1, 1}},
|
2019-11-06 20:08:20 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestValidatorSpanMap_NilDB(t *testing.T) {
|
2020-03-19 21:46:44 +00:00
|
|
|
app := cli.App{}
|
2020-01-22 05:39:21 +00:00
|
|
|
set := flag.NewFlagSet("test", 0)
|
2020-03-19 21:46:44 +00:00
|
|
|
db := setupDB(t, cli.NewContext(&app, set, nil))
|
2020-02-13 19:51:30 +00:00
|
|
|
ctx := context.Background()
|
2019-11-06 20:08:20 +00:00
|
|
|
|
|
|
|
validatorIdx := uint64(1)
|
2020-04-23 19:27:06 +00:00
|
|
|
vsm, _, err := db.EpochSpansMap(ctx, validatorIdx)
|
2019-11-06 20:08:20 +00:00
|
|
|
if err != nil {
|
2020-03-05 05:35:14 +00:00
|
|
|
t.Fatalf("Nil EpochSpansMap should not return error: %v", err)
|
2019-11-06 20:08:20 +00:00
|
|
|
}
|
2020-03-05 05:35:14 +00:00
|
|
|
if !reflect.DeepEqual(vsm, map[uint64]types.Span{}) {
|
|
|
|
t.Fatal("EpochSpansMap should return nil")
|
2019-11-06 20:08:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-05 05:35:14 +00:00
|
|
|
func TestStore_SaveSpans(t *testing.T) {
|
2020-03-19 21:46:44 +00:00
|
|
|
app := cli.App{}
|
2020-01-22 05:39:21 +00:00
|
|
|
set := flag.NewFlagSet("test", 0)
|
2020-03-19 21:46:44 +00:00
|
|
|
db := setupDB(t, cli.NewContext(&app, set, nil))
|
2020-02-13 19:51:30 +00:00
|
|
|
ctx := context.Background()
|
2019-11-06 20:08:20 +00:00
|
|
|
|
|
|
|
for _, tt := range spanTests {
|
2020-03-05 05:35:14 +00:00
|
|
|
err := db.SaveEpochSpansMap(ctx, tt.epoch, tt.spanMap)
|
2019-11-06 20:08:20 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Save validator span map failed: %v", err)
|
|
|
|
}
|
2020-04-23 19:27:06 +00:00
|
|
|
sm, _, err := db.EpochSpansMap(ctx, tt.epoch)
|
2019-11-06 20:08:20 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to get validator span map: %v", err)
|
|
|
|
}
|
|
|
|
|
2020-03-05 05:35:14 +00:00
|
|
|
if sm == nil || !reflect.DeepEqual(sm, tt.spanMap) {
|
2019-11-06 20:08:20 +00:00
|
|
|
t.Fatalf("Get should return validator span map: %v got: %v", tt.spanMap, sm)
|
|
|
|
}
|
2020-03-05 05:35:14 +00:00
|
|
|
s, err := db.EpochSpanByValidatorIndex(ctx, 1, tt.epoch)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to get validator span for epoch 1: %v", err)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(s, tt.spanMap[1]) {
|
|
|
|
t.Fatalf("Get should return validator spans for epoch 1: %v got: %v", tt.spanMap[1], s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStore_SaveCachedSpans(t *testing.T) {
|
2020-03-19 21:46:44 +00:00
|
|
|
app := cli.App{}
|
2020-01-22 05:39:21 +00:00
|
|
|
set := flag.NewFlagSet("test", 0)
|
2020-03-19 21:46:44 +00:00
|
|
|
db := setupDB(t, cli.NewContext(&app, set, nil))
|
2020-02-13 19:51:30 +00:00
|
|
|
ctx := context.Background()
|
2020-01-22 05:39:21 +00:00
|
|
|
|
|
|
|
for _, tt := range spanTests {
|
2020-03-05 05:35:14 +00:00
|
|
|
err := db.SaveEpochSpansMap(ctx, tt.epoch, tt.spanMap)
|
2020-01-22 05:39:21 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Save validator span map failed: %v", err)
|
|
|
|
}
|
|
|
|
// wait for value to pass through cache buffers
|
|
|
|
time.Sleep(time.Millisecond * 10)
|
2020-04-23 19:27:06 +00:00
|
|
|
sm, _, err := db.EpochSpansMap(ctx, tt.epoch)
|
2020-01-22 05:39:21 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to get validator span map: %v", err)
|
|
|
|
}
|
|
|
|
|
2020-03-05 05:35:14 +00:00
|
|
|
if sm == nil || !reflect.DeepEqual(sm, tt.spanMap) {
|
2020-01-22 05:39:21 +00:00
|
|
|
t.Fatalf("Get should return validator span map: %v got: %v", tt.spanMap, sm)
|
|
|
|
}
|
2020-03-05 05:35:14 +00:00
|
|
|
s, err := db.EpochSpanByValidatorIndex(ctx, 1, tt.epoch)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to get validator span for epoch 1: %v", err)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(s, tt.spanMap[1]) {
|
|
|
|
t.Fatalf("Get should return validator spans for epoch 1: %v got: %v", tt.spanMap[1], s)
|
|
|
|
}
|
2020-01-22 05:39:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-05 05:35:14 +00:00
|
|
|
func TestStore_DeleteEpochSpans(t *testing.T) {
|
2020-03-19 21:46:44 +00:00
|
|
|
app := cli.App{}
|
2020-01-22 05:39:21 +00:00
|
|
|
set := flag.NewFlagSet("test", 0)
|
2020-03-19 21:46:44 +00:00
|
|
|
db := setupDB(t, cli.NewContext(&app, set, nil))
|
2020-02-13 19:51:30 +00:00
|
|
|
ctx := context.Background()
|
2020-04-22 15:53:09 +00:00
|
|
|
db.spanCacheEnabled = false
|
2019-11-06 20:08:20 +00:00
|
|
|
for _, tt := range spanTests {
|
2020-03-05 05:35:14 +00:00
|
|
|
err := db.SaveEpochSpansMap(ctx, tt.epoch, tt.spanMap)
|
2019-11-06 20:08:20 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Save validator span map failed: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range spanTests {
|
2020-04-23 19:27:06 +00:00
|
|
|
sm, _, err := db.EpochSpansMap(ctx, tt.epoch)
|
2019-11-06 20:08:20 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to get validator span map: %v", err)
|
|
|
|
}
|
2020-03-05 05:35:14 +00:00
|
|
|
if sm == nil || !reflect.DeepEqual(sm, tt.spanMap) {
|
2019-11-06 20:08:20 +00:00
|
|
|
t.Fatalf("Get should return validator span map: %v got: %v", tt.spanMap, sm)
|
|
|
|
}
|
2020-03-05 05:35:14 +00:00
|
|
|
err = db.DeleteEpochSpans(ctx, tt.epoch)
|
2019-11-06 20:08:20 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Delete validator span map error: %v", err)
|
|
|
|
}
|
2020-04-23 19:27:06 +00:00
|
|
|
sm, _, err = db.EpochSpansMap(ctx, tt.epoch)
|
2019-11-06 20:08:20 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-03-05 05:35:14 +00:00
|
|
|
if !reflect.DeepEqual(sm, map[uint64]types.Span{}) {
|
2019-11-06 20:08:20 +00:00
|
|
|
t.Errorf("Expected validator span map to be deleted, received: %v", sm)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-01-22 05:39:21 +00:00
|
|
|
|
2020-03-08 21:11:59 +00:00
|
|
|
func TestValidatorSpanMap_DeletesOnCacheSavesToDB(t *testing.T) {
|
2020-03-19 21:46:44 +00:00
|
|
|
app := cli.App{}
|
2020-01-22 05:39:21 +00:00
|
|
|
set := flag.NewFlagSet("test", 0)
|
2020-03-19 21:46:44 +00:00
|
|
|
db := setupDB(t, cli.NewContext(&app, set, nil))
|
2020-02-13 19:51:30 +00:00
|
|
|
ctx := context.Background()
|
2020-01-22 05:39:21 +00:00
|
|
|
|
|
|
|
for _, tt := range spanTests {
|
2020-03-05 05:35:14 +00:00
|
|
|
err := db.SaveEpochSpansMap(ctx, tt.epoch, tt.spanMap)
|
2020-01-22 05:39:21 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Save validator span map failed: %v", err)
|
|
|
|
}
|
|
|
|
}
|
2020-03-08 21:11:59 +00:00
|
|
|
// Wait for value to pass through cache buffers.
|
2020-01-22 05:39:21 +00:00
|
|
|
time.Sleep(time.Millisecond * 10)
|
|
|
|
for _, tt := range spanTests {
|
2020-04-23 19:27:06 +00:00
|
|
|
spanMap, _, err := db.EpochSpansMap(ctx, tt.epoch)
|
2020-01-22 05:39:21 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to get validator span map: %v", err)
|
|
|
|
}
|
2020-03-08 21:11:59 +00:00
|
|
|
if spanMap == nil || !reflect.DeepEqual(spanMap, tt.spanMap) {
|
|
|
|
t.Fatalf("Get should return validator span map: %v got: %v", tt.spanMap, spanMap)
|
2020-01-22 05:39:21 +00:00
|
|
|
}
|
2020-03-08 21:11:59 +00:00
|
|
|
|
|
|
|
if err = db.DeleteEpochSpans(ctx, tt.epoch); err != nil {
|
2020-01-22 05:39:21 +00:00
|
|
|
t.Fatalf("Delete validator span map error: %v", err)
|
|
|
|
}
|
2020-03-08 21:11:59 +00:00
|
|
|
// Wait for value to pass through cache buffers.
|
2020-04-23 19:27:06 +00:00
|
|
|
db.EnableSpanCache(false)
|
2020-01-22 05:39:21 +00:00
|
|
|
time.Sleep(time.Millisecond * 10)
|
2020-04-23 19:27:06 +00:00
|
|
|
spanMap, _, err = db.EpochSpansMap(ctx, tt.epoch)
|
2020-01-22 05:39:21 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-04-23 19:27:06 +00:00
|
|
|
db.EnableSpanCache(true)
|
2020-03-08 21:11:59 +00:00
|
|
|
if !reflect.DeepEqual(spanMap, tt.spanMap) {
|
|
|
|
t.Errorf("Expected validator span map to be deleted, received: %v", spanMap)
|
2020-01-22 05:39:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestValidatorSpanMap_SaveOnEvict(t *testing.T) {
|
2020-03-09 05:35:39 +00:00
|
|
|
db := setupDBDiffCacheSize(t, 5)
|
2020-02-13 19:51:30 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
|
2020-01-22 05:39:21 +00:00
|
|
|
tsm := &spanMapTestStruct{
|
2020-03-05 05:35:14 +00:00
|
|
|
epoch: 1,
|
|
|
|
spanMap: map[uint64]types.Span{
|
|
|
|
1: {MinSpan: 10, MaxSpan: 20, SigBytes: [2]byte{0, 1}},
|
|
|
|
2: {MinSpan: 11, MaxSpan: 21, HasAttested: true},
|
|
|
|
3: {MinSpan: 12, MaxSpan: 22},
|
2020-01-22 05:39:21 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for i := uint64(0); i < 6; i++ {
|
2020-03-05 05:35:14 +00:00
|
|
|
err := db.SaveEpochSpansMap(ctx, i, tsm.spanMap)
|
2020-01-22 05:39:21 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Save validator span map failed: %v", err)
|
|
|
|
}
|
|
|
|
}
|
2020-02-04 18:48:51 +00:00
|
|
|
|
|
|
|
// Wait for value to pass through cache buffers.
|
2020-01-22 05:39:21 +00:00
|
|
|
time.Sleep(time.Millisecond * 1000)
|
|
|
|
for i := uint64(0); i < 6; i++ {
|
2020-04-23 19:27:06 +00:00
|
|
|
sm, _, err := db.EpochSpansMap(ctx, i)
|
2020-01-22 05:39:21 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to get validator span map: %v", err)
|
|
|
|
}
|
2020-03-05 05:35:14 +00:00
|
|
|
if sm == nil || !reflect.DeepEqual(sm, tsm.spanMap) {
|
2020-01-22 05:39:21 +00:00
|
|
|
t.Fatalf("Get should return validator: %d span map: %v got: %v", i, tsm.spanMap, sm)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestValidatorSpanMap_SaveCachedSpansMaps(t *testing.T) {
|
2020-03-19 21:46:44 +00:00
|
|
|
app := cli.App{}
|
2020-01-22 05:39:21 +00:00
|
|
|
set := flag.NewFlagSet("test", 0)
|
2020-03-19 21:46:44 +00:00
|
|
|
db := setupDB(t, cli.NewContext(&app, set, nil))
|
2020-02-13 19:51:30 +00:00
|
|
|
ctx := context.Background()
|
2020-01-22 05:39:21 +00:00
|
|
|
|
|
|
|
for _, tt := range spanTests {
|
2020-03-05 05:35:14 +00:00
|
|
|
err := db.SaveEpochSpansMap(ctx, tt.epoch, tt.spanMap)
|
2020-01-22 05:39:21 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Save validator span map failed: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// wait for value to pass through cache buffers
|
|
|
|
time.Sleep(time.Millisecond * 10)
|
2020-03-08 17:56:43 +00:00
|
|
|
if err := db.SaveCachedSpansMaps(ctx); err != nil {
|
2020-01-22 05:39:21 +00:00
|
|
|
t.Errorf("Failed to save cached span maps to db: %v", err)
|
|
|
|
}
|
2020-05-22 19:44:48 +00:00
|
|
|
db.spanCache.Purge()
|
2020-01-22 05:39:21 +00:00
|
|
|
for _, tt := range spanTests {
|
2020-04-23 19:27:06 +00:00
|
|
|
sm, _, err := db.EpochSpansMap(ctx, tt.epoch)
|
2020-01-22 05:39:21 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to get validator span map: %v", err)
|
|
|
|
}
|
2020-03-05 05:35:14 +00:00
|
|
|
if !reflect.DeepEqual(sm, tt.spanMap) {
|
2020-01-22 05:39:21 +00:00
|
|
|
t.Fatalf("Get should return validator span map: %v got: %v", tt.spanMap, sm)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-04-21 16:50:04 +00:00
|
|
|
|
|
|
|
func TestStore_ReadWriteEpochsSpanByValidatorsIndices(t *testing.T) {
|
|
|
|
app := cli.App{}
|
|
|
|
set := flag.NewFlagSet("test", 0)
|
|
|
|
db := setupDB(t, cli.NewContext(&app, set, nil))
|
|
|
|
ctx := context.Background()
|
2020-04-22 15:53:09 +00:00
|
|
|
db.spanCacheEnabled = false
|
2020-04-21 16:50:04 +00:00
|
|
|
|
|
|
|
for _, tt := range spanTests {
|
|
|
|
err := db.SaveEpochSpansMap(ctx, tt.epoch, tt.spanMap)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Save validator span map failed: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
res, err := db.EpochsSpanByValidatorsIndices(ctx, []uint64{1, 2, 3}, 3)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if len(res) != len(spanTests) {
|
|
|
|
t.Errorf("Wanted map of %d elemets, received map of %d elements", len(spanTests), len(res))
|
|
|
|
}
|
|
|
|
for _, tt := range spanTests {
|
|
|
|
if !reflect.DeepEqual(res[tt.epoch], tt.spanMap) {
|
|
|
|
t.Errorf("Wanted span map to be equal to: %v , received span map: %v ", spanTests[0].spanMap, res[1])
|
|
|
|
}
|
|
|
|
}
|
2020-05-04 01:14:34 +00:00
|
|
|
db1 := setupDB(t, cli.NewContext(&app, set, nil))
|
|
|
|
if err := db1.SaveEpochsSpanByValidatorsIndices(ctx, res); err != nil {
|
2020-04-21 16:50:04 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-05-04 01:14:34 +00:00
|
|
|
res, err = db1.EpochsSpanByValidatorsIndices(ctx, []uint64{1, 2, 3}, 3)
|
2020-04-21 16:50:04 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if len(res) != len(spanTests) {
|
|
|
|
t.Errorf("Wanted map of %d elemets, received map of %d elements", len(spanTests), len(res))
|
|
|
|
}
|
|
|
|
for _, tt := range spanTests {
|
|
|
|
if !reflect.DeepEqual(res[tt.epoch], tt.spanMap) {
|
|
|
|
t.Errorf("Wanted span map to be equal to: %v , received span map: %v ", spanTests[0].spanMap, res[1])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|