github.com/prysmaticlabs/prysm@v1.4.4/validator/testing/mock_slasher.go (about)

     1  package testing
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  
     7  	eth "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1"
     8  	slashpb "github.com/prysmaticlabs/prysm/proto/slashing"
     9  	"google.golang.org/grpc"
    10  	"google.golang.org/protobuf/proto"
    11  )
    12  
    13  // MockSlasher mocks the slasher rpc server.
    14  type MockSlasher struct {
    15  	SlashAttestation                     bool
    16  	SlashBlock                           bool
    17  	IsSlashableAttestationCalled         bool
    18  	IsSlashableAttestationNoUpdateCalled bool
    19  	IsSlashableBlockCalled               bool
    20  	IsSlashableBlockNoUpdateCalled       bool
    21  }
    22  
    23  // HighestAttestations will return an empty array of attestations.
    24  func (ms MockSlasher) HighestAttestations(ctx context.Context, req *slashpb.HighestAttestationRequest, _ ...grpc.CallOption) (*slashpb.HighestAttestationResponse, error) {
    25  	return &slashpb.HighestAttestationResponse{
    26  		Attestations: nil,
    27  	}, nil
    28  }
    29  
    30  // IsSlashableAttestation returns slashbale attestation if slash attestation is set to true.
    31  func (ms MockSlasher) IsSlashableAttestation(_ context.Context, in *eth.IndexedAttestation, _ ...grpc.CallOption) (*slashpb.AttesterSlashingResponse, error) {
    32  	ms.IsSlashableAttestationCalled = true
    33  	if ms.SlashAttestation {
    34  
    35  		slashingAtt, ok := proto.Clone(in).(*eth.IndexedAttestation)
    36  		if !ok {
    37  			return nil, errors.New("object is not of type *eth.IndexedAttestation")
    38  		}
    39  		slashingAtt.Data.BeaconBlockRoot = []byte("slashing")
    40  		slashings := []*eth.AttesterSlashing{{
    41  			Attestation_1: in,
    42  			Attestation_2: slashingAtt,
    43  		},
    44  		}
    45  		return &slashpb.AttesterSlashingResponse{
    46  			AttesterSlashing: slashings,
    47  		}, nil
    48  	}
    49  	return nil, nil
    50  }
    51  
    52  // IsSlashableAttestationNoUpdate returns slashbale if slash attestation is set to true.
    53  func (ms MockSlasher) IsSlashableAttestationNoUpdate(_ context.Context, _ *eth.IndexedAttestation, _ ...grpc.CallOption) (*slashpb.Slashable, error) {
    54  	ms.IsSlashableAttestationNoUpdateCalled = true
    55  	return &slashpb.Slashable{
    56  		Slashable: ms.SlashAttestation,
    57  	}, nil
    58  
    59  }
    60  
    61  // IsSlashableBlock returns proposer slashing if slash block is set to true.
    62  func (ms MockSlasher) IsSlashableBlock(_ context.Context, in *eth.SignedBeaconBlockHeader, _ ...grpc.CallOption) (*slashpb.ProposerSlashingResponse, error) {
    63  	ms.IsSlashableBlockCalled = true
    64  	if ms.SlashBlock {
    65  		slashingBlk, ok := proto.Clone(in).(*eth.SignedBeaconBlockHeader)
    66  		if !ok {
    67  			return nil, errors.New("object is not of type *eth.SignedBeaconBlockHeader")
    68  		}
    69  		slashingBlk.Header.BodyRoot = []byte("slashing")
    70  		slashings := []*eth.ProposerSlashing{{
    71  			Header_1: in,
    72  			Header_2: slashingBlk,
    73  		},
    74  		}
    75  		return &slashpb.ProposerSlashingResponse{
    76  			ProposerSlashing: slashings,
    77  		}, nil
    78  	}
    79  	return nil, nil
    80  }
    81  
    82  // IsSlashableBlockNoUpdate returns slashbale if slash block is set to true.
    83  func (ms MockSlasher) IsSlashableBlockNoUpdate(_ context.Context, _ *eth.BeaconBlockHeader, _ ...grpc.CallOption) (*slashpb.Slashable, error) {
    84  	ms.IsSlashableBlockNoUpdateCalled = true
    85  	return &slashpb.Slashable{
    86  		Slashable: ms.SlashBlock,
    87  	}, nil
    88  }