prysm-pulse/slasher/db/kv/proposer_slashings_test.go
Ivan Martinez 2473680759
Add spans to Slasher DB functions (#4855)
* Add interface and move slashing types to /types package

* Add spans for all DB functions

* Fix packages

* Fix func call
2020-02-13 13:51:30 -06:00

170 lines
4.3 KiB
Go

package kv
import (
"context"
"flag"
"reflect"
"sort"
"testing"
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
"github.com/prysmaticlabs/prysm/slasher/db/types"
"github.com/urfave/cli"
)
func TestStore_ProposerSlashingNilBucket(t *testing.T) {
app := cli.NewApp()
set := flag.NewFlagSet("test", 0)
db := setupDB(t, cli.NewContext(app, set, nil))
defer teardownDB(t, db)
ctx := context.Background()
ps := &ethpb.ProposerSlashing{ProposerIndex: 1}
has, _, err := db.HasProposerSlashing(ctx, ps)
if err != nil {
t.Fatalf("HasProposerSlashing should not return error: %v", err)
}
if has {
t.Fatal("HasProposerSlashing should return false")
}
p, err := db.ProposalSlashingsByStatus(ctx, types.SlashingStatus(types.Active))
if err != nil {
t.Fatalf("Failed to get proposer 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_SaveProposerSlashing(t *testing.T) {
app := cli.NewApp()
set := flag.NewFlagSet("test", 0)
db := setupDB(t, cli.NewContext(app, set, nil))
defer teardownDB(t, db)
ctx := context.Background()
tests := []struct {
ss types.SlashingStatus
ps *ethpb.ProposerSlashing
}{
{
ss: types.Active,
ps: &ethpb.ProposerSlashing{ProposerIndex: 1},
},
{
ss: types.Included,
ps: &ethpb.ProposerSlashing{ProposerIndex: 2},
},
{
ss: types.Reverted,
ps: &ethpb.ProposerSlashing{ProposerIndex: 3},
},
}
for _, tt := range tests {
err := db.SaveProposerSlashing(ctx, tt.ss, tt.ps)
if err != nil {
t.Fatalf("Save proposer slashing failed: %v", err)
}
proposerSlashings, err := db.ProposalSlashingsByStatus(ctx, tt.ss)
if err != nil {
t.Fatalf("Failed to get proposer slashings: %v", err)
}
if proposerSlashings == nil || !reflect.DeepEqual(proposerSlashings[0], tt.ps) {
t.Fatalf("Proposer slashing: %v should be part of proposer slashings response: %v", tt.ps, proposerSlashings)
}
}
}
func TestStore_UpdateProposerSlashingStatus(t *testing.T) {
app := cli.NewApp()
set := flag.NewFlagSet("test", 0)
db := setupDB(t, cli.NewContext(app, set, nil))
defer teardownDB(t, db)
ctx := context.Background()
tests := []struct {
ss types.SlashingStatus
ps *ethpb.ProposerSlashing
}{
{
ss: types.Active,
ps: &ethpb.ProposerSlashing{ProposerIndex: 1},
},
{
ss: types.Active,
ps: &ethpb.ProposerSlashing{ProposerIndex: 2},
},
{
ss: types.Active,
ps: &ethpb.ProposerSlashing{ProposerIndex: 3},
},
}
for _, tt := range tests {
err := db.SaveProposerSlashing(ctx, tt.ss, tt.ps)
if err != nil {
t.Fatalf("Save proposer slashing failed: %v", err)
}
}
for _, tt := range tests {
has, st, err := db.HasProposerSlashing(ctx, tt.ps)
if err != nil {
t.Fatalf("Failed to get proposer slashing: %v", err)
}
if !has {
t.Fatalf("Failed to find proposer slashing: %v", tt.ps)
}
if st != tt.ss {
t.Fatalf("Failed to find proposer slashing with the correct status: %v", tt.ps)
}
err = db.SaveProposerSlashing(ctx, types.SlashingStatus(types.Included), tt.ps)
has, st, err = db.HasProposerSlashing(ctx, tt.ps)
if err != nil {
t.Fatalf("Failed to get proposer slashing: %v", err)
}
if !has {
t.Fatalf("Failed to find proposer slashing: %v", tt.ps)
}
if st != types.Included {
t.Fatalf("Failed to find proposer slashing with the correct status: %v", tt.ps)
}
}
}
func TestStore_SaveProposerSlashings(t *testing.T) {
app := cli.NewApp()
set := flag.NewFlagSet("test", 0)
db := setupDB(t, cli.NewContext(app, set, nil))
defer teardownDB(t, db)
ctx := context.Background()
ps := []*ethpb.ProposerSlashing{
{ProposerIndex: 1},
{ProposerIndex: 2},
{ProposerIndex: 3},
}
err := db.SaveProposerSlashings(ctx, types.Active, ps)
if err != nil {
t.Fatalf("Save proposer slashings failed: %v", err)
}
proposerSlashings, err := db.ProposalSlashingsByStatus(ctx, types.Active)
if err != nil {
t.Fatalf("Failed to get proposer slashings: %v", err)
}
sort.SliceStable(proposerSlashings, func(i, j int) bool {
return proposerSlashings[i].ProposerIndex < proposerSlashings[j].ProposerIndex
})
if proposerSlashings == nil || !reflect.DeepEqual(proposerSlashings, ps) {
t.Fatalf("Proposer slashing: %v should be part of proposer slashings response: %v", ps, proposerSlashings)
}
}