evm, rpc: fix tx log attribute value is not parsable by some client (#615)

* 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 <federico.kunze94@gmail.com>
This commit is contained in:
yihuang 2021-10-05 18:26:31 +08:00 committed by GitHub
parent d6423f0c3f
commit cda968bddd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 22 additions and 8 deletions

View File

@ -53,6 +53,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
### Bug Fixes ### 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. * (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. * (cmd) [tharsis#483](https://github.com/tharsis/ethermint/pull/483) Use config values on genesis accounts.

View File

@ -400,7 +400,8 @@ func (e *EVMBackend) GetTransactionLogs(txHash common.Hash) ([]*ethtypes.Log, er
if err != nil { if err != nil {
return nil, err 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 // 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{} blockLogs := [][]*ethtypes.Log{}
for _, txResult := range blockRes.TxsResults { 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) blockLogs = append(blockLogs, logs)
} }

View File

@ -2,10 +2,10 @@ package backend
import ( import (
"bytes" "bytes"
"encoding/json"
"errors" "errors"
"math/big" "math/big"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types" sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/ethereum/go-ethereum/common" "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 // 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) logs := make([]*evmtypes.Log, 0)
for _, event := range events { for _, event := range events {
if event.Type != evmtypes.EventTypeTxLog { if event.Type != evmtypes.EventTypeTxLog {
continue continue
} }
for _, attr := range event.Attributes { for _, attr := range event.Attributes {
if !bytes.Equal(attr.Key, []byte(evmtypes.AttributeKeyTxLog)) { if !bytes.Equal(attr.Key, []byte(evmtypes.AttributeKeyTxLog)) {
continue continue
} }
var log evmtypes.Log 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) logs = append(logs, &log)
} }
} }
return evmtypes.LogsToEthereum(logs) return evmtypes.LogsToEthereum(logs), nil
} }

View File

@ -2,6 +2,7 @@ package keeper
import ( import (
"context" "context"
"encoding/json"
"fmt" "fmt"
"github.com/palantir/stacktrace" "github.com/palantir/stacktrace"
@ -54,8 +55,11 @@ func (k *Keeper) EthereumTx(goCtx context.Context, msg *types.MsgEthereumTx) (*t
txLogAttrs := make([]sdk.Attribute, 0) txLogAttrs := make([]sdk.Attribute, 0)
for _, log := range response.Logs { for _, log := range response.Logs {
bz := k.cdc.MustMarshal(log) value, err := json.Marshal(log)
txLogAttrs = append(txLogAttrs, sdk.NewAttribute(types.AttributeKeyTxLog, string(bz))) if err != nil {
return nil, stacktrace.Propagate(err, "failed to encode log")
}
txLogAttrs = append(txLogAttrs, sdk.NewAttribute(types.AttributeKeyTxLog, string(value)))
} }
// emit events // emit events