From cda968bddde343bbbf1dc53ec5dcdde2ef384422 Mon Sep 17 00:00:00 2001 From: yihuang Date: Tue, 5 Oct 2021 18:26:31 +0800 Subject: [PATCH] evm, rpc: fix tx log attribute value is not parsable by some client (#615) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Problem: tx log attribute value not parsable by some client Closes: #614 Solution: - encode the value to json string rather than bytes Apply suggestions from code review * rm cdc and changelog Co-authored-by: Federico Kunze Küllmer <31522760+fedekunze@users.noreply.github.com> Co-authored-by: Federico Kunze --- CHANGELOG.md | 1 + rpc/ethereum/backend/backend.go | 9 +++++++-- rpc/ethereum/backend/utils.go | 12 ++++++++---- x/evm/keeper/msg_server.go | 8 ++++++-- 4 files changed, 22 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f10db1cc..4afb6097 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -53,6 +53,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ ### Bug Fixes +* (rpc, evm) [tharsis#614](https://github.com/tharsis/ethermint/issues/614) Use JSON for (un)marshaling tx `Log`s from events. * (rpc) [tharsis#611](https://github.com/tharsis/ethermint/pull/611) Fix panic on JSON-RPC when querying for an invalid block height. * (cmd) [tharsis#483](https://github.com/tharsis/ethermint/pull/483) Use config values on genesis accounts. diff --git a/rpc/ethereum/backend/backend.go b/rpc/ethereum/backend/backend.go index 67e3ce80..39534d59 100644 --- a/rpc/ethereum/backend/backend.go +++ b/rpc/ethereum/backend/backend.go @@ -400,7 +400,8 @@ func (e *EVMBackend) GetTransactionLogs(txHash common.Hash) ([]*ethtypes.Log, er if err != nil { return nil, err } - return TxLogsFromEvents(e.clientCtx.Codec, tx.TxResult.Events), nil + + return TxLogsFromEvents(tx.TxResult.Events) } // PendingTransactions returns the transactions that are in the transaction pool @@ -433,7 +434,11 @@ func (e *EVMBackend) GetLogsByHeight(height *int64) ([][]*ethtypes.Log, error) { blockLogs := [][]*ethtypes.Log{} for _, txResult := range blockRes.TxsResults { - logs := TxLogsFromEvents(e.clientCtx.Codec, txResult.Events) + logs, err := TxLogsFromEvents(txResult.Events) + if err != nil { + return nil, err + } + blockLogs = append(blockLogs, logs) } diff --git a/rpc/ethereum/backend/utils.go b/rpc/ethereum/backend/utils.go index 9453e87b..9f243913 100644 --- a/rpc/ethereum/backend/utils.go +++ b/rpc/ethereum/backend/utils.go @@ -2,10 +2,10 @@ package backend import ( "bytes" + "encoding/json" "errors" "math/big" - "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" "github.com/ethereum/go-ethereum/common" @@ -135,21 +135,25 @@ func (e *EVMBackend) getAccountNonce(accAddr common.Address, pending bool, heigh } // TxLogsFromEvents parses ethereum logs from cosmos events -func TxLogsFromEvents(codec codec.Codec, events []abci.Event) []*ethtypes.Log { +func TxLogsFromEvents(events []abci.Event) ([]*ethtypes.Log, error) { logs := make([]*evmtypes.Log, 0) for _, event := range events { if event.Type != evmtypes.EventTypeTxLog { continue } + for _, attr := range event.Attributes { if !bytes.Equal(attr.Key, []byte(evmtypes.AttributeKeyTxLog)) { continue } var log evmtypes.Log - codec.MustUnmarshal(attr.Value, &log) + if err := json.Unmarshal(attr.Value, &log); err != nil { + return nil, err + } + logs = append(logs, &log) } } - return evmtypes.LogsToEthereum(logs) + return evmtypes.LogsToEthereum(logs), nil } diff --git a/x/evm/keeper/msg_server.go b/x/evm/keeper/msg_server.go index 10831db1..056b660a 100644 --- a/x/evm/keeper/msg_server.go +++ b/x/evm/keeper/msg_server.go @@ -2,6 +2,7 @@ package keeper import ( "context" + "encoding/json" "fmt" "github.com/palantir/stacktrace" @@ -54,8 +55,11 @@ func (k *Keeper) EthereumTx(goCtx context.Context, msg *types.MsgEthereumTx) (*t txLogAttrs := make([]sdk.Attribute, 0) for _, log := range response.Logs { - bz := k.cdc.MustMarshal(log) - txLogAttrs = append(txLogAttrs, sdk.NewAttribute(types.AttributeKeyTxLog, string(bz))) + value, err := json.Marshal(log) + if err != nil { + return nil, stacktrace.Propagate(err, "failed to encode log") + } + txLogAttrs = append(txLogAttrs, sdk.NewAttribute(types.AttributeKeyTxLog, string(value))) } // emit events