Skip to content

Commit

Permalink
Validate coins denom upon creation
Browse files Browse the repository at this point in the history
Disallow empty denoms.
  • Loading branch information
Alessio Treglia committed Feb 10, 2019
1 parent 2d10d70 commit 0d176a6
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 27 deletions.
4 changes: 4 additions & 0 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 Down Expand Up @@ -30,6 +31,9 @@ func NewCoin(denom string, amount Int) Coin {
if amount.LT(ZeroInt()) {
panic(fmt.Sprintf("negative coin amount: %v\n", amount))
}
if len(strings.TrimSpace(denom)) == 0 {
panic(errors.New("denom cannot be an empty string\n"))
}
if strings.ToLower(denom) != denom {
panic(fmt.Sprintf("denom cannot contain upper case characters: %s\n", denom))
}
Expand Down
35 changes: 8 additions & 27 deletions types/coin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -452,55 +452,36 @@ func TestSortCoins(t *testing.T) {
func TestAmountOf(t *testing.T) {
case0 := Coins{}
case1 := Coins{
NewInt64Coin("", 0),
}
case2 := Coins{
NewInt64Coin(" ", 0),
}
case3 := Coins{
NewInt64Coin("gold", 0),
}
case4 := Coins{
case2 := Coins{
NewInt64Coin("gas", 1),
NewInt64Coin("mineral", 1),
NewInt64Coin("tree", 1),
}
case5 := Coins{
case3 := Coins{
NewInt64Coin("mineral", 1),
NewInt64Coin("tree", 1),
}
case6 := Coins{
NewInt64Coin("", 6),
}
case7 := Coins{
NewInt64Coin(" ", 7),
}
case8 := Coins{
case4 := Coins{
NewInt64Coin("gas", 8),
}

cases := []struct {
coins Coins
amountOf int64
amountOfSpace int64
amountOfGAS int64
amountOfMINERAL int64
amountOfTREE int64
}{
{case0, 0, 0, 0, 0, 0},
{case1, 0, 0, 0, 0, 0},
{case2, 0, 0, 0, 0, 0},
{case3, 0, 0, 0, 0, 0},
{case4, 0, 0, 1, 1, 1},
{case5, 0, 0, 0, 1, 1},
{case6, 6, 0, 0, 0, 0},
{case7, 0, 7, 0, 0, 0},
{case8, 0, 0, 8, 0, 0},
{case0, 0, 0, 0, 0},
{case1, 0, 0, 0, 0},
{case2, 0, 1, 1, 1},
{case3, 0, 0, 1, 1},
{case4, 0, 8, 0, 0},
}

for _, tc := range cases {
assert.Equal(t, NewInt(tc.amountOf), tc.coins.AmountOf(""))
assert.Equal(t, NewInt(tc.amountOfSpace), tc.coins.AmountOf(" "))
assert.Equal(t, NewInt(tc.amountOfGAS), tc.coins.AmountOf("gas"))
assert.Equal(t, NewInt(tc.amountOfMINERAL), tc.coins.AmountOf("mineral"))
assert.Equal(t, NewInt(tc.amountOfTREE), tc.coins.AmountOf("tree"))
Expand Down
3 changes: 3 additions & 0 deletions types/dec_coin.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ func NewDecCoin(denom string, amount int64) DecCoin {
if amount < 0 {
panic(fmt.Sprintf("negative decimal coin amount: %v\n", amount))
}
if len(strings.TrimSpace(denom)) == 0 {
panic(errors.New("denom cannot be an empty string\n"))
}
if strings.ToLower(denom) != denom {
panic(fmt.Sprintf("denom cannot contain upper case characters: %s\n", denom))
}
Expand Down

0 comments on commit 0d176a6

Please sign in to comment.