prysm-pulse/slasher/db/min_max_span_test.go
shayzluf 607f086de9 Surround detection (#3967)
* min max span update logic

* add comment to exported method

* Update slasher/rpc/update_min_max_span.go

Co-Authored-By: Raul Jordan <raul@prysmaticlabs.com>

* Update slasher/rpc/update_min_max_span.go

Co-Authored-By: Raul Jordan <raul@prysmaticlabs.com>

* Update slasher/rpc/update_min_max_span.go

Co-Authored-By: Raul Jordan <raul@prysmaticlabs.com>

* Update slasher/rpc/update_min_max_span_test.go

Co-Authored-By: Raul Jordan <raul@prysmaticlabs.com>

* Update slasher/rpc/update_min_max_span.go

Co-Authored-By: terence tsao <terence@prysmaticlabs.com>

* Update slasher/rpc/update_min_max_span.go

Co-Authored-By: Raul Jordan <raul@prysmaticlabs.com>

* weak subjectivity error

* add context

* SlasherDb change to SlasherDB

* gaz

* raul feedback

* fix old problem

* gofmt goimports

* gaz

* import fix

* change order

* min max span detection

* added benchmark

* max diff without error

* Update slasher/rpc/detect_update_min_max_span_bench_test.go

Co-Authored-By: Raul Jordan <raul@prysmaticlabs.com>

* Update slasher/db/indexed_attestations.go

Co-Authored-By: Raul Jordan <raul@prysmaticlabs.com>

* Update slasher/rpc/detect_update_min_max_span_bench_test.go

Co-Authored-By: Raul Jordan <raul@prysmaticlabs.com>

* Update slasher/rpc/detect_update_min_max_span_test.go

Co-Authored-By: Raul Jordan <raul@prysmaticlabs.com>

* Update slasher/rpc/detect_update_min_max_span.go

Co-Authored-By: Raul Jordan <raul@prysmaticlabs.com>

* Update slasher/rpc/detect_update_min_max_span_test.go

Co-Authored-By: Raul Jordan <raul@prysmaticlabs.com>

* Update slasher/rpc/detect_update_min_max_span_bench_test.go

Co-Authored-By: Raul Jordan <raul@prysmaticlabs.com>

* raul feedback, benchmark fix

* raul feedback

* gaz

* fix merge

* bench fix

* another bench fix

* comments

* changed names of functions and proto

* name change fix

* name change fix

* fix test

* clarification comment

* change to interface

* Update proto/eth/v1alpha1/slasher.proto

Co-Authored-By: Ivan Martinez <ivanthegreatdev@gmail.com>

* Update slasher/rpc/detect_update_min_max_span.go

Co-Authored-By: Raul Jordan <raul@prysmaticlabs.com>

* Update slasher/rpc/detect_update_min_max_span.go

Co-Authored-By: Raul Jordan <raul@prysmaticlabs.com>

* change order to reduce confusion

* Update proto/eth/v1alpha1/slasher.proto

Co-Authored-By: terence tsao <terence@prysmaticlabs.com>

* Update slasher/rpc/detect_update_min_max_span.go

Co-Authored-By: terence tsao <terence@prysmaticlabs.com>

* Update slasher/rpc/detect_update_min_max_span.go

Co-Authored-By: terence tsao <terence@prysmaticlabs.com>

* Update slasher/rpc/detect_update_min_max_span.go

Co-Authored-By: terence tsao <terence@prysmaticlabs.com>

* Apply suggestions from code review

Co-Authored-By: terence tsao <terence@prysmaticlabs.com>

* Update slasher/rpc/detect_update_min_max_span.go

* Fix some comments

* terence feedback

* preston feedback

* fix test

* fix comments
2019-11-18 13:49:39 -06:00

119 lines
3.0 KiB
Go

package db
import (
"reflect"
"testing"
"github.com/gogo/protobuf/proto"
ethpb "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1"
)
type spanMapTestStruct struct {
validatorIdx uint64
spanMap *ethpb.EpochSpanMap
}
var spanTests []spanMapTestStruct
func init() {
spanTests = []spanMapTestStruct{
{
validatorIdx: 1,
spanMap: &ethpb.EpochSpanMap{
EpochSpanMap: map[uint64]*ethpb.MinMaxEpochSpan{
1: {MinEpochSpan: 10, MaxEpochSpan: 20},
2: {MinEpochSpan: 11, MaxEpochSpan: 21},
3: {MinEpochSpan: 12, MaxEpochSpan: 22},
},
},
},
{
validatorIdx: 2,
spanMap: &ethpb.EpochSpanMap{
EpochSpanMap: map[uint64]*ethpb.MinMaxEpochSpan{
1: {MinEpochSpan: 10, MaxEpochSpan: 20},
2: {MinEpochSpan: 11, MaxEpochSpan: 21},
3: {MinEpochSpan: 12, MaxEpochSpan: 22},
},
},
},
{
validatorIdx: 3,
spanMap: &ethpb.EpochSpanMap{
EpochSpanMap: map[uint64]*ethpb.MinMaxEpochSpan{
1: {MinEpochSpan: 10, MaxEpochSpan: 20},
2: {MinEpochSpan: 11, MaxEpochSpan: 21},
3: {MinEpochSpan: 12, MaxEpochSpan: 22},
},
},
},
}
}
func TestValidatorSpanMap_NilDB(t *testing.T) {
db := SetupSlasherDB(t)
defer TeardownSlasherDB(t, db)
validatorIdx := uint64(1)
vsm, err := db.ValidatorSpansMap(validatorIdx)
if err != nil {
t.Fatalf("Nil ValidatorSpansMap should not return error: %v", err)
}
if !reflect.DeepEqual(vsm.EpochSpanMap, map[uint64]*ethpb.MinMaxEpochSpan{}) {
t.Fatal("ValidatorSpansMap should return nil")
}
}
func TestValidatorSpanMap_Save(t *testing.T) {
db := SetupSlasherDB(t)
defer TeardownSlasherDB(t, db)
for _, tt := range spanTests {
err := db.SaveValidatorSpansMap(tt.validatorIdx, tt.spanMap)
if err != nil {
t.Fatalf("Save validator span map failed: %v", err)
}
sm, err := db.ValidatorSpansMap(tt.validatorIdx)
if err != nil {
t.Fatalf("Failed to get validator span map: %v", err)
}
if sm == nil || !proto.Equal(sm, tt.spanMap) {
t.Fatalf("Get should return validator span map: %v got: %v", tt.spanMap, sm)
}
}
}
func TestValidatorSpanMap_Delete(t *testing.T) {
db := SetupSlasherDB(t)
defer TeardownSlasherDB(t, db)
for _, tt := range spanTests {
err := db.SaveValidatorSpansMap(tt.validatorIdx, tt.spanMap)
if err != nil {
t.Fatalf("Save validator span map failed: %v", err)
}
}
for _, tt := range spanTests {
sm, err := db.ValidatorSpansMap(tt.validatorIdx)
if err != nil {
t.Fatalf("Failed to get validator span map: %v", err)
}
if sm == nil || !proto.Equal(sm, tt.spanMap) {
t.Fatalf("Get should return validator span map: %v got: %v", tt.spanMap, sm)
}
err = db.DeleteValidatorSpanMap(tt.validatorIdx)
if err != nil {
t.Fatalf("Delete validator span map error: %v", err)
}
sm, err = db.ValidatorSpansMap(tt.validatorIdx)
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(sm.EpochSpanMap, map[uint64]*ethpb.MinMaxEpochSpan{}) {
t.Errorf("Expected validator span map to be deleted, received: %v", sm)
}
}
}