mirror of
https://github.com/OffchainLabs/prysm.git
synced 2026-01-08 23:18:15 -05:00
Use Path to Deposit Data JSON File in Generate Genesis State (#8575)
* use path to deposit data json * gaz Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com>
This commit is contained in:
@@ -61,6 +61,5 @@ go_test(
|
||||
"//shared/interop:go_default_library",
|
||||
"//shared/testutil/assert:go_default_library",
|
||||
"//shared/testutil/require:go_default_library",
|
||||
"@com_github_prysmaticlabs_ethereumapis//eth/v1alpha1:go_default_library",
|
||||
],
|
||||
)
|
||||
|
||||
@@ -18,19 +18,21 @@ import (
|
||||
"github.com/prysmaticlabs/prysm/shared/params"
|
||||
)
|
||||
|
||||
// GenesisValidator struct representing JSON input we can accept to generate
|
||||
// a genesis state from, containing a validator's full deposit data as a hex string.
|
||||
type GenesisValidator struct {
|
||||
DepositData string `json:"deposit_data"`
|
||||
// DepositDataJSON representing a json object of hex string and uint64 values for
|
||||
// validators on eth2. This file can be generated using the official eth2.0-deposit-cli.
|
||||
type DepositDataJSON struct {
|
||||
PubKey string `json:"pubkey"`
|
||||
Amount uint64 `json:"amount"`
|
||||
WithdrawalCredentials string `json:"withdrawal_credentials"`
|
||||
DepositDataRoot string `json:"deposit_data_root"`
|
||||
Signature string `json:"signature"`
|
||||
}
|
||||
|
||||
var (
|
||||
validatorJSONInput = flag.String(
|
||||
"validator-json-file",
|
||||
depositJSONFile = flag.String(
|
||||
"deposit-json-file",
|
||||
"",
|
||||
"Path to JSON file formatted as a list of hex public keys and their corresponding deposit data as hex"+
|
||||
" such as [ { public_key: '0x1', deposit_data: '0x2' }, ... ]"+
|
||||
" this file will be used for generating a genesis state from a list of specified validator public keys",
|
||||
"Path to deposit_data.json file generated by the eth2.0-deposit-cli tool",
|
||||
)
|
||||
numValidators = flag.Int("num-validators", 0, "Number of validators to deterministically generate in the generated genesis state")
|
||||
useMainnetConfig = flag.Bool("mainnet-config", false, "Select whether genesis state should be generated with mainnet or minimal (default) params")
|
||||
@@ -54,8 +56,8 @@ func main() {
|
||||
}
|
||||
var genesisState *pb.BeaconState
|
||||
var err error
|
||||
if *validatorJSONInput != "" {
|
||||
inputFile := *validatorJSONInput
|
||||
if *depositJSONFile != "" {
|
||||
inputFile := *depositJSONFile
|
||||
expanded, err := fileutil.ExpandPath(inputFile)
|
||||
if err != nil {
|
||||
log.Printf("Could not expand file path %s: %v", inputFile, err)
|
||||
@@ -133,29 +135,19 @@ func genesisStateFromJSONValidators(r io.Reader, genesisTime uint64) (*pb.Beacon
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var validatorsJSON []*GenesisValidator
|
||||
if err := json.Unmarshal(enc, &validatorsJSON); err != nil {
|
||||
var depositJSON []*DepositDataJSON
|
||||
if err := json.Unmarshal(enc, &depositJSON); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
depositDataList := make([]*ethpb.Deposit_Data, len(validatorsJSON))
|
||||
depositDataRoots := make([][]byte, len(validatorsJSON))
|
||||
for i, val := range validatorsJSON {
|
||||
depositDataString := val.DepositData
|
||||
depositDataString = strings.TrimPrefix(depositDataString, "0x")
|
||||
depositDataHex, err := hex.DecodeString(depositDataString)
|
||||
depositDataList := make([]*ethpb.Deposit_Data, len(depositJSON))
|
||||
depositDataRoots := make([][]byte, len(depositJSON))
|
||||
for i, val := range depositJSON {
|
||||
data, dataRootBytes, err := depositJSONToDepositData(val)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
data := ðpb.Deposit_Data{}
|
||||
if err := data.UnmarshalSSZ(depositDataHex); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
depositDataList[i] = data
|
||||
root, err := data.HashTreeRoot()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
depositDataRoots[i] = root[:]
|
||||
depositDataRoots[i] = dataRootBytes
|
||||
}
|
||||
beaconState, _, err := interop.GenerateGenesisStateFromDepositData(genesisTime, depositDataList, depositDataRoots)
|
||||
if err != nil {
|
||||
@@ -163,3 +155,30 @@ func genesisStateFromJSONValidators(r io.Reader, genesisTime uint64) (*pb.Beacon
|
||||
}
|
||||
return beaconState, nil
|
||||
}
|
||||
|
||||
func depositJSONToDepositData(input *DepositDataJSON) (depositData *ethpb.Deposit_Data, dataRoot []byte, err error) {
|
||||
pubKeyBytes, err := hex.DecodeString(strings.TrimPrefix(input.PubKey, "0x"))
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
withdrawalbytes, err := hex.DecodeString(strings.TrimPrefix(input.WithdrawalCredentials, "0x"))
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
signatureBytes, err := hex.DecodeString(strings.TrimPrefix(input.Signature, "0x"))
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
dataRootBytes, err := hex.DecodeString(strings.TrimPrefix(input.DepositDataRoot, "0x"))
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
depositData = ðpb.Deposit_Data{
|
||||
PublicKey: pubKeyBytes,
|
||||
WithdrawalCredentials: withdrawalbytes,
|
||||
Amount: input.Amount,
|
||||
Signature: signatureBytes,
|
||||
}
|
||||
dataRoot = dataRootBytes
|
||||
return
|
||||
}
|
||||
|
||||
@@ -6,7 +6,6 @@ import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
|
||||
"github.com/prysmaticlabs/prysm/shared/bls"
|
||||
"github.com/prysmaticlabs/prysm/shared/interop"
|
||||
"github.com/prysmaticlabs/prysm/shared/testutil/assert"
|
||||
@@ -14,7 +13,8 @@ import (
|
||||
)
|
||||
|
||||
func Test_genesisStateFromJSONValidators(t *testing.T) {
|
||||
jsonData, depositDataList := createGenesisDepositData(t)
|
||||
numKeys := 5
|
||||
jsonData := createGenesisDepositData(t, numKeys)
|
||||
jsonInput, err := json.Marshal(jsonData)
|
||||
require.NoError(t, err)
|
||||
genesisState, err := genesisStateFromJSONValidators(
|
||||
@@ -22,12 +22,11 @@ func Test_genesisStateFromJSONValidators(t *testing.T) {
|
||||
)
|
||||
require.NoError(t, err)
|
||||
for i, val := range genesisState.Validators {
|
||||
assert.DeepEqual(t, val.PublicKey, depositDataList[i].PublicKey)
|
||||
assert.DeepEqual(t, fmt.Sprintf("%#x", val.PublicKey), jsonData[i].PubKey)
|
||||
}
|
||||
}
|
||||
|
||||
func createGenesisDepositData(t *testing.T) ([]*GenesisValidator, []*ethpb.Deposit_Data) {
|
||||
numKeys := 5
|
||||
func createGenesisDepositData(t *testing.T, numKeys int) []*DepositDataJSON {
|
||||
pubKeys := make([]bls.PublicKey, numKeys)
|
||||
privKeys := make([]bls.SecretKey, numKeys)
|
||||
for i := 0; i < numKeys; i++ {
|
||||
@@ -38,14 +37,17 @@ func createGenesisDepositData(t *testing.T) ([]*GenesisValidator, []*ethpb.Depos
|
||||
}
|
||||
dataList, _, err := interop.DepositDataFromKeys(privKeys, pubKeys)
|
||||
require.NoError(t, err)
|
||||
jsonData := make([]*GenesisValidator, numKeys)
|
||||
jsonData := make([]*DepositDataJSON, numKeys)
|
||||
for i := 0; i < numKeys; i++ {
|
||||
data := dataList[i]
|
||||
enc, err := data.MarshalSSZ()
|
||||
dataRoot, err := dataList[i].HashTreeRoot()
|
||||
require.NoError(t, err)
|
||||
jsonData[i] = &GenesisValidator{
|
||||
DepositData: fmt.Sprintf("%#x", enc),
|
||||
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, dataList
|
||||
return jsonData
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user