Skip to content

Commit

Permalink
fix: adding checks to ensure coin denoms are sorted in Balance duri…
Browse files Browse the repository at this point in the history
…ng `Validate` (backport cosmos#9829) (cosmos#10849)

* adding checks to ensure denoms are sorted

(cherry picked from commit ce1e650)

* updating changelog

(cherry picked from commit 2cea5e5)

# Conflicts:
#	CHANGELOG.md

* refactoring balance.coin validation

(cherry picked from commit 40d22c7)

* adding 0 value coin test case

(cherry picked from commit 2468b64)

* added valid sorted coin testcase

(cherry picked from commit 66b91ee)

* updating changelog

(cherry picked from commit 59b788a)

# Conflicts:
#	CHANGELOG.md

* conflicts

Co-authored-by: Spoorthi <9302666+spoo-bar@users.noreply.github.com>
Co-authored-by: Federico Kunze Küllmer <federico.kunze94@gmail.com>
  • Loading branch information
3 people authored and JimLarson committed Jul 7, 2022
1 parent 0995427 commit 04b968b
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 24 deletions.
3 changes: 1 addition & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ Ref: https://keepachangelog.com/en/1.0.0/

## [Unreleased]

## [v0.45.0](https://github.com/cosmos/cosmos-sdk/releases/tag/v0.45.0) - 2021-12-07

### State Machine Breaking

* (auth) [\#10536](https://github.com/cosmos/cosmos-sdk/pull/10536]) Enable `SetSequence` for `ModuleAccount`.
Expand All @@ -62,6 +60,7 @@ Ref: https://keepachangelog.com/en/1.0.0/

* (types) [\#9627](https://github.com/cosmos/cosmos-sdk/pull/9627) Fix nil pointer panic on `NewBigIntFromInt`.
* [#10725](https://github.com/cosmos/cosmos-sdk/pull/10725) populate `ctx.ConsensusParams` for begin/end blockers.
* [\#9829](https://github.com/cosmos/cosmos-sdk/pull/9829) Fixed Coin denom sorting not being checked during `Balance.Validate` check. Refactored the Validation logic to use `Coins.Validate` for `Balance.Coins`

## [v0.44.5](https://github.com/cosmos/cosmos-sdk/releases/tag/v0.44.5) - 2021-12-02

Expand Down
25 changes: 3 additions & 22 deletions x/bank/types/balance.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,33 +30,14 @@ func (b Balance) GetCoins() sdk.Coins {

// Validate checks for address and coins correctness.
func (b Balance) Validate() error {
_, err := sdk.AccAddressFromBech32(b.Address)
if err != nil {
if _, err := sdk.AccAddressFromBech32(b.Address); err != nil {
return err
}
seenDenoms := make(map[string]bool)

// NOTE: we perform a custom validation since the coins.Validate function
// errors on zero balance coins
for _, coin := range b.Coins {
if seenDenoms[coin.Denom] {
return fmt.Errorf("duplicate denomination %s", coin.Denom)
}

if err := sdk.ValidateDenom(coin.Denom); err != nil {
return err
}

if coin.IsNegative() {
return fmt.Errorf("coin %s amount is cannot be negative", coin.Denom)
}

seenDenoms[coin.Denom] = true
if err := b.Coins.Validate(); err != nil {
return err
}

// sort the coins post validation
b.Coins = b.Coins.Sort()

return nil
}

Expand Down
35 changes: 35 additions & 0 deletions x/bank/types/balance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,41 @@ func TestBalanceValidate(t *testing.T) {
},
true,
},
{
"0 value coin",
bank.Balance{
Address: "cosmos1yq8lgssgxlx9smjhes6ryjasmqmd3ts2559g0t",
Coins: sdk.Coins{
sdk.NewInt64Coin("atom", 0),
sdk.NewInt64Coin("zatom", 2),
},
},
true,
},
{
"unsorted coins",
bank.Balance{
Address: "cosmos1yq8lgssgxlx9smjhes6ryjasmqmd3ts2559g0t",
Coins: sdk.Coins{
sdk.NewInt64Coin("atom", 2),
sdk.NewInt64Coin("zatom", 2),
sdk.NewInt64Coin("batom", 12),
},
},
true,
},
{
"valid sorted coins",
bank.Balance{
Address: "cosmos1yq8lgssgxlx9smjhes6ryjasmqmd3ts2559g0t",
Coins: sdk.Coins{
sdk.NewInt64Coin("atom", 2),
sdk.NewInt64Coin("batom", 12),
sdk.NewInt64Coin("zatom", 2),
},
},
false,
},
}

for _, tc := range testCases {
Expand Down

0 comments on commit 04b968b

Please sign in to comment.