From 7cfe66eb57c2076810b6b83256cd56323e0005c9 Mon Sep 17 00:00:00 2001 From: atheesh Date: Thu, 31 Mar 2022 14:56:16 +0530 Subject: [PATCH 1/3] fix: handle panic in `authz` --- x/staking/types/authz.go | 5 +++++ x/staking/types/authz_test.go | 12 ++++++++++++ 2 files changed, 17 insertions(+) diff --git a/x/staking/types/authz.go b/x/staking/types/authz.go index 45ce23089a3..e26712ade2e 100644 --- a/x/staking/types/authz.go +++ b/x/staking/types/authz.go @@ -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 diff --git a/x/staking/types/authz_test.go b/x/staking/types/authz_test.go index fd7f17d63e6..3c5bb9addc0 100644 --- a/x/staking/types/authz_test.go +++ b/x/staking/types/authz_test.go @@ -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_____") @@ -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), + false, + true, + nil, + }, { "delegate: verify remaining coins", []sdk.ValAddress{val1, val2}, From dd613bdd42e7eaf50c6a1e1753e3ae707cb90118 Mon Sep 17 00:00:00 2001 From: atheesh Date: Fri, 1 Apr 2022 15:40:30 +0530 Subject: [PATCH 2/3] fix tests --- x/staking/types/authz_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x/staking/types/authz_test.go b/x/staking/types/authz_test.go index 3c5bb9addc0..1f649f0a28a 100644 --- a/x/staking/types/authz_test.go +++ b/x/staking/types/authz_test.go @@ -78,8 +78,8 @@ func TestAuthzAuthorizations(t *testing.T) { stakingtypes.AuthorizationType_AUTHORIZATION_TYPE_DELEGATE, &coin100, stakingtypes.NewMsgDelegate(delAddr, val1, coin150), - false, true, + false, nil, }, { From de46a1cbeec70ba777508ab14b58b82a8c028c4d Mon Sep 17 00:00:00 2001 From: atheesh Date: Fri, 1 Apr 2022 15:44:44 +0530 Subject: [PATCH 3/3] update changelog --- CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ba140fcca94..3d996abb6a5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -241,7 +241,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