Skip to content

Commit

Permalink
Added option to specify a root path for IBFT validators dir (#1986)
Browse files Browse the repository at this point in the history
  • Loading branch information
begmaroman authored Oct 18, 2023
1 parent 4efe1d7 commit b1e7a26
Show file tree
Hide file tree
Showing 11 changed files with 146 additions and 136 deletions.
27 changes: 22 additions & 5 deletions command/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package command
import (
"errors"
"os"
"path/filepath"
"strings"

"github.com/0xPolygon/polygon-edge/crypto"
Expand All @@ -14,15 +15,25 @@ import (
"github.com/hashicorp/go-hclog"
)

// Flags shared across multiple spaces
const (
ConsensusFlag = "consensus"
NoDiscoverFlag = "no-discover"
BootnodeFlag = "bootnode"
LogLevelFlag = "log-level"

IBFTValidatorTypeFlag = "ibft-validator-type"
IBFTValidatorFlag = "ibft-validator"
IBFTValidatorPrefixFlag = "ibft-validators-prefix-path"
ValidatorFlag = "validators"
ValidatorRootFlag = "validators-path"
ValidatorPrefixFlag = "validators-prefix"
MinValidatorCountFlag = "min-validator-count"
MaxValidatorCountFlag = "max-validator-count"

IBFTValidatorTypeFlag = "ibft-validator-type"
)

const (
DefaultValidatorRoot = "./"
DefaultValidatorPrefix = "test-chain-"
)

var (
Expand Down Expand Up @@ -56,10 +67,16 @@ func ValidateMinMaxValidatorsNumber(minValidatorCount uint64, maxValidatorCount
// GetValidatorsFromPrefixPath extracts the addresses of the validators based on the directory
// prefix. It scans the directories for validator private keys and compiles a list of addresses
func GetValidatorsFromPrefixPath(
root string,
prefix string,
validatorType validators.ValidatorType,
) (validators.Validators, error) {
files, err := os.ReadDir(".")
files, err := os.ReadDir(root)
if err != nil {
return nil, err
}

fullRootPath, err := filepath.Abs(root)
if err != nil {
return nil, err
}
Expand All @@ -78,7 +95,7 @@ func GetValidatorsFromPrefixPath(
&secrets.SecretsManagerParams{
Logger: hclog.NewNullLogger(),
Extra: map[string]interface{}{
secrets.Path: path,
secrets.Path: filepath.Join(fullRootPath, path),
},
},
)
Expand Down
72 changes: 27 additions & 45 deletions command/genesis/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ package genesis
import (
"fmt"

"github.com/spf13/cobra"

"github.com/0xPolygon/polygon-edge/command"
"github.com/0xPolygon/polygon-edge/command/genesis/predeploy"
"github.com/0xPolygon/polygon-edge/command/helper"
"github.com/0xPolygon/polygon-edge/consensus/ibft"
"github.com/0xPolygon/polygon-edge/helper/common"
"github.com/0xPolygon/polygon-edge/validators"
"github.com/spf13/cobra"
)

func GetCommand() *cobra.Command {
Expand Down Expand Up @@ -116,35 +117,6 @@ func setFlags(cmd *cobra.Command) {
"admin for proxy contracts",
)

// IBFT Validators
{
cmd.Flags().StringVar(
&params.rawIBFTValidatorType,
command.IBFTValidatorTypeFlag,
string(validators.BLSValidatorType),
"the type of validators in IBFT",
)

cmd.Flags().StringVar(
&params.validatorPrefixPath,
command.IBFTValidatorPrefixFlag,
"",
"prefix path for validator folder directory. "+
"Needs to be present if ibft-validator is omitted",
)

cmd.Flags().StringArrayVar(
&params.ibftValidatorsRaw,
command.IBFTValidatorFlag,
[]string{},
"addresses to be used as IBFT validators, can be used multiple times. "+
"Needs to be present if ibft-validators-prefix-path is omitted",
)

// --ibft-validator-prefix-path & --ibft-validator can't be given at same time
cmd.MarkFlagsMutuallyExclusive(command.IBFTValidatorPrefixFlag, command.IBFTValidatorFlag)
}

// PoS
{
cmd.Flags().BoolVar(
Expand All @@ -157,45 +129,55 @@ func setFlags(cmd *cobra.Command) {

cmd.Flags().Uint64Var(
&params.minNumValidators,
minValidatorCount,
command.MinValidatorCountFlag,
1,
"the minimum number of validators in the validator set for PoS",
)

cmd.Flags().Uint64Var(
&params.maxNumValidators,
maxValidatorCount,
command.MaxValidatorCountFlag,
common.MaxSafeJSInt,
"the maximum number of validators in the validator set for PoS",
)
}

// PolyBFT
{
cmd.Flags().StringVar(
&params.validatorsPath,
validatorsPathFlag,
"./",
"root path containing polybft validators secrets",
command.ValidatorRootFlag,
command.DefaultValidatorRoot,
"root path containing validators secrets",
)

cmd.Flags().StringVar(
&params.validatorsPrefixPath,
validatorsPrefixFlag,
defaultValidatorPrefixPath,
"folder prefix names for polybft validators secrets",
command.ValidatorPrefixFlag,
command.DefaultValidatorPrefix,
"folder prefix names for validators secrets",
)

cmd.Flags().StringArrayVar(
&params.validators,
validatorsFlag,
command.ValidatorFlag,
[]string{},
"validators defined by user (format: <P2P multi address>:<ECDSA address>:<public BLS key>)",
"validators defined by user (polybft format: <P2P multi address>:<ECDSA address>:<public BLS key>)",
)

cmd.MarkFlagsMutuallyExclusive(validatorsFlag, validatorsPathFlag)
cmd.MarkFlagsMutuallyExclusive(validatorsFlag, validatorsPrefixFlag)
cmd.MarkFlagsMutuallyExclusive(command.ValidatorFlag, command.ValidatorRootFlag)
cmd.MarkFlagsMutuallyExclusive(command.ValidatorFlag, command.ValidatorPrefixFlag)
}

// IBFT Validators
{
cmd.Flags().StringVar(
&params.rawIBFTValidatorType,
command.IBFTValidatorTypeFlag,
string(validators.BLSValidatorType),
"the type of validators in IBFT",
)
}

// PolyBFT
{
cmd.Flags().Uint64Var(
&params.sprintSize,
sprintSizeFlag,
Expand Down
50 changes: 24 additions & 26 deletions command/genesis/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ const (
burnContractFlag = "burn-contract"
genesisBaseFeeConfigFlag = "base-fee-config"
posFlag = "pos"
minValidatorCount = "min-validator-count"
maxValidatorCount = "max-validator-count"
nativeTokenConfigFlag = "native-token-config"
rewardTokenCodeFlag = "reward-token-code"
rewardWalletFlag = "reward-wallet"
Expand Down Expand Up @@ -74,31 +72,33 @@ var (
)

type genesisParams struct {
genesisPath string
name string
consensusRaw string
validatorPrefixPath string
premine []string
bootnodes []string
ibftValidators validators.Validators

ibftValidatorsRaw []string
genesisPath string
name string
consensusRaw string
premine []string
bootnodes []string

chainID uint64
epochSize uint64

blockGasLimit uint64
isPos bool

burnContract string
baseFeeConfig string
parsedBaseFeeConfig *baseFeeInfo

minNumValidators uint64
maxNumValidators uint64
// PoS
isPos bool
minNumValidators uint64
maxNumValidators uint64
validatorsPath string
validatorsPrefixPath string
validators []string

// IBFT
rawIBFTValidatorType string
ibftValidatorType validators.ValidatorType
ibftValidators validators.Validators

extraData []byte
consensus server.ConsensusType
Expand All @@ -108,13 +108,10 @@ type genesisParams struct {
genesisConfig *chain.Chain

// PolyBFT
validatorsPath string
validatorsPrefixPath string
validators []string
sprintSize uint64
blockTime time.Duration
epochReward uint64
blockTimeDrift uint64
sprintSize uint64
blockTime time.Duration
epochReward uint64
blockTimeDrift uint64

initialStateRoot string

Expand Down Expand Up @@ -222,11 +219,11 @@ func (p *genesisParams) isPolyBFTConsensus() bool {
}

func (p *genesisParams) areValidatorsSetManually() bool {
return len(p.ibftValidatorsRaw) != 0
return len(p.validators) != 0
}

func (p *genesisParams) areValidatorsSetByPrefix() bool {
return p.validatorPrefixPath != ""
return p.validatorsPrefixPath != ""
}

func (p *genesisParams) getRequiredFlags() []string {
Expand Down Expand Up @@ -262,11 +259,11 @@ func (p *genesisParams) initRawParams() error {

// setValidatorSetFromCli sets validator set from cli command
func (p *genesisParams) setValidatorSetFromCli() error {
if len(p.ibftValidatorsRaw) == 0 {
if len(p.validators) == 0 {
return nil
}

newValidators, err := validators.ParseValidators(p.ibftValidatorType, p.ibftValidatorsRaw)
newValidators, err := validators.ParseValidators(p.ibftValidatorType, p.validators)
if err != nil {
return err
}
Expand All @@ -285,7 +282,8 @@ func (p *genesisParams) setValidatorSetFromPrefixPath() error {
}

validators, err := command.GetValidatorsFromPrefixPath(
p.validatorPrefixPath,
p.validatorsPath,
p.validatorsPrefixPath,
p.ibftValidatorType,
)

Expand Down
6 changes: 0 additions & 6 deletions command/genesis/polybft_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,6 @@ import (
)

const (
validatorsFlag = "validators"
validatorsPathFlag = "validators-path"
validatorsPrefixFlag = "validators-prefix"

defaultValidatorPrefixPath = "test-chain-"

sprintSizeFlag = "sprint-size"
blockTimeFlag = "block-time"
trieRootFlag = "trieroot"
Expand Down
Loading

0 comments on commit b1e7a26

Please sign in to comment.