github.com/theQRL/go-zond@v0.1.1/zond/tracers/js/goja.go (about)

     1  // Copyright 2022 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package js
    18  
    19  import (
    20  	"encoding/json"
    21  	"errors"
    22  	"fmt"
    23  	"math/big"
    24  
    25  	"github.com/dop251/goja"
    26  
    27  	"github.com/theQRL/go-zond/common"
    28  	"github.com/theQRL/go-zond/common/hexutil"
    29  	"github.com/theQRL/go-zond/core/vm"
    30  	"github.com/theQRL/go-zond/crypto"
    31  	"github.com/theQRL/go-zond/zond/tracers"
    32  	jsassets "github.com/theQRL/go-zond/zond/tracers/js/internal/tracers"
    33  )
    34  
    35  var assetTracers = make(map[string]string)
    36  
    37  // init retrieves the JavaScript transaction tracers included in go-ethereum.
    38  func init() {
    39  	var err error
    40  	assetTracers, err = jsassets.Load()
    41  	if err != nil {
    42  		panic(err)
    43  	}
    44  	type ctorFn = func(*tracers.Context, json.RawMessage) (tracers.Tracer, error)
    45  	lookup := func(code string) ctorFn {
    46  		return func(ctx *tracers.Context, cfg json.RawMessage) (tracers.Tracer, error) {
    47  			return newJsTracer(code, ctx, cfg)
    48  		}
    49  	}
    50  	for name, code := range assetTracers {
    51  		tracers.DefaultDirectory.Register(name, lookup(code), true)
    52  	}
    53  	tracers.DefaultDirectory.RegisterJSEval(newJsTracer)
    54  }
    55  
    56  // bigIntProgram is compiled once and the exported function mostly invoked to convert
    57  // hex strings into big ints.
    58  var bigIntProgram = goja.MustCompile("bigInt", bigIntegerJS, false)
    59  
    60  type toBigFn = func(vm *goja.Runtime, val string) (goja.Value, error)
    61  type toBufFn = func(vm *goja.Runtime, val []byte) (goja.Value, error)
    62  type fromBufFn = func(vm *goja.Runtime, buf goja.Value, allowString bool) ([]byte, error)
    63  
    64  func toBuf(vm *goja.Runtime, bufType goja.Value, val []byte) (goja.Value, error) {
    65  	// bufType is usually Uint8Array. This is equivalent to `new Uint8Array(val)` in JS.
    66  	return vm.New(bufType, vm.ToValue(vm.NewArrayBuffer(val)))
    67  }
    68  
    69  func fromBuf(vm *goja.Runtime, bufType goja.Value, buf goja.Value, allowString bool) ([]byte, error) {
    70  	obj := buf.ToObject(vm)
    71  	switch obj.ClassName() {
    72  	case "String":
    73  		if !allowString {
    74  			break
    75  		}
    76  		return common.FromHex(obj.String()), nil
    77  
    78  	case "Array":
    79  		var b []byte
    80  		if err := vm.ExportTo(buf, &b); err != nil {
    81  			return nil, err
    82  		}
    83  		return b, nil
    84  
    85  	case "Object":
    86  		if !obj.Get("constructor").SameAs(bufType) {
    87  			break
    88  		}
    89  		b := obj.Export().([]byte)
    90  		return b, nil
    91  	}
    92  	return nil, errors.New("invalid buffer type")
    93  }
    94  
    95  // jsTracer is an implementation of the Tracer interface which evaluates
    96  // JS functions on the relevant EVM hooks. It uses Goja as its JS engine.
    97  type jsTracer struct {
    98  	vm                *goja.Runtime
    99  	env               *vm.EVM
   100  	toBig             toBigFn               // Converts a hex string into a JS bigint
   101  	toBuf             toBufFn               // Converts a []byte into a JS buffer
   102  	fromBuf           fromBufFn             // Converts an array, hex string or Uint8Array to a []byte
   103  	ctx               map[string]goja.Value // KV-bag passed to JS in `result`
   104  	activePrecompiles []common.Address      // List of active precompiles at current block
   105  	traceStep         bool                  // True if tracer object exposes a `step()` method
   106  	traceFrame        bool                  // True if tracer object exposes the `enter()` and `exit()` methods
   107  	gasLimit          uint64                // Amount of gas bought for the whole tx
   108  	err               error                 // Any error that should stop tracing
   109  	obj               *goja.Object          // Trace object
   110  
   111  	// Methods exposed by tracer
   112  	result goja.Callable
   113  	fault  goja.Callable
   114  	step   goja.Callable
   115  	enter  goja.Callable
   116  	exit   goja.Callable
   117  
   118  	// Underlying structs being passed into JS
   119  	log         *steplog
   120  	frame       *callframe
   121  	frameResult *callframeResult
   122  
   123  	// Goja-wrapping of types prepared for JS consumption
   124  	logValue         goja.Value
   125  	dbValue          goja.Value
   126  	frameValue       goja.Value
   127  	frameResultValue goja.Value
   128  }
   129  
   130  // newJsTracer instantiates a new JS tracer instance. code is a
   131  // Javascript snippet which evaluates to an expression returning
   132  // an object with certain methods:
   133  //
   134  // The methods `result` and `fault` are required to be present.
   135  // The methods `step`, `enter`, and `exit` are optional, but note that
   136  // `enter` and `exit` always go together.
   137  func newJsTracer(code string, ctx *tracers.Context, cfg json.RawMessage) (tracers.Tracer, error) {
   138  	vm := goja.New()
   139  	// By default field names are exported to JS as is, i.e. capitalized.
   140  	vm.SetFieldNameMapper(goja.UncapFieldNameMapper())
   141  	t := &jsTracer{
   142  		vm:  vm,
   143  		ctx: make(map[string]goja.Value),
   144  	}
   145  	if ctx == nil {
   146  		ctx = new(tracers.Context)
   147  	}
   148  	if ctx.BlockHash != (common.Hash{}) {
   149  		t.ctx["blockHash"] = vm.ToValue(ctx.BlockHash.Bytes())
   150  		if ctx.TxHash != (common.Hash{}) {
   151  			t.ctx["txIndex"] = vm.ToValue(ctx.TxIndex)
   152  			t.ctx["txHash"] = vm.ToValue(ctx.TxHash.Bytes())
   153  		}
   154  	}
   155  
   156  	t.setTypeConverters()
   157  	t.setBuiltinFunctions()
   158  	ret, err := vm.RunString("(" + code + ")")
   159  	if err != nil {
   160  		return nil, err
   161  	}
   162  	// Check tracer's interface for required and optional methods.
   163  	obj := ret.ToObject(vm)
   164  	result, ok := goja.AssertFunction(obj.Get("result"))
   165  	if !ok {
   166  		return nil, errors.New("trace object must expose a function result()")
   167  	}
   168  	fault, ok := goja.AssertFunction(obj.Get("fault"))
   169  	if !ok {
   170  		return nil, errors.New("trace object must expose a function fault()")
   171  	}
   172  	step, ok := goja.AssertFunction(obj.Get("step"))
   173  	t.traceStep = ok
   174  	enter, hasEnter := goja.AssertFunction(obj.Get("enter"))
   175  	exit, hasExit := goja.AssertFunction(obj.Get("exit"))
   176  	if hasEnter != hasExit {
   177  		return nil, errors.New("trace object must expose either both or none of enter() and exit()")
   178  	}
   179  	t.traceFrame = hasEnter
   180  	t.obj = obj
   181  	t.step = step
   182  	t.enter = enter
   183  	t.exit = exit
   184  	t.result = result
   185  	t.fault = fault
   186  
   187  	// Pass in config
   188  	if setup, ok := goja.AssertFunction(obj.Get("setup")); ok {
   189  		cfgStr := "{}"
   190  		if cfg != nil {
   191  			cfgStr = string(cfg)
   192  		}
   193  		if _, err := setup(obj, vm.ToValue(cfgStr)); err != nil {
   194  			return nil, err
   195  		}
   196  	}
   197  	// Setup objects carrying data to JS. These are created once and re-used.
   198  	t.log = &steplog{
   199  		vm:       vm,
   200  		op:       &opObj{vm: vm},
   201  		memory:   &memoryObj{vm: vm, toBig: t.toBig, toBuf: t.toBuf},
   202  		stack:    &stackObj{vm: vm, toBig: t.toBig},
   203  		contract: &contractObj{vm: vm, toBig: t.toBig, toBuf: t.toBuf},
   204  	}
   205  	t.frame = &callframe{vm: vm, toBig: t.toBig, toBuf: t.toBuf}
   206  	t.frameResult = &callframeResult{vm: vm, toBuf: t.toBuf}
   207  	t.frameValue = t.frame.setupObject()
   208  	t.frameResultValue = t.frameResult.setupObject()
   209  	t.logValue = t.log.setupObject()
   210  	return t, nil
   211  }
   212  
   213  // CaptureTxStart implements the Tracer interface and is invoked at the beginning of
   214  // transaction processing.
   215  func (t *jsTracer) CaptureTxStart(gasLimit uint64) {
   216  	t.gasLimit = gasLimit
   217  }
   218  
   219  // CaptureTxEnd implements the Tracer interface and is invoked at the end of
   220  // transaction processing.
   221  func (t *jsTracer) CaptureTxEnd(restGas uint64) {
   222  	t.ctx["gasUsed"] = t.vm.ToValue(t.gasLimit - restGas)
   223  }
   224  
   225  // CaptureStart implements the Tracer interface to initialize the tracing operation.
   226  func (t *jsTracer) CaptureStart(env *vm.EVM, from common.Address, to common.Address, create bool, input []byte, gas uint64, value *big.Int) {
   227  	t.env = env
   228  	db := &dbObj{db: env.StateDB, vm: t.vm, toBig: t.toBig, toBuf: t.toBuf, fromBuf: t.fromBuf}
   229  	t.dbValue = db.setupObject()
   230  	if create {
   231  		t.ctx["type"] = t.vm.ToValue("CREATE")
   232  	} else {
   233  		t.ctx["type"] = t.vm.ToValue("CALL")
   234  	}
   235  	t.ctx["from"] = t.vm.ToValue(from.Bytes())
   236  	t.ctx["to"] = t.vm.ToValue(to.Bytes())
   237  	t.ctx["input"] = t.vm.ToValue(input)
   238  	t.ctx["gas"] = t.vm.ToValue(t.gasLimit)
   239  	gasPriceBig, err := t.toBig(t.vm, env.TxContext.GasPrice.String())
   240  	if err != nil {
   241  		t.err = err
   242  		return
   243  	}
   244  	t.ctx["gasPrice"] = gasPriceBig
   245  	valueBig, err := t.toBig(t.vm, value.String())
   246  	if err != nil {
   247  		t.err = err
   248  		return
   249  	}
   250  	t.ctx["value"] = valueBig
   251  	t.ctx["block"] = t.vm.ToValue(env.Context.BlockNumber.Uint64())
   252  	// Update list of precompiles based on current block
   253  	rules := env.ChainConfig().Rules(env.Context.BlockNumber, env.Context.Random != nil, env.Context.Time)
   254  	t.activePrecompiles = vm.ActivePrecompiles(rules)
   255  }
   256  
   257  // CaptureState implements the Tracer interface to trace a single step of VM execution.
   258  func (t *jsTracer) CaptureState(pc uint64, op vm.OpCode, gas, cost uint64, scope *vm.ScopeContext, rData []byte, depth int, err error) {
   259  	if !t.traceStep {
   260  		return
   261  	}
   262  	if t.err != nil {
   263  		return
   264  	}
   265  
   266  	log := t.log
   267  	log.op.op = op
   268  	log.memory.memory = scope.Memory
   269  	log.stack.stack = scope.Stack
   270  	log.contract.contract = scope.Contract
   271  	log.pc = pc
   272  	log.gas = gas
   273  	log.cost = cost
   274  	log.refund = t.env.StateDB.GetRefund()
   275  	log.depth = depth
   276  	log.err = err
   277  	if _, err := t.step(t.obj, t.logValue, t.dbValue); err != nil {
   278  		t.onError("step", err)
   279  	}
   280  }
   281  
   282  // CaptureFault implements the Tracer interface to trace an execution fault
   283  func (t *jsTracer) CaptureFault(pc uint64, op vm.OpCode, gas, cost uint64, scope *vm.ScopeContext, depth int, err error) {
   284  	if t.err != nil {
   285  		return
   286  	}
   287  	// Other log fields have been already set as part of the last CaptureState.
   288  	t.log.err = err
   289  	if _, err := t.fault(t.obj, t.logValue, t.dbValue); err != nil {
   290  		t.onError("fault", err)
   291  	}
   292  }
   293  
   294  // CaptureEnd is called after the call finishes to finalize the tracing.
   295  func (t *jsTracer) CaptureEnd(output []byte, gasUsed uint64, err error) {
   296  	t.ctx["output"] = t.vm.ToValue(output)
   297  	if err != nil {
   298  		t.ctx["error"] = t.vm.ToValue(err.Error())
   299  	}
   300  }
   301  
   302  // CaptureEnter is called when EVM enters a new scope (via call, create or selfdestruct).
   303  func (t *jsTracer) CaptureEnter(typ vm.OpCode, from common.Address, to common.Address, input []byte, gas uint64, value *big.Int) {
   304  	if !t.traceFrame {
   305  		return
   306  	}
   307  	if t.err != nil {
   308  		return
   309  	}
   310  
   311  	t.frame.typ = typ.String()
   312  	t.frame.from = from
   313  	t.frame.to = to
   314  	t.frame.input = common.CopyBytes(input)
   315  	t.frame.gas = uint(gas)
   316  	t.frame.value = nil
   317  	if value != nil {
   318  		t.frame.value = new(big.Int).SetBytes(value.Bytes())
   319  	}
   320  
   321  	if _, err := t.enter(t.obj, t.frameValue); err != nil {
   322  		t.onError("enter", err)
   323  	}
   324  }
   325  
   326  // CaptureExit is called when EVM exits a scope, even if the scope didn't
   327  // execute any code.
   328  func (t *jsTracer) CaptureExit(output []byte, gasUsed uint64, err error) {
   329  	if !t.traceFrame {
   330  		return
   331  	}
   332  
   333  	t.frameResult.gasUsed = uint(gasUsed)
   334  	t.frameResult.output = common.CopyBytes(output)
   335  	t.frameResult.err = err
   336  
   337  	if _, err := t.exit(t.obj, t.frameResultValue); err != nil {
   338  		t.onError("exit", err)
   339  	}
   340  }
   341  
   342  // GetResult calls the Javascript 'result' function and returns its value, or any accumulated error
   343  func (t *jsTracer) GetResult() (json.RawMessage, error) {
   344  	ctx := t.vm.ToValue(t.ctx)
   345  	res, err := t.result(t.obj, ctx, t.dbValue)
   346  	if err != nil {
   347  		return nil, wrapError("result", err)
   348  	}
   349  	encoded, err := json.Marshal(res)
   350  	if err != nil {
   351  		return nil, err
   352  	}
   353  	return json.RawMessage(encoded), t.err
   354  }
   355  
   356  // Stop terminates execution of the tracer at the first opportune moment.
   357  func (t *jsTracer) Stop(err error) {
   358  	t.vm.Interrupt(err)
   359  }
   360  
   361  // onError is called anytime the running JS code is interrupted
   362  // and returns an error. It in turn pings the EVM to cancel its
   363  // execution.
   364  func (t *jsTracer) onError(context string, err error) {
   365  	t.err = wrapError(context, err)
   366  	// `env` is set on CaptureStart which comes before any JS execution.
   367  	// So it should be non-nil.
   368  	t.env.Cancel()
   369  }
   370  
   371  func wrapError(context string, err error) error {
   372  	return fmt.Errorf("%v    in server-side tracer function '%v'", err, context)
   373  }
   374  
   375  // setBuiltinFunctions injects Go functions which are available to tracers into the environment.
   376  // It depends on type converters having been set up.
   377  func (t *jsTracer) setBuiltinFunctions() {
   378  	vm := t.vm
   379  	// TODO: load console from goja-nodejs
   380  	vm.Set("toHex", func(v goja.Value) string {
   381  		b, err := t.fromBuf(vm, v, false)
   382  		if err != nil {
   383  			vm.Interrupt(err)
   384  			return ""
   385  		}
   386  		return hexutil.Encode(b)
   387  	})
   388  	vm.Set("toWord", func(v goja.Value) goja.Value {
   389  		// TODO: add test with []byte len < 32 or > 32
   390  		b, err := t.fromBuf(vm, v, true)
   391  		if err != nil {
   392  			vm.Interrupt(err)
   393  			return nil
   394  		}
   395  		b = common.BytesToHash(b).Bytes()
   396  		res, err := t.toBuf(vm, b)
   397  		if err != nil {
   398  			vm.Interrupt(err)
   399  			return nil
   400  		}
   401  		return res
   402  	})
   403  	vm.Set("toAddress", func(v goja.Value) goja.Value {
   404  		a, err := t.fromBuf(vm, v, true)
   405  		if err != nil {
   406  			vm.Interrupt(err)
   407  			return nil
   408  		}
   409  		a = common.BytesToAddress(a).Bytes()
   410  		res, err := t.toBuf(vm, a)
   411  		if err != nil {
   412  			vm.Interrupt(err)
   413  			return nil
   414  		}
   415  		return res
   416  	})
   417  	vm.Set("toContract", func(from goja.Value, nonce uint) goja.Value {
   418  		a, err := t.fromBuf(vm, from, true)
   419  		if err != nil {
   420  			vm.Interrupt(err)
   421  			return nil
   422  		}
   423  		addr := common.BytesToAddress(a)
   424  		b := crypto.CreateAddress(addr, uint64(nonce)).Bytes()
   425  		res, err := t.toBuf(vm, b)
   426  		if err != nil {
   427  			vm.Interrupt(err)
   428  			return nil
   429  		}
   430  		return res
   431  	})
   432  	vm.Set("toContract2", func(from goja.Value, salt string, initcode goja.Value) goja.Value {
   433  		a, err := t.fromBuf(vm, from, true)
   434  		if err != nil {
   435  			vm.Interrupt(err)
   436  			return nil
   437  		}
   438  		addr := common.BytesToAddress(a)
   439  		code, err := t.fromBuf(vm, initcode, true)
   440  		if err != nil {
   441  			vm.Interrupt(err)
   442  			return nil
   443  		}
   444  		code = common.CopyBytes(code)
   445  		codeHash := crypto.Keccak256(code)
   446  		b := crypto.CreateAddress2(addr, common.HexToHash(salt), codeHash).Bytes()
   447  		res, err := t.toBuf(vm, b)
   448  		if err != nil {
   449  			vm.Interrupt(err)
   450  			return nil
   451  		}
   452  		return res
   453  	})
   454  	vm.Set("isPrecompiled", func(v goja.Value) bool {
   455  		a, err := t.fromBuf(vm, v, true)
   456  		if err != nil {
   457  			vm.Interrupt(err)
   458  			return false
   459  		}
   460  		addr := common.BytesToAddress(a)
   461  		for _, p := range t.activePrecompiles {
   462  			if p == addr {
   463  				return true
   464  			}
   465  		}
   466  		return false
   467  	})
   468  	vm.Set("slice", func(slice goja.Value, start, end int) goja.Value {
   469  		b, err := t.fromBuf(vm, slice, false)
   470  		if err != nil {
   471  			vm.Interrupt(err)
   472  			return nil
   473  		}
   474  		if start < 0 || start > end || end > len(b) {
   475  			vm.Interrupt(fmt.Sprintf("Tracer accessed out of bound memory: available %d, offset %d, size %d", len(b), start, end-start))
   476  			return nil
   477  		}
   478  		res, err := t.toBuf(vm, b[start:end])
   479  		if err != nil {
   480  			vm.Interrupt(err)
   481  			return nil
   482  		}
   483  		return res
   484  	})
   485  }
   486  
   487  // setTypeConverters sets up utilities for converting Go types into those
   488  // suitable for JS consumption.
   489  func (t *jsTracer) setTypeConverters() error {
   490  	// Inject bigint logic.
   491  	// TODO: To be replaced after goja adds support for native JS bigint.
   492  	toBigCode, err := t.vm.RunProgram(bigIntProgram)
   493  	if err != nil {
   494  		return err
   495  	}
   496  	// Used to create JS bigint objects from go.
   497  	toBigFn, ok := goja.AssertFunction(toBigCode)
   498  	if !ok {
   499  		return errors.New("failed to bind bigInt func")
   500  	}
   501  	toBigWrapper := func(vm *goja.Runtime, val string) (goja.Value, error) {
   502  		return toBigFn(goja.Undefined(), vm.ToValue(val))
   503  	}
   504  	t.toBig = toBigWrapper
   505  	// NOTE: We need this workaround to create JS buffers because
   506  	// goja doesn't at the moment expose constructors for typed arrays.
   507  	//
   508  	// Cache uint8ArrayType once to be used every time for less overhead.
   509  	uint8ArrayType := t.vm.Get("Uint8Array")
   510  	toBufWrapper := func(vm *goja.Runtime, val []byte) (goja.Value, error) {
   511  		return toBuf(vm, uint8ArrayType, val)
   512  	}
   513  	t.toBuf = toBufWrapper
   514  	fromBufWrapper := func(vm *goja.Runtime, buf goja.Value, allowString bool) ([]byte, error) {
   515  		return fromBuf(vm, uint8ArrayType, buf, allowString)
   516  	}
   517  	t.fromBuf = fromBufWrapper
   518  	return nil
   519  }
   520  
   521  type opObj struct {
   522  	vm *goja.Runtime
   523  	op vm.OpCode
   524  }
   525  
   526  func (o *opObj) ToNumber() int {
   527  	return int(o.op)
   528  }
   529  
   530  func (o *opObj) ToString() string {
   531  	return o.op.String()
   532  }
   533  
   534  func (o *opObj) IsPush() bool {
   535  	return o.op.IsPush()
   536  }
   537  
   538  func (o *opObj) setupObject() *goja.Object {
   539  	obj := o.vm.NewObject()
   540  	obj.Set("toNumber", o.vm.ToValue(o.ToNumber))
   541  	obj.Set("toString", o.vm.ToValue(o.ToString))
   542  	obj.Set("isPush", o.vm.ToValue(o.IsPush))
   543  	return obj
   544  }
   545  
   546  type memoryObj struct {
   547  	memory *vm.Memory
   548  	vm     *goja.Runtime
   549  	toBig  toBigFn
   550  	toBuf  toBufFn
   551  }
   552  
   553  func (mo *memoryObj) Slice(begin, end int64) goja.Value {
   554  	b, err := mo.slice(begin, end)
   555  	if err != nil {
   556  		mo.vm.Interrupt(err)
   557  		return nil
   558  	}
   559  	res, err := mo.toBuf(mo.vm, b)
   560  	if err != nil {
   561  		mo.vm.Interrupt(err)
   562  		return nil
   563  	}
   564  	return res
   565  }
   566  
   567  // slice returns the requested range of memory as a byte slice.
   568  func (mo *memoryObj) slice(begin, end int64) ([]byte, error) {
   569  	if end == begin {
   570  		return []byte{}, nil
   571  	}
   572  	if end < begin || begin < 0 {
   573  		return nil, fmt.Errorf("tracer accessed out of bound memory: offset %d, end %d", begin, end)
   574  	}
   575  	slice, err := tracers.GetMemoryCopyPadded(mo.memory, begin, end-begin)
   576  	if err != nil {
   577  		return nil, err
   578  	}
   579  	return slice, nil
   580  }
   581  
   582  func (mo *memoryObj) GetUint(addr int64) goja.Value {
   583  	value, err := mo.getUint(addr)
   584  	if err != nil {
   585  		mo.vm.Interrupt(err)
   586  		return nil
   587  	}
   588  	res, err := mo.toBig(mo.vm, value.String())
   589  	if err != nil {
   590  		mo.vm.Interrupt(err)
   591  		return nil
   592  	}
   593  	return res
   594  }
   595  
   596  // getUint returns the 32 bytes at the specified address interpreted as a uint.
   597  func (mo *memoryObj) getUint(addr int64) (*big.Int, error) {
   598  	if mo.memory.Len() < int(addr)+32 || addr < 0 {
   599  		return nil, fmt.Errorf("tracer accessed out of bound memory: available %d, offset %d, size %d", mo.memory.Len(), addr, 32)
   600  	}
   601  	return new(big.Int).SetBytes(mo.memory.GetPtr(addr, 32)), nil
   602  }
   603  
   604  func (mo *memoryObj) Length() int {
   605  	return mo.memory.Len()
   606  }
   607  
   608  func (m *memoryObj) setupObject() *goja.Object {
   609  	o := m.vm.NewObject()
   610  	o.Set("slice", m.vm.ToValue(m.Slice))
   611  	o.Set("getUint", m.vm.ToValue(m.GetUint))
   612  	o.Set("length", m.vm.ToValue(m.Length))
   613  	return o
   614  }
   615  
   616  type stackObj struct {
   617  	stack *vm.Stack
   618  	vm    *goja.Runtime
   619  	toBig toBigFn
   620  }
   621  
   622  func (s *stackObj) Peek(idx int) goja.Value {
   623  	value, err := s.peek(idx)
   624  	if err != nil {
   625  		s.vm.Interrupt(err)
   626  		return nil
   627  	}
   628  	res, err := s.toBig(s.vm, value.String())
   629  	if err != nil {
   630  		s.vm.Interrupt(err)
   631  		return nil
   632  	}
   633  	return res
   634  }
   635  
   636  // peek returns the nth-from-the-top element of the stack.
   637  func (s *stackObj) peek(idx int) (*big.Int, error) {
   638  	if len(s.stack.Data()) <= idx || idx < 0 {
   639  		return nil, fmt.Errorf("tracer accessed out of bound stack: size %d, index %d", len(s.stack.Data()), idx)
   640  	}
   641  	return s.stack.Back(idx).ToBig(), nil
   642  }
   643  
   644  func (s *stackObj) Length() int {
   645  	return len(s.stack.Data())
   646  }
   647  
   648  func (s *stackObj) setupObject() *goja.Object {
   649  	o := s.vm.NewObject()
   650  	o.Set("peek", s.vm.ToValue(s.Peek))
   651  	o.Set("length", s.vm.ToValue(s.Length))
   652  	return o
   653  }
   654  
   655  type dbObj struct {
   656  	db      vm.StateDB
   657  	vm      *goja.Runtime
   658  	toBig   toBigFn
   659  	toBuf   toBufFn
   660  	fromBuf fromBufFn
   661  }
   662  
   663  func (do *dbObj) GetBalance(addrSlice goja.Value) goja.Value {
   664  	a, err := do.fromBuf(do.vm, addrSlice, false)
   665  	if err != nil {
   666  		do.vm.Interrupt(err)
   667  		return nil
   668  	}
   669  	addr := common.BytesToAddress(a)
   670  	value := do.db.GetBalance(addr)
   671  	res, err := do.toBig(do.vm, value.String())
   672  	if err != nil {
   673  		do.vm.Interrupt(err)
   674  		return nil
   675  	}
   676  	return res
   677  }
   678  
   679  func (do *dbObj) GetNonce(addrSlice goja.Value) uint64 {
   680  	a, err := do.fromBuf(do.vm, addrSlice, false)
   681  	if err != nil {
   682  		do.vm.Interrupt(err)
   683  		return 0
   684  	}
   685  	addr := common.BytesToAddress(a)
   686  	return do.db.GetNonce(addr)
   687  }
   688  
   689  func (do *dbObj) GetCode(addrSlice goja.Value) goja.Value {
   690  	a, err := do.fromBuf(do.vm, addrSlice, false)
   691  	if err != nil {
   692  		do.vm.Interrupt(err)
   693  		return nil
   694  	}
   695  	addr := common.BytesToAddress(a)
   696  	code := do.db.GetCode(addr)
   697  	res, err := do.toBuf(do.vm, code)
   698  	if err != nil {
   699  		do.vm.Interrupt(err)
   700  		return nil
   701  	}
   702  	return res
   703  }
   704  
   705  func (do *dbObj) GetState(addrSlice goja.Value, hashSlice goja.Value) goja.Value {
   706  	a, err := do.fromBuf(do.vm, addrSlice, false)
   707  	if err != nil {
   708  		do.vm.Interrupt(err)
   709  		return nil
   710  	}
   711  	addr := common.BytesToAddress(a)
   712  	h, err := do.fromBuf(do.vm, hashSlice, false)
   713  	if err != nil {
   714  		do.vm.Interrupt(err)
   715  		return nil
   716  	}
   717  	hash := common.BytesToHash(h)
   718  	state := do.db.GetState(addr, hash).Bytes()
   719  	res, err := do.toBuf(do.vm, state)
   720  	if err != nil {
   721  		do.vm.Interrupt(err)
   722  		return nil
   723  	}
   724  	return res
   725  }
   726  
   727  func (do *dbObj) Exists(addrSlice goja.Value) bool {
   728  	a, err := do.fromBuf(do.vm, addrSlice, false)
   729  	if err != nil {
   730  		do.vm.Interrupt(err)
   731  		return false
   732  	}
   733  	addr := common.BytesToAddress(a)
   734  	return do.db.Exist(addr)
   735  }
   736  
   737  func (do *dbObj) setupObject() *goja.Object {
   738  	o := do.vm.NewObject()
   739  	o.Set("getBalance", do.vm.ToValue(do.GetBalance))
   740  	o.Set("getNonce", do.vm.ToValue(do.GetNonce))
   741  	o.Set("getCode", do.vm.ToValue(do.GetCode))
   742  	o.Set("getState", do.vm.ToValue(do.GetState))
   743  	o.Set("exists", do.vm.ToValue(do.Exists))
   744  	return o
   745  }
   746  
   747  type contractObj struct {
   748  	contract *vm.Contract
   749  	vm       *goja.Runtime
   750  	toBig    toBigFn
   751  	toBuf    toBufFn
   752  }
   753  
   754  func (co *contractObj) GetCaller() goja.Value {
   755  	caller := co.contract.Caller().Bytes()
   756  	res, err := co.toBuf(co.vm, caller)
   757  	if err != nil {
   758  		co.vm.Interrupt(err)
   759  		return nil
   760  	}
   761  	return res
   762  }
   763  
   764  func (co *contractObj) GetAddress() goja.Value {
   765  	addr := co.contract.Address().Bytes()
   766  	res, err := co.toBuf(co.vm, addr)
   767  	if err != nil {
   768  		co.vm.Interrupt(err)
   769  		return nil
   770  	}
   771  	return res
   772  }
   773  
   774  func (co *contractObj) GetValue() goja.Value {
   775  	value := co.contract.Value()
   776  	res, err := co.toBig(co.vm, value.String())
   777  	if err != nil {
   778  		co.vm.Interrupt(err)
   779  		return nil
   780  	}
   781  	return res
   782  }
   783  
   784  func (co *contractObj) GetInput() goja.Value {
   785  	input := common.CopyBytes(co.contract.Input)
   786  	res, err := co.toBuf(co.vm, input)
   787  	if err != nil {
   788  		co.vm.Interrupt(err)
   789  		return nil
   790  	}
   791  	return res
   792  }
   793  
   794  func (c *contractObj) setupObject() *goja.Object {
   795  	o := c.vm.NewObject()
   796  	o.Set("getCaller", c.vm.ToValue(c.GetCaller))
   797  	o.Set("getAddress", c.vm.ToValue(c.GetAddress))
   798  	o.Set("getValue", c.vm.ToValue(c.GetValue))
   799  	o.Set("getInput", c.vm.ToValue(c.GetInput))
   800  	return o
   801  }
   802  
   803  type callframe struct {
   804  	vm    *goja.Runtime
   805  	toBig toBigFn
   806  	toBuf toBufFn
   807  
   808  	typ   string
   809  	from  common.Address
   810  	to    common.Address
   811  	input []byte
   812  	gas   uint
   813  	value *big.Int
   814  }
   815  
   816  func (f *callframe) GetType() string {
   817  	return f.typ
   818  }
   819  
   820  func (f *callframe) GetFrom() goja.Value {
   821  	from := f.from.Bytes()
   822  	res, err := f.toBuf(f.vm, from)
   823  	if err != nil {
   824  		f.vm.Interrupt(err)
   825  		return nil
   826  	}
   827  	return res
   828  }
   829  
   830  func (f *callframe) GetTo() goja.Value {
   831  	to := f.to.Bytes()
   832  	res, err := f.toBuf(f.vm, to)
   833  	if err != nil {
   834  		f.vm.Interrupt(err)
   835  		return nil
   836  	}
   837  	return res
   838  }
   839  
   840  func (f *callframe) GetInput() goja.Value {
   841  	input := f.input
   842  	res, err := f.toBuf(f.vm, input)
   843  	if err != nil {
   844  		f.vm.Interrupt(err)
   845  		return nil
   846  	}
   847  	return res
   848  }
   849  
   850  func (f *callframe) GetGas() uint {
   851  	return f.gas
   852  }
   853  
   854  func (f *callframe) GetValue() goja.Value {
   855  	if f.value == nil {
   856  		return goja.Undefined()
   857  	}
   858  	res, err := f.toBig(f.vm, f.value.String())
   859  	if err != nil {
   860  		f.vm.Interrupt(err)
   861  		return nil
   862  	}
   863  	return res
   864  }
   865  
   866  func (f *callframe) setupObject() *goja.Object {
   867  	o := f.vm.NewObject()
   868  	o.Set("getType", f.vm.ToValue(f.GetType))
   869  	o.Set("getFrom", f.vm.ToValue(f.GetFrom))
   870  	o.Set("getTo", f.vm.ToValue(f.GetTo))
   871  	o.Set("getInput", f.vm.ToValue(f.GetInput))
   872  	o.Set("getGas", f.vm.ToValue(f.GetGas))
   873  	o.Set("getValue", f.vm.ToValue(f.GetValue))
   874  	return o
   875  }
   876  
   877  type callframeResult struct {
   878  	vm    *goja.Runtime
   879  	toBuf toBufFn
   880  
   881  	gasUsed uint
   882  	output  []byte
   883  	err     error
   884  }
   885  
   886  func (r *callframeResult) GetGasUsed() uint {
   887  	return r.gasUsed
   888  }
   889  
   890  func (r *callframeResult) GetOutput() goja.Value {
   891  	res, err := r.toBuf(r.vm, r.output)
   892  	if err != nil {
   893  		r.vm.Interrupt(err)
   894  		return nil
   895  	}
   896  	return res
   897  }
   898  
   899  func (r *callframeResult) GetError() goja.Value {
   900  	if r.err != nil {
   901  		return r.vm.ToValue(r.err.Error())
   902  	}
   903  	return goja.Undefined()
   904  }
   905  
   906  func (r *callframeResult) setupObject() *goja.Object {
   907  	o := r.vm.NewObject()
   908  	o.Set("getGasUsed", r.vm.ToValue(r.GetGasUsed))
   909  	o.Set("getOutput", r.vm.ToValue(r.GetOutput))
   910  	o.Set("getError", r.vm.ToValue(r.GetError))
   911  	return o
   912  }
   913  
   914  type steplog struct {
   915  	vm *goja.Runtime
   916  
   917  	op       *opObj
   918  	memory   *memoryObj
   919  	stack    *stackObj
   920  	contract *contractObj
   921  
   922  	pc     uint64
   923  	gas    uint64
   924  	cost   uint64
   925  	depth  int
   926  	refund uint64
   927  	err    error
   928  }
   929  
   930  func (l *steplog) GetPC() uint64     { return l.pc }
   931  func (l *steplog) GetGas() uint64    { return l.gas }
   932  func (l *steplog) GetCost() uint64   { return l.cost }
   933  func (l *steplog) GetDepth() int     { return l.depth }
   934  func (l *steplog) GetRefund() uint64 { return l.refund }
   935  
   936  func (l *steplog) GetError() goja.Value {
   937  	if l.err != nil {
   938  		return l.vm.ToValue(l.err.Error())
   939  	}
   940  	return goja.Undefined()
   941  }
   942  
   943  func (l *steplog) setupObject() *goja.Object {
   944  	o := l.vm.NewObject()
   945  	// Setup basic fields.
   946  	o.Set("getPC", l.vm.ToValue(l.GetPC))
   947  	o.Set("getGas", l.vm.ToValue(l.GetGas))
   948  	o.Set("getCost", l.vm.ToValue(l.GetCost))
   949  	o.Set("getDepth", l.vm.ToValue(l.GetDepth))
   950  	o.Set("getRefund", l.vm.ToValue(l.GetRefund))
   951  	o.Set("getError", l.vm.ToValue(l.GetError))
   952  	// Setup nested objects.
   953  	o.Set("op", l.op.setupObject())
   954  	o.Set("stack", l.stack.setupObject())
   955  	o.Set("memory", l.memory.setupObject())
   956  	o.Set("contract", l.contract.setupObject())
   957  	return o
   958  }