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

change the symbol minimum length to 2 #811

Merged
merged 3 commits into from
Dec 31, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
34 changes: 19 additions & 15 deletions app/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be put in the right place

# Block height of BEP8 upgrade
BEP8Height = {{ .UpgradeConfig.BEP8Height }}
# Block height of BEP67 upgrade
Expand Down Expand Up @@ -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"`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto


// TODO: add upgrade name
BEP8Height int64 `mapstructure:"BEP8Height"`
Expand All @@ -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,
}
}

Expand Down
96 changes: 93 additions & 3 deletions common/types/mini_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
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"
Expand Down Expand Up @@ -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")
Expand All @@ -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) {
Expand All @@ -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))
}
Expand Down
101 changes: 98 additions & 3 deletions common/types/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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")
Expand All @@ -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) {
Expand All @@ -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")
Expand Down Expand Up @@ -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))
}
Expand Down
9 changes: 5 additions & 4 deletions common/upgrade/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions plugins/dex/client/cli/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions plugins/dex/store/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down
Loading