prysm-pulse/slasher/db/attester_slashings_test.go
shayzluf ade61717a4
Slasher data update from archive (#4563)
* 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

* data update from archive node

* gaz

* slashing detection on old attestations

* un-export

* rename

* 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

* fix fail to start beacon client

* mock beacon service

* fix imports

* gaz

* goimports

* add clear db flag

* print finalized epoch

* better msg

* Update slasher/db/attester_slashings.go

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

* raul feedback

* raul feedback

* raul feedback

* raul feedback

* raul feedback

* add detection in runtime

* fix tests

* raul feedbacks

* raul feedback

* raul feedback

* goimports

* Update beacon-chain/blockchain/process_attestation_helpers.go

* Update beacon-chain/blockchain/receive_block.go

* Update beacon-chain/core/blocks/block_operations_test.go

* Update beacon-chain/core/blocks/block_operations.go

* Update beacon-chain/core/epoch/epoch_processing.go

* Update beacon-chain/sync/validate_aggregate_proof_test.go

* Update shared/testutil/block.go

* Update slasher/service/data_update.go

* Update tools/blocktree/main.go

* Update slasher/service/service.go

* Update beacon-chain/core/epoch/precompute/attestation_test.go

* Update beacon-chain/core/helpers/committee_test.go

* Update beacon-chain/core/state/transition_test.go

* Update beacon-chain/rpc/aggregator/server_test.go

* Update beacon-chain/sync/validate_aggregate_proof.go

* Update beacon-chain/rpc/validator/proposer_test.go

* Update beacon-chain/blockchain/forkchoice/process_attestation.go

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

* Update slasher/db/indexed_attestations.go

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

* Update slasher/service/data_update.go

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

* terence feedback

* terence feedback

* goimports

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-29 07:14:51 +05:30

191 lines
5.3 KiB
Go

package db
import (
"flag"
"reflect"
"sort"
"testing"
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
"github.com/urfave/cli"
)
func TestStore_AttesterSlashingNilBucket(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)
as := &ethpb.AttesterSlashing{Attestation_1: &ethpb.IndexedAttestation{Signature: []byte("hello")}}
has, _, err := db.HasAttesterSlashing(as)
if err != nil {
t.Fatalf("HasAttesterSlashing should not return error: %v", err)
}
if has {
t.Fatal("HasAttesterSlashing should return false")
}
p, err := db.AttesterSlashings(SlashingStatus(Active))
if err != nil {
t.Fatalf("Failed to get attester slashing: %v", err)
}
if p == nil || len(p) != 0 {
t.Fatalf("Get should return empty attester slashing array for a non existent key")
}
}
func TestStore_SaveAttesterSlashing(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)
tests := []struct {
ss SlashingStatus
as *ethpb.AttesterSlashing
}{
{
ss: Active,
as: &ethpb.AttesterSlashing{Attestation_1: &ethpb.IndexedAttestation{Signature: []byte("hello")}},
},
{
ss: Included,
as: &ethpb.AttesterSlashing{Attestation_1: &ethpb.IndexedAttestation{Signature: []byte("hello2")}},
},
{
ss: Reverted,
as: &ethpb.AttesterSlashing{Attestation_1: &ethpb.IndexedAttestation{Signature: []byte("hello3")}},
},
}
for _, tt := range tests {
err := db.SaveAttesterSlashing(tt.ss, tt.as)
if err != nil {
t.Fatalf("save attester slashing failed: %v", err)
}
attesterSlashings, err := db.AttesterSlashings(tt.ss)
if err != nil {
t.Fatalf("failed to get attester slashings: %v", err)
}
if attesterSlashings == nil || !reflect.DeepEqual(attesterSlashings[0], tt.as) {
t.Fatalf("attester slashing: %v should be part of attester slashings response: %v", tt.as, attesterSlashings)
}
}
}
func TestStore_SaveAttesterSlashings(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)
as := []*ethpb.AttesterSlashing{
{Attestation_1: &ethpb.IndexedAttestation{Signature: []byte("1")}},
{Attestation_1: &ethpb.IndexedAttestation{Signature: []byte("2")}},
{Attestation_1: &ethpb.IndexedAttestation{Signature: []byte("3")}},
}
err := db.SaveAttesterSlashings(Active, as)
if err != nil {
t.Fatalf("save attester slashing failed: %v", err)
}
attesterSlashings, err := db.AttesterSlashings(Active)
if err != nil {
t.Fatalf("failed to get attester slashings: %v", err)
}
sort.SliceStable(attesterSlashings, func(i, j int) bool {
return attesterSlashings[i].Attestation_1.Signature[0] < attesterSlashings[j].Attestation_1.Signature[0]
})
if attesterSlashings == nil || !reflect.DeepEqual(attesterSlashings, as) {
t.Fatalf("Attester slashing: %v should be part of attester slashings response: %v", as, attesterSlashings)
}
}
func TestStore_UpdateAttesterSlashingStatus(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)
tests := []struct {
ss SlashingStatus
as *ethpb.AttesterSlashing
}{
{
ss: Active,
as: &ethpb.AttesterSlashing{Attestation_1: &ethpb.IndexedAttestation{Signature: []byte("hello")}},
},
{
ss: Active,
as: &ethpb.AttesterSlashing{Attestation_1: &ethpb.IndexedAttestation{Signature: []byte("hello2")}},
},
{
ss: Active,
as: &ethpb.AttesterSlashing{Attestation_1: &ethpb.IndexedAttestation{Signature: []byte("hello3")}},
},
}
for _, tt := range tests {
err := db.SaveAttesterSlashing(tt.ss, tt.as)
if err != nil {
t.Fatalf("save attester slashing failed: %v", err)
}
}
for _, tt := range tests {
has, st, err := db.HasAttesterSlashing(tt.as)
if err != nil {
t.Fatalf("Failed to get attester slashing: %v", err)
}
if !has {
t.Fatalf("Failed to find attester slashing: %v", tt.as)
}
if st != tt.ss {
t.Fatalf("Failed to find attester slashing with the correct status: %v", tt.as)
}
err = db.SaveAttesterSlashing(SlashingStatus(Included), tt.as)
has, st, err = db.HasAttesterSlashing(tt.as)
if err != nil {
t.Fatalf("Failed to get attester slashing: %v", err)
}
if !has {
t.Fatalf("Failed to find attester slashing: %v", tt.as)
}
if st != Included {
t.Fatalf("Failed to find attester slashing with the correct status: %v", tt.as)
}
}
}
func TestStore_LatestEpochDetected(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)
e, err := db.GetLatestEpochDetected()
if err != nil {
t.Fatalf("Get latest epoch detected failed: %v", err)
}
if e != 0 {
t.Fatalf("Latest epoch detected should have been 0 before setting got: %d", e)
}
epoch := uint64(1)
err = db.SetLatestEpochDetected(epoch)
if err != nil {
t.Fatalf("Set latest epoch detected failed: %v", err)
}
e, err = db.GetLatestEpochDetected()
if err != nil {
t.Fatalf("Get latest epoch detected failed: %v", err)
}
if e != epoch {
t.Fatalf("Latest epoch detected should have been: %d got: %d", epoch, e)
}
}