Remove Optional Signature Verification for VoluntaryExit and BlockHeader (#3053)

* Remove verifySignatures from ProposerSlashings

* Remove flag from process transfers

* resolve all conflicts

* fix more references to old pbs

* Fix merge conflicts

* Remove verifySignature flag from ProcessBlockHeader

* fx spectest

* Fix test errors

* Fix tests

* Fix tests

* Goimports

* Fix test finally

* Move test helpers to testutil

* Goimports

* Fix imports

* Add tests for new helpers

* Run gazelle

* Fix tests
This commit is contained in:
Ivan Martinez
2019-07-25 13:53:46 -04:00
committed by GitHub
parent 59253afb96
commit e452b46873
19 changed files with 376 additions and 155 deletions

View File

@@ -14,6 +14,8 @@ go_library(
importpath = "github.com/prysmaticlabs/prysm/shared/testutil",
visibility = ["//visibility:public"],
deps = [
"//beacon-chain/core/helpers:go_default_library",
"//proto/beacon/p2p/v1:go_default_library",
"//proto/eth/v1alpha1:go_default_library",
"//shared/bls:go_default_library",
"//shared/hashutil:go_default_library",
@@ -29,7 +31,17 @@ go_library(
go_test(
name = "go_default_test",
srcs = ["json_to_pb_converter_test.go"],
srcs = [
"helpers_test.go",
"json_to_pb_converter_test.go",
],
embed = [":go_default_library"],
deps = ["//proto/testing:go_default_library"],
deps = [
"//beacon-chain/core/helpers:go_default_library",
"//proto/beacon/p2p/v1:go_default_library",
"//proto/eth/v1alpha1:go_default_library",
"//proto/testing:go_default_library",
"//shared/params:go_default_library",
"@com_github_prysmaticlabs_go_ssz//:go_default_library",
],
)

View File

@@ -2,10 +2,14 @@ package testutil
import (
"crypto/rand"
"encoding/binary"
"fmt"
"sync"
"testing"
"github.com/prysmaticlabs/go-ssz"
"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers"
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/hashutil"
@@ -106,6 +110,41 @@ func GenerateEth1Data(t testing.TB, deposits []*ethpb.Deposit) *ethpb.Eth1Data {
return eth1Data
}
// SignBlock generates a signed block using the block slot and the beacon proposer priv key.
func SignBlock(beaconState *pb.BeaconState, block *ethpb.BeaconBlock, privKeys []*bls.SecretKey) (*ethpb.BeaconBlock, error) {
slot := beaconState.Slot
beaconState.Slot = block.Slot
proposerIdx, err := helpers.BeaconProposerIndex(beaconState)
if err != nil {
return nil, err
}
beaconState.Slot = slot
signingRoot, err := ssz.SigningRoot(block)
if err != nil {
return nil, err
}
epoch := helpers.SlotToEpoch(block.Slot)
domain := helpers.Domain(beaconState, epoch, params.BeaconConfig().DomainBeaconProposer)
blockSig := privKeys[proposerIdx].Sign(signingRoot[:], domain).Marshal()
block.Signature = blockSig[:]
return block, nil
}
// CreateRandaoReveal generates a epoch signature using the beacon proposer priv key.
func CreateRandaoReveal(beaconState *pb.BeaconState, epoch uint64, privKeys []*bls.SecretKey) ([]byte, error) {
// We fetch the proposer's index as that is whom the RANDAO will be verified against.
proposerIdx, err := helpers.BeaconProposerIndex(beaconState)
if err != nil {
return []byte{}, fmt.Errorf("could not get beacon proposer index: %v", err)
}
buf := make([]byte, 32)
binary.LittleEndian.PutUint64(buf, epoch)
domain := helpers.Domain(beaconState, epoch, params.BeaconConfig().DomainRandao)
// We make the previous validator's index sign the message instead of the proposer.
epochSignature := privKeys[proposerIdx].Sign(buf, domain)
return epochSignature.Marshal(), nil
}
// ResetCache clears out the old trie, private keys and deposits.
func ResetCache() {
trie = nil

View File

@@ -0,0 +1,102 @@
package testutil
import (
"bytes"
"encoding/binary"
"testing"
"github.com/prysmaticlabs/go-ssz"
"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers"
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
ethpb "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1"
"github.com/prysmaticlabs/prysm/shared/params"
)
func TestSignBlock(t *testing.T) {
deposits, privKeys := SetupInitialDeposits(t, 100)
validators := make([]*ethpb.Validator, len(deposits))
for i := 0; i < len(validators); i++ {
validators[i] = &ethpb.Validator{
ExitEpoch: params.BeaconConfig().FarFutureEpoch,
PublicKey: privKeys[i].PublicKey().Marshal()[:],
}
}
beaconState := &pb.BeaconState{
Slot: 0,
Fork: &pb.Fork{
CurrentVersion: params.BeaconConfig().GenesisForkVersion,
PreviousVersion: params.BeaconConfig().GenesisForkVersion,
},
Validators: validators,
RandaoMixes: make([][]byte, params.BeaconConfig().EpochsPerHistoricalVector),
ActiveIndexRoots: make([][]byte, params.BeaconConfig().EpochsPerHistoricalVector),
}
block := &ethpb.BeaconBlock{
Slot: 0,
ParentRoot: []byte{0xC0},
}
proposerIdx, err := helpers.BeaconProposerIndex(beaconState)
if err != nil {
t.Error(err)
}
signingRoot, err := ssz.SigningRoot(block)
if err != nil {
t.Error(err)
}
epoch := helpers.SlotToEpoch(block.Slot)
domain := helpers.Domain(beaconState, epoch, params.BeaconConfig().DomainBeaconProposer)
blockSig := privKeys[proposerIdx].Sign(signingRoot[:], domain).Marshal()
signedBlock, err := SignBlock(beaconState, block, privKeys)
if err != nil {
t.Error(err)
}
if !bytes.Equal(blockSig[:], signedBlock.Signature) {
t.Errorf("Expected block signatures to be equal, received %#x != %#x", blockSig[:], signedBlock.Signature)
}
}
func TestCreateRandaoReveal(t *testing.T) {
deposits, privKeys := SetupInitialDeposits(t, 100)
validators := make([]*ethpb.Validator, len(deposits))
for i := 0; i < len(validators); i++ {
validators[i] = &ethpb.Validator{
ExitEpoch: params.BeaconConfig().FarFutureEpoch,
PublicKey: privKeys[i].PublicKey().Marshal()[:],
}
}
beaconState := &pb.BeaconState{
Slot: 0,
Fork: &pb.Fork{
CurrentVersion: params.BeaconConfig().GenesisForkVersion,
PreviousVersion: params.BeaconConfig().GenesisForkVersion,
},
Validators: validators,
RandaoMixes: make([][]byte, params.BeaconConfig().EpochsPerHistoricalVector),
ActiveIndexRoots: make([][]byte, params.BeaconConfig().EpochsPerHistoricalVector),
}
epoch := helpers.CurrentEpoch(beaconState)
randaoReveal, err := CreateRandaoReveal(beaconState, epoch, privKeys)
if err != nil {
t.Error(err)
}
proposerIdx, err := helpers.BeaconProposerIndex(beaconState)
if err != nil {
t.Error(err)
}
buf := make([]byte, 32)
binary.LittleEndian.PutUint64(buf, epoch)
domain := helpers.Domain(beaconState, epoch, params.BeaconConfig().DomainRandao)
// We make the previous validator's index sign the message instead of the proposer.
epochSignature := privKeys[proposerIdx].Sign(buf, domain).Marshal()
if !bytes.Equal(randaoReveal[:], epochSignature[:]) {
t.Errorf("Expected randao reveals to be equal, received %#x != %#x", randaoReveal[:], epochSignature[:])
}
}