Skip to content

Commit a2b2c8a

Browse files
authored
Merge pull request bnb-chain#2941 from Arachnid/tracing
internal/ethapi: Improve tracer error reporting and serialization
2 parents 4ce83bf + d20238c commit a2b2c8a

2 files changed

Lines changed: 26 additions & 11 deletions

File tree

‎internal/ethapi/tracer.go‎

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package ethapi
1818

1919
import (
20+
"encoding/json"
2021
"errors"
2122
"fmt"
2223
"math/big"
@@ -55,6 +56,11 @@ func (ocw *opCodeWrapper) isPush() bool {
5556
return ocw.op.IsPush()
5657
}
5758

59+
// MarshalJSON serializes the opcode as JSON
60+
func (ocw *opCodeWrapper) MarshalJSON() ([]byte, error) {
61+
return json.Marshal(ocw.op.String())
62+
}
63+
5864
// toValue returns an otto.Value for the opCodeWrapper
5965
func (ocw *opCodeWrapper) toValue(vm *otto.Otto) otto.Value {
6066
value, _ := vm.ToValue(ocw)
@@ -165,8 +171,6 @@ type JavascriptTracer struct {
165171
traceobj *otto.Object // User-supplied object to call
166172
log map[string]interface{} // (Reusable) map for the `log` arg to `step`
167173
logvalue otto.Value // JS view of `log`
168-
opcode *opCodeWrapper // Wrapper around the opcode
169-
opcodevalue otto.Value // JS view of 'opcode'
170174
memory *memoryWrapper // Wrapper around the VM memory
171175
memvalue otto.Value // JS view of `memory`
172176
stack *stackWrapper // Wrapper around the VM stack
@@ -213,7 +217,6 @@ func NewJavascriptTracer(code string) (*JavascriptTracer, error) {
213217
logvalue, _ := vm.ToValue(log)
214218

215219
// Create persistent wrappers for memory and stack
216-
opcode := &opCodeWrapper{}
217220
mem := &memoryWrapper{}
218221
stack := &stackWrapper{}
219222
db := &dbWrapper{}
@@ -223,8 +226,6 @@ func NewJavascriptTracer(code string) (*JavascriptTracer, error) {
223226
traceobj: jstracer,
224227
log: log,
225228
logvalue: logvalue,
226-
opcode: opcode,
227-
opcodevalue: opcode.toValue(vm),
228229
memory: mem,
229230
memvalue: mem.toValue(vm),
230231
stack: stack,
@@ -265,16 +266,26 @@ func (jst *JavascriptTracer) callSafely(method string, argumentList ...interface
265266
return ret, err
266267
}
267268

269+
func wrapError(context string, err error) error {
270+
var message string
271+
switch err := err.(type) {
272+
case *otto.Error:
273+
message = err.String()
274+
default:
275+
message = err.Error()
276+
}
277+
return fmt.Errorf("%v in server-side tracer function '%v'", message, context)
278+
}
279+
268280
// CaptureState implements the Tracer interface to trace a single step of VM execution
269281
func (jst *JavascriptTracer) CaptureState(env vm.Environment, pc uint64, op vm.OpCode, gas, cost *big.Int, memory *vm.Memory, stack *vm.Stack, contract *vm.Contract, depth int, err error) {
270282
if jst.err == nil {
271-
jst.opcode.op = op
272283
jst.memory.memory = memory
273284
jst.stack.stack = stack
274285
jst.db.db = env.Db()
275286

276287
jst.log["pc"] = pc
277-
jst.log["op"] = jst.opcodevalue
288+
jst.log["op"] = &opCodeWrapper{op}
278289
jst.log["gas"] = gas.Int64()
279290
jst.log["gasPrice"] = cost.Int64()
280291
jst.log["memory"] = jst.memvalue
@@ -285,7 +296,7 @@ func (jst *JavascriptTracer) CaptureState(env vm.Environment, pc uint64, op vm.O
285296

286297
_, err := jst.callSafely("step", jst.logvalue, jst.dbvalue)
287298
if err != nil {
288-
jst.err = err
299+
jst.err = wrapError("step", err)
289300
}
290301
}
291302
}
@@ -296,5 +307,9 @@ func (jst *JavascriptTracer) GetResult() (result interface{}, err error) {
296307
return nil, jst.err
297308
}
298309

299-
return jst.callSafely("result")
310+
result, err = jst.callSafely("result")
311+
if err != nil {
312+
err = wrapError("result", err)
313+
}
314+
return
300315
}

‎internal/ethapi/tracer_test.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ func TestHalt(t *testing.T) {
161161
tracer.Stop(timeout)
162162
}()
163163

164-
if _, err = runTrace(tracer); err != timeout {
164+
if _, err = runTrace(tracer); err.Error() != "stahp in server-side tracer function 'step'" {
165165
t.Errorf("Expected timeout error, got %v", err)
166166
}
167167
}
@@ -180,7 +180,7 @@ func TestHaltBetweenSteps(t *testing.T) {
180180
tracer.Stop(timeout)
181181
tracer.CaptureState(env, 0, 0, big.NewInt(0), big.NewInt(0), nil, nil, contract, 0, nil)
182182

183-
if _, err := tracer.GetResult(); err != timeout {
183+
if _, err := tracer.GetResult(); err.Error() != "stahp in server-side tracer function 'step'" {
184184
t.Errorf("Expected timeout error, got %v", err)
185185
}
186186
}

0 commit comments

Comments
 (0)