From 5ca8c5bb83e412d8ca68c61f402e83d920a7f2eb Mon Sep 17 00:00:00 2001 From: Christopher Goes Date: Wed, 2 Jan 2019 19:17:27 +0100 Subject: [PATCH] Merge PR #3207: Fix token printing bug * Add IsPositive, case check on coins[0] * Link to correct PR * Add testcase --- PENDING.md | 1 + types/coin.go | 7 ++++++- types/coin_test.go | 5 +++++ 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/PENDING.md b/PENDING.md index 101a30650..82ce4ad66 100644 --- a/PENDING.md +++ b/PENDING.md @@ -19,6 +19,7 @@ BREAKING CHANGES * SDK * [\#3064](https://github.com/cosmos/cosmos-sdk/issues/3064) Sanitize `sdk.Coin` denom. Coins denoms are now case insensitive, i.e. 100fooToken equals to 100FOOTOKEN. + * \#3207 - Fix token printing bug * Tendermint diff --git a/types/coin.go b/types/coin.go index 365476327..e1ad00ec2 100644 --- a/types/coin.go +++ b/types/coin.go @@ -144,8 +144,13 @@ func (coins Coins) IsValid() bool { case 1: return coins[0].IsPositive() default: + if strings.ToLower(coins[0].Denom) != coins[0].Denom { + return false + } + if !coins[0].IsPositive() { + return false + } lowDenom := coins[0].Denom - for _, coin := range coins[1:] { if strings.ToLower(coin.Denom) != coin.Denom { return false diff --git a/types/coin_test.go b/types/coin_test.go index b5d6006d8..14d8925cc 100644 --- a/types/coin_test.go +++ b/types/coin_test.go @@ -286,6 +286,10 @@ func TestCoins(t *testing.T) { {"gas", NewInt(1)}, {"mineral", NewInt(1)}, } + neg := Coins{ + {"gas", NewInt(-1)}, + {"mineral", NewInt(1)}, + } assert.True(t, good.IsValid(), "Coins are valid") assert.False(t, mixedCase.IsValid(), "Coins denoms contain upper case characters") @@ -298,6 +302,7 @@ func TestCoins(t *testing.T) { assert.False(t, badSort2.IsValid(), "Coins are not sorted") assert.False(t, badAmt.IsValid(), "Coins cannot include 0 amounts") assert.False(t, dup.IsValid(), "Duplicate coin") + assert.False(t, neg.IsValid(), "Negative first-denom coin") } func TestCoinsGT(t *testing.T) {