adding checks to ensure denoms are sorted
This commit is contained in:
parent
19aeb763a3
commit
ce1e65083e
|
@ -34,6 +34,11 @@ func (b Balance) Validate() error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var prevDenom string
|
||||||
|
if !b.Coins.Empty() {
|
||||||
|
prevDenom = b.Coins[0].Denom
|
||||||
|
}
|
||||||
seenDenoms := make(map[string]bool)
|
seenDenoms := make(map[string]bool)
|
||||||
|
|
||||||
// NOTE: we perform a custom validation since the coins.Validate function
|
// NOTE: we perform a custom validation since the coins.Validate function
|
||||||
|
@ -47,16 +52,18 @@ func (b Balance) Validate() error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if coin.Denom < prevDenom {
|
||||||
|
return fmt.Errorf("denomination %s is not sorted", coin.Denom)
|
||||||
|
}
|
||||||
|
|
||||||
if coin.IsNegative() {
|
if coin.IsNegative() {
|
||||||
return fmt.Errorf("coin %s amount is cannot be negative", coin.Denom)
|
return fmt.Errorf("coin %s amount is cannot be negative", coin.Denom)
|
||||||
}
|
}
|
||||||
|
|
||||||
seenDenoms[coin.Denom] = true
|
seenDenoms[coin.Denom] = true
|
||||||
|
prevDenom = coin.Denom
|
||||||
}
|
}
|
||||||
|
|
||||||
// sort the coins post validation
|
|
||||||
b.Coins = b.Coins.Sort()
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -64,6 +64,18 @@ func TestBalanceValidate(t *testing.T) {
|
||||||
},
|
},
|
||||||
true,
|
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 {
|
for _, tc := range testCases {
|
||||||
|
|
Loading…
Reference in New Issue