diff --git a/types/coin.go b/types/coin.go index 627febca6b04..cd39a218815b 100644 --- a/types/coin.go +++ b/types/coin.go @@ -1,6 +1,7 @@ package types import ( + "errors" "fmt" "regexp" "sort" @@ -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)) } diff --git a/types/coin_test.go b/types/coin_test.go index 52bdc54e98aa..354dde13f1d9 100644 --- a/types/coin_test.go +++ b/types/coin_test.go @@ -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")) diff --git a/types/dec_coin.go b/types/dec_coin.go index e3222ae32063..9caec08be9c0 100644 --- a/types/dec_coin.go +++ b/types/dec_coin.go @@ -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)) }