goja.go 25 KB

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