From 5076a1f33c88395f80ae62a8eb85727f6d2ad9a0 Mon Sep 17 00:00:00 2001 From: MinZhu Date: Mon, 2 May 2022 09:10:07 +0800 Subject: [PATCH] =?UTF-8?q?Merge=20PR=EF=BC=9Asmb=20fix=20(#1971)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * add log * change log * remove for test * optimize GetTxFeeAndFromHandler * remove log * encapsulate getTxFeeHandler for serial usage * optimize GetTxFeeHandler * optimize getTxFeeHandler --- app/app.go | 1 + app/app_parallel.go | 42 ++++++++++++++++++++++++ app/app_partial_concurrent.go | 38 --------------------- libs/cosmos-sdk/baseapp/baseapp.go | 5 +-- libs/cosmos-sdk/baseapp/baseapp_runtx.go | 4 +-- libs/cosmos-sdk/baseapp/options.go | 7 ++++ libs/cosmos-sdk/types/handler.go | 1 + 7 files changed, 56 insertions(+), 42 deletions(-) delete mode 100644 app/app_partial_concurrent.go diff --git a/app/app.go b/app/app.go index 2a5c0dbd3d..99de2bba94 100644 --- a/app/app.go +++ b/app/app.go @@ -532,6 +532,7 @@ func NewOKExChainApp( app.SetParallelTxHandlers(updateFeeCollectorHandler(app.BankKeeper, app.SupplyKeeper), fixLogForParallelTxHandler(app.EvmKeeper)) app.SetPreDeliverTxHandler(preDeliverTxHandler(app.AccountKeeper)) app.SetPartialConcurrentHandlers(getTxFeeAndFromHandler(app.AccountKeeper)) + app.SetGetTxFeeHandler(getTxFeeHandler()) if loadLatest { err := app.LoadLatestVersion(app.keys[bam.MainStoreKey]) diff --git a/app/app_parallel.go b/app/app_parallel.go index bb5113aada..d6c5b1e3fc 100644 --- a/app/app_parallel.go +++ b/app/app_parallel.go @@ -1,9 +1,13 @@ package app import ( + "encoding/hex" + "strings" + ethermint "github.com/okex/exchain/app/types" sdk "github.com/okex/exchain/libs/cosmos-sdk/types" "github.com/okex/exchain/libs/cosmos-sdk/x/auth" + authante "github.com/okex/exchain/libs/cosmos-sdk/x/auth/ante" "github.com/okex/exchain/libs/cosmos-sdk/x/bank" "github.com/okex/exchain/libs/cosmos-sdk/x/supply" "github.com/okex/exchain/x/evm" @@ -61,3 +65,41 @@ func evmTxVerifySigHandler(chainID string, blockHeight int64, evmTx *evmtypes.Ms } return nil } + +func getTxFeeHandler() sdk.GetTxFeeHandler { + return func(tx sdk.Tx) (fee sdk.Coins) { + if feeTx, ok := tx.(authante.FeeTx); ok { + fee = feeTx.GetFee() + } + + return + } +} + +// getTxFeeAndFromHandler get tx fee and from +func getTxFeeAndFromHandler(ak auth.AccountKeeper) sdk.GetTxFeeAndFromHandler { + return func(ctx sdk.Context, tx sdk.Tx) (fee sdk.Coins, isEvm bool, from string, to string, err error) { + if evmTx, ok := tx.(*evmtypes.MsgEthereumTx); ok { + isEvm = true + err = evmTx.VerifySig(evmTx.ChainID(), ctx.BlockHeight()) + if err != nil { + return + } + fee = evmTx.GetFee() + from = evmTx.BaseTx.From + if len(from) > 2 { + from = strings.ToLower(from[2:]) + } + if evmTx.To() != nil { + to = strings.ToLower(evmTx.To().String()[2:]) + } + } else if feeTx, ok := tx.(authante.FeeTx); ok { + fee = feeTx.GetFee() + feePayer := feeTx.FeePayer(ctx) + feePayerAcc := ak.GetAccount(ctx, feePayer) + from = hex.EncodeToString(feePayerAcc.GetAddress()) + } + + return + } +} diff --git a/app/app_partial_concurrent.go b/app/app_partial_concurrent.go deleted file mode 100644 index 51567dc0b6..0000000000 --- a/app/app_partial_concurrent.go +++ /dev/null @@ -1,38 +0,0 @@ -package app - -import ( - "encoding/hex" - sdk "github.com/okex/exchain/libs/cosmos-sdk/types" - "github.com/okex/exchain/libs/cosmos-sdk/x/auth" - authante "github.com/okex/exchain/libs/cosmos-sdk/x/auth/ante" - evmtypes "github.com/okex/exchain/x/evm/types" - "strings" -) - -// getTxFeeAndFromHandler get tx fee and from -func getTxFeeAndFromHandler(ak auth.AccountKeeper) sdk.GetTxFeeAndFromHandler { - return func(ctx sdk.Context, tx sdk.Tx) (fee sdk.Coins, isEvm bool, from string, to string, err error) { - if evmTx, ok := tx.(*evmtypes.MsgEthereumTx); ok { - isEvm = true - err = evmTx.VerifySig(evmTx.ChainID(), ctx.BlockHeight()) - if err != nil { - return - } - fee = evmTx.GetFee() - from = evmTx.BaseTx.From - if len(from) > 2 { - from = strings.ToLower(from[2:]) - } - if evmTx.To() != nil { - to = strings.ToLower(evmTx.To().String()[2:]) - } - } else if feeTx, ok := tx.(authante.FeeTx); ok { - fee = feeTx.GetFee() - feePayer := feeTx.FeePayer(ctx) - feePayerAcc := ak.GetAccount(ctx, feePayer) - from = hex.EncodeToString(feePayerAcc.GetAddress()) - } - - return - } -} diff --git a/libs/cosmos-sdk/baseapp/baseapp.go b/libs/cosmos-sdk/baseapp/baseapp.go index 129826dc24..09c3af5be4 100644 --- a/libs/cosmos-sdk/baseapp/baseapp.go +++ b/libs/cosmos-sdk/baseapp/baseapp.go @@ -149,6 +149,7 @@ type BaseApp struct { // nolint: maligned logFix sdk.LogFix getTxFeeAndFromHandler sdk.GetTxFeeAndFromHandler + getTxFeeHandler sdk.GetTxFeeHandler // volatile states: // @@ -194,8 +195,8 @@ type BaseApp struct { // nolint: maligned parallelTxManage *parallelTxManager deliverTxsMgr *DTTManager - feeForCollector sdk.Coins - feeChanged bool // used to judge whether should update the fee-collector account + feeForCollector sdk.Coins + feeChanged bool // used to judge whether should update the fee-collector account chainCache *sdk.Cache blockCache *sdk.Cache diff --git a/libs/cosmos-sdk/baseapp/baseapp_runtx.go b/libs/cosmos-sdk/baseapp/baseapp_runtx.go index 5119a6a55b..a52c716dbf 100644 --- a/libs/cosmos-sdk/baseapp/baseapp_runtx.go +++ b/libs/cosmos-sdk/baseapp/baseapp_runtx.go @@ -125,8 +125,8 @@ func (app *BaseApp) runtxWithInfo(info *runTxInfo, mode runTxMode, txBytes []byt } app.pin(RunAnte, false, mode) - if app.getTxFeeAndFromHandler != nil && mode == runTxModeDeliver { - fee, _, _, _, _ := app.getTxFeeAndFromHandler(info.ctx, tx) + if app.getTxFeeHandler != nil && mode == runTxModeDeliver { + fee := app.getTxFeeHandler(tx) app.UpdateFeeForCollector(fee, true) } diff --git a/libs/cosmos-sdk/baseapp/options.go b/libs/cosmos-sdk/baseapp/options.go index 2ed0db2adc..b925d1e3ca 100644 --- a/libs/cosmos-sdk/baseapp/options.go +++ b/libs/cosmos-sdk/baseapp/options.go @@ -184,3 +184,10 @@ func (app *BaseApp) SetPartialConcurrentHandlers(etf sdk.GetTxFeeAndFromHandler) } app.getTxFeeAndFromHandler = etf } + +func (app *BaseApp) SetGetTxFeeHandler(handler sdk.GetTxFeeHandler){ + if app.sealed { + panic("SetGetTxFeeHandler() on sealed BaseApp") + } + app.getTxFeeHandler = handler +} \ No newline at end of file diff --git a/libs/cosmos-sdk/types/handler.go b/libs/cosmos-sdk/types/handler.go index 505876e2c7..e614a1eb59 100644 --- a/libs/cosmos-sdk/types/handler.go +++ b/libs/cosmos-sdk/types/handler.go @@ -18,6 +18,7 @@ type UpdateFeeCollectorAccHandler func(ctx Context, balance Coins) error type LogFix func(logIndex []int, errs []error) (logs [][]byte) type GetTxFeeAndFromHandler func(ctx Context, tx Tx) (Coins, bool, string, string, error) +type GetTxFeeHandler func(tx Tx) Coins // AnteDecorator wraps the next AnteHandler to perform custom pre- and post-processing. type AnteDecorator interface {