prysm-pulse/slasher/rpc/server_test.go
shayzluf 82de66bb90 slasher grpc server (#3786)
* first version of the watchtower api

* service files

* Begin work on grpc server

* More changes to server

* REnames and mock setup

* working test

* merge

* double propose detection test

* nishant review

* todo change

* gaz

* fix service

* gaz

* remove unused import

* gaz

* resolve circular dependency

* resolve circular dependency 2nd try

* remove package

* fix package

* fix test

* added tests

* gaz

* remove status check

* gaz

* remove context

* remove context

* change var name

* moved to rpc dir

* gaz

* remove server code

* gaz

* slasher server

* visibility change

* pb

* service update

* gaz

* slasher grpc server

* making it work

* setup db and start

* gaz

* service flags fixes

* grpc service running

* go imports

* remove new initializer

* gaz

* remove feature flags

* change back SetupSlasherDB

* fix SetupSlasherDB calls

* define err

* fix bad merge

* fix test

* fix imports

* fix imports

* fix imports

* add cancel

* comment stop

* fix cancel issue

* remove unneeded code

* bring back bad merge that removed TODO

* fixed slasher to be runable again

* wait for channel close

* gaz

* small test

* flags fix

* fix flag order

* remove flag
2019-10-31 11:26:55 +08:00

168 lines
3.9 KiB
Go

package rpc
import (
"context"
"testing"
"github.com/gogo/protobuf/proto"
ethpb "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1"
"github.com/prysmaticlabs/prysm/slasher/db"
)
func TestServer_IsSlashableBlock(t *testing.T) {
dbs := db.SetupSlasherDB(t)
defer db.TeardownSlasherDB(t, dbs)
ctx := context.Background()
slasherServer := &Server{
SlasherDb: dbs,
}
psr := &ethpb.ProposerSlashingRequest{
BlockHeader: &ethpb.BeaconBlockHeader{
Slot: 1,
StateRoot: []byte("A"),
},
ValidatorIndex: 1,
}
psr2 := &ethpb.ProposerSlashingRequest{
BlockHeader: &ethpb.BeaconBlockHeader{
Slot: 1,
StateRoot: []byte("B"),
},
ValidatorIndex: 1,
}
if _, err := slasherServer.IsSlashableBlock(ctx, psr); err != nil {
t.Errorf("Could not call RPC method: %v", err)
}
sr, err := slasherServer.IsSlashableBlock(ctx, psr2)
if err != nil {
t.Errorf("Could not call RPC method: %v", err)
}
want := &ethpb.ProposerSlashing{
ProposerIndex: psr.ValidatorIndex,
Header_1: psr2.BlockHeader,
Header_2: psr.BlockHeader,
}
if len(sr.ProposerSlashing) != 1 {
t.Errorf("Should return 1 slashaing proof: %v", sr)
}
if !proto.Equal(sr.ProposerSlashing[0], want) {
t.Errorf("wanted slashing proof: %v got: %v", want, sr.ProposerSlashing[0])
}
}
func TestServer_IsNotSlashableBlock(t *testing.T) {
dbs := db.SetupSlasherDB(t)
defer db.TeardownSlasherDB(t, dbs)
slasherServer := &Server{
SlasherDb: dbs,
}
psr := &ethpb.ProposerSlashingRequest{
BlockHeader: &ethpb.BeaconBlockHeader{
Slot: 1,
StateRoot: []byte("A"),
},
ValidatorIndex: 1,
}
psr2 := &ethpb.ProposerSlashingRequest{
BlockHeader: &ethpb.BeaconBlockHeader{
Slot: 65,
StateRoot: []byte("B"),
},
ValidatorIndex: 1,
}
ctx := context.Background()
if _, err := slasherServer.IsSlashableBlock(ctx, psr); err != nil {
t.Errorf("Could not call RPC method: %v", err)
}
sr, err := slasherServer.IsSlashableBlock(ctx, psr2)
if err != nil {
t.Errorf("Could not call RPC method: %v", err)
}
if len(sr.ProposerSlashing) != 0 {
t.Errorf("Should return 0 slashaing proof: %v", sr)
}
}
func TestServer_DoubleBlock(t *testing.T) {
dbs := db.SetupSlasherDB(t)
defer db.TeardownSlasherDB(t, dbs)
ctx := context.Background()
slasherServer := &Server{
ctx: ctx,
SlasherDb: dbs,
}
psr := &ethpb.ProposerSlashingRequest{
BlockHeader: &ethpb.BeaconBlockHeader{
Slot: 1,
StateRoot: []byte("A"),
},
ValidatorIndex: 1,
}
if _, err := slasherServer.IsSlashableBlock(ctx, psr); err != nil {
t.Errorf("Could not call RPC method: %v", err)
}
sr, err := slasherServer.IsSlashableBlock(ctx, psr)
if err != nil {
t.Errorf("Could not call RPC method: %v", err)
}
if len(sr.ProposerSlashing) != 0 {
t.Errorf("Should return 0 slashaing proof: %v", sr)
}
}
func TestServer_SameEpochDifferentSlotSlashable(t *testing.T) {
dbs := db.SetupSlasherDB(t)
defer db.TeardownSlasherDB(t, dbs)
ctx := context.Background()
slasherServer := &Server{
ctx: ctx,
SlasherDb: dbs,
}
psr := &ethpb.ProposerSlashingRequest{
BlockHeader: &ethpb.BeaconBlockHeader{
Slot: 1,
StateRoot: []byte("A"),
},
ValidatorIndex: 1,
}
psr2 := &ethpb.ProposerSlashingRequest{
BlockHeader: &ethpb.BeaconBlockHeader{
Slot: 63,
StateRoot: []byte("B"),
},
ValidatorIndex: 1,
}
want := &ethpb.ProposerSlashing{
ProposerIndex: psr.ValidatorIndex,
Header_1: psr2.BlockHeader,
Header_2: psr.BlockHeader,
}
if _, err := slasherServer.IsSlashableBlock(ctx, psr); err != nil {
t.Errorf("Could not call RPC method: %v", err)
}
sr, err := slasherServer.IsSlashableBlock(ctx, psr2)
if err != nil {
t.Errorf("Could not call RPC method: %v", err)
}
if len(sr.ProposerSlashing) != 1 {
t.Errorf("Should return 1 slashaing proof: %v", sr)
}
if !proto.Equal(sr.ProposerSlashing[0], want) {
t.Errorf("wanted slashing proof: %v got: %v", want, sr.ProposerSlashing[0])
}
}