From 8bd37f700d13eb18e634edfdf15cec5a09a1e79f Mon Sep 17 00:00:00 2001 From: atheesh Date: Mon, 2 May 2022 17:33:58 +0530 Subject: [PATCH 1/5] fix: expiration when overwriting an existing grant --- x/feegrant/keeper/keeper.go | 9 ++- x/feegrant/keeper/keeper_test.go | 105 ++++++++++++++++++++++++++++++- 2 files changed, 110 insertions(+), 4 deletions(-) diff --git a/x/feegrant/keeper/keeper.go b/x/feegrant/keeper/keeper.go index 408273f91af7..a89c6f66fc61 100644 --- a/x/feegrant/keeper/keeper.go +++ b/x/feegrant/keeper/keeper.go @@ -2,6 +2,7 @@ package keeper import ( "fmt" + "strings" "time" storetypes "github.com/cosmos/cosmos-sdk/store/types" @@ -52,8 +53,12 @@ func (k Keeper) GrantAllowance(ctx sdk.Context, granter, grantee sdk.AccAddress, key := feegrant.FeeAllowanceKey(granter, grantee) var oldExp *time.Time - existingGrant, err := k.getGrant(ctx, grantee, granter) - if err != nil && existingGrant != nil && existingGrant.GetAllowance() != nil { + existingGrant, err := k.getGrant(ctx, granter, grantee) + if err != nil && !strings.Contains(err.Error(), "fee-grant not found") { + return err + } + + if existingGrant != nil && existingGrant.GetAllowance() != nil { grantInfo, err := existingGrant.GetGrant() if err != nil { return err diff --git a/x/feegrant/keeper/keeper_test.go b/x/feegrant/keeper/keeper_test.go index 933d85c7cefe..fadff19e1a7e 100644 --- a/x/feegrant/keeper/keeper_test.go +++ b/x/feegrant/keeper/keeper_test.go @@ -46,6 +46,7 @@ func (suite *KeeperTestSuite) TestKeeperCrud() { // some helpers eth := sdk.NewCoins(sdk.NewInt64Coin("eth", 123)) exp := suite.sdkCtx.BlockTime().AddDate(1, 0, 0) + exp2 := suite.sdkCtx.BlockTime().AddDate(2, 0, 0) basic := &feegrant.BasicAllowance{ SpendLimit: suite.atom, Expiration: &exp, @@ -56,6 +57,11 @@ func (suite *KeeperTestSuite) TestKeeperCrud() { Expiration: &exp, } + basic3 := &feegrant.BasicAllowance{ + SpendLimit: eth, + Expiration: &exp2, + } + // let's set up some initial state here err := suite.keeper.GrantAllowance(suite.sdkCtx, suite.addrs[0], suite.addrs[1], basic) suite.Require().NoError(err) @@ -85,7 +91,7 @@ func (suite *KeeperTestSuite) TestKeeperCrud() { err = suite.keeper.GrantAllowance(suite.sdkCtx, suite.addrs[0], suite.addrs[2], basic) suite.Require().NoError(err) - err = suite.keeper.GrantAllowance(suite.sdkCtx, suite.addrs[1], suite.addrs[2], basic2) + err = suite.keeper.GrantAllowance(suite.sdkCtx, suite.addrs[1], suite.addrs[2], basic3) suite.Require().NoError(err) // end state: @@ -115,7 +121,7 @@ func (suite *KeeperTestSuite) TestKeeperCrud() { "addr modified": { granter: suite.addrs[1], grantee: suite.addrs[2], - allowance: basic2, + allowance: basic3, }, } @@ -273,6 +279,8 @@ func (suite *KeeperTestSuite) TestPruneGrants() { grantee sdk.AccAddress allowance feegrant.FeeAllowanceI expErrMsg string + preRun func() + postRun func() }{ { name: "grant not pruned from state", @@ -326,6 +334,99 @@ func (suite *KeeperTestSuite) TestPruneGrants() { Expiration: &oneYearExpiry, }, }, + { + name: "grant created with a day expiry & overwritten with no expiry shouldn't be pruned: no error", + ctx: suite.sdkCtx.WithBlockTime(now.AddDate(0, 0, 2)), + granter: suite.addrs[2], + grantee: suite.addrs[1], + allowance: &feegrant.BasicAllowance{ + SpendLimit: eth, + }, + preRun: func() { + // create a grant with same granter, grantee with a day expiry. + now := suite.sdkCtx.BlockTime().AddDate(0, 0, 1) + allowance := &feegrant.BasicAllowance{ + SpendLimit: suite.atom, + Expiration: &now, + } + err := suite.keeper.GrantAllowance(suite.sdkCtx, suite.addrs[2], suite.addrs[1], allowance) + suite.NoError(err) + }, + postRun: func() { + _, err := suite.msgSrvr.RevokeAllowance(suite.sdkCtx, &feegrant.MsgRevokeAllowance{ + Granter: suite.addrs[2].String(), + Grantee: suite.addrs[1].String(), + }) + suite.NoError(err) + }, + }, + { + name: "grant created with a day expiry & overwritten with a year expiry shouldn't be pruned: no error", + ctx: suite.sdkCtx.WithBlockTime(now.AddDate(0, 0, 2)), + granter: suite.addrs[2], + grantee: suite.addrs[1], + allowance: &feegrant.BasicAllowance{ + SpendLimit: eth, + Expiration: &oneYearExpiry, + }, + preRun: func() { + // create a grant with same granter, grantee with a day expiry. + day := suite.sdkCtx.BlockTime().AddDate(0, 0, 1) + allowance := &feegrant.BasicAllowance{ + SpendLimit: suite.atom, + Expiration: &day, + } + err := suite.keeper.GrantAllowance(suite.sdkCtx, suite.addrs[2], suite.addrs[1], allowance) + suite.NoError(err) + }, + postRun: func() { + _, err := suite.msgSrvr.RevokeAllowance(suite.sdkCtx, &feegrant.MsgRevokeAllowance{ + Granter: suite.addrs[2].String(), + Grantee: suite.addrs[1].String(), + }) + suite.NoError(err) + }, + }, + { + name: "grant created with a year expiry & overwritten with a day expiry should be pruned after a day: error", + ctx: suite.sdkCtx.WithBlockTime(now.AddDate(0, 0, 1)), + granter: suite.addrs[2], + grantee: suite.addrs[1], + allowance: &feegrant.BasicAllowance{ + SpendLimit: eth, + Expiration: &oneYearExpiry, + }, + preRun: func() { + // create a grant with same granter, grantee with 2 day expiry. + day := suite.sdkCtx.BlockTime().AddDate(0, 0, 1) + allowance := &feegrant.BasicAllowance{ + SpendLimit: suite.atom, + Expiration: &day, + } + err := suite.keeper.GrantAllowance(suite.sdkCtx, suite.addrs[2], suite.addrs[1], allowance) + suite.NoError(err) + }, + postRun: func() {}, + }, + { + name: "grant created with no expiry & overwritten with a day expiry should be pruned after a day: error", + ctx: suite.sdkCtx.WithBlockTime(now.AddDate(0, 0, 1)), + granter: suite.addrs[2], + grantee: suite.addrs[1], + allowance: &feegrant.BasicAllowance{ + SpendLimit: eth, + Expiration: &oneYearExpiry, + }, + preRun: func() { + // create a grant with same granter, grantee with no expiry. + allowance := &feegrant.BasicAllowance{ + SpendLimit: suite.atom, + } + err := suite.keeper.GrantAllowance(suite.sdkCtx, suite.addrs[2], suite.addrs[1], allowance) + suite.NoError(err) + }, + postRun: func() {}, + }, } for _, tc := range testCases { From be1851c615922055f8ad0fd48fd11b4adc01cba3 Mon Sep 17 00:00:00 2001 From: atheesh Date: Mon, 2 May 2022 18:16:20 +0530 Subject: [PATCH 2/5] add tests --- x/feegrant/keeper/keeper_test.go | 41 +++++++++++++++++++------------- 1 file changed, 24 insertions(+), 17 deletions(-) diff --git a/x/feegrant/keeper/keeper_test.go b/x/feegrant/keeper/keeper_test.go index fadff19e1a7e..dfbba8de89c3 100644 --- a/x/feegrant/keeper/keeper_test.go +++ b/x/feegrant/keeper/keeper_test.go @@ -271,6 +271,7 @@ func (suite *KeeperTestSuite) TestPruneGrants() { eth := sdk.NewCoins(sdk.NewInt64Coin("eth", 123)) now := suite.sdkCtx.BlockTime() oneYearExpiry := now.AddDate(1, 0, 0) + oneDay := now.AddDate(0, 0, 1) testCases := []struct { name string @@ -343,11 +344,10 @@ func (suite *KeeperTestSuite) TestPruneGrants() { SpendLimit: eth, }, preRun: func() { - // create a grant with same granter, grantee with a day expiry. - now := suite.sdkCtx.BlockTime().AddDate(0, 0, 1) + // create a grant with a day expiry. allowance := &feegrant.BasicAllowance{ SpendLimit: suite.atom, - Expiration: &now, + Expiration: &oneDay, } err := suite.keeper.GrantAllowance(suite.sdkCtx, suite.addrs[2], suite.addrs[1], allowance) suite.NoError(err) @@ -370,11 +370,10 @@ func (suite *KeeperTestSuite) TestPruneGrants() { Expiration: &oneYearExpiry, }, preRun: func() { - // create a grant with same granter, grantee with a day expiry. - day := suite.sdkCtx.BlockTime().AddDate(0, 0, 1) + // create a grant with a day expiry. allowance := &feegrant.BasicAllowance{ SpendLimit: suite.atom, - Expiration: &day, + Expiration: &oneDay, } err := suite.keeper.GrantAllowance(suite.sdkCtx, suite.addrs[2], suite.addrs[1], allowance) suite.NoError(err) @@ -389,50 +388,55 @@ func (suite *KeeperTestSuite) TestPruneGrants() { }, { name: "grant created with a year expiry & overwritten with a day expiry should be pruned after a day: error", - ctx: suite.sdkCtx.WithBlockTime(now.AddDate(0, 0, 1)), + ctx: suite.sdkCtx.WithBlockTime(now.AddDate(0, 0, 2)), granter: suite.addrs[2], grantee: suite.addrs[1], allowance: &feegrant.BasicAllowance{ SpendLimit: eth, - Expiration: &oneYearExpiry, + Expiration: &oneDay, }, preRun: func() { - // create a grant with same granter, grantee with 2 day expiry. - day := suite.sdkCtx.BlockTime().AddDate(0, 0, 1) + // create a grant with a year expiry. allowance := &feegrant.BasicAllowance{ SpendLimit: suite.atom, - Expiration: &day, + Expiration: &oneYearExpiry, } err := suite.keeper.GrantAllowance(suite.sdkCtx, suite.addrs[2], suite.addrs[1], allowance) suite.NoError(err) }, - postRun: func() {}, + postRun: func() {}, + expErrMsg: "not found", }, { name: "grant created with no expiry & overwritten with a day expiry should be pruned after a day: error", - ctx: suite.sdkCtx.WithBlockTime(now.AddDate(0, 0, 1)), + ctx: suite.sdkCtx.WithBlockTime(now.AddDate(0, 0, 2)), granter: suite.addrs[2], grantee: suite.addrs[1], allowance: &feegrant.BasicAllowance{ SpendLimit: eth, - Expiration: &oneYearExpiry, + Expiration: &oneDay, }, preRun: func() { - // create a grant with same granter, grantee with no expiry. + // create a grant with no expiry. allowance := &feegrant.BasicAllowance{ SpendLimit: suite.atom, } err := suite.keeper.GrantAllowance(suite.sdkCtx, suite.addrs[2], suite.addrs[1], allowance) suite.NoError(err) }, - postRun: func() {}, + postRun: func() {}, + expErrMsg: "not found", }, } for _, tc := range testCases { tc := tc suite.Run(tc.name, func() { - suite.keeper.GrantAllowance(suite.sdkCtx, tc.granter, tc.grantee, tc.allowance) + if tc.preRun != nil { + tc.preRun() + } + err := suite.keeper.GrantAllowance(suite.sdkCtx, tc.granter, tc.grantee, tc.allowance) + suite.NoError(err) suite.app.FeeGrantKeeper.RemoveExpiredAllowances(tc.ctx) grant, err := suite.keeper.GetAllowance(tc.ctx, tc.granter, tc.grantee) if tc.expErrMsg != "" { @@ -441,6 +445,9 @@ func (suite *KeeperTestSuite) TestPruneGrants() { } else { suite.NotNil(grant) } + if tc.postRun != nil { + tc.postRun() + } }) } } From b8859bc457be020f5bca03e45bb10b3de0f8c166 Mon Sep 17 00:00:00 2001 From: atheesh Date: Wed, 4 May 2022 11:18:50 +0530 Subject: [PATCH 3/5] remove string match --- x/feegrant/keeper/keeper.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x/feegrant/keeper/keeper.go b/x/feegrant/keeper/keeper.go index 452fb564cf65..076db2a158ae 100644 --- a/x/feegrant/keeper/keeper.go +++ b/x/feegrant/keeper/keeper.go @@ -54,7 +54,7 @@ func (k Keeper) GrantAllowance(ctx sdk.Context, granter, grantee sdk.AccAddress, var oldExp *time.Time existingGrant, err := k.getGrant(ctx, granter, grantee) - if err != nil && !strings.Contains(err.Error(), "fee-grant not found") { + if err != nil && !strings.Contains(err.Error(), sdkerrors.ErrUnauthorized.Error()) { return err } From 86ff4f6b696c7a4143ad38c5d871efc1c26e982f Mon Sep 17 00:00:00 2001 From: Amaury M <1293565+amaurym@users.noreply.github.com> Date: Thu, 5 May 2022 10:50:50 +0200 Subject: [PATCH 4/5] try fix errors Is --- x/feegrant/keeper/keeper.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/x/feegrant/keeper/keeper.go b/x/feegrant/keeper/keeper.go index 076db2a158ae..4800d512f242 100644 --- a/x/feegrant/keeper/keeper.go +++ b/x/feegrant/keeper/keeper.go @@ -2,7 +2,6 @@ package keeper import ( "fmt" - "strings" "time" storetypes "github.com/cosmos/cosmos-sdk/store/types" @@ -54,7 +53,10 @@ func (k Keeper) GrantAllowance(ctx sdk.Context, granter, grantee sdk.AccAddress, var oldExp *time.Time existingGrant, err := k.getGrant(ctx, granter, grantee) - if err != nil && !strings.Contains(err.Error(), sdkerrors.ErrUnauthorized.Error()) { + + // If we didn't find any grant, we don't return any error. + // All other kinds of errors are returned. + if err != nil && !sdkerrors.IsOf(err, sdkerrors.ErrNotFound) { return err } @@ -189,7 +191,7 @@ func (k Keeper) getGrant(ctx sdk.Context, granter sdk.AccAddress, grantee sdk.Ac key := feegrant.FeeAllowanceKey(granter, grantee) bz := store.Get(key) if len(bz) == 0 { - return nil, sdkerrors.Wrap(sdkerrors.ErrUnauthorized, "fee-grant not found") + return nil, sdkerrors.ErrNotFound.Wrap("fee-grant not found") } var feegrant feegrant.Grant From ceb06412946cd32616c6fa21429c66c6f80788e3 Mon Sep 17 00:00:00 2001 From: atheesh Date: Fri, 6 May 2022 10:52:15 +0530 Subject: [PATCH 5/5] fix tests --- x/feegrant/client/testutil/suite.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x/feegrant/client/testutil/suite.go b/x/feegrant/client/testutil/suite.go index 5423a18e9503..d3bd39e313b7 100644 --- a/x/feegrant/client/testutil/suite.go +++ b/x/feegrant/client/testutil/suite.go @@ -663,7 +663,7 @@ func (s *IntegrationTestSuite) TestNewCmdRevokeFeegrant() { }, commonFlags..., ), - false, 4, &sdk.TxResponse{}, + false, 38, &sdk.TxResponse{}, }, { "Valid revoke",