prysm-pulse/slasher/db/indexed_attestations_test.go
shayzluf b030771174 Slasher span cache (#4388)
* 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>
2020-01-21 23:39:21 -06:00

210 lines
5.6 KiB
Go

package db
import (
"flag"
"reflect"
"testing"
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
"github.com/urfave/cli"
)
type testStruct struct {
iA *ethpb.IndexedAttestation
}
var tests []testStruct
func init() {
tests = []testStruct{
{
iA: &ethpb.IndexedAttestation{Signature: []byte("let me in"), AttestingIndices: []uint64{0}, Data: &ethpb.AttestationData{
Source: &ethpb.Checkpoint{Epoch: 0},
Target: &ethpb.Checkpoint{Epoch: 1},
}},
},
{
iA: &ethpb.IndexedAttestation{Signature: []byte("let me in 2nd"), AttestingIndices: []uint64{1, 2}, Data: &ethpb.AttestationData{
Source: &ethpb.Checkpoint{Epoch: 0},
Target: &ethpb.Checkpoint{Epoch: 2},
}},
},
{
iA: &ethpb.IndexedAttestation{Signature: []byte("let me in 3rd"), AttestingIndices: []uint64{0}, Data: &ethpb.AttestationData{
Source: &ethpb.Checkpoint{Epoch: 1},
Target: &ethpb.Checkpoint{Epoch: 2},
}},
},
}
}
func TestNilDBHistoryIdxAtt(t *testing.T) {
app := cli.NewApp()
set := flag.NewFlagSet("test", 0)
ctx := cli.NewContext(app, set, nil)
db := SetupSlasherDB(t, ctx)
defer TeardownSlasherDB(t, db)
epoch := uint64(1)
validatorID := uint64(1)
hasIdxAtt := db.HasIndexedAttestation(epoch, validatorID)
if hasIdxAtt {
t.Fatal("HasIndexedAttestation should return false")
}
idxAtt, err := db.IndexedAttestation(epoch, validatorID)
if err != nil {
t.Fatalf("failed to get indexed attestation: %v", err)
}
if idxAtt != nil {
t.Fatalf("get should return nil for a non existent key")
}
}
func TestSaveIdxAtt(t *testing.T) {
app := cli.NewApp()
set := flag.NewFlagSet("test", 0)
ctx := cli.NewContext(app, set, nil)
db := SetupSlasherDB(t, ctx)
defer TeardownSlasherDB(t, db)
for _, tt := range tests {
err := db.SaveIndexedAttestation(tt.iA)
if err != nil {
t.Fatalf("save indexed attestation failed: %v", err)
}
iAarray, err := db.IndexedAttestation(tt.iA.Data.Target.Epoch, tt.iA.AttestingIndices[0])
if err != nil {
t.Fatalf("failed to get indexed attestation: %v", err)
}
if iAarray == nil || !reflect.DeepEqual(iAarray[0], tt.iA) {
t.Fatalf("get should return indexed attestation: %v", iAarray)
}
}
}
func TestDeleteHistoryIdxAtt(t *testing.T) {
app := cli.NewApp()
set := flag.NewFlagSet("test", 0)
ctx := cli.NewContext(app, set, nil)
db := SetupSlasherDB(t, ctx)
defer TeardownSlasherDB(t, db)
for _, tt := range tests {
err := db.SaveIndexedAttestation(tt.iA)
if err != nil {
t.Fatalf("save indexed attestation failed: %v", err)
}
}
for _, tt := range tests {
iAarray, err := db.IndexedAttestation(tt.iA.Data.Target.Epoch, tt.iA.AttestingIndices[0])
if err != nil {
t.Fatalf("failed to get index attestation: %v", err)
}
if iAarray == nil || !reflect.DeepEqual(iAarray[0], tt.iA) {
t.Fatalf("get should return indexed attestation: %v", iAarray)
}
err = db.DeleteIndexedAttestation(tt.iA)
if err != nil {
t.Fatalf("delete index attestation failed: %v", err)
}
iAarray, err = db.IndexedAttestation(tt.iA.Data.Target.Epoch, tt.iA.AttestingIndices[0])
hasA := db.HasIndexedAttestation(tt.iA.Data.Target.Epoch, tt.iA.AttestingIndices[0])
if err != nil {
t.Fatal(err)
}
if len(iAarray) != 0 {
t.Errorf("Expected index attestation to have been deleted, received: %v", iAarray)
}
if hasA {
t.Errorf("Expected indexed attestation indexes list to be deleted, received: %v", hasA)
}
}
}
func TestHasIdxAtt(t *testing.T) {
app := cli.NewApp()
set := flag.NewFlagSet("test", 0)
ctx := cli.NewContext(app, set, nil)
db := SetupSlasherDB(t, ctx)
defer TeardownSlasherDB(t, db)
for _, tt := range tests {
found := db.HasIndexedAttestation(tt.iA.Data.Target.Epoch, tt.iA.AttestingIndices[0])
if found {
t.Fatal("has indexed attestation should return false for indexed attestations that are not in db")
}
err := db.SaveIndexedAttestation(tt.iA)
if err != nil {
t.Fatalf("save indexed attestation failed: %v", err)
}
}
for _, tt := range tests {
found := db.HasIndexedAttestation(tt.iA.Data.Target.Epoch, tt.iA.AttestingIndices[0])
if !found {
t.Fatal("has indexed attestation should return true")
}
}
}
func TestPruneHistoryIdxAtt(t *testing.T) {
app := cli.NewApp()
set := flag.NewFlagSet("test", 0)
ctx := cli.NewContext(app, set, nil)
db := SetupSlasherDB(t, ctx)
defer TeardownSlasherDB(t, db)
for _, tt := range tests {
err := db.SaveIndexedAttestation(tt.iA)
if err != nil {
t.Fatalf("save indexed attestation failed: %v", err)
}
iAarray, err := db.IndexedAttestation(tt.iA.Data.Target.Epoch, tt.iA.AttestingIndices[0])
if err != nil {
t.Fatalf("failed to get indexed attestation: %v", err)
}
if iAarray == nil || !reflect.DeepEqual(iAarray[0], tt.iA) {
t.Fatalf("get should return bh: %v", iAarray)
}
}
currentEpoch := uint64(3)
historyToKeep := uint64(1)
err := db.pruneAttHistory(currentEpoch, historyToKeep)
if err != nil {
t.Fatalf("failed to prune: %v", err)
}
for _, tt := range tests {
iAarray, err := db.IndexedAttestation(tt.iA.Data.Target.Epoch, tt.iA.AttestingIndices[0])
if err != nil {
t.Fatalf("failed to get indexed attestation: %v", err)
}
hasIa := db.HasIndexedAttestation(tt.iA.Data.Target.Epoch, tt.iA.AttestingIndices[0])
if tt.iA.Data.Source.Epoch > currentEpoch-historyToKeep {
if iAarray == nil || !reflect.DeepEqual(iAarray[0], tt.iA) {
t.Fatalf("get should return indexed attestation: %v", iAarray)
}
if !hasIa {
t.Fatalf("get should have indexed attestation for epoch: %v", iAarray)
}
} else {
if iAarray != nil || hasIa {
t.Fatalf("indexed attestation should have been pruned: %v has attestation: %v", iAarray, hasIa)
}
}
}
}