Skip to content

Commit

Permalink
EVM-50: Add --num feature to "secrets init" command (0xPolygon#770)
Browse files Browse the repository at this point in the history
  • Loading branch information
begmaroman authored Sep 30, 2022
1 parent 49b9e68 commit f2a895f
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 25 deletions.
5 changes: 1 addition & 4 deletions command/secrets/init/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@ const (
ecdsaFlag = "ecdsa"
blsFlag = "bls"
networkFlag = "network"
)

var (
params = &initParams{}
numFlag = "num"
)

var (
Expand Down
14 changes: 14 additions & 0 deletions command/secrets/init/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
88 changes: 73 additions & 15 deletions command/secrets/init/secrets_init.go
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -21,64 +36,107 @@ func GetCommand() *cobra.Command {

func setFlags(cmd *cobra.Command) {
cmd.Flags().StringVar(
&params.dataDir,
&basicParams.dataDir,
dataDirFlag,
"",
"the directory for the Polygon Edge data if the local FS is used",
)

cmd.Flags().StringVar(
&params.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(
&params.generatesECDSA,
&basicParams.generatesECDSA,
ecdsaFlag,
true,
"the flag indicating whether new ECDSA key is created",
)

cmd.Flags().BoolVar(
&params.generatesNetwork,
&basicParams.generatesNetwork,
networkFlag,
true,
"the flag indicating whether new Network key is created",
)

cmd.Flags().BoolVar(
&params.generatesBLS,
&basicParams.generatesBLS,
blsFlag,
true,
"the flag indicating whether new BLS key is created",
)
}

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
}
9 changes: 3 additions & 6 deletions docker/local/polygon-edge.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,16 @@ 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..."
"$POLYGON_EDGE_BIN" genesis \
--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"
;;

Expand Down

0 comments on commit f2a895f

Please sign in to comment.