Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Debug trace tx changes #2230

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,12 @@ require (
cosmossdk.io/tools/rosetta v0.2.1
github.com/binance-chain/tss-lib v0.0.0-20201118045712-70b2cb4bf916
github.com/btcsuite/btcd/btcutil v1.1.3
github.com/cockroachdb/errors v1.10.0
github.com/cometbft/cometbft v0.37.4
github.com/cometbft/cometbft-db v0.8.0
github.com/huandu/skiplist v1.2.0
github.com/nanmu42/etherscan-api v1.10.0
github.com/onrik/ethrpc v1.2.0
github.com/tendermint/tendermint v0.34.12
go.nhat.io/grpcmock v0.25.0
)

Expand All @@ -77,7 +78,6 @@ require (
github.com/agl/ed25519 v0.0.0-20200225211852-fd4d107ace12 // indirect
github.com/bool64/shared v0.1.5 // indirect
github.com/cespare/xxhash v1.1.0 // indirect
github.com/cockroachdb/errors v1.10.0 // indirect
github.com/cockroachdb/logtags v0.0.0-20230118201751-21c54148d20b // indirect
github.com/cockroachdb/pebble v0.0.0-20220817183557-09c6e030a677 // indirect
github.com/cockroachdb/redact v1.1.5 // indirect
Expand All @@ -98,7 +98,6 @@ require (
github.com/golang/mock v1.6.0 // indirect
github.com/google/pprof v0.0.0-20230602150820-91b7bce49751 // indirect
github.com/google/s2a-go v0.1.7 // indirect
github.com/huandu/skiplist v1.2.0 // indirect
github.com/iancoleman/orderedmap v0.3.0 // indirect
github.com/ipfs/boxo v0.10.0 // indirect
github.com/jmhodges/levigo v1.0.0 // indirect
Expand Down Expand Up @@ -216,7 +215,7 @@ require (
github.com/gtank/ristretto255 v0.1.2 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-getter v1.7.4 // indirect
github.com/hashicorp/go-getter v1.7.4
github.com/hashicorp/go-immutable-radix v1.3.1 // indirect
github.com/hashicorp/go-safetemp v1.0.0 // indirect
github.com/hashicorp/go-version v1.6.0 // indirect
Expand Down Expand Up @@ -321,7 +320,7 @@ require (
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.24.0 // indirect
golang.org/x/crypto v0.17.0
golang.org/x/exp v0.0.0-20230711153332-06a737ee72cb // indirect
golang.org/x/exp v0.0.0-20230711153332-06a737ee72cb
golang.org/x/mod v0.11.0 // indirect
golang.org/x/net v0.19.0
golang.org/x/oauth2 v0.15.0 // indirect
Expand Down Expand Up @@ -354,4 +353,4 @@ replace (

replace github.com/cometbft/cometbft-db => github.com/notional-labs/cometbft-db v0.0.0-20230321185329-6dc7c0ca6345

replace github.com/evmos/ethermint => github.com/zeta-chain/ethermint v0.0.0-20240429123701-35f3f79bf83f
replace github.com/evmos/ethermint => github.com/zeta-chain/ethermint v0.0.0-20240521185249-8bae394152de
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

5 changes: 2 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1639,7 +1639,6 @@ github.com/tendermint/tendermint v0.34.0-rc6/go.mod h1:ugzyZO5foutZImv0Iyx/gOFCX
github.com/tendermint/tendermint v0.34.0/go.mod h1:Aj3PIipBFSNO21r+Lq3TtzQ+uKESxkbA3yo/INM4QwQ=
github.com/tendermint/tendermint v0.34.10/go.mod h1:aeHL7alPh4uTBIJQ8mgFEE8VwJLXI1VD3rVOmH2Mcy0=
github.com/tendermint/tendermint v0.34.11/go.mod h1:aeHL7alPh4uTBIJQ8mgFEE8VwJLXI1VD3rVOmH2Mcy0=
github.com/tendermint/tendermint v0.34.12 h1:m+kUYNhONedhJfHmHG8lqsdZvbR5t6vmhaok1yXjpKg=
github.com/tendermint/tendermint v0.34.12/go.mod h1:aeHL7alPh4uTBIJQ8mgFEE8VwJLXI1VD3rVOmH2Mcy0=
github.com/tendermint/tm-db v0.6.2/go.mod h1:GYtQ67SUvATOcoY8/+x6ylk8Qo02BQyLrAs+yAcLvGI=
github.com/tendermint/tm-db v0.6.3/go.mod h1:lfA1dL9/Y/Y8wwyPp2NMLyn5P5Ptr/gvDFNWtrCWSf8=
Expand Down Expand Up @@ -1733,8 +1732,8 @@ github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9de
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
github.com/zeta-chain/ethermint v0.0.0-20240429123701-35f3f79bf83f h1:joafCsPgohPEn93VCbNXi9IAl6kNvKy8u+kv5amEvUk=
github.com/zeta-chain/ethermint v0.0.0-20240429123701-35f3f79bf83f/go.mod h1:s1zA6OpXv3Tb5I0M6M6j5fo/AssaZL/pgkc7G0W2kN8=
github.com/zeta-chain/ethermint v0.0.0-20240521185249-8bae394152de h1:TvN2pOvj+PxXeFdrBzCbNGSfXU+Bnnl7ZGlBJ3lmgoM=
github.com/zeta-chain/ethermint v0.0.0-20240521185249-8bae394152de/go.mod h1:s1zA6OpXv3Tb5I0M6M6j5fo/AssaZL/pgkc7G0W2kN8=
github.com/zeta-chain/go-tss v0.1.1-0.20240430111318-1785e48eb127 h1:AGQepvsMIVHAHPlplzNcSCyMoGBY1DfO4WHG/QHUSIU=
github.com/zeta-chain/go-tss v0.1.1-0.20240430111318-1785e48eb127/go.mod h1:bVpAoSlRYYCY/R34horVU3cheeHqhSVxygelc++emIU=
github.com/zeta-chain/keystone/keys v0.0.0-20231105174229-903bc9405da2 h1:gd2uE0X+ZbdFJ8DubxNqLbOVlCB12EgWdzSNRAR82tM=
Expand Down
85 changes: 67 additions & 18 deletions rpc/backend/tracing.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@
import (
"encoding/json"
"fmt"
"math/big"

tmrpctypes "github.com/cometbft/cometbft/rpc/core/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
ethtypes "github.com/ethereum/go-ethereum/core/types"
evmtypes "github.com/evmos/ethermint/x/evm/types"
"github.com/pkg/errors"
rpctypes "github.com/zeta-chain/zetacore/rpc/types"
Expand All @@ -49,6 +51,11 @@
return nil, err
}

blockResult, err := b.TendermintBlockResultByNumber(&blk.Block.Height)
if err != nil {
return nil, fmt.Errorf("block result not found for height %d", blk.Block.Height)
}

// check tx index is not out of bound
// #nosec G701 txs number in block is always less than MaxUint32
if uint32(len(blk.Block.Txs)) < transaction.TxIndex {
Expand All @@ -57,18 +64,24 @@
}

var predecessors []*evmtypes.MsgEthereumTx
for _, txBz := range blk.Block.Txs[:transaction.TxIndex] {
for i, txBz := range blk.Block.Txs[:transaction.TxIndex] {
tx, err := b.clientCtx.TxConfig.TxDecoder()(txBz)
if err != nil {
b.logger.Debug("failed to decode transaction in block", "height", blk.Block.Height, "error", err.Error())
continue
}
for _, msg := range tx.GetMsgs() {
ethMsg, ok := msg.(*evmtypes.MsgEthereumTx)
if !ok {

// get parsed eth txs from events from tx result
parsed, err := rpctypes.ParseTxResult(blockResult.TxsResults[i], tx)

Check failure on line 75 in rpc/backend/tracing.go

View workflow job for this annotation

GitHub Actions / lint

Consider pre-allocating `predecessors` (prealloc)

Check failure on line 75 in rpc/backend/tracing.go

View workflow job for this annotation

GitHub Actions / lint

Consider pre-allocating `predecessors` (prealloc)
if err != nil {
return nil, err
}

for _, parsedTx := range parsed.Txs {
ethMsg := getMsgEthereumTxFromParsedTx(parsedTx, tx)
if ethMsg == nil {
continue
}

predecessors = append(predecessors, ethMsg)
}
}
Expand All @@ -79,24 +92,30 @@
return nil, err
}

// add predecessor messages in current cosmos tx
// #nosec G701 always in range
for i := 0; i < int(transaction.MsgIndex); i++ {
ethMsg, ok := tx.GetMsgs()[i].(*evmtypes.MsgEthereumTx)
if !ok {
// get parsed eth txs from events from tx result
parsed, err := rpctypes.ParseTxResult(blockResult.TxsResults[transaction.TxIndex], tx)
if err != nil {
return nil, err
}

// iterate parsed eth txs, if its type 88, recreate it and get MsgEthereumTx, otherwise pull it from sdk.Msgs
// when requested tx is found, break, no more predecessors
var ethMsg *evmtypes.MsgEthereumTx
for _, parsedTx := range parsed.Txs {
ethMsg = getMsgEthereumTxFromParsedTx(parsedTx, tx)
if parsedTx.Hash.Hex() == hash.Hex() {
if ethMsg == nil {
return nil, errors.New("error while parsing tx")
}
break
}
if ethMsg == nil {
continue
}
predecessors = append(predecessors, ethMsg)
}

ethMessage, ok := tx.GetMsgs()[transaction.MsgIndex].(*evmtypes.MsgEthereumTx)
if !ok {
b.logger.Debug("invalid transaction type", "type", fmt.Sprintf("%T", tx))
return nil, fmt.Errorf("invalid transaction type %T", tx)
}

traceTxRequest := evmtypes.QueryTraceTxRequest{
Msg: ethMessage,
Msg: ethMsg,
Predecessors: predecessors,
BlockNumber: blk.Block.Height,
BlockTime: blk.Block.Time,
Expand Down Expand Up @@ -131,7 +150,37 @@
return decodedResult, nil
}

func getMsgEthereumTxFromParsedTx(parsedTx rpctypes.ParsedTx, tx sdk.Tx) *evmtypes.MsgEthereumTx {
if parsedTx.Type == 88 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should precise in comment what does it represent

recipient := parsedTx.Recipient
// TODO: is this ok?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think so, we should explain in comment for signature not set

t := ethtypes.NewTx(&ethtypes.LegacyTx{
Nonce: parsedTx.Nonce,
Data: parsedTx.Data,
Gas: parsedTx.GasUsed,
To: &recipient,
GasPrice: nil,
Value: parsedTx.Amount,
V: big.NewInt(0),
R: big.NewInt(0),
S: big.NewInt(0),
})
ethMsg := &evmtypes.MsgEthereumTx{}
err := ethMsg.FromEthereumTx(t)
if err != nil {
return nil
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We shouldn't return an error here?

}
return ethMsg
} else {
ethMsg, ok := tx.GetMsgs()[parsedTx.MsgIndex].(*evmtypes.MsgEthereumTx)
if !ok {
return nil
}
return ethMsg
}
}

// TraceBlock configures a new tracer according to the provided configuration, and

Check warning on line 183 in rpc/backend/tracing.go

View workflow job for this annotation

GitHub Actions / lint

indent-error-flow: if block ends with a return statement, so drop this else and outdent its block (revive)

Check warning on line 183 in rpc/backend/tracing.go

View workflow job for this annotation

GitHub Actions / lint

indent-error-flow: if block ends with a return statement, so drop this else and outdent its block (revive)
// executes all the transactions contained within. The return value will be one item
// per transaction, dependent on the requested tracer.
func (b *Backend) TraceBlock(height rpctypes.BlockNumber,
Expand Down
30 changes: 30 additions & 0 deletions rpc/types/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@ package types

import (
"fmt"
"math"
"math/big"
"strconv"

abci "github.com/cometbft/cometbft/abci/types"
tmrpctypes "github.com/cometbft/cometbft/rpc/core/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
ethermint "github.com/evmos/ethermint/types"
evmtypes "github.com/evmos/ethermint/x/evm/types"
)
Expand Down Expand Up @@ -68,6 +70,7 @@ const (

// ParsedTx is the tx infos parsed from events.
type ParsedTx struct {
// max uint32 means there is no sdk.Msg corresponding to eth tx
MsgIndex int

// the following fields are parsed from events
Expand All @@ -83,6 +86,8 @@ type ParsedTx struct {
Amount *big.Int
Recipient common.Address
Sender common.Address
Nonce uint64
Data []byte
}

// NewParsedTx initialize a ParsedTx
Expand Down Expand Up @@ -176,6 +181,18 @@ func ParseTxResult(result *abci.ResponseDeliverTx, tx sdk.Tx) (*ParsedTxs, error
p.Txs[i].GasUsed = gasLimit
}
}

// fix msg indexes, because some eth txs indexed here don't have corresponding sdk.Msg
currMsgIndex := 0
for _, tx := range p.Txs {
if tx.Type == 88 {
tx.MsgIndex = math.MaxUint32
// todo: fix mapping as well
} else {
tx.MsgIndex = currMsgIndex
currMsgIndex++
}
}
return p, nil
}

Expand Down Expand Up @@ -371,6 +388,19 @@ func fillTxAttribute(tx *ParsedTx, key []byte, value []byte) error {
if !success {
return nil
}
case "TxNonce":
nonce, err := strconv.ParseUint(string(value), 10, 64)
if err != nil {
return err
}
tx.Nonce = nonce

case "TxData":
hexBytes, err := hexutil.Decode(string(value))
if err != nil {
return err
}
tx.Data = hexBytes
}
return nil
}
Expand Down
3 changes: 2 additions & 1 deletion x/fungible/keeper/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -688,7 +688,6 @@
if gasLimit != nil {
gasCap = gasLimit.Uint64()
}

msg := ethtypes.NewMessage(
from,
contract,
Expand Down Expand Up @@ -756,6 +755,8 @@
}

if !noEthereumTxEvent {
attrs = append(attrs, sdk.NewAttribute("TxData", hexutil.Encode(msg.Data()))) // adding txData for more info in rpc methods
attrs = append(attrs, sdk.NewAttribute("TxNonce", fmt.Sprint(nonce))) // adding nonce for more info in rpc methods

Check warning on line 759 in x/fungible/keeper/evm.go

View check run for this annotation

Codecov / codecov/patch

x/fungible/keeper/evm.go#L758-L759

Added lines #L758 - L759 were not covered by tests
Comment on lines +758 to +759
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comments should be above the line. And I think we can make these more descriptive

ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
evmtypes.EventTypeEthereumTx,
Expand Down
Loading