2020-07-03 19:54:42 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/featureconfig"
|
2020-10-15 19:35:31 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil"
|
2020-07-21 13:02:21 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil/require"
|
2020-07-03 19:54:42 +00:00
|
|
|
mockSlasher "github.com/prysmaticlabs/prysm/validator/testing"
|
|
|
|
)
|
|
|
|
|
2020-10-15 19:35:31 +00:00
|
|
|
func TestPreBlockSignLocalValidation(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
config := &featureconfig.Flags{
|
|
|
|
SlasherProtection: false,
|
|
|
|
}
|
|
|
|
reset := featureconfig.InitWithReset(config)
|
|
|
|
defer reset()
|
|
|
|
validator, _, validatorKey, finish := setup(t)
|
|
|
|
defer finish()
|
|
|
|
|
|
|
|
block := ðpb.BeaconBlock{
|
|
|
|
Slot: 10,
|
|
|
|
ProposerIndex: 0,
|
|
|
|
}
|
|
|
|
err := validator.db.SaveProposalHistoryForSlot(ctx, validatorKey.PublicKey().Marshal(), 10, []byte{1})
|
|
|
|
require.NoError(t, err)
|
|
|
|
pubKey := [48]byte{}
|
|
|
|
copy(pubKey[:], validatorKey.PublicKey().Marshal())
|
|
|
|
err = validator.preBlockSignValidations(context.Background(), pubKey, block)
|
|
|
|
require.ErrorContains(t, failedPreBlockSignLocalErr, err)
|
|
|
|
block.Slot = 9
|
|
|
|
err = validator.preBlockSignValidations(context.Background(), pubKey, block)
|
|
|
|
require.NoError(t, err, "Expected allowed attestation not to throw error")
|
|
|
|
}
|
|
|
|
|
2020-07-03 19:54:42 +00:00
|
|
|
func TestPreBlockSignValidation(t *testing.T) {
|
|
|
|
config := &featureconfig.Flags{
|
|
|
|
SlasherProtection: true,
|
|
|
|
}
|
|
|
|
reset := featureconfig.InitWithReset(config)
|
|
|
|
defer reset()
|
2020-10-14 22:20:20 +00:00
|
|
|
validator, _, validatorKey, finish := setup(t)
|
2020-07-03 19:54:42 +00:00
|
|
|
defer finish()
|
2020-10-14 22:20:20 +00:00
|
|
|
pubKey := [48]byte{}
|
|
|
|
copy(pubKey[:], validatorKey.PublicKey().Marshal())
|
2020-07-03 19:54:42 +00:00
|
|
|
|
|
|
|
block := ðpb.BeaconBlock{
|
|
|
|
Slot: 10,
|
|
|
|
ProposerIndex: 0,
|
|
|
|
}
|
|
|
|
mockProtector := &mockSlasher.MockProtector{AllowBlock: false}
|
|
|
|
validator.protector = mockProtector
|
2020-10-14 22:20:20 +00:00
|
|
|
err := validator.preBlockSignValidations(context.Background(), pubKey, block)
|
2020-07-21 13:02:21 +00:00
|
|
|
require.ErrorContains(t, failedPreBlockSignExternalErr, err)
|
2020-07-03 19:54:42 +00:00
|
|
|
mockProtector.AllowBlock = true
|
2020-10-14 22:20:20 +00:00
|
|
|
err = validator.preBlockSignValidations(context.Background(), pubKey, block)
|
2020-07-21 13:02:21 +00:00
|
|
|
require.NoError(t, err, "Expected allowed attestation not to throw error")
|
2020-07-03 19:54:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestPostBlockSignUpdate(t *testing.T) {
|
|
|
|
config := &featureconfig.Flags{
|
|
|
|
SlasherProtection: true,
|
|
|
|
}
|
|
|
|
reset := featureconfig.InitWithReset(config)
|
|
|
|
defer reset()
|
2020-10-14 22:20:20 +00:00
|
|
|
validator, _, validatorKey, finish := setup(t)
|
2020-07-03 19:54:42 +00:00
|
|
|
defer finish()
|
2020-10-14 22:20:20 +00:00
|
|
|
pubKey := [48]byte{}
|
|
|
|
copy(pubKey[:], validatorKey.PublicKey().Marshal())
|
2020-10-15 19:35:31 +00:00
|
|
|
emptyBlock := testutil.NewBeaconBlock()
|
|
|
|
emptyBlock.Block.Slot = 10
|
|
|
|
emptyBlock.Block.ProposerIndex = 0
|
2020-07-03 19:54:42 +00:00
|
|
|
mockProtector := &mockSlasher.MockProtector{AllowBlock: false}
|
|
|
|
validator.protector = mockProtector
|
2020-10-15 19:35:31 +00:00
|
|
|
err := validator.postBlockSignUpdate(context.Background(), pubKey, emptyBlock, nil)
|
2020-07-21 13:02:21 +00:00
|
|
|
require.ErrorContains(t, failedPostBlockSignErr, err, "Expected error when post signature update is detected as slashable")
|
2020-07-03 19:54:42 +00:00
|
|
|
mockProtector.AllowBlock = true
|
2020-10-15 19:35:31 +00:00
|
|
|
err = validator.postBlockSignUpdate(context.Background(), pubKey, emptyBlock, ðpb.DomainResponse{SignatureDomain: make([]byte, 32)})
|
2020-07-21 13:02:21 +00:00
|
|
|
require.NoError(t, err, "Expected allowed attestation not to throw error")
|
2020-07-03 19:54:42 +00:00
|
|
|
}
|