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