mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2024-12-25 04:47:18 +00:00
Use deterministic method to create test deposits (#3639)
* Use deterministic method to create test deposits * More descriptive failure messages for tests
This commit is contained in:
parent
944d3b16fd
commit
628da919a4
@ -21,6 +21,8 @@ go_library(
|
||||
"//proto/beacon/p2p/v1:go_default_library",
|
||||
"//proto/eth/v1alpha1:go_default_library",
|
||||
"//shared/bls:go_default_library",
|
||||
"//shared/hashutil:go_default_library",
|
||||
"//shared/interop:go_default_library",
|
||||
"//shared/params:go_default_library",
|
||||
"//shared/trieutil:go_default_library",
|
||||
"@com_github_ghodss_yaml//:go_default_library",
|
||||
|
@ -1,7 +1,6 @@
|
||||
package testutil
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"encoding/binary"
|
||||
"sync"
|
||||
"testing"
|
||||
@ -12,6 +11,8 @@ import (
|
||||
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"
|
||||
"github.com/prysmaticlabs/prysm/shared/interop"
|
||||
"github.com/prysmaticlabs/prysm/shared/params"
|
||||
"github.com/prysmaticlabs/prysm/shared/trieutil"
|
||||
)
|
||||
@ -20,11 +21,16 @@ var lock sync.Mutex
|
||||
|
||||
// Caches
|
||||
var deposits []*ethpb.Deposit
|
||||
var depositDataRoots [][32]byte
|
||||
var privKeys []*bls.SecretKey
|
||||
var trie *trieutil.MerkleTrie
|
||||
|
||||
// SetupInitialDeposits prepares the entered amount of deposits
|
||||
// and secret keys.
|
||||
// SetupInitialDeposits prepares the entered amount of deposits,
|
||||
// deposit data roots, and secret keys.
|
||||
// The deposits are configured such that for deposit n the validator
|
||||
// account is key n and the withdrawal account is key n+1. As such,
|
||||
// if all secret keys for n validators are required then numDeposits
|
||||
// should be n+1
|
||||
func SetupInitialDeposits(t testing.TB, numDeposits uint64) ([]*ethpb.Deposit, []*bls.SecretKey) {
|
||||
lock.Lock()
|
||||
defer lock.Unlock()
|
||||
@ -39,31 +45,47 @@ func SetupInitialDeposits(t testing.TB, numDeposits uint64) ([]*ethpb.Deposit, [
|
||||
}
|
||||
}
|
||||
|
||||
// Extend caches as needed.
|
||||
for i := len(deposits); uint64(len(deposits)) < numDeposits; i++ {
|
||||
var withdrawalCreds [32]byte
|
||||
copy(withdrawalCreds[:], []byte("testing"))
|
||||
depositData := ðpb.Deposit_Data{
|
||||
Amount: params.BeaconConfig().MaxEffectiveBalance,
|
||||
WithdrawalCredentials: withdrawalCreds[:],
|
||||
}
|
||||
priv, err := bls.RandKey(rand.Reader)
|
||||
if numDeposits > uint64(len(deposits)) {
|
||||
// Additional deposits required
|
||||
numExisting := uint64(len(deposits))
|
||||
numRequired := numDeposits - uint64(len(deposits))
|
||||
// Fetch the required number of keys
|
||||
secretKeys, publicKeys, err := interop.DeterministicallyGenerateKeys(numExisting, numRequired+1)
|
||||
if err != nil {
|
||||
t.Fatalf("could not generate random key: %v", err)
|
||||
}
|
||||
privKeys = append(privKeys, priv)
|
||||
depositData.PublicKey = priv.PublicKey().Marshal()[:]
|
||||
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)
|
||||
}
|
||||
depositData.Signature = priv.Sign(root[:], domain).Marshal()
|
||||
deposit := ðpb.Deposit{
|
||||
Data: depositData,
|
||||
t.Fatalf("could not create deterministic keys: %v", err)
|
||||
}
|
||||
privKeys = append(privKeys, secretKeys[:len(secretKeys)-1]...)
|
||||
|
||||
deposits = append(deposits, deposit)
|
||||
// Create the deposits
|
||||
for i := uint64(0); i < numRequired; i++ {
|
||||
withdrawalCreds := hashutil.Hash(publicKeys[i+1].Marshal())
|
||||
withdrawalCreds[0] = params.BeaconConfig().BLSWithdrawalPrefixByte
|
||||
|
||||
depositData := ðpb.Deposit_Data{
|
||||
PublicKey: publicKeys[i].Marshal()[:],
|
||||
Amount: params.BeaconConfig().MaxEffectiveBalance,
|
||||
WithdrawalCredentials: withdrawalCreds[:],
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
depositData.Signature = secretKeys[i].Sign(root[:], domain).Marshal()
|
||||
|
||||
depositDataRoot, err := ssz.HashTreeRoot(depositData)
|
||||
if err != nil {
|
||||
t.Fatalf("could not get hash tree root of deposit data %v", err)
|
||||
}
|
||||
depositDataRoots = append(depositDataRoots, depositDataRoot)
|
||||
|
||||
deposit := ðpb.Deposit{
|
||||
Data: depositData,
|
||||
}
|
||||
|
||||
deposits = append(deposits, deposit)
|
||||
}
|
||||
}
|
||||
|
||||
d, _ := GenerateDepositProof(t, deposits[0:numDeposits])
|
||||
|
@ -12,6 +12,60 @@ import (
|
||||
"github.com/prysmaticlabs/prysm/shared/params"
|
||||
)
|
||||
|
||||
func TestSetupInitialDeposits(t *testing.T) {
|
||||
entries := 1
|
||||
deposits, privKeys := SetupInitialDeposits(t, uint64(entries))
|
||||
if len(deposits) != entries {
|
||||
t.Fatalf("incorrect number of deposits returned, wanted %d but received %d", entries, len(deposits))
|
||||
}
|
||||
if len(privKeys) != entries {
|
||||
t.Fatalf("incorrect number of private keys returned, wanted %d but received %d", entries, len(privKeys))
|
||||
}
|
||||
expectedPublicKeyAt0 := []byte{0xa9, 0x9a, 0x76, 0xed, 0x77, 0x96, 0xf7, 0xbe, 0x22, 0xd5, 0xb7, 0xe8, 0x5d, 0xee, 0xb7, 0xc5, 0x67, 0x7e, 0x88, 0xe5, 0x11, 0xe0, 0xb3, 0x37, 0x61, 0x8f, 0x8c, 0x4e, 0xb6, 0x13, 0x49, 0xb4, 0xbf, 0x2d, 0x15, 0x3f, 0x64, 0x9f, 0x7b, 0x53, 0x35, 0x9f, 0xe8, 0xb9, 0x4a, 0x38, 0xe4, 0x4c}
|
||||
if !bytes.Equal(deposits[0].Data.PublicKey, expectedPublicKeyAt0) {
|
||||
t.Fatalf("incorrect public key, wanted %x but received %x", expectedPublicKeyAt0, deposits[0].Data.PublicKey)
|
||||
}
|
||||
expectedWithdrawalCredentialsAt0 := []byte{0x00, 0xec, 0x7e, 0xf7, 0x78, 0x0c, 0x9d, 0x15, 0x15, 0x97, 0x92, 0x40, 0x36, 0x26, 0x2d, 0xd2, 0x8d, 0xc6, 0x0e, 0x12, 0x28, 0xf4, 0xda, 0x6f, 0xec, 0xf9, 0xd4, 0x02, 0xcb, 0x3f, 0x35, 0x94}
|
||||
if !bytes.Equal(deposits[0].Data.WithdrawalCredentials, expectedWithdrawalCredentialsAt0) {
|
||||
t.Fatalf("incorrect withdrawal credentials, wanted %x but received %x", expectedWithdrawalCredentialsAt0, deposits[0].Data.WithdrawalCredentials)
|
||||
}
|
||||
expectedSignatureAt0 := []byte{0xb3, 0xb9, 0x6e, 0xba, 0x50, 0xfa, 0x47, 0x49, 0x26, 0xfa, 0x46, 0xbb, 0xea, 0x3c, 0x8c, 0x73, 0x4c, 0x85, 0xc9, 0x70, 0x4e, 0x54, 0xb7, 0x19, 0xe5, 0x4e, 0x1b, 0xc5, 0x83, 0x77, 0xdd, 0x00, 0x30, 0x0b, 0x9e, 0xe4, 0xb0, 0x5b, 0xb2, 0x7b, 0x81, 0x8b, 0x38, 0xeb, 0xa2, 0x89, 0xcb, 0xe0, 0x06, 0x7a, 0x34, 0x56, 0xbc, 0xb8, 0xad, 0x59, 0xd0, 0x17, 0xfc, 0xf0, 0x04, 0xe5, 0xf1, 0xc5, 0xff, 0x1b, 0xf2, 0xe4, 0x89, 0x6b, 0x53, 0x2f, 0x4a, 0xea, 0x4b, 0x4c, 0x47, 0x06, 0x9a, 0x26, 0xe3, 0x85, 0x98, 0xf3, 0xd3, 0x37, 0x04, 0x7b, 0x8d, 0x0b, 0xd5, 0x25, 0xe4, 0x9f, 0xfc, 0xd2}
|
||||
if !bytes.Equal(deposits[0].Data.Signature, expectedSignatureAt0) {
|
||||
t.Fatalf("incorrect signature, wanted %x but received %x", expectedSignatureAt0, deposits[0].Data.Signature)
|
||||
}
|
||||
|
||||
entries = 1024
|
||||
deposits, privKeys = SetupInitialDeposits(t, uint64(entries))
|
||||
if len(deposits) != entries {
|
||||
t.Fatalf("incorrect number of deposits returned, wanted %d but received %d", entries, len(deposits))
|
||||
}
|
||||
if len(privKeys) != entries {
|
||||
t.Fatalf("incorrect number of private keys returned, wanted %d but received %d", entries, len(privKeys))
|
||||
}
|
||||
// Ensure 0 has not changed
|
||||
if !bytes.Equal(deposits[0].Data.PublicKey, expectedPublicKeyAt0) {
|
||||
t.Fatalf("incorrect public key, wanted %x but received %x", expectedPublicKeyAt0, deposits[0].Data.PublicKey)
|
||||
}
|
||||
if !bytes.Equal(deposits[0].Data.WithdrawalCredentials, expectedWithdrawalCredentialsAt0) {
|
||||
t.Fatalf("incorrect withdrawal credentials, wanted %x but received %x", expectedWithdrawalCredentialsAt0, deposits[0].Data.WithdrawalCredentials)
|
||||
}
|
||||
if !bytes.Equal(deposits[0].Data.Signature, expectedSignatureAt0) {
|
||||
t.Fatalf("incorrect signature, wanted %x but received %x", expectedSignatureAt0, deposits[0].Data.Signature)
|
||||
}
|
||||
expectedPublicKeyAt1023 := []byte{0x81, 0x2b, 0x93, 0x5e, 0xc8, 0x4b, 0x0e, 0x9a, 0x83, 0x95, 0x55, 0xaf, 0x33, 0x60, 0xca, 0xfb, 0x83, 0x1b, 0xd6, 0x12, 0xcf, 0xa2, 0x2e, 0x25, 0xea, 0xb0, 0x3c, 0xf5, 0xfd, 0xb0, 0x2a, 0xf5, 0x2b, 0xa4, 0x01, 0x7a, 0xee, 0xa8, 0x8a, 0x2f, 0x62, 0x2c, 0x78, 0x6e, 0x7f, 0x47, 0x6f, 0x4b}
|
||||
if !bytes.Equal(deposits[1023].Data.PublicKey, expectedPublicKeyAt1023) {
|
||||
t.Fatalf("incorrect public key, wanted %x but received %x", expectedPublicKeyAt1023, deposits[1023].Data.PublicKey)
|
||||
}
|
||||
expectedWithdrawalCredentialsAt1023 := []byte{0x00, 0x23, 0xd5, 0x76, 0xbc, 0x6c, 0x15, 0xdb, 0xc4, 0x34, 0x70, 0x1f, 0x3f, 0x41, 0xfd, 0x3e, 0x67, 0x59, 0xd2, 0xea, 0x7c, 0xdc, 0x64, 0x71, 0x0e, 0xe2, 0x8d, 0xde, 0xf7, 0xd2, 0xda, 0x28}
|
||||
if !bytes.Equal(deposits[1023].Data.WithdrawalCredentials, expectedWithdrawalCredentialsAt1023) {
|
||||
t.Fatalf("incorrect withdrawal credentials, wanted %x but received %x", expectedWithdrawalCredentialsAt1023, deposits[1023].Data.WithdrawalCredentials)
|
||||
}
|
||||
expectedSignatureAt1023 := []byte{0xa2, 0xad, 0x23, 0x3b, 0x6d, 0xa0, 0xd9, 0xf8, 0xb4, 0xac, 0xe0, 0xc9, 0xae, 0x25, 0x81, 0xfb, 0xca, 0x2d, 0x0a, 0xed, 0x6a, 0xdc, 0xd6, 0xda, 0x49, 0x0a, 0x75, 0xab, 0x3a, 0x3c, 0xc6, 0x37, 0xec, 0x65, 0xe3, 0x3d, 0xbc, 0x00, 0xad, 0xd8, 0x5f, 0x1e, 0x7b, 0x93, 0xcd, 0x63, 0x74, 0x8e, 0x0c, 0x28, 0x60, 0x4f, 0x99, 0x33, 0x6a, 0x29, 0x21, 0x57, 0xb6, 0xe0, 0x45, 0x9f, 0xaa, 0x10, 0xe9, 0x78, 0x02, 0x01, 0x68, 0x65, 0xcf, 0x6a, 0x4c, 0x2a, 0xd5, 0x5f, 0x37, 0xa1, 0x66, 0x05, 0x2b, 0x55, 0x86, 0xe7, 0x68, 0xb7, 0xfd, 0x76, 0xd5, 0x91, 0x3e, 0xeb, 0x6e, 0x46, 0x3f, 0x6d}
|
||||
if !bytes.Equal(deposits[1023].Data.Signature, expectedSignatureAt1023) {
|
||||
t.Fatalf("incorrect signature, wanted %x but received %x", expectedSignatureAt1023, deposits[1023].Data.Signature)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSignBlock(t *testing.T) {
|
||||
deposits, privKeys := SetupInitialDeposits(t, 100)
|
||||
validators := make([]*ethpb.Validator, len(deposits))
|
||||
|
Loading…
Reference in New Issue
Block a user