2020-06-23 16:46:48 +00:00
|
|
|
package testing
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
|
2021-07-21 21:34:07 +00:00
|
|
|
eth "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1"
|
2020-06-23 16:46:48 +00:00
|
|
|
slashpb "github.com/prysmaticlabs/prysm/proto/slashing"
|
|
|
|
"google.golang.org/grpc"
|
2021-05-28 06:57:47 +00:00
|
|
|
"google.golang.org/protobuf/proto"
|
2020-06-23 16:46:48 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// MockSlasher mocks the slasher rpc server.
|
|
|
|
type MockSlasher struct {
|
|
|
|
SlashAttestation bool
|
|
|
|
SlashBlock bool
|
|
|
|
IsSlashableAttestationCalled bool
|
|
|
|
IsSlashableAttestationNoUpdateCalled bool
|
|
|
|
IsSlashableBlockCalled bool
|
|
|
|
IsSlashableBlockNoUpdateCalled bool
|
|
|
|
}
|
|
|
|
|
2020-11-22 08:51:20 +00:00
|
|
|
// HighestAttestations will return an empty array of attestations.
|
|
|
|
func (ms MockSlasher) HighestAttestations(ctx context.Context, req *slashpb.HighestAttestationRequest, _ ...grpc.CallOption) (*slashpb.HighestAttestationResponse, error) {
|
|
|
|
return &slashpb.HighestAttestationResponse{
|
|
|
|
Attestations: nil,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2020-06-23 16:46:48 +00:00
|
|
|
// IsSlashableAttestation returns slashbale attestation if slash attestation is set to true.
|
2020-10-12 08:11:05 +00:00
|
|
|
func (ms MockSlasher) IsSlashableAttestation(_ context.Context, in *eth.IndexedAttestation, _ ...grpc.CallOption) (*slashpb.AttesterSlashingResponse, error) {
|
2020-06-23 16:46:48 +00:00
|
|
|
ms.IsSlashableAttestationCalled = true
|
|
|
|
if ms.SlashAttestation {
|
|
|
|
|
|
|
|
slashingAtt, ok := proto.Clone(in).(*eth.IndexedAttestation)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("object is not of type *eth.IndexedAttestation")
|
|
|
|
}
|
|
|
|
slashingAtt.Data.BeaconBlockRoot = []byte("slashing")
|
|
|
|
slashings := []*eth.AttesterSlashing{{
|
|
|
|
Attestation_1: in,
|
|
|
|
Attestation_2: slashingAtt,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
return &slashpb.AttesterSlashingResponse{
|
|
|
|
AttesterSlashing: slashings,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsSlashableAttestationNoUpdate returns slashbale if slash attestation is set to true.
|
2020-10-12 08:11:05 +00:00
|
|
|
func (ms MockSlasher) IsSlashableAttestationNoUpdate(_ context.Context, _ *eth.IndexedAttestation, _ ...grpc.CallOption) (*slashpb.Slashable, error) {
|
2020-06-23 16:46:48 +00:00
|
|
|
ms.IsSlashableAttestationNoUpdateCalled = true
|
|
|
|
return &slashpb.Slashable{
|
|
|
|
Slashable: ms.SlashAttestation,
|
|
|
|
}, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsSlashableBlock returns proposer slashing if slash block is set to true.
|
2020-10-12 08:11:05 +00:00
|
|
|
func (ms MockSlasher) IsSlashableBlock(_ context.Context, in *eth.SignedBeaconBlockHeader, _ ...grpc.CallOption) (*slashpb.ProposerSlashingResponse, error) {
|
2020-06-23 16:46:48 +00:00
|
|
|
ms.IsSlashableBlockCalled = true
|
|
|
|
if ms.SlashBlock {
|
|
|
|
slashingBlk, ok := proto.Clone(in).(*eth.SignedBeaconBlockHeader)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("object is not of type *eth.SignedBeaconBlockHeader")
|
|
|
|
}
|
|
|
|
slashingBlk.Header.BodyRoot = []byte("slashing")
|
|
|
|
slashings := []*eth.ProposerSlashing{{
|
|
|
|
Header_1: in,
|
|
|
|
Header_2: slashingBlk,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
return &slashpb.ProposerSlashingResponse{
|
|
|
|
ProposerSlashing: slashings,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsSlashableBlockNoUpdate returns slashbale if slash block is set to true.
|
2020-10-12 08:11:05 +00:00
|
|
|
func (ms MockSlasher) IsSlashableBlockNoUpdate(_ context.Context, _ *eth.BeaconBlockHeader, _ ...grpc.CallOption) (*slashpb.Slashable, error) {
|
2020-06-23 16:46:48 +00:00
|
|
|
ms.IsSlashableBlockNoUpdateCalled = true
|
|
|
|
return &slashpb.Slashable{
|
|
|
|
Slashable: ms.SlashBlock,
|
|
|
|
}, nil
|
|
|
|
}
|