Skip to content

Commit

Permalink
Use Path to Deposit Data JSON File in Generate Genesis State (#8575)
Browse files Browse the repository at this point in the history
* use path to deposit data json

* gaz

Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com>
  • Loading branch information
rauljordan and prylabs-bulldozer[bot] authored Mar 8, 2021
1 parent 0f1d144 commit 4a98300
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 40 deletions.
1 change: 0 additions & 1 deletion tools/genesis-state-gen/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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",
],
)
75 changes: 47 additions & 28 deletions tools/genesis-state-gen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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)
Expand Down Expand Up @@ -133,33 +135,50 @@ 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 := &ethpb.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 {
return nil, err
}
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 = &ethpb.Deposit_Data{
PublicKey: pubKeyBytes,
WithdrawalCredentials: withdrawalbytes,
Amount: input.Amount,
Signature: signatureBytes,
}
dataRoot = dataRootBytes
return
}
24 changes: 13 additions & 11 deletions tools/genesis-state-gen/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,27 @@ 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"
"github.com/prysmaticlabs/prysm/shared/testutil/require"
)

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(
bytes.NewReader(jsonInput), 0, /* genesis time defaults to time.Now() */
)
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++ {
Expand All @@ -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
}

0 comments on commit 4a98300

Please sign in to comment.