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

Added options to TraceTransaction function #257

Merged
merged 4 commits into from
Oct 6, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
52 changes: 49 additions & 3 deletions jsonrpc/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,21 @@ type Debug struct {
c *Client
}

// Eth returns the reference to the eth namespace
// Debug returns the reference to the debug namespace
func (c *Client) Debug() *Debug {
return c.endpoints.d
}

type TraceTransactionOptions struct {
EnableMemory bool
DisableStack bool
DisableStorage bool
EnableReturnData bool
Timeout string
Tracer string
TracerConfig map[string]interface{}
}

type TransactionTrace struct {
Gas uint64
ReturnValue string
Expand All @@ -28,8 +38,44 @@ type StructLogs struct {
Storage map[string]string
}

func (d *Debug) TraceTransaction(hash ethgo.Hash) (*TransactionTrace, error) {
func (d *Debug) TraceTransaction(hash ethgo.Hash, opts *TraceTransactionOptions) (*TransactionTrace, error) {
var res *TransactionTrace
err := d.c.Call("debug_traceTransaction", &res, hash)
err := d.c.Call("debug_traceTransaction", &res, hash, toTraceTransactionOpts(opts))
return res, err
}

func toTraceTransactionOpts(opts *TraceTransactionOptions) map[string]interface{} {
begmaroman marked this conversation as resolved.
Show resolved Hide resolved
optsMap := make(map[string]interface{})

if opts != nil {
if opts.EnableMemory {
optsMap["enableMemory"] = true
}

if opts.DisableStack {
optsMap["disableStack"] = true
}

if opts.DisableStorage {
optsMap["disableStorage"] = true
}

if opts.EnableReturnData {
optsMap["enableReturnData"] = true
}

if opts.Timeout != "" {
optsMap["timeout"] = opts.Timeout
}

if opts.Tracer != "" {
optsMap["tracer"] = opts.Tracer

if len(opts.TracerConfig) > 0 {
optsMap["tracerConfig"] = opts.TracerConfig
}
}
}

return optsMap
}
48 changes: 47 additions & 1 deletion jsonrpc/debug_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,54 @@ func TestDebug_TraceTransaction(t *testing.T) {
r, err := s.TxnTo(addr, "setA2")
require.NoError(t, err)

trace, err := c.Debug().TraceTransaction(r.TransactionHash)
trace, err := c.Debug().TraceTransaction(r.TransactionHash, nil)
assert.NoError(t, err)
assert.Greater(t, trace.Gas, uint64(20000))
assert.NotEmpty(t, trace.StructLogs)
}

func Test_toTraceTransactionOpts(t *testing.T) {
tests := []struct {
name string
opts *TraceTransactionOptions
want map[string]interface{}
}{
{
name: "nil options provided",
opts: nil,
want: map[string]interface{}{},
},
{
name: "all fields are provided",
opts: &TraceTransactionOptions{
EnableMemory: true,
DisableStack: true,
DisableStorage: true,
EnableReturnData: true,
Timeout: "1s",
Tracer: "callTracer",
TracerConfig: map[string]interface{}{
"onlyTopCall": true,
"withLog": true,
},
},
want: map[string]interface{}{
"disableStack": true,
"disableStorage": true,
"enableMemory": true,
"enableReturnData": true,
"timeout": "1s",
"tracer": "callTracer",
"tracerConfig": map[string]interface{}{
"onlyTopCall": true,
"withLog": true,
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, toTraceTransactionOpts(tt.opts), "toTraceTransactionOpts(%v)", tt.opts)
})
}
}
Loading