1717package ethapi
1818
1919import (
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
5965func (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
269281func (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}
0 commit comments