Skip to content

Commit

Permalink
Paralelize stake command
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan-Ethernal committed May 3, 2023
1 parent f203acd commit e5a31d2
Show file tree
Hide file tree
Showing 6 changed files with 218 additions and 147 deletions.
4 changes: 2 additions & 2 deletions command/polybftsecrets/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ const (
PrivateKeyFlag = "private-key"
ChainIDFlag = "chain-id"

AccountDirFlagDesc = "the directory for the Polygon Edge data if the local FS is used"
AccountConfigFlagDesc = "the path to the SecretsManager config file, if omitted, the local FS secrets manager is used"
AccountDirFlagDesc = "the directory for the Polygon Edge data (the local FS secrets manager is used)"
AccountConfigFlagDesc = "the path to the SecretsManager config file (if omitted, the local FS secrets manager is used)"
PrivateKeyFlagDesc = "hex-encoded private key of the account which executes rootchain commands"
ChainIDFlagDesc = "ID of child chain"
)
Expand Down
3 changes: 1 addition & 2 deletions command/rootchain/registration/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ type registerParams struct {

func (rp *registerParams) validateFlags() error {
// validate jsonrpc address
_, err := helper.ParseJSONRPCAddress(rp.jsonRPC)
if err != nil {
if _, err := helper.ParseJSONRPCAddress(rp.jsonRPC); err != nil {
return fmt.Errorf("failed to parse json rpc address. Error: %w", err)
}

Expand Down
4 changes: 2 additions & 2 deletions command/rootchain/registration/register_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ func setFlags(cmd *cobra.Command) {
&params.secretDirs,
polybftsecrets.AccountDirFlag,
nil,
polybftsecrets.AccountDirFlagDesc,
"the directories holding the secrets data (the local FS secrets manager is used)",
)

cmd.Flags().StringSliceVar(
&params.secretConfigs,
polybftsecrets.AccountConfigFlag,
nil,
polybftsecrets.AccountConfigFlagDesc,
"paths to the secrets manager config file (if omitted, the local FS secrets managers are used)",
)

cmd.Flags().StringVar(
Expand Down
63 changes: 52 additions & 11 deletions command/rootchain/staking/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,82 @@ package staking

import (
"bytes"
"errors"
"fmt"
"math/big"

"github.com/0xPolygon/polygon-edge/command/helper"
sidechainHelper "github.com/0xPolygon/polygon-edge/command/sidechain"
"github.com/0xPolygon/polygon-edge/command/sidechain"
)

var (
errInconsistentSecrets = errors.New("secrets count and amounts must be same length")
)

type stakeParams struct {
accountDir string
accountConfig string
secretDirs []string
secretConfigs []string
amounts []string
stakeManagerAddr string
nativeRootTokenAddr string
jsonRPC string
chainID uint64
amount string

amountValue *big.Int
amountValues []*big.Int
secretsDirsProvided bool
secretsCount int
}

func (sp *stakeParams) validateFlags() (err error) {
if sp.amountValue, err = helper.ParseAmount(sp.amount); err != nil {
return err
func (sp *stakeParams) validateFlags() error {
sp.secretsDirsProvided = len(sp.secretDirs) > 0
if sp.secretsDirsProvided {
sp.secretsCount = len(params.secretDirs)
} else {
sp.secretsCount = len(params.secretConfigs)
}

if len(sp.amounts) != sp.secretsCount {
return errInconsistentSecrets
}

// validate amount values
sp.amountValues = make([]*big.Int, len(sp.amounts))
for i, amount := range sp.amounts {
amountValue, err := helper.ParseAmount(amount)
if err != nil {
return err
}

sp.amountValues[i] = amountValue
}

// validate jsonrpc address
if _, err := helper.ParseJSONRPCAddress(sp.jsonRPC); err != nil {
return fmt.Errorf("failed to parse json rpc address. Error: %w", err)
}

return sidechainHelper.ValidateSecretFlags(sp.accountDir, sp.accountConfig)
for i := 0; i < sp.secretsCount; i++ {
secretDir := ""
secretConfig := ""

if params.secretsDirsProvided {
secretDir = params.secretDirs[i]
} else {
secretConfig = params.secretConfigs[i]
}

// validate provided secrets
if err := sidechain.ValidateSecretFlags(secretDir, secretConfig); err != nil {
return err
}
}

return nil
}

type stakeResult struct {
validatorAddress string
amount uint64
amount *big.Int
}

func (sr stakeResult) GetOutput() string {
Expand All @@ -46,7 +87,7 @@ func (sr stakeResult) GetOutput() string {

vals := make([]string, 0, 2)
vals = append(vals, fmt.Sprintf("Validator Address|%s", sr.validatorAddress))
vals = append(vals, fmt.Sprintf("Amount Staked|%v", sr.amount))
vals = append(vals, fmt.Sprintf("Amount Staked|%d", sr.amount))

buffer.WriteString(helper.FormatKV(vals))
buffer.WriteString("\n")
Expand Down
Loading

0 comments on commit e5a31d2

Please sign in to comment.