eth/tracers/js: drop duktape engine (#24934)
#23773 added a JS tracer which uses Goja as its engine. In this PR I remove the previous tracer which used duktape as well as remove the dependencies. This PR also comes with 2 fixes in the Goja tracer and one small behavioural change: I had handled errors in the native Go functions by panicing. My oversight was that Goja only handles panics with a Goja.Value as argument. The difference is panic(goja.Value) allows JS to catch the exception whereas Interrupt(error) doesn't. There was a race in how I handled Stop. Because of 1. some of the methods that simply return nil on error (like memory.slice) now throw an exception.
This commit is contained in:
parent
af02e97929
commit
ba47d800b1
@ -134,10 +134,6 @@ func TestCallTracerNative(t *testing.T) {
|
|||||||
testCallTracer("callTracer", "call_tracer", t)
|
testCallTracer("callTracer", "call_tracer", t)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCallTracerLegacyDuktape(t *testing.T) {
|
|
||||||
testCallTracer("callTracerLegacyDuktape", "call_tracer_legacy", t)
|
|
||||||
}
|
|
||||||
|
|
||||||
func testCallTracer(tracerName string, dirPath string, t *testing.T) {
|
func testCallTracer(tracerName string, dirPath string, t *testing.T) {
|
||||||
files, err := os.ReadDir(filepath.Join("testdata", dirPath))
|
files, err := os.ReadDir(filepath.Join("testdata", dirPath))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -30,7 +30,6 @@ import (
|
|||||||
"github.com/ethereum/go-ethereum/crypto"
|
"github.com/ethereum/go-ethereum/crypto"
|
||||||
"github.com/ethereum/go-ethereum/eth/tracers"
|
"github.com/ethereum/go-ethereum/eth/tracers"
|
||||||
jsassets "github.com/ethereum/go-ethereum/eth/tracers/js/internal/tracers"
|
jsassets "github.com/ethereum/go-ethereum/eth/tracers/js/internal/tracers"
|
||||||
"github.com/ethereum/go-ethereum/log"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var assetTracers = make(map[string]string)
|
var assetTracers = make(map[string]string)
|
||||||
@ -42,7 +41,7 @@ func init() {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
tracers.RegisterLookup(true, newGojaTracer)
|
tracers.RegisterLookup(true, newJsTracer)
|
||||||
}
|
}
|
||||||
|
|
||||||
// bigIntProgram is compiled once and the exported function mostly invoked to convert
|
// bigIntProgram is compiled once and the exported function mostly invoked to convert
|
||||||
@ -90,7 +89,9 @@ func fromBuf(vm *goja.Runtime, bufType goja.Value, buf goja.Value, allowString b
|
|||||||
return nil, fmt.Errorf("invalid buffer type")
|
return nil, fmt.Errorf("invalid buffer type")
|
||||||
}
|
}
|
||||||
|
|
||||||
type gojaTracer struct {
|
// jsTracer is an implementation of the Tracer interface which evaluates
|
||||||
|
// JS functions on the relevant EVM hooks. It uses Goja as its JS engine.
|
||||||
|
type jsTracer struct {
|
||||||
vm *goja.Runtime
|
vm *goja.Runtime
|
||||||
env *vm.EVM
|
env *vm.EVM
|
||||||
toBig toBigFn // Converts a hex string into a JS bigint
|
toBig toBigFn // Converts a hex string into a JS bigint
|
||||||
@ -123,14 +124,20 @@ type gojaTracer struct {
|
|||||||
frameResultValue goja.Value
|
frameResultValue goja.Value
|
||||||
}
|
}
|
||||||
|
|
||||||
func newGojaTracer(code string, ctx *tracers.Context) (tracers.Tracer, error) {
|
// newJsTracer instantiates a new JS tracer instance. code is either
|
||||||
|
// the name of a built-in JS tracer or a Javascript snippet which
|
||||||
|
// evaluates to an expression returning an object with certain methods.
|
||||||
|
// The methods `result` and `fault` are required to be present.
|
||||||
|
// The methods `step`, `enter`, and `exit` are optional, but note that
|
||||||
|
// `enter` and `exit` always go together.
|
||||||
|
func newJsTracer(code string, ctx *tracers.Context) (tracers.Tracer, error) {
|
||||||
if c, ok := assetTracers[code]; ok {
|
if c, ok := assetTracers[code]; ok {
|
||||||
code = c
|
code = c
|
||||||
}
|
}
|
||||||
vm := goja.New()
|
vm := goja.New()
|
||||||
// By default field names are exported to JS as is, i.e. capitalized.
|
// By default field names are exported to JS as is, i.e. capitalized.
|
||||||
vm.SetFieldNameMapper(goja.UncapFieldNameMapper())
|
vm.SetFieldNameMapper(goja.UncapFieldNameMapper())
|
||||||
t := &gojaTracer{
|
t := &jsTracer{
|
||||||
vm: vm,
|
vm: vm,
|
||||||
ctx: make(map[string]goja.Value),
|
ctx: make(map[string]goja.Value),
|
||||||
}
|
}
|
||||||
@ -179,8 +186,8 @@ func newGojaTracer(code string, ctx *tracers.Context) (tracers.Tracer, error) {
|
|||||||
t.log = &steplog{
|
t.log = &steplog{
|
||||||
vm: vm,
|
vm: vm,
|
||||||
op: &opObj{vm: vm},
|
op: &opObj{vm: vm},
|
||||||
memory: &memoryObj{w: new(memoryWrapper), vm: vm, toBig: t.toBig, toBuf: t.toBuf},
|
memory: &memoryObj{vm: vm, toBig: t.toBig, toBuf: t.toBuf},
|
||||||
stack: &stackObj{w: new(stackWrapper), vm: vm, toBig: t.toBig},
|
stack: &stackObj{vm: vm, toBig: t.toBig},
|
||||||
contract: &contractObj{vm: vm, toBig: t.toBig, toBuf: t.toBuf},
|
contract: &contractObj{vm: vm, toBig: t.toBig, toBuf: t.toBuf},
|
||||||
}
|
}
|
||||||
t.frame = &callframe{vm: vm, toBig: t.toBig, toBuf: t.toBuf}
|
t.frame = &callframe{vm: vm, toBig: t.toBig, toBuf: t.toBuf}
|
||||||
@ -193,16 +200,16 @@ func newGojaTracer(code string, ctx *tracers.Context) (tracers.Tracer, error) {
|
|||||||
|
|
||||||
// CaptureTxStart implements the Tracer interface and is invoked at the beginning of
|
// CaptureTxStart implements the Tracer interface and is invoked at the beginning of
|
||||||
// transaction processing.
|
// transaction processing.
|
||||||
func (t *gojaTracer) CaptureTxStart(gasLimit uint64) {
|
func (t *jsTracer) CaptureTxStart(gasLimit uint64) {
|
||||||
t.gasLimit = gasLimit
|
t.gasLimit = gasLimit
|
||||||
}
|
}
|
||||||
|
|
||||||
// CaptureTxStart implements the Tracer interface and is invoked at the end of
|
// CaptureTxStart implements the Tracer interface and is invoked at the end of
|
||||||
// transaction processing.
|
// transaction processing.
|
||||||
func (t *gojaTracer) CaptureTxEnd(restGas uint64) {}
|
func (t *jsTracer) CaptureTxEnd(restGas uint64) {}
|
||||||
|
|
||||||
// CaptureStart implements the Tracer interface to initialize the tracing operation.
|
// CaptureStart implements the Tracer interface to initialize the tracing operation.
|
||||||
func (t *gojaTracer) CaptureStart(env *vm.EVM, from common.Address, to common.Address, create bool, input []byte, gas uint64, value *big.Int) {
|
func (t *jsTracer) CaptureStart(env *vm.EVM, from common.Address, to common.Address, create bool, input []byte, gas uint64, value *big.Int) {
|
||||||
t.env = env
|
t.env = env
|
||||||
db := &dbObj{db: env.StateDB, vm: t.vm, toBig: t.toBig, toBuf: t.toBuf, fromBuf: t.fromBuf}
|
db := &dbObj{db: env.StateDB, vm: t.vm, toBig: t.toBig, toBuf: t.toBuf, fromBuf: t.fromBuf}
|
||||||
t.dbValue = db.setupObject()
|
t.dbValue = db.setupObject()
|
||||||
@ -230,7 +237,7 @@ func (t *gojaTracer) CaptureStart(env *vm.EVM, from common.Address, to common.Ad
|
|||||||
}
|
}
|
||||||
|
|
||||||
// CaptureState implements the Tracer interface to trace a single step of VM execution.
|
// CaptureState implements the Tracer interface to trace a single step of VM execution.
|
||||||
func (t *gojaTracer) CaptureState(pc uint64, op vm.OpCode, gas, cost uint64, scope *vm.ScopeContext, rData []byte, depth int, err error) {
|
func (t *jsTracer) CaptureState(pc uint64, op vm.OpCode, gas, cost uint64, scope *vm.ScopeContext, rData []byte, depth int, err error) {
|
||||||
if !t.traceStep {
|
if !t.traceStep {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -240,8 +247,8 @@ func (t *gojaTracer) CaptureState(pc uint64, op vm.OpCode, gas, cost uint64, sco
|
|||||||
|
|
||||||
log := t.log
|
log := t.log
|
||||||
log.op.op = op
|
log.op.op = op
|
||||||
log.memory.w.memory = scope.Memory
|
log.memory.memory = scope.Memory
|
||||||
log.stack.w.stack = scope.Stack
|
log.stack.stack = scope.Stack
|
||||||
log.contract.contract = scope.Contract
|
log.contract.contract = scope.Contract
|
||||||
log.pc = uint(pc)
|
log.pc = uint(pc)
|
||||||
log.gas = uint(gas)
|
log.gas = uint(gas)
|
||||||
@ -249,24 +256,24 @@ func (t *gojaTracer) CaptureState(pc uint64, op vm.OpCode, gas, cost uint64, sco
|
|||||||
log.depth = uint(depth)
|
log.depth = uint(depth)
|
||||||
log.err = err
|
log.err = err
|
||||||
if _, err := t.step(t.obj, t.logValue, t.dbValue); err != nil {
|
if _, err := t.step(t.obj, t.logValue, t.dbValue); err != nil {
|
||||||
t.err = wrapError("step", err)
|
t.onError("step", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// CaptureFault implements the Tracer interface to trace an execution fault
|
// CaptureFault implements the Tracer interface to trace an execution fault
|
||||||
func (t *gojaTracer) CaptureFault(pc uint64, op vm.OpCode, gas, cost uint64, scope *vm.ScopeContext, depth int, err error) {
|
func (t *jsTracer) CaptureFault(pc uint64, op vm.OpCode, gas, cost uint64, scope *vm.ScopeContext, depth int, err error) {
|
||||||
if t.err != nil {
|
if t.err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
// Other log fields have been already set as part of the last CaptureState.
|
// Other log fields have been already set as part of the last CaptureState.
|
||||||
t.log.err = err
|
t.log.err = err
|
||||||
if _, err := t.fault(t.obj, t.logValue, t.dbValue); err != nil {
|
if _, err := t.fault(t.obj, t.logValue, t.dbValue); err != nil {
|
||||||
t.err = wrapError("fault", err)
|
t.onError("fault", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// CaptureEnd is called after the call finishes to finalize the tracing.
|
// CaptureEnd is called after the call finishes to finalize the tracing.
|
||||||
func (t *gojaTracer) CaptureEnd(output []byte, gasUsed uint64, duration time.Duration, err error) {
|
func (t *jsTracer) CaptureEnd(output []byte, gasUsed uint64, duration time.Duration, err error) {
|
||||||
t.ctx["output"] = t.vm.ToValue(output)
|
t.ctx["output"] = t.vm.ToValue(output)
|
||||||
t.ctx["time"] = t.vm.ToValue(duration.String())
|
t.ctx["time"] = t.vm.ToValue(duration.String())
|
||||||
t.ctx["gasUsed"] = t.vm.ToValue(gasUsed)
|
t.ctx["gasUsed"] = t.vm.ToValue(gasUsed)
|
||||||
@ -276,7 +283,7 @@ func (t *gojaTracer) CaptureEnd(output []byte, gasUsed uint64, duration time.Dur
|
|||||||
}
|
}
|
||||||
|
|
||||||
// CaptureEnter is called when EVM enters a new scope (via call, create or selfdestruct).
|
// CaptureEnter is called when EVM enters a new scope (via call, create or selfdestruct).
|
||||||
func (t *gojaTracer) CaptureEnter(typ vm.OpCode, from common.Address, to common.Address, input []byte, gas uint64, value *big.Int) {
|
func (t *jsTracer) CaptureEnter(typ vm.OpCode, from common.Address, to common.Address, input []byte, gas uint64, value *big.Int) {
|
||||||
if !t.traceFrame {
|
if !t.traceFrame {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -295,13 +302,13 @@ func (t *gojaTracer) CaptureEnter(typ vm.OpCode, from common.Address, to common.
|
|||||||
}
|
}
|
||||||
|
|
||||||
if _, err := t.enter(t.obj, t.frameValue); err != nil {
|
if _, err := t.enter(t.obj, t.frameValue); err != nil {
|
||||||
t.err = wrapError("enter", err)
|
t.onError("enter", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// CaptureExit is called when EVM exits a scope, even if the scope didn't
|
// CaptureExit is called when EVM exits a scope, even if the scope didn't
|
||||||
// execute any code.
|
// execute any code.
|
||||||
func (t *gojaTracer) CaptureExit(output []byte, gasUsed uint64, err error) {
|
func (t *jsTracer) CaptureExit(output []byte, gasUsed uint64, err error) {
|
||||||
if !t.traceFrame {
|
if !t.traceFrame {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -311,12 +318,12 @@ func (t *gojaTracer) CaptureExit(output []byte, gasUsed uint64, err error) {
|
|||||||
t.frameResult.err = err
|
t.frameResult.err = err
|
||||||
|
|
||||||
if _, err := t.exit(t.obj, t.frameResultValue); err != nil {
|
if _, err := t.exit(t.obj, t.frameResultValue); err != nil {
|
||||||
t.err = wrapError("exit", err)
|
t.onError("exit", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetResult calls the Javascript 'result' function and returns its value, or any accumulated error
|
// GetResult calls the Javascript 'result' function and returns its value, or any accumulated error
|
||||||
func (t *gojaTracer) GetResult() (json.RawMessage, error) {
|
func (t *jsTracer) GetResult() (json.RawMessage, error) {
|
||||||
ctx := t.vm.ToValue(t.ctx)
|
ctx := t.vm.ToValue(t.ctx)
|
||||||
res, err := t.result(t.obj, ctx, t.dbValue)
|
res, err := t.result(t.obj, ctx, t.dbValue)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -330,20 +337,34 @@ func (t *gojaTracer) GetResult() (json.RawMessage, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Stop terminates execution of the tracer at the first opportune moment.
|
// Stop terminates execution of the tracer at the first opportune moment.
|
||||||
func (t *gojaTracer) Stop(err error) {
|
func (t *jsTracer) Stop(err error) {
|
||||||
t.vm.Interrupt(err)
|
t.vm.Interrupt(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// onError is called anytime the running JS code is interrupted
|
||||||
|
// and returns an error. It in turn pings the EVM to cancel its
|
||||||
|
// execution.
|
||||||
|
func (t *jsTracer) onError(context string, err error) {
|
||||||
|
t.err = wrapError(context, err)
|
||||||
|
// `env` is set on CaptureStart which comes before any JS execution.
|
||||||
|
// So it should be non-nil.
|
||||||
t.env.Cancel()
|
t.env.Cancel()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func wrapError(context string, err error) error {
|
||||||
|
return fmt.Errorf("%v in server-side tracer function '%v'", err, context)
|
||||||
|
}
|
||||||
|
|
||||||
// setBuiltinFunctions injects Go functions which are available to tracers into the environment.
|
// setBuiltinFunctions injects Go functions which are available to tracers into the environment.
|
||||||
// It depends on type converters having been set up.
|
// It depends on type converters having been set up.
|
||||||
func (t *gojaTracer) setBuiltinFunctions() {
|
func (t *jsTracer) setBuiltinFunctions() {
|
||||||
vm := t.vm
|
vm := t.vm
|
||||||
// TODO: load console from goja-nodejs
|
// TODO: load console from goja-nodejs
|
||||||
vm.Set("toHex", func(v goja.Value) string {
|
vm.Set("toHex", func(v goja.Value) string {
|
||||||
b, err := t.fromBuf(vm, v, false)
|
b, err := t.fromBuf(vm, v, false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
vm.Interrupt(err)
|
||||||
|
return ""
|
||||||
}
|
}
|
||||||
return hexutil.Encode(b)
|
return hexutil.Encode(b)
|
||||||
})
|
})
|
||||||
@ -351,63 +372,73 @@ func (t *gojaTracer) setBuiltinFunctions() {
|
|||||||
// TODO: add test with []byte len < 32 or > 32
|
// TODO: add test with []byte len < 32 or > 32
|
||||||
b, err := t.fromBuf(vm, v, true)
|
b, err := t.fromBuf(vm, v, true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
vm.Interrupt(err)
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
b = common.BytesToHash(b).Bytes()
|
b = common.BytesToHash(b).Bytes()
|
||||||
res, err := t.toBuf(vm, b)
|
res, err := t.toBuf(vm, b)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
vm.Interrupt(err)
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
return res
|
return res
|
||||||
})
|
})
|
||||||
vm.Set("toAddress", func(v goja.Value) goja.Value {
|
vm.Set("toAddress", func(v goja.Value) goja.Value {
|
||||||
a, err := t.fromBuf(vm, v, true)
|
a, err := t.fromBuf(vm, v, true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
vm.Interrupt(err)
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
a = common.BytesToAddress(a).Bytes()
|
a = common.BytesToAddress(a).Bytes()
|
||||||
res, err := t.toBuf(vm, a)
|
res, err := t.toBuf(vm, a)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
vm.Interrupt(err)
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
return res
|
return res
|
||||||
})
|
})
|
||||||
vm.Set("toContract", func(from goja.Value, nonce uint) goja.Value {
|
vm.Set("toContract", func(from goja.Value, nonce uint) goja.Value {
|
||||||
a, err := t.fromBuf(vm, from, true)
|
a, err := t.fromBuf(vm, from, true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
vm.Interrupt(err)
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
addr := common.BytesToAddress(a)
|
addr := common.BytesToAddress(a)
|
||||||
b := crypto.CreateAddress(addr, uint64(nonce)).Bytes()
|
b := crypto.CreateAddress(addr, uint64(nonce)).Bytes()
|
||||||
res, err := t.toBuf(vm, b)
|
res, err := t.toBuf(vm, b)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
vm.Interrupt(err)
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
return res
|
return res
|
||||||
})
|
})
|
||||||
vm.Set("toContract2", func(from goja.Value, salt string, initcode goja.Value) goja.Value {
|
vm.Set("toContract2", func(from goja.Value, salt string, initcode goja.Value) goja.Value {
|
||||||
a, err := t.fromBuf(vm, from, true)
|
a, err := t.fromBuf(vm, from, true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
vm.Interrupt(err)
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
addr := common.BytesToAddress(a)
|
addr := common.BytesToAddress(a)
|
||||||
code, err := t.fromBuf(vm, initcode, true)
|
code, err := t.fromBuf(vm, initcode, true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
vm.Interrupt(err)
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
code = common.CopyBytes(code)
|
code = common.CopyBytes(code)
|
||||||
codeHash := crypto.Keccak256(code)
|
codeHash := crypto.Keccak256(code)
|
||||||
b := crypto.CreateAddress2(addr, common.HexToHash(salt), codeHash).Bytes()
|
b := crypto.CreateAddress2(addr, common.HexToHash(salt), codeHash).Bytes()
|
||||||
res, err := t.toBuf(vm, b)
|
res, err := t.toBuf(vm, b)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
vm.Interrupt(err)
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
return res
|
return res
|
||||||
})
|
})
|
||||||
vm.Set("isPrecompiled", func(v goja.Value) bool {
|
vm.Set("isPrecompiled", func(v goja.Value) bool {
|
||||||
a, err := t.fromBuf(vm, v, true)
|
a, err := t.fromBuf(vm, v, true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
vm.Interrupt(err)
|
||||||
|
return false
|
||||||
}
|
}
|
||||||
addr := common.BytesToAddress(a)
|
addr := common.BytesToAddress(a)
|
||||||
for _, p := range t.activePrecompiles {
|
for _, p := range t.activePrecompiles {
|
||||||
@ -420,14 +451,17 @@ func (t *gojaTracer) setBuiltinFunctions() {
|
|||||||
vm.Set("slice", func(slice goja.Value, start, end int) goja.Value {
|
vm.Set("slice", func(slice goja.Value, start, end int) goja.Value {
|
||||||
b, err := t.fromBuf(vm, slice, false)
|
b, err := t.fromBuf(vm, slice, false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
vm.Interrupt(err)
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
if start < 0 || start > end || end > len(b) {
|
if start < 0 || start > end || end > len(b) {
|
||||||
log.Warn("Tracer accessed out of bound memory", "available", len(b), "offset", start, "size", end-start)
|
vm.Interrupt(fmt.Sprintf("Tracer accessed out of bound memory: available %d, offset %d, size %d", len(b), start, end-start))
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
res, err := t.toBuf(vm, b[start:end])
|
res, err := t.toBuf(vm, b[start:end])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
vm.Interrupt(err)
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
return res
|
return res
|
||||||
})
|
})
|
||||||
@ -435,7 +469,7 @@ func (t *gojaTracer) setBuiltinFunctions() {
|
|||||||
|
|
||||||
// setTypeConverters sets up utilities for converting Go types into those
|
// setTypeConverters sets up utilities for converting Go types into those
|
||||||
// suitable for JS consumption.
|
// suitable for JS consumption.
|
||||||
func (t *gojaTracer) setTypeConverters() error {
|
func (t *jsTracer) setTypeConverters() error {
|
||||||
// Inject bigint logic.
|
// Inject bigint logic.
|
||||||
// TODO: To be replaced after goja adds support for native JS bigint.
|
// TODO: To be replaced after goja adds support for native JS bigint.
|
||||||
toBigCode, err := t.vm.RunProgram(bigIntProgram)
|
toBigCode, err := t.vm.RunProgram(bigIntProgram)
|
||||||
@ -493,32 +527,64 @@ func (o *opObj) setupObject() *goja.Object {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type memoryObj struct {
|
type memoryObj struct {
|
||||||
w *memoryWrapper
|
memory *vm.Memory
|
||||||
vm *goja.Runtime
|
vm *goja.Runtime
|
||||||
toBig toBigFn
|
toBig toBigFn
|
||||||
toBuf toBufFn
|
toBuf toBufFn
|
||||||
}
|
}
|
||||||
|
|
||||||
func (mo *memoryObj) Slice(begin, end int64) goja.Value {
|
func (mo *memoryObj) Slice(begin, end int64) goja.Value {
|
||||||
b := mo.w.slice(begin, end)
|
b, err := mo.slice(begin, end)
|
||||||
|
if err != nil {
|
||||||
|
mo.vm.Interrupt(err)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
res, err := mo.toBuf(mo.vm, b)
|
res, err := mo.toBuf(mo.vm, b)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
mo.vm.Interrupt(err)
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// slice returns the requested range of memory as a byte slice.
|
||||||
|
func (mo *memoryObj) slice(begin, end int64) ([]byte, error) {
|
||||||
|
if end == begin {
|
||||||
|
return []byte{}, nil
|
||||||
|
}
|
||||||
|
if end < begin || begin < 0 {
|
||||||
|
return nil, fmt.Errorf("Tracer accessed out of bound memory: offset %d, end %d", begin, end)
|
||||||
|
}
|
||||||
|
if mo.memory.Len() < int(end) {
|
||||||
|
return nil, fmt.Errorf("Tracer accessed out of bound memory: available %d, offset %d, size %d", mo.memory.Len(), begin, end-begin)
|
||||||
|
}
|
||||||
|
return mo.memory.GetCopy(begin, end-begin), nil
|
||||||
|
}
|
||||||
|
|
||||||
func (mo *memoryObj) GetUint(addr int64) goja.Value {
|
func (mo *memoryObj) GetUint(addr int64) goja.Value {
|
||||||
value := mo.w.getUint(addr)
|
value, err := mo.getUint(addr)
|
||||||
|
if err != nil {
|
||||||
|
mo.vm.Interrupt(err)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
res, err := mo.toBig(mo.vm, value.String())
|
res, err := mo.toBig(mo.vm, value.String())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
mo.vm.Interrupt(err)
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// getUint returns the 32 bytes at the specified address interpreted as a uint.
|
||||||
|
func (mo *memoryObj) getUint(addr int64) (*big.Int, error) {
|
||||||
|
if mo.memory.Len() < int(addr)+32 || addr < 0 {
|
||||||
|
return nil, fmt.Errorf("Tracer accessed out of bound memory: available %d, offset %d, size %d", mo.memory.Len(), addr, 32)
|
||||||
|
}
|
||||||
|
return new(big.Int).SetBytes(mo.memory.GetPtr(addr, 32)), nil
|
||||||
|
}
|
||||||
|
|
||||||
func (mo *memoryObj) Length() int {
|
func (mo *memoryObj) Length() int {
|
||||||
return mo.w.memory.Len()
|
return mo.memory.Len()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *memoryObj) setupObject() *goja.Object {
|
func (m *memoryObj) setupObject() *goja.Object {
|
||||||
@ -530,22 +596,35 @@ func (m *memoryObj) setupObject() *goja.Object {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type stackObj struct {
|
type stackObj struct {
|
||||||
w *stackWrapper
|
stack *vm.Stack
|
||||||
vm *goja.Runtime
|
vm *goja.Runtime
|
||||||
toBig toBigFn
|
toBig toBigFn
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *stackObj) Peek(idx int) goja.Value {
|
func (s *stackObj) Peek(idx int) goja.Value {
|
||||||
value := s.w.peek(idx)
|
value, err := s.peek(idx)
|
||||||
|
if err != nil {
|
||||||
|
s.vm.Interrupt(err)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
res, err := s.toBig(s.vm, value.String())
|
res, err := s.toBig(s.vm, value.String())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
s.vm.Interrupt(err)
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// peek returns the nth-from-the-top element of the stack.
|
||||||
|
func (s *stackObj) peek(idx int) (*big.Int, error) {
|
||||||
|
if len(s.stack.Data()) <= idx || idx < 0 {
|
||||||
|
return nil, fmt.Errorf("Tracer accessed out of bound stack: size %d, index %d", len(s.stack.Data()), idx)
|
||||||
|
}
|
||||||
|
return s.stack.Back(idx).ToBig(), nil
|
||||||
|
}
|
||||||
|
|
||||||
func (s *stackObj) Length() int {
|
func (s *stackObj) Length() int {
|
||||||
return len(s.w.stack.Data())
|
return len(s.stack.Data())
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *stackObj) setupObject() *goja.Object {
|
func (s *stackObj) setupObject() *goja.Object {
|
||||||
@ -566,13 +645,15 @@ type dbObj struct {
|
|||||||
func (do *dbObj) GetBalance(addrSlice goja.Value) goja.Value {
|
func (do *dbObj) GetBalance(addrSlice goja.Value) goja.Value {
|
||||||
a, err := do.fromBuf(do.vm, addrSlice, false)
|
a, err := do.fromBuf(do.vm, addrSlice, false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
do.vm.Interrupt(err)
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
addr := common.BytesToAddress(a)
|
addr := common.BytesToAddress(a)
|
||||||
value := do.db.GetBalance(addr)
|
value := do.db.GetBalance(addr)
|
||||||
res, err := do.toBig(do.vm, value.String())
|
res, err := do.toBig(do.vm, value.String())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
do.vm.Interrupt(err)
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
@ -580,7 +661,8 @@ func (do *dbObj) GetBalance(addrSlice goja.Value) goja.Value {
|
|||||||
func (do *dbObj) GetNonce(addrSlice goja.Value) uint64 {
|
func (do *dbObj) GetNonce(addrSlice goja.Value) uint64 {
|
||||||
a, err := do.fromBuf(do.vm, addrSlice, false)
|
a, err := do.fromBuf(do.vm, addrSlice, false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
do.vm.Interrupt(err)
|
||||||
|
return 0
|
||||||
}
|
}
|
||||||
addr := common.BytesToAddress(a)
|
addr := common.BytesToAddress(a)
|
||||||
return do.db.GetNonce(addr)
|
return do.db.GetNonce(addr)
|
||||||
@ -589,13 +671,15 @@ func (do *dbObj) GetNonce(addrSlice goja.Value) uint64 {
|
|||||||
func (do *dbObj) GetCode(addrSlice goja.Value) goja.Value {
|
func (do *dbObj) GetCode(addrSlice goja.Value) goja.Value {
|
||||||
a, err := do.fromBuf(do.vm, addrSlice, false)
|
a, err := do.fromBuf(do.vm, addrSlice, false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
do.vm.Interrupt(err)
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
addr := common.BytesToAddress(a)
|
addr := common.BytesToAddress(a)
|
||||||
code := do.db.GetCode(addr)
|
code := do.db.GetCode(addr)
|
||||||
res, err := do.toBuf(do.vm, code)
|
res, err := do.toBuf(do.vm, code)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
do.vm.Interrupt(err)
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
@ -603,18 +687,21 @@ func (do *dbObj) GetCode(addrSlice goja.Value) goja.Value {
|
|||||||
func (do *dbObj) GetState(addrSlice goja.Value, hashSlice goja.Value) goja.Value {
|
func (do *dbObj) GetState(addrSlice goja.Value, hashSlice goja.Value) goja.Value {
|
||||||
a, err := do.fromBuf(do.vm, addrSlice, false)
|
a, err := do.fromBuf(do.vm, addrSlice, false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
do.vm.Interrupt(err)
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
addr := common.BytesToAddress(a)
|
addr := common.BytesToAddress(a)
|
||||||
h, err := do.fromBuf(do.vm, hashSlice, false)
|
h, err := do.fromBuf(do.vm, hashSlice, false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
do.vm.Interrupt(err)
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
hash := common.BytesToHash(h)
|
hash := common.BytesToHash(h)
|
||||||
state := do.db.GetState(addr, hash).Bytes()
|
state := do.db.GetState(addr, hash).Bytes()
|
||||||
res, err := do.toBuf(do.vm, state)
|
res, err := do.toBuf(do.vm, state)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
do.vm.Interrupt(err)
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
@ -622,7 +709,8 @@ func (do *dbObj) GetState(addrSlice goja.Value, hashSlice goja.Value) goja.Value
|
|||||||
func (do *dbObj) Exists(addrSlice goja.Value) bool {
|
func (do *dbObj) Exists(addrSlice goja.Value) bool {
|
||||||
a, err := do.fromBuf(do.vm, addrSlice, false)
|
a, err := do.fromBuf(do.vm, addrSlice, false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
do.vm.Interrupt(err)
|
||||||
|
return false
|
||||||
}
|
}
|
||||||
addr := common.BytesToAddress(a)
|
addr := common.BytesToAddress(a)
|
||||||
return do.db.Exist(addr)
|
return do.db.Exist(addr)
|
||||||
@ -649,7 +737,8 @@ func (co *contractObj) GetCaller() goja.Value {
|
|||||||
caller := co.contract.Caller().Bytes()
|
caller := co.contract.Caller().Bytes()
|
||||||
res, err := co.toBuf(co.vm, caller)
|
res, err := co.toBuf(co.vm, caller)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
co.vm.Interrupt(err)
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
@ -658,7 +747,8 @@ func (co *contractObj) GetAddress() goja.Value {
|
|||||||
addr := co.contract.Address().Bytes()
|
addr := co.contract.Address().Bytes()
|
||||||
res, err := co.toBuf(co.vm, addr)
|
res, err := co.toBuf(co.vm, addr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
co.vm.Interrupt(err)
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
@ -667,7 +757,8 @@ func (co *contractObj) GetValue() goja.Value {
|
|||||||
value := co.contract.Value()
|
value := co.contract.Value()
|
||||||
res, err := co.toBig(co.vm, value.String())
|
res, err := co.toBig(co.vm, value.String())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
co.vm.Interrupt(err)
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
@ -676,7 +767,8 @@ func (co *contractObj) GetInput() goja.Value {
|
|||||||
input := co.contract.Input
|
input := co.contract.Input
|
||||||
res, err := co.toBuf(co.vm, input)
|
res, err := co.toBuf(co.vm, input)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
co.vm.Interrupt(err)
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
@ -711,7 +803,8 @@ func (f *callframe) GetFrom() goja.Value {
|
|||||||
from := f.from.Bytes()
|
from := f.from.Bytes()
|
||||||
res, err := f.toBuf(f.vm, from)
|
res, err := f.toBuf(f.vm, from)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
f.vm.Interrupt(err)
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
@ -720,7 +813,8 @@ func (f *callframe) GetTo() goja.Value {
|
|||||||
to := f.to.Bytes()
|
to := f.to.Bytes()
|
||||||
res, err := f.toBuf(f.vm, to)
|
res, err := f.toBuf(f.vm, to)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
f.vm.Interrupt(err)
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
@ -729,7 +823,8 @@ func (f *callframe) GetInput() goja.Value {
|
|||||||
input := f.input
|
input := f.input
|
||||||
res, err := f.toBuf(f.vm, input)
|
res, err := f.toBuf(f.vm, input)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
f.vm.Interrupt(err)
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
@ -744,7 +839,8 @@ func (f *callframe) GetValue() goja.Value {
|
|||||||
}
|
}
|
||||||
res, err := f.toBig(f.vm, f.value.String())
|
res, err := f.toBig(f.vm, f.value.String())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
f.vm.Interrupt(err)
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
@ -776,7 +872,8 @@ func (r *callframeResult) GetGasUsed() uint {
|
|||||||
func (r *callframeResult) GetOutput() goja.Value {
|
func (r *callframeResult) GetOutput() goja.Value {
|
||||||
res, err := r.toBuf(r.vm, r.output)
|
res, err := r.toBuf(r.vm, r.output)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
r.vm.Interrupt(err)
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
@ -1,885 +0,0 @@
|
|||||||
// Copyright 2017 The go-ethereum Authors
|
|
||||||
// This file is part of the go-ethereum library.
|
|
||||||
//
|
|
||||||
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
||||||
// it under the terms of the GNU Lesser General Public License as published by
|
|
||||||
// the Free Software Foundation, either version 3 of the License, or
|
|
||||||
// (at your option) any later version.
|
|
||||||
//
|
|
||||||
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
||||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
// GNU Lesser General Public License for more details.
|
|
||||||
//
|
|
||||||
// You should have received a copy of the GNU Lesser General Public License
|
|
||||||
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
|
|
||||||
// Package js is a collection of tracers written in javascript.
|
|
||||||
package js
|
|
||||||
|
|
||||||
import (
|
|
||||||
"encoding/json"
|
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
"math/big"
|
|
||||||
"strings"
|
|
||||||
"sync/atomic"
|
|
||||||
"time"
|
|
||||||
"unsafe"
|
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum/common"
|
|
||||||
"github.com/ethereum/go-ethereum/common/hexutil"
|
|
||||||
"github.com/ethereum/go-ethereum/core/vm"
|
|
||||||
"github.com/ethereum/go-ethereum/crypto"
|
|
||||||
"github.com/ethereum/go-ethereum/eth/tracers"
|
|
||||||
jsassets "github.com/ethereum/go-ethereum/eth/tracers/js/internal/tracers"
|
|
||||||
"github.com/ethereum/go-ethereum/log"
|
|
||||||
"gopkg.in/olebedev/go-duktape.v3"
|
|
||||||
)
|
|
||||||
|
|
||||||
// init retrieves the JavaScript transaction tracers included in go-ethereum.
|
|
||||||
func init() {
|
|
||||||
assetTracers, err := jsassets.Load()
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
// TODO: Either disable duktape or solve conflicts between goja and duktape
|
|
||||||
tracers.RegisterLookup(false, func(name string, ctx *tracers.Context) (tracers.Tracer, error) {
|
|
||||||
if !strings.HasSuffix(name, "Duktape") {
|
|
||||||
return nil, errors.New("only suffix Duktape supported")
|
|
||||||
}
|
|
||||||
name = strings.TrimSuffix(name, "Duktape")
|
|
||||||
code, ok := assetTracers[name]
|
|
||||||
if !ok {
|
|
||||||
return nil, errors.New("only pre-built tracers supported")
|
|
||||||
}
|
|
||||||
return newJsTracer(code, ctx)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// makeSlice convert an unsafe memory pointer with the given type into a Go byte
|
|
||||||
// slice.
|
|
||||||
//
|
|
||||||
// Note, the returned slice uses the same memory area as the input arguments.
|
|
||||||
// If those are duktape stack items, popping them off **will** make the slice
|
|
||||||
// contents change.
|
|
||||||
func makeSlice(ptr unsafe.Pointer, size uint) []byte {
|
|
||||||
var sl = struct {
|
|
||||||
addr uintptr
|
|
||||||
len int
|
|
||||||
cap int
|
|
||||||
}{uintptr(ptr), int(size), int(size)}
|
|
||||||
|
|
||||||
return *(*[]byte)(unsafe.Pointer(&sl))
|
|
||||||
}
|
|
||||||
|
|
||||||
// popSlice pops a buffer off the JavaScript stack and returns it as a slice.
|
|
||||||
func popSlice(ctx *duktape.Context) []byte {
|
|
||||||
blob := common.CopyBytes(makeSlice(ctx.GetBuffer(-1)))
|
|
||||||
ctx.Pop()
|
|
||||||
return blob
|
|
||||||
}
|
|
||||||
|
|
||||||
// pushBigInt create a JavaScript BigInteger in the VM.
|
|
||||||
func pushBigInt(n *big.Int, ctx *duktape.Context) {
|
|
||||||
ctx.GetGlobalString("bigInt")
|
|
||||||
ctx.PushString(n.String())
|
|
||||||
ctx.Call(1)
|
|
||||||
}
|
|
||||||
|
|
||||||
// opWrapper provides a JavaScript wrapper around OpCode.
|
|
||||||
type opWrapper struct {
|
|
||||||
op vm.OpCode
|
|
||||||
}
|
|
||||||
|
|
||||||
// pushObject assembles a JSVM object wrapping a swappable opcode and pushes it
|
|
||||||
// onto the VM stack.
|
|
||||||
func (ow *opWrapper) pushObject(vm *duktape.Context) {
|
|
||||||
obj := vm.PushObject()
|
|
||||||
|
|
||||||
vm.PushGoFunction(func(ctx *duktape.Context) int { ctx.PushInt(int(ow.op)); return 1 })
|
|
||||||
vm.PutPropString(obj, "toNumber")
|
|
||||||
|
|
||||||
vm.PushGoFunction(func(ctx *duktape.Context) int { ctx.PushString(ow.op.String()); return 1 })
|
|
||||||
vm.PutPropString(obj, "toString")
|
|
||||||
|
|
||||||
vm.PushGoFunction(func(ctx *duktape.Context) int { ctx.PushBoolean(ow.op.IsPush()); return 1 })
|
|
||||||
vm.PutPropString(obj, "isPush")
|
|
||||||
}
|
|
||||||
|
|
||||||
// memoryWrapper provides a JavaScript wrapper around vm.Memory.
|
|
||||||
type memoryWrapper struct {
|
|
||||||
memory *vm.Memory
|
|
||||||
}
|
|
||||||
|
|
||||||
// slice returns the requested range of memory as a byte slice.
|
|
||||||
func (mw *memoryWrapper) slice(begin, end int64) []byte {
|
|
||||||
if end == begin {
|
|
||||||
return []byte{}
|
|
||||||
}
|
|
||||||
if end < begin || begin < 0 {
|
|
||||||
// TODO(karalabe): We can't js-throw from Go inside duktape inside Go. The Go
|
|
||||||
// runtime goes belly up https://github.com/golang/go/issues/15639.
|
|
||||||
log.Warn("Tracer accessed out of bound memory", "offset", begin, "end", end)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
if mw.memory.Len() < int(end) {
|
|
||||||
// TODO(karalabe): We can't js-throw from Go inside duktape inside Go. The Go
|
|
||||||
// runtime goes belly up https://github.com/golang/go/issues/15639.
|
|
||||||
log.Warn("Tracer accessed out of bound memory", "available", mw.memory.Len(), "offset", begin, "size", end-begin)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
return mw.memory.GetCopy(begin, end-begin)
|
|
||||||
}
|
|
||||||
|
|
||||||
// getUint returns the 32 bytes at the specified address interpreted as a uint.
|
|
||||||
func (mw *memoryWrapper) getUint(addr int64) *big.Int {
|
|
||||||
if mw.memory.Len() < int(addr)+32 || addr < 0 {
|
|
||||||
// TODO(karalabe): We can't js-throw from Go inside duktape inside Go. The Go
|
|
||||||
// runtime goes belly up https://github.com/golang/go/issues/15639.
|
|
||||||
log.Warn("Tracer accessed out of bound memory", "available", mw.memory.Len(), "offset", addr, "size", 32)
|
|
||||||
return new(big.Int)
|
|
||||||
}
|
|
||||||
return new(big.Int).SetBytes(mw.memory.GetPtr(addr, 32))
|
|
||||||
}
|
|
||||||
|
|
||||||
// pushObject assembles a JSVM object wrapping a swappable memory and pushes it
|
|
||||||
// onto the VM stack.
|
|
||||||
func (mw *memoryWrapper) pushObject(vm *duktape.Context) {
|
|
||||||
obj := vm.PushObject()
|
|
||||||
|
|
||||||
// Generate the `length` method which returns the memory length
|
|
||||||
vm.PushGoFunction(func(ctx *duktape.Context) int { ctx.PushInt(mw.memory.Len()); return 1 })
|
|
||||||
vm.PutPropString(obj, "length")
|
|
||||||
|
|
||||||
// Generate the `slice` method which takes two ints and returns a buffer
|
|
||||||
vm.PushGoFunction(func(ctx *duktape.Context) int {
|
|
||||||
blob := mw.slice(int64(ctx.GetInt(-2)), int64(ctx.GetInt(-1)))
|
|
||||||
ctx.Pop2()
|
|
||||||
|
|
||||||
ptr := ctx.PushFixedBuffer(len(blob))
|
|
||||||
copy(makeSlice(ptr, uint(len(blob))), blob)
|
|
||||||
return 1
|
|
||||||
})
|
|
||||||
vm.PutPropString(obj, "slice")
|
|
||||||
|
|
||||||
// Generate the `getUint` method which takes an int and returns a bigint
|
|
||||||
vm.PushGoFunction(func(ctx *duktape.Context) int {
|
|
||||||
offset := int64(ctx.GetInt(-1))
|
|
||||||
ctx.Pop()
|
|
||||||
|
|
||||||
pushBigInt(mw.getUint(offset), ctx)
|
|
||||||
return 1
|
|
||||||
})
|
|
||||||
vm.PutPropString(obj, "getUint")
|
|
||||||
}
|
|
||||||
|
|
||||||
// stackWrapper provides a JavaScript wrapper around vm.Stack.
|
|
||||||
type stackWrapper struct {
|
|
||||||
stack *vm.Stack
|
|
||||||
}
|
|
||||||
|
|
||||||
// peek returns the nth-from-the-top element of the stack.
|
|
||||||
func (sw *stackWrapper) peek(idx int) *big.Int {
|
|
||||||
if len(sw.stack.Data()) <= idx || idx < 0 {
|
|
||||||
// TODO(karalabe): We can't js-throw from Go inside duktape inside Go. The Go
|
|
||||||
// runtime goes belly up https://github.com/golang/go/issues/15639.
|
|
||||||
log.Warn("Tracer accessed out of bound stack", "size", len(sw.stack.Data()), "index", idx)
|
|
||||||
return new(big.Int)
|
|
||||||
}
|
|
||||||
return sw.stack.Back(idx).ToBig()
|
|
||||||
}
|
|
||||||
|
|
||||||
// pushObject assembles a JSVM object wrapping a swappable stack and pushes it
|
|
||||||
// onto the VM stack.
|
|
||||||
func (sw *stackWrapper) pushObject(vm *duktape.Context) {
|
|
||||||
obj := vm.PushObject()
|
|
||||||
|
|
||||||
vm.PushGoFunction(func(ctx *duktape.Context) int { ctx.PushInt(len(sw.stack.Data())); return 1 })
|
|
||||||
vm.PutPropString(obj, "length")
|
|
||||||
|
|
||||||
// Generate the `peek` method which takes an int and returns a bigint
|
|
||||||
vm.PushGoFunction(func(ctx *duktape.Context) int {
|
|
||||||
offset := ctx.GetInt(-1)
|
|
||||||
ctx.Pop()
|
|
||||||
|
|
||||||
pushBigInt(sw.peek(offset), ctx)
|
|
||||||
return 1
|
|
||||||
})
|
|
||||||
vm.PutPropString(obj, "peek")
|
|
||||||
}
|
|
||||||
|
|
||||||
// dbWrapper provides a JavaScript wrapper around vm.Database.
|
|
||||||
type dbWrapper struct {
|
|
||||||
db vm.StateDB
|
|
||||||
}
|
|
||||||
|
|
||||||
// pushObject assembles a JSVM object wrapping a swappable database and pushes it
|
|
||||||
// onto the VM stack.
|
|
||||||
func (dw *dbWrapper) pushObject(vm *duktape.Context) {
|
|
||||||
obj := vm.PushObject()
|
|
||||||
|
|
||||||
// Push the wrapper for statedb.GetBalance
|
|
||||||
vm.PushGoFunction(func(ctx *duktape.Context) int {
|
|
||||||
pushBigInt(dw.db.GetBalance(common.BytesToAddress(popSlice(ctx))), ctx)
|
|
||||||
return 1
|
|
||||||
})
|
|
||||||
vm.PutPropString(obj, "getBalance")
|
|
||||||
|
|
||||||
// Push the wrapper for statedb.GetNonce
|
|
||||||
vm.PushGoFunction(func(ctx *duktape.Context) int {
|
|
||||||
ctx.PushInt(int(dw.db.GetNonce(common.BytesToAddress(popSlice(ctx)))))
|
|
||||||
return 1
|
|
||||||
})
|
|
||||||
vm.PutPropString(obj, "getNonce")
|
|
||||||
|
|
||||||
// Push the wrapper for statedb.GetCode
|
|
||||||
vm.PushGoFunction(func(ctx *duktape.Context) int {
|
|
||||||
code := dw.db.GetCode(common.BytesToAddress(popSlice(ctx)))
|
|
||||||
|
|
||||||
ptr := ctx.PushFixedBuffer(len(code))
|
|
||||||
copy(makeSlice(ptr, uint(len(code))), code)
|
|
||||||
return 1
|
|
||||||
})
|
|
||||||
vm.PutPropString(obj, "getCode")
|
|
||||||
|
|
||||||
// Push the wrapper for statedb.GetState
|
|
||||||
vm.PushGoFunction(func(ctx *duktape.Context) int {
|
|
||||||
hash := popSlice(ctx)
|
|
||||||
addr := popSlice(ctx)
|
|
||||||
|
|
||||||
state := dw.db.GetState(common.BytesToAddress(addr), common.BytesToHash(hash))
|
|
||||||
|
|
||||||
ptr := ctx.PushFixedBuffer(len(state))
|
|
||||||
copy(makeSlice(ptr, uint(len(state))), state[:])
|
|
||||||
return 1
|
|
||||||
})
|
|
||||||
vm.PutPropString(obj, "getState")
|
|
||||||
|
|
||||||
// Push the wrapper for statedb.Exists
|
|
||||||
vm.PushGoFunction(func(ctx *duktape.Context) int {
|
|
||||||
ctx.PushBoolean(dw.db.Exist(common.BytesToAddress(popSlice(ctx))))
|
|
||||||
return 1
|
|
||||||
})
|
|
||||||
vm.PutPropString(obj, "exists")
|
|
||||||
}
|
|
||||||
|
|
||||||
// contractWrapper provides a JavaScript wrapper around vm.Contract
|
|
||||||
type contractWrapper struct {
|
|
||||||
contract *vm.Contract
|
|
||||||
}
|
|
||||||
|
|
||||||
// pushObject assembles a JSVM object wrapping a swappable contract and pushes it
|
|
||||||
// onto the VM stack.
|
|
||||||
func (cw *contractWrapper) pushObject(vm *duktape.Context) {
|
|
||||||
obj := vm.PushObject()
|
|
||||||
|
|
||||||
// Push the wrapper for contract.Caller
|
|
||||||
vm.PushGoFunction(func(ctx *duktape.Context) int {
|
|
||||||
ptr := ctx.PushFixedBuffer(20)
|
|
||||||
copy(makeSlice(ptr, 20), cw.contract.Caller().Bytes())
|
|
||||||
return 1
|
|
||||||
})
|
|
||||||
vm.PutPropString(obj, "getCaller")
|
|
||||||
|
|
||||||
// Push the wrapper for contract.Address
|
|
||||||
vm.PushGoFunction(func(ctx *duktape.Context) int {
|
|
||||||
ptr := ctx.PushFixedBuffer(20)
|
|
||||||
copy(makeSlice(ptr, 20), cw.contract.Address().Bytes())
|
|
||||||
return 1
|
|
||||||
})
|
|
||||||
vm.PutPropString(obj, "getAddress")
|
|
||||||
|
|
||||||
// Push the wrapper for contract.Value
|
|
||||||
vm.PushGoFunction(func(ctx *duktape.Context) int {
|
|
||||||
pushBigInt(cw.contract.Value(), ctx)
|
|
||||||
return 1
|
|
||||||
})
|
|
||||||
vm.PutPropString(obj, "getValue")
|
|
||||||
|
|
||||||
// Push the wrapper for contract.Input
|
|
||||||
vm.PushGoFunction(func(ctx *duktape.Context) int {
|
|
||||||
blob := cw.contract.Input
|
|
||||||
|
|
||||||
ptr := ctx.PushFixedBuffer(len(blob))
|
|
||||||
copy(makeSlice(ptr, uint(len(blob))), blob)
|
|
||||||
return 1
|
|
||||||
})
|
|
||||||
vm.PutPropString(obj, "getInput")
|
|
||||||
}
|
|
||||||
|
|
||||||
type frame struct {
|
|
||||||
typ *string
|
|
||||||
from *common.Address
|
|
||||||
to *common.Address
|
|
||||||
input []byte
|
|
||||||
gas *uint
|
|
||||||
value *big.Int
|
|
||||||
}
|
|
||||||
|
|
||||||
func newFrame() *frame {
|
|
||||||
return &frame{
|
|
||||||
typ: new(string),
|
|
||||||
from: new(common.Address),
|
|
||||||
to: new(common.Address),
|
|
||||||
gas: new(uint),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (f *frame) pushObject(vm *duktape.Context) {
|
|
||||||
obj := vm.PushObject()
|
|
||||||
|
|
||||||
vm.PushGoFunction(func(ctx *duktape.Context) int { pushValue(ctx, *f.typ); return 1 })
|
|
||||||
vm.PutPropString(obj, "getType")
|
|
||||||
|
|
||||||
vm.PushGoFunction(func(ctx *duktape.Context) int { pushValue(ctx, *f.from); return 1 })
|
|
||||||
vm.PutPropString(obj, "getFrom")
|
|
||||||
|
|
||||||
vm.PushGoFunction(func(ctx *duktape.Context) int { pushValue(ctx, *f.to); return 1 })
|
|
||||||
vm.PutPropString(obj, "getTo")
|
|
||||||
|
|
||||||
vm.PushGoFunction(func(ctx *duktape.Context) int { pushValue(ctx, f.input); return 1 })
|
|
||||||
vm.PutPropString(obj, "getInput")
|
|
||||||
|
|
||||||
vm.PushGoFunction(func(ctx *duktape.Context) int { pushValue(ctx, *f.gas); return 1 })
|
|
||||||
vm.PutPropString(obj, "getGas")
|
|
||||||
|
|
||||||
vm.PushGoFunction(func(ctx *duktape.Context) int {
|
|
||||||
if f.value != nil {
|
|
||||||
pushValue(ctx, f.value)
|
|
||||||
} else {
|
|
||||||
ctx.PushUndefined()
|
|
||||||
}
|
|
||||||
return 1
|
|
||||||
})
|
|
||||||
vm.PutPropString(obj, "getValue")
|
|
||||||
}
|
|
||||||
|
|
||||||
type frameResult struct {
|
|
||||||
gasUsed *uint
|
|
||||||
output []byte
|
|
||||||
errorValue *string
|
|
||||||
}
|
|
||||||
|
|
||||||
func newFrameResult() *frameResult {
|
|
||||||
return &frameResult{
|
|
||||||
gasUsed: new(uint),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *frameResult) pushObject(vm *duktape.Context) {
|
|
||||||
obj := vm.PushObject()
|
|
||||||
|
|
||||||
vm.PushGoFunction(func(ctx *duktape.Context) int { pushValue(ctx, *r.gasUsed); return 1 })
|
|
||||||
vm.PutPropString(obj, "getGasUsed")
|
|
||||||
|
|
||||||
vm.PushGoFunction(func(ctx *duktape.Context) int { pushValue(ctx, r.output); return 1 })
|
|
||||||
vm.PutPropString(obj, "getOutput")
|
|
||||||
|
|
||||||
vm.PushGoFunction(func(ctx *duktape.Context) int {
|
|
||||||
if r.errorValue != nil {
|
|
||||||
pushValue(ctx, *r.errorValue)
|
|
||||||
} else {
|
|
||||||
ctx.PushUndefined()
|
|
||||||
}
|
|
||||||
return 1
|
|
||||||
})
|
|
||||||
vm.PutPropString(obj, "getError")
|
|
||||||
}
|
|
||||||
|
|
||||||
// jsTracer provides an implementation of Tracer that evaluates a Javascript
|
|
||||||
// function for each VM execution step.
|
|
||||||
type jsTracer struct {
|
|
||||||
vm *duktape.Context // Javascript VM instance
|
|
||||||
env *vm.EVM // EVM instance executing the code being traced
|
|
||||||
|
|
||||||
tracerObject int // Stack index of the tracer JavaScript object
|
|
||||||
stateObject int // Stack index of the global state to pull arguments from
|
|
||||||
|
|
||||||
opWrapper *opWrapper // Wrapper around the VM opcode
|
|
||||||
stackWrapper *stackWrapper // Wrapper around the VM stack
|
|
||||||
memoryWrapper *memoryWrapper // Wrapper around the VM memory
|
|
||||||
contractWrapper *contractWrapper // Wrapper around the contract object
|
|
||||||
dbWrapper *dbWrapper // Wrapper around the VM environment
|
|
||||||
|
|
||||||
pcValue *uint // Swappable pc value wrapped by a log accessor
|
|
||||||
gasValue *uint // Swappable gas value wrapped by a log accessor
|
|
||||||
costValue *uint // Swappable cost value wrapped by a log accessor
|
|
||||||
depthValue *uint // Swappable depth value wrapped by a log accessor
|
|
||||||
errorValue *string // Swappable error value wrapped by a log accessor
|
|
||||||
refundValue *uint // Swappable refund value wrapped by a log accessor
|
|
||||||
|
|
||||||
frame *frame // Represents entry into call frame. Fields are swappable
|
|
||||||
frameResult *frameResult // Represents exit from a call frame. Fields are swappable
|
|
||||||
|
|
||||||
ctx map[string]interface{} // Transaction context gathered throughout execution
|
|
||||||
err error // Error, if one has occurred
|
|
||||||
|
|
||||||
interrupt uint32 // Atomic flag to signal execution interruption
|
|
||||||
reason error // Textual reason for the interruption
|
|
||||||
|
|
||||||
activePrecompiles []common.Address // Updated on CaptureStart based on given rules
|
|
||||||
traceSteps bool // When true, will invoke step() on each opcode
|
|
||||||
traceCallFrames bool // When true, will invoke enter() and exit() js funcs
|
|
||||||
gasLimit uint64 // Amount of gas bought for the whole tx
|
|
||||||
}
|
|
||||||
|
|
||||||
// New instantiates a new tracer instance. code specifies a Javascript snippet,
|
|
||||||
// which must evaluate to an expression returning an object with 'step', 'fault'
|
|
||||||
// and 'result' functions.
|
|
||||||
func newJsTracer(code string, ctx *tracers.Context) (tracers.Tracer, error) {
|
|
||||||
if ctx == nil {
|
|
||||||
ctx = new(tracers.Context)
|
|
||||||
}
|
|
||||||
tracer := &jsTracer{
|
|
||||||
vm: duktape.New(),
|
|
||||||
ctx: make(map[string]interface{}),
|
|
||||||
opWrapper: new(opWrapper),
|
|
||||||
stackWrapper: new(stackWrapper),
|
|
||||||
memoryWrapper: new(memoryWrapper),
|
|
||||||
contractWrapper: new(contractWrapper),
|
|
||||||
dbWrapper: new(dbWrapper),
|
|
||||||
pcValue: new(uint),
|
|
||||||
gasValue: new(uint),
|
|
||||||
costValue: new(uint),
|
|
||||||
depthValue: new(uint),
|
|
||||||
refundValue: new(uint),
|
|
||||||
frame: newFrame(),
|
|
||||||
frameResult: newFrameResult(),
|
|
||||||
}
|
|
||||||
if ctx.BlockHash != (common.Hash{}) {
|
|
||||||
tracer.ctx["blockHash"] = ctx.BlockHash
|
|
||||||
|
|
||||||
if ctx.TxHash != (common.Hash{}) {
|
|
||||||
tracer.ctx["txIndex"] = ctx.TxIndex
|
|
||||||
tracer.ctx["txHash"] = ctx.TxHash
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// Set up builtins for this environment
|
|
||||||
tracer.vm.PushGlobalGoFunction("toHex", func(ctx *duktape.Context) int {
|
|
||||||
ctx.PushString(hexutil.Encode(popSlice(ctx)))
|
|
||||||
return 1
|
|
||||||
})
|
|
||||||
tracer.vm.PushGlobalGoFunction("toWord", func(ctx *duktape.Context) int {
|
|
||||||
var word common.Hash
|
|
||||||
if ptr, size := ctx.GetBuffer(-1); ptr != nil {
|
|
||||||
word = common.BytesToHash(makeSlice(ptr, size))
|
|
||||||
} else {
|
|
||||||
word = common.HexToHash(ctx.GetString(-1))
|
|
||||||
}
|
|
||||||
ctx.Pop()
|
|
||||||
copy(makeSlice(ctx.PushFixedBuffer(32), 32), word[:])
|
|
||||||
return 1
|
|
||||||
})
|
|
||||||
tracer.vm.PushGlobalGoFunction("toAddress", func(ctx *duktape.Context) int {
|
|
||||||
var addr common.Address
|
|
||||||
if ptr, size := ctx.GetBuffer(-1); ptr != nil {
|
|
||||||
addr = common.BytesToAddress(makeSlice(ptr, size))
|
|
||||||
} else {
|
|
||||||
addr = common.HexToAddress(ctx.GetString(-1))
|
|
||||||
}
|
|
||||||
ctx.Pop()
|
|
||||||
copy(makeSlice(ctx.PushFixedBuffer(20), 20), addr[:])
|
|
||||||
return 1
|
|
||||||
})
|
|
||||||
tracer.vm.PushGlobalGoFunction("toContract", func(ctx *duktape.Context) int {
|
|
||||||
var from common.Address
|
|
||||||
if ptr, size := ctx.GetBuffer(-2); ptr != nil {
|
|
||||||
from = common.BytesToAddress(makeSlice(ptr, size))
|
|
||||||
} else {
|
|
||||||
from = common.HexToAddress(ctx.GetString(-2))
|
|
||||||
}
|
|
||||||
nonce := uint64(ctx.GetInt(-1))
|
|
||||||
ctx.Pop2()
|
|
||||||
|
|
||||||
contract := crypto.CreateAddress(from, nonce)
|
|
||||||
copy(makeSlice(ctx.PushFixedBuffer(20), 20), contract[:])
|
|
||||||
return 1
|
|
||||||
})
|
|
||||||
tracer.vm.PushGlobalGoFunction("toContract2", func(ctx *duktape.Context) int {
|
|
||||||
var from common.Address
|
|
||||||
if ptr, size := ctx.GetBuffer(-3); ptr != nil {
|
|
||||||
from = common.BytesToAddress(makeSlice(ptr, size))
|
|
||||||
} else {
|
|
||||||
from = common.HexToAddress(ctx.GetString(-3))
|
|
||||||
}
|
|
||||||
// Retrieve salt hex string from js stack
|
|
||||||
salt := common.HexToHash(ctx.GetString(-2))
|
|
||||||
// Retrieve code slice from js stack
|
|
||||||
var code []byte
|
|
||||||
if ptr, size := ctx.GetBuffer(-1); ptr != nil {
|
|
||||||
code = common.CopyBytes(makeSlice(ptr, size))
|
|
||||||
} else {
|
|
||||||
code = common.FromHex(ctx.GetString(-1))
|
|
||||||
}
|
|
||||||
codeHash := crypto.Keccak256(code)
|
|
||||||
ctx.Pop3()
|
|
||||||
contract := crypto.CreateAddress2(from, salt, codeHash)
|
|
||||||
copy(makeSlice(ctx.PushFixedBuffer(20), 20), contract[:])
|
|
||||||
return 1
|
|
||||||
})
|
|
||||||
tracer.vm.PushGlobalGoFunction("isPrecompiled", func(ctx *duktape.Context) int {
|
|
||||||
addr := common.BytesToAddress(popSlice(ctx))
|
|
||||||
for _, p := range tracer.activePrecompiles {
|
|
||||||
if p == addr {
|
|
||||||
ctx.PushBoolean(true)
|
|
||||||
return 1
|
|
||||||
}
|
|
||||||
}
|
|
||||||
ctx.PushBoolean(false)
|
|
||||||
return 1
|
|
||||||
})
|
|
||||||
tracer.vm.PushGlobalGoFunction("slice", func(ctx *duktape.Context) int {
|
|
||||||
start, end := ctx.GetInt(-2), ctx.GetInt(-1)
|
|
||||||
ctx.Pop2()
|
|
||||||
|
|
||||||
blob := popSlice(ctx)
|
|
||||||
size := end - start
|
|
||||||
|
|
||||||
if start < 0 || start > end || end > len(blob) {
|
|
||||||
// TODO(karalabe): We can't js-throw from Go inside duktape inside Go. The Go
|
|
||||||
// runtime goes belly up https://github.com/golang/go/issues/15639.
|
|
||||||
log.Warn("Tracer accessed out of bound memory", "available", len(blob), "offset", start, "size", size)
|
|
||||||
ctx.PushFixedBuffer(0)
|
|
||||||
return 1
|
|
||||||
}
|
|
||||||
copy(makeSlice(ctx.PushFixedBuffer(size), uint(size)), blob[start:end])
|
|
||||||
return 1
|
|
||||||
})
|
|
||||||
// Push the JavaScript tracer as object #0 onto the JSVM stack and validate it
|
|
||||||
if err := tracer.vm.PevalString("(" + code + ")"); err != nil {
|
|
||||||
log.Warn("Failed to compile tracer", "err", err)
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
tracer.tracerObject = 0 // yeah, nice, eval can't return the index itself
|
|
||||||
|
|
||||||
hasStep := tracer.vm.GetPropString(tracer.tracerObject, "step")
|
|
||||||
tracer.vm.Pop()
|
|
||||||
|
|
||||||
if !tracer.vm.GetPropString(tracer.tracerObject, "fault") {
|
|
||||||
return nil, fmt.Errorf("trace object must expose a function fault()")
|
|
||||||
}
|
|
||||||
tracer.vm.Pop()
|
|
||||||
|
|
||||||
if !tracer.vm.GetPropString(tracer.tracerObject, "result") {
|
|
||||||
return nil, fmt.Errorf("trace object must expose a function result()")
|
|
||||||
}
|
|
||||||
tracer.vm.Pop()
|
|
||||||
|
|
||||||
hasEnter := tracer.vm.GetPropString(tracer.tracerObject, "enter")
|
|
||||||
tracer.vm.Pop()
|
|
||||||
hasExit := tracer.vm.GetPropString(tracer.tracerObject, "exit")
|
|
||||||
tracer.vm.Pop()
|
|
||||||
if hasEnter != hasExit {
|
|
||||||
return nil, fmt.Errorf("trace object must expose either both or none of enter() and exit()")
|
|
||||||
}
|
|
||||||
tracer.traceCallFrames = hasEnter && hasExit
|
|
||||||
tracer.traceSteps = hasStep
|
|
||||||
|
|
||||||
// Tracer is valid, inject the big int library to access large numbers
|
|
||||||
tracer.vm.EvalString(bigIntegerJS)
|
|
||||||
tracer.vm.PutGlobalString("bigInt")
|
|
||||||
|
|
||||||
// Push the global environment state as object #1 into the JSVM stack
|
|
||||||
tracer.stateObject = tracer.vm.PushObject()
|
|
||||||
|
|
||||||
logObject := tracer.vm.PushObject()
|
|
||||||
|
|
||||||
tracer.opWrapper.pushObject(tracer.vm)
|
|
||||||
tracer.vm.PutPropString(logObject, "op")
|
|
||||||
|
|
||||||
tracer.stackWrapper.pushObject(tracer.vm)
|
|
||||||
tracer.vm.PutPropString(logObject, "stack")
|
|
||||||
|
|
||||||
tracer.memoryWrapper.pushObject(tracer.vm)
|
|
||||||
tracer.vm.PutPropString(logObject, "memory")
|
|
||||||
|
|
||||||
tracer.contractWrapper.pushObject(tracer.vm)
|
|
||||||
tracer.vm.PutPropString(logObject, "contract")
|
|
||||||
|
|
||||||
tracer.vm.PushGoFunction(func(ctx *duktape.Context) int { ctx.PushUint(*tracer.pcValue); return 1 })
|
|
||||||
tracer.vm.PutPropString(logObject, "getPC")
|
|
||||||
|
|
||||||
tracer.vm.PushGoFunction(func(ctx *duktape.Context) int { ctx.PushUint(*tracer.gasValue); return 1 })
|
|
||||||
tracer.vm.PutPropString(logObject, "getGas")
|
|
||||||
|
|
||||||
tracer.vm.PushGoFunction(func(ctx *duktape.Context) int { ctx.PushUint(*tracer.costValue); return 1 })
|
|
||||||
tracer.vm.PutPropString(logObject, "getCost")
|
|
||||||
|
|
||||||
tracer.vm.PushGoFunction(func(ctx *duktape.Context) int { ctx.PushUint(*tracer.depthValue); return 1 })
|
|
||||||
tracer.vm.PutPropString(logObject, "getDepth")
|
|
||||||
|
|
||||||
tracer.vm.PushGoFunction(func(ctx *duktape.Context) int { ctx.PushUint(*tracer.refundValue); return 1 })
|
|
||||||
tracer.vm.PutPropString(logObject, "getRefund")
|
|
||||||
|
|
||||||
tracer.vm.PushGoFunction(func(ctx *duktape.Context) int {
|
|
||||||
if tracer.errorValue != nil {
|
|
||||||
ctx.PushString(*tracer.errorValue)
|
|
||||||
} else {
|
|
||||||
ctx.PushUndefined()
|
|
||||||
}
|
|
||||||
return 1
|
|
||||||
})
|
|
||||||
tracer.vm.PutPropString(logObject, "getError")
|
|
||||||
|
|
||||||
tracer.vm.PutPropString(tracer.stateObject, "log")
|
|
||||||
|
|
||||||
tracer.frame.pushObject(tracer.vm)
|
|
||||||
tracer.vm.PutPropString(tracer.stateObject, "frame")
|
|
||||||
|
|
||||||
tracer.frameResult.pushObject(tracer.vm)
|
|
||||||
tracer.vm.PutPropString(tracer.stateObject, "frameResult")
|
|
||||||
|
|
||||||
tracer.dbWrapper.pushObject(tracer.vm)
|
|
||||||
tracer.vm.PutPropString(tracer.stateObject, "db")
|
|
||||||
|
|
||||||
return tracer, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Stop terminates execution of the tracer at the first opportune moment.
|
|
||||||
func (jst *jsTracer) Stop(err error) {
|
|
||||||
jst.reason = err
|
|
||||||
atomic.StoreUint32(&jst.interrupt, 1)
|
|
||||||
}
|
|
||||||
|
|
||||||
// call executes a method on a JS object, catching any errors, formatting and
|
|
||||||
// returning them as error objects.
|
|
||||||
func (jst *jsTracer) call(noret bool, method string, args ...string) (json.RawMessage, error) {
|
|
||||||
// Execute the JavaScript call and return any error
|
|
||||||
jst.vm.PushString(method)
|
|
||||||
for _, arg := range args {
|
|
||||||
jst.vm.GetPropString(jst.stateObject, arg)
|
|
||||||
}
|
|
||||||
code := jst.vm.PcallProp(jst.tracerObject, len(args))
|
|
||||||
defer jst.vm.Pop()
|
|
||||||
|
|
||||||
if code != 0 {
|
|
||||||
err := jst.vm.SafeToString(-1)
|
|
||||||
return nil, errors.New(err)
|
|
||||||
}
|
|
||||||
// No error occurred, extract return value and return
|
|
||||||
if noret {
|
|
||||||
return nil, nil
|
|
||||||
}
|
|
||||||
// Push a JSON marshaller onto the stack. We can't marshal from the out-
|
|
||||||
// side because duktape can crash on large nestings and we can't catch
|
|
||||||
// C++ exceptions ourselves from Go. TODO(karalabe): Yuck, why wrap?!
|
|
||||||
jst.vm.PushString("(JSON.stringify)")
|
|
||||||
jst.vm.Eval()
|
|
||||||
|
|
||||||
jst.vm.Swap(-1, -2)
|
|
||||||
if code = jst.vm.Pcall(1); code != 0 {
|
|
||||||
err := jst.vm.SafeToString(-1)
|
|
||||||
return nil, errors.New(err)
|
|
||||||
}
|
|
||||||
return json.RawMessage(jst.vm.SafeToString(-1)), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func wrapError(context string, err error) error {
|
|
||||||
return fmt.Errorf("%v in server-side tracer function '%v'", err, context)
|
|
||||||
}
|
|
||||||
|
|
||||||
// CaptureTxStart implements the Tracer interface and is invoked at the beginning of
|
|
||||||
// transaction processing.
|
|
||||||
func (jst *jsTracer) CaptureTxStart(gasLimit uint64) {
|
|
||||||
jst.gasLimit = gasLimit
|
|
||||||
}
|
|
||||||
|
|
||||||
// CaptureTxEnd implements the Tracer interface and is invoked at the end of
|
|
||||||
// transaction processing.
|
|
||||||
func (*jsTracer) CaptureTxEnd(restGas uint64) {}
|
|
||||||
|
|
||||||
// CaptureStart implements the Tracer interface and is invoked before executing the
|
|
||||||
// top-level call frame of a transaction.
|
|
||||||
func (jst *jsTracer) CaptureStart(env *vm.EVM, from common.Address, to common.Address, create bool, input []byte, gas uint64, value *big.Int) {
|
|
||||||
jst.env = env
|
|
||||||
jst.ctx["type"] = "CALL"
|
|
||||||
if create {
|
|
||||||
jst.ctx["type"] = "CREATE"
|
|
||||||
}
|
|
||||||
jst.ctx["from"] = from
|
|
||||||
jst.ctx["to"] = to
|
|
||||||
jst.ctx["input"] = input
|
|
||||||
jst.ctx["gas"] = gas
|
|
||||||
jst.ctx["gasPrice"] = env.TxContext.GasPrice
|
|
||||||
jst.ctx["value"] = value
|
|
||||||
|
|
||||||
// Initialize the context
|
|
||||||
jst.ctx["block"] = env.Context.BlockNumber.Uint64()
|
|
||||||
jst.dbWrapper.db = env.StateDB
|
|
||||||
// Update list of precompiles based on current block
|
|
||||||
rules := env.ChainConfig().Rules(env.Context.BlockNumber, env.Context.Random != nil)
|
|
||||||
jst.activePrecompiles = vm.ActivePrecompiles(rules)
|
|
||||||
|
|
||||||
// Intrinsic costs are the only things reduced from initial gas to this point
|
|
||||||
jst.ctx["intrinsicGas"] = jst.gasLimit - gas
|
|
||||||
}
|
|
||||||
|
|
||||||
// CaptureState implements the Tracer interface to trace a single step of VM execution.
|
|
||||||
func (jst *jsTracer) CaptureState(pc uint64, op vm.OpCode, gas, cost uint64, scope *vm.ScopeContext, rData []byte, depth int, err error) {
|
|
||||||
if !jst.traceSteps {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if jst.err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
// If tracing was interrupted, set the error and stop
|
|
||||||
if atomic.LoadUint32(&jst.interrupt) > 0 {
|
|
||||||
jst.err = jst.reason
|
|
||||||
jst.env.Cancel()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
jst.opWrapper.op = op
|
|
||||||
jst.stackWrapper.stack = scope.Stack
|
|
||||||
jst.memoryWrapper.memory = scope.Memory
|
|
||||||
jst.contractWrapper.contract = scope.Contract
|
|
||||||
|
|
||||||
*jst.pcValue = uint(pc)
|
|
||||||
*jst.gasValue = uint(gas)
|
|
||||||
*jst.costValue = uint(cost)
|
|
||||||
*jst.depthValue = uint(depth)
|
|
||||||
*jst.refundValue = uint(jst.env.StateDB.GetRefund())
|
|
||||||
|
|
||||||
jst.errorValue = nil
|
|
||||||
if err != nil {
|
|
||||||
jst.errorValue = new(string)
|
|
||||||
*jst.errorValue = err.Error()
|
|
||||||
}
|
|
||||||
|
|
||||||
if _, err := jst.call(true, "step", "log", "db"); err != nil {
|
|
||||||
jst.err = wrapError("step", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// CaptureFault implements the Tracer interface to trace an execution fault
|
|
||||||
func (jst *jsTracer) CaptureFault(pc uint64, op vm.OpCode, gas, cost uint64, scope *vm.ScopeContext, depth int, err error) {
|
|
||||||
if jst.err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
// Apart from the error, everything matches the previous invocation
|
|
||||||
jst.errorValue = new(string)
|
|
||||||
*jst.errorValue = err.Error()
|
|
||||||
|
|
||||||
if _, err := jst.call(true, "fault", "log", "db"); err != nil {
|
|
||||||
jst.err = wrapError("fault", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// CaptureEnd is called after the top-level call finishes.
|
|
||||||
func (jst *jsTracer) CaptureEnd(output []byte, gasUsed uint64, t time.Duration, err error) {
|
|
||||||
jst.ctx["output"] = output
|
|
||||||
jst.ctx["time"] = t.String()
|
|
||||||
jst.ctx["gasUsed"] = gasUsed
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
jst.ctx["error"] = err.Error()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// CaptureEnter is called when EVM enters a new scope (via call, create or selfdestruct).
|
|
||||||
func (jst *jsTracer) CaptureEnter(typ vm.OpCode, from common.Address, to common.Address, input []byte, gas uint64, value *big.Int) {
|
|
||||||
if !jst.traceCallFrames {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if jst.err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
// If tracing was interrupted, set the error and stop
|
|
||||||
if atomic.LoadUint32(&jst.interrupt) > 0 {
|
|
||||||
jst.err = jst.reason
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
*jst.frame.typ = typ.String()
|
|
||||||
*jst.frame.from = from
|
|
||||||
*jst.frame.to = to
|
|
||||||
jst.frame.input = common.CopyBytes(input)
|
|
||||||
*jst.frame.gas = uint(gas)
|
|
||||||
jst.frame.value = nil
|
|
||||||
if value != nil {
|
|
||||||
jst.frame.value = new(big.Int).SetBytes(value.Bytes())
|
|
||||||
}
|
|
||||||
|
|
||||||
if _, err := jst.call(true, "enter", "frame"); err != nil {
|
|
||||||
jst.err = wrapError("enter", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// CaptureExit is called when EVM exits a scope, even if the scope didn't
|
|
||||||
// execute any code.
|
|
||||||
func (jst *jsTracer) CaptureExit(output []byte, gasUsed uint64, err error) {
|
|
||||||
if !jst.traceCallFrames {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
// If tracing was interrupted, set the error and stop
|
|
||||||
if atomic.LoadUint32(&jst.interrupt) > 0 {
|
|
||||||
jst.err = jst.reason
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
jst.frameResult.output = common.CopyBytes(output)
|
|
||||||
*jst.frameResult.gasUsed = uint(gasUsed)
|
|
||||||
jst.frameResult.errorValue = nil
|
|
||||||
if err != nil {
|
|
||||||
jst.frameResult.errorValue = new(string)
|
|
||||||
*jst.frameResult.errorValue = err.Error()
|
|
||||||
}
|
|
||||||
|
|
||||||
if _, err := jst.call(true, "exit", "frameResult"); err != nil {
|
|
||||||
jst.err = wrapError("exit", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetResult calls the Javascript 'result' function and returns its value, or any accumulated error
|
|
||||||
func (jst *jsTracer) GetResult() (json.RawMessage, error) {
|
|
||||||
// Transform the context into a JavaScript object and inject into the state
|
|
||||||
obj := jst.vm.PushObject()
|
|
||||||
|
|
||||||
for key, val := range jst.ctx {
|
|
||||||
jst.addToObj(obj, key, val)
|
|
||||||
}
|
|
||||||
jst.vm.PutPropString(jst.stateObject, "ctx")
|
|
||||||
|
|
||||||
// Finalize the trace and return the results
|
|
||||||
result, err := jst.call(false, "result", "ctx", "db")
|
|
||||||
if err != nil {
|
|
||||||
jst.err = wrapError("result", err)
|
|
||||||
}
|
|
||||||
// Clean up the JavaScript environment
|
|
||||||
jst.vm.DestroyHeap()
|
|
||||||
jst.vm.Destroy()
|
|
||||||
|
|
||||||
return result, jst.err
|
|
||||||
}
|
|
||||||
|
|
||||||
// addToObj pushes a field to a JS object.
|
|
||||||
func (jst *jsTracer) addToObj(obj int, key string, val interface{}) {
|
|
||||||
pushValue(jst.vm, val)
|
|
||||||
jst.vm.PutPropString(obj, key)
|
|
||||||
}
|
|
||||||
|
|
||||||
func pushValue(ctx *duktape.Context, val interface{}) {
|
|
||||||
switch val := val.(type) {
|
|
||||||
case uint64:
|
|
||||||
ctx.PushUint(uint(val))
|
|
||||||
case string:
|
|
||||||
ctx.PushString(val)
|
|
||||||
case []byte:
|
|
||||||
ptr := ctx.PushFixedBuffer(len(val))
|
|
||||||
copy(makeSlice(ptr, uint(len(val))), val)
|
|
||||||
case common.Address:
|
|
||||||
ptr := ctx.PushFixedBuffer(20)
|
|
||||||
copy(makeSlice(ptr, 20), val[:])
|
|
||||||
case *big.Int:
|
|
||||||
pushBigInt(val, ctx)
|
|
||||||
case int:
|
|
||||||
ctx.PushInt(val)
|
|
||||||
case uint:
|
|
||||||
ctx.PushUint(val)
|
|
||||||
case common.Hash:
|
|
||||||
ptr := ctx.PushFixedBuffer(32)
|
|
||||||
copy(makeSlice(ptr, 32), val[:])
|
|
||||||
default:
|
|
||||||
panic(fmt.Sprintf("unsupported type: %T", val))
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,4 +1,4 @@
|
|||||||
// Copyright 2017 The go-ethereum Authors
|
// Copyright 2022 The go-ethereum Authors
|
||||||
// This file is part of the go-ethereum library.
|
// This file is part of the go-ethereum library.
|
||||||
//
|
//
|
||||||
// The go-ethereum library is free software: you can redistribute it and/or modify
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
||||||
@ -82,20 +82,10 @@ func runTrace(tracer tracers.Tracer, vmctx *vmContext, chaincfg *params.ChainCon
|
|||||||
return tracer.GetResult()
|
return tracer.GetResult()
|
||||||
}
|
}
|
||||||
|
|
||||||
type tracerCtor = func(string, *tracers.Context) (tracers.Tracer, error)
|
func TestTracer(t *testing.T) {
|
||||||
|
|
||||||
func TestDuktapeTracer(t *testing.T) {
|
|
||||||
testTracer(t, newJsTracer)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestGojaTracer(t *testing.T) {
|
|
||||||
testTracer(t, newGojaTracer)
|
|
||||||
}
|
|
||||||
|
|
||||||
func testTracer(t *testing.T, newTracer tracerCtor) {
|
|
||||||
execTracer := func(code string) ([]byte, string) {
|
execTracer := func(code string) ([]byte, string) {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
tracer, err := newTracer(code, nil)
|
tracer, err := newJsTracer(code, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@ -112,13 +102,16 @@ func testTracer(t *testing.T, newTracer tracerCtor) {
|
|||||||
}{
|
}{
|
||||||
{ // tests that we don't panic on bad arguments to memory access
|
{ // tests that we don't panic on bad arguments to memory access
|
||||||
code: "{depths: [], step: function(log) { this.depths.push(log.memory.slice(-1,-2)); }, fault: function() {}, result: function() { return this.depths; }}",
|
code: "{depths: [], step: function(log) { this.depths.push(log.memory.slice(-1,-2)); }, fault: function() {}, result: function() { return this.depths; }}",
|
||||||
want: `[{},{},{}]`,
|
want: ``,
|
||||||
|
fail: "Tracer accessed out of bound memory: offset -1, end -2 at step (<eval>:1:53(15)) in server-side tracer function 'step'",
|
||||||
}, { // tests that we don't panic on bad arguments to stack peeks
|
}, { // tests that we don't panic on bad arguments to stack peeks
|
||||||
code: "{depths: [], step: function(log) { this.depths.push(log.stack.peek(-1)); }, fault: function() {}, result: function() { return this.depths; }}",
|
code: "{depths: [], step: function(log) { this.depths.push(log.stack.peek(-1)); }, fault: function() {}, result: function() { return this.depths; }}",
|
||||||
want: `["0","0","0"]`,
|
want: ``,
|
||||||
|
fail: "Tracer accessed out of bound stack: size 0, index -1 at step (<eval>:1:53(13)) in server-side tracer function 'step'",
|
||||||
}, { // tests that we don't panic on bad arguments to memory getUint
|
}, { // tests that we don't panic on bad arguments to memory getUint
|
||||||
code: "{ depths: [], step: function(log, db) { this.depths.push(log.memory.getUint(-64));}, fault: function() {}, result: function() { return this.depths; }}",
|
code: "{ depths: [], step: function(log, db) { this.depths.push(log.memory.getUint(-64));}, fault: function() {}, result: function() { return this.depths; }}",
|
||||||
want: `["0","0","0"]`,
|
want: ``,
|
||||||
|
fail: "Tracer accessed out of bound memory: available 0, offset -64, size 32 at step (<eval>:1:58(13)) in server-side tracer function 'step'",
|
||||||
}, { // tests some general counting
|
}, { // tests some general counting
|
||||||
code: "{count: 0, step: function() { this.count += 1; }, fault: function() {}, result: function() { return this.count; }}",
|
code: "{count: 0, step: function() { this.count += 1; }, fault: function() {}, result: function() { return this.count; }}",
|
||||||
want: `3`,
|
want: `3`,
|
||||||
@ -154,18 +147,9 @@ func testTracer(t *testing.T, newTracer tracerCtor) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestHaltDuktape(t *testing.T) {
|
func TestHalt(t *testing.T) {
|
||||||
t.Skip("duktape doesn't support abortion")
|
|
||||||
testHalt(t, newJsTracer)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestHaltGoja(t *testing.T) {
|
|
||||||
testHalt(t, newGojaTracer)
|
|
||||||
}
|
|
||||||
|
|
||||||
func testHalt(t *testing.T, newTracer tracerCtor) {
|
|
||||||
timeout := errors.New("stahp")
|
timeout := errors.New("stahp")
|
||||||
tracer, err := newTracer("{step: function() { while(1); }, result: function() { return null; }, fault: function(){}}", nil)
|
tracer, err := newJsTracer("{step: function() { while(1); }, result: function() { return null; }, fault: function(){}}", nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@ -178,16 +162,8 @@ func testHalt(t *testing.T, newTracer tracerCtor) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestHaltBetweenStepsDuktape(t *testing.T) {
|
func TestHaltBetweenSteps(t *testing.T) {
|
||||||
testHaltBetweenSteps(t, newJsTracer)
|
tracer, err := newJsTracer("{step: function() {}, fault: function() {}, result: function() { return null; }}", nil)
|
||||||
}
|
|
||||||
|
|
||||||
func TestHaltBetweenStepsGoja(t *testing.T) {
|
|
||||||
testHaltBetweenSteps(t, newGojaTracer)
|
|
||||||
}
|
|
||||||
|
|
||||||
func testHaltBetweenSteps(t *testing.T, newTracer tracerCtor) {
|
|
||||||
tracer, err := newTracer("{step: function() {}, fault: function() {}, result: function() { return null; }}", nil)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@ -206,20 +182,12 @@ func testHaltBetweenSteps(t *testing.T, newTracer tracerCtor) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestNoStepExecDuktape(t *testing.T) {
|
|
||||||
testNoStepExec(t, newJsTracer)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestNoStepExecGoja(t *testing.T) {
|
|
||||||
testNoStepExec(t, newGojaTracer)
|
|
||||||
}
|
|
||||||
|
|
||||||
// testNoStepExec tests a regular value transfer (no exec), and accessing the statedb
|
// testNoStepExec tests a regular value transfer (no exec), and accessing the statedb
|
||||||
// in 'result'
|
// in 'result'
|
||||||
func testNoStepExec(t *testing.T, newTracer tracerCtor) {
|
func TestNoStepExec(t *testing.T) {
|
||||||
execTracer := func(code string) []byte {
|
execTracer := func(code string) []byte {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
tracer, err := newTracer(code, nil)
|
tracer, err := newJsTracer(code, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@ -247,21 +215,13 @@ func testNoStepExec(t *testing.T, newTracer tracerCtor) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestIsPrecompileDuktape(t *testing.T) {
|
func TestIsPrecompile(t *testing.T) {
|
||||||
testIsPrecompile(t, newJsTracer)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestIsPrecompileGoja(t *testing.T) {
|
|
||||||
testIsPrecompile(t, newGojaTracer)
|
|
||||||
}
|
|
||||||
|
|
||||||
func testIsPrecompile(t *testing.T, newTracer tracerCtor) {
|
|
||||||
chaincfg := ¶ms.ChainConfig{ChainID: big.NewInt(1), HomesteadBlock: big.NewInt(0), DAOForkBlock: nil, DAOForkSupport: false, EIP150Block: big.NewInt(0), EIP150Hash: common.Hash{}, EIP155Block: big.NewInt(0), EIP158Block: big.NewInt(0), ByzantiumBlock: big.NewInt(100), ConstantinopleBlock: big.NewInt(0), PetersburgBlock: big.NewInt(0), IstanbulBlock: big.NewInt(200), MuirGlacierBlock: big.NewInt(0), BerlinBlock: big.NewInt(300), LondonBlock: big.NewInt(0), TerminalTotalDifficulty: nil, Ethash: new(params.EthashConfig), Clique: nil}
|
chaincfg := ¶ms.ChainConfig{ChainID: big.NewInt(1), HomesteadBlock: big.NewInt(0), DAOForkBlock: nil, DAOForkSupport: false, EIP150Block: big.NewInt(0), EIP150Hash: common.Hash{}, EIP155Block: big.NewInt(0), EIP158Block: big.NewInt(0), ByzantiumBlock: big.NewInt(100), ConstantinopleBlock: big.NewInt(0), PetersburgBlock: big.NewInt(0), IstanbulBlock: big.NewInt(200), MuirGlacierBlock: big.NewInt(0), BerlinBlock: big.NewInt(300), LondonBlock: big.NewInt(0), TerminalTotalDifficulty: nil, Ethash: new(params.EthashConfig), Clique: nil}
|
||||||
chaincfg.ByzantiumBlock = big.NewInt(100)
|
chaincfg.ByzantiumBlock = big.NewInt(100)
|
||||||
chaincfg.IstanbulBlock = big.NewInt(200)
|
chaincfg.IstanbulBlock = big.NewInt(200)
|
||||||
chaincfg.BerlinBlock = big.NewInt(300)
|
chaincfg.BerlinBlock = big.NewInt(300)
|
||||||
txCtx := vm.TxContext{GasPrice: big.NewInt(100000)}
|
txCtx := vm.TxContext{GasPrice: big.NewInt(100000)}
|
||||||
tracer, err := newTracer("{addr: toAddress('0000000000000000000000000000000000000009'), res: null, step: function() { this.res = isPrecompiled(this.addr); }, fault: function() {}, result: function() { return this.res; }}", nil)
|
tracer, err := newJsTracer("{addr: toAddress('0000000000000000000000000000000000000009'), res: null, step: function() { this.res = isPrecompiled(this.addr); }, fault: function() {}, result: function() { return this.res; }}", nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@ -275,7 +235,7 @@ func testIsPrecompile(t *testing.T, newTracer tracerCtor) {
|
|||||||
t.Errorf("Tracer should not consider blake2f as precompile in byzantium")
|
t.Errorf("Tracer should not consider blake2f as precompile in byzantium")
|
||||||
}
|
}
|
||||||
|
|
||||||
tracer, _ = newTracer("{addr: toAddress('0000000000000000000000000000000000000009'), res: null, step: function() { this.res = isPrecompiled(this.addr); }, fault: function() {}, result: function() { return this.res; }}", nil)
|
tracer, _ = newJsTracer("{addr: toAddress('0000000000000000000000000000000000000009'), res: null, step: function() { this.res = isPrecompiled(this.addr); }, fault: function() {}, result: function() { return this.res; }}", nil)
|
||||||
blockCtx = vm.BlockContext{BlockNumber: big.NewInt(250)}
|
blockCtx = vm.BlockContext{BlockNumber: big.NewInt(250)}
|
||||||
res, err = runTrace(tracer, &vmContext{blockCtx, txCtx}, chaincfg)
|
res, err = runTrace(tracer, &vmContext{blockCtx, txCtx}, chaincfg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -286,24 +246,16 @@ func testIsPrecompile(t *testing.T, newTracer tracerCtor) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestEnterExitDuktape(t *testing.T) {
|
func TestEnterExit(t *testing.T) {
|
||||||
testEnterExit(t, newJsTracer)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestEnterExitGoja(t *testing.T) {
|
|
||||||
testEnterExit(t, newGojaTracer)
|
|
||||||
}
|
|
||||||
|
|
||||||
func testEnterExit(t *testing.T, newTracer tracerCtor) {
|
|
||||||
// test that either both or none of enter() and exit() are defined
|
// test that either both or none of enter() and exit() are defined
|
||||||
if _, err := newTracer("{step: function() {}, fault: function() {}, result: function() { return null; }, enter: function() {}}", new(tracers.Context)); err == nil {
|
if _, err := newJsTracer("{step: function() {}, fault: function() {}, result: function() { return null; }, enter: function() {}}", new(tracers.Context)); err == nil {
|
||||||
t.Fatal("tracer creation should've failed without exit() definition")
|
t.Fatal("tracer creation should've failed without exit() definition")
|
||||||
}
|
}
|
||||||
if _, err := newTracer("{step: function() {}, fault: function() {}, result: function() { return null; }, enter: function() {}, exit: function() {}}", new(tracers.Context)); err != nil {
|
if _, err := newJsTracer("{step: function() {}, fault: function() {}, result: function() { return null; }, enter: function() {}, exit: function() {}}", new(tracers.Context)); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
// test that the enter and exit method are correctly invoked and the values passed
|
// test that the enter and exit method are correctly invoked and the values passed
|
||||||
tracer, err := newTracer("{enters: 0, exits: 0, enterGas: 0, gasUsed: 0, step: function() {}, fault: function() {}, result: function() { return {enters: this.enters, exits: this.exits, enterGas: this.enterGas, gasUsed: this.gasUsed} }, enter: function(frame) { this.enters++; this.enterGas = frame.getGas(); }, exit: function(res) { this.exits++; this.gasUsed = res.getGasUsed(); }}", new(tracers.Context))
|
tracer, err := newJsTracer("{enters: 0, exits: 0, enterGas: 0, gasUsed: 0, step: function() {}, fault: function() {}, result: function() { return {enters: this.enters, exits: this.exits, enterGas: this.enterGas, gasUsed: this.gasUsed} }, enter: function(frame) { this.enters++; this.enterGas = frame.getGas(); }, exit: function(res) { this.exits++; this.gasUsed = res.getGasUsed(); }}", new(tracers.Context))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@ -322,20 +274,3 @@ func testEnterExit(t *testing.T, newTracer tracerCtor) {
|
|||||||
t.Errorf("Number of invocations of enter() and exit() is wrong. Have %s, want %s\n", have, want)
|
t.Errorf("Number of invocations of enter() and exit() is wrong. Have %s, want %s\n", have, want)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Tests too deep object / serialization crash for duktape
|
|
||||||
func TestRecursionLimit(t *testing.T) {
|
|
||||||
code := "{step: function() {}, fault: function() {}, result: function() { var o={}; var x=o; for (var i=0; i<1000; i++){ o.foo={}; o=o.foo; } return x; }}"
|
|
||||||
fail := "RangeError: json encode recursion limit in server-side tracer function 'result'"
|
|
||||||
tracer, err := newJsTracer(code, nil)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
got := ""
|
|
||||||
if _, err := runTrace(tracer, testCtx(), params.TestChainConfig); err != nil {
|
|
||||||
got = err.Error()
|
|
||||||
}
|
|
||||||
if got != fail {
|
|
||||||
t.Errorf("expected error to be '%s' got '%s'\n", fail, got)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
1
go.mod
1
go.mod
@ -70,7 +70,6 @@ require (
|
|||||||
golang.org/x/time v0.0.0-20210220033141-f8bda1e9f3ba
|
golang.org/x/time v0.0.0-20210220033141-f8bda1e9f3ba
|
||||||
golang.org/x/tools v0.1.0
|
golang.org/x/tools v0.1.0
|
||||||
gopkg.in/natefinch/npipe.v2 v2.0.0-20160621034901-c1b8fa8bdcce
|
gopkg.in/natefinch/npipe.v2 v2.0.0-20160621034901-c1b8fa8bdcce
|
||||||
gopkg.in/olebedev/go-duktape.v3 v3.0.0-20200619000410-60c24ae608a6
|
|
||||||
gopkg.in/urfave/cli.v1 v1.20.0
|
gopkg.in/urfave/cli.v1 v1.20.0
|
||||||
gotest.tools v2.2.0+incompatible // indirect
|
gotest.tools v2.2.0+incompatible // indirect
|
||||||
)
|
)
|
||||||
|
4
go.sum
4
go.sum
@ -109,8 +109,6 @@ github.com/dnaeon/go-vcr v1.2.0 h1:zHCHvJYTMh1N7xnV7zf1m1GPBF9Ad0Jk/whtQ1663qI=
|
|||||||
github.com/dnaeon/go-vcr v1.2.0/go.mod h1:R4UdLID7HZT3taECzJs4YgbbH6PIGXB6W/sc5OLb6RQ=
|
github.com/dnaeon/go-vcr v1.2.0/go.mod h1:R4UdLID7HZT3taECzJs4YgbbH6PIGXB6W/sc5OLb6RQ=
|
||||||
github.com/docker/docker v1.4.2-0.20180625184442-8e610b2b55bf h1:sh8rkQZavChcmakYiSlqu2425CHyFXLZZnvm7PDpU8M=
|
github.com/docker/docker v1.4.2-0.20180625184442-8e610b2b55bf h1:sh8rkQZavChcmakYiSlqu2425CHyFXLZZnvm7PDpU8M=
|
||||||
github.com/docker/docker v1.4.2-0.20180625184442-8e610b2b55bf/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk=
|
github.com/docker/docker v1.4.2-0.20180625184442-8e610b2b55bf/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk=
|
||||||
github.com/dop251/goja v0.0.0-20211011172007-d99e4b8cbf48 h1:iZOop7pqsg+56twTopWgwCGxdB5SI2yDO8Ti7eTRliQ=
|
|
||||||
github.com/dop251/goja v0.0.0-20211011172007-d99e4b8cbf48/go.mod h1:R9ET47fwRVRPZnOGvHxxhuZcbrMCuiqOz3Rlrh4KSnk=
|
|
||||||
github.com/dop251/goja v0.0.0-20220405120441-9037c2b61cbf h1:Yt+4K30SdjOkRoRRm3vYNQgR+/ZIy0RmeUDZo7Y8zeQ=
|
github.com/dop251/goja v0.0.0-20220405120441-9037c2b61cbf h1:Yt+4K30SdjOkRoRRm3vYNQgR+/ZIy0RmeUDZo7Y8zeQ=
|
||||||
github.com/dop251/goja v0.0.0-20220405120441-9037c2b61cbf/go.mod h1:R9ET47fwRVRPZnOGvHxxhuZcbrMCuiqOz3Rlrh4KSnk=
|
github.com/dop251/goja v0.0.0-20220405120441-9037c2b61cbf/go.mod h1:R9ET47fwRVRPZnOGvHxxhuZcbrMCuiqOz3Rlrh4KSnk=
|
||||||
github.com/dop251/goja_nodejs v0.0.0-20210225215109-d91c329300e7/go.mod h1:hn7BA7c8pLvoGndExHudxTDKZ84Pyvv+90pbBjbTz0Y=
|
github.com/dop251/goja_nodejs v0.0.0-20210225215109-d91c329300e7/go.mod h1:hn7BA7c8pLvoGndExHudxTDKZ84Pyvv+90pbBjbTz0Y=
|
||||||
@ -632,8 +630,6 @@ gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
|
|||||||
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
|
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
|
||||||
gopkg.in/natefinch/npipe.v2 v2.0.0-20160621034901-c1b8fa8bdcce h1:+JknDZhAj8YMt7GC73Ei8pv4MzjDUNPHgQWJdtMAaDU=
|
gopkg.in/natefinch/npipe.v2 v2.0.0-20160621034901-c1b8fa8bdcce h1:+JknDZhAj8YMt7GC73Ei8pv4MzjDUNPHgQWJdtMAaDU=
|
||||||
gopkg.in/natefinch/npipe.v2 v2.0.0-20160621034901-c1b8fa8bdcce/go.mod h1:5AcXVHNjg+BDxry382+8OKon8SEWiKktQR07RKPsv1c=
|
gopkg.in/natefinch/npipe.v2 v2.0.0-20160621034901-c1b8fa8bdcce/go.mod h1:5AcXVHNjg+BDxry382+8OKon8SEWiKktQR07RKPsv1c=
|
||||||
gopkg.in/olebedev/go-duktape.v3 v3.0.0-20200619000410-60c24ae608a6 h1:a6cXbcDDUkSBlpnkWV1bJ+vv3mOgQEltEJ2rPxroVu0=
|
|
||||||
gopkg.in/olebedev/go-duktape.v3 v3.0.0-20200619000410-60c24ae608a6/go.mod h1:uAJfkITjFhyEEuUfm7bsmCZRbW5WRq8s9EY8HZ6hCns=
|
|
||||||
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ=
|
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ=
|
||||||
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
|
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
|
||||||
gopkg.in/urfave/cli.v1 v1.20.0 h1:NdAVW6RYxDif9DhDHaAortIu956m2c0v+09AZBPTbE0=
|
gopkg.in/urfave/cli.v1 v1.20.0 h1:NdAVW6RYxDif9DhDHaAortIu956m2c0v+09AZBPTbE0=
|
||||||
|
Loading…
Reference in New Issue
Block a user