prysm-pulse/slasher/db/proposer_slashings_test.go
shayzluf 0b35743d2c
Attester proposer slashing store (#4315)
* Merge branch 'master' of github.com:prysmaticlabs/prysm into update_validators

# Conflicts:
#	slasher/flags/flags.go
#	slasher/main.go
#	slasher/service/data_update.go
#	slasher/service/service.go
#	slasher/service/service_test.go

* proposal and attester store

* day to status

* comment change

* one bucket

* Merge branch 'master' of github.com:prysmaticlabs/prysm into attester_proposer_slashing_store
# Please enter a commit message to explain why this merge is necessary,
# especially if it merges an updated upstream into a topic branch.
#
# Lines starting with '#' will be ignored, and an empty message aborts
# the commit.

added comments

* comment

* typo fix

* raul review fix

* raul review fix full

* nishant feedback

* test fix

* fix tests and remove update gofmt goimports

* remove blank line in imports

* nishant fixes

* comment and fir delete proposer slashings

* avoid marshal twice

* remove space

* Update slasher/db/attester_slashings.go

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

* terence feedback

Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com>
Co-authored-by: terence tsao <terence@prysmaticlabs.com>
2020-01-14 07:43:25 +05:30

125 lines
2.9 KiB
Go

package db
import (
"reflect"
"testing"
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
)
func TestStore_ProposerSlashingNilBucket(t *testing.T) {
db := SetupSlasherDB(t)
defer TeardownSlasherDB(t, db)
ps := &ethpb.ProposerSlashing{ProposerIndex: 1}
has, _, err := db.HasProposerSlashing(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(SlashingStatus(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) {
db := SetupSlasherDB(t)
defer TeardownSlasherDB(t, db)
tests := []struct {
ss SlashingStatus
ps *ethpb.ProposerSlashing
}{
{
ss: Active,
ps: &ethpb.ProposerSlashing{ProposerIndex: 1},
},
{
ss: Included,
ps: &ethpb.ProposerSlashing{ProposerIndex: 2},
},
{
ss: Reverted,
ps: &ethpb.ProposerSlashing{ProposerIndex: 3},
},
}
for _, tt := range tests {
err := db.SaveProposerSlashing(tt.ss, tt.ps)
if err != nil {
t.Fatalf("save proposer slashing failed: %v", err)
}
proposerSlashings, err := db.ProposalSlashingsByStatus(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) {
db := SetupSlasherDB(t)
defer TeardownSlasherDB(t, db)
tests := []struct {
ss SlashingStatus
ps *ethpb.ProposerSlashing
}{
{
ss: Active,
ps: &ethpb.ProposerSlashing{ProposerIndex: 1},
},
{
ss: Active,
ps: &ethpb.ProposerSlashing{ProposerIndex: 2},
},
{
ss: Active,
ps: &ethpb.ProposerSlashing{ProposerIndex: 3},
},
}
for _, tt := range tests {
err := db.SaveProposerSlashing(tt.ss, tt.ps)
if err != nil {
t.Fatalf("save proposer slashing failed: %v", err)
}
}
for _, tt := range tests {
has, st, err := db.HasProposerSlashing(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(SlashingStatus(Included), tt.ps)
has, st, err = db.HasProposerSlashing(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 != Included {
t.Fatalf("failed to find proposer slashing with the correct status: %v", tt.ps)
}
}
}