Skip to content
This repository has been archived by the owner on Dec 4, 2024. It is now read-only.

Commit

Permalink
genesis and some ibft removals
Browse files Browse the repository at this point in the history
  • Loading branch information
igorcrevar committed Nov 6, 2023
1 parent 67c6677 commit e780abd
Show file tree
Hide file tree
Showing 13 changed files with 136 additions and 200 deletions.
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
27 changes: 1 addition & 26 deletions command/genesis/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/0xPolygon/polygon-edge/command"
"github.com/0xPolygon/polygon-edge/command/genesis/predeploy"
"github.com/0xPolygon/polygon-edge/helper/common"
"github.com/0xPolygon/polygon-edge/server"
)

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

setFlags(genesisCmd)
setLegacyFlags(genesisCmd)

genesisCmd.AddCommand(
// genesis predeploy
Expand Down Expand Up @@ -309,30 +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
}

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

return nil
return params.validateFlags()
}

func runCommand(cmd *cobra.Command, _ []string) {
Expand All @@ -344,7 +320,6 @@ func runCommand(cmd *cobra.Command, _ []string) {
if params.isPolyBFTConsensus() {
err = params.generatePolyBftChainConfig(outputter)
} else {
_, _ = outputter.Write([]byte(fmt.Sprintf("%s\n", common.IBFTImportantNotice)))
err = params.generateGenesis()
}

Expand Down
136 changes: 1 addition & 135 deletions command/genesis/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,13 @@ package genesis
import (
"errors"
"fmt"
"math/big"
"os"
"strings"
"time"

"github.com/0xPolygon/polygon-edge/chain"
"github.com/0xPolygon/polygon-edge/command"
"github.com/0xPolygon/polygon-edge/command/helper"
"github.com/0xPolygon/polygon-edge/consensus/polybft"
"github.com/0xPolygon/polygon-edge/contracts"
"github.com/0xPolygon/polygon-edge/server"
"github.com/0xPolygon/polygon-edge/types"
)
Expand All @@ -34,11 +31,6 @@ const (
proxyContractsAdminFlag = "proxy-contracts-admin"
)

// Legacy flags that need to be preserved for running clients
const (
chainIDFlagLEGACY = "chainid"
)

var (
params = &genesisParams{}
)
Expand Down Expand Up @@ -82,7 +74,6 @@ type genesisParams struct {
validators []string

extraData []byte
consensus server.ConsensusType

consensusEngineConfig map[string]interface{}

Expand Down Expand Up @@ -149,32 +140,9 @@ func (p *genesisParams) validateFlags() error {
}

if p.isPolyBFTConsensus() {
if err := p.extractNativeTokenMetadata(); err != nil {
return err
}

if err := p.validateBurnContract(); err != nil {
return err
}

if err := p.validateRewardWalletAndToken(); err != nil {
if err := p.validatePolyBFT(); err != nil {
return err
}

if err := p.validatePremineInfo(); err != nil {
return err
}

if err := p.validateProxyContractsAdmin(); err != nil {
return err
}

if p.epochSize < 2 {
// Epoch size must be greater than 1, so new transactions have a chance to be added to a block.
// Otherwise, every block would be an endblock (meaning it will not have any transactions).
// Check is placed here to avoid additional parsing if epochSize < 2
return errInvalidEpochSize
}
}

// Validate validatorsPath only if validators information were not provided via CLI flag
Expand Down Expand Up @@ -266,33 +234,6 @@ func (p *genesisParams) initGenesisConfig() error {
return nil
}

// validateRewardWalletAndToken validates reward wallet flag
func (p *genesisParams) validateRewardWalletAndToken() error {
if p.rewardWallet == "" {
return errRewardWalletNotDefined
}

if !p.nativeTokenConfig.IsMintable && p.rewardTokenCode == "" {
return errRewardTokenOnNonMintable
}

premineInfo, err := helper.ParsePremineInfo(p.rewardWallet)
if err != nil {
return err
}

if premineInfo.Address == types.ZeroAddress {
return errRewardWalletZero
}

// If epoch rewards are enabled, reward wallet must have some amount of premine
if p.epochReward > 0 && premineInfo.Amount.Cmp(big.NewInt(0)) < 1 {
return errRewardWalletAmountZero
}

return nil
}

// parsePremineInfo parses premine flag
func (p *genesisParams) parsePremineInfo() error {
p.premineInfos = make([]*helper.PremineInfo, 0, len(p.premine))
Expand All @@ -309,52 +250,6 @@ func (p *genesisParams) parsePremineInfo() error {
return nil
}

// validatePremineInfo validates whether reserve account (0x0 address) is premined
func (p *genesisParams) validatePremineInfo() error {
for _, premineInfo := range p.premineInfos {
if premineInfo.Address == types.ZeroAddress {
// we have premine of zero address, just return
return nil
}
}

return errReserveAccMustBePremined
}

// validateBlockTrackerPollInterval validates block tracker block interval
// which can not be 0
func (p *genesisParams) validateBlockTrackerPollInterval() error {
if p.blockTrackerPollInterval == 0 {
return helper.ErrBlockTrackerPollInterval
}

return nil
}

// validateBurnContract validates burn contract. If native token is mintable,
// burn contract flag must not be set. If native token is non mintable only one burn contract
// can be set and the specified address will be used to predeploy default EIP1559 burn contract.
func (p *genesisParams) validateBurnContract() error {
if p.isBurnContractEnabled() {
burnContractInfo, err := parseBurnContractInfo(p.burnContract)
if err != nil {
return fmt.Errorf("invalid burn contract info provided: %w", err)
}

if p.nativeTokenConfig.IsMintable {
if burnContractInfo.Address != types.ZeroAddress {
return errors.New("only zero address is allowed as burn destination for mintable native token")
}
} else {
if burnContractInfo.Address == types.ZeroAddress {
return errors.New("it is not allowed to deploy burn contract to 0x0 address")
}
}
}

return nil
}

func (p *genesisParams) validateGenesisBaseFeeConfig() error {
if p.baseFeeConfig == "" {
return errors.New("invalid input(empty string) for genesis base fee config flag")
Expand Down Expand Up @@ -382,40 +277,11 @@ func (p *genesisParams) validateGenesisBaseFeeConfig() error {
return nil
}

func (p *genesisParams) validateProxyContractsAdmin() error {
if strings.TrimSpace(p.proxyContractsAdmin) == "" {
return errors.New("proxy contracts admin address must be set")
}

proxyContractsAdminAddr := types.StringToAddress(p.proxyContractsAdmin)
if proxyContractsAdminAddr == types.ZeroAddress {
return errors.New("proxy contracts admin address must not be zero address")
}

if proxyContractsAdminAddr == contracts.SystemCaller {
return errors.New("proxy contracts admin address must not be system caller address")
}

return nil
}

// isBurnContractEnabled returns true in case burn contract info is provided
func (p *genesisParams) isBurnContractEnabled() bool {
return p.burnContract != ""
}

// extractNativeTokenMetadata parses provided native token metadata (such as name, symbol and decimals count)
func (p *genesisParams) extractNativeTokenMetadata() error {
tokenConfig, err := polybft.ParseRawTokenConfig(p.nativeTokenConfigRaw)
if err != nil {
return err
}

p.nativeTokenConfig = tokenConfig

return nil
}

func (p *genesisParams) getResult() command.CommandResult {
return &GenesisResult{
Message: fmt.Sprintf("\nGenesis written to %s\n", p.genesisPath),
Expand Down
129 changes: 129 additions & 0 deletions command/genesis/polybft_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,135 @@ func (p *genesisParams) getValidatorAccounts() ([]*validator.GenesisValidator, e
return validators, nil
}

func (p *genesisParams) validatePolyBFT() error {
if err := p.extractNativeTokenMetadata(); err != nil {
return err
}

if err := p.validateBurnContract(); err != nil {
return err
}

if err := p.validateRewardWalletAndToken(); err != nil {
return err
}

if err := p.validatePremineInfo(); err != nil {
return err
}

if p.epochSize < 2 {
// Epoch size must be greater than 1, so new transactions have a chance to be added to a block.
// Otherwise, every block would be an endblock (meaning it will not have any transactions).
// Check is placed here to avoid additional parsing if epochSize < 2
return errInvalidEpochSize
}

return p.validateProxyContractsAdmin()
}

// validateRewardWalletAndToken validates reward wallet flag
func (p *genesisParams) validateRewardWalletAndToken() error {
if p.rewardWallet == "" {
return errRewardWalletNotDefined
}

if !p.nativeTokenConfig.IsMintable && p.rewardTokenCode == "" {
return errRewardTokenOnNonMintable
}

premineInfo, err := helper.ParsePremineInfo(p.rewardWallet)
if err != nil {
return err
}

if premineInfo.Address == types.ZeroAddress {
return errRewardWalletZero
}

// If epoch rewards are enabled, reward wallet must have some amount of premine
if p.epochReward > 0 && premineInfo.Amount.Cmp(big.NewInt(0)) < 1 {
return errRewardWalletAmountZero
}

return nil
}

// validatePremineInfo validates whether reserve account (0x0 address) is premined
func (p *genesisParams) validatePremineInfo() error {
for _, premineInfo := range p.premineInfos {
if premineInfo.Address == types.ZeroAddress {
// we have premine of zero address, just return
return nil
}
}

return errReserveAccMustBePremined
}

// validateBlockTrackerPollInterval validates block tracker block interval
// which can not be 0
func (p *genesisParams) validateBlockTrackerPollInterval() error {
if p.blockTrackerPollInterval == 0 {
return helper.ErrBlockTrackerPollInterval
}

return nil
}

// validateBurnContract validates burn contract. If native token is mintable,
// burn contract flag must not be set. If native token is non mintable only one burn contract
// can be set and the specified address will be used to predeploy default EIP1559 burn contract.
func (p *genesisParams) validateBurnContract() error {
if p.isBurnContractEnabled() {
burnContractInfo, err := parseBurnContractInfo(p.burnContract)
if err != nil {
return fmt.Errorf("invalid burn contract info provided: %w", err)
}

if p.nativeTokenConfig.IsMintable {
if burnContractInfo.Address != types.ZeroAddress {
return errors.New("only zero address is allowed as burn destination for mintable native token")
}
} else {
if burnContractInfo.Address == types.ZeroAddress {
return errors.New("it is not allowed to deploy burn contract to 0x0 address")
}
}
}

return nil
}

func (p *genesisParams) validateProxyContractsAdmin() error {
if strings.TrimSpace(p.proxyContractsAdmin) == "" {
return errors.New("proxy contracts admin address must be set")
}

proxyContractsAdminAddr := types.StringToAddress(p.proxyContractsAdmin)
if proxyContractsAdminAddr == types.ZeroAddress {
return errors.New("proxy contracts admin address must not be zero address")
}

if proxyContractsAdminAddr == contracts.SystemCaller {
return errors.New("proxy contracts admin address must not be system caller address")
}

return nil
}

// extractNativeTokenMetadata parses provided native token metadata (such as name, symbol and decimals count)
func (p *genesisParams) extractNativeTokenMetadata() error {
tokenConfig, err := polybft.ParseRawTokenConfig(p.nativeTokenConfigRaw)
if err != nil {
return err
}

p.nativeTokenConfig = tokenConfig

return nil
}

func stringSliceToAddressSlice(addrs []string) []types.Address {
res := make([]types.Address, len(addrs))
for indx, addr := range addrs {
Expand Down
Loading

0 comments on commit e780abd

Please sign in to comment.