Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: panic in authz #11512

Merged
merged 8 commits into from
Apr 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,8 @@ Ref: https://keepachangelog.com/en/1.0.0/
* (x/authz) [\#11252](https://github.com/cosmos/cosmos-sdk/pull/11252) Allow insufficient funds error for authz simulation
* (cli) [\#11313](https://github.com/cosmos/cosmos-sdk/pull/11313) Fixes `--gas auto` when executing CLI transactions in `--generate-only` mode
* (cli) [\#11337](https://github.com/cosmos/cosmos-sdk/pull/11337) Fixes `show-adress` cli cmd
* (crypto) [\#11298](https://github.com/cosmos/cosmos-sdk/pull/11298) Fix cgo secp signature verification and update libscep256k1 library.
* (crypto) [\#11298](https://github.com/cosmos/cosmos-sdk/pull/11298) Fix cgo secp signature verification and update libscep256k1 library.
* (x/authz) [\#11512](https://github.com/cosmos/cosmos-sdk/pull/11512) Fix response of a panic to error, when subtracting balances.

### State Machine Breaking

Expand Down
5 changes: 5 additions & 0 deletions x/staking/types/authz.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,11 @@ func (a StakeAuthorization) Accept(ctx sdk.Context, msg sdk.Msg) (authz.AcceptRe
Updated: &StakeAuthorization{Validators: a.GetValidators(), AuthorizationType: a.GetAuthorizationType()}}, nil
}

// check sufficient balance exists.
if _, isNegative := sdk.NewCoins(*a.MaxTokens).SafeSub(sdk.NewCoins(amount)); isNegative {
return authz.AcceptResponse{}, sdkerrors.ErrInsufficientFunds.Wrapf("amount is more than max tokens")
}

limitLeft := a.MaxTokens.Sub(amount)
if limitLeft.IsZero() {
return authz.AcceptResponse{Accept: true, Delete: true}, nil
Expand Down
12 changes: 12 additions & 0 deletions x/staking/types/authz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

var (
coin100 = sdk.NewInt64Coin("steak", 100)
coin150 = sdk.NewInt64Coin("steak", 150)
coin50 = sdk.NewInt64Coin("steak", 50)
delAddr = sdk.AccAddress("_____delegator _____")
val1 = sdk.ValAddress("_____validator1_____")
Expand Down Expand Up @@ -70,6 +71,17 @@ func TestAuthzAuthorizations(t *testing.T) {
true,
nil,
},
{
"delegate: coins more than allowed",
[]sdk.ValAddress{val1, val2},
[]sdk.ValAddress{},
stakingtypes.AuthorizationType_AUTHORIZATION_TYPE_DELEGATE,
&coin100,
stakingtypes.NewMsgDelegate(delAddr, val1, coin150),
true,
false,
nil,
},
{
"delegate: verify remaining coins",
[]sdk.ValAddress{val1, val2},
Expand Down