Skip to content

Commit

Permalink
Provide mintable parameter as a part of native token config (#1472)
Browse files Browse the repository at this point in the history
* Provide mintable parameter as a part of native token config

* Change mintable flag to boolean and make it mandatory

* Minor change
  • Loading branch information
Stefan-Ethernal authored May 5, 2023
1 parent 0276776 commit 062414f
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 50 deletions.
9 changes: 1 addition & 8 deletions command/genesis/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,18 +219,11 @@ func setFlags(cmd *cobra.Command) {
"trie root from the corresponding triedb",
)

cmd.Flags().BoolVar(
&params.mintableNativeToken,
mintableTokenFlag,
false,
"flag indicate whether mintable or non-mintable native token is deployed",
)

cmd.Flags().StringVar(
&params.nativeTokenConfigRaw,
nativeTokenConfigFlag,
"",
"configuration of native token in format <name:symbol:decimals count>",
"configuration of native token in format <name:symbol:decimals count:mintable flag>",
)

cmd.Flags().StringVar(
Expand Down
43 changes: 23 additions & 20 deletions command/genesis/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,14 @@ 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"

defaultNativeTokenName = "Polygon"
defaultNativeTokenSymbol = "MATIC"
defaultNativeTokenDecimals = uint8(18)
nativeTokenParamsNumber = 3
nativeTokenParamsNumber = 4
)

// Legacy flags that need to be preserved for running clients
Expand All @@ -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 <name:symbol:decimals count>")
errInvalidTokenParams = errors.New("native token params were not submitted in proper format " +
"(<name:symbol:decimals count:mintable flag>)")
errRewardWalletAmountZero = errors.New("reward wallet amount can not be zero or negative")
)

Expand Down Expand Up @@ -122,7 +121,6 @@ type genesisParams struct {
bridgeBlockListAdmin []string
bridgeBlockListEnabled []string

mintableNativeToken bool
nativeTokenConfigRaw string
nativeTokenConfig *polybft.TokenConfig

Expand Down Expand Up @@ -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
}

Expand All @@ -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
}
Expand Down
3 changes: 1 addition & 2 deletions command/genesis/polybft_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion consensus/polybft/polybft.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
10 changes: 4 additions & 6 deletions consensus/polybft/polybft_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`

Expand Down Expand Up @@ -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 {
Expand Down
3 changes: 1 addition & 2 deletions e2e-polybft/e2e/consensus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
11 changes: 0 additions & 11 deletions e2e-polybft/framework/test-cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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))
Expand Down

0 comments on commit 062414f

Please sign in to comment.