Skip to content

Commit

Permalink
Problem: block number is not filled in eth logs (#345)
Browse files Browse the repository at this point in the history
* Problem: block number is not filled in eth logs

Solution:
- fill in both block hash and block number in json-rpc
- remove the block hash in events, to save some space in tx_indexer

* fix unit test

* fix unit test

* fill in tx hash to logs

* fix block number

* cleanup

* remove tx log event
  • Loading branch information
yihuang authored Sep 13, 2023
1 parent 922f9eb commit 199ccc2
Show file tree
Hide file tree
Showing 17 changed files with 209 additions and 189 deletions.
2 changes: 2 additions & 0 deletions proto/ethermint/evm/v1/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,8 @@ message MsgEthereumTxResponse {
string vm_error = 4;
// gas_used specifies how much gas was consumed by the transaction
uint64 gas_used = 5;
// include the block hash for json-rpc to use
bytes block_hash = 6;
}

// MsgUpdateParams defines a Msg for updating the x/evm module parameters.
Expand Down
25 changes: 17 additions & 8 deletions rpc/backend/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/cosmos/cosmos-sdk/client"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
errortypes "github.com/cosmos/cosmos-sdk/types/errors"

abci "github.com/cometbft/cometbft/abci/types"
Expand All @@ -18,6 +19,7 @@ import (
"github.com/evmos/ethermint/rpc/backend/mocks"
rpc "github.com/evmos/ethermint/rpc/types"
evmtypes "github.com/evmos/ethermint/x/evm/types"
"github.com/gogo/protobuf/proto"
mock "github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -178,19 +180,26 @@ func TestRegisterConsensusParams(t *testing.T) {
// BlockResults

func RegisterBlockResultsWithEventLog(client *mocks.Client, height int64) (*tmrpctypes.ResultBlockResults, error) {
any, err := codectypes.NewAnyWithValue(&evmtypes.MsgEthereumTxResponse{
Logs: []*evmtypes.Log{
{Data: []byte("data")},
},
})
if err != nil {
return nil, err
}
data, err := proto.Marshal(&sdk.TxMsgData{MsgResponses: []*codectypes.Any{any}})
if err != nil {
return nil, err
}

res := &tmrpctypes.ResultBlockResults{
Height: height,
TxsResults: []*abci.ResponseDeliverTx{
{Code: 0, GasUsed: 0, Events: []abci.Event{{
Type: evmtypes.EventTypeTxLog,
Attributes: []abci.EventAttribute{{
Key: evmtypes.AttributeKeyTxLog,
Value: "{\"test\": \"hello\"}",
Index: true,
}},
}}},
{Code: 0, GasUsed: 0, Data: data},
},
}

client.On("BlockResults", rpc.ContextWithHeight(height), mock.AnythingOfType("*int64")).
Return(res, nil)
return res, nil
Expand Down
9 changes: 4 additions & 5 deletions rpc/backend/filters_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package backend

import (
"encoding/json"

tmtypes "github.com/cometbft/cometbft/types"
"github.com/ethereum/go-ethereum/common"
ethtypes "github.com/ethereum/go-ethereum/core/types"
Expand All @@ -15,9 +13,10 @@ func (suite *BackendTestSuite) TestGetLogs() {
_, bz := suite.buildEthereumTx()
block := tmtypes.MakeBlock(1, []tmtypes.Tx{bz}, nil, nil)
logs := make([]*evmtypes.Log, 0, 1)
var log evmtypes.Log
json.Unmarshal([]byte{0x7b, 0x22, 0x74, 0x65, 0x73, 0x74, 0x22, 0x3a, 0x20, 0x22, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x22, 0x7d}, &log)
logs = append(logs, &log)
logs = append(logs, &evmtypes.Log{
Data: []byte("data"),
BlockNumber: 1,
})

testCases := []struct {
name string
Expand Down
2 changes: 1 addition & 1 deletion rpc/backend/tx_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ func (b *Backend) GetTransactionReceipt(hash common.Hash) (map[string]interface{
}

// parse tx logs from events
logs, err := TxLogsFromEvents(blockRes.TxsResults[res.TxIndex].Events, int(res.MsgIndex))
logs, err := evmtypes.DecodeMsgLogsFromEvents(blockRes.TxsResults[res.TxIndex].Data, int(res.MsgIndex), uint64(blockRes.Height))
if err != nil {
b.logger.Debug("failed to parse logs", "hash", hexTx, "error", err.Error())
}
Expand Down
57 changes: 2 additions & 55 deletions rpc/backend/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
package backend

import (
"encoding/json"
"fmt"
"math/big"
"sort"
Expand Down Expand Up @@ -277,57 +276,6 @@ func (b *Backend) processBlock(
return nil
}

// AllTxLogsFromEvents parses all ethereum logs from cosmos events
func AllTxLogsFromEvents(events []abci.Event) ([][]*ethtypes.Log, error) {
allLogs := make([][]*ethtypes.Log, 0, 4)
for _, event := range events {
if event.Type != evmtypes.EventTypeTxLog {
continue
}

logs, err := ParseTxLogsFromEvent(event)
if err != nil {
return nil, err
}

allLogs = append(allLogs, logs)
}
return allLogs, nil
}

// TxLogsFromEvents parses ethereum logs from cosmos events for specific msg index
func TxLogsFromEvents(events []abci.Event, msgIndex int) ([]*ethtypes.Log, error) {
for _, event := range events {
if event.Type != evmtypes.EventTypeTxLog {
continue
}

if msgIndex > 0 {
// not the eth tx we want
msgIndex--
continue
}

return ParseTxLogsFromEvent(event)
}
return nil, fmt.Errorf("eth tx logs not found for message index %d", msgIndex)
}

// ParseTxLogsFromEvent parse tx logs from one event
func ParseTxLogsFromEvent(event abci.Event) ([]*ethtypes.Log, error) {
var ethLogs []*ethtypes.Log
for _, attr := range event.Attributes {
var log evmtypes.Log
if attr.Key == evmtypes.AttributeKeyTxLog {
if err := json.Unmarshal([]byte(attr.Value), &log); err != nil {
return nil, err
}
ethLogs = append(ethLogs, log.ToEthereum())
}
}
return ethLogs, nil
}

// ShouldIgnoreGasUsed returns true if the gasUsed in result should be ignored
// workaround for issue: https://github.com/cosmos/cosmos-sdk/issues/10832
func ShouldIgnoreGasUsed(res *abci.ResponseDeliverTx) bool {
Expand All @@ -338,12 +286,11 @@ func ShouldIgnoreGasUsed(res *abci.ResponseDeliverTx) bool {
func GetLogsFromBlockResults(blockRes *tmrpctypes.ResultBlockResults) ([][]*ethtypes.Log, error) {
blockLogs := [][]*ethtypes.Log{}
for _, txResult := range blockRes.TxsResults {
logs, err := AllTxLogsFromEvents(txResult.Events)
logs, err := evmtypes.DecodeTxLogsFromEvents(txResult.Data, uint64(blockRes.Height))
if err != nil {
return nil, err
}

blockLogs = append(blockLogs, logs...)
blockLogs = append(blockLogs, logs)
}
return blockLogs, nil
}
Expand Down
8 changes: 7 additions & 1 deletion rpc/namespaces/ethereum/eth/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,13 @@ func (e *PublicAPI) GetTransactionLogs(txHash common.Hash) ([]*ethtypes.Log, err
}

// parse tx logs from events
return backend.TxLogsFromEvents(resBlockResult.TxsResults[res.TxIndex].Events, int(res.MsgIndex))
logs, err := evmtypes.DecodeMsgLogsFromEvents(resBlockResult.TxsResults[res.TxIndex].Data, int(res.MsgIndex), uint64(resBlockResult.Height))
if err != nil {
e.logger.Debug("failed to parse tx logs", "error", err.Error())
return nil, nil
}

return logs, nil
}

// SignTypedData signs EIP-712 conformant typed data
Expand Down
4 changes: 2 additions & 2 deletions rpc/namespaces/ethereum/eth/filters/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ func (api *PublicFilterAPI) Logs(ctx context.Context, crit filters.FilterCriteri
api.logger.Debug("event data type mismatch", "type", fmt.Sprintf("%T", ev.Data))
continue
}
txLogs, err := evmtypes.DecodeTxLogsFromEvents(dataTx.TxResult.Result.Data)
txLogs, err := evmtypes.DecodeTxLogsFromEvents(dataTx.TxResult.Result.Data, uint64(dataTx.TxResult.Height))
if err != nil {
api.logger.Error("fail to decode tx response", "error", err.Error())
return
Expand Down Expand Up @@ -494,7 +494,7 @@ func (api *PublicFilterAPI) NewFilter(criteria filters.FilterCriteria) (rpc.ID,
api.logger.Debug("event data type mismatch", "type", fmt.Sprintf("%T", ev.Data))
continue
}
txLogs, err := evmtypes.DecodeTxLogsFromEvents(dataTx.TxResult.Result.Data)
txLogs, err := evmtypes.DecodeTxLogsFromEvents(dataTx.TxResult.Result.Data, uint64(dataTx.TxResult.Height))
if err != nil {
api.logger.Error("fail to decode tx response", "error", err.Error())
return
Expand Down
3 changes: 0 additions & 3 deletions rpc/types/events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ func TestParseTxResult(t *testing.T) {
{Key: "recipient", Value: "0x775b87ef5D82ca211811C1a02CE0fE0CA3a455d7"},
{Key: "ethereumTxFailed", Value: "contract reverted"},
}},
{Type: evmtypes.EventTypeTxLog, Attributes: []abci.EventAttribute{}},
},
},
[]*ParsedTx{
Expand Down Expand Up @@ -141,7 +140,6 @@ func TestParseTxResult(t *testing.T) {
{Key: "recipient", Value: "0x775b87ef5D82ca211811C1a02CE0fE0CA3a455d7"},
{Key: "ethereumTxFailed", Value: "contract reverted"},
}},
{Type: evmtypes.EventTypeTxLog, Attributes: []abci.EventAttribute{}},
},
},
nil,
Expand All @@ -168,7 +166,6 @@ func TestParseTxResult(t *testing.T) {
{Key: "recipient", Value: "0x775b87ef5D82ca211811C1a02CE0fE0CA3a455d7"},
{Key: "ethereumTxFailed", Value: "contract reverted"},
}},
{Type: evmtypes.EventTypeTxLog, Attributes: []abci.EventAttribute{}},
},
},
nil,
Expand Down
2 changes: 1 addition & 1 deletion rpc/websockets.go
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,7 @@ func (api *pubSubAPI) subscribeLogs(wsConn *wsConn, subID rpc.ID, extra interfac
api.logger.Debug("event data type mismatch", "type", fmt.Sprintf("%T", event.Data))
continue
}
txLogs, err := evmtypes.DecodeTxLogsFromEvents(dataTx.TxResult.Result.Data)
txLogs, err := evmtypes.DecodeTxLogsFromEvents(dataTx.TxResult.Result.Data, uint64(dataTx.TxResult.Height))
if err != nil {
api.logger.Error("failed to decode tx response", "error", err.Error())
return
Expand Down
14 changes: 0 additions & 14 deletions x/evm/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package keeper

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

Expand Down Expand Up @@ -113,25 +112,12 @@ func (k *Keeper) EthereumTx(goCtx context.Context, msg *types.MsgEthereumTx) (*t
attrs = append(attrs, sdk.NewAttribute(types.AttributeKeyEthereumTxFailed, response.VmError))
}

txLogAttrs := make([]sdk.Attribute, len(response.Logs))
for i, log := range response.Logs {
value, err := json.Marshal(log)
if err != nil {
return nil, errorsmod.Wrap(err, "failed to encode log")
}
txLogAttrs[i] = sdk.NewAttribute(types.AttributeKeyTxLog, string(value))
}

// emit events
ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
types.EventTypeEthereumTx,
attrs...,
),
sdk.NewEvent(
types.EventTypeTxLog,
txLogAttrs...,
),
sdk.NewEvent(
sdk.EventTypeMessage,
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
Expand Down
11 changes: 6 additions & 5 deletions x/evm/keeper/state_transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -438,10 +438,11 @@ func (k *Keeper) ApplyMessageWithConfig(ctx sdk.Context,
leftoverGas = msg.Gas() - gasUsed

return &types.MsgEthereumTxResponse{
GasUsed: gasUsed,
VmError: vmError,
Ret: ret,
Logs: types.NewLogsFromEth(stateDB.Logs()),
Hash: txConfig.TxHash.Hex(),
GasUsed: gasUsed,
VmError: vmError,
Ret: ret,
Logs: types.NewLogsFromEth(stateDB.Logs()),
Hash: txConfig.TxHash.Hex(),
BlockHash: ctx.HeaderHash().Bytes(),
}, nil
}
2 changes: 0 additions & 2 deletions x/evm/keeper/statedb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -666,7 +666,6 @@ func (suite *KeeperTestSuite) TestAddLog() {
},
&ethtypes.Log{
Address: addr,
TxHash: txHash,
Topics: make([]common.Hash, 0),
},
func(vm.StateDB) {},
Expand All @@ -680,7 +679,6 @@ func (suite *KeeperTestSuite) TestAddLog() {
},
&ethtypes.Log{
Address: addr,
TxHash: txHash3,
Topics: make([]common.Hash, 0),
},
func(vm.StateDB) {},
Expand Down
2 changes: 0 additions & 2 deletions x/evm/statedb/statedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,6 @@ func (s *StateDB) Keeper() Keeper {
func (s *StateDB) AddLog(log *ethtypes.Log) {
s.journal.append(addLogChange{})

log.TxHash = s.txConfig.TxHash
log.BlockHash = s.txConfig.BlockHash
log.TxIndex = s.txConfig.TxIndex
log.Index = s.txConfig.LogIndex + uint(len(s.logs))
s.logs = append(s.logs, log)
Expand Down
Loading

0 comments on commit 199ccc2

Please sign in to comment.