prysm-pulse/cmd/prysmctl/testnet/generate_genesis_test.go
Raul Jordan 20ed47a107
Add a Generate Genesis State Command to Prysmctl (#11259)
* genesis tool

* done with tool

* delete old tool

* no fatal

* fix up

* Update cmd/prysmctl/testnet/generate_genesis.go

Co-authored-by: Radosław Kapka <rkapka@wp.pl>

* radek feedback

* more feedback

* required

* fix up

* gaz

* radek feedback

Co-authored-by: Radosław Kapka <rkapka@wp.pl>
2022-08-19 09:26:25 +00:00

56 lines
1.7 KiB
Go

package testnet
import (
"bytes"
"context"
"encoding/json"
"fmt"
"testing"
"github.com/prysmaticlabs/prysm/v3/crypto/bls"
"github.com/prysmaticlabs/prysm/v3/runtime/interop"
"github.com/prysmaticlabs/prysm/v3/testing/assert"
"github.com/prysmaticlabs/prysm/v3/testing/require"
)
func Test_genesisStateFromJSONValidators(t *testing.T) {
numKeys := 5
jsonData := createGenesisDepositData(t, numKeys)
jsonInput, err := json.Marshal(jsonData)
require.NoError(t, err)
ctx := context.Background()
genesisState, err := genesisStateFromJSONValidators(
ctx, bytes.NewReader(jsonInput), 0, /* genesis time defaults to time.Now() */
)
require.NoError(t, err)
for i, val := range genesisState.Validators {
assert.DeepEqual(t, fmt.Sprintf("%#x", val.PublicKey), jsonData[i].PubKey)
}
}
func createGenesisDepositData(t *testing.T, numKeys int) []*depositDataJSON {
pubKeys := make([]bls.PublicKey, numKeys)
privKeys := make([]bls.SecretKey, numKeys)
for i := 0; i < numKeys; i++ {
randKey, err := bls.RandKey()
require.NoError(t, err)
privKeys[i] = randKey
pubKeys[i] = randKey.PublicKey()
}
dataList, _, err := interop.DepositDataFromKeys(privKeys, pubKeys)
require.NoError(t, err)
jsonData := make([]*depositDataJSON, numKeys)
for i := 0; i < numKeys; i++ {
dataRoot, err := dataList[i].HashTreeRoot()
require.NoError(t, err)
jsonData[i] = &depositDataJSON{
PubKey: fmt.Sprintf("%#x", dataList[i].PublicKey),
Amount: dataList[i].Amount,
WithdrawalCredentials: fmt.Sprintf("%#x", dataList[i].WithdrawalCredentials),
DepositDataRoot: fmt.Sprintf("%#x", dataRoot),
Signature: fmt.Sprintf("%#x", dataList[i].Signature),
}
}
return jsonData
}