2019-08-18 15:33:58 +00:00
|
|
|
package sync
|
|
|
|
|
|
|
|
import (
|
2019-12-20 03:18:08 +00:00
|
|
|
"bytes"
|
2019-08-18 15:33:58 +00:00
|
|
|
"context"
|
|
|
|
"crypto/rand"
|
2019-12-20 03:18:08 +00:00
|
|
|
"reflect"
|
2019-08-18 15:33:58 +00:00
|
|
|
"testing"
|
|
|
|
|
2019-12-20 03:18:08 +00:00
|
|
|
pubsub "github.com/libp2p/go-libp2p-pubsub"
|
|
|
|
pubsubpb "github.com/libp2p/go-libp2p-pubsub/pb"
|
2019-11-27 05:08:18 +00:00
|
|
|
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
|
2019-08-18 15:33:58 +00:00
|
|
|
"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"
|
2019-12-20 03:18:08 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/p2p"
|
2019-08-18 15:33:58 +00:00
|
|
|
p2ptest "github.com/prysmaticlabs/prysm/beacon-chain/p2p/testing"
|
2019-09-20 17:54:32 +00:00
|
|
|
mockSync "github.com/prysmaticlabs/prysm/beacon-chain/sync/initial-sync/testing"
|
2019-08-18 15:33:58 +00:00
|
|
|
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
|
|
|
|
"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)
|
|
|
|
}
|
2019-10-01 14:36:36 +00:00
|
|
|
domain := helpers.Domain(state.Fork, helpers.CurrentEpoch(state), params.BeaconConfig().DomainVoluntaryExit)
|
2019-12-03 20:29:05 +00:00
|
|
|
priv := bls.RandKey()
|
|
|
|
|
2019-08-18 15:33:58 +00:00
|
|
|
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) {
|
2019-12-20 03:18:08 +00:00
|
|
|
p := p2ptest.NewTestP2P(t)
|
2019-08-18 15:33:58 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
|
2019-08-23 17:48:40 +00:00
|
|
|
exit, s := setupValidExit(t)
|
2019-08-18 15:33:58 +00:00
|
|
|
|
2019-12-17 01:53:55 +00:00
|
|
|
r := &Service{
|
2019-12-20 03:18:08 +00:00
|
|
|
p2p: p,
|
2019-08-26 20:06:16 +00:00
|
|
|
chain: &mock.ChainService{
|
|
|
|
State: s,
|
|
|
|
},
|
2019-09-20 17:54:32 +00:00
|
|
|
initialSync: &mockSync.Sync{IsSyncing: false},
|
2019-08-18 15:33:58 +00:00
|
|
|
}
|
|
|
|
|
2019-12-20 03:18:08 +00:00
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
if _, err := p.Encoding().Encode(buf, exit); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
m := &pubsub.Message{
|
|
|
|
Message: &pubsubpb.Message{
|
|
|
|
Data: buf.Bytes(),
|
|
|
|
TopicIDs: []string{
|
|
|
|
p2p.GossipTypeMapping[reflect.TypeOf(exit)],
|
|
|
|
},
|
|
|
|
},
|
2019-10-01 15:13:04 +00:00
|
|
|
}
|
2019-12-20 03:18:08 +00:00
|
|
|
valid := r.validateVoluntaryExit(ctx, "", m)
|
2019-10-01 15:13:04 +00:00
|
|
|
if !valid {
|
2019-08-18 15:33:58 +00:00
|
|
|
t.Error("Failed validation")
|
|
|
|
}
|
|
|
|
|
2019-12-20 03:18:08 +00:00
|
|
|
if m.ValidatorData == nil {
|
|
|
|
t.Error("Decoded message was not set on the message validator data")
|
2019-08-18 15:33:58 +00:00
|
|
|
}
|
|
|
|
}
|
2019-09-04 00:22:15 +00:00
|
|
|
|
2019-12-20 03:18:08 +00:00
|
|
|
func TestValidateVoluntaryExit_ValidExit_Syncing(t *testing.T) {
|
|
|
|
p := p2ptest.NewTestP2P(t)
|
2019-09-04 00:22:15 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
exit, s := setupValidExit(t)
|
|
|
|
|
2019-12-17 01:53:55 +00:00
|
|
|
r := &Service{
|
2019-12-20 03:18:08 +00:00
|
|
|
p2p: p,
|
2019-09-04 00:22:15 +00:00
|
|
|
chain: &mock.ChainService{
|
|
|
|
State: s,
|
|
|
|
},
|
2019-12-20 03:18:08 +00:00
|
|
|
initialSync: &mockSync.Sync{IsSyncing: true},
|
2019-09-04 00:22:15 +00:00
|
|
|
}
|
2019-12-20 03:18:08 +00:00
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
if _, err := p.Encoding().Encode(buf, exit); err != nil {
|
|
|
|
t.Fatal(err)
|
2019-09-04 00:22:15 +00:00
|
|
|
}
|
2019-12-20 03:18:08 +00:00
|
|
|
m := &pubsub.Message{
|
|
|
|
Message: &pubsubpb.Message{
|
|
|
|
Data: buf.Bytes(),
|
|
|
|
TopicIDs: []string{
|
|
|
|
p2p.GossipTypeMapping[reflect.TypeOf(exit)],
|
|
|
|
},
|
2019-09-20 17:54:32 +00:00
|
|
|
},
|
|
|
|
}
|
2019-12-20 03:18:08 +00:00
|
|
|
valid := r.validateVoluntaryExit(ctx, "", m)
|
2019-10-01 15:13:04 +00:00
|
|
|
if valid {
|
2019-09-20 17:54:32 +00:00
|
|
|
t.Error("Validation should have failed")
|
|
|
|
}
|
|
|
|
}
|