From ad83067f4a5f4d72b5bcc64e52da0ae5c8b6d74b Mon Sep 17 00:00:00 2001 From: yihuang Date: Thu, 13 Jan 2022 21:12:57 +0800 Subject: [PATCH] Problem: newPendingTransactions filter don't return ethereum tx hash backport: https://github.com/tharsis/ethermint/pull/900 --- CHANGELOG.md | 4 ++++ rpc/ethereum/types/utils.go | 9 +++++++-- rpc/websockets.go | 17 ++++++++++++----- server/json_rpc.go | 2 +- 4 files changed, 24 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 76d6a4f746..37d3cfea73 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -41,6 +41,10 @@ Ref: https://keepachangelog.com/en/1.0.0/ * (rpc) [crypto-org-chain#42](https://github.com/crypto-org-chain/ethermint/pull/42) Fix `eth_getLogs` when specify blockHash without address/topics, and limit the response size. +### Bug Fixes + +- (rpc) [tharsis#900](https://github.com/tharsis/ethermint/pull/900) newPendingTransactions filter should return ethereum tx hash. + ## [v0.7.2-cronos-6] - 2021-12-17 ### Bug Fixes diff --git a/rpc/ethereum/types/utils.go b/rpc/ethereum/types/utils.go index 0909741109..4b2623007e 100644 --- a/rpc/ethereum/types/utils.go +++ b/rpc/ethereum/types/utils.go @@ -3,6 +3,7 @@ package types import ( "context" "encoding/hex" + "errors" "fmt" "math/big" @@ -26,9 +27,13 @@ func RawTxToEthTx(clientCtx client.Context, txBz tmtypes.Tx) (*evmtypes.MsgEther return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error()) } - ethTx, ok := tx.(*evmtypes.MsgEthereumTx) + if len(tx.GetMsgs()) != 1 { + return nil, errors.New("not ethereum tx") + } + + ethTx, ok := tx.GetMsgs()[0].(*evmtypes.MsgEthereumTx) if !ok { - return nil, fmt.Errorf("invalid transaction type %T, expected %T", tx, evmtypes.MsgEthereumTx{}) + return nil, fmt.Errorf("invalid msg type %T, expected %T", tx, evmtypes.MsgEthereumTx{}) } return ethTx, nil } diff --git a/rpc/websockets.go b/rpc/websockets.go index 1be2a72981..c06ec0e0e1 100644 --- a/rpc/websockets.go +++ b/rpc/websockets.go @@ -11,6 +11,7 @@ import ( "net/http" "sync" + "github.com/cosmos/cosmos-sdk/client" "github.com/gorilla/mux" "github.com/gorilla/websocket" "github.com/pkg/errors" @@ -71,7 +72,7 @@ type websocketsServer struct { logger log.Logger } -func NewWebsocketsServer(logger log.Logger, tmWSClient *rpcclient.WSClient, cfg config.Config) WebsocketsServer { +func NewWebsocketsServer(clientCtx client.Context, logger log.Logger, tmWSClient *rpcclient.WSClient, cfg config.Config) WebsocketsServer { logger = logger.With("api", "websocket-server") _, port, _ := net.SplitHostPort(cfg.JSONRPC.Address) @@ -80,7 +81,7 @@ func NewWebsocketsServer(logger log.Logger, tmWSClient *rpcclient.WSClient, cfg wsAddr: cfg.JSONRPC.WsAddress, certFile: cfg.TLS.CertificatePath, keyFile: cfg.TLS.KeyPath, - api: newPubSubAPI(logger, tmWSClient), + api: newPubSubAPI(clientCtx, logger, tmWSClient), logger: logger, } } @@ -291,16 +292,18 @@ type pubSubAPI struct { filtersMu *sync.RWMutex filters map[rpc.ID]*wsSubscription logger log.Logger + clientCtx client.Context } // newPubSubAPI creates an instance of the ethereum PubSub API. -func newPubSubAPI(logger log.Logger, tmWSClient *rpcclient.WSClient) *pubSubAPI { +func newPubSubAPI(clientCtx client.Context, logger log.Logger, tmWSClient *rpcclient.WSClient) *pubSubAPI { logger = logger.With("module", "websocket-client") return &pubSubAPI{ events: rpcfilters.NewEventSystem(logger, tmWSClient), filtersMu: new(sync.RWMutex), filters: make(map[rpc.ID]*wsSubscription), logger: logger, + clientCtx: clientCtx, } } @@ -675,7 +678,11 @@ func (api *pubSubAPI) subscribePendingTransactions(wsConn *wsConn) (rpc.ID, erro select { case ev := <-txsCh: data, _ := ev.Data.(tmtypes.EventDataTx) - txHash := common.BytesToHash(tmtypes.Tx(data.Tx).Hash()) + ethTx, err := types.RawTxToEthTx(api.clientCtx, data.Tx) + if err != nil { + // not ethereum tx + panic("debug") + } api.filtersMu.RLock() for subID, wsSub := range api.filters { @@ -690,7 +697,7 @@ func (api *pubSubAPI) subscribePendingTransactions(wsConn *wsConn) (rpc.ID, erro Method: "eth_subscription", Params: &SubscriptionResult{ Subscription: subID, - Result: txHash, + Result: ethTx.Hash, }, } diff --git a/server/json_rpc.go b/server/json_rpc.go index 204427fce7..315cc850dc 100644 --- a/server/json_rpc.go +++ b/server/json_rpc.go @@ -75,7 +75,7 @@ func StartJSONRPC(ctx *server.Context, clientCtx client.Context, tmRPCAddr, tmEn // allocate separate WS connection to Tendermint tmWsClient = ConnectTmWS(tmRPCAddr, tmEndpoint, ctx.Logger) - wsSrv := rpc.NewWebsocketsServer(ctx.Logger, tmWsClient, config) + wsSrv := rpc.NewWebsocketsServer(clientCtx, ctx.Logger, tmWsClient, config) wsSrv.Start() return httpSrv, httpSrvDone, nil }