mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2024-12-27 21:57:16 +00:00
b030771174
* first version * cli context * fix service * starting change to ccache * ristretto cache * added test * test on evict * remove evict test * test onevict * comment for exported flag * update all span maps on load * fix setup db * span cache added to help flags * start save cache on exit * save cache to db before close * comment fix * fix flags * setup db new * nishant feedback * workspace cr * lint fix * fix calls * start db * fix test * Update slasher/db/db.go Co-Authored-By: Nishant Das <nishdas93@gmail.com> * add flag * nishant feedback * export Config * fix imports * fix imports * fix imports * Update slasher/service/service.go Co-Authored-By: terence tsao <terence@prysmaticlabs.com> * Update slasher/service/service.go Co-Authored-By: terence tsao <terence@prysmaticlabs.com> * Update slasher/service/service.go Co-Authored-By: terence tsao <terence@prysmaticlabs.com> * Update slasher/service/service.go Co-Authored-By: terence tsao <terence@prysmaticlabs.com> * remove mod print Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com> Co-authored-by: Raul Jordan <raul@prysmaticlabs.com> Co-authored-by: Nishant Das <nish1993@hotmail.com> Co-authored-by: terence tsao <terence@prysmaticlabs.com>
300 lines
9.8 KiB
Go
300 lines
9.8 KiB
Go
package rpc
|
|
|
|
import (
|
|
"context"
|
|
"flag"
|
|
"testing"
|
|
|
|
"github.com/gogo/protobuf/proto"
|
|
slashpb "github.com/prysmaticlabs/prysm/proto/slashing"
|
|
"github.com/prysmaticlabs/prysm/shared/params"
|
|
"github.com/prysmaticlabs/prysm/slasher/db"
|
|
"github.com/urfave/cli"
|
|
)
|
|
|
|
type spanMapTestStruct struct {
|
|
validatorIdx uint64
|
|
sourceEpoch uint64
|
|
targetEpoch uint64
|
|
slashingTargetEpoch uint64
|
|
resultSpanMap *slashpb.EpochSpanMap
|
|
}
|
|
|
|
var spanTestsMax []spanMapTestStruct
|
|
var spanTestsMin []spanMapTestStruct
|
|
|
|
func init() {
|
|
// Test data following example of a max span by https://github.com/protolambda
|
|
// from here: https://github.com/protolambda/eth2-surround/blob/master/README.md#min-max-surround
|
|
spanTestsMax = []spanMapTestStruct{
|
|
{
|
|
validatorIdx: 0,
|
|
sourceEpoch: 3,
|
|
targetEpoch: 6,
|
|
slashingTargetEpoch: 0,
|
|
resultSpanMap: &slashpb.EpochSpanMap{
|
|
EpochSpanMap: map[uint64]*slashpb.MinMaxEpochSpan{
|
|
4: {MinEpochSpan: 0, MaxEpochSpan: 2},
|
|
5: {MinEpochSpan: 0, MaxEpochSpan: 1},
|
|
},
|
|
},
|
|
},
|
|
{
|
|
validatorIdx: 0,
|
|
sourceEpoch: 8,
|
|
targetEpoch: 18,
|
|
slashingTargetEpoch: 0,
|
|
resultSpanMap: &slashpb.EpochSpanMap{
|
|
EpochSpanMap: map[uint64]*slashpb.MinMaxEpochSpan{
|
|
4: {MinEpochSpan: 0, MaxEpochSpan: 2},
|
|
5: {MinEpochSpan: 0, MaxEpochSpan: 1},
|
|
9: {MinEpochSpan: 0, MaxEpochSpan: 9},
|
|
10: {MinEpochSpan: 0, MaxEpochSpan: 8},
|
|
11: {MinEpochSpan: 0, MaxEpochSpan: 7},
|
|
12: {MinEpochSpan: 0, MaxEpochSpan: 6},
|
|
13: {MinEpochSpan: 0, MaxEpochSpan: 5},
|
|
14: {MinEpochSpan: 0, MaxEpochSpan: 4},
|
|
15: {MinEpochSpan: 0, MaxEpochSpan: 3},
|
|
16: {MinEpochSpan: 0, MaxEpochSpan: 2},
|
|
17: {MinEpochSpan: 0, MaxEpochSpan: 1},
|
|
},
|
|
},
|
|
},
|
|
{
|
|
validatorIdx: 0,
|
|
sourceEpoch: 4,
|
|
targetEpoch: 12,
|
|
slashingTargetEpoch: 0,
|
|
resultSpanMap: &slashpb.EpochSpanMap{
|
|
EpochSpanMap: map[uint64]*slashpb.MinMaxEpochSpan{
|
|
4: {MinEpochSpan: 0, MaxEpochSpan: 2},
|
|
5: {MinEpochSpan: 0, MaxEpochSpan: 7},
|
|
6: {MinEpochSpan: 0, MaxEpochSpan: 6},
|
|
7: {MinEpochSpan: 0, MaxEpochSpan: 5},
|
|
8: {MinEpochSpan: 0, MaxEpochSpan: 4},
|
|
9: {MinEpochSpan: 0, MaxEpochSpan: 9},
|
|
10: {MinEpochSpan: 0, MaxEpochSpan: 8},
|
|
11: {MinEpochSpan: 0, MaxEpochSpan: 7},
|
|
12: {MinEpochSpan: 0, MaxEpochSpan: 6},
|
|
13: {MinEpochSpan: 0, MaxEpochSpan: 5},
|
|
14: {MinEpochSpan: 0, MaxEpochSpan: 4},
|
|
15: {MinEpochSpan: 0, MaxEpochSpan: 3},
|
|
16: {MinEpochSpan: 0, MaxEpochSpan: 2},
|
|
17: {MinEpochSpan: 0, MaxEpochSpan: 1},
|
|
},
|
|
},
|
|
},
|
|
{
|
|
validatorIdx: 0,
|
|
sourceEpoch: 10,
|
|
targetEpoch: 15,
|
|
slashingTargetEpoch: 18,
|
|
resultSpanMap: &slashpb.EpochSpanMap{
|
|
EpochSpanMap: map[uint64]*slashpb.MinMaxEpochSpan{
|
|
4: {MinEpochSpan: 0, MaxEpochSpan: 2},
|
|
5: {MinEpochSpan: 0, MaxEpochSpan: 7},
|
|
6: {MinEpochSpan: 0, MaxEpochSpan: 6},
|
|
7: {MinEpochSpan: 0, MaxEpochSpan: 5},
|
|
8: {MinEpochSpan: 0, MaxEpochSpan: 4},
|
|
9: {MinEpochSpan: 0, MaxEpochSpan: 9},
|
|
10: {MinEpochSpan: 0, MaxEpochSpan: 8},
|
|
11: {MinEpochSpan: 0, MaxEpochSpan: 7},
|
|
12: {MinEpochSpan: 0, MaxEpochSpan: 6},
|
|
13: {MinEpochSpan: 0, MaxEpochSpan: 5},
|
|
14: {MinEpochSpan: 0, MaxEpochSpan: 4},
|
|
15: {MinEpochSpan: 0, MaxEpochSpan: 3},
|
|
16: {MinEpochSpan: 0, MaxEpochSpan: 2},
|
|
17: {MinEpochSpan: 0, MaxEpochSpan: 1},
|
|
},
|
|
},
|
|
},
|
|
}
|
|
|
|
spanTestsMin = []spanMapTestStruct{
|
|
{
|
|
validatorIdx: 0,
|
|
sourceEpoch: 4,
|
|
targetEpoch: 6,
|
|
slashingTargetEpoch: 0,
|
|
resultSpanMap: &slashpb.EpochSpanMap{
|
|
EpochSpanMap: map[uint64]*slashpb.MinMaxEpochSpan{
|
|
1: {MinEpochSpan: 5, MaxEpochSpan: 0},
|
|
2: {MinEpochSpan: 4, MaxEpochSpan: 0},
|
|
3: {MinEpochSpan: 3, MaxEpochSpan: 0},
|
|
},
|
|
},
|
|
},
|
|
{
|
|
validatorIdx: 0,
|
|
sourceEpoch: 13,
|
|
targetEpoch: 18,
|
|
slashingTargetEpoch: 0,
|
|
resultSpanMap: &slashpb.EpochSpanMap{
|
|
EpochSpanMap: map[uint64]*slashpb.MinMaxEpochSpan{
|
|
1: {MinEpochSpan: 5, MaxEpochSpan: 0},
|
|
2: {MinEpochSpan: 4, MaxEpochSpan: 0},
|
|
3: {MinEpochSpan: 3, MaxEpochSpan: 0},
|
|
4: {MinEpochSpan: 14, MaxEpochSpan: 0},
|
|
5: {MinEpochSpan: 13, MaxEpochSpan: 0},
|
|
6: {MinEpochSpan: 12, MaxEpochSpan: 0},
|
|
7: {MinEpochSpan: 11, MaxEpochSpan: 0},
|
|
8: {MinEpochSpan: 10, MaxEpochSpan: 0},
|
|
9: {MinEpochSpan: 9, MaxEpochSpan: 0},
|
|
10: {MinEpochSpan: 8, MaxEpochSpan: 0},
|
|
11: {MinEpochSpan: 7, MaxEpochSpan: 0},
|
|
12: {MinEpochSpan: 6, MaxEpochSpan: 0},
|
|
},
|
|
},
|
|
},
|
|
{
|
|
validatorIdx: 0,
|
|
sourceEpoch: 11,
|
|
targetEpoch: 15,
|
|
slashingTargetEpoch: 0,
|
|
resultSpanMap: &slashpb.EpochSpanMap{
|
|
EpochSpanMap: map[uint64]*slashpb.MinMaxEpochSpan{
|
|
1: {MinEpochSpan: 5, MaxEpochSpan: 0},
|
|
2: {MinEpochSpan: 4, MaxEpochSpan: 0},
|
|
3: {MinEpochSpan: 3, MaxEpochSpan: 0},
|
|
4: {MinEpochSpan: 11, MaxEpochSpan: 0},
|
|
5: {MinEpochSpan: 10, MaxEpochSpan: 0},
|
|
6: {MinEpochSpan: 9, MaxEpochSpan: 0},
|
|
7: {MinEpochSpan: 8, MaxEpochSpan: 0},
|
|
8: {MinEpochSpan: 7, MaxEpochSpan: 0},
|
|
9: {MinEpochSpan: 6, MaxEpochSpan: 0},
|
|
10: {MinEpochSpan: 5, MaxEpochSpan: 0},
|
|
11: {MinEpochSpan: 7, MaxEpochSpan: 0},
|
|
12: {MinEpochSpan: 6, MaxEpochSpan: 0},
|
|
},
|
|
},
|
|
},
|
|
{
|
|
validatorIdx: 0,
|
|
sourceEpoch: 10,
|
|
targetEpoch: 20,
|
|
slashingTargetEpoch: 15,
|
|
resultSpanMap: &slashpb.EpochSpanMap{
|
|
EpochSpanMap: map[uint64]*slashpb.MinMaxEpochSpan{
|
|
1: {MinEpochSpan: 5, MaxEpochSpan: 0},
|
|
2: {MinEpochSpan: 4, MaxEpochSpan: 0},
|
|
3: {MinEpochSpan: 3, MaxEpochSpan: 0},
|
|
4: {MinEpochSpan: 11, MaxEpochSpan: 0},
|
|
5: {MinEpochSpan: 10, MaxEpochSpan: 0},
|
|
6: {MinEpochSpan: 9, MaxEpochSpan: 0},
|
|
7: {MinEpochSpan: 8, MaxEpochSpan: 0},
|
|
8: {MinEpochSpan: 7, MaxEpochSpan: 0},
|
|
9: {MinEpochSpan: 6, MaxEpochSpan: 0},
|
|
10: {MinEpochSpan: 5, MaxEpochSpan: 0},
|
|
11: {MinEpochSpan: 7, MaxEpochSpan: 0},
|
|
12: {MinEpochSpan: 6, MaxEpochSpan: 0},
|
|
},
|
|
},
|
|
},
|
|
}
|
|
}
|
|
|
|
func TestServer_UpdateMaxEpochSpan(t *testing.T) {
|
|
app := cli.NewApp()
|
|
set := flag.NewFlagSet("test", 0)
|
|
c := cli.NewContext(app, set, nil)
|
|
dbs := db.SetupSlasherDB(t, c)
|
|
defer db.TeardownSlasherDB(t, dbs)
|
|
ctx := context.Background()
|
|
slasherServer := &Server{
|
|
SlasherDB: dbs,
|
|
}
|
|
for _, tt := range spanTestsMax {
|
|
spanMap, err := slasherServer.SlasherDB.ValidatorSpansMap(tt.validatorIdx)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
st, spanMap, err := slasherServer.DetectAndUpdateMaxEpochSpan(ctx, tt.sourceEpoch, tt.targetEpoch, tt.validatorIdx, spanMap)
|
|
if err != nil {
|
|
t.Fatalf("Failed to update span: %v", err)
|
|
}
|
|
if err := slasherServer.SlasherDB.SaveValidatorSpansMap(tt.validatorIdx, spanMap); err != nil {
|
|
t.Fatalf("Couldnt save span map for validator id: %d", tt.validatorIdx)
|
|
}
|
|
if st != tt.slashingTargetEpoch {
|
|
t.Fatalf("Expected slashing target: %d got: %d", tt.slashingTargetEpoch, st)
|
|
}
|
|
sm, err := slasherServer.SlasherDB.ValidatorSpansMap(tt.validatorIdx)
|
|
if err != nil {
|
|
t.Fatalf("Failed to retrieve span: %v", err)
|
|
}
|
|
if sm == nil || !proto.Equal(sm, tt.resultSpanMap) {
|
|
t.Fatalf("Get should return validator span map: %v got: %v", tt.resultSpanMap, sm)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestServer_UpdateMinEpochSpan(t *testing.T) {
|
|
app := cli.NewApp()
|
|
set := flag.NewFlagSet("test", 0)
|
|
c := cli.NewContext(app, set, nil)
|
|
dbs := db.SetupSlasherDB(t, c)
|
|
defer db.TeardownSlasherDB(t, dbs)
|
|
ctx := context.Background()
|
|
slasherServer := &Server{
|
|
SlasherDB: dbs,
|
|
}
|
|
for _, tt := range spanTestsMin {
|
|
spanMap, err := slasherServer.SlasherDB.ValidatorSpansMap(tt.validatorIdx)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
st, spanMap, err := slasherServer.DetectAndUpdateMinEpochSpan(ctx, tt.sourceEpoch, tt.targetEpoch, tt.validatorIdx, spanMap)
|
|
if err != nil {
|
|
t.Fatalf("Failed to update span: %v", err)
|
|
}
|
|
if err := slasherServer.SlasherDB.SaveValidatorSpansMap(tt.validatorIdx, spanMap); err != nil {
|
|
t.Fatalf("Couldnt save span map for validator id: %d", tt.validatorIdx)
|
|
}
|
|
if st != tt.slashingTargetEpoch {
|
|
t.Fatalf("Expected slashing target: %d got: %d", tt.slashingTargetEpoch, st)
|
|
}
|
|
sm, err := slasherServer.SlasherDB.ValidatorSpansMap(tt.validatorIdx)
|
|
if err != nil {
|
|
t.Fatalf("Failed to retrieve span: %v", err)
|
|
}
|
|
if sm == nil || !proto.Equal(sm, tt.resultSpanMap) {
|
|
t.Fatalf("Get should return validator span map: %v got: %v", tt.resultSpanMap, sm)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestServer_FailToUpdate(t *testing.T) {
|
|
app := cli.NewApp()
|
|
set := flag.NewFlagSet("test", 0)
|
|
c := cli.NewContext(app, set, nil)
|
|
dbs := db.SetupSlasherDB(t, c)
|
|
defer db.TeardownSlasherDB(t, dbs)
|
|
ctx := context.Background()
|
|
slasherServer := &Server{
|
|
SlasherDB: dbs,
|
|
}
|
|
spanTestsFail := spanMapTestStruct{
|
|
validatorIdx: 0,
|
|
sourceEpoch: 0,
|
|
slashingTargetEpoch: 0,
|
|
targetEpoch: params.BeaconConfig().WeakSubjectivityPeriod + 1,
|
|
resultSpanMap: &slashpb.EpochSpanMap{
|
|
EpochSpanMap: map[uint64]*slashpb.MinMaxEpochSpan{
|
|
4: {MinEpochSpan: 0, MaxEpochSpan: 2},
|
|
5: {MinEpochSpan: 0, MaxEpochSpan: 1},
|
|
},
|
|
},
|
|
}
|
|
spanMap, err := slasherServer.SlasherDB.ValidatorSpansMap(spanTestsFail.validatorIdx)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, _, err := slasherServer.DetectAndUpdateMinEpochSpan(ctx, spanTestsFail.sourceEpoch, spanTestsFail.targetEpoch, spanTestsFail.validatorIdx, spanMap); err == nil {
|
|
t.Fatalf("Update should not support diff greater then weak subjectivity period: %v ", params.BeaconConfig().WeakSubjectivityPeriod)
|
|
}
|
|
if _, _, err := slasherServer.DetectAndUpdateMaxEpochSpan(ctx, spanTestsFail.sourceEpoch, spanTestsFail.targetEpoch, spanTestsFail.validatorIdx, spanMap); err == nil {
|
|
t.Fatalf("Update should not support diff greater then weak subjectivity period: %v ", params.BeaconConfig().WeakSubjectivityPeriod)
|
|
}
|
|
|
|
}
|