Skip to content

Commit

Permalink
adding checks to ensure denoms are sorted
Browse files Browse the repository at this point in the history
  • Loading branch information
spoo-bar committed Aug 2, 2021
1 parent 19aeb76 commit ce1e650
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
13 changes: 10 additions & 3 deletions x/bank/types/balance.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}

Expand Down
12 changes: 12 additions & 0 deletions x/bank/types/balance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit ce1e650

Please sign in to comment.