From d51c9c660b6283b7abac503a32038c801d0f26c1 Mon Sep 17 00:00:00 2001
From: Alex Johnson <alex@skip.money>
Date: Mon, 10 Jun 2024 09:50:32 -0400
Subject: [PATCH 1/2] refactor: generic fallback ante decorator (#102)

(cherry picked from commit 04c7e6f84b96ef88cb82aa475c90b5572c24a4c3)

# Conflicts:
#	tests/app/app.go
---
 tests/app/ante.go               | 10 ++++++----
 tests/app/app.go                | 31 +++++++++++++++++++++++++++++++
 x/feemarket/ante/fee.go         | 20 +++++++++++++-------
 x/feemarket/ante/suite/suite.go | 10 ++++++----
 4 files changed, 56 insertions(+), 15 deletions(-)

diff --git a/tests/app/ante.go b/tests/app/ante.go
index e2d92e0..69d0e4d 100644
--- a/tests/app/ante.go
+++ b/tests/app/ante.go
@@ -46,10 +46,12 @@ func NewAnteHandler(options AnteHandlerOptions) (sdk.AnteHandler, error) {
 		authante.NewConsumeGasForTxSizeDecorator(options.AccountKeeper),
 		feemarketante.NewFeeMarketCheckDecorator( // fee market check replaces fee deduct decorator
 			options.FeeMarketKeeper,
-			options.AccountKeeper,
-			options.BaseOptions.BankKeeper,
-			options.BaseOptions.FeegrantKeeper,
-			options.BaseOptions.TxFeeChecker,
+			authante.NewDeductFeeDecorator(
+				options.AccountKeeper,
+				options.BaseOptions.BankKeeper,
+				options.BaseOptions.FeegrantKeeper,
+				options.BaseOptions.TxFeeChecker,
+			),
 		), // fees are deducted in the fee market deduct post handler
 		authante.NewSetPubKeyDecorator(options.AccountKeeper), // SetPubKeyDecorator must be called before all signature verification decorators
 		authante.NewValidateSigCountDecorator(options.AccountKeeper),
diff --git a/tests/app/app.go b/tests/app/app.go
index ab3de82..ff66963 100644
--- a/tests/app/app.go
+++ b/tests/app/app.go
@@ -8,9 +8,32 @@ import (
 	"path/filepath"
 	"reflect"
 
+<<<<<<< HEAD
 	"cosmossdk.io/depinject"
 	dbm "github.com/cometbft/cometbft-db"
 	"github.com/cometbft/cometbft/libs/log"
+=======
+	autocliv1 "cosmossdk.io/api/cosmos/autocli/v1"
+	reflectionv1 "cosmossdk.io/api/cosmos/reflection/v1"
+	"cosmossdk.io/client/v2/autocli"
+	"cosmossdk.io/core/appmodule"
+	"cosmossdk.io/log"
+	storetypes "cosmossdk.io/store/types"
+	"cosmossdk.io/x/circuit"
+	circuitkeeper "cosmossdk.io/x/circuit/keeper"
+	circuittypes "cosmossdk.io/x/circuit/types"
+	evidencetypes "cosmossdk.io/x/evidence/types"
+	"cosmossdk.io/x/feegrant"
+	feegrantkeeper "cosmossdk.io/x/feegrant/keeper"
+	feegrantmodule "cosmossdk.io/x/feegrant/module"
+	"cosmossdk.io/x/nft"
+	"cosmossdk.io/x/tx/signing"
+	"cosmossdk.io/x/upgrade"
+	upgradekeeper "cosmossdk.io/x/upgrade/keeper"
+	upgradetypes "cosmossdk.io/x/upgrade/types"
+	abci "github.com/cometbft/cometbft/abci/types"
+	dbm "github.com/cosmos/cosmos-db"
+>>>>>>> 04c7e6f (refactor: generic fallback ante decorator (#102))
 	"github.com/cosmos/cosmos-sdk/baseapp"
 	"github.com/cosmos/cosmos-sdk/client"
 	"github.com/cosmos/cosmos-sdk/codec"
@@ -60,11 +83,19 @@ import (
 	slashingkeeper "github.com/cosmos/cosmos-sdk/x/slashing/keeper"
 	"github.com/cosmos/cosmos-sdk/x/staking"
 	stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"
+<<<<<<< HEAD
 	"github.com/cosmos/cosmos-sdk/x/upgrade"
 	upgradeclient "github.com/cosmos/cosmos-sdk/x/upgrade/client"
 	upgradekeeper "github.com/cosmos/cosmos-sdk/x/upgrade/keeper"
 
 	feemarketmodule "github.com/skip-mev/feemarket/x/feemarket"
+=======
+	stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
+	"github.com/cosmos/gogoproto/proto"
+	"github.com/spf13/cast"
+
+	"github.com/skip-mev/feemarket/x/feemarket"
+>>>>>>> 04c7e6f (refactor: generic fallback ante decorator (#102))
 	feemarketkeeper "github.com/skip-mev/feemarket/x/feemarket/keeper"
 	feemarkettypes "github.com/skip-mev/feemarket/x/feemarket/types"
 )
diff --git a/x/feemarket/ante/fee.go b/x/feemarket/ante/fee.go
index aff6034..0033e8f 100644
--- a/x/feemarket/ante/fee.go
+++ b/x/feemarket/ante/fee.go
@@ -7,7 +7,6 @@ import (
 	sdkmath "cosmossdk.io/math"
 	sdk "github.com/cosmos/cosmos-sdk/types"
 	sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
-	"github.com/cosmos/cosmos-sdk/x/auth/ante"
 
 	feemarkettypes "github.com/skip-mev/feemarket/x/feemarket/types"
 )
@@ -32,22 +31,23 @@ func newFeeMarketCheckDecorator(fmk FeeMarketKeeper) feeMarketCheckDecorator {
 //
 // CONTRACT: Tx must implement FeeTx interface
 type FeeMarketCheckDecorator struct {
-	feemarketKeeper    FeeMarketKeeper
+	feemarketKeeper FeeMarketKeeper
+
 	feemarketDecorator feeMarketCheckDecorator
-	cosmosDecorator    ante.DeductFeeDecorator
+	fallbackDecorator  sdk.AnteDecorator
 }
 
-func NewFeeMarketCheckDecorator(fmk FeeMarketKeeper, ak AccountKeeper, bk BankKeeper, fgk FeeGrantKeeper, txfc ante.TxFeeChecker) FeeMarketCheckDecorator {
+func NewFeeMarketCheckDecorator(fmk FeeMarketKeeper, fallbackDecorator sdk.AnteDecorator) FeeMarketCheckDecorator {
 	return FeeMarketCheckDecorator{
 		feemarketKeeper: fmk,
 		feemarketDecorator: newFeeMarketCheckDecorator(
 			fmk,
 		),
-		cosmosDecorator: ante.NewDeductFeeDecorator(ak, bk, fgk, txfc),
+		fallbackDecorator: fallbackDecorator,
 	}
 }
 
-// AnteHandle calls the feemarket internal antehandler if the keeper is enabled.  If disabled, the Cosmos SDK
+// AnteHandle calls the feemarket internal antehandler if the keeper is enabled.  If disabled, the fallback
 // fee antehandler is fallen back to.
 func (d FeeMarketCheckDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (newCtx sdk.Context, err error) {
 	params, err := d.feemarketKeeper.GetParams(ctx)
@@ -57,7 +57,13 @@ func (d FeeMarketCheckDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate
 	if params.Enabled {
 		return d.feemarketDecorator.anteHandle(ctx, tx, simulate, next)
 	}
-	return d.cosmosDecorator.AnteHandle(ctx, tx, simulate, next)
+
+	// only use fallback if not nil
+	if d.fallbackDecorator != nil {
+		return d.fallbackDecorator.AnteHandle(ctx, tx, simulate, next)
+	}
+
+	return next(ctx, tx, simulate)
 }
 
 // anteHandle checks if the tx provides sufficient fee to cover the required fee from the fee market.
diff --git a/x/feemarket/ante/suite/suite.go b/x/feemarket/ante/suite/suite.go
index f044898..a2e33a2 100644
--- a/x/feemarket/ante/suite/suite.go
+++ b/x/feemarket/ante/suite/suite.go
@@ -99,10 +99,12 @@ func (s *TestSuite) SetupHandlers(mock bool) {
 		authante.NewSetUpContextDecorator(), // outermost AnteDecorator. SetUpContext must be called first
 		feemarketante.NewFeeMarketCheckDecorator( // fee market replaces fee deduct decorator
 			s.FeeMarketKeeper,
-			s.AccountKeeper,
-			s.BankKeeper,
-			s.FeeGrantKeeper,
-			nil,
+			authante.NewDeductFeeDecorator(
+				s.AccountKeeper,
+				s.BankKeeper,
+				s.FeeGrantKeeper,
+				nil,
+			),
 		),
 		authante.NewSigGasConsumeDecorator(s.AccountKeeper, authante.DefaultSigVerificationGasConsumer),
 	}

From 2e5402c99d71730dc2221c2040c8a166696eff69 Mon Sep 17 00:00:00 2001
From: aljo242 <alex@skip.money>
Date: Mon, 10 Jun 2024 10:41:37 -0400
Subject: [PATCH 2/2] ok

---
 tests/app/app.go | 32 --------------------------------
 1 file changed, 32 deletions(-)

diff --git a/tests/app/app.go b/tests/app/app.go
index ff66963..c80dce6 100644
--- a/tests/app/app.go
+++ b/tests/app/app.go
@@ -8,32 +8,9 @@ import (
 	"path/filepath"
 	"reflect"
 
-<<<<<<< HEAD
 	"cosmossdk.io/depinject"
 	dbm "github.com/cometbft/cometbft-db"
 	"github.com/cometbft/cometbft/libs/log"
-=======
-	autocliv1 "cosmossdk.io/api/cosmos/autocli/v1"
-	reflectionv1 "cosmossdk.io/api/cosmos/reflection/v1"
-	"cosmossdk.io/client/v2/autocli"
-	"cosmossdk.io/core/appmodule"
-	"cosmossdk.io/log"
-	storetypes "cosmossdk.io/store/types"
-	"cosmossdk.io/x/circuit"
-	circuitkeeper "cosmossdk.io/x/circuit/keeper"
-	circuittypes "cosmossdk.io/x/circuit/types"
-	evidencetypes "cosmossdk.io/x/evidence/types"
-	"cosmossdk.io/x/feegrant"
-	feegrantkeeper "cosmossdk.io/x/feegrant/keeper"
-	feegrantmodule "cosmossdk.io/x/feegrant/module"
-	"cosmossdk.io/x/nft"
-	"cosmossdk.io/x/tx/signing"
-	"cosmossdk.io/x/upgrade"
-	upgradekeeper "cosmossdk.io/x/upgrade/keeper"
-	upgradetypes "cosmossdk.io/x/upgrade/types"
-	abci "github.com/cometbft/cometbft/abci/types"
-	dbm "github.com/cosmos/cosmos-db"
->>>>>>> 04c7e6f (refactor: generic fallback ante decorator (#102))
 	"github.com/cosmos/cosmos-sdk/baseapp"
 	"github.com/cosmos/cosmos-sdk/client"
 	"github.com/cosmos/cosmos-sdk/codec"
@@ -83,19 +60,10 @@ import (
 	slashingkeeper "github.com/cosmos/cosmos-sdk/x/slashing/keeper"
 	"github.com/cosmos/cosmos-sdk/x/staking"
 	stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"
-<<<<<<< HEAD
 	"github.com/cosmos/cosmos-sdk/x/upgrade"
 	upgradeclient "github.com/cosmos/cosmos-sdk/x/upgrade/client"
 	upgradekeeper "github.com/cosmos/cosmos-sdk/x/upgrade/keeper"
-
 	feemarketmodule "github.com/skip-mev/feemarket/x/feemarket"
-=======
-	stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
-	"github.com/cosmos/gogoproto/proto"
-	"github.com/spf13/cast"
-
-	"github.com/skip-mev/feemarket/x/feemarket"
->>>>>>> 04c7e6f (refactor: generic fallback ante decorator (#102))
 	feemarketkeeper "github.com/skip-mev/feemarket/x/feemarket/keeper"
 	feemarkettypes "github.com/skip-mev/feemarket/x/feemarket/types"
 )