mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2024-12-29 06:37:17 +00:00
168 lines
3.9 KiB
Go
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 := ðpb.ProposerSlashingRequest{
|
|
BlockHeader: ðpb.BeaconBlockHeader{
|
|
Slot: 1,
|
|
StateRoot: []byte("A"),
|
|
},
|
|
ValidatorIndex: 1,
|
|
}
|
|
psr2 := ðpb.ProposerSlashingRequest{
|
|
BlockHeader: ðpb.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 := ðpb.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 := ðpb.ProposerSlashingRequest{
|
|
BlockHeader: ðpb.BeaconBlockHeader{
|
|
Slot: 1,
|
|
StateRoot: []byte("A"),
|
|
},
|
|
ValidatorIndex: 1,
|
|
}
|
|
psr2 := ðpb.ProposerSlashingRequest{
|
|
BlockHeader: ðpb.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 := ðpb.ProposerSlashingRequest{
|
|
BlockHeader: ðpb.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_SameSlotSlashable(t *testing.T) {
|
|
dbs := db.SetupSlasherDB(t)
|
|
defer db.TeardownSlasherDB(t, dbs)
|
|
ctx := context.Background()
|
|
slasherServer := &Server{
|
|
ctx: ctx,
|
|
SlasherDB: dbs,
|
|
}
|
|
psr := ðpb.ProposerSlashingRequest{
|
|
BlockHeader: ðpb.BeaconBlockHeader{
|
|
Slot: 1,
|
|
StateRoot: []byte("A"),
|
|
},
|
|
ValidatorIndex: 1,
|
|
}
|
|
psr2 := ðpb.ProposerSlashingRequest{
|
|
BlockHeader: ðpb.BeaconBlockHeader{
|
|
Slot: 1,
|
|
StateRoot: []byte("B"),
|
|
},
|
|
ValidatorIndex: 1,
|
|
}
|
|
want := ðpb.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])
|
|
|
|
}
|
|
}
|