Merge pull request #9829 from cosmos/spoorthi/9706-refactor-validate-balance
fix: adding checks to ensure coin denoms are sorted in `Balance` during `Validate`
This commit is contained in:
commit
87ebb3e503
|
@ -97,6 +97,8 @@ Ref: https://keepachangelog.com/en/1.0.0/
|
||||||
* [\#9762](https://github.com/cosmos/cosmos-sdk/pull/9762) The init command uses the chain-id from the client config if --chain-id is not provided
|
* [\#9762](https://github.com/cosmos/cosmos-sdk/pull/9762) The init command uses the chain-id from the client config if --chain-id is not provided
|
||||||
* [\#9793](https://github.com/cosmos/cosmos-sdk/pull/9793) Fixed ECDSA/secp256r1 transaction malleability.
|
* [\#9793](https://github.com/cosmos/cosmos-sdk/pull/9793) Fixed ECDSA/secp256r1 transaction malleability.
|
||||||
* [\#9836](https://github.com/cosmos/cosmos-sdk/pull/9836) Fixes capability initialization issue on tx revert by moving initialization logic to `InitChain` and `BeginBlock`.
|
* [\#9836](https://github.com/cosmos/cosmos-sdk/pull/9836) Fixes capability initialization issue on tx revert by moving initialization logic to `InitChain` and `BeginBlock`.
|
||||||
|
* [\#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`.
|
||||||
|
|
||||||
|
|
||||||
### State Machine Breaking
|
### State Machine Breaking
|
||||||
|
|
||||||
|
|
|
@ -30,33 +30,14 @@ func (b Balance) GetCoins() sdk.Coins {
|
||||||
|
|
||||||
// Validate checks for address and coins correctness.
|
// Validate checks for address and coins correctness.
|
||||||
func (b Balance) Validate() error {
|
func (b Balance) Validate() error {
|
||||||
_, err := sdk.AccAddressFromBech32(b.Address)
|
if _, err := sdk.AccAddressFromBech32(b.Address); err != nil {
|
||||||
if 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
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if coin.IsNegative() {
|
if err := b.Coins.Validate(); err != nil {
|
||||||
return fmt.Errorf("coin %s amount is cannot be negative", coin.Denom)
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
seenDenoms[coin.Denom] = true
|
|
||||||
}
|
|
||||||
|
|
||||||
// sort the coins post validation
|
|
||||||
b.Coins = b.Coins.Sort()
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -64,6 +64,41 @@ func TestBalanceValidate(t *testing.T) {
|
||||||
},
|
},
|
||||||
true,
|
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 {
|
for _, tc := range testCases {
|
||||||
|
|
Loading…
Reference in New Issue