Skip to content

Commit

Permalink
Merge PR cosmos#3951: Remove ';' delimiting support from ParseDecCoins
Browse files Browse the repository at this point in the history
  • Loading branch information
alexanderbez authored Mar 25, 2019
1 parent 294ac8e commit dd7de2a
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 16 deletions.
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))

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

0 comments on commit dd7de2a

Please sign in to comment.