From ce1e65083ea7fcbb744a3927de8be80f1f1bf77f Mon Sep 17 00:00:00 2001 From: Spoorthi <9302666+spoo-bar@users.noreply.github.com> Date: Mon, 2 Aug 2021 10:16:23 +0200 Subject: [PATCH] adding checks to ensure denoms are sorted --- x/bank/types/balance.go | 13 ++++++++++--- x/bank/types/balance_test.go | 12 ++++++++++++ 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/x/bank/types/balance.go b/x/bank/types/balance.go index b944fea27f85..2aa7967d5060 100644 --- a/x/bank/types/balance.go +++ b/x/bank/types/balance.go @@ -34,6 +34,11 @@ func (b Balance) Validate() error { if err != nil { return err } + + var prevDenom string + if !b.Coins.Empty() { + prevDenom = b.Coins[0].Denom + } seenDenoms := make(map[string]bool) // NOTE: we perform a custom validation since the coins.Validate function @@ -47,16 +52,18 @@ func (b Balance) Validate() error { return err } + if coin.Denom < prevDenom { + return fmt.Errorf("denomination %s is not sorted", coin.Denom) + } + if coin.IsNegative() { return fmt.Errorf("coin %s amount is cannot be negative", coin.Denom) } seenDenoms[coin.Denom] = true + prevDenom = coin.Denom } - // sort the coins post validation - b.Coins = b.Coins.Sort() - return nil } diff --git a/x/bank/types/balance_test.go b/x/bank/types/balance_test.go index f328314910b7..1c7d4255c76b 100644 --- a/x/bank/types/balance_test.go +++ b/x/bank/types/balance_test.go @@ -64,6 +64,18 @@ func TestBalanceValidate(t *testing.T) { }, true, }, + { + "unsorted coins", + bank.Balance{ + Address: "cosmos1yq8lgssgxlx9smjhes6ryjasmqmd3ts2559g0t", + Coins: sdk.Coins{ + sdk.NewInt64Coin("atom", 2), + sdk.NewInt64Coin("zatom", 2), + sdk.NewInt64Coin("batom", 12), + }, + }, + true, + }, } for _, tc := range testCases {