From f2a895f6df82667537132622ea49cd7dd8104eda Mon Sep 17 00:00:00 2001 From: Roman Behma <13855864+begmaroman@users.noreply.github.com> Date: Fri, 30 Sep 2022 15:16:15 +0100 Subject: [PATCH] EVM-50: Add --num feature to "secrets init" command (#770) --- command/secrets/init/params.go | 5 +- command/secrets/init/result.go | 14 +++++ command/secrets/init/secrets_init.go | 88 +++++++++++++++++++++++----- docker/local/polygon-edge.sh | 9 +-- 4 files changed, 91 insertions(+), 25 deletions(-) diff --git a/command/secrets/init/params.go b/command/secrets/init/params.go index eae48dbf58..59ba57baca 100644 --- a/command/secrets/init/params.go +++ b/command/secrets/init/params.go @@ -14,10 +14,7 @@ const ( ecdsaFlag = "ecdsa" blsFlag = "bls" networkFlag = "network" -) - -var ( - params = &initParams{} + numFlag = "num" ) var ( diff --git a/command/secrets/init/result.go b/command/secrets/init/result.go index 18e7d2a2ca..927dcd87f9 100644 --- a/command/secrets/init/result.go +++ b/command/secrets/init/result.go @@ -4,10 +4,24 @@ import ( "bytes" "fmt" + "github.com/0xPolygon/polygon-edge/command" + "github.com/0xPolygon/polygon-edge/command/helper" "github.com/0xPolygon/polygon-edge/types" ) +type Results []command.CommandResult + +func (r Results) GetOutput() string { + var buffer bytes.Buffer + + for _, result := range r { + buffer.WriteString(result.GetOutput()) + } + + return buffer.String() +} + type SecretsInitResult struct { Address types.Address `json:"address"` BLSPubkey string `json:"bls_pubkey"` diff --git a/command/secrets/init/secrets_init.go b/command/secrets/init/secrets_init.go index 8e2ac9fe59..5f91cca42f 100644 --- a/command/secrets/init/secrets_init.go +++ b/command/secrets/init/secrets_init.go @@ -1,8 +1,23 @@ package init import ( - "github.com/0xPolygon/polygon-edge/command" + "fmt" + "github.com/spf13/cobra" + + "github.com/0xPolygon/polygon-edge/command" +) + +const ( + // maxInitNum is the maximum value for "num" flag + maxInitNum = 30 +) + +var ( + errInvalidNum = fmt.Errorf("num flag value should be between 1 and %d", maxInitNum) + + basicParams initParams + initNumber int ) func GetCommand() *cobra.Command { @@ -21,38 +36,50 @@ func GetCommand() *cobra.Command { func setFlags(cmd *cobra.Command) { cmd.Flags().StringVar( - ¶ms.dataDir, + &basicParams.dataDir, dataDirFlag, "", "the directory for the Polygon Edge data if the local FS is used", ) cmd.Flags().StringVar( - ¶ms.configPath, + &basicParams.configPath, configFlag, "", "the path to the SecretsManager config file, "+ "if omitted, the local FS secrets manager is used", ) + cmd.Flags().IntVar( + &initNumber, + numFlag, + 1, + "the flag indicating how many secrets should be created, only for the local FS", + ) + + // Don't accept data-dir and config flags because they are related to different secrets managers. + // data-dir is about the local FS as secrets storage, config is about remote secrets manager. cmd.MarkFlagsMutuallyExclusive(dataDirFlag, configFlag) + // num flag should be used with data-dir flag only so it should not be used with config flag. + cmd.MarkFlagsMutuallyExclusive(numFlag, configFlag) + cmd.Flags().BoolVar( - ¶ms.generatesECDSA, + &basicParams.generatesECDSA, ecdsaFlag, true, "the flag indicating whether new ECDSA key is created", ) cmd.Flags().BoolVar( - ¶ms.generatesNetwork, + &basicParams.generatesNetwork, networkFlag, true, "the flag indicating whether new Network key is created", ) cmd.Flags().BoolVar( - ¶ms.generatesBLS, + &basicParams.generatesBLS, blsFlag, true, "the flag indicating whether new BLS key is created", @@ -60,25 +87,56 @@ func setFlags(cmd *cobra.Command) { } func runPreRun(_ *cobra.Command, _ []string) error { - return params.validateFlags() + if initNumber < 1 || initNumber > maxInitNum { + return errInvalidNum + } + + return basicParams.validateFlags() } func runCommand(cmd *cobra.Command, _ []string) { outputter := command.InitializeOutputter(cmd) defer outputter.WriteOutput() - if err := params.initSecrets(); err != nil { - outputter.SetError(err) + paramsList := newParamsList(basicParams, initNumber) + results := make(Results, len(paramsList)) + + for i, params := range paramsList { + if err := params.initSecrets(); err != nil { + outputter.SetError(err) + + return + } - return + res, err := params.getResult() + if err != nil { + outputter.SetError(err) + + return + } + + results[i] = res } - res, err := params.getResult() - if err != nil { - outputter.SetError(err) + outputter.SetCommandResult(results) +} + +// newParamsList creates a list of initParams with num elements. +// This function basically copies the given initParams but updating dataDir by applying an index. +func newParamsList(params initParams, num int) []initParams { + if num == 1 { + return []initParams{params} + } - return + paramsList := make([]initParams, num) + for i := 1; i <= num; i++ { + paramsList[i-1] = initParams{ + dataDir: fmt.Sprintf("%s%d", params.dataDir, i), + generatesECDSA: params.generatesECDSA, + generatesBLS: params.generatesBLS, + generatesNetwork: params.generatesNetwork, + } } - outputter.SetCommandResult(res) + return paramsList } diff --git a/docker/local/polygon-edge.sh b/docker/local/polygon-edge.sh index 1b11b2fb6e..7073b39d3b 100755 --- a/docker/local/polygon-edge.sh +++ b/docker/local/polygon-edge.sh @@ -8,10 +8,7 @@ case "$1" in "init") echo "Generating secrets..." - node1id=$("$POLYGON_EDGE_BIN" secrets init --data-dir data-1 --json | jq -r '.node_id') - node2id=$("$POLYGON_EDGE_BIN" secrets init --data-dir data-2 --json | jq -r '.node_id') - "$POLYGON_EDGE_BIN" secrets init --data-dir data-3 - "$POLYGON_EDGE_BIN" secrets init --data-dir data-4 + secrets=$("$POLYGON_EDGE_BIN" secrets init --num 4 --data-dir data- --json) echo "Secrets have been successfully generated" echo "Generating genesis file..." @@ -19,8 +16,8 @@ case "$1" in --dir /genesis/genesis.json \ --consensus ibft \ --ibft-validators-prefix-path data- \ - --bootnode /dns4/node-1/tcp/1478/p2p/"$node1id" \ - --bootnode /dns4/node-2/tcp/1478/p2p/"$node2id" + --bootnode /dns4/node-1/tcp/1478/p2p/$(echo $secrets | jq -r '.[0] | .node_id') \ + --bootnode /dns4/node-2/tcp/1478/p2p/$(echo $secrets | jq -r '.[1] | .node_id') echo "Genesis file has been successfully generated" ;;