From 9df2fd3e667fcbb12372ccc197bcac032c0de69b Mon Sep 17 00:00:00 2001 From: begmaroman Date: Wed, 4 Oct 2023 18:12:53 +0800 Subject: [PATCH 1/4] Added options to TraceTransaction endpoint call wrapper function --- jsonrpc/debug.go | 52 ++++++++++++++++++++++++++++++++++++++++--- jsonrpc/debug_test.go | 43 ++++++++++++++++++++++++++++++++++- 2 files changed, 91 insertions(+), 4 deletions(-) diff --git a/jsonrpc/debug.go b/jsonrpc/debug.go index 7fa42712..518e689f 100644 --- a/jsonrpc/debug.go +++ b/jsonrpc/debug.go @@ -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{} `json:"tracerConfig,omitempty"` +} + type TransactionTrace struct { Gas uint64 ReturnValue string @@ -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{} { + 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 +} diff --git a/jsonrpc/debug_test.go b/jsonrpc/debug_test.go index fda91c6f..827c332c 100644 --- a/jsonrpc/debug_test.go +++ b/jsonrpc/debug_test.go @@ -22,8 +22,49 @@ 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: "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) + }) + } +} From e9f02e1a614b3007a54485786dc3f7598bf606d1 Mon Sep 17 00:00:00 2001 From: begmaroman Date: Thu, 5 Oct 2023 09:43:40 +0800 Subject: [PATCH 2/4] Added test case --- jsonrpc/debug_test.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/jsonrpc/debug_test.go b/jsonrpc/debug_test.go index 827c332c..f8d8d1e9 100644 --- a/jsonrpc/debug_test.go +++ b/jsonrpc/debug_test.go @@ -34,6 +34,11 @@ func Test_toTraceTransactionOpts(t *testing.T) { opts *TraceTransactionOptions want map[string]interface{} }{ + { + name: "nil options provided", + opts: nil, + want: map[string]interface{}{}, + }, { name: "all fields are provided", opts: &TraceTransactionOptions{ From 4073c1e4a0ebc75b82e28fd77713619588f65a1c Mon Sep 17 00:00:00 2001 From: begmaroman Date: Thu, 5 Oct 2023 09:44:22 +0800 Subject: [PATCH 3/4] Removed struct field flag --- jsonrpc/debug.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jsonrpc/debug.go b/jsonrpc/debug.go index 518e689f..b060faf2 100644 --- a/jsonrpc/debug.go +++ b/jsonrpc/debug.go @@ -18,7 +18,7 @@ type TraceTransactionOptions struct { EnableReturnData bool Timeout string Tracer string - TracerConfig map[string]interface{} `json:"tracerConfig,omitempty"` + TracerConfig map[string]interface{} } type TransactionTrace struct { From 1b3ffd64c26339930cacc6805366b57352a26c27 Mon Sep 17 00:00:00 2001 From: begmaroman Date: Thu, 5 Oct 2023 16:48:54 +0800 Subject: [PATCH 4/4] Minor updates --- jsonrpc/debug.go | 54 ++++++++----------------------------------- jsonrpc/debug_test.go | 48 +------------------------------------- 2 files changed, 10 insertions(+), 92 deletions(-) diff --git a/jsonrpc/debug.go b/jsonrpc/debug.go index b060faf2..851c7030 100644 --- a/jsonrpc/debug.go +++ b/jsonrpc/debug.go @@ -12,13 +12,13 @@ func (c *Client) Debug() *Debug { } type TraceTransactionOptions struct { - EnableMemory bool - DisableStack bool - DisableStorage bool - EnableReturnData bool - Timeout string - Tracer string - TracerConfig map[string]interface{} + EnableMemory bool `json:"enableMemory"` + DisableStack bool `json:"disableStack"` + DisableStorage bool `json:"disableStorage"` + EnableReturnData bool `json:"enableReturnData"` + Timeout string `json:"timeout,omitempty"` + Tracer string `json:"tracer,omitempty"` + TracerConfig map[string]interface{} `json:"tracerConfig,omitempty"` } type TransactionTrace struct { @@ -38,44 +38,8 @@ type StructLogs struct { Storage map[string]string } -func (d *Debug) TraceTransaction(hash ethgo.Hash, opts *TraceTransactionOptions) (*TransactionTrace, error) { +func (d *Debug) TraceTransaction(hash ethgo.Hash, opts TraceTransactionOptions) (*TransactionTrace, error) { var res *TransactionTrace - err := d.c.Call("debug_traceTransaction", &res, hash, toTraceTransactionOpts(opts)) + err := d.c.Call("debug_traceTransaction", &res, hash, opts) return res, err } - -func toTraceTransactionOpts(opts *TraceTransactionOptions) map[string]interface{} { - 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 -} diff --git a/jsonrpc/debug_test.go b/jsonrpc/debug_test.go index f8d8d1e9..e4a36579 100644 --- a/jsonrpc/debug_test.go +++ b/jsonrpc/debug_test.go @@ -22,54 +22,8 @@ func TestDebug_TraceTransaction(t *testing.T) { r, err := s.TxnTo(addr, "setA2") require.NoError(t, err) - trace, err := c.Debug().TraceTransaction(r.TransactionHash, nil) + trace, err := c.Debug().TraceTransaction(r.TransactionHash, TraceTransactionOptions{}) 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) - }) - } -}