From 062414f91a6a30b7e7d072dffd8a63ed03a41dfe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20Negovanovi=C4=87?= <93934272+Stefan-Ethernal@users.noreply.github.com> Date: Fri, 5 May 2023 14:57:32 +0200 Subject: [PATCH] Provide mintable parameter as a part of native token config (#1472) * Provide mintable parameter as a part of native token config * Change mintable flag to boolean and make it mandatory * Minor change --- command/genesis/genesis.go | 9 +----- command/genesis/params.go | 43 ++++++++++++++------------- command/genesis/polybft_params.go | 3 +- consensus/polybft/polybft.go | 2 +- consensus/polybft/polybft_config.go | 10 +++---- e2e-polybft/e2e/consensus_test.go | 3 +- e2e-polybft/framework/test-cluster.go | 11 ------- 7 files changed, 31 insertions(+), 50 deletions(-) diff --git a/command/genesis/genesis.go b/command/genesis/genesis.go index 439945f4bb..f7512ed348 100644 --- a/command/genesis/genesis.go +++ b/command/genesis/genesis.go @@ -219,18 +219,11 @@ func setFlags(cmd *cobra.Command) { "trie root from the corresponding triedb", ) - cmd.Flags().BoolVar( - ¶ms.mintableNativeToken, - mintableTokenFlag, - false, - "flag indicate whether mintable or non-mintable native token is deployed", - ) - cmd.Flags().StringVar( ¶ms.nativeTokenConfigRaw, nativeTokenConfigFlag, "", - "configuration of native token in format ", + "configuration of native token in format ", ) cmd.Flags().StringVar( diff --git a/command/genesis/params.go b/command/genesis/params.go index 45fcc22dd9..bc8ea1d7a3 100644 --- a/command/genesis/params.go +++ b/command/genesis/params.go @@ -36,7 +36,6 @@ const ( posFlag = "pos" minValidatorCount = "min-validator-count" maxValidatorCount = "max-validator-count" - mintableTokenFlag = "mintable-native-token" nativeTokenConfigFlag = "native-token-config" rewardTokenCodeFlag = "reward-token-code" rewardWalletFlag = "reward-wallet" @@ -44,7 +43,7 @@ const ( defaultNativeTokenName = "Polygon" defaultNativeTokenSymbol = "MATIC" defaultNativeTokenDecimals = uint8(18) - nativeTokenParamsNumber = 3 + nativeTokenParamsNumber = 4 ) // Legacy flags that need to be preserved for running clients @@ -60,8 +59,8 @@ var ( errValidatorsNotSpecified = errors.New("validator information not specified") errUnsupportedConsensus = errors.New("specified consensusRaw not supported") errInvalidEpochSize = errors.New("epoch size must be greater than 1") - errInvalidTokenParams = errors.New("native token params were not submitted in proper" + - " format ") + errInvalidTokenParams = errors.New("native token params were not submitted in proper format " + + "()") errRewardWalletAmountZero = errors.New("reward wallet amount can not be zero or negative") ) @@ -122,7 +121,6 @@ type genesisParams struct { bridgeBlockListAdmin []string bridgeBlockListEnabled []string - mintableNativeToken bool nativeTokenConfigRaw string nativeTokenConfig *polybft.TokenConfig @@ -478,32 +476,27 @@ func (p *genesisParams) validateRewardWallet() error { func (p *genesisParams) extractNativeTokenMetadata() error { if p.nativeTokenConfigRaw == "" { p.nativeTokenConfig = &polybft.TokenConfig{ - Name: defaultNativeTokenName, - Symbol: defaultNativeTokenSymbol, - Decimals: defaultNativeTokenDecimals, + Name: defaultNativeTokenName, + Symbol: defaultNativeTokenSymbol, + Decimals: defaultNativeTokenDecimals, + IsMintable: false, } return nil } params := strings.Split(p.nativeTokenConfigRaw, ":") - if len(params) != nativeTokenParamsNumber { // 3 parameters + if len(params) != nativeTokenParamsNumber { return errInvalidTokenParams } - p.nativeTokenConfig = &polybft.TokenConfig{ - Name: defaultNativeTokenName, - Symbol: defaultNativeTokenSymbol, - Decimals: defaultNativeTokenDecimals, - } - - p.nativeTokenConfig.Name = strings.TrimSpace(params[0]) - if p.nativeTokenConfig.Name == "" { + name := strings.TrimSpace(params[0]) + if name == "" { return errInvalidTokenParams } - p.nativeTokenConfig.Symbol = strings.TrimSpace(params[1]) - if p.nativeTokenConfig.Symbol == "" { + symbol := strings.TrimSpace(params[1]) + if symbol == "" { return errInvalidTokenParams } @@ -512,7 +505,17 @@ func (p *genesisParams) extractNativeTokenMetadata() error { return errInvalidTokenParams } - p.nativeTokenConfig.Decimals = uint8(decimals) + isMintable, err := strconv.ParseBool(strings.TrimSpace(params[3])) + if err != nil { + return errInvalidTokenParams + } + + p.nativeTokenConfig = &polybft.TokenConfig{ + Name: name, + Symbol: symbol, + Decimals: uint8(decimals), + IsMintable: isMintable, + } return nil } diff --git a/command/genesis/polybft_params.go b/command/genesis/polybft_params.go index 17e74b1413..26b9a79a72 100644 --- a/command/genesis/polybft_params.go +++ b/command/genesis/polybft_params.go @@ -143,7 +143,6 @@ func (p *genesisParams) generatePolyBftChainConfig(o command.OutputFormatter) er // use 1st account as governance address Governance: initialValidators[0].Address, InitialTrieRoot: types.StringToHash(p.initialStateRoot), - MintableNativeToken: p.mintableNativeToken, NativeTokenConfig: p.nativeTokenConfig, BridgeAllowListAdmin: bridgeAllowListAdmin, BridgeBlockListAdmin: bridgeBlockListAdmin, @@ -367,7 +366,7 @@ func (p *genesisParams) deployContracts(totalStake *big.Int, }, } - if !params.mintableNativeToken { + if !params.nativeTokenConfig.IsMintable { genesisContracts = append(genesisContracts, &contractInfo{artifact: contractsapi.NativeERC20, address: contracts.NativeERC20TokenContract}) } else { diff --git a/consensus/polybft/polybft.go b/consensus/polybft/polybft.go index 1959671f80..b79940ac4f 100644 --- a/consensus/polybft/polybft.go +++ b/consensus/polybft/polybft.go @@ -215,7 +215,7 @@ func GenesisPostHookFactory(config *chain.Chain, engineName string) func(txn *st rootNativeERC20Token = polyBFTConfig.Bridge.RootNativeERC20Addr } - if polyBFTConfig.MintableNativeToken { + if polyBFTConfig.NativeTokenConfig.IsMintable { // initialize NativeERC20Mintable SC params := &contractsapi.InitializeNativeERC20MintableFn{ Predicate_: contracts.ChildERC20PredicateContract, diff --git a/consensus/polybft/polybft_config.go b/consensus/polybft/polybft_config.go index 52c668b134..6cdc5e789a 100644 --- a/consensus/polybft/polybft_config.go +++ b/consensus/polybft/polybft_config.go @@ -37,9 +37,6 @@ type PolyBFTConfig struct { // Governance is the initial governance address Governance types.Address `json:"governance"` - // MintableNativeToken denotes whether mintable native token is used - MintableNativeToken bool `json:"mintableNative"` - // NativeTokenConfig defines name, symbol and decimal count of the native token NativeTokenConfig *TokenConfig `json:"nativeTokenConfig"` @@ -239,9 +236,10 @@ func (r *RootchainConfig) ToBridgeConfig() *BridgeConfig { // TokenConfig is the configuration of native token used by edge network type TokenConfig struct { - Name string `json:"name"` - Symbol string `json:"symbol"` - Decimals uint8 `json:"decimals"` + Name string `json:"name"` + Symbol string `json:"symbol"` + Decimals uint8 `json:"decimals"` + IsMintable bool `json:"isMintable"` } type RewardsConfig struct { diff --git a/e2e-polybft/e2e/consensus_test.go b/e2e-polybft/e2e/consensus_test.go index a3c00049e6..0c822d7fa3 100644 --- a/e2e-polybft/e2e/consensus_test.go +++ b/e2e-polybft/e2e/consensus_test.go @@ -461,8 +461,7 @@ func TestE2E_Consensus_MintableERC20NativeToken(t *testing.T) { cluster := framework.NewTestCluster(t, validatorCount, - framework.WithMintableNativeToken(true), - framework.WithNativeTokenConfig(fmt.Sprintf("%s:%s:%d", tokenName, tokenSymbol, decimals)), + framework.WithNativeTokenConfig(fmt.Sprintf("%s:%s:%d:true", tokenName, tokenSymbol, decimals)), framework.WithEpochSize(epochSize), framework.WithSecretsCallback(func(addrs []types.Address, config *framework.TestClusterConfig) { for i, addr := range addrs { diff --git a/e2e-polybft/framework/test-cluster.go b/e2e-polybft/framework/test-cluster.go index b291ea290d..f2c594b5e5 100644 --- a/e2e-polybft/framework/test-cluster.go +++ b/e2e-polybft/framework/test-cluster.go @@ -76,7 +76,6 @@ type TestClusterConfig struct { Premine []string // address[:amount] PremineValidators []string // address[:amount] StakeAmounts []string // address[:amount] - MintableNativeToken bool WithoutBridge bool BootnodeCount int NonValidatorCount int @@ -200,12 +199,6 @@ func WithPremine(addresses ...types.Address) ClusterOption { } } -func WithMintableNativeToken(mintableToken bool) ClusterOption { - return func(h *TestClusterConfig) { - h.MintableNativeToken = mintableToken - } -} - func WithSecretsCallback(fn func([]types.Address, *TestClusterConfig)) ClusterOption { return func(h *TestClusterConfig) { h.SecretsCallback = fn @@ -478,10 +471,6 @@ func NewTestCluster(t *testing.T, validatorsCount int, opts ...ClusterOption) *T } } - if cluster.Config.MintableNativeToken { - args = append(args, "--mintable-native-token") - } - if len(cluster.Config.BurnContracts) != 0 { for block, addr := range cluster.Config.BurnContracts { args = append(args, "--burn-contract", fmt.Sprintf("%d:%s", block, addr))