github.com/ethw3/go-ethereuma@v0.0.0-20221013053120-c14602a4c23c/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/ethw3/go-ethereuma/common" 29 "github.com/ethw3/go-ethereuma/common/hexutil" 30 "github.com/ethw3/go-ethereuma/core/vm" 31 "github.com/ethw3/go-ethereuma/crypto" 32 "github.com/ethw3/go-ethereuma/eth/tracers" 33 jsassets "github.com/ethw3/go-ethereuma/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("slice", func(slice goja.Value, start, end int) goja.Value { 458 b, err := t.fromBuf(vm, slice, false) 459 if err != nil { 460 vm.Interrupt(err) 461 return nil 462 } 463 if start < 0 || start > end || end > len(b) { 464 vm.Interrupt(fmt.Sprintf("Tracer accessed out of bound memory: available %d, offset %d, size %d", len(b), start, end-start)) 465 return nil 466 } 467 res, err := t.toBuf(vm, b[start:end]) 468 if err != nil { 469 vm.Interrupt(err) 470 return nil 471 } 472 return res 473 }) 474 } 475 476 // setTypeConverters sets up utilities for converting Go types into those 477 // suitable for JS consumption. 478 func (t *jsTracer) setTypeConverters() error { 479 // Inject bigint logic. 480 // TODO: To be replaced after goja adds support for native JS bigint. 481 toBigCode, err := t.vm.RunProgram(bigIntProgram) 482 if err != nil { 483 return err 484 } 485 // Used to create JS bigint objects from go. 486 toBigFn, ok := goja.AssertFunction(toBigCode) 487 if !ok { 488 return errors.New("failed to bind bigInt func") 489 } 490 toBigWrapper := func(vm *goja.Runtime, val string) (goja.Value, error) { 491 return toBigFn(goja.Undefined(), vm.ToValue(val)) 492 } 493 t.toBig = toBigWrapper 494 // NOTE: We need this workaround to create JS buffers because 495 // goja doesn't at the moment expose constructors for typed arrays. 496 // 497 // Cache uint8ArrayType once to be used every time for less overhead. 498 uint8ArrayType := t.vm.Get("Uint8Array") 499 toBufWrapper := func(vm *goja.Runtime, val []byte) (goja.Value, error) { 500 return toBuf(vm, uint8ArrayType, val) 501 } 502 t.toBuf = toBufWrapper 503 fromBufWrapper := func(vm *goja.Runtime, buf goja.Value, allowString bool) ([]byte, error) { 504 return fromBuf(vm, uint8ArrayType, buf, allowString) 505 } 506 t.fromBuf = fromBufWrapper 507 return nil 508 } 509 510 type opObj struct { 511 vm *goja.Runtime 512 op vm.OpCode 513 } 514 515 func (o *opObj) ToNumber() int { 516 return int(o.op) 517 } 518 519 func (o *opObj) ToString() string { 520 return o.op.String() 521 } 522 523 func (o *opObj) IsPush() bool { 524 return o.op.IsPush() 525 } 526 527 func (o *opObj) setupObject() *goja.Object { 528 obj := o.vm.NewObject() 529 obj.Set("toNumber", o.vm.ToValue(o.ToNumber)) 530 obj.Set("toString", o.vm.ToValue(o.ToString)) 531 obj.Set("isPush", o.vm.ToValue(o.IsPush)) 532 return obj 533 } 534 535 type memoryObj struct { 536 memory *vm.Memory 537 vm *goja.Runtime 538 toBig toBigFn 539 toBuf toBufFn 540 } 541 542 func (mo *memoryObj) Slice(begin, end int64) goja.Value { 543 b, err := mo.slice(begin, end) 544 if err != nil { 545 mo.vm.Interrupt(err) 546 return nil 547 } 548 res, err := mo.toBuf(mo.vm, b) 549 if err != nil { 550 mo.vm.Interrupt(err) 551 return nil 552 } 553 return res 554 } 555 556 // slice returns the requested range of memory as a byte slice. 557 func (mo *memoryObj) slice(begin, end int64) ([]byte, error) { 558 if end == begin { 559 return []byte{}, nil 560 } 561 if end < begin || begin < 0 { 562 return nil, fmt.Errorf("tracer accessed out of bound memory: offset %d, end %d", begin, end) 563 } 564 if mo.memory.Len() < int(end) { 565 return nil, fmt.Errorf("tracer accessed out of bound memory: available %d, offset %d, size %d", mo.memory.Len(), begin, end-begin) 566 } 567 return mo.memory.GetCopy(begin, end-begin), nil 568 } 569 570 func (mo *memoryObj) GetUint(addr int64) goja.Value { 571 value, err := mo.getUint(addr) 572 if err != nil { 573 mo.vm.Interrupt(err) 574 return nil 575 } 576 res, err := mo.toBig(mo.vm, value.String()) 577 if err != nil { 578 mo.vm.Interrupt(err) 579 return nil 580 } 581 return res 582 } 583 584 // getUint returns the 32 bytes at the specified address interpreted as a uint. 585 func (mo *memoryObj) getUint(addr int64) (*big.Int, error) { 586 if mo.memory.Len() < int(addr)+32 || addr < 0 { 587 return nil, fmt.Errorf("tracer accessed out of bound memory: available %d, offset %d, size %d", mo.memory.Len(), addr, 32) 588 } 589 return new(big.Int).SetBytes(mo.memory.GetPtr(addr, 32)), nil 590 } 591 592 func (mo *memoryObj) Length() int { 593 return mo.memory.Len() 594 } 595 596 func (m *memoryObj) setupObject() *goja.Object { 597 o := m.vm.NewObject() 598 o.Set("slice", m.vm.ToValue(m.Slice)) 599 o.Set("getUint", m.vm.ToValue(m.GetUint)) 600 o.Set("length", m.vm.ToValue(m.Length)) 601 return o 602 } 603 604 type stackObj struct { 605 stack *vm.Stack 606 vm *goja.Runtime 607 toBig toBigFn 608 } 609 610 func (s *stackObj) Peek(idx int) goja.Value { 611 value, err := s.peek(idx) 612 if err != nil { 613 s.vm.Interrupt(err) 614 return nil 615 } 616 res, err := s.toBig(s.vm, value.String()) 617 if err != nil { 618 s.vm.Interrupt(err) 619 return nil 620 } 621 return res 622 } 623 624 // peek returns the nth-from-the-top element of the stack. 625 func (s *stackObj) peek(idx int) (*big.Int, error) { 626 if len(s.stack.Data()) <= idx || idx < 0 { 627 return nil, fmt.Errorf("tracer accessed out of bound stack: size %d, index %d", len(s.stack.Data()), idx) 628 } 629 return s.stack.Back(idx).ToBig(), nil 630 } 631 632 func (s *stackObj) Length() int { 633 return len(s.stack.Data()) 634 } 635 636 func (s *stackObj) setupObject() *goja.Object { 637 o := s.vm.NewObject() 638 o.Set("peek", s.vm.ToValue(s.Peek)) 639 o.Set("length", s.vm.ToValue(s.Length)) 640 return o 641 } 642 643 type dbObj struct { 644 db vm.StateDB 645 vm *goja.Runtime 646 toBig toBigFn 647 toBuf toBufFn 648 fromBuf fromBufFn 649 } 650 651 func (do *dbObj) GetBalance(addrSlice goja.Value) goja.Value { 652 a, err := do.fromBuf(do.vm, addrSlice, false) 653 if err != nil { 654 do.vm.Interrupt(err) 655 return nil 656 } 657 addr := common.BytesToAddress(a) 658 value := do.db.GetBalance(addr) 659 res, err := do.toBig(do.vm, value.String()) 660 if err != nil { 661 do.vm.Interrupt(err) 662 return nil 663 } 664 return res 665 } 666 667 func (do *dbObj) GetNonce(addrSlice goja.Value) uint64 { 668 a, err := do.fromBuf(do.vm, addrSlice, false) 669 if err != nil { 670 do.vm.Interrupt(err) 671 return 0 672 } 673 addr := common.BytesToAddress(a) 674 return do.db.GetNonce(addr) 675 } 676 677 func (do *dbObj) GetCode(addrSlice goja.Value) goja.Value { 678 a, err := do.fromBuf(do.vm, addrSlice, false) 679 if err != nil { 680 do.vm.Interrupt(err) 681 return nil 682 } 683 addr := common.BytesToAddress(a) 684 code := do.db.GetCode(addr) 685 res, err := do.toBuf(do.vm, code) 686 if err != nil { 687 do.vm.Interrupt(err) 688 return nil 689 } 690 return res 691 } 692 693 func (do *dbObj) GetState(addrSlice goja.Value, hashSlice 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 h, err := do.fromBuf(do.vm, hashSlice, false) 701 if err != nil { 702 do.vm.Interrupt(err) 703 return nil 704 } 705 hash := common.BytesToHash(h) 706 state := do.db.GetState(addr, hash).Bytes() 707 res, err := do.toBuf(do.vm, state) 708 if err != nil { 709 do.vm.Interrupt(err) 710 return nil 711 } 712 return res 713 } 714 715 func (do *dbObj) Exists(addrSlice goja.Value) bool { 716 a, err := do.fromBuf(do.vm, addrSlice, false) 717 if err != nil { 718 do.vm.Interrupt(err) 719 return false 720 } 721 addr := common.BytesToAddress(a) 722 return do.db.Exist(addr) 723 } 724 725 func (do *dbObj) setupObject() *goja.Object { 726 o := do.vm.NewObject() 727 o.Set("getBalance", do.vm.ToValue(do.GetBalance)) 728 o.Set("getNonce", do.vm.ToValue(do.GetNonce)) 729 o.Set("getCode", do.vm.ToValue(do.GetCode)) 730 o.Set("getState", do.vm.ToValue(do.GetState)) 731 o.Set("exists", do.vm.ToValue(do.Exists)) 732 return o 733 } 734 735 type contractObj struct { 736 contract *vm.Contract 737 vm *goja.Runtime 738 toBig toBigFn 739 toBuf toBufFn 740 } 741 742 func (co *contractObj) GetCaller() goja.Value { 743 caller := co.contract.Caller().Bytes() 744 res, err := co.toBuf(co.vm, caller) 745 if err != nil { 746 co.vm.Interrupt(err) 747 return nil 748 } 749 return res 750 } 751 752 func (co *contractObj) GetAddress() goja.Value { 753 addr := co.contract.Address().Bytes() 754 res, err := co.toBuf(co.vm, addr) 755 if err != nil { 756 co.vm.Interrupt(err) 757 return nil 758 } 759 return res 760 } 761 762 func (co *contractObj) GetValue() goja.Value { 763 value := co.contract.Value() 764 res, err := co.toBig(co.vm, value.String()) 765 if err != nil { 766 co.vm.Interrupt(err) 767 return nil 768 } 769 return res 770 } 771 772 func (co *contractObj) GetInput() goja.Value { 773 input := common.CopyBytes(co.contract.Input) 774 res, err := co.toBuf(co.vm, input) 775 if err != nil { 776 co.vm.Interrupt(err) 777 return nil 778 } 779 return res 780 } 781 782 func (c *contractObj) setupObject() *goja.Object { 783 o := c.vm.NewObject() 784 o.Set("getCaller", c.vm.ToValue(c.GetCaller)) 785 o.Set("getAddress", c.vm.ToValue(c.GetAddress)) 786 o.Set("getValue", c.vm.ToValue(c.GetValue)) 787 o.Set("getInput", c.vm.ToValue(c.GetInput)) 788 return o 789 } 790 791 type callframe struct { 792 vm *goja.Runtime 793 toBig toBigFn 794 toBuf toBufFn 795 796 typ string 797 from common.Address 798 to common.Address 799 input []byte 800 gas uint 801 value *big.Int 802 } 803 804 func (f *callframe) GetType() string { 805 return f.typ 806 } 807 808 func (f *callframe) GetFrom() goja.Value { 809 from := f.from.Bytes() 810 res, err := f.toBuf(f.vm, from) 811 if err != nil { 812 f.vm.Interrupt(err) 813 return nil 814 } 815 return res 816 } 817 818 func (f *callframe) GetTo() goja.Value { 819 to := f.to.Bytes() 820 res, err := f.toBuf(f.vm, to) 821 if err != nil { 822 f.vm.Interrupt(err) 823 return nil 824 } 825 return res 826 } 827 828 func (f *callframe) GetInput() goja.Value { 829 input := f.input 830 res, err := f.toBuf(f.vm, input) 831 if err != nil { 832 f.vm.Interrupt(err) 833 return nil 834 } 835 return res 836 } 837 838 func (f *callframe) GetGas() uint { 839 return f.gas 840 } 841 842 func (f *callframe) GetValue() goja.Value { 843 if f.value == nil { 844 return goja.Undefined() 845 } 846 res, err := f.toBig(f.vm, f.value.String()) 847 if err != nil { 848 f.vm.Interrupt(err) 849 return nil 850 } 851 return res 852 } 853 854 func (f *callframe) setupObject() *goja.Object { 855 o := f.vm.NewObject() 856 o.Set("getType", f.vm.ToValue(f.GetType)) 857 o.Set("getFrom", f.vm.ToValue(f.GetFrom)) 858 o.Set("getTo", f.vm.ToValue(f.GetTo)) 859 o.Set("getInput", f.vm.ToValue(f.GetInput)) 860 o.Set("getGas", f.vm.ToValue(f.GetGas)) 861 o.Set("getValue", f.vm.ToValue(f.GetValue)) 862 return o 863 } 864 865 type callframeResult struct { 866 vm *goja.Runtime 867 toBuf toBufFn 868 869 gasUsed uint 870 output []byte 871 err error 872 } 873 874 func (r *callframeResult) GetGasUsed() uint { 875 return r.gasUsed 876 } 877 878 func (r *callframeResult) GetOutput() goja.Value { 879 res, err := r.toBuf(r.vm, r.output) 880 if err != nil { 881 r.vm.Interrupt(err) 882 return nil 883 } 884 return res 885 } 886 887 func (r *callframeResult) GetError() goja.Value { 888 if r.err != nil { 889 return r.vm.ToValue(r.err.Error()) 890 } 891 return goja.Undefined() 892 } 893 894 func (r *callframeResult) setupObject() *goja.Object { 895 o := r.vm.NewObject() 896 o.Set("getGasUsed", r.vm.ToValue(r.GetGasUsed)) 897 o.Set("getOutput", r.vm.ToValue(r.GetOutput)) 898 o.Set("getError", r.vm.ToValue(r.GetError)) 899 return o 900 } 901 902 type steplog struct { 903 vm *goja.Runtime 904 905 op *opObj 906 memory *memoryObj 907 stack *stackObj 908 contract *contractObj 909 910 pc uint 911 gas uint 912 cost uint 913 depth uint 914 refund uint 915 err error 916 } 917 918 func (l *steplog) GetPC() uint { 919 return l.pc 920 } 921 922 func (l *steplog) GetGas() uint { 923 return l.gas 924 } 925 926 func (l *steplog) GetCost() uint { 927 return l.cost 928 } 929 930 func (l *steplog) GetDepth() uint { 931 return l.depth 932 } 933 934 func (l *steplog) GetRefund() uint { 935 return l.refund 936 } 937 938 func (l *steplog) GetError() goja.Value { 939 if l.err != nil { 940 return l.vm.ToValue(l.err.Error()) 941 } 942 return goja.Undefined() 943 } 944 945 func (l *steplog) setupObject() *goja.Object { 946 o := l.vm.NewObject() 947 // Setup basic fields. 948 o.Set("getPC", l.vm.ToValue(l.GetPC)) 949 o.Set("getGas", l.vm.ToValue(l.GetGas)) 950 o.Set("getCost", l.vm.ToValue(l.GetCost)) 951 o.Set("getDepth", l.vm.ToValue(l.GetDepth)) 952 o.Set("getRefund", l.vm.ToValue(l.GetRefund)) 953 o.Set("getError", l.vm.ToValue(l.GetError)) 954 // Setup nested objects. 955 o.Set("op", l.op.setupObject()) 956 o.Set("stack", l.stack.setupObject()) 957 o.Set("memory", l.memory.setupObject()) 958 o.Set("contract", l.contract.setupObject()) 959 return o 960 }