Skip to content

Commit 0d176a6

Browse files
author
Alessio Treglia
committed
Validate coins denom upon creation
Disallow empty denoms.
1 parent 2d10d70 commit 0d176a6

File tree

3 files changed

+15
-27
lines changed

3 files changed

+15
-27
lines changed

types/coin.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package types
22

33
import (
4+
"errors"
45
"fmt"
56
"regexp"
67
"sort"
@@ -30,6 +31,9 @@ func NewCoin(denom string, amount Int) Coin {
3031
if amount.LT(ZeroInt()) {
3132
panic(fmt.Sprintf("negative coin amount: %v\n", amount))
3233
}
34+
if len(strings.TrimSpace(denom)) == 0 {
35+
panic(errors.New("denom cannot be an empty string\n"))
36+
}
3337
if strings.ToLower(denom) != denom {
3438
panic(fmt.Sprintf("denom cannot contain upper case characters: %s\n", denom))
3539
}

types/coin_test.go

Lines changed: 8 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -452,55 +452,36 @@ func TestSortCoins(t *testing.T) {
452452
func TestAmountOf(t *testing.T) {
453453
case0 := Coins{}
454454
case1 := Coins{
455-
NewInt64Coin("", 0),
456-
}
457-
case2 := Coins{
458-
NewInt64Coin(" ", 0),
459-
}
460-
case3 := Coins{
461455
NewInt64Coin("gold", 0),
462456
}
463-
case4 := Coins{
457+
case2 := Coins{
464458
NewInt64Coin("gas", 1),
465459
NewInt64Coin("mineral", 1),
466460
NewInt64Coin("tree", 1),
467461
}
468-
case5 := Coins{
462+
case3 := Coins{
469463
NewInt64Coin("mineral", 1),
470464
NewInt64Coin("tree", 1),
471465
}
472-
case6 := Coins{
473-
NewInt64Coin("", 6),
474-
}
475-
case7 := Coins{
476-
NewInt64Coin(" ", 7),
477-
}
478-
case8 := Coins{
466+
case4 := Coins{
479467
NewInt64Coin("gas", 8),
480468
}
481469

482470
cases := []struct {
483471
coins Coins
484472
amountOf int64
485-
amountOfSpace int64
486473
amountOfGAS int64
487474
amountOfMINERAL int64
488475
amountOfTREE int64
489476
}{
490-
{case0, 0, 0, 0, 0, 0},
491-
{case1, 0, 0, 0, 0, 0},
492-
{case2, 0, 0, 0, 0, 0},
493-
{case3, 0, 0, 0, 0, 0},
494-
{case4, 0, 0, 1, 1, 1},
495-
{case5, 0, 0, 0, 1, 1},
496-
{case6, 6, 0, 0, 0, 0},
497-
{case7, 0, 7, 0, 0, 0},
498-
{case8, 0, 0, 8, 0, 0},
477+
{case0, 0, 0, 0, 0},
478+
{case1, 0, 0, 0, 0},
479+
{case2, 0, 1, 1, 1},
480+
{case3, 0, 0, 1, 1},
481+
{case4, 0, 8, 0, 0},
499482
}
500483

501484
for _, tc := range cases {
502-
assert.Equal(t, NewInt(tc.amountOf), tc.coins.AmountOf(""))
503-
assert.Equal(t, NewInt(tc.amountOfSpace), tc.coins.AmountOf(" "))
504485
assert.Equal(t, NewInt(tc.amountOfGAS), tc.coins.AmountOf("gas"))
505486
assert.Equal(t, NewInt(tc.amountOfMINERAL), tc.coins.AmountOf("mineral"))
506487
assert.Equal(t, NewInt(tc.amountOfTREE), tc.coins.AmountOf("tree"))

types/dec_coin.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ func NewDecCoin(denom string, amount int64) DecCoin {
2222
if amount < 0 {
2323
panic(fmt.Sprintf("negative decimal coin amount: %v\n", amount))
2424
}
25+
if len(strings.TrimSpace(denom)) == 0 {
26+
panic(errors.New("denom cannot be an empty string\n"))
27+
}
2528
if strings.ToLower(denom) != denom {
2629
panic(fmt.Sprintf("denom cannot contain upper case characters: %s\n", denom))
2730
}

0 commit comments

Comments
 (0)