Skip to content

Commit

Permalink
Use validate denom instead of denom vs lower(denom) comparisons
Browse files Browse the repository at this point in the history
  • Loading branch information
Alessio Treglia committed Feb 16, 2019
1 parent 22f45a6 commit c8cba87
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 13 deletions.
24 changes: 16 additions & 8 deletions types/coin.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package types

import (
"errors"
"fmt"
"regexp"
"sort"
Expand All @@ -27,10 +28,10 @@ type Coin struct {
// NewCoin returns a new coin with a denomination and amount. It will panic if
// the amount is negative.
func NewCoin(denom string, amount Int) Coin {
validateDenom(denom)
mustValidateDenom(denom)

if amount.LT(ZeroInt()) {
panic(fmt.Sprintf("negative coin amount: %v\n", amount))
panic(fmt.Errorf("negative coin amount: %v", amount))
}

return Coin{
Expand Down Expand Up @@ -148,7 +149,7 @@ func (coins Coins) IsValid() bool {
case 0:
return true
case 1:
if strings.ToLower(coins[0].Denom) != coins[0].Denom {
if err := validateDenom(coins[0].Denom); err != nil {
return false
}
return coins[0].IsPositive()
Expand Down Expand Up @@ -360,7 +361,7 @@ func (coins Coins) Empty() bool {

// Returns the amount of a denom from coins
func (coins Coins) AmountOf(denom string) Int {
validateDenom(denom)
mustValidateDenom(denom)

switch len(coins) {
case 0:
Expand Down Expand Up @@ -488,9 +489,16 @@ var (
reDecCoin = regexp.MustCompile(fmt.Sprintf(`^(%s)%s(%s)$`, reDecAmt, reSpc, reDnmString))
)

func validateDenom(denom string) {
func validateDenom(denom string) error {
if !reDnm.MatchString(denom) {
panic("illegal characters")
return errors.New("illegal characters")
}
return nil
}

func mustValidateDenom(denom string) {
if err := validateDenom(denom); err != nil {
panic(err)
}
}

Expand All @@ -511,8 +519,8 @@ func ParseCoin(coinStr string) (coin Coin, err error) {
return Coin{}, fmt.Errorf("failed to parse coin amount: %s", amountStr)
}

if denomStr != strings.ToLower(denomStr) {
return Coin{}, fmt.Errorf("denom cannot contain upper case characters: %s", denomStr)
if err := validateDenom(denomStr); err != nil {
return Coin{}, fmt.Errorf("invalid denom cannot contain upper case characters or spaces: %s", err)
}

return NewCoin(denomStr, amount), nil
Expand Down
10 changes: 5 additions & 5 deletions types/dec_coin.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type DecCoin struct {
}

func NewDecCoin(denom string, amount Int) DecCoin {
validateDenom(denom)
mustValidateDenom(denom)

if amount.LT(ZeroInt()) {
panic(fmt.Sprintf("negative coin amount: %v\n", amount))
Expand Down Expand Up @@ -352,7 +352,7 @@ func (coins DecCoins) Empty() bool {

// returns the amount of a denom from deccoins
func (coins DecCoins) AmountOf(denom string) Dec {
validateDenom(denom)
mustValidateDenom(denom)

switch len(coins) {
case 0:
Expand Down Expand Up @@ -415,7 +415,7 @@ func (coins DecCoins) IsValid() bool {
return true

case 1:
if strings.ToLower(coins[0].Denom) != coins[0].Denom {
if err := validateDenom(coins[0].Denom); err != nil {
return false
}
return coins[0].IsPositive()
Expand Down Expand Up @@ -515,8 +515,8 @@ func ParseDecCoin(coinStr string) (coin DecCoin, err error) {
return DecCoin{}, errors.Wrap(err, fmt.Sprintf("failed to parse decimal coin amount: %s", amountStr))
}

if denomStr != strings.ToLower(denomStr) {
return DecCoin{}, fmt.Errorf("denom cannot contain upper case characters: %s", denomStr)
if err := validateDenom(denomStr); err != nil {
return DecCoin{}, fmt.Errorf("invalid denom cannot contain upper case characters or spaces: %s", err)
}

return NewDecCoinFromDec(denomStr, amount), nil
Expand Down

0 comments on commit c8cba87

Please # to comment.