From 7da60cd2518c929713d274c3a3148ce487b608c0 Mon Sep 17 00:00:00 2001 From: HuangYi Date: Mon, 11 Jul 2022 17:03:23 +0800 Subject: [PATCH] NewAnteHandler returns error if input not valid make NewAnteHandler safer too use, and closer to cosmos-sdk api. extracted from https://github.com/evmos/ethermint/pull/1168 --- app/ante/ante.go | 7 +++++-- app/ante/handler_options.go | 2 +- app/ante/utils_test.go | 9 ++++----- app/app.go | 10 ++++------ 4 files changed, 14 insertions(+), 14 deletions(-) diff --git a/app/ante/ante.go b/app/ante/ante.go index 94cc70289c..50fabbffdc 100644 --- a/app/ante/ante.go +++ b/app/ante/ante.go @@ -23,7 +23,10 @@ const ( // Ethereum or SDK transaction to an internal ante handler for performing // transaction-level processing (e.g. fee payment, signature verification) before // being passed onto it's respective handler. -func NewAnteHandler(options HandlerOptions) sdk.AnteHandler { +func NewAnteHandler(options HandlerOptions) (sdk.AnteHandler, error) { + if err := options.validate(); err != nil { + return nil, err + } return func( ctx sdk.Context, tx sdk.Tx, sim bool, ) (newCtx sdk.Context, err error) { @@ -62,7 +65,7 @@ func NewAnteHandler(options HandlerOptions) sdk.AnteHandler { } return anteHandler(ctx, tx, sim) - } + }, nil } func Recover(logger tmlog.Logger, err *error) { diff --git a/app/ante/handler_options.go b/app/ante/handler_options.go index 79271e3ddd..ec0e34ed47 100644 --- a/app/ante/handler_options.go +++ b/app/ante/handler_options.go @@ -28,7 +28,7 @@ type HandlerOptions struct { MaxTxGasWanted uint64 } -func (options HandlerOptions) Validate() error { +func (options HandlerOptions) validate() error { if options.AccountKeeper == nil { return sdkerrors.Wrap(sdkerrors.ErrLogic, "account keeper is required for AnteHandler") } diff --git a/app/ante/utils_test.go b/app/ante/utils_test.go index 5c030eb82d..b789f12882 100644 --- a/app/ante/utils_test.go +++ b/app/ante/utils_test.go @@ -105,7 +105,7 @@ func (suite *AnteTestSuite) SetupTest() { suite.clientCtx = client.Context{}.WithTxConfig(encodingConfig.TxConfig) - options := ante.HandlerOptions{ + anteHandler, err := ante.NewAnteHandler(ante.HandlerOptions{ AccountKeeper: suite.app.AccountKeeper, BankKeeper: suite.app.BankKeeper, EvmKeeper: suite.app.EvmKeeper, @@ -114,11 +114,10 @@ func (suite *AnteTestSuite) SetupTest() { FeeMarketKeeper: suite.app.FeeMarketKeeper, SignModeHandler: encodingConfig.TxConfig.SignModeHandler(), SigGasConsumer: ante.DefaultSigVerificationGasConsumer, - } - - suite.Require().NoError(options.Validate()) + }) + suite.Require().NoError(err) - suite.anteHandler = ante.NewAnteHandler(options) + suite.anteHandler = anteHandler suite.ethSigner = ethtypes.LatestSignerForChainID(suite.app.EvmKeeper.ChainID()) } diff --git a/app/app.go b/app/app.go index c0acf7d426..b0a6f5455a 100644 --- a/app/app.go +++ b/app/app.go @@ -570,7 +570,7 @@ func NewEthermintApp( // use Ethermint's custom AnteHandler maxGasWanted := cast.ToUint64(appOpts.Get(srvflags.EVMMaxTxGasWanted)) - options := ante.HandlerOptions{ + anteHandler, err := ante.NewAnteHandler(ante.HandlerOptions{ AccountKeeper: app.AccountKeeper, BankKeeper: app.BankKeeper, EvmKeeper: app.EvmKeeper, @@ -580,13 +580,11 @@ func NewEthermintApp( SignModeHandler: encodingConfig.TxConfig.SignModeHandler(), SigGasConsumer: ante.DefaultSigVerificationGasConsumer, MaxTxGasWanted: maxGasWanted, - } - - if err := options.Validate(); err != nil { + }) + if err != nil { panic(err) } - - app.SetAnteHandler(ante.NewAnteHandler(options)) + app.SetAnteHandler(anteHandler) app.SetEndBlocker(app.EndBlocker) if loadLatest {