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