2020-05-20 15:23:22 +00:00
|
|
|
package slashingprotection
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
2021-07-21 21:34:07 +00:00
|
|
|
eth "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1"
|
2020-08-27 18:13:32 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/bytesutil"
|
2020-07-23 01:13:52 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil/assert"
|
2020-06-23 16:46:48 +00:00
|
|
|
mockSlasher "github.com/prysmaticlabs/prysm/validator/testing"
|
2020-05-20 15:23:22 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestService_VerifyAttestation(t *testing.T) {
|
2020-06-23 16:46:48 +00:00
|
|
|
s := &Service{slasherClient: mockSlasher.MockSlasher{SlashAttestation: true}}
|
2020-05-20 15:23:22 +00:00
|
|
|
att := ð.IndexedAttestation{
|
|
|
|
AttestingIndices: []uint64{1, 2},
|
|
|
|
Data: ð.AttestationData{
|
|
|
|
Slot: 5,
|
|
|
|
CommitteeIndex: 2,
|
|
|
|
BeaconBlockRoot: []byte("great block"),
|
|
|
|
Source: ð.Checkpoint{
|
|
|
|
Epoch: 4,
|
|
|
|
Root: []byte("good source"),
|
|
|
|
},
|
|
|
|
Target: ð.Checkpoint{
|
|
|
|
Epoch: 10,
|
|
|
|
Root: []byte("good target"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2020-07-23 01:13:52 +00:00
|
|
|
assert.Equal(t, false, s.CheckAttestationSafety(context.Background(), att), "Expected verify attestation to fail verification")
|
2020-06-23 16:46:48 +00:00
|
|
|
s = &Service{slasherClient: mockSlasher.MockSlasher{SlashAttestation: false}}
|
2020-07-23 01:13:52 +00:00
|
|
|
assert.Equal(t, true, s.CheckAttestationSafety(context.Background(), att), "Expected verify attestation to pass verification")
|
2020-05-20 15:23:22 +00:00
|
|
|
}
|
|
|
|
|
2020-06-23 16:46:48 +00:00
|
|
|
func TestService_CommitAttestation(t *testing.T) {
|
|
|
|
s := &Service{slasherClient: mockSlasher.MockSlasher{SlashAttestation: true}}
|
|
|
|
att := ð.IndexedAttestation{
|
|
|
|
AttestingIndices: []uint64{1, 2},
|
|
|
|
Data: ð.AttestationData{
|
|
|
|
Slot: 5,
|
|
|
|
CommitteeIndex: 2,
|
|
|
|
BeaconBlockRoot: []byte("great block"),
|
|
|
|
Source: ð.Checkpoint{
|
|
|
|
Epoch: 4,
|
|
|
|
Root: []byte("good source"),
|
|
|
|
},
|
|
|
|
Target: ð.Checkpoint{
|
|
|
|
Epoch: 10,
|
|
|
|
Root: []byte("good target"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2020-07-23 01:13:52 +00:00
|
|
|
assert.Equal(t, false, s.CommitAttestation(context.Background(), att), "Expected commit attestation to fail verification")
|
2020-06-23 16:46:48 +00:00
|
|
|
s = &Service{slasherClient: mockSlasher.MockSlasher{SlashAttestation: false}}
|
2020-07-23 01:13:52 +00:00
|
|
|
assert.Equal(t, true, s.CommitAttestation(context.Background(), att), "Expected commit attestation to pass verification")
|
2020-06-23 16:46:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestService_CommitBlock(t *testing.T) {
|
|
|
|
s := &Service{slasherClient: mockSlasher.MockSlasher{SlashBlock: true}}
|
2020-05-20 15:23:22 +00:00
|
|
|
blk := ð.SignedBeaconBlockHeader{
|
|
|
|
Header: ð.BeaconBlockHeader{
|
|
|
|
Slot: 0,
|
|
|
|
ProposerIndex: 0,
|
2020-08-27 18:13:32 +00:00
|
|
|
ParentRoot: bytesutil.PadTo([]byte("parent"), 32),
|
|
|
|
StateRoot: bytesutil.PadTo([]byte("state"), 32),
|
|
|
|
BodyRoot: bytesutil.PadTo([]byte("body"), 32),
|
2020-05-20 15:23:22 +00:00
|
|
|
},
|
|
|
|
}
|
2020-09-11 14:53:53 +00:00
|
|
|
slashable, err := s.CommitBlock(context.Background(), blk)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, false, slashable, "Expected commit block to fail verification")
|
2020-06-23 16:46:48 +00:00
|
|
|
s = &Service{slasherClient: mockSlasher.MockSlasher{SlashBlock: false}}
|
2020-09-11 14:53:53 +00:00
|
|
|
slashable, err = s.CommitBlock(context.Background(), blk)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, true, slashable, "Expected commit block to pass verification")
|
2020-06-23 16:46:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestService_VerifyBlock(t *testing.T) {
|
|
|
|
s := &Service{slasherClient: mockSlasher.MockSlasher{SlashBlock: true}}
|
|
|
|
blk := ð.BeaconBlockHeader{
|
|
|
|
Slot: 0,
|
|
|
|
ProposerIndex: 0,
|
2020-08-27 18:13:32 +00:00
|
|
|
ParentRoot: bytesutil.PadTo([]byte("parent"), 32),
|
|
|
|
StateRoot: bytesutil.PadTo([]byte("state"), 32),
|
|
|
|
BodyRoot: bytesutil.PadTo([]byte("body"), 32),
|
2020-06-23 16:46:48 +00:00
|
|
|
}
|
2020-07-23 01:13:52 +00:00
|
|
|
assert.Equal(t, false, s.CheckBlockSafety(context.Background(), blk), "Expected verify block to fail verification")
|
2020-06-23 16:46:48 +00:00
|
|
|
s = &Service{slasherClient: mockSlasher.MockSlasher{SlashBlock: false}}
|
2020-07-23 01:13:52 +00:00
|
|
|
assert.Equal(t, true, s.CheckBlockSafety(context.Background(), blk), "Expected verify block to pass verification")
|
2020-05-20 15:23:22 +00:00
|
|
|
}
|