From 64aa2255c6aa3395a48630cd999de97fc27aad28 Mon Sep 17 00:00:00 2001 From: Assaf Morami Date: Thu, 11 May 2023 11:46:31 +0300 Subject: [PATCH 1/2] Infinite feegrant bug test This test is designed to reproduce an infinite Feegrant bug that occurs when using AllowedMsgAllowance with an internal BasicAllowance. I suspect that this line (https://github.com/scrtlabs/cosmos-sdk/blob/61091b357e/x/feegrant/basic_fee.go#L31) does not update the internal BasicAllowance after using part of the grant. This may be related to this line (https://github.com/scrtlabs/cosmos-sdk/blob/61091b357e/x/feegrant/filtered_fee.go#L47) as well. --- x/feegrant/filtered_fee_test.go | 82 +++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 x/feegrant/filtered_fee_test.go diff --git a/x/feegrant/filtered_fee_test.go b/x/feegrant/filtered_fee_test.go new file mode 100644 index 00000000000..a1be11fa65a --- /dev/null +++ b/x/feegrant/filtered_fee_test.go @@ -0,0 +1,82 @@ +package feegrant_test + +import ( + "testing" + "time" + + bank "github.com/cosmos/cosmos-sdk/x/bank/types" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + tmproto "github.com/tendermint/tendermint/proto/tendermint/types" + + codectypes "github.com/cosmos/cosmos-sdk/codec/types" + "github.com/cosmos/cosmos-sdk/simapp" + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/x/feegrant" +) + +func TestFilteredFeeValidAllow(t *testing.T) { + app := simapp.Setup(false) + + smallAtom := sdk.NewCoins(sdk.NewInt64Coin("atom", 488)) + bigAtom := sdk.NewCoins(sdk.NewInt64Coin("atom", 1000)) + leftAtom := sdk.NewCoins(sdk.NewInt64Coin("atom", 512)) + + basicAllowance, _ := codectypes.NewAnyWithValue(&feegrant.BasicAllowance{ + SpendLimit: bigAtom, + }) + + cases := map[string]struct { + allowance *feegrant.AllowedMsgAllowance + // all other checks are ignored if valid=false + fee sdk.Coins + blockTime time.Time + valid bool + accept bool + remove bool + remains sdk.Coins + }{ + "internal fee is updated": { + allowance: &feegrant.AllowedMsgAllowance{ + Allowance: basicAllowance, + AllowedMessages: []string{"/cosmos.bank.v1beta1.MsgSend"}, + }, + fee: smallAtom, + accept: true, + remove: false, + remains: leftAtom, + }, + } + + for name, stc := range cases { + tc := stc // to make scopelint happy + t.Run(name, func(t *testing.T) { + err := tc.allowance.ValidateBasic() + require.NoError(t, err) + + ctx := app.BaseApp.NewContext(false, tmproto.Header{}).WithBlockTime(tc.blockTime) + + // now try to deduct + removed, err := tc.allowance.Accept(ctx, tc.fee, []sdk.Msg{ + &bank.MsgSend{ + FromAddress: "gm", + ToAddress: "gn", + Amount: tc.fee, + }, + }) + if !tc.accept { + require.Error(t, err) + return + } + require.NoError(t, err) + + require.Equal(t, tc.remove, removed) + if !removed { + var basicAllowanceLeft feegrant.BasicAllowance + app.AppCodec().Unmarshal(tc.allowance.Allowance.Value, &basicAllowanceLeft) + + assert.Equal(t, tc.remains, basicAllowanceLeft.SpendLimit) + } + }) + } +} From 034306f904245dc72060dc38e4fa4bf6275d1537 Mon Sep 17 00:00:00 2001 From: Assaf Morami Date: Thu, 11 May 2023 12:08:37 +0300 Subject: [PATCH 2/2] Fix infinite feegrant bug The internal allowance update in AllowedMsgAllowance was never propagated to the parent AllowedMsgAllowance. --- x/feegrant/filtered_fee.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/x/feegrant/filtered_fee.go b/x/feegrant/filtered_fee.go index f83f3c5f627..b9eaff18cbd 100644 --- a/x/feegrant/filtered_fee.go +++ b/x/feegrant/filtered_fee.go @@ -63,7 +63,17 @@ func (a *AllowedMsgAllowance) Accept(ctx sdk.Context, fee sdk.Coins, msgs []sdk. return false, err } - return allowance.Accept(ctx, fee, msgs) + remove, err := allowance.Accept(ctx, fee, msgs) + if err != nil { + return false, err + } + + a.Allowance, err = types.NewAnyWithValue(allowance.(proto.Message)) + if err != nil { + return false, err + } + + return remove, nil } func (a *AllowedMsgAllowance) allowedMsgsToMap(ctx sdk.Context) map[string]bool {