github.com/prysmaticlabs/prysm@v1.4.4/validator/slashing-protection/external_test.go (about)

     1  package slashingprotection
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	eth "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1"
     8  	"github.com/prysmaticlabs/prysm/shared/bytesutil"
     9  	"github.com/prysmaticlabs/prysm/shared/testutil/assert"
    10  	mockSlasher "github.com/prysmaticlabs/prysm/validator/testing"
    11  )
    12  
    13  func TestService_VerifyAttestation(t *testing.T) {
    14  	s := &Service{slasherClient: mockSlasher.MockSlasher{SlashAttestation: true}}
    15  	att := &eth.IndexedAttestation{
    16  		AttestingIndices: []uint64{1, 2},
    17  		Data: &eth.AttestationData{
    18  			Slot:            5,
    19  			CommitteeIndex:  2,
    20  			BeaconBlockRoot: []byte("great block"),
    21  			Source: &eth.Checkpoint{
    22  				Epoch: 4,
    23  				Root:  []byte("good source"),
    24  			},
    25  			Target: &eth.Checkpoint{
    26  				Epoch: 10,
    27  				Root:  []byte("good target"),
    28  			},
    29  		},
    30  	}
    31  	assert.Equal(t, false, s.CheckAttestationSafety(context.Background(), att), "Expected verify attestation to fail verification")
    32  	s = &Service{slasherClient: mockSlasher.MockSlasher{SlashAttestation: false}}
    33  	assert.Equal(t, true, s.CheckAttestationSafety(context.Background(), att), "Expected verify attestation to pass verification")
    34  }
    35  
    36  func TestService_CommitAttestation(t *testing.T) {
    37  	s := &Service{slasherClient: mockSlasher.MockSlasher{SlashAttestation: true}}
    38  	att := &eth.IndexedAttestation{
    39  		AttestingIndices: []uint64{1, 2},
    40  		Data: &eth.AttestationData{
    41  			Slot:            5,
    42  			CommitteeIndex:  2,
    43  			BeaconBlockRoot: []byte("great block"),
    44  			Source: &eth.Checkpoint{
    45  				Epoch: 4,
    46  				Root:  []byte("good source"),
    47  			},
    48  			Target: &eth.Checkpoint{
    49  				Epoch: 10,
    50  				Root:  []byte("good target"),
    51  			},
    52  		},
    53  	}
    54  	assert.Equal(t, false, s.CommitAttestation(context.Background(), att), "Expected commit attestation to fail verification")
    55  	s = &Service{slasherClient: mockSlasher.MockSlasher{SlashAttestation: false}}
    56  	assert.Equal(t, true, s.CommitAttestation(context.Background(), att), "Expected commit attestation to pass verification")
    57  }
    58  
    59  func TestService_CommitBlock(t *testing.T) {
    60  	s := &Service{slasherClient: mockSlasher.MockSlasher{SlashBlock: true}}
    61  	blk := &eth.SignedBeaconBlockHeader{
    62  		Header: &eth.BeaconBlockHeader{
    63  			Slot:          0,
    64  			ProposerIndex: 0,
    65  			ParentRoot:    bytesutil.PadTo([]byte("parent"), 32),
    66  			StateRoot:     bytesutil.PadTo([]byte("state"), 32),
    67  			BodyRoot:      bytesutil.PadTo([]byte("body"), 32),
    68  		},
    69  	}
    70  	slashable, err := s.CommitBlock(context.Background(), blk)
    71  	assert.NoError(t, err)
    72  	assert.Equal(t, false, slashable, "Expected commit block to fail verification")
    73  	s = &Service{slasherClient: mockSlasher.MockSlasher{SlashBlock: false}}
    74  	slashable, err = s.CommitBlock(context.Background(), blk)
    75  	assert.NoError(t, err)
    76  	assert.Equal(t, true, slashable, "Expected commit block to pass verification")
    77  }
    78  
    79  func TestService_VerifyBlock(t *testing.T) {
    80  	s := &Service{slasherClient: mockSlasher.MockSlasher{SlashBlock: true}}
    81  	blk := &eth.BeaconBlockHeader{
    82  		Slot:          0,
    83  		ProposerIndex: 0,
    84  		ParentRoot:    bytesutil.PadTo([]byte("parent"), 32),
    85  		StateRoot:     bytesutil.PadTo([]byte("state"), 32),
    86  		BodyRoot:      bytesutil.PadTo([]byte("body"), 32),
    87  	}
    88  	assert.Equal(t, false, s.CheckBlockSafety(context.Background(), blk), "Expected verify block to fail verification")
    89  	s = &Service{slasherClient: mockSlasher.MockSlasher{SlashBlock: false}}
    90  	assert.Equal(t, true, s.CheckBlockSafety(context.Background(), blk), "Expected verify block to pass verification")
    91  }