Skip to content

Commit

Permalink
First pass
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan-Ethernal committed Nov 3, 2023
1 parent 4dc5d91 commit c77c303
Show file tree
Hide file tree
Showing 118 changed files with 104 additions and 29,420 deletions.
132 changes: 4 additions & 128 deletions command/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,8 @@ package command

import (
"errors"
"os"
"path/filepath"
"strings"

"github.com/0xPolygon/polygon-edge/crypto"
"github.com/0xPolygon/polygon-edge/helper/common"
"github.com/0xPolygon/polygon-edge/secrets"
"github.com/0xPolygon/polygon-edge/secrets/local"
"github.com/0xPolygon/polygon-edge/types"
"github.com/0xPolygon/polygon-edge/validators"
"github.com/hashicorp/go-hclog"
)

// Flags shared across multiple spaces
Expand All @@ -27,8 +18,11 @@ const (
ValidatorPrefixFlag = "validators-prefix"
MinValidatorCountFlag = "min-validator-count"
MaxValidatorCountFlag = "max-validator-count"
)

IBFTValidatorTypeFlag = "ibft-validator-type"
var (
MinValidatorCount = uint64(1)
MaxValidatorCount = common.MaxSafeJSInt
)

const (
Expand All @@ -44,8 +38,6 @@ var (
"than MaxSafeJSInt (2^53 - 2)")

ErrValidatorNumberExceedsMax = errors.New("validator number exceeds max validator number")
ErrECDSAKeyNotFound = errors.New("ECDSA key not found in given path")
ErrBLSKeyNotFound = errors.New("BLS key not found in given path")
)

func ValidateMinMaxValidatorsNumber(minValidatorCount uint64, maxValidatorCount uint64) error {
Expand All @@ -63,119 +55,3 @@ func ValidateMinMaxValidatorsNumber(minValidatorCount uint64, maxValidatorCount

return nil
}

// 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(root)
if err != nil {
return nil, err
}

fullRootPath, err := filepath.Abs(root)
if err != nil {
return nil, err
}

validatorSet := validators.NewValidatorSetFromType(validatorType)

for _, file := range files {
path := file.Name()

if !file.IsDir() || !strings.HasPrefix(path, prefix) {
continue
}

localSecretsManager, err := local.SecretsManagerFactory(
nil,
&secrets.SecretsManagerParams{
Logger: hclog.NewNullLogger(),
Extra: map[string]interface{}{
secrets.Path: filepath.Join(fullRootPath, path),
},
},
)
if err != nil {
return nil, err
}

address, err := getValidatorAddressFromSecretManager(localSecretsManager)
if err != nil {
return nil, err
}

switch validatorType {
case validators.ECDSAValidatorType:
if err := validatorSet.Add(&validators.ECDSAValidator{
Address: address,
}); err != nil {
return nil, err
}

case validators.BLSValidatorType:
blsPublicKey, err := getBLSPublicKeyBytesFromSecretManager(localSecretsManager)
if err != nil {
return nil, err
}

if err := validatorSet.Add(&validators.BLSValidator{
Address: address,
BLSPublicKey: blsPublicKey,
}); err != nil {
return nil, err
}
}
}

return validatorSet, nil
}

func getValidatorAddressFromSecretManager(manager secrets.SecretsManager) (types.Address, error) {
if !manager.HasSecret(secrets.ValidatorKey) {
return types.ZeroAddress, ErrECDSAKeyNotFound
}

keyBytes, err := manager.GetSecret(secrets.ValidatorKey)
if err != nil {
return types.ZeroAddress, err
}

privKey, err := crypto.BytesToECDSAPrivateKey(keyBytes)
if err != nil {
return types.ZeroAddress, err
}

return crypto.PubKeyToAddress(&privKey.PublicKey), nil
}

func getBLSPublicKeyBytesFromSecretManager(manager secrets.SecretsManager) ([]byte, error) {
if !manager.HasSecret(secrets.ValidatorBLSKey) {
return nil, ErrBLSKeyNotFound
}

keyBytes, err := manager.GetSecret(secrets.ValidatorBLSKey)
if err != nil {
return nil, err
}

secretKey, err := crypto.BytesToBLSSecretKey(keyBytes)
if err != nil {
return nil, err
}

pubKey, err := secretKey.GetPublicKey()
if err != nil {
return nil, err
}

pubKeyBytes, err := pubKey.MarshalBinary()
if err != nil {
return nil, err
}

return pubKeyBytes, nil
}
1 change: 1 addition & 0 deletions command/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const (
DefaultGenesisGasLimit = 5242880 // 0x500000
DefaultGenesisBaseFeeEM = chain.GenesisBaseFeeEM
DefaultGenesisBaseFeeChangeDenom = chain.BaseFeeChangeDenom
DefaultEpochSize = 10
)

var (
Expand Down
97 changes: 38 additions & 59 deletions command/genesis/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@ import (

"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/0xPolygon/polygon-edge/server"
)

func GetCommand() *cobra.Command {
Expand Down Expand Up @@ -106,7 +104,7 @@ func setFlags(cmd *cobra.Command) {
cmd.Flags().Uint64Var(
&params.epochSize,
epochSizeFlag,
ibft.DefaultEpochSize,
command.DefaultEpochSize,
"the epoch size for the chain",
)

Expand All @@ -117,64 +115,43 @@ func setFlags(cmd *cobra.Command) {
"admin for proxy contracts",
)

// PoS
{
cmd.Flags().BoolVar(
&params.isPos,
posFlag,
false,
"the flag indicating that the client should use Proof of Stake IBFT. Defaults to "+
"Proof of Authority if flag is not provided or false",
)

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

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

cmd.Flags().StringVar(
&params.validatorsPath,
command.ValidatorRootFlag,
command.DefaultValidatorRoot,
"root path containing validators secrets",
)
cmd.Flags().Uint64Var(
&params.maxNumValidators,
command.MaxValidatorCountFlag,
common.MaxSafeJSInt,
"the maximum number of validators in the validator set for PoS",
)

cmd.Flags().StringVar(
&params.validatorsPrefixPath,
command.ValidatorPrefixFlag,
command.DefaultValidatorPrefix,
"folder prefix names for validators secrets",
)
cmd.Flags().StringVar(
&params.validatorsPath,
command.ValidatorRootFlag,
command.DefaultValidatorRoot,
"root path containing validators secrets",
)

cmd.Flags().StringArrayVar(
&params.validators,
command.ValidatorFlag,
[]string{},
"validators defined by user (polybft format: <P2P multi address>:<ECDSA address>:<public BLS key>)",
)
cmd.Flags().StringVar(
&params.validatorsPrefixPath,
command.ValidatorPrefixFlag,
command.DefaultValidatorPrefix,
"folder prefix names for validators secrets",
)

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

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

// PolyBFT
{
Expand Down Expand Up @@ -351,9 +328,11 @@ func preRunCommand(cmd *cobra.Command, _ []string) error {
return err
}

helper.SetRequiredFlags(cmd, params.getRequiredFlags())
//nolint:godox
// TODO: @Stefan-Ethernal Maybe it can be removed
params.consensus = server.ConsensusType(params.consensusRaw)

return params.initRawParams()
return nil
}

func runCommand(cmd *cobra.Command, _ []string) {
Expand Down
Loading

0 comments on commit c77c303

Please sign in to comment.