2019-08-22 14:31:56 +00:00
|
|
|
package sync
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/rand"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/prysmaticlabs/go-ssz"
|
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers"
|
|
|
|
p2ptest "github.com/prysmaticlabs/prysm/beacon-chain/p2p/testing"
|
|
|
|
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
|
|
|
|
ethpb "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1"
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/bls"
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/params"
|
2019-08-26 20:06:16 +00:00
|
|
|
mock "github.com/prysmaticlabs/prysm/beacon-chain/blockchain/testing"
|
2019-08-22 14:31:56 +00:00
|
|
|
)
|
|
|
|
|
2019-08-23 17:48:40 +00:00
|
|
|
func setupValidProposerSlashing(t *testing.T) (*ethpb.ProposerSlashing, *pb.BeaconState) {
|
2019-08-22 14:31:56 +00:00
|
|
|
validators := make([]*ethpb.Validator, 100)
|
|
|
|
for i := 0; i < len(validators); i++ {
|
|
|
|
validators[i] = ðpb.Validator{
|
|
|
|
EffectiveBalance: params.BeaconConfig().MaxEffectiveBalance,
|
|
|
|
Slashed: false,
|
|
|
|
ExitEpoch: params.BeaconConfig().FarFutureEpoch,
|
|
|
|
WithdrawableEpoch: params.BeaconConfig().FarFutureEpoch,
|
|
|
|
ActivationEpoch: 0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
validatorBalances := make([]uint64, len(validators))
|
|
|
|
for i := 0; i < len(validatorBalances); i++ {
|
|
|
|
validatorBalances[i] = params.BeaconConfig().MaxEffectiveBalance
|
|
|
|
}
|
|
|
|
|
|
|
|
currentSlot := uint64(0)
|
2019-08-23 17:48:40 +00:00
|
|
|
state := &pb.BeaconState{
|
2019-08-22 14:31:56 +00:00
|
|
|
Validators: validators,
|
|
|
|
Slot: currentSlot,
|
|
|
|
Balances: validatorBalances,
|
|
|
|
Fork: &pb.Fork{
|
|
|
|
CurrentVersion: params.BeaconConfig().GenesisForkVersion,
|
|
|
|
PreviousVersion: params.BeaconConfig().GenesisForkVersion,
|
|
|
|
Epoch: 0,
|
|
|
|
},
|
|
|
|
Slashings: make([]uint64, params.BeaconConfig().EpochsPerSlashingsVector),
|
|
|
|
RandaoMixes: make([][]byte, params.BeaconConfig().EpochsPerHistoricalVector),
|
|
|
|
ActiveIndexRoots: make([][]byte, params.BeaconConfig().EpochsPerHistoricalVector),
|
|
|
|
}
|
|
|
|
|
|
|
|
domain := helpers.Domain(
|
2019-08-23 17:48:40 +00:00
|
|
|
state,
|
|
|
|
helpers.CurrentEpoch(state),
|
2019-08-22 14:31:56 +00:00
|
|
|
params.BeaconConfig().DomainBeaconProposer,
|
|
|
|
)
|
|
|
|
privKey, err := bls.RandKey(rand.Reader)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Could not generate random private key: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
header1 := ðpb.BeaconBlockHeader{
|
|
|
|
Slot: 0,
|
|
|
|
StateRoot: []byte("A"),
|
|
|
|
}
|
|
|
|
signingRoot, err := ssz.SigningRoot(header1)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Could not get signing root of beacon block header: %v", err)
|
|
|
|
}
|
|
|
|
header1.Signature = privKey.Sign(signingRoot[:], domain).Marshal()[:]
|
|
|
|
|
|
|
|
header2 := ðpb.BeaconBlockHeader{
|
|
|
|
Slot: 0,
|
|
|
|
StateRoot: []byte("B"),
|
|
|
|
}
|
|
|
|
signingRoot, err = ssz.SigningRoot(header2)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Could not get signing root of beacon block header: %v", err)
|
|
|
|
}
|
|
|
|
header2.Signature = privKey.Sign(signingRoot[:], domain).Marshal()[:]
|
|
|
|
|
|
|
|
slashing := ðpb.ProposerSlashing{
|
|
|
|
ProposerIndex: 1,
|
|
|
|
Header_1: header1,
|
|
|
|
Header_2: header2,
|
|
|
|
}
|
|
|
|
|
2019-08-23 17:48:40 +00:00
|
|
|
state.Validators[1].PublicKey = privKey.PublicKey().Marshal()[:]
|
2019-08-22 14:31:56 +00:00
|
|
|
|
|
|
|
b := make([]byte, 32)
|
|
|
|
if _, err := rand.Read(b); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-08-23 17:48:40 +00:00
|
|
|
|
|
|
|
return slashing, state
|
2019-08-22 14:31:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestValidateProposerSlashing_ValidSlashing(t *testing.T) {
|
|
|
|
p2p := p2ptest.NewTestP2P(t)
|
|
|
|
ctx := context.Background()
|
|
|
|
|
2019-08-23 17:48:40 +00:00
|
|
|
slashing, s := setupValidProposerSlashing(t)
|
2019-08-22 14:31:56 +00:00
|
|
|
|
|
|
|
r := &RegularSync{
|
2019-08-23 17:48:40 +00:00
|
|
|
p2p: p2p,
|
2019-08-26 20:06:16 +00:00
|
|
|
chain: &mock.ChainService{State: s},
|
2019-08-22 14:31:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !r.validateProposerSlashing(ctx, slashing, p2p) {
|
|
|
|
t.Error("Failed validation")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !p2p.BroadcastCalled {
|
|
|
|
t.Error("Broadcast was not called")
|
|
|
|
}
|
|
|
|
|
|
|
|
// A second message with the same information should not be valid for processing or
|
|
|
|
// propagation.
|
|
|
|
p2p.BroadcastCalled = false
|
|
|
|
if r.validateProposerSlashing(ctx, slashing, p2p) {
|
|
|
|
t.Error("Passed validation when should have failed")
|
|
|
|
}
|
|
|
|
|
|
|
|
if p2p.BroadcastCalled {
|
|
|
|
t.Error("broadcast was called when it should not have been called")
|
|
|
|
}
|
|
|
|
}
|