Skip to content
This repository has been archived by the owner on Apr 4, 2024. It is now read-only.

Problem: eth_newPendingTransactionFilter don't return correct tx hash #1012

Merged
merged 9 commits into from
Mar 24, 2022
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
* (ante) [tharsis#991](https://github.com/tharsis/ethermint/pull/991) Set an upper bound to gasWanted to prevent DoS attack.
* (rpc) [tharsis#1006](https://github.com/tharsis/ethermint/pull/1006) Use `string` as the parameters type to correct ambiguous results.
* (ante) [tharsis#1004](https://github.com/tharsis/ethermint/pull/1004) make MaxTxGasWanted configurable.
* (rpc) [tharsis#1012](https://github.com/tharsis/ethermint/pull/1012) fix the tx hash in filter entries created by `eth_newPendingTransactionFilter`.

## [v0.11.0] - 2022-03-06

Expand Down
2 changes: 1 addition & 1 deletion rpc/apis.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func GetRPCAPIs(ctx *server.Context, clientCtx client.Context, tmWSClient *rpccl
rpc.API{
Namespace: EthNamespace,
Version: apiVersion,
Service: filters.NewPublicAPI(ctx.Logger, tmWSClient, evmBackend),
Service: filters.NewPublicAPI(ctx.Logger, clientCtx, tmWSClient, evmBackend),
Public: true,
},
)
Expand Down
47 changes: 34 additions & 13 deletions rpc/ethereum/namespaces/eth/filters/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"sync"
"time"

"github.com/cosmos/cosmos-sdk/client"
"github.com/tharsis/ethermint/rpc/ethereum/types"

"github.com/tendermint/tendermint/libs/log"
Expand Down Expand Up @@ -56,20 +57,22 @@ type filter struct {
// information related to the Ethereum protocol such as blocks, transactions and logs.
type PublicFilterAPI struct {
logger log.Logger
clientCtx client.Context
backend Backend
events *EventSystem
filtersMu sync.Mutex
filters map[rpc.ID]*filter
}

// NewPublicAPI returns a new PublicFilterAPI instance.
func NewPublicAPI(logger log.Logger, tmWSClient *rpcclient.WSClient, backend Backend) *PublicFilterAPI {
func NewPublicAPI(logger log.Logger, clientCtx client.Context, tmWSClient *rpcclient.WSClient, backend Backend) *PublicFilterAPI {
logger = logger.With("api", "filter")
api := &PublicFilterAPI{
logger: logger,
backend: backend,
filters: make(map[rpc.ID]*filter),
events: NewEventSystem(logger, tmWSClient),
logger: logger,
clientCtx: clientCtx,
backend: backend,
filters: make(map[rpc.ID]*filter),
events: NewEventSystem(logger, tmWSClient),
}

go api.timeoutLoop()
Expand Down Expand Up @@ -141,11 +144,20 @@ func (api *PublicFilterAPI) NewPendingTransactionFilter() rpc.ID {
continue
}

txHash := common.BytesToHash(tmtypes.Tx(data.Tx).Hash())
tx, err := api.clientCtx.TxConfig.TxDecoder()(data.Tx)
if err != nil {
api.logger.Debug("fail to decode tx", "error", err.Error())
continue
}

api.filtersMu.Lock()
if f, found := api.filters[pendingTxSub.ID()]; found {
f.hashes = append(f.hashes, txHash)
for _, msg := range tx.GetMsgs() {
ethTx, ok := msg.(*evmtypes.MsgEthereumTx)
if ok {
f.hashes = append(f.hashes, common.HexToHash(ethTx.Hash))
}
}
}
api.filtersMu.Unlock()
case <-errCh:
Expand Down Expand Up @@ -198,13 +210,22 @@ func (api *PublicFilterAPI) NewPendingTransactions(ctx context.Context) (*rpc.Su
continue
}

txHash := common.BytesToHash(tmtypes.Tx(data.Tx).Hash())

// To keep the original behavior, send a single tx hash in one notification.
// TODO(rjl493456442) Send a batch of tx hashes in one notification
err = notifier.Notify(rpcSub.ID, txHash)
tx, err := api.clientCtx.TxConfig.TxDecoder()(data.Tx)
if err != nil {
return
api.logger.Debug("fail to decode tx", "error", err.Error())
continue
}

for _, msg := range tx.GetMsgs() {
ethTx, ok := msg.(*evmtypes.MsgEthereumTx)
if ok {
// To keep the original behavior, send a single tx hash in one notification.
// TODO(rjl493456442) Send a batch of tx hashes in one notification
fedekunze marked this conversation as resolved.
Show resolved Hide resolved
err = notifier.Notify(rpcSub.ID, common.HexToHash(ethTx.Hash))
if err != nil {
return
fedekunze marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
case <-rpcSub.Err():
pendingTxSub.Unsubscribe(api.events)
Expand Down