Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem: decode tx is unnecessary in tx listener (backport: #491) #492

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
### Improvements

* (app) [#483](https://github.com/crypto-org-chain/ethermint/pull/483) Make keyring-backend client config accessible in app.
* (rpc) [#491](https://github.com/crypto-org-chain/ethermint/pull/491) Avoid unnecessary tx decode in tx listener.

## v0.21.x-cronos

Expand Down
2 changes: 2 additions & 0 deletions app/ante/handler_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ type HandlerOptions struct {
TxFeeChecker ante.TxFeeChecker
DisabledAuthzMsgs []string
ExtraDecorators []sdk.AnteDecorator
PendingTxListener PendingTxListener
}

func (options HandlerOptions) validate() error {
Expand Down Expand Up @@ -88,6 +89,7 @@ func newEthAnteHandler(ctx sdk.Context, options HandlerOptions, extra ...sdk.Ant
NewEthEmitEventDecorator(options.EvmKeeper), // emit eth tx hash and index at the very last ante handler.
}
decorators = append(decorators, extra...)
decorators = append(decorators, newTxListenerDecorator(options.PendingTxListener))
return sdk.ChainAnteDecorators(decorators...)
}

Expand Down
33 changes: 33 additions & 0 deletions app/ante/tx_listener.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package ante

import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/ethereum/go-ethereum/common"
evmtypes "github.com/evmos/ethermint/x/evm/types"
)

type PendingTxListener func(common.Hash)

type TxListenerDecorator struct {
pendingTxListener PendingTxListener
}

// newTxListenerDecorator creates a new TxListenerDecorator with the provided PendingTxListener.
// CONTRACT: must be put at the last of the chained decorators
func newTxListenerDecorator(pendingTxListener PendingTxListener) TxListenerDecorator {
return TxListenerDecorator{pendingTxListener}
}

func (d TxListenerDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (sdk.Context, error) {
if ctx.IsReCheckTx() {
return next(ctx, tx, simulate)
}
if ctx.IsCheckTx() && !simulate && d.pendingTxListener != nil {
for _, msg := range tx.GetMsgs() {
if ethTx, ok := msg.(*evmtypes.MsgEthereumTx); ok {
d.pendingTxListener(common.HexToHash(ethTx.Hash))

Check warning on line 28 in app/ante/tx_listener.go

View check run for this annotation

Codecov / codecov/patch

app/ante/tx_listener.go#L26-L28

Added lines #L26 - L28 were not covered by tests
}
}
}
return next(ctx, tx, simulate)
}
20 changes: 8 additions & 12 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@

consensusparamkeeper "github.com/cosmos/cosmos-sdk/x/consensus/keeper"
consensusparamtypes "github.com/cosmos/cosmos-sdk/x/consensus/types"
"github.com/ethereum/go-ethereum/common"

// Force-load the tracer engines to trigger registration due to Go-Ethereum v1.10.15 changes
_ "github.com/ethereum/go-ethereum/eth/tracers/js"
Expand Down Expand Up @@ -215,8 +216,6 @@
_ servertypes.Application = (*EthermintApp)(nil)
)

type PendingTxListener func([]byte)

// var _ server.Application (*EthermintApp)(nil)

// EthermintApp implements an extended ABCI application. It is an application
Expand All @@ -233,7 +232,7 @@

invCheckPeriod uint

pendingTxListeners []PendingTxListener
pendingTxListeners []ante.PendingTxListener

// keys to access the substores
keys map[string]*storetypes.KVStoreKey
Expand Down Expand Up @@ -771,6 +770,7 @@
sdk.MsgTypeURL(&evmtypes.MsgEthereumTx{}),
sdk.MsgTypeURL(&vestingtypes.MsgCreateVestingAccount{}),
},
PendingTxListener: app.onPendingTx,
})
if err != nil {
panic(err)
Expand Down Expand Up @@ -951,19 +951,15 @@
node.RegisterNodeService(clientCtx, app.GRPCQueryRouter())
}

// RegisterPendingTxListener is used by json-rpc server to listen to pending transactions in CheckTx.
func (app *EthermintApp) RegisterPendingTxListener(listener PendingTxListener) {
// RegisterPendingTxListener is used by json-rpc server to listen to pending transactions callback.
func (app *EthermintApp) RegisterPendingTxListener(listener ante.PendingTxListener) {

Check warning on line 955 in app/app.go

View check run for this annotation

Codecov / codecov/patch

app/app.go#L955

Added line #L955 was not covered by tests
app.pendingTxListeners = append(app.pendingTxListeners, listener)
}

func (app *EthermintApp) CheckTx(req abci.RequestCheckTx) abci.ResponseCheckTx {
res := app.BaseApp.CheckTx(req)
if res.Code == 0 && req.Type == abci.CheckTxType_New {
for _, listener := range app.pendingTxListeners {
listener(req.Tx)
}
func (app *EthermintApp) onPendingTx(hash common.Hash) {
for _, listener := range app.pendingTxListeners {
listener(hash)

Check warning on line 961 in app/app.go

View check run for this annotation

Codecov / codecov/patch

app/app.go#L959-L961

Added lines #L959 - L961 were not covered by tests
}
return res
}

// RegisterSwaggerAPI registers swagger route with API Server
Expand Down
16 changes: 2 additions & 14 deletions rpc/stream/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,20 +115,8 @@
}

// ListenPendingTx is a callback passed to application to listen for pending transactions in CheckTx.
func (s *RPCStream) ListenPendingTx(bytes []byte) {
tx, err := s.txDecoder(bytes)
if err != nil {
s.logger.Error("fail to decode tx", "error", err.Error())
return
}

var hashes []common.Hash
for _, msg := range tx.GetMsgs() {
if ethTx, ok := msg.(*evmtypes.MsgEthereumTx); ok {
hashes = append(hashes, ethTx.AsTransaction().Hash())
}
}
s.pendingTxStream.Add(hashes...)
func (s *RPCStream) ListenPendingTx(hash common.Hash) {
s.pendingTxStream.Add(hash)

Check warning on line 119 in rpc/stream/rpc.go

View check run for this annotation

Codecov / codecov/patch

rpc/stream/rpc.go#L118-L119

Added lines #L118 - L119 were not covered by tests
}

func (s *RPCStream) start(
Expand Down
5 changes: 2 additions & 3 deletions server/json_rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,17 @@ import (
"github.com/cosmos/cosmos-sdk/server/types"
ethlog "github.com/ethereum/go-ethereum/log"
ethrpc "github.com/ethereum/go-ethereum/rpc"
"github.com/evmos/ethermint/app"
"github.com/evmos/ethermint/app/ante"
"github.com/evmos/ethermint/rpc"
"github.com/evmos/ethermint/rpc/stream"

"github.com/evmos/ethermint/server/config"
ethermint "github.com/evmos/ethermint/types"
)

const MaxRetry = 6

type AppWithPendingTxStream interface {
RegisterPendingTxListener(listener app.PendingTxListener)
RegisterPendingTxListener(listener ante.PendingTxListener)
}

// StartJSONRPC starts the JSON-RPC server
Expand Down
Loading