Skip to content

Commit

Permalink
fix(zetacore): allow object for tracerConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
gartnera committed Mar 3, 2025
1 parent 760c8bb commit 2472bed
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 12 deletions.
4 changes: 2 additions & 2 deletions rpc/backend/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,10 +165,10 @@ type EVMBackend interface {
BloomStatus() (uint64, uint64)

// Tracing
TraceTransaction(hash common.Hash, config *evmtypes.TraceConfig) (interface{}, error)
TraceTransaction(hash common.Hash, config *rpctypes.TraceConfig) (interface{}, error)
TraceBlock(
height rpctypes.BlockNumber,
config *evmtypes.TraceConfig,
config *rpctypes.TraceConfig,
block *tmrpctypes.ResultBlock,
) ([]*evmtypes.TxTraceResult, error)
}
Expand Down
33 changes: 29 additions & 4 deletions rpc/backend/tracing.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (

// TraceTransaction returns the structured logs created during the execution of EVM
// and returns them as a JSON object.
func (b *Backend) TraceTransaction(hash common.Hash, config *evmtypes.TraceConfig) (interface{}, error) {
func (b *Backend) TraceTransaction(hash common.Hash, config *rpctypes.TraceConfig) (interface{}, error) {
// Get transaction by hash
transaction, _, err := b.GetTxByEthHash(hash)
if err != nil {
Expand Down Expand Up @@ -80,7 +80,7 @@ func (b *Backend) TraceTransaction(hash common.Hash, config *evmtypes.TraceConfi
}

if config != nil {
traceTxRequest.TraceConfig = config
traceTxRequest.TraceConfig = convertConfig(config)
}

// minus one to get the context of block beginning
Expand Down Expand Up @@ -109,7 +109,7 @@ func (b *Backend) TraceTransaction(hash common.Hash, config *evmtypes.TraceConfi
// 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,
config *evmtypes.TraceConfig,
config *rpctypes.TraceConfig,
block *tmrpctypes.ResultBlock,
) ([]*evmtypes.TxTraceResult, error) {
txs := block.Block.Txs
Expand Down Expand Up @@ -137,7 +137,7 @@ func (b *Backend) TraceBlock(height rpctypes.BlockNumber,

traceBlockRequest := &evmtypes.QueryTraceBlockRequest{
Txs: msgs,
TraceConfig: config,
TraceConfig: convertConfig(config),
BlockNumber: block.Block.Height,
BlockTime: block.Block.Time,
BlockHash: common.Bytes2Hex(block.BlockID.Hash),
Expand All @@ -157,3 +157,28 @@ func (b *Backend) TraceBlock(height rpctypes.BlockNumber,

return decodedResults, nil
}

func convertConfig(config *rpctypes.TraceConfig) *evmtypes.TraceConfig {
if config == nil {
return &evmtypes.TraceConfig{}
}

cfg := config.TraceConfig

if config.TracerConfig != nil {
switch v := config.TracerConfig.(type) {
case string:
// It's already a string, use it directly
cfg.TracerJsonConfig = v
default:
// convert anything else to json
// it will be rejected by the ethermint side if it's invalid
jsonBytes, err := json.Marshal(v)
if err == nil {
cfg.TracerJsonConfig = string(jsonBytes)
}
}
}

return &cfg
}
41 changes: 41 additions & 0 deletions rpc/backend/tracing_convert_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package backend

import (
"encoding/json"
"testing"

"github.com/stretchr/testify/require"
rpctypes "github.com/zeta-chain/node/rpc/types"
)

const expectedConvertedValue = `{"onlyTopCall":false}`

const brokenStyleRaw = `
{
"tracer": "callTracer",
"tracerConfig": "{\"onlyTopCall\":false}"
}
`

const compliantStyleRaw = `
{
"tracer": "callTracer",
"tracerConfig": {"onlyTopCall":false}
}
`

func TestConvertConfig(t *testing.T) {
brokenStyle := &rpctypes.TraceConfig{}
err := json.Unmarshal([]byte(brokenStyleRaw), brokenStyle)
require.NoError(t, err)

brokenStyleConverted := convertConfig(brokenStyle)
require.Equal(t, expectedConvertedValue, brokenStyleConverted.TracerJsonConfig)

compliantStyle := &rpctypes.TraceConfig{}
err = json.Unmarshal([]byte(compliantStyleRaw), compliantStyle)
require.NoError(t, err)

compliantStyleConverted := convertConfig(compliantStyle)
require.Equal(t, expectedConvertedValue, compliantStyleConverted.TracerJsonConfig)
}
7 changes: 4 additions & 3 deletions rpc/backend/tracing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/zeta-chain/ethermint/crypto/ethsecp256k1"
"github.com/zeta-chain/ethermint/indexer"
evmtypes "github.com/zeta-chain/ethermint/x/evm/types"
rpctypes "github.com/zeta-chain/node/rpc/types"

"github.com/zeta-chain/node/rpc/backend/mocks"
)
Expand Down Expand Up @@ -272,15 +273,15 @@ func (suite *BackendTestSuite) TestTraceBlock() {
registerMock func()
expTraceResults []*evmtypes.TxTraceResult
resBlock *tmrpctypes.ResultBlock
config *evmtypes.TraceConfig
config *rpctypes.TraceConfig
expPass bool
}{
{
"pass - no transaction returning empty array",
func() {},
[]*evmtypes.TxTraceResult{},
&resBlockEmpty,
&evmtypes.TraceConfig{},
&rpctypes.TraceConfig{},
true,
},
{
Expand All @@ -293,7 +294,7 @@ func (suite *BackendTestSuite) TestTraceBlock() {
},
[]*evmtypes.TxTraceResult{},
&resBlockFilled,
&evmtypes.TraceConfig{},
&rpctypes.TraceConfig{},
false,
},
}
Expand Down
6 changes: 3 additions & 3 deletions rpc/namespaces/ethereum/debug/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func NewAPI(

// TraceTransaction returns the structured logs created during the execution of EVM
// and returns them as a JSON object.
func (a *API) TraceTransaction(hash common.Hash, config *evmtypes.TraceConfig) (interface{}, error) {
func (a *API) TraceTransaction(hash common.Hash, config *rpctypes.TraceConfig) (interface{}, error) {
a.logger.Debug("debug_traceTransaction", "hash", hash)
return a.backend.TraceTransaction(hash, config)
}
Expand All @@ -81,7 +81,7 @@ func (a *API) TraceTransaction(hash common.Hash, config *evmtypes.TraceConfig) (
// EVM and returns them as a JSON object.
func (a *API) TraceBlockByNumber(
height rpctypes.BlockNumber,
config *evmtypes.TraceConfig,
config *rpctypes.TraceConfig,
) ([]*evmtypes.TxTraceResult, error) {
a.logger.Debug("debug_traceBlockByNumber", "height", height)
if height == 0 {
Expand All @@ -99,7 +99,7 @@ func (a *API) TraceBlockByNumber(

// TraceBlockByHash returns the structured logs created during the execution of
// EVM and returns them as a JSON object.
func (a *API) TraceBlockByHash(hash common.Hash, config *evmtypes.TraceConfig) ([]*evmtypes.TxTraceResult, error) {
func (a *API) TraceBlockByHash(hash common.Hash, config *rpctypes.TraceConfig) ([]*evmtypes.TxTraceResult, error) {
a.logger.Debug("debug_traceBlockByHash", "hash", hash)
// Get Tendermint Block
resBlock, err := a.backend.TendermintBlockByHash(hash)
Expand Down
6 changes: 6 additions & 0 deletions rpc/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
ethtypes "github.com/ethereum/go-ethereum/core/types"
evmtypes "github.com/zeta-chain/ethermint/x/evm/types"
)

// Copied the Account and StorageResult types since they are registered under an
Expand Down Expand Up @@ -115,3 +116,8 @@ type OneFeeHistory struct {
Reward []*big.Int // each element of the array will have the tip provided to miners for the percentile given
GasUsedRatio float64 // the ratio of gas used to the gas limit for each block
}

type TraceConfig struct {
evmtypes.TraceConfig
TracerConfig interface{} `json:"tracerConfig"`
}

0 comments on commit 2472bed

Please sign in to comment.