From de5fff164e032188262c5780056cd83118042e62 Mon Sep 17 00:00:00 2001 From: "fletcher.fan" Date: Thu, 24 Dec 2020 13:31:37 +0800 Subject: [PATCH 1/3] change the symbol minimum length to 2 --- app/app.go | 1 + app/config/config.go | 34 ++++---- common/types/mini_token.go | 96 ++++++++++++++++++++- common/types/token.go | 101 ++++++++++++++++++++++- common/upgrade/upgrade.go | 9 +- plugins/dex/client/cli/list.go | 6 +- plugins/dex/store/utils.go | 4 +- plugins/tokens/client/cli/helper.go | 4 +- plugins/tokens/client/cli/issue.go | 6 +- plugins/tokens/client/cli/issue_mini.go | 2 +- plugins/tokens/client/cli/issue_tiny.go | 2 +- plugins/tokens/client/cli/seturi_mini.go | 2 +- 12 files changed, 229 insertions(+), 38 deletions(-) diff --git a/app/app.go b/app/app.go index dc59e7382..f5db2a001 100644 --- a/app/app.go +++ b/app/app.go @@ -316,6 +316,7 @@ func SetUpgradeConfig(upgradeConfig *config.UpgradeConfig) { upgrade.Mgr.AddUpgradeHeight(upgrade.ListingRuleUpgrade, upgradeConfig.ListingRuleUpgradeHeight) upgrade.Mgr.AddUpgradeHeight(upgrade.FixZeroBalance, upgradeConfig.FixZeroBalanceHeight) upgrade.Mgr.AddUpgradeHeight(upgrade.LaunchBscUpgrade, upgradeConfig.LaunchBscUpgradeHeight) + upgrade.Mgr.AddUpgradeHeight(upgrade.AdjustTokenSymbolLength, upgradeConfig.AdjustTokenSymbolLengthHeight) upgrade.Mgr.AddUpgradeHeight(upgrade.BEP8, upgradeConfig.BEP8Height) upgrade.Mgr.AddUpgradeHeight(upgrade.BEP67, upgradeConfig.BEP67Height) diff --git a/app/config/config.go b/app/config/config.go index d9493c3ca..4068e71d4 100644 --- a/app/config/config.go +++ b/app/config/config.go @@ -67,6 +67,8 @@ ListingRuleUpgradeHeight = {{ .UpgradeConfig.ListingRuleUpgradeHeight }} FixZeroBalanceHeight = {{ .UpgradeConfig.FixZeroBalanceHeight }} # Block height of smart chain upgrade LaunchBscUpgradeHeight = {{ .UpgradeConfig.LaunchBscUpgradeHeight }} +# Block height of token length adjustment upgrade +AdjustTokenSymbolLengthHeight = {{ .UpgradeConfig.AdjustTokenSymbolLengthHeight }} # Block height of BEP8 upgrade BEP8Height = {{ .UpgradeConfig.BEP8Height }} # Block height of BEP67 upgrade @@ -487,7 +489,8 @@ type UpgradeConfig struct { ListingRuleUpgradeHeight int64 `mapstructure:"ListingRuleUpgradeHeight"` FixZeroBalanceHeight int64 `mapstructure:"FixZeroBalanceHeight"` // TODO: add upgrade name - LaunchBscUpgradeHeight int64 `mapstructure:"LaunchBscUpgradeHeight"` + LaunchBscUpgradeHeight int64 `mapstructure:"LaunchBscUpgradeHeight"` + AdjustTokenSymbolLengthHeight int64 `mapstructure:"AdjustTokenSymbolLengthHeight"` // TODO: add upgrade name BEP8Height int64 `mapstructure:"BEP8Height"` @@ -498,20 +501,21 @@ type UpgradeConfig struct { func defaultUpgradeConfig() *UpgradeConfig { // make the upgraded functions enabled by default return &UpgradeConfig{ - BEP6Height: 1, - BEP9Height: 1, - BEP10Height: 1, - BEP19Height: 1, - BEP12Height: 1, - BEP3Height: 1, - FixSignBytesOverflowHeight: 1, - LotSizeUpgradeHeight: 1, - ListingRuleUpgradeHeight: 1, - FixZeroBalanceHeight: 1, - BEP8Height: 1, - BEP67Height: 1, - BEP70Height: 1, - LaunchBscUpgradeHeight: math.MaxInt64, + BEP6Height: 1, + BEP9Height: 1, + BEP10Height: 1, + BEP19Height: 1, + BEP12Height: 1, + BEP3Height: 1, + FixSignBytesOverflowHeight: 1, + LotSizeUpgradeHeight: 1, + ListingRuleUpgradeHeight: 1, + FixZeroBalanceHeight: 1, + BEP8Height: 1, + BEP67Height: 1, + BEP70Height: 1, + LaunchBscUpgradeHeight: 1, + AdjustTokenSymbolLengthHeight: math.MaxInt64, } } diff --git a/common/types/mini_token.go b/common/types/mini_token.go index e02227a7b..76e05b2ca 100644 --- a/common/types/mini_token.go +++ b/common/types/mini_token.go @@ -4,6 +4,7 @@ import ( "bytes" "errors" "fmt" + "github.com/binance-chain/node/common/upgrade" "regexp" "strings" @@ -15,6 +16,7 @@ import ( const ( MiniTokenSymbolMaxLen = 8 MiniTokenSymbolMinLen = 3 + MiniTokenSymbolNewMinLen = 2 MiniTokenSymbolSuffixLen = 4 // probably enough. if it collides (unlikely) the issuer can just use another tx. MiniTokenSymbolTxHashSuffixLen = 3 // probably enough. if it collides (unlikely) the issuer can just use another tx. MiniTokenSymbolMSuffix = "M" @@ -158,6 +160,10 @@ func IsValidMiniTokenSymbol(symbol string) bool { return ValidateMiniTokenSymbol(symbol) == nil } +func IsValidMiniTokenSymbolLocal(symbol string) bool { + return ValidateMiniTokenSymbolLocal(symbol) == nil +} + func ValidateIssueMiniSymbol(symbol string) error { if len(symbol) == 0 { return errors.New("token symbol cannot be empty") @@ -169,8 +175,37 @@ func ValidateIssueMiniSymbol(symbol string) error { } // check len without suffix - if symbolLen := len(symbol); symbolLen > MiniTokenSymbolMaxLen || symbolLen < MiniTokenSymbolMinLen { - return errors.New("length of token symbol is limited to 3~8") + symbolLen := len(symbol) + if sdk.IsUpgrade(upgrade.AdjustTokenSymbolLength) { + if symbolLen > MiniTokenSymbolMaxLen || symbolLen < MiniTokenSymbolNewMinLen { + return errors.New("length of token symbol is limited to 2~8") + } + } else { + if symbolLen > MiniTokenSymbolMaxLen || symbolLen < MiniTokenSymbolMinLen { + return errors.New("length of token symbol is limited to 3~8") + } + } + + if !utils.IsAlphaNum(symbol) { + return errors.New("token symbol should be alphanumeric") + } + + return nil +} + +func ValidateIssueMiniSymbolLocal(symbol string) error { + if len(symbol) == 0 { + return errors.New("token symbol cannot be empty") + } + + if symbol == NativeTokenSymbol || + symbol == NativeTokenSymbolDotBSuffixed { + return errors.New("symbol cannot be the same as native token") + } + + // check len without suffix + if symbolLen := len(symbol); symbolLen > MiniTokenSymbolMaxLen || symbolLen < MiniTokenSymbolNewMinLen { + return errors.New("length of token symbol is limited to 2~8") } if !utils.IsAlphaNum(symbol) { @@ -197,9 +232,64 @@ func ValidateMiniTokenSymbol(symbol string) error { symbolPart := parts[0] // check len without suffix - if len(symbolPart) < MiniTokenSymbolMinLen { + if sdk.IsUpgrade(upgrade.AdjustTokenSymbolLength) { + if len(symbolPart) < MiniTokenSymbolNewMinLen { + return fmt.Errorf("mini-token symbol part is too short, got %d chars", len(symbolPart)) + } + } else if len(symbolPart) < MiniTokenSymbolMinLen { + return fmt.Errorf("mini-token symbol part is too short, got %d chars", len(symbolPart)) + } + + if len(symbolPart) > MiniTokenSymbolMaxLen { + return fmt.Errorf("mini-token symbol part is too long, got %d chars", len(symbolPart)) + } + + if !utils.IsAlphaNum(symbolPart) { + return errors.New("mini-token symbol part should be alphanumeric") + } + + suffixPart := parts[1] + if len(suffixPart) != MiniTokenSymbolSuffixLen { + return fmt.Errorf("mini-token symbol suffix must be %d chars in length, got %d", MiniTokenSymbolSuffixLen, len(suffixPart)) + } + + if suffixPart[len(suffixPart)-1:] != MiniTokenSymbolMSuffix { + return fmt.Errorf("mini-token symbol suffix must end with M") + } + + // prohibit non-hexadecimal chars in the suffix part + isHex, err := regexp.MatchString(fmt.Sprintf("[0-9A-F]{%d}M", MiniTokenSymbolTxHashSuffixLen), suffixPart) + if err != nil { + return err + } + if !isHex { + return fmt.Errorf("mini-token symbol tx hash suffix must be hex with a length of %d", MiniTokenSymbolTxHashSuffixLen) + } + + return nil +} + +func ValidateMiniTokenSymbolLocal(symbol string) error { + if len(symbol) == 0 { + return errors.New("suffixed token symbol cannot be empty") + } + + if symbol == NativeTokenSymbol || + symbol == NativeTokenSymbolDotBSuffixed { + return errors.New("symbol cannot be the same as native token") + } + + parts, err := splitSuffixedTokenSymbol(symbol) + if err != nil { + return err + } + + symbolPart := parts[0] + // check len without suffix + if len(symbolPart) < MiniTokenSymbolNewMinLen { return fmt.Errorf("mini-token symbol part is too short, got %d chars", len(symbolPart)) } + if len(symbolPart) > MiniTokenSymbolMaxLen { return fmt.Errorf("mini-token symbol part is too long, got %d chars", len(symbolPart)) } diff --git a/common/types/token.go b/common/types/token.go index 8e4be05d9..d25e1362f 100644 --- a/common/types/token.go +++ b/common/types/token.go @@ -9,12 +9,14 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/binance-chain/node/common/upgrade" "github.com/binance-chain/node/common/utils" ) const ( TokenSymbolMaxLen = 8 TokenSymbolMinLen = 3 + TokenSymbolNewMinLen = 2 TokenSymbolTxHashSuffixLen = 3 // probably enough. if it collides (unlikely) the issuer can just use another tx. TokenSymbolDotBSuffix = ".B" @@ -125,6 +127,28 @@ func (token Token) String() string { token.Name, token.Symbol, token.TotalSupply, token.Owner, token.Mintable) } +func ValidateIssueSymbolLocal(symbol string) error { + if len(symbol) == 0 { + return errors.New("token symbol cannot be empty") + } + + if strings.HasSuffix(symbol, TokenSymbolDotBSuffix) { + symbol = strings.TrimSuffix(symbol, TokenSymbolDotBSuffix) + } + + // check len without .B suffix + symbolLen := len(symbol) + if symbolLen > TokenSymbolMaxLen || symbolLen < TokenSymbolNewMinLen { + return errors.New("length of token symbol is limited to 2~8") + } + + if !utils.IsAlphaNum(symbol) { + return errors.New("token symbol should be alphanumeric") + } + + return nil +} + func ValidateIssueSymbol(symbol string) error { if len(symbol) == 0 { return errors.New("token symbol cannot be empty") @@ -135,8 +159,15 @@ func ValidateIssueSymbol(symbol string) error { } // check len without .B suffix - if symbolLen := len(symbol); symbolLen > TokenSymbolMaxLen || symbolLen < TokenSymbolMinLen { - return errors.New("length of token symbol is limited to 3~8") + symbolLen := len(symbol) + if sdk.IsUpgrade(upgrade.AdjustTokenSymbolLength) { + if symbolLen > TokenSymbolMaxLen || symbolLen < TokenSymbolNewMinLen { + return errors.New("length of token symbol is limited to 2~8") + } + } else { + if symbolLen > TokenSymbolMaxLen || symbolLen < TokenSymbolMinLen { + return errors.New("length of token symbol is limited to 3~8") + } } if !utils.IsAlphaNum(symbol) { @@ -156,6 +187,65 @@ func ValidateTokenSymbols(coins sdk.Coins) error { return nil } +func ValidateTokenSymbolLocal(symbol string) error { + if len(symbol) == 0 { + return errors.New("suffixed token symbol cannot be empty") + } + + // suffix exception for native token (less drama in existing tests) + if symbol == NativeTokenSymbol || + symbol == NativeTokenSymbolDotBSuffixed { + return nil + } + + parts, err := splitSuffixedTokenSymbol(symbol) + if err != nil { + return err + } + + symbolPart := parts[0] + + // since the native token was given a suffix exception above, do not allow it to have a suffix + if symbolPart == NativeTokenSymbol || + symbolPart == NativeTokenSymbolDotBSuffixed { + return errors.New("native token symbol should not be suffixed with tx hash") + } + + if strings.HasSuffix(symbolPart, TokenSymbolDotBSuffix) { + symbolPart = strings.TrimSuffix(symbolPart, TokenSymbolDotBSuffix) + } + + // check len without .B suffix + if len(symbolPart) < TokenSymbolNewMinLen { + return fmt.Errorf("token symbol part is too short, got %d chars", len(symbolPart)) + } + + if len(symbolPart) > TokenSymbolMaxLen { + return fmt.Errorf("token symbol part is too long, got %d chars", len(symbolPart)) + } + + if !utils.IsAlphaNum(symbolPart) { + return errors.New("token symbol part should be alphanumeric") + } + + txHashPart := parts[1] + + if len(txHashPart) != TokenSymbolTxHashSuffixLen { + return fmt.Errorf("token symbol tx hash suffix must be %d chars in length, got %d", TokenSymbolTxHashSuffixLen, len(txHashPart)) + } + + // prohibit non-hexadecimal chars in the suffix part + isHex, err := regexp.MatchString(fmt.Sprintf("[0-9A-F]{%d}", TokenSymbolTxHashSuffixLen), txHashPart) + if err != nil { + return err + } + if !isHex { + return fmt.Errorf("token symbol tx hash suffix must be hex with a length of %d", TokenSymbolTxHashSuffixLen) + } + + return nil +} + func ValidateTokenSymbol(symbol string) error { if len(symbol) == 0 { return errors.New("suffixed token symbol cannot be empty") @@ -185,9 +275,14 @@ func ValidateTokenSymbol(symbol string) error { } // check len without .B suffix - if len(symbolPart) < TokenSymbolMinLen { + if sdk.IsUpgrade(upgrade.AdjustTokenSymbolLength) { + if len(symbolPart) < TokenSymbolNewMinLen { + return fmt.Errorf("token symbol part is too short, got %d chars", len(symbolPart)) + } + } else if len(symbolPart) < TokenSymbolMinLen { return fmt.Errorf("token symbol part is too short, got %d chars", len(symbolPart)) } + if len(symbolPart) > TokenSymbolMaxLen { return fmt.Errorf("token symbol part is too long, got %d chars", len(symbolPart)) } diff --git a/common/upgrade/upgrade.go b/common/upgrade/upgrade.go index cb75de802..94a0ba26c 100644 --- a/common/upgrade/upgrade.go +++ b/common/upgrade/upgrade.go @@ -18,10 +18,11 @@ const ( // Archimedes Upgrade BEP3 = sdk.BEP3 // https://github.com/binance-chain/BEPs/pull/30 // Heisenberg Upgrade - FixSignBytesOverflow = sdk.FixSignBytesOverflow - LotSizeOptimization = "LotSizeOptimization" - ListingRuleUpgrade = "ListingRuleUpgrade" // Remove restriction that only the owner of base asset can list trading pair - FixZeroBalance = "FixZeroBalance" + FixSignBytesOverflow = sdk.FixSignBytesOverflow + LotSizeOptimization = "LotSizeOptimization" + ListingRuleUpgrade = "ListingRuleUpgrade" // Remove restriction that only the owner of base asset can list trading pair + FixZeroBalance = "FixZeroBalance" + AdjustTokenSymbolLength = "AdjustTokenSymbolLength" // TODO: add upgrade name LaunchBscUpgrade = sdk.LaunchBscUpgrade diff --git a/plugins/dex/client/cli/list.go b/plugins/dex/client/cli/list.go index dccac14dc..79a056833 100644 --- a/plugins/dex/client/cli/list.go +++ b/plugins/dex/client/cli/list.go @@ -33,13 +33,13 @@ func listTradingPairCmd(cdc *wire.Codec) *cobra.Command { } baseAsset := viper.GetString(flagBaseAsset) - err = types.ValidateTokenSymbol(baseAsset) + err = types.ValidateTokenSymbolLocal(baseAsset) if err != nil { return err } quoteAsset := viper.GetString(flagQuoteAsset) - err = types.ValidateTokenSymbol(quoteAsset) + err = types.ValidateTokenSymbolLocal(quoteAsset) if err != nil { return err } @@ -89,7 +89,7 @@ func listMiniTradingPairCmd(cdc *wire.Codec) *cobra.Command { } baseAsset := viper.GetString(flagBaseAsset) - err = types.ValidateMiniTokenSymbol(baseAsset) + err = types.ValidateMiniTokenSymbolLocal(baseAsset) if err != nil { return err } diff --git a/plugins/dex/store/utils.go b/plugins/dex/store/utils.go index 3f39a594b..e573a45cf 100644 --- a/plugins/dex/store/utils.go +++ b/plugins/dex/store/utils.go @@ -18,10 +18,10 @@ func ValidatePairSymbol(symbol string) error { return errors.New("invalid symbol: trading pair must contain an underscore ('_')") } for _, tokenSymbol := range tokenSymbols { - if types.IsValidMiniTokenSymbol(tokenSymbol) { + if types.IsValidMiniTokenSymbolLocal(tokenSymbol) { continue } - if err := types.ValidateTokenSymbol(tokenSymbol); err != nil { + if err := types.ValidateTokenSymbolLocal(tokenSymbol); err != nil { return err } } diff --git a/plugins/tokens/client/cli/helper.go b/plugins/tokens/client/cli/helper.go index 07fd494f3..90ad3c0c2 100644 --- a/plugins/tokens/client/cli/helper.go +++ b/plugins/tokens/client/cli/helper.go @@ -33,8 +33,8 @@ func (c Commander) checkAndSendTx(cmd *cobra.Command, args []string, builder msg } symbol := viper.GetString(flagSymbol) - if !types.IsValidMiniTokenSymbol(symbol) { - err = types.ValidateTokenSymbol(symbol) + if !types.IsValidMiniTokenSymbolLocal(symbol) { + err = types.ValidateTokenSymbolLocal(symbol) if err != nil { return err } diff --git a/plugins/tokens/client/cli/issue.go b/plugins/tokens/client/cli/issue.go index 222e51a8b..960456647 100644 --- a/plugins/tokens/client/cli/issue.go +++ b/plugins/tokens/client/cli/issue.go @@ -59,7 +59,7 @@ func (c Commander) issueToken(cmd *cobra.Command, args []string) error { } symbol := viper.GetString(flagSymbol) - err = types.ValidateIssueSymbol(symbol) + err = types.ValidateIssueSymbolLocal(symbol) if err != nil { return err } @@ -87,13 +87,13 @@ func (c Commander) mintToken(cmd *cobra.Command, args []string) error { symbol := viper.GetString(flagSymbol) amount := viper.GetInt64(flagAmount) - if types.IsValidMiniTokenSymbol(strings.ToUpper(symbol)) { + if types.IsValidMiniTokenSymbolLocal(strings.ToUpper(symbol)) { err = checkMiniTokenSupplyAmount(amount) if err != nil { return err } } else { - err = types.ValidateTokenSymbol(symbol) + err = types.ValidateTokenSymbolLocal(symbol) if err != nil { return err } diff --git a/plugins/tokens/client/cli/issue_mini.go b/plugins/tokens/client/cli/issue_mini.go index 80190062c..ab2dc6be3 100644 --- a/plugins/tokens/client/cli/issue_mini.go +++ b/plugins/tokens/client/cli/issue_mini.go @@ -45,7 +45,7 @@ func (c Commander) issueMiniToken(cmd *cobra.Command, args []string) error { } symbol := viper.GetString(flagSymbol) - err = types.ValidateIssueMiniSymbol(symbol) + err = types.ValidateIssueMiniSymbolLocal(symbol) if err != nil { return err } diff --git a/plugins/tokens/client/cli/issue_tiny.go b/plugins/tokens/client/cli/issue_tiny.go index b0fb3a9e5..d2f541c29 100644 --- a/plugins/tokens/client/cli/issue_tiny.go +++ b/plugins/tokens/client/cli/issue_tiny.go @@ -39,7 +39,7 @@ func (c Commander) issueTinyToken(cmd *cobra.Command, args []string) error { } symbol := viper.GetString(flagSymbol) - err = types.ValidateIssueMiniSymbol(symbol) + err = types.ValidateIssueMiniSymbolLocal(symbol) if err != nil { return err } diff --git a/plugins/tokens/client/cli/seturi_mini.go b/plugins/tokens/client/cli/seturi_mini.go index b92bb496c..eff149d80 100644 --- a/plugins/tokens/client/cli/seturi_mini.go +++ b/plugins/tokens/client/cli/seturi_mini.go @@ -28,7 +28,7 @@ func (c Commander) setTokenURI(cmd *cobra.Command, args []string) error { return err } symbol := viper.GetString(flagSymbol) - err = types.ValidateMiniTokenSymbol(symbol) + err = types.ValidateMiniTokenSymbolLocal(symbol) if err != nil { return err } From 86ae1ac1130a0c7552cdaedde67a55e80f7e4e23 Mon Sep 17 00:00:00 2001 From: "fletcher.fan" Date: Mon, 28 Dec 2020 17:44:16 +0800 Subject: [PATCH 2/3] format fix --- common/types/mini_token.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/common/types/mini_token.go b/common/types/mini_token.go index 76e05b2ca..67b65ca00 100644 --- a/common/types/mini_token.go +++ b/common/types/mini_token.go @@ -4,12 +4,12 @@ import ( "bytes" "errors" "fmt" - "github.com/binance-chain/node/common/upgrade" "regexp" "strings" sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/binance-chain/node/common/upgrade" "github.com/binance-chain/node/common/utils" ) From 7ea79747aa452ecd8b489472beedcb6a00f02866 Mon Sep 17 00:00:00 2001 From: "fletcher.fan" Date: Wed, 30 Dec 2020 12:29:34 +0800 Subject: [PATCH 3/3] token symbol check for client and server --- app/app.go | 3 +- app/config/config.go | 9 ++- common/types/mini_token.go | 90 ++-------------------- common/types/token.go | 95 ++---------------------- common/types/token_test.go | 1 + common/upgrade/upgrade.go | 11 +-- plugins/dex/client/cli/list.go | 6 +- plugins/dex/store/utils.go | 4 +- plugins/tokens/client/cli/helper.go | 4 +- plugins/tokens/client/cli/issue.go | 6 +- plugins/tokens/client/cli/issue_mini.go | 2 +- plugins/tokens/client/cli/issue_tiny.go | 2 +- plugins/tokens/client/cli/seturi_mini.go | 2 +- 13 files changed, 43 insertions(+), 192 deletions(-) diff --git a/app/app.go b/app/app.go index f5db2a001..8020245fc 100644 --- a/app/app.go +++ b/app/app.go @@ -316,12 +316,13 @@ func SetUpgradeConfig(upgradeConfig *config.UpgradeConfig) { upgrade.Mgr.AddUpgradeHeight(upgrade.ListingRuleUpgrade, upgradeConfig.ListingRuleUpgradeHeight) upgrade.Mgr.AddUpgradeHeight(upgrade.FixZeroBalance, upgradeConfig.FixZeroBalanceHeight) upgrade.Mgr.AddUpgradeHeight(upgrade.LaunchBscUpgrade, upgradeConfig.LaunchBscUpgradeHeight) - upgrade.Mgr.AddUpgradeHeight(upgrade.AdjustTokenSymbolLength, upgradeConfig.AdjustTokenSymbolLengthHeight) upgrade.Mgr.AddUpgradeHeight(upgrade.BEP8, upgradeConfig.BEP8Height) upgrade.Mgr.AddUpgradeHeight(upgrade.BEP67, upgradeConfig.BEP67Height) upgrade.Mgr.AddUpgradeHeight(upgrade.BEP70, upgradeConfig.BEP70Height) + upgrade.Mgr.AddUpgradeHeight(upgrade.AdjustTokenSymbolLength, upgradeConfig.AdjustTokenSymbolLengthHeight) + // register store keys of upgrade upgrade.Mgr.RegisterStoreKeys(upgrade.BEP9, common.TimeLockStoreKey.Name()) upgrade.Mgr.RegisterStoreKeys(upgrade.BEP3, common.AtomicSwapStoreKey.Name()) diff --git a/app/config/config.go b/app/config/config.go index 4068e71d4..30558f9a6 100644 --- a/app/config/config.go +++ b/app/config/config.go @@ -67,14 +67,14 @@ ListingRuleUpgradeHeight = {{ .UpgradeConfig.ListingRuleUpgradeHeight }} FixZeroBalanceHeight = {{ .UpgradeConfig.FixZeroBalanceHeight }} # Block height of smart chain upgrade LaunchBscUpgradeHeight = {{ .UpgradeConfig.LaunchBscUpgradeHeight }} -# Block height of token length adjustment upgrade -AdjustTokenSymbolLengthHeight = {{ .UpgradeConfig.AdjustTokenSymbolLengthHeight }} # Block height of BEP8 upgrade BEP8Height = {{ .UpgradeConfig.BEP8Height }} # Block height of BEP67 upgrade BEP67Height = {{ .UpgradeConfig.BEP67Height }} # Block height of BEP70 upgrade BEP70Height = {{ .UpgradeConfig.BEP70Height }} +# Block height of token length adjustment upgrade +AdjustTokenSymbolLengthHeight = {{ .UpgradeConfig.AdjustTokenSymbolLengthHeight }} [query] # ABCI query interface black list, suggested value: ["custom/gov/proposals", "custom/timelock/timelocks", "custom/atomicSwap/swapcreator", "custom/atomicSwap/swaprecipient"] @@ -489,13 +489,14 @@ type UpgradeConfig struct { ListingRuleUpgradeHeight int64 `mapstructure:"ListingRuleUpgradeHeight"` FixZeroBalanceHeight int64 `mapstructure:"FixZeroBalanceHeight"` // TODO: add upgrade name - LaunchBscUpgradeHeight int64 `mapstructure:"LaunchBscUpgradeHeight"` - AdjustTokenSymbolLengthHeight int64 `mapstructure:"AdjustTokenSymbolLengthHeight"` + LaunchBscUpgradeHeight int64 `mapstructure:"LaunchBscUpgradeHeight"` // TODO: add upgrade name BEP8Height int64 `mapstructure:"BEP8Height"` BEP67Height int64 `mapstructure:"BEP67Height"` BEP70Height int64 `mapstructure:"BEP70Height"` + + AdjustTokenSymbolLengthHeight int64 `mapstructure:"AdjustTokenSymbolLengthHeight"` } func defaultUpgradeConfig() *UpgradeConfig { diff --git a/common/types/mini_token.go b/common/types/mini_token.go index 67b65ca00..1cb7842cb 100644 --- a/common/types/mini_token.go +++ b/common/types/mini_token.go @@ -160,10 +160,8 @@ func IsValidMiniTokenSymbol(symbol string) bool { return ValidateMiniTokenSymbol(symbol) == nil } -func IsValidMiniTokenSymbolLocal(symbol string) bool { - return ValidateMiniTokenSymbolLocal(symbol) == nil -} - +// This function is used by both client and server side, and the client needs to use MiniTokenSymbolNewMinLen for the validation. +// If the UpgradeMgr.GetHeight == 0, that indicates the function is invoked by client side, and we should use MiniTokenSymbolNewMinLen func ValidateIssueMiniSymbol(symbol string) error { if len(symbol) == 0 { return errors.New("token symbol cannot be empty") @@ -176,36 +174,12 @@ func ValidateIssueMiniSymbol(symbol string) error { // check len without suffix symbolLen := len(symbol) - if sdk.IsUpgrade(upgrade.AdjustTokenSymbolLength) { + if sdk.UpgradeMgr.GetHeight() == 0 || sdk.IsUpgrade(upgrade.AdjustTokenSymbolLength) { if symbolLen > MiniTokenSymbolMaxLen || symbolLen < MiniTokenSymbolNewMinLen { return errors.New("length of token symbol is limited to 2~8") } - } else { - if symbolLen > MiniTokenSymbolMaxLen || symbolLen < MiniTokenSymbolMinLen { - return errors.New("length of token symbol is limited to 3~8") - } - } - - if !utils.IsAlphaNum(symbol) { - return errors.New("token symbol should be alphanumeric") - } - - return nil -} - -func ValidateIssueMiniSymbolLocal(symbol string) error { - if len(symbol) == 0 { - return errors.New("token symbol cannot be empty") - } - - if symbol == NativeTokenSymbol || - symbol == NativeTokenSymbolDotBSuffixed { - return errors.New("symbol cannot be the same as native token") - } - - // check len without suffix - if symbolLen := len(symbol); symbolLen > MiniTokenSymbolMaxLen || symbolLen < MiniTokenSymbolNewMinLen { - return errors.New("length of token symbol is limited to 2~8") + } else if symbolLen > MiniTokenSymbolMaxLen || symbolLen < MiniTokenSymbolMinLen { + return errors.New("length of token symbol is limited to 3~8") } if !utils.IsAlphaNum(symbol) { @@ -232,7 +206,9 @@ func ValidateMiniTokenSymbol(symbol string) error { symbolPart := parts[0] // check len without suffix - if sdk.IsUpgrade(upgrade.AdjustTokenSymbolLength) { + // This function is used by both client and server side, and the client needs to use MiniTokenSymbolNewMinLen for the validation. + // If the UpgradeMgr.GetHeight == 0, that indicates the function is invoked by client side, and we should use MiniTokenSymbolNewMinLen + if sdk.UpgradeMgr.GetHeight() == 0 || sdk.IsUpgrade(upgrade.AdjustTokenSymbolLength) { if len(symbolPart) < MiniTokenSymbolNewMinLen { return fmt.Errorf("mini-token symbol part is too short, got %d chars", len(symbolPart)) } @@ -268,53 +244,3 @@ func ValidateMiniTokenSymbol(symbol string) error { return nil } - -func ValidateMiniTokenSymbolLocal(symbol string) error { - if len(symbol) == 0 { - return errors.New("suffixed token symbol cannot be empty") - } - - if symbol == NativeTokenSymbol || - symbol == NativeTokenSymbolDotBSuffixed { - return errors.New("symbol cannot be the same as native token") - } - - parts, err := splitSuffixedTokenSymbol(symbol) - if err != nil { - return err - } - - symbolPart := parts[0] - // check len without suffix - if len(symbolPart) < MiniTokenSymbolNewMinLen { - return fmt.Errorf("mini-token symbol part is too short, got %d chars", len(symbolPart)) - } - - if len(symbolPart) > MiniTokenSymbolMaxLen { - return fmt.Errorf("mini-token symbol part is too long, got %d chars", len(symbolPart)) - } - - if !utils.IsAlphaNum(symbolPart) { - return errors.New("mini-token symbol part should be alphanumeric") - } - - suffixPart := parts[1] - if len(suffixPart) != MiniTokenSymbolSuffixLen { - return fmt.Errorf("mini-token symbol suffix must be %d chars in length, got %d", MiniTokenSymbolSuffixLen, len(suffixPart)) - } - - if suffixPart[len(suffixPart)-1:] != MiniTokenSymbolMSuffix { - return fmt.Errorf("mini-token symbol suffix must end with M") - } - - // prohibit non-hexadecimal chars in the suffix part - isHex, err := regexp.MatchString(fmt.Sprintf("[0-9A-F]{%d}M", MiniTokenSymbolTxHashSuffixLen), suffixPart) - if err != nil { - return err - } - if !isHex { - return fmt.Errorf("mini-token symbol tx hash suffix must be hex with a length of %d", MiniTokenSymbolTxHashSuffixLen) - } - - return nil -} diff --git a/common/types/token.go b/common/types/token.go index d25e1362f..ccc81b2ce 100644 --- a/common/types/token.go +++ b/common/types/token.go @@ -127,28 +127,8 @@ func (token Token) String() string { token.Name, token.Symbol, token.TotalSupply, token.Owner, token.Mintable) } -func ValidateIssueSymbolLocal(symbol string) error { - if len(symbol) == 0 { - return errors.New("token symbol cannot be empty") - } - - if strings.HasSuffix(symbol, TokenSymbolDotBSuffix) { - symbol = strings.TrimSuffix(symbol, TokenSymbolDotBSuffix) - } - - // check len without .B suffix - symbolLen := len(symbol) - if symbolLen > TokenSymbolMaxLen || symbolLen < TokenSymbolNewMinLen { - return errors.New("length of token symbol is limited to 2~8") - } - - if !utils.IsAlphaNum(symbol) { - return errors.New("token symbol should be alphanumeric") - } - - return nil -} - +// This function is used by both client and server side, and the client needs to use TokenSymbolNewMinLen for the validation. +// If the UpgradeMgr.GetHeight == 0, that indicates the function is invoked by client side, and we should use TokenSymbolNewMinLen func ValidateIssueSymbol(symbol string) error { if len(symbol) == 0 { return errors.New("token symbol cannot be empty") @@ -160,14 +140,12 @@ func ValidateIssueSymbol(symbol string) error { // check len without .B suffix symbolLen := len(symbol) - if sdk.IsUpgrade(upgrade.AdjustTokenSymbolLength) { + if sdk.UpgradeMgr.GetHeight() == 0 || sdk.IsUpgrade(upgrade.AdjustTokenSymbolLength) { if symbolLen > TokenSymbolMaxLen || symbolLen < TokenSymbolNewMinLen { return errors.New("length of token symbol is limited to 2~8") } - } else { - if symbolLen > TokenSymbolMaxLen || symbolLen < TokenSymbolMinLen { - return errors.New("length of token symbol is limited to 3~8") - } + } else if symbolLen > TokenSymbolMaxLen || symbolLen < TokenSymbolMinLen { + return errors.New("length of token symbol is limited to 3~8") } if !utils.IsAlphaNum(symbol) { @@ -187,65 +165,6 @@ func ValidateTokenSymbols(coins sdk.Coins) error { return nil } -func ValidateTokenSymbolLocal(symbol string) error { - if len(symbol) == 0 { - return errors.New("suffixed token symbol cannot be empty") - } - - // suffix exception for native token (less drama in existing tests) - if symbol == NativeTokenSymbol || - symbol == NativeTokenSymbolDotBSuffixed { - return nil - } - - parts, err := splitSuffixedTokenSymbol(symbol) - if err != nil { - return err - } - - symbolPart := parts[0] - - // since the native token was given a suffix exception above, do not allow it to have a suffix - if symbolPart == NativeTokenSymbol || - symbolPart == NativeTokenSymbolDotBSuffixed { - return errors.New("native token symbol should not be suffixed with tx hash") - } - - if strings.HasSuffix(symbolPart, TokenSymbolDotBSuffix) { - symbolPart = strings.TrimSuffix(symbolPart, TokenSymbolDotBSuffix) - } - - // check len without .B suffix - if len(symbolPart) < TokenSymbolNewMinLen { - return fmt.Errorf("token symbol part is too short, got %d chars", len(symbolPart)) - } - - if len(symbolPart) > TokenSymbolMaxLen { - return fmt.Errorf("token symbol part is too long, got %d chars", len(symbolPart)) - } - - if !utils.IsAlphaNum(symbolPart) { - return errors.New("token symbol part should be alphanumeric") - } - - txHashPart := parts[1] - - if len(txHashPart) != TokenSymbolTxHashSuffixLen { - return fmt.Errorf("token symbol tx hash suffix must be %d chars in length, got %d", TokenSymbolTxHashSuffixLen, len(txHashPart)) - } - - // prohibit non-hexadecimal chars in the suffix part - isHex, err := regexp.MatchString(fmt.Sprintf("[0-9A-F]{%d}", TokenSymbolTxHashSuffixLen), txHashPart) - if err != nil { - return err - } - if !isHex { - return fmt.Errorf("token symbol tx hash suffix must be hex with a length of %d", TokenSymbolTxHashSuffixLen) - } - - return nil -} - func ValidateTokenSymbol(symbol string) error { if len(symbol) == 0 { return errors.New("suffixed token symbol cannot be empty") @@ -275,7 +194,9 @@ func ValidateTokenSymbol(symbol string) error { } // check len without .B suffix - if sdk.IsUpgrade(upgrade.AdjustTokenSymbolLength) { + // This function is used by both client and server side, and the client needs to use TokenSymbolNewMinLen for the validation. + // If the UpgradeMgr.GetHeight == 0, that indicates the function is invoked by client side, and we should use TokenSymbolNewMinLen + if sdk.UpgradeMgr.GetHeight() == 0 || sdk.IsUpgrade(upgrade.AdjustTokenSymbolLength) { if len(symbolPart) < TokenSymbolNewMinLen { return fmt.Errorf("token symbol part is too short, got %d chars", len(symbolPart)) } diff --git a/common/types/token_test.go b/common/types/token_test.go index e0e1f34da..50b847c92 100644 --- a/common/types/token_test.go +++ b/common/types/token_test.go @@ -67,6 +67,7 @@ var tokenMapperSymbolTestCases = []struct { } func TestNewToken(t *testing.T) { + sdk.UpgradeMgr.SetHeight(1) for _, tt := range tokenMapperSymbolTestCases { t.Run(tt.symbol, func(t *testing.T) { _, err := types.NewToken(tt.symbol, tt.symbol, 100000, sdk.AccAddress{}, false) diff --git a/common/upgrade/upgrade.go b/common/upgrade/upgrade.go index 94a0ba26c..e0e0474fb 100644 --- a/common/upgrade/upgrade.go +++ b/common/upgrade/upgrade.go @@ -18,11 +18,10 @@ const ( // Archimedes Upgrade BEP3 = sdk.BEP3 // https://github.com/binance-chain/BEPs/pull/30 // Heisenberg Upgrade - FixSignBytesOverflow = sdk.FixSignBytesOverflow - LotSizeOptimization = "LotSizeOptimization" - ListingRuleUpgrade = "ListingRuleUpgrade" // Remove restriction that only the owner of base asset can list trading pair - FixZeroBalance = "FixZeroBalance" - AdjustTokenSymbolLength = "AdjustTokenSymbolLength" + FixSignBytesOverflow = sdk.FixSignBytesOverflow + LotSizeOptimization = "LotSizeOptimization" + ListingRuleUpgrade = "ListingRuleUpgrade" // Remove restriction that only the owner of base asset can list trading pair + FixZeroBalance = "FixZeroBalance" // TODO: add upgrade name LaunchBscUpgrade = sdk.LaunchBscUpgrade @@ -31,6 +30,8 @@ const ( BEP8 = sdk.BEP8 // https://github.com/binance-chain/BEPs/pull/69 Mini token upgrade BEP67 = "BEP67" // https://github.com/binance-chain/BEPs/pull/67 Expiry time upgrade BEP70 = "BEP70" // https://github.com/binance-chain/BEPs/pull/70 BUSD Pair Upgrade + + AdjustTokenSymbolLength = "AdjustTokenSymbolLength" ) func UpgradeBEP10(before func(), after func()) { diff --git a/plugins/dex/client/cli/list.go b/plugins/dex/client/cli/list.go index 79a056833..dccac14dc 100644 --- a/plugins/dex/client/cli/list.go +++ b/plugins/dex/client/cli/list.go @@ -33,13 +33,13 @@ func listTradingPairCmd(cdc *wire.Codec) *cobra.Command { } baseAsset := viper.GetString(flagBaseAsset) - err = types.ValidateTokenSymbolLocal(baseAsset) + err = types.ValidateTokenSymbol(baseAsset) if err != nil { return err } quoteAsset := viper.GetString(flagQuoteAsset) - err = types.ValidateTokenSymbolLocal(quoteAsset) + err = types.ValidateTokenSymbol(quoteAsset) if err != nil { return err } @@ -89,7 +89,7 @@ func listMiniTradingPairCmd(cdc *wire.Codec) *cobra.Command { } baseAsset := viper.GetString(flagBaseAsset) - err = types.ValidateMiniTokenSymbolLocal(baseAsset) + err = types.ValidateMiniTokenSymbol(baseAsset) if err != nil { return err } diff --git a/plugins/dex/store/utils.go b/plugins/dex/store/utils.go index e573a45cf..3f39a594b 100644 --- a/plugins/dex/store/utils.go +++ b/plugins/dex/store/utils.go @@ -18,10 +18,10 @@ func ValidatePairSymbol(symbol string) error { return errors.New("invalid symbol: trading pair must contain an underscore ('_')") } for _, tokenSymbol := range tokenSymbols { - if types.IsValidMiniTokenSymbolLocal(tokenSymbol) { + if types.IsValidMiniTokenSymbol(tokenSymbol) { continue } - if err := types.ValidateTokenSymbolLocal(tokenSymbol); err != nil { + if err := types.ValidateTokenSymbol(tokenSymbol); err != nil { return err } } diff --git a/plugins/tokens/client/cli/helper.go b/plugins/tokens/client/cli/helper.go index 90ad3c0c2..07fd494f3 100644 --- a/plugins/tokens/client/cli/helper.go +++ b/plugins/tokens/client/cli/helper.go @@ -33,8 +33,8 @@ func (c Commander) checkAndSendTx(cmd *cobra.Command, args []string, builder msg } symbol := viper.GetString(flagSymbol) - if !types.IsValidMiniTokenSymbolLocal(symbol) { - err = types.ValidateTokenSymbolLocal(symbol) + if !types.IsValidMiniTokenSymbol(symbol) { + err = types.ValidateTokenSymbol(symbol) if err != nil { return err } diff --git a/plugins/tokens/client/cli/issue.go b/plugins/tokens/client/cli/issue.go index 960456647..222e51a8b 100644 --- a/plugins/tokens/client/cli/issue.go +++ b/plugins/tokens/client/cli/issue.go @@ -59,7 +59,7 @@ func (c Commander) issueToken(cmd *cobra.Command, args []string) error { } symbol := viper.GetString(flagSymbol) - err = types.ValidateIssueSymbolLocal(symbol) + err = types.ValidateIssueSymbol(symbol) if err != nil { return err } @@ -87,13 +87,13 @@ func (c Commander) mintToken(cmd *cobra.Command, args []string) error { symbol := viper.GetString(flagSymbol) amount := viper.GetInt64(flagAmount) - if types.IsValidMiniTokenSymbolLocal(strings.ToUpper(symbol)) { + if types.IsValidMiniTokenSymbol(strings.ToUpper(symbol)) { err = checkMiniTokenSupplyAmount(amount) if err != nil { return err } } else { - err = types.ValidateTokenSymbolLocal(symbol) + err = types.ValidateTokenSymbol(symbol) if err != nil { return err } diff --git a/plugins/tokens/client/cli/issue_mini.go b/plugins/tokens/client/cli/issue_mini.go index ab2dc6be3..80190062c 100644 --- a/plugins/tokens/client/cli/issue_mini.go +++ b/plugins/tokens/client/cli/issue_mini.go @@ -45,7 +45,7 @@ func (c Commander) issueMiniToken(cmd *cobra.Command, args []string) error { } symbol := viper.GetString(flagSymbol) - err = types.ValidateIssueMiniSymbolLocal(symbol) + err = types.ValidateIssueMiniSymbol(symbol) if err != nil { return err } diff --git a/plugins/tokens/client/cli/issue_tiny.go b/plugins/tokens/client/cli/issue_tiny.go index d2f541c29..b0fb3a9e5 100644 --- a/plugins/tokens/client/cli/issue_tiny.go +++ b/plugins/tokens/client/cli/issue_tiny.go @@ -39,7 +39,7 @@ func (c Commander) issueTinyToken(cmd *cobra.Command, args []string) error { } symbol := viper.GetString(flagSymbol) - err = types.ValidateIssueMiniSymbolLocal(symbol) + err = types.ValidateIssueMiniSymbol(symbol) if err != nil { return err } diff --git a/plugins/tokens/client/cli/seturi_mini.go b/plugins/tokens/client/cli/seturi_mini.go index eff149d80..b92bb496c 100644 --- a/plugins/tokens/client/cli/seturi_mini.go +++ b/plugins/tokens/client/cli/seturi_mini.go @@ -28,7 +28,7 @@ func (c Commander) setTokenURI(cmd *cobra.Command, args []string) error { return err } symbol := viper.GetString(flagSymbol) - err = types.ValidateMiniTokenSymbolLocal(symbol) + err = types.ValidateMiniTokenSymbol(symbol) if err != nil { return err }