prysm-pulse/beacon-chain/sync/validate_voluntary_exit_test.go

129 lines
3.2 KiB
Go
Raw Normal View History

package sync
import (
"bytes"
"context"
"crypto/rand"
"reflect"
"testing"
pubsub "github.com/libp2p/go-libp2p-pubsub"
pubsubpb "github.com/libp2p/go-libp2p-pubsub/pb"
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
"github.com/prysmaticlabs/go-ssz"
2019-08-27 13:27:04 +00:00
mock "github.com/prysmaticlabs/prysm/beacon-chain/blockchain/testing"
"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers"
"github.com/prysmaticlabs/prysm/beacon-chain/p2p"
p2ptest "github.com/prysmaticlabs/prysm/beacon-chain/p2p/testing"
mockSync "github.com/prysmaticlabs/prysm/beacon-chain/sync/initial-sync/testing"
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
"github.com/prysmaticlabs/prysm/shared/bls"
"github.com/prysmaticlabs/prysm/shared/params"
)
func setupValidExit(t *testing.T) (*ethpb.VoluntaryExit, *pb.BeaconState) {
exit := &ethpb.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.Fork, helpers.CurrentEpoch(state), params.BeaconConfig().DomainVoluntaryExit)
Change BLS to Herumi Again (#4181) * change to herumi's bls * change alias * change to better * add benchmark * build * change to bazel fork * fix prefix * Merge branch 'master' of https://github.com/prysmaticlabs/geth-sharding into herumiBLS * make it work with library * update to latest * change again * add import * update to latest * add sha commit * new static lib with groups swapped * using herumis new lib * fix dep paths in c headers * update again * new changes * fix commit * Merge branch 'master' of https://github.com/prysmaticlabs/geth-sharding into herumiBLS * fix serialization * comment * fix test * Merge branch 'master' of https://github.com/prysmaticlabs/geth-sharding into herumiBLS * fix to herumis latest version * fix test * fix benchmarks * Merge branch 'master' of https://github.com/prysmaticlabs/geth-sharding into herumiBLS * Merge branch 'master' of https://github.com/prysmaticlabs/geth-sharding into herumiBLS * add new workspace * change commit and remove init * get test to pass * remove parameter * remove reverse byte order * make gazelle happy * set pure to off * fix failing tests * Merge branch 'master' into herumiBLS * Merge branch 'master' of https://github.com/prysmaticlabs/geth-sharding into herumiBLS * Merge branch 'herumiBLS' of https://github.com/prysmaticlabs/geth-sharding into herumiBLS * remove old ref * use HashWithDomain functions * update to latest version * clean up * gaz * add back removed code * switch off pure * Merge branch 'master' of https://github.com/prysmaticlabs/geth-sharding into herumiBLS * use local repo * resolve docker issues * Merge branch 'master' of https://github.com/prysmaticlabs/geth-sharding into herumiBLS * fix build and tests * gaz * Merge branch 'master' into herumiBLS * Merge refs/heads/master into herumiBLS * Merge refs/heads/master into herumiBLS
2019-12-03 20:29:05 +00:00
priv := bls.RandKey()
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)
}
return exit, state
}
func TestValidateVoluntaryExit_ValidExit(t *testing.T) {
p := p2ptest.NewTestP2P(t)
ctx := context.Background()
exit, s := setupValidExit(t)
2019-12-17 01:53:55 +00:00
r := &Service{
p2p: p,
chain: &mock.ChainService{
State: s,
},
initialSync: &mockSync.Sync{IsSyncing: false},
}
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)],
},
},
}
valid := r.validateVoluntaryExit(ctx, "", m)
if !valid {
t.Error("Failed validation")
}
if m.ValidatorData == nil {
t.Error("Decoded message was not set on the message validator data")
}
}
func TestValidateVoluntaryExit_ValidExit_Syncing(t *testing.T) {
p := p2ptest.NewTestP2P(t)
ctx := context.Background()
exit, s := setupValidExit(t)
2019-12-17 01:53:55 +00:00
r := &Service{
p2p: p,
chain: &mock.ChainService{
State: s,
},
initialSync: &mockSync.Sync{IsSyncing: true},
}
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)],
},
},
}
valid := r.validateVoluntaryExit(ctx, "", m)
if valid {
t.Error("Validation should have failed")
}
}