Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove IBFT consensus protocol #1

Merged
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/deploy.nightly.devnet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ jobs:

{% for item in hostvars %}
{% if (hostvars[item].tags.Role == "fullnode" or hostvars[item].tags.Role == "validator") %}
polygon-edge polybft-secrets --data-dir {{ hostvars[item].tags["Name"] }} --json --insecure > {{ hostvars[item].tags["Name"] }}.json
polygon-edge secrets init --data-dir {{ hostvars[item].tags["Name"] }} --json --insecure > {{ hostvars[item].tags["Name"] }}.json
{% endif %}
{% endfor %}

Expand Down
1 change: 0 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ protoc: check-protoc
./server/proto/*.proto \
./network/proto/*.proto \
./txpool/proto/*.proto \
./consensus/ibft/**/*.proto \
./consensus/polybft/**/*.proto

.PHONY: build
Expand Down
2 changes: 1 addition & 1 deletion command/bridge/mint/mint_erc20.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (

"github.com/0xPolygon/polygon-edge/command"
"github.com/0xPolygon/polygon-edge/command/helper"
"github.com/0xPolygon/polygon-edge/command/polybftsecrets"
rootHelper "github.com/0xPolygon/polygon-edge/command/rootchain/helper"
polybftsecrets "github.com/0xPolygon/polygon-edge/command/secrets/init"
"github.com/0xPolygon/polygon-edge/txrelayer"
"github.com/0xPolygon/polygon-edge/types"
"github.com/spf13/cobra"
Expand Down
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)
goran-ethernal marked this conversation as resolved.
Show resolved Hide resolved
MaxValidatorCount = common.MaxSafeJSInt
goran-ethernal marked this conversation as resolved.
Show resolved Hide resolved
)

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
116 changes: 35 additions & 81 deletions command/genesis/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@ 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"
)

func GetCommand() *cobra.Command {
Expand All @@ -22,7 +19,6 @@ func GetCommand() *cobra.Command {
}

setFlags(genesisCmd)
setLegacyFlags(genesisCmd)

genesisCmd.AddCommand(
// genesis predeploy
Expand Down Expand Up @@ -106,7 +102,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 +113,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,
goran-ethernal marked this conversation as resolved.
Show resolved Hide resolved
"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 @@ -332,28 +307,8 @@ func setFlags(cmd *cobra.Command) {
}
}

// setLegacyFlags sets the legacy flags to preserve backwards compatibility
// with running partners
func setLegacyFlags(cmd *cobra.Command) {
// Legacy chainid flag
cmd.Flags().Uint64Var(
&params.chainID,
chainIDFlagLEGACY,
command.DefaultChainID,
"the ID of the chain",
)

_ = cmd.Flags().MarkHidden(chainIDFlagLEGACY)
}

func preRunCommand(cmd *cobra.Command, _ []string) error {
if err := params.validateFlags(); err != nil {
return err
}

helper.SetRequiredFlags(cmd, params.getRequiredFlags())

return params.initRawParams()
return params.validateFlags()
}

func runCommand(cmd *cobra.Command, _ []string) {
Expand All @@ -363,9 +318,8 @@ func runCommand(cmd *cobra.Command, _ []string) {
var err error

if params.isPolyBFTConsensus() {
err = params.generatePolyBftChainConfig(outputter)
err = params.generateChainConfig(outputter)
} else {
_, _ = outputter.Write([]byte(fmt.Sprintf("%s\n", common.IBFTImportantNotice)))
err = params.generateGenesis()
}

Expand Down
Loading