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

Remove ';' delimiting support from ParseDecCoins #3951

Merged
merged 3 commits into from
Mar 25, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#3915 Remove ';' delimiting support from ParseDecCoins
20 changes: 16 additions & 4 deletions server/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package config

import (
"fmt"
"strings"

sdk "github.com/cosmos/cosmos-sdk/types"
)
Expand All @@ -14,7 +15,7 @@ const (
type BaseConfig struct {
// The minimum gas prices a validator is willing to accept for processing a
// transaction. A transaction's fees must meet the minimum of any denomination
// specified in this config (e.g. 0.01photino;0.0001stake).
// specified in this config (e.g. 0.25token1;0.0001token2).
MinGasPrices string `mapstructure:"minimum-gas-prices"`
}

Expand All @@ -31,9 +32,20 @@ func (c *Config) SetMinGasPrices(gasPrices sdk.DecCoins) {
// GetMinGasPrices returns the validator's minimum gas prices based on the set
// configuration.
func (c *Config) GetMinGasPrices() sdk.DecCoins {
gasPrices, err := sdk.ParseDecCoins(c.MinGasPrices)
if err != nil {
panic(fmt.Sprintf("invalid minimum gas prices: %v", err))
if c.MinGasPrices == "" {
return sdk.DecCoins{}
}

gasPricesStr := strings.Split(c.MinGasPrices, ";")
gasPrices := make(sdk.DecCoins, len(gasPricesStr))
Copy link
Contributor

Choose a reason for hiding this comment

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

? Wouldn't it be better to have a NewDecCoins(...) constructor kind of thing?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't think so.


for i, s := range gasPricesStr {
gasPrice, err := sdk.ParseDecCoin(s)
if err != nil {
panic(fmt.Errorf("failed to parse minimum gas price coin (%s): %s", s, err))
}

gasPrices[i] = gasPrice
}

return gasPrices
Expand Down
2 changes: 1 addition & 1 deletion server/config/toml.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const defaultConfigTemplate = `# This is a TOML config file.

# The minimum gas prices a validator is willing to accept for processing a
# transaction. A transaction's fees must meet the minimum of any denomination
# specified in this config (e.g. 0.01photino;0.0001stake).
# specified in this config (e.g. 0.25token1;0.0001token2).
minimum-gas-prices = "{{ .BaseConfig.MinGasPrices }}"
`

Expand Down
12 changes: 7 additions & 5 deletions types/coin.go
Original file line number Diff line number Diff line change
Expand Up @@ -589,25 +589,27 @@ func ParseCoin(coinStr string) (coin Coin, err error) {
// ParseCoins will parse out a list of coins separated by commas.
// If nothing is provided, it returns nil Coins.
// Returned coins are sorted.
func ParseCoins(coinsStr string) (coins Coins, err error) {
func ParseCoins(coinsStr string) (Coins, error) {
coinsStr = strings.TrimSpace(coinsStr)
if len(coinsStr) == 0 {
return nil, nil
}

coinStrs := strings.Split(coinsStr, ",")
for _, coinStr := range coinStrs {
coins := make(Coins, len(coinStrs))
for i, coinStr := range coinStrs {
coin, err := ParseCoin(coinStr)
if err != nil {
return nil, err
}
coins = append(coins, coin)

coins[i] = coin
}

// Sort coins for determinism.
// sort coins for determinism
coins.Sort()

// Validate coins before returning.
// validate coins before returning
if !coins.IsValid() {
return nil, fmt.Errorf("parseCoins invalid: %#v", coins)
}
Expand Down
11 changes: 5 additions & 6 deletions types/dec_coin.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package types

import (
"fmt"
"regexp"
"sort"
"strings"

Expand Down Expand Up @@ -545,21 +544,21 @@ func ParseDecCoin(coinStr string) (coin DecCoin, err error) {
// ParseDecCoins will parse out a list of decimal coins separated by commas.
// If nothing is provided, it returns nil DecCoins. Returned decimal coins are
// sorted.
func ParseDecCoins(coinsStr string) (coins DecCoins, err error) {
func ParseDecCoins(coinsStr string) (DecCoins, error) {
coinsStr = strings.TrimSpace(coinsStr)
if len(coinsStr) == 0 {
return nil, nil
}

splitRe := regexp.MustCompile(",|;")
coinStrs := splitRe.Split(coinsStr, -1)
for _, coinStr := range coinStrs {
coinStrs := strings.Split(coinsStr, ",")
coins := make(DecCoins, len(coinStrs))
for i, coinStr := range coinStrs {
coin, err := ParseDecCoin(coinStr)
if err != nil {
return nil, err
}

coins = append(coins, coin)
coins[i] = coin
}

// sort coins for determinism
Expand Down