Skip to content

Commit

Permalink
refactoring balance.coin validation
Browse files Browse the repository at this point in the history
  • Loading branch information
spoo-bar committed Aug 3, 2021
1 parent 2cea5e5 commit 40d22c7
Showing 1 changed file with 3 additions and 29 deletions.
32 changes: 3 additions & 29 deletions x/bank/types/balance.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,38 +30,12 @@ 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
}

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
// 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.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
if err := b.Coins.Validate(); err != nil {
return err
}

return nil
Expand Down

0 comments on commit 40d22c7

Please sign in to comment.