prysm-pulse/shared/testutil/helpers.go
terence tsao 1b5b8a57e0 Remove unused proto schemas (#3005)
* Update io_kubernetes_build commit hash to 1246899

* Update dependency build_bazel_rules_nodejs to v0.33.1

* Update dependency com_github_hashicorp_golang_lru to v0.5.1

* Update libp2p

* Update io_bazel_rules_k8s commit hash to e68d5d7

* Starting to remove old protos

* Bazel build proto passes

* Fixing pb version

* Cleaned up core package

* Fixing tests

* 6 tests failing

* Update proto bugs

* Fixed incorrect validator ordering proto

* Sync with master

* Update go-ssz commit

* Removed bad copies from v1alpha1 folder

* add json spec json to pb handler

* add nested proto example

* proto/testing test works

* fix refactoring build failures

* use merged ssz

* push latest changes

* used forked json encoding

* used forked json encoding

* fix warning

* fix build issues

* fix test and lint

* fix build

* lint
2019-07-22 10:03:57 -04:00

100 lines
3.1 KiB
Go

package testutil
import (
"crypto/rand"
"strconv"
"testing"
"github.com/prysmaticlabs/go-ssz"
ethpb "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1"
"github.com/prysmaticlabs/prysm/shared/bls"
"github.com/prysmaticlabs/prysm/shared/hashutil"
"github.com/prysmaticlabs/prysm/shared/params"
"github.com/prysmaticlabs/prysm/shared/trieutil"
)
// SetupInitialDeposits prepares the entered amount of deposits
// and secret keys.
func SetupInitialDeposits(t testing.TB, numDeposits uint64, generateKeys bool) ([]*ethpb.Deposit, []*bls.SecretKey) {
privKeys := make([]*bls.SecretKey, numDeposits)
deposits := make([]*ethpb.Deposit, numDeposits)
for i := 0; i < len(deposits); i++ {
pubkey := []byte{}
var sig [96]byte
var withdrawalCreds [32]byte
copy(withdrawalCreds[:], []byte("testing"))
depositData := &ethpb.Deposit_Data{
Amount: params.BeaconConfig().MaxEffectiveBalance,
WithdrawalCredentials: withdrawalCreds[:],
}
if generateKeys {
priv, err := bls.RandKey(rand.Reader)
if err != nil {
t.Fatalf("could not generate random key: %v", err)
}
privKeys[i] = priv
pubkey = priv.PublicKey().Marshal()
depositData.PublicKey = pubkey
domain := bls.Domain(params.BeaconConfig().DomainDeposit, params.BeaconConfig().GenesisForkVersion)
root, err := ssz.SigningRoot(depositData)
if err != nil {
t.Fatalf("could not get signing root of deposit data %v", err)
}
marshalledSig := priv.Sign(root[:], domain).Marshal()
copy(sig[:], marshalledSig)
depositData.Signature = sig[:]
} else {
privKeys = []*bls.SecretKey{}
pubkey = make([]byte, params.BeaconConfig().BLSPubkeyLength)
copy(pubkey[:], []byte(strconv.FormatUint(uint64(i), 10)))
copy(sig[:], []byte("testing"))
depositData.PublicKey = pubkey
depositData.Signature = sig[:]
}
deposits[i] = &ethpb.Deposit{
Data: depositData,
}
}
deposits, _ = GenerateDepositProof(t, deposits)
return deposits, privKeys
}
// GenerateDepositProof takes an array of deposits and generates the deposit trie for them and proofs.
func GenerateDepositProof(t testing.TB, deposits []*ethpb.Deposit) ([]*ethpb.Deposit, [32]byte) {
encodedDeposits := make([][]byte, len(deposits))
for i := 0; i < len(encodedDeposits); i++ {
hashedDeposit, err := hashutil.DepositHash(deposits[i].Data)
if err != nil {
t.Fatalf("could not tree hash deposit data: %v", err)
}
encodedDeposits[i] = hashedDeposit[:]
}
depositTrie, err := trieutil.GenerateTrieFromItems(encodedDeposits, int(params.BeaconConfig().DepositContractTreeDepth))
if err != nil {
t.Fatalf("Could not generate deposit trie: %v", err)
}
for i := range deposits {
proof, err := depositTrie.MerkleProof(int(i))
if err != nil {
t.Fatalf("Could not generate proof: %v", err)
}
deposits[i].Proof = proof
}
root := depositTrie.Root()
return deposits, root
}
// GenerateEth1Data takes an array of deposits and generates the deposit trie for them.
func GenerateEth1Data(t testing.TB, deposits []*ethpb.Deposit) *ethpb.Eth1Data {
_, root := GenerateDepositProof(t, deposits)
eth1Data := &ethpb.Eth1Data{
BlockHash: root[:],
DepositRoot: root[:],
}
return eth1Data
}