From b1e7a2624a94e0a5567c00b3d572e43646792c4c Mon Sep 17 00:00:00 2001 From: Roman Behma <13855864+begmaroman@users.noreply.github.com> Date: Wed, 18 Oct 2023 14:23:20 +0700 Subject: [PATCH] Added option to specify a root path for IBFT validators dir (#1986) --- command/common.go | 27 ++++++++--- command/genesis/genesis.go | 72 +++++++++++------------------- command/genesis/params.go | 50 ++++++++++----------- command/genesis/polybft_params.go | 6 --- command/ibft/switch/ibft_switch.go | 63 +++++++++++++++----------- command/ibft/switch/params.go | 39 ++++++++++------ docker/local/polygon-edge.sh | 15 +++---- e2e/framework/config.go | 2 +- e2e/framework/testserver.go | 4 +- scripts/cluster | 2 +- validators/helper.go | 2 +- 11 files changed, 146 insertions(+), 136 deletions(-) diff --git a/command/common.go b/command/common.go index 796bb693c2..e35e1ee067 100644 --- a/command/common.go +++ b/command/common.go @@ -3,6 +3,7 @@ package command import ( "errors" "os" + "path/filepath" "strings" "github.com/0xPolygon/polygon-edge/crypto" @@ -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 ( @@ -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 } @@ -78,7 +95,7 @@ func GetValidatorsFromPrefixPath( &secrets.SecretsManagerParams{ Logger: hclog.NewNullLogger(), Extra: map[string]interface{}{ - secrets.Path: path, + secrets.Path: filepath.Join(fullRootPath, path), }, }, ) diff --git a/command/genesis/genesis.go b/command/genesis/genesis.go index fad6c7c9d6..bb6b38907a 100644 --- a/command/genesis/genesis.go +++ b/command/genesis/genesis.go @@ -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 { @@ -116,35 +117,6 @@ func setFlags(cmd *cobra.Command) { "admin for proxy contracts", ) - // IBFT Validators - { - cmd.Flags().StringVar( - ¶ms.rawIBFTValidatorType, - command.IBFTValidatorTypeFlag, - string(validators.BLSValidatorType), - "the type of validators in IBFT", - ) - - cmd.Flags().StringVar( - ¶ms.validatorPrefixPath, - command.IBFTValidatorPrefixFlag, - "", - "prefix path for validator folder directory. "+ - "Needs to be present if ibft-validator is omitted", - ) - - cmd.Flags().StringArrayVar( - ¶ms.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( @@ -157,45 +129,55 @@ func setFlags(cmd *cobra.Command) { cmd.Flags().Uint64Var( ¶ms.minNumValidators, - minValidatorCount, + command.MinValidatorCountFlag, 1, "the minimum number of validators in the validator set for PoS", ) cmd.Flags().Uint64Var( ¶ms.maxNumValidators, - maxValidatorCount, + command.MaxValidatorCountFlag, common.MaxSafeJSInt, "the maximum number of validators in the validator set for PoS", ) - } - // PolyBFT - { cmd.Flags().StringVar( ¶ms.validatorsPath, - validatorsPathFlag, - "./", - "root path containing polybft validators secrets", + command.ValidatorRootFlag, + command.DefaultValidatorRoot, + "root path containing validators secrets", ) cmd.Flags().StringVar( ¶ms.validatorsPrefixPath, - validatorsPrefixFlag, - defaultValidatorPrefixPath, - "folder prefix names for polybft validators secrets", + command.ValidatorPrefixFlag, + command.DefaultValidatorPrefix, + "folder prefix names for validators secrets", ) cmd.Flags().StringArrayVar( ¶ms.validators, - validatorsFlag, + command.ValidatorFlag, []string{}, - "validators defined by user (format: ::)", + "validators defined by user (polybft format: ::)", ) - 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( + ¶ms.rawIBFTValidatorType, + command.IBFTValidatorTypeFlag, + string(validators.BLSValidatorType), + "the type of validators in IBFT", + ) + } + + // PolyBFT + { cmd.Flags().Uint64Var( ¶ms.sprintSize, sprintSizeFlag, diff --git a/command/genesis/params.go b/command/genesis/params.go index 1e202827fa..4c55f5de8e 100644 --- a/command/genesis/params.go +++ b/command/genesis/params.go @@ -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" @@ -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 @@ -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 @@ -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 { @@ -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 } @@ -285,7 +282,8 @@ func (p *genesisParams) setValidatorSetFromPrefixPath() error { } validators, err := command.GetValidatorsFromPrefixPath( - p.validatorPrefixPath, + p.validatorsPath, + p.validatorsPrefixPath, p.ibftValidatorType, ) diff --git a/command/genesis/polybft_params.go b/command/genesis/polybft_params.go index bacd4ed63c..a71388405e 100644 --- a/command/genesis/polybft_params.go +++ b/command/genesis/polybft_params.go @@ -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" diff --git a/command/ibft/switch/ibft_switch.go b/command/ibft/switch/ibft_switch.go index 759ccb442b..419a97bd8e 100644 --- a/command/ibft/switch/ibft_switch.go +++ b/command/ibft/switch/ibft_switch.go @@ -3,10 +3,11 @@ package ibftswitch import ( "fmt" + "github.com/spf13/cobra" + "github.com/0xPolygon/polygon-edge/command" "github.com/0xPolygon/polygon-edge/command/helper" "github.com/0xPolygon/polygon-edge/validators" - "github.com/spf13/cobra" ) func GetCommand() *cobra.Command { @@ -55,50 +56,58 @@ func setFlags(cmd *cobra.Command) { ) } - // Validator Configurations - cmd.Flags().StringVar( - ¶ms.rawIBFTValidatorType, - command.IBFTValidatorTypeFlag, - string(validators.BLSValidatorType), - "the type of validators in IBFT", - ) - + // IBFT { - // PoA Configuration cmd.Flags().StringVar( - ¶ms.ibftValidatorPrefixPath, - command.IBFTValidatorPrefixFlag, - "", - "prefix path for validator folder directory. "+ - "Needs to be present if ibft-validator is omitted", + ¶ms.rawIBFTValidatorType, + command.IBFTValidatorTypeFlag, + string(validators.BLSValidatorType), + "the type of validators in IBFT", ) - - cmd.Flags().StringArrayVar( - ¶ms.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", - ) - - cmd.MarkFlagsMutuallyExclusive(command.IBFTValidatorPrefixFlag, command.IBFTValidatorFlag) } { // PoS Configuration cmd.Flags().StringVar( ¶ms.minValidatorCountRaw, - minValidatorCount, + command.MinValidatorCountFlag, "", "the minimum number of validators in the validator set for PoS", ) cmd.Flags().StringVar( ¶ms.maxValidatorCountRaw, - maxValidatorCount, + command.MaxValidatorCountFlag, "", "the maximum number of validators in the validator set for PoS", ) + + cmd.Flags().StringVar( + ¶ms.validatorRootPath, + command.ValidatorRootFlag, + command.DefaultValidatorRoot, + "root path for validator folder directory. "+ + "Needs to be present if validators is omitted", + ) + + cmd.Flags().StringVar( + ¶ms.validatorPrefixPath, + command.ValidatorPrefixFlag, + command.DefaultValidatorPrefix, + "prefix path for validator folder directory. "+ + "Needs to be present if validators is omitted", + ) + + cmd.Flags().StringArrayVar( + ¶ms.validatorsRaw, + command.ValidatorFlag, + []string{}, + "addresses to be used as IBFT validators, can be used multiple times. "+ + "Needs to be present if validators-prefix is omitted", + ) + + cmd.MarkFlagsMutuallyExclusive(command.ValidatorPrefixFlag, command.ValidatorFlag) + cmd.MarkFlagsMutuallyExclusive(command.ValidatorRootFlag, command.ValidatorFlag) } } diff --git a/command/ibft/switch/params.go b/command/ibft/switch/params.go index 62980244c6..0f13d68314 100644 --- a/command/ibft/switch/params.go +++ b/command/ibft/switch/params.go @@ -14,12 +14,10 @@ import ( ) const ( - chainFlag = "chain" - typeFlag = "type" - deploymentFlag = "deployment" - fromFlag = "from" - minValidatorCount = "min-validator-count" - maxValidatorCount = "max-validator-count" + chainFlag = "chain" + typeFlag = "type" + deploymentFlag = "deployment" + fromFlag = "from" ) var ( @@ -49,9 +47,10 @@ type switchParams struct { ibftValidatorType validators.ValidatorType // PoA - ibftValidatorPrefixPath string - ibftValidatorsRaw []string - ibftValidators validators.Validators + validatorRootPath string + validatorPrefixPath string + validatorsRaw []string + ibftValidators validators.Validators // PoS maxValidatorCountRaw string @@ -173,12 +172,13 @@ func (p *switchParams) initPoAConfig() error { } func (p *switchParams) setValidatorSetFromPrefixPath() error { - if p.ibftValidatorPrefixPath == "" { + if p.validatorPrefixPath == "" { return nil } validators, err := command.GetValidatorsFromPrefixPath( - p.ibftValidatorPrefixPath, + p.validatorRootPath, + p.validatorPrefixPath, p.ibftValidatorType, ) if err != nil { @@ -194,11 +194,11 @@ func (p *switchParams) setValidatorSetFromPrefixPath() error { // setValidatorSetFromCli sets validator set from cli command func (p *switchParams) setValidatorSetFromCli() error { - if len(p.ibftValidatorsRaw) == 0 { + if len(p.validatorsRaw) == 0 { return nil } - newSet, err := validators.ParseValidators(p.ibftValidatorType, p.ibftValidatorsRaw) + newSet, err := validators.ParseValidators(p.ibftValidatorType, p.validatorsRaw) if err != nil { return err } @@ -246,7 +246,18 @@ func (p *switchParams) initPoSConfig() error { p.maxValidatorCount = &value } - return p.validateMinMaxValidatorNumber() + if err := p.validateMinMaxValidatorNumber(); err != nil { + return err + } + + // Validate validatorRootPath only if validators information were not provided via CLI flag + if len(p.validatorsRaw) == 0 { + if _, err := os.Stat(p.validatorRootPath); err != nil { + return fmt.Errorf("invalid validators path ('%s') provided. Error: %w", p.validatorRootPath, err) + } + } + + return nil } func (p *switchParams) validateMinMaxValidatorNumber() error { diff --git a/docker/local/polygon-edge.sh b/docker/local/polygon-edge.sh index 2e6b25d1c2..fbd8803489 100755 --- a/docker/local/polygon-edge.sh +++ b/docker/local/polygon-edge.sh @@ -24,6 +24,8 @@ createGenesisConfig() { "$POLYGON_EDGE_BIN" genesis $CHAIN_CUSTOM_OPTIONS \ --dir /data/genesis.json \ + --validators-path /data \ + --validators-prefix data- \ --consensus $consensus_type \ --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')" \ @@ -45,8 +47,7 @@ case "$1" in rm -f /data/genesis.json - createGenesisConfig "$2" "$secrets" \ - --ibft-validators-prefix-path data- + createGenesisConfig "$2" "$secrets" fi ;; "polybft") @@ -58,12 +59,10 @@ case "$1" in proxyContractsAdmin=0x5aaeb6053f3e94c9b9a09f33669435e7ef1beaed - createGenesisConfig "$2" "$secrets" \ - --validators-path /data \ - --validators-prefix data- \ - --reward-wallet 0xDEADBEEF:1000000 \ - --native-token-config "Polygon:MATIC:18:true:$(echo "$secrets" | jq -r '.[0] | .address')" \ - --proxy-contracts-admin ${proxyContractsAdmin} + createGenesisConfig "$2" "$secrets" \ + --reward-wallet 0xDEADBEEF:1000000 \ + --native-token-config "Polygon:MATIC:18:true:$(echo "$secrets" | jq -r '.[0] | .address')" \ + --proxy-contracts-admin ${proxyContractsAdmin} echo "Deploying stake manager..." "$POLYGON_EDGE_BIN" polybft stake-manager-deploy \ diff --git a/e2e/framework/config.go b/e2e/framework/config.go index aed0eab16c..5758c6b542 100644 --- a/e2e/framework/config.go +++ b/e2e/framework/config.go @@ -145,7 +145,7 @@ func (t *TestServerConfig) SetDevInterval(interval int) { } // SetDevStakingAddresses sets the Staking smart contract staker addresses for the dev mode. -// These addresses should be passed into the `ibft-validator` flag in genesis generation. +// These addresses should be passed into the `validators` flag in genesis generation. // Since invoking the dev consensus will not generate the ibft base folders, this is the only way // to signalize to the genesis creation process who the validators are func (t *TestServerConfig) SetDevStakingAddresses(stakingAddresses []types.Address) { diff --git a/e2e/framework/testserver.go b/e2e/framework/testserver.go index ca1e8783e2..fde06aa48d 100644 --- a/e2e/framework/testserver.go +++ b/e2e/framework/testserver.go @@ -298,7 +298,7 @@ func (t *TestServer) GenerateGenesis() error { return errors.New("prefix of IBFT directory is not set") } - args = append(args, "--ibft-validators-prefix-path", t.Config.IBFTDirPrefix) + args = append(args, "--validators-prefix", t.Config.IBFTDirPrefix) if t.Config.EpochSize != 0 { args = append(args, "--epoch-size", strconv.FormatUint(t.Config.EpochSize, 10)) @@ -313,7 +313,7 @@ func (t *TestServer) GenerateGenesis() error { // Set up any initial staker addresses for the predeployed Staking SC for _, stakerAddress := range t.Config.DevStakers { - args = append(args, "--ibft-validator", stakerAddress.String()) + args = append(args, "--validators", stakerAddress.String()) } case ConsensusDummy: args = append(args, "--consensus", "dummy") diff --git a/scripts/cluster b/scripts/cluster index 9fd71adbf2..700e7ac224 100755 --- a/scripts/cluster +++ b/scripts/cluster @@ -54,7 +54,7 @@ function initIbftConsensus() { node1_id=$(./polygon-edge secrets output --data-dir test-chain-1 | grep Node | head -n 1 | awk -F ' ' '{print $4}') node2_id=$(./polygon-edge secrets output --data-dir test-chain-2 | grep Node | head -n 1 | awk -F ' ' '{print $4}') - genesis_params="--consensus ibft --ibft-validators-prefix-path test-chain- \ + genesis_params="--consensus ibft --validators-prefix test-chain- \ --bootnode /ip4/127.0.0.1/tcp/30301/p2p/$node1_id \ --bootnode /ip4/127.0.0.1/tcp/30302/p2p/$node2_id" } diff --git a/validators/helper.go b/validators/helper.go index a00c0368c6..e7a60713bc 100644 --- a/validators/helper.go +++ b/validators/helper.go @@ -78,7 +78,7 @@ func ParseValidator(validatorType ValidatorType, validator string) (Validator, e } } -// ParseValidator parses an array of validator represented in string +// ParseValidators parses an array of validator represented in string func ParseValidators(validatorType ValidatorType, rawValidators []string) (Validators, error) { set := NewValidatorSetFromType(validatorType) if set == nil {