2019-08-18 15:33:58 +00:00
|
|
|
package sync
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/rand"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/prysmaticlabs/go-ssz"
|
2019-08-27 13:27:04 +00:00
|
|
|
mock "github.com/prysmaticlabs/prysm/beacon-chain/blockchain/testing"
|
2019-08-18 15:33:58 +00:00
|
|
|
"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-23 17:48:40 +00:00
|
|
|
func setupValidExit(t *testing.T) (*ethpb.VoluntaryExit, *pb.BeaconState) {
|
2019-08-18 15:33:58 +00:00
|
|
|
exit := ðpb.VoluntaryExit{
|
|
|
|
ValidatorIndex: 0,
|
|
|
|
Epoch: 0,
|
|
|
|
}
|
|
|
|
registry := []*ethpb.Validator{
|
|
|
|
{
|
|
|
|
ExitEpoch: params.BeaconConfig().FarFutureEpoch,
|
|
|
|
ActivationEpoch: 0,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
state := &pb.BeaconState{
|
|
|
|
Validators: registry,
|
|
|
|
Fork: &pb.Fork{
|
|
|
|
CurrentVersion: params.BeaconConfig().GenesisForkVersion,
|
|
|
|
PreviousVersion: params.BeaconConfig().GenesisForkVersion,
|
|
|
|
},
|
|
|
|
Slot: params.BeaconConfig().SlotsPerEpoch * 5,
|
|
|
|
}
|
|
|
|
state.Slot = state.Slot + (params.BeaconConfig().PersistentCommitteePeriod * params.BeaconConfig().SlotsPerEpoch)
|
|
|
|
signingRoot, err := ssz.SigningRoot(exit)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
domain := helpers.Domain(state, helpers.CurrentEpoch(state), params.BeaconConfig().DomainVoluntaryExit)
|
|
|
|
priv, err := bls.RandKey(rand.Reader)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
sig := priv.Sign(signingRoot[:], domain)
|
|
|
|
exit.Signature = sig.Marshal()
|
|
|
|
state.Validators[0].PublicKey = priv.PublicKey().Marshal()[:]
|
|
|
|
|
|
|
|
b := make([]byte, 32)
|
|
|
|
if _, err := rand.Read(b); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-08-23 17:48:40 +00:00
|
|
|
|
|
|
|
return exit, state
|
2019-08-18 15:33:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestValidateVoluntaryExit_ValidExit(t *testing.T) {
|
|
|
|
p2p := p2ptest.NewTestP2P(t)
|
|
|
|
ctx := context.Background()
|
|
|
|
|
2019-08-23 17:48:40 +00:00
|
|
|
exit, s := setupValidExit(t)
|
2019-08-18 15:33:58 +00:00
|
|
|
|
|
|
|
r := &RegularSync{
|
2019-08-26 20:06:16 +00:00
|
|
|
p2p: p2p,
|
|
|
|
chain: &mock.ChainService{
|
|
|
|
State: s,
|
|
|
|
},
|
2019-08-18 15:33:58 +00:00
|
|
|
}
|
|
|
|
|
2019-09-04 00:22:15 +00:00
|
|
|
if !r.validateVoluntaryExit(ctx, exit, p2p, false /*fromSelf*/) {
|
2019-08-18 15:33:58 +00:00
|
|
|
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
|
2019-09-04 00:22:15 +00:00
|
|
|
if r.validateVoluntaryExit(ctx, exit, p2p, false /*fromSelf*/) {
|
2019-08-18 15:33:58 +00:00
|
|
|
t.Error("Passed validation when should have failed")
|
|
|
|
}
|
|
|
|
|
|
|
|
if p2p.BroadcastCalled {
|
|
|
|
t.Error("broadcast was called when it should not have been called")
|
|
|
|
}
|
|
|
|
}
|
2019-09-04 00:22:15 +00:00
|
|
|
|
|
|
|
func TestValidateVoluntaryExit_ValidExit_FromSelf(t *testing.T) {
|
|
|
|
p2p := p2ptest.NewTestP2P(t)
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
exit, s := setupValidExit(t)
|
|
|
|
|
|
|
|
r := &RegularSync{
|
|
|
|
p2p: p2p,
|
|
|
|
chain: &mock.ChainService{
|
|
|
|
State: s,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
if r.validateVoluntaryExit(ctx, exit, p2p, true /*fromSelf*/) {
|
|
|
|
t.Error("Validation should have failed")
|
|
|
|
}
|
|
|
|
|
|
|
|
if p2p.BroadcastCalled {
|
|
|
|
t.Error("Broadcast was called")
|
|
|
|
}
|
|
|
|
}
|