Skip to content

Commit

Permalink
Add evm rpc endpoint debug_traceCall (sei-protocol#1692)
Browse files Browse the repository at this point in the history
  • Loading branch information
yzang2019 authored May 22, 2024
1 parent 382b11f commit d12cdfc
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 9 deletions.
29 changes: 20 additions & 9 deletions evmrpc/tracers.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/eth/tracers"
_ "github.com/ethereum/go-ethereum/eth/tracers/native" // run init()s to register native tracers
"github.com/ethereum/go-ethereum/lib/ethapi"
"github.com/ethereum/go-ethereum/rpc"
"github.com/sei-protocol/sei-chain/x/evm/keeper"
rpcclient "github.com/tendermint/tendermint/rpc/client"
Expand All @@ -28,20 +29,30 @@ func NewDebugAPI(tmClient rpcclient.Client, k *keeper.Keeper, ctxProvider func(i
return &DebugAPI{tracersAPI: tracersAPI, tmClient: tmClient, keeper: k, ctxProvider: ctxProvider, txDecoder: txDecoder, connectionType: connectionType}
}

func (api *DebugAPI) TraceTransaction(ctx context.Context, hash common.Hash, config *tracers.TraceConfig) (interface{}, error) {
func (api *DebugAPI) TraceTransaction(ctx context.Context, hash common.Hash, config *tracers.TraceConfig) (result interface{}, returnErr error) {
startTime := time.Now()
defer recordMetrics("debug_traceTransaction", api.connectionType, startTime, true)
return api.tracersAPI.TraceTransaction(ctx, hash, config)
defer recordMetrics("debug_traceTransaction", api.connectionType, startTime, returnErr == nil)
result, returnErr = api.tracersAPI.TraceTransaction(ctx, hash, config)
return
}

func (api *DebugAPI) TraceBlockByNumber(ctx context.Context, number rpc.BlockNumber, config *tracers.TraceConfig) (interface{}, error) {
func (api *DebugAPI) TraceBlockByNumber(ctx context.Context, number rpc.BlockNumber, config *tracers.TraceConfig) (result interface{}, returnErr error) {
startTime := time.Now()
defer recordMetrics("debug_traceBlockByNumber", api.connectionType, startTime, true)
return api.tracersAPI.TraceBlockByNumber(ctx, number, config)
defer recordMetrics("debug_traceBlockByNumber", api.connectionType, startTime, returnErr == nil)
result, returnErr = api.tracersAPI.TraceBlockByNumber(ctx, number, config)
return
}

func (api *DebugAPI) TraceBlockByHash(ctx context.Context, hash common.Hash, config *tracers.TraceConfig) (interface{}, error) {
func (api *DebugAPI) TraceBlockByHash(ctx context.Context, hash common.Hash, config *tracers.TraceConfig) (result interface{}, returnErr error) {
startTime := time.Now()
defer recordMetrics("debug_traceBlockByHash", api.connectionType, startTime, true)
return api.tracersAPI.TraceBlockByHash(ctx, hash, config)
defer recordMetrics("debug_traceBlockByHash", api.connectionType, startTime, returnErr == nil)
result, returnErr = api.tracersAPI.TraceBlockByHash(ctx, hash, config)
return
}

func (api *DebugAPI) TraceCall(ctx context.Context, args ethapi.TransactionArgs, blockNrOrHash rpc.BlockNumberOrHash, config *tracers.TraceCallConfig) (result interface{}, returnErr error) {
startTime := time.Now()
defer recordMetrics("debug_traceCall", api.connectionType, startTime, returnErr == nil)
result, returnErr = api.tracersAPI.TraceCall(ctx, args, blockNrOrHash, config)
return
}
17 changes: 17 additions & 0 deletions evmrpc/tracers_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package evmrpc_test

import (
"fmt"
"testing"

testkeeper "github.com/sei-protocol/sei-chain/testutil/keeper"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -69,3 +71,18 @@ func TestTraceBlockByHash(t *testing.T) {
result = resObj["result"].([]interface{})[0].(map[string]interface{})["result"].(map[string]interface{})
require.Equal(t, 3, len(result))
}

func TestTraceCall(t *testing.T) {
_, from := testkeeper.MockAddressPair()
_, contractAddr := testkeeper.MockAddressPair()
txArgs := map[string]interface{}{
"from": from.Hex(),
"to": contractAddr.Hex(),
"chainId": fmt.Sprintf("%#x", EVMKeeper.ChainID(Ctx)),
}

resObj := sendRequestGoodWithNamespace(t, "debug", "traceCall", txArgs, "0x65")
result := resObj["result"].(map[string]interface{})
require.Equal(t, float64(21000), result["gas"])
require.Equal(t, false, result["failed"])
}

0 comments on commit d12cdfc

Please sign in to comment.