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

Commit

Permalink
evm, rpc: fix tx log attribute value is not parsable by some client (#…
Browse files Browse the repository at this point in the history
…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>
  • Loading branch information
3 people authored Oct 5, 2021
1 parent d6423f0 commit cda968b
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
9 changes: 7 additions & 2 deletions rpc/ethereum/backend/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}

Expand Down
12 changes: 8 additions & 4 deletions rpc/ethereum/backend/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
8 changes: 6 additions & 2 deletions x/evm/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package keeper

import (
"context"
"encoding/json"
"fmt"

"github.com/palantir/stacktrace"
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit cda968b

Please sign in to comment.