mirror of
https://gitlab.com/pulsechaincom/erigon-pulse.git
synced 2024-12-22 03:30:37 +00:00
Native tracers step 7 - restructure js tracer files (#6415)
Co-authored-by: Alex Sharp <alexsharp@Alexs-MacBook-Pro-2.local>
This commit is contained in:
parent
f655e337d1
commit
f364eff389
@ -56,6 +56,9 @@ import (
|
||||
"github.com/ledgerwatch/erigon/turbo/rpchelper"
|
||||
"github.com/ledgerwatch/erigon/turbo/services"
|
||||
"github.com/ledgerwatch/erigon/turbo/snapshotsync"
|
||||
|
||||
// Force-load native and js packages, to trigger registration
|
||||
_ "github.com/ledgerwatch/erigon/eth/tracers/js"
|
||||
)
|
||||
|
||||
var rootCmd = &cobra.Command{
|
||||
|
@ -15,6 +15,9 @@ import (
|
||||
"github.com/ledgerwatch/erigon/rpc/rpccfg"
|
||||
"github.com/ledgerwatch/erigon/turbo/snapshotsync"
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
// Force-load native and js packages, to trigger registration
|
||||
_ "github.com/ledgerwatch/erigon/eth/tracers/js"
|
||||
)
|
||||
|
||||
/*
|
||||
@ -94,7 +97,7 @@ func TestGeneratedDebugApi(t *testing.T) {
|
||||
],
|
||||
"from": "0x71562b71999873db5b286df957af199ec94617f7",
|
||||
"gas": "0x7120",
|
||||
"gasUsed": "0x161c",
|
||||
"gasUsed": "0x684c",
|
||||
"input": "0x01000100",
|
||||
"output": "0x",
|
||||
"to": "0x00000000000000000000000000000000000002ff",
|
||||
|
@ -479,7 +479,7 @@ func (ot *OeTracer) CaptureState(pc uint64, op vm.OpCode, gas, cost uint64, scop
|
||||
setMem = true
|
||||
}
|
||||
if setMem && ot.lastMemLen > 0 {
|
||||
cpy := memory.GetCopy(ot.lastMemOff, ot.lastMemLen)
|
||||
cpy := memory.GetCopy(int64(ot.lastMemOff), int64(ot.lastMemLen))
|
||||
if len(cpy) == 0 {
|
||||
cpy = make([]byte, ot.lastMemLen)
|
||||
}
|
||||
@ -494,7 +494,7 @@ func (ot *OeTracer) CaptureState(pc uint64, op vm.OpCode, gas, cost uint64, scop
|
||||
ot.lastOffStack.Ex.Push = []string{}
|
||||
}
|
||||
if ot.lastMemLen > 0 && memory != nil {
|
||||
cpy := memory.GetCopy(ot.lastMemOff, ot.lastMemLen)
|
||||
cpy := memory.GetCopy(int64(ot.lastMemOff), int64(ot.lastMemLen))
|
||||
if len(cpy) == 0 {
|
||||
cpy = make([]byte, ot.lastMemLen)
|
||||
}
|
||||
|
@ -264,7 +264,7 @@ func opSAR(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte
|
||||
|
||||
func opKeccak256(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
|
||||
offset, size := scope.Stack.Pop(), scope.Stack.Peek()
|
||||
data := scope.Memory.GetPtr(offset.Uint64(), size.Uint64())
|
||||
data := scope.Memory.GetPtr(int64(offset.Uint64()), int64(size.Uint64()))
|
||||
|
||||
if interpreter.hasher == nil {
|
||||
interpreter.hasher = sha3.NewLegacyKeccak256().(keccakState)
|
||||
@ -533,7 +533,7 @@ func opPop(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte
|
||||
func opMload(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
|
||||
v := scope.Stack.Peek()
|
||||
offset := v.Uint64()
|
||||
v.SetBytes(scope.Memory.GetPtr(offset, 32))
|
||||
v.SetBytes(scope.Memory.GetPtr(int64(offset), 32))
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
@ -640,7 +640,7 @@ func opCreate(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]b
|
||||
value = scope.Stack.Pop()
|
||||
offset = scope.Stack.Pop()
|
||||
size = scope.Stack.Peek()
|
||||
input = scope.Memory.GetCopy(offset.Uint64(), size.Uint64())
|
||||
input = scope.Memory.GetCopy(int64(offset.Uint64()), int64(size.Uint64()))
|
||||
gas = scope.Contract.Gas
|
||||
)
|
||||
if interpreter.evm.ChainRules().IsTangerineWhistle {
|
||||
@ -682,7 +682,7 @@ func opCreate2(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]
|
||||
endowment = scope.Stack.Pop()
|
||||
offset, size = scope.Stack.Pop(), scope.Stack.Pop()
|
||||
salt = scope.Stack.Pop()
|
||||
input = scope.Memory.GetCopy(offset.Uint64(), size.Uint64())
|
||||
input = scope.Memory.GetCopy(int64(offset.Uint64()), int64(size.Uint64()))
|
||||
gas = scope.Contract.Gas
|
||||
)
|
||||
|
||||
@ -721,7 +721,7 @@ func opCall(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byt
|
||||
addr, value, inOffset, inSize, retOffset, retSize := stack.Pop(), stack.Pop(), stack.Pop(), stack.Pop(), stack.Pop(), stack.Pop()
|
||||
toAddr := common.Address(addr.Bytes20())
|
||||
// Get the arguments from the memory.
|
||||
args := scope.Memory.GetPtr(inOffset.Uint64(), inSize.Uint64())
|
||||
args := scope.Memory.GetPtr(int64(inOffset.Uint64()), int64(inSize.Uint64()))
|
||||
|
||||
if !value.IsZero() {
|
||||
if interpreter.readOnly {
|
||||
@ -759,7 +759,7 @@ func opCallCode(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([
|
||||
addr, value, inOffset, inSize, retOffset, retSize := stack.Pop(), stack.Pop(), stack.Pop(), stack.Pop(), stack.Pop(), stack.Pop()
|
||||
toAddr := common.Address(addr.Bytes20())
|
||||
// Get arguments from the memory.
|
||||
args := scope.Memory.GetPtr(inOffset.Uint64(), inSize.Uint64())
|
||||
args := scope.Memory.GetPtr(int64(inOffset.Uint64()), int64(inSize.Uint64()))
|
||||
|
||||
if !value.IsZero() {
|
||||
gas += params.CallStipend
|
||||
@ -793,7 +793,7 @@ func opDelegateCall(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext
|
||||
addr, inOffset, inSize, retOffset, retSize := stack.Pop(), stack.Pop(), stack.Pop(), stack.Pop(), stack.Pop()
|
||||
toAddr := common.Address(addr.Bytes20())
|
||||
// Get arguments from the memory.
|
||||
args := scope.Memory.GetPtr(inOffset.Uint64(), inSize.Uint64())
|
||||
args := scope.Memory.GetPtr(int64(inOffset.Uint64()), int64(inSize.Uint64()))
|
||||
|
||||
ret, returnGas, err := interpreter.evm.DelegateCall(scope.Contract, toAddr, args, gas)
|
||||
if err != nil {
|
||||
@ -823,7 +823,7 @@ func opStaticCall(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext)
|
||||
addr, inOffset, inSize, retOffset, retSize := stack.Pop(), stack.Pop(), stack.Pop(), stack.Pop(), stack.Pop()
|
||||
toAddr := common.Address(addr.Bytes20())
|
||||
// Get arguments from the memory.
|
||||
args := scope.Memory.GetPtr(inOffset.Uint64(), inSize.Uint64())
|
||||
args := scope.Memory.GetPtr(int64(inOffset.Uint64()), int64(inSize.Uint64()))
|
||||
|
||||
ret, returnGas, err := interpreter.evm.StaticCall(scope.Contract, toAddr, args, gas)
|
||||
if err != nil {
|
||||
@ -845,13 +845,13 @@ func opStaticCall(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext)
|
||||
|
||||
func opReturn(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
|
||||
offset, size := scope.Stack.Pop(), scope.Stack.Pop()
|
||||
ret := scope.Memory.GetPtr(offset.Uint64(), size.Uint64())
|
||||
ret := scope.Memory.GetPtr(int64(offset.Uint64()), int64(size.Uint64()))
|
||||
return ret, errStopToken
|
||||
}
|
||||
|
||||
func opRevert(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
|
||||
offset, size := scope.Stack.Pop(), scope.Stack.Pop()
|
||||
ret := scope.Memory.GetPtr(offset.Uint64(), size.Uint64())
|
||||
ret := scope.Memory.GetPtr(int64(offset.Uint64()), int64(size.Uint64()))
|
||||
interpreter.returnData = ret
|
||||
return ret, ErrExecutionReverted
|
||||
}
|
||||
@ -896,7 +896,7 @@ func makeLog(size int) executionFunc {
|
||||
topics[i] = addr.Bytes32()
|
||||
}
|
||||
|
||||
d := scope.Memory.GetCopy(mStart.Uint64(), mSize.Uint64())
|
||||
d := scope.Memory.GetCopy(int64(mStart.Uint64()), int64(mSize.Uint64()))
|
||||
interpreter.evm.IntraBlockState().AddLog(&types.Log{
|
||||
Address: scope.Contract.Address(),
|
||||
Topics: topics,
|
||||
|
@ -69,12 +69,12 @@ func (m *Memory) Resize(size uint64) {
|
||||
}
|
||||
|
||||
// GetCopy returns offset + size as a new slice
|
||||
func (m *Memory) GetCopy(offset, size uint64) (cpy []byte) {
|
||||
func (m *Memory) GetCopy(offset, size int64) (cpy []byte) {
|
||||
if size == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
if uint64(len(m.store)) > offset {
|
||||
if len(m.store) > int(offset) {
|
||||
cpy = make([]byte, size)
|
||||
copy(cpy, m.store[offset:])
|
||||
|
||||
@ -85,12 +85,12 @@ func (m *Memory) GetCopy(offset, size uint64) (cpy []byte) {
|
||||
}
|
||||
|
||||
// GetPtr returns the offset + size
|
||||
func (m *Memory) GetPtr(offset, size uint64) []byte {
|
||||
func (m *Memory) GetPtr(offset, size int64) []byte {
|
||||
if size == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
if uint64(len(m.store)) > offset {
|
||||
if len(m.store) > int(offset) {
|
||||
return m.store[offset : offset+size]
|
||||
}
|
||||
|
||||
|
File diff suppressed because one or more lines are too long
@ -1,21 +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/>.
|
||||
|
||||
//go:generate go-bindata -nometadata -o assets.go -pkg tracers -ignore tracers.go -ignore assets.go ./...
|
||||
//go:generate gofmt -s -w assets.go
|
||||
|
||||
// Package tracers contains the actual JavaScript tracer assets.
|
||||
package tracers
|
20
eth/tracers/js/bigint.go
Normal file
20
eth/tracers/js/bigint.go
Normal file
File diff suppressed because one or more lines are too long
975
eth/tracers/js/goja.go
Normal file
975
eth/tracers/js/goja.go
Normal file
@ -0,0 +1,975 @@
|
||||
// Copyright 2022 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
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"math/big"
|
||||
|
||||
"github.com/dop251/goja"
|
||||
"github.com/holiman/uint256"
|
||||
|
||||
"github.com/ledgerwatch/erigon/common"
|
||||
"github.com/ledgerwatch/erigon/common/hexutil"
|
||||
"github.com/ledgerwatch/erigon/core/vm"
|
||||
"github.com/ledgerwatch/erigon/core/vm/evmtypes"
|
||||
"github.com/ledgerwatch/erigon/core/vm/stack"
|
||||
"github.com/ledgerwatch/erigon/crypto"
|
||||
"github.com/ledgerwatch/erigon/eth/tracers"
|
||||
jsassets "github.com/ledgerwatch/erigon/eth/tracers/js/internal/tracers"
|
||||
)
|
||||
|
||||
const (
|
||||
memoryPadLimit = 1024 * 1024
|
||||
)
|
||||
|
||||
var assetTracers = make(map[string]string)
|
||||
|
||||
// init retrieves the JavaScript transaction tracers included in go-ethereum.
|
||||
func init() {
|
||||
var err error
|
||||
assetTracers, err = jsassets.Load()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
tracers.RegisterLookup(true, newJsTracer)
|
||||
}
|
||||
|
||||
// bigIntProgram is compiled once and the exported function mostly invoked to convert
|
||||
// hex strings into big ints.
|
||||
var bigIntProgram = goja.MustCompile("bigInt", bigIntegerJS, false)
|
||||
|
||||
type toBigFn = func(vm *goja.Runtime, val string) (goja.Value, error)
|
||||
type toBufFn = func(vm *goja.Runtime, val []byte) (goja.Value, error)
|
||||
type fromBufFn = func(vm *goja.Runtime, buf goja.Value, allowString bool) ([]byte, error)
|
||||
|
||||
func toBuf(vm *goja.Runtime, bufType goja.Value, val []byte) (goja.Value, error) {
|
||||
// bufType is usually Uint8Array. This is equivalent to `new Uint8Array(val)` in JS.
|
||||
return vm.New(bufType, vm.ToValue(vm.NewArrayBuffer(val)))
|
||||
}
|
||||
|
||||
func fromBuf(vm *goja.Runtime, bufType goja.Value, buf goja.Value, allowString bool) ([]byte, error) {
|
||||
obj := buf.ToObject(vm)
|
||||
switch obj.ClassName() {
|
||||
case "String":
|
||||
if !allowString {
|
||||
break
|
||||
}
|
||||
return common.FromHex(obj.String()), nil
|
||||
|
||||
case "Array":
|
||||
var b []byte
|
||||
if err := vm.ExportTo(buf, &b); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return b, nil
|
||||
|
||||
case "Object":
|
||||
if !obj.Get("constructor").SameAs(bufType) {
|
||||
break
|
||||
}
|
||||
b := obj.Get("buffer").Export().(goja.ArrayBuffer).Bytes()
|
||||
return b, nil
|
||||
}
|
||||
return nil, fmt.Errorf("invalid buffer type")
|
||||
}
|
||||
|
||||
// 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
|
||||
env *vm.EVM
|
||||
toBig toBigFn // Converts a hex string into a JS bigint
|
||||
toBuf toBufFn // Converts a []byte into a JS buffer
|
||||
fromBuf fromBufFn // Converts an array, hex string or Uint8Array to a []byte
|
||||
ctx map[string]goja.Value // KV-bag passed to JS in `result`
|
||||
activePrecompiles []common.Address // List of active precompiles at current block
|
||||
traceStep bool // True if tracer object exposes a `step()` method
|
||||
traceFrame bool // True if tracer object exposes the `enter()` and `exit()` methods
|
||||
gasLimit uint64 // Amount of gas bought for the whole tx
|
||||
err error // Any error that should stop tracing
|
||||
obj *goja.Object // Trace object
|
||||
|
||||
// Methods exposed by tracer
|
||||
result goja.Callable
|
||||
fault goja.Callable
|
||||
step goja.Callable
|
||||
enter goja.Callable
|
||||
exit goja.Callable
|
||||
|
||||
// Underlying structs being passed into JS
|
||||
log *steplog
|
||||
frame *callframe
|
||||
frameResult *callframeResult
|
||||
|
||||
// Goja-wrapping of types prepared for JS consumption
|
||||
logValue goja.Value
|
||||
dbValue goja.Value
|
||||
frameValue goja.Value
|
||||
frameResultValue goja.Value
|
||||
}
|
||||
|
||||
// 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, cfg json.RawMessage) (tracers.Tracer, error) {
|
||||
if c, ok := assetTracers[code]; ok {
|
||||
code = c
|
||||
}
|
||||
vm := goja.New()
|
||||
// By default field names are exported to JS as is, i.e. capitalized.
|
||||
vm.SetFieldNameMapper(goja.UncapFieldNameMapper())
|
||||
t := &jsTracer{
|
||||
vm: vm,
|
||||
ctx: make(map[string]goja.Value),
|
||||
}
|
||||
if ctx == nil {
|
||||
ctx = new(tracers.Context)
|
||||
}
|
||||
if ctx.BlockHash != (common.Hash{}) {
|
||||
t.ctx["blockHash"] = vm.ToValue(ctx.BlockHash.Bytes())
|
||||
if ctx.TxHash != (common.Hash{}) {
|
||||
t.ctx["txIndex"] = vm.ToValue(ctx.TxIndex)
|
||||
t.ctx["txHash"] = vm.ToValue(ctx.TxHash.Bytes())
|
||||
}
|
||||
}
|
||||
|
||||
t.setTypeConverters()
|
||||
t.setBuiltinFunctions()
|
||||
ret, err := vm.RunString("(" + code + ")")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// Check tracer's interface for required and optional methods.
|
||||
obj := ret.ToObject(vm)
|
||||
result, ok := goja.AssertFunction(obj.Get("result"))
|
||||
if !ok {
|
||||
return nil, errors.New("trace object must expose a function result()")
|
||||
}
|
||||
fault, ok := goja.AssertFunction(obj.Get("fault"))
|
||||
if !ok {
|
||||
return nil, errors.New("trace object must expose a function fault()")
|
||||
}
|
||||
step, ok := goja.AssertFunction(obj.Get("step"))
|
||||
t.traceStep = ok
|
||||
enter, hasEnter := goja.AssertFunction(obj.Get("enter"))
|
||||
exit, hasExit := goja.AssertFunction(obj.Get("exit"))
|
||||
if hasEnter != hasExit {
|
||||
return nil, errors.New("trace object must expose either both or none of enter() and exit()")
|
||||
}
|
||||
t.traceFrame = hasEnter
|
||||
t.obj = obj
|
||||
t.step = step
|
||||
t.enter = enter
|
||||
t.exit = exit
|
||||
t.result = result
|
||||
t.fault = fault
|
||||
|
||||
// Pass in config
|
||||
if setup, ok := goja.AssertFunction(obj.Get("setup")); ok {
|
||||
cfgStr := "{}"
|
||||
if cfg != nil {
|
||||
cfgStr = string(cfg)
|
||||
}
|
||||
if _, err := setup(obj, vm.ToValue(cfgStr)); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
// Setup objects carrying data to JS. These are created once and re-used.
|
||||
t.log = &steplog{
|
||||
vm: vm,
|
||||
op: &opObj{vm: vm},
|
||||
memory: &memoryObj{vm: vm, toBig: t.toBig, toBuf: t.toBuf},
|
||||
stack: &stackObj{vm: vm, toBig: t.toBig},
|
||||
contract: &contractObj{vm: vm, toBig: t.toBig, toBuf: t.toBuf},
|
||||
}
|
||||
t.frame = &callframe{vm: vm, toBig: t.toBig, toBuf: t.toBuf}
|
||||
t.frameResult = &callframeResult{vm: vm, toBuf: t.toBuf}
|
||||
t.frameValue = t.frame.setupObject()
|
||||
t.frameResultValue = t.frameResult.setupObject()
|
||||
t.logValue = t.log.setupObject()
|
||||
return t, nil
|
||||
}
|
||||
|
||||
// CaptureTxStart implements the Tracer interface and is invoked at the beginning of
|
||||
// transaction processing.
|
||||
func (t *jsTracer) CaptureTxStart(gasLimit uint64) {
|
||||
t.gasLimit = gasLimit
|
||||
}
|
||||
|
||||
// CaptureTxEnd implements the Tracer interface and is invoked at the end of
|
||||
// transaction processing.
|
||||
func (t *jsTracer) CaptureTxEnd(restGas uint64) {
|
||||
t.ctx["gasUsed"] = t.vm.ToValue(t.gasLimit - restGas)
|
||||
}
|
||||
|
||||
// CaptureStart implements the Tracer interface to initialize the tracing operation.
|
||||
func (t *jsTracer) CaptureStart(env *vm.EVM, from common.Address, to common.Address, precompile, create bool, input []byte, gas uint64, value *uint256.Int, code []byte) {
|
||||
t.env = env
|
||||
db := &dbObj{ibs: env.IntraBlockState(), vm: t.vm, toBig: t.toBig, toBuf: t.toBuf, fromBuf: t.fromBuf}
|
||||
t.dbValue = db.setupObject()
|
||||
if create {
|
||||
t.ctx["type"] = t.vm.ToValue("CREATE")
|
||||
} else {
|
||||
t.ctx["type"] = t.vm.ToValue("CALL")
|
||||
}
|
||||
t.ctx["from"] = t.vm.ToValue(from.Bytes())
|
||||
t.ctx["to"] = t.vm.ToValue(to.Bytes())
|
||||
t.ctx["input"] = t.vm.ToValue(input)
|
||||
t.ctx["gas"] = t.vm.ToValue(gas)
|
||||
t.ctx["gasPrice"] = t.vm.ToValue(env.TxContext().GasPrice.ToBig())
|
||||
valueBig, err := t.toBig(t.vm, value.ToBig().String())
|
||||
if err != nil {
|
||||
t.err = err
|
||||
return
|
||||
}
|
||||
t.ctx["value"] = valueBig
|
||||
t.ctx["block"] = t.vm.ToValue(env.Context().BlockNumber)
|
||||
// Update list of precompiles based on current block
|
||||
rules := env.ChainConfig().Rules(env.Context().BlockNumber, env.Context().Time)
|
||||
t.activePrecompiles = vm.ActivePrecompiles(rules)
|
||||
}
|
||||
|
||||
// CaptureState implements the Tracer interface to trace a single step of VM execution.
|
||||
func (t *jsTracer) CaptureState(pc uint64, op vm.OpCode, gas, cost uint64, scope *vm.ScopeContext, rData []byte, depth int, err error) {
|
||||
if !t.traceStep {
|
||||
return
|
||||
}
|
||||
if t.err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
log := t.log
|
||||
log.op.op = op
|
||||
log.memory.memory = scope.Memory
|
||||
log.stack.stack = scope.Stack
|
||||
log.contract.contract = scope.Contract
|
||||
log.pc = pc
|
||||
log.gas = gas
|
||||
log.cost = cost
|
||||
log.refund = t.env.IntraBlockState().GetRefund()
|
||||
log.depth = depth
|
||||
log.err = err
|
||||
if _, err := t.step(t.obj, t.logValue, t.dbValue); err != nil {
|
||||
t.onError("step", err)
|
||||
}
|
||||
}
|
||||
|
||||
// CaptureFault implements the Tracer interface to trace an execution fault
|
||||
func (t *jsTracer) CaptureFault(pc uint64, op vm.OpCode, gas, cost uint64, scope *vm.ScopeContext, depth int, err error) {
|
||||
if t.err != nil {
|
||||
return
|
||||
}
|
||||
// Other log fields have been already set as part of the last CaptureState.
|
||||
t.log.err = err
|
||||
if _, err := t.fault(t.obj, t.logValue, t.dbValue); err != nil {
|
||||
t.onError("fault", err)
|
||||
}
|
||||
}
|
||||
|
||||
// CaptureEnd is called after the call finishes to finalize the tracing.
|
||||
func (t *jsTracer) CaptureEnd(output []byte, gasUsed uint64, err error) {
|
||||
t.ctx["output"] = t.vm.ToValue(output)
|
||||
if err != nil {
|
||||
t.ctx["error"] = t.vm.ToValue(err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
// CaptureEnter is called when EVM enters a new scope (via call, create or selfdestruct).
|
||||
func (t *jsTracer) CaptureEnter(typ vm.OpCode, from common.Address, to common.Address, precompile, create bool, input []byte, gas uint64, value *uint256.Int, code []byte) {
|
||||
if !t.traceFrame {
|
||||
return
|
||||
}
|
||||
if t.err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
t.frame.typ = typ.String()
|
||||
t.frame.from = from
|
||||
t.frame.to = to
|
||||
t.frame.input = common.CopyBytes(input)
|
||||
t.frame.gas = uint(gas)
|
||||
t.frame.value = nil
|
||||
if value != nil {
|
||||
t.frame.value = new(big.Int).SetBytes(value.Bytes())
|
||||
}
|
||||
|
||||
if _, err := t.enter(t.obj, t.frameValue); err != nil {
|
||||
t.onError("enter", err)
|
||||
}
|
||||
}
|
||||
|
||||
// CaptureExit is called when EVM exits a scope, even if the scope didn't
|
||||
// execute any code.
|
||||
func (t *jsTracer) CaptureExit(output []byte, gasUsed uint64, err error) {
|
||||
if !t.traceFrame {
|
||||
return
|
||||
}
|
||||
|
||||
t.frameResult.gasUsed = uint(gasUsed)
|
||||
t.frameResult.output = common.CopyBytes(output)
|
||||
t.frameResult.err = err
|
||||
|
||||
if _, err := t.exit(t.obj, t.frameResultValue); err != nil {
|
||||
t.onError("exit", err)
|
||||
}
|
||||
}
|
||||
|
||||
// GetResult calls the Javascript 'result' function and returns its value, or any accumulated error
|
||||
func (t *jsTracer) GetResult() (json.RawMessage, error) {
|
||||
ctx := t.vm.ToValue(t.ctx)
|
||||
res, err := t.result(t.obj, ctx, t.dbValue)
|
||||
if err != nil {
|
||||
return nil, wrapError("result", err)
|
||||
}
|
||||
encoded, err := json.Marshal(res)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return json.RawMessage(encoded), t.err
|
||||
}
|
||||
|
||||
// Stop terminates execution of the tracer at the first opportune moment.
|
||||
func (t *jsTracer) Stop(err error) {
|
||||
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()
|
||||
}
|
||||
|
||||
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.
|
||||
// It depends on type converters having been set up.
|
||||
func (t *jsTracer) setBuiltinFunctions() {
|
||||
vm := t.vm
|
||||
// TODO: load console from goja-nodejs
|
||||
vm.Set("toHex", func(v goja.Value) string {
|
||||
b, err := t.fromBuf(vm, v, false)
|
||||
if err != nil {
|
||||
vm.Interrupt(err)
|
||||
return ""
|
||||
}
|
||||
return hexutil.Encode(b)
|
||||
})
|
||||
vm.Set("toWord", func(v goja.Value) goja.Value {
|
||||
// TODO: add test with []byte len < 32 or > 32
|
||||
b, err := t.fromBuf(vm, v, true)
|
||||
if err != nil {
|
||||
vm.Interrupt(err)
|
||||
return nil
|
||||
}
|
||||
b = common.BytesToHash(b).Bytes()
|
||||
res, err := t.toBuf(vm, b)
|
||||
if err != nil {
|
||||
vm.Interrupt(err)
|
||||
return nil
|
||||
}
|
||||
return res
|
||||
})
|
||||
vm.Set("toAddress", func(v goja.Value) goja.Value {
|
||||
a, err := t.fromBuf(vm, v, true)
|
||||
if err != nil {
|
||||
vm.Interrupt(err)
|
||||
return nil
|
||||
}
|
||||
a = common.BytesToAddress(a).Bytes()
|
||||
res, err := t.toBuf(vm, a)
|
||||
if err != nil {
|
||||
vm.Interrupt(err)
|
||||
return nil
|
||||
}
|
||||
return res
|
||||
})
|
||||
vm.Set("toContract", func(from goja.Value, nonce uint) goja.Value {
|
||||
a, err := t.fromBuf(vm, from, true)
|
||||
if err != nil {
|
||||
vm.Interrupt(err)
|
||||
return nil
|
||||
}
|
||||
addr := common.BytesToAddress(a)
|
||||
b := crypto.CreateAddress(addr, uint64(nonce)).Bytes()
|
||||
res, err := t.toBuf(vm, b)
|
||||
if err != nil {
|
||||
vm.Interrupt(err)
|
||||
return nil
|
||||
}
|
||||
return res
|
||||
})
|
||||
vm.Set("toContract2", func(from goja.Value, salt string, initcode goja.Value) goja.Value {
|
||||
a, err := t.fromBuf(vm, from, true)
|
||||
if err != nil {
|
||||
vm.Interrupt(err)
|
||||
return nil
|
||||
}
|
||||
addr := common.BytesToAddress(a)
|
||||
code, err := t.fromBuf(vm, initcode, true)
|
||||
if err != nil {
|
||||
vm.Interrupt(err)
|
||||
return nil
|
||||
}
|
||||
code = common.CopyBytes(code)
|
||||
codeHash := crypto.Keccak256(code)
|
||||
b := crypto.CreateAddress2(addr, common.HexToHash(salt), codeHash).Bytes()
|
||||
res, err := t.toBuf(vm, b)
|
||||
if err != nil {
|
||||
vm.Interrupt(err)
|
||||
return nil
|
||||
}
|
||||
return res
|
||||
})
|
||||
vm.Set("isPrecompiled", func(v goja.Value) bool {
|
||||
a, err := t.fromBuf(vm, v, true)
|
||||
if err != nil {
|
||||
vm.Interrupt(err)
|
||||
return false
|
||||
}
|
||||
addr := common.BytesToAddress(a)
|
||||
for _, p := range t.activePrecompiles {
|
||||
if p == addr {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
})
|
||||
vm.Set("slice", func(slice goja.Value, start, end int) goja.Value {
|
||||
b, err := t.fromBuf(vm, slice, false)
|
||||
if err != nil {
|
||||
vm.Interrupt(err)
|
||||
return nil
|
||||
}
|
||||
if start < 0 || start > end || end > len(b) {
|
||||
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])
|
||||
if err != nil {
|
||||
vm.Interrupt(err)
|
||||
return nil
|
||||
}
|
||||
return res
|
||||
})
|
||||
}
|
||||
|
||||
// setTypeConverters sets up utilities for converting Go types into those
|
||||
// suitable for JS consumption.
|
||||
func (t *jsTracer) setTypeConverters() error {
|
||||
// Inject bigint logic.
|
||||
// TODO: To be replaced after goja adds support for native JS bigint.
|
||||
toBigCode, err := t.vm.RunProgram(bigIntProgram)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// Used to create JS bigint objects from go.
|
||||
toBigFn, ok := goja.AssertFunction(toBigCode)
|
||||
if !ok {
|
||||
return errors.New("failed to bind bigInt func")
|
||||
}
|
||||
toBigWrapper := func(vm *goja.Runtime, val string) (goja.Value, error) {
|
||||
return toBigFn(goja.Undefined(), vm.ToValue(val))
|
||||
}
|
||||
t.toBig = toBigWrapper
|
||||
// NOTE: We need this workaround to create JS buffers because
|
||||
// goja doesn't at the moment expose constructors for typed arrays.
|
||||
//
|
||||
// Cache uint8ArrayType once to be used every time for less overhead.
|
||||
uint8ArrayType := t.vm.Get("Uint8Array")
|
||||
toBufWrapper := func(vm *goja.Runtime, val []byte) (goja.Value, error) {
|
||||
return toBuf(vm, uint8ArrayType, val)
|
||||
}
|
||||
t.toBuf = toBufWrapper
|
||||
fromBufWrapper := func(vm *goja.Runtime, buf goja.Value, allowString bool) ([]byte, error) {
|
||||
return fromBuf(vm, uint8ArrayType, buf, allowString)
|
||||
}
|
||||
t.fromBuf = fromBufWrapper
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t *jsTracer) CaptureAccountRead(account common.Address) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t *jsTracer) CaptureAccountWrite(account common.Address) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t *jsTracer) CaptureSelfDestruct(from common.Address, to common.Address, value *uint256.Int) {}
|
||||
|
||||
type opObj struct {
|
||||
vm *goja.Runtime
|
||||
op vm.OpCode
|
||||
}
|
||||
|
||||
func (o *opObj) ToNumber() int {
|
||||
return int(o.op)
|
||||
}
|
||||
|
||||
func (o *opObj) ToString() string {
|
||||
return o.op.String()
|
||||
}
|
||||
|
||||
func (o *opObj) IsPush() bool {
|
||||
return o.op.IsPush()
|
||||
}
|
||||
|
||||
func (o *opObj) setupObject() *goja.Object {
|
||||
obj := o.vm.NewObject()
|
||||
obj.Set("toNumber", o.vm.ToValue(o.ToNumber))
|
||||
obj.Set("toString", o.vm.ToValue(o.ToString))
|
||||
obj.Set("isPush", o.vm.ToValue(o.IsPush))
|
||||
return obj
|
||||
}
|
||||
|
||||
type memoryObj struct {
|
||||
memory *vm.Memory
|
||||
vm *goja.Runtime
|
||||
toBig toBigFn
|
||||
toBuf toBufFn
|
||||
}
|
||||
|
||||
func (mo *memoryObj) Slice(begin, end int64) goja.Value {
|
||||
b, err := mo.slice(begin, end)
|
||||
if err != nil {
|
||||
mo.vm.Interrupt(err)
|
||||
return nil
|
||||
}
|
||||
res, err := mo.toBuf(mo.vm, b)
|
||||
if err != nil {
|
||||
mo.vm.Interrupt(err)
|
||||
return nil
|
||||
}
|
||||
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)
|
||||
}
|
||||
mlen := mo.memory.Len()
|
||||
if end-int64(mlen) > memoryPadLimit {
|
||||
return nil, fmt.Errorf("tracer reached limit for padding memory slice: end %d, memorySize %d", end, mlen)
|
||||
}
|
||||
slice := make([]byte, end-begin)
|
||||
end = min(end, int64(mo.memory.Len()))
|
||||
ptr := mo.memory.GetPtr(begin, end-begin)
|
||||
copy(slice, ptr)
|
||||
return slice, nil
|
||||
}
|
||||
|
||||
func (mo *memoryObj) GetUint(addr int64) goja.Value {
|
||||
value, err := mo.getUint(addr)
|
||||
if err != nil {
|
||||
mo.vm.Interrupt(err)
|
||||
return nil
|
||||
}
|
||||
res, err := mo.toBig(mo.vm, value.String())
|
||||
if err != nil {
|
||||
mo.vm.Interrupt(err)
|
||||
return nil
|
||||
}
|
||||
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 {
|
||||
return mo.memory.Len()
|
||||
}
|
||||
|
||||
func (m *memoryObj) setupObject() *goja.Object {
|
||||
o := m.vm.NewObject()
|
||||
o.Set("slice", m.vm.ToValue(m.Slice))
|
||||
o.Set("getUint", m.vm.ToValue(m.GetUint))
|
||||
o.Set("length", m.vm.ToValue(m.Length))
|
||||
return o
|
||||
}
|
||||
|
||||
type stackObj struct {
|
||||
stack *stack.Stack
|
||||
vm *goja.Runtime
|
||||
toBig toBigFn
|
||||
}
|
||||
|
||||
func (s *stackObj) Peek(idx int) goja.Value {
|
||||
value, err := s.peek(idx)
|
||||
if err != nil {
|
||||
s.vm.Interrupt(err)
|
||||
return nil
|
||||
}
|
||||
res, err := s.toBig(s.vm, value.String())
|
||||
if err != nil {
|
||||
s.vm.Interrupt(err)
|
||||
return nil
|
||||
}
|
||||
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 {
|
||||
return len(s.stack.Data)
|
||||
}
|
||||
|
||||
func (s *stackObj) setupObject() *goja.Object {
|
||||
o := s.vm.NewObject()
|
||||
o.Set("peek", s.vm.ToValue(s.Peek))
|
||||
o.Set("length", s.vm.ToValue(s.Length))
|
||||
return o
|
||||
}
|
||||
|
||||
type dbObj struct {
|
||||
ibs evmtypes.IntraBlockState
|
||||
vm *goja.Runtime
|
||||
toBig toBigFn
|
||||
toBuf toBufFn
|
||||
fromBuf fromBufFn
|
||||
}
|
||||
|
||||
func (do *dbObj) GetBalance(addrSlice goja.Value) goja.Value {
|
||||
a, err := do.fromBuf(do.vm, addrSlice, false)
|
||||
if err != nil {
|
||||
do.vm.Interrupt(err)
|
||||
return nil
|
||||
}
|
||||
addr := common.BytesToAddress(a)
|
||||
value := do.ibs.GetBalance(addr)
|
||||
res, err := do.toBig(do.vm, value.String())
|
||||
if err != nil {
|
||||
do.vm.Interrupt(err)
|
||||
return nil
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
func (do *dbObj) GetNonce(addrSlice goja.Value) uint64 {
|
||||
a, err := do.fromBuf(do.vm, addrSlice, false)
|
||||
if err != nil {
|
||||
do.vm.Interrupt(err)
|
||||
return 0
|
||||
}
|
||||
addr := common.BytesToAddress(a)
|
||||
return do.ibs.GetNonce(addr)
|
||||
}
|
||||
|
||||
func (do *dbObj) GetCode(addrSlice goja.Value) goja.Value {
|
||||
a, err := do.fromBuf(do.vm, addrSlice, false)
|
||||
if err != nil {
|
||||
do.vm.Interrupt(err)
|
||||
return nil
|
||||
}
|
||||
addr := common.BytesToAddress(a)
|
||||
code := do.ibs.GetCode(addr)
|
||||
res, err := do.toBuf(do.vm, code)
|
||||
if err != nil {
|
||||
do.vm.Interrupt(err)
|
||||
return nil
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
func (do *dbObj) GetState(addrSlice goja.Value, hashSlice goja.Value) goja.Value {
|
||||
a, err := do.fromBuf(do.vm, addrSlice, false)
|
||||
if err != nil {
|
||||
do.vm.Interrupt(err)
|
||||
return nil
|
||||
}
|
||||
addr := common.BytesToAddress(a)
|
||||
h, err := do.fromBuf(do.vm, hashSlice, false)
|
||||
if err != nil {
|
||||
do.vm.Interrupt(err)
|
||||
return nil
|
||||
}
|
||||
hash := common.BytesToHash(h)
|
||||
var outValue uint256.Int
|
||||
do.ibs.GetState(addr, &hash, &outValue)
|
||||
res, err := do.toBuf(do.vm, outValue.Bytes())
|
||||
if err != nil {
|
||||
do.vm.Interrupt(err)
|
||||
return nil
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
func (do *dbObj) Exists(addrSlice goja.Value) bool {
|
||||
a, err := do.fromBuf(do.vm, addrSlice, false)
|
||||
if err != nil {
|
||||
do.vm.Interrupt(err)
|
||||
return false
|
||||
}
|
||||
addr := common.BytesToAddress(a)
|
||||
return do.ibs.Exist(addr)
|
||||
}
|
||||
|
||||
func (do *dbObj) setupObject() *goja.Object {
|
||||
o := do.vm.NewObject()
|
||||
o.Set("getBalance", do.vm.ToValue(do.GetBalance))
|
||||
o.Set("getNonce", do.vm.ToValue(do.GetNonce))
|
||||
o.Set("getCode", do.vm.ToValue(do.GetCode))
|
||||
o.Set("getState", do.vm.ToValue(do.GetState))
|
||||
o.Set("exists", do.vm.ToValue(do.Exists))
|
||||
return o
|
||||
}
|
||||
|
||||
type contractObj struct {
|
||||
contract *vm.Contract
|
||||
vm *goja.Runtime
|
||||
toBig toBigFn
|
||||
toBuf toBufFn
|
||||
}
|
||||
|
||||
func (co *contractObj) GetCaller() goja.Value {
|
||||
caller := co.contract.Caller().Bytes()
|
||||
res, err := co.toBuf(co.vm, caller)
|
||||
if err != nil {
|
||||
co.vm.Interrupt(err)
|
||||
return nil
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
func (co *contractObj) GetAddress() goja.Value {
|
||||
addr := co.contract.Address().Bytes()
|
||||
res, err := co.toBuf(co.vm, addr)
|
||||
if err != nil {
|
||||
co.vm.Interrupt(err)
|
||||
return nil
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
func (co *contractObj) GetValue() goja.Value {
|
||||
value := co.contract.Value()
|
||||
res, err := co.toBig(co.vm, value.String())
|
||||
if err != nil {
|
||||
co.vm.Interrupt(err)
|
||||
return nil
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
func (co *contractObj) GetInput() goja.Value {
|
||||
input := common.CopyBytes(co.contract.Input)
|
||||
res, err := co.toBuf(co.vm, input)
|
||||
if err != nil {
|
||||
co.vm.Interrupt(err)
|
||||
return nil
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
func (c *contractObj) setupObject() *goja.Object {
|
||||
o := c.vm.NewObject()
|
||||
o.Set("getCaller", c.vm.ToValue(c.GetCaller))
|
||||
o.Set("getAddress", c.vm.ToValue(c.GetAddress))
|
||||
o.Set("getValue", c.vm.ToValue(c.GetValue))
|
||||
o.Set("getInput", c.vm.ToValue(c.GetInput))
|
||||
return o
|
||||
}
|
||||
|
||||
type callframe struct {
|
||||
vm *goja.Runtime
|
||||
toBig toBigFn
|
||||
toBuf toBufFn
|
||||
|
||||
typ string
|
||||
from common.Address
|
||||
to common.Address
|
||||
input []byte
|
||||
gas uint
|
||||
value *big.Int
|
||||
}
|
||||
|
||||
func (f *callframe) GetType() string {
|
||||
return f.typ
|
||||
}
|
||||
|
||||
func (f *callframe) GetFrom() goja.Value {
|
||||
from := f.from.Bytes()
|
||||
res, err := f.toBuf(f.vm, from)
|
||||
if err != nil {
|
||||
f.vm.Interrupt(err)
|
||||
return nil
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
func (f *callframe) GetTo() goja.Value {
|
||||
to := f.to.Bytes()
|
||||
res, err := f.toBuf(f.vm, to)
|
||||
if err != nil {
|
||||
f.vm.Interrupt(err)
|
||||
return nil
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
func (f *callframe) GetInput() goja.Value {
|
||||
input := f.input
|
||||
res, err := f.toBuf(f.vm, input)
|
||||
if err != nil {
|
||||
f.vm.Interrupt(err)
|
||||
return nil
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
func (f *callframe) GetGas() uint {
|
||||
return f.gas
|
||||
}
|
||||
|
||||
func (f *callframe) GetValue() goja.Value {
|
||||
if f.value == nil {
|
||||
return goja.Undefined()
|
||||
}
|
||||
res, err := f.toBig(f.vm, f.value.String())
|
||||
if err != nil {
|
||||
f.vm.Interrupt(err)
|
||||
return nil
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
func (f *callframe) setupObject() *goja.Object {
|
||||
o := f.vm.NewObject()
|
||||
o.Set("getType", f.vm.ToValue(f.GetType))
|
||||
o.Set("getFrom", f.vm.ToValue(f.GetFrom))
|
||||
o.Set("getTo", f.vm.ToValue(f.GetTo))
|
||||
o.Set("getInput", f.vm.ToValue(f.GetInput))
|
||||
o.Set("getGas", f.vm.ToValue(f.GetGas))
|
||||
o.Set("getValue", f.vm.ToValue(f.GetValue))
|
||||
return o
|
||||
}
|
||||
|
||||
type callframeResult struct {
|
||||
vm *goja.Runtime
|
||||
toBuf toBufFn
|
||||
|
||||
gasUsed uint
|
||||
output []byte
|
||||
err error
|
||||
}
|
||||
|
||||
func (r *callframeResult) GetGasUsed() uint {
|
||||
return r.gasUsed
|
||||
}
|
||||
|
||||
func (r *callframeResult) GetOutput() goja.Value {
|
||||
res, err := r.toBuf(r.vm, r.output)
|
||||
if err != nil {
|
||||
r.vm.Interrupt(err)
|
||||
return nil
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
func (r *callframeResult) GetError() goja.Value {
|
||||
if r.err != nil {
|
||||
return r.vm.ToValue(r.err.Error())
|
||||
}
|
||||
return goja.Undefined()
|
||||
}
|
||||
|
||||
func (r *callframeResult) setupObject() *goja.Object {
|
||||
o := r.vm.NewObject()
|
||||
o.Set("getGasUsed", r.vm.ToValue(r.GetGasUsed))
|
||||
o.Set("getOutput", r.vm.ToValue(r.GetOutput))
|
||||
o.Set("getError", r.vm.ToValue(r.GetError))
|
||||
return o
|
||||
}
|
||||
|
||||
type steplog struct {
|
||||
vm *goja.Runtime
|
||||
|
||||
op *opObj
|
||||
memory *memoryObj
|
||||
stack *stackObj
|
||||
contract *contractObj
|
||||
|
||||
pc uint64
|
||||
gas uint64
|
||||
cost uint64
|
||||
depth int
|
||||
refund uint64
|
||||
err error
|
||||
}
|
||||
|
||||
func (l *steplog) GetPC() uint64 { return l.pc }
|
||||
func (l *steplog) GetGas() uint64 { return l.gas }
|
||||
func (l *steplog) GetCost() uint64 { return l.cost }
|
||||
func (l *steplog) GetDepth() int { return l.depth }
|
||||
func (l *steplog) GetRefund() uint64 { return l.refund }
|
||||
|
||||
func (l *steplog) GetError() goja.Value {
|
||||
if l.err != nil {
|
||||
return l.vm.ToValue(l.err.Error())
|
||||
}
|
||||
return goja.Undefined()
|
||||
}
|
||||
|
||||
func (l *steplog) setupObject() *goja.Object {
|
||||
o := l.vm.NewObject()
|
||||
// Setup basic fields.
|
||||
o.Set("getPC", l.vm.ToValue(l.GetPC))
|
||||
o.Set("getGas", l.vm.ToValue(l.GetGas))
|
||||
o.Set("getCost", l.vm.ToValue(l.GetCost))
|
||||
o.Set("getDepth", l.vm.ToValue(l.GetDepth))
|
||||
o.Set("getRefund", l.vm.ToValue(l.GetRefund))
|
||||
o.Set("getError", l.vm.ToValue(l.GetError))
|
||||
// Setup nested objects.
|
||||
o.Set("op", l.op.setupObject())
|
||||
o.Set("stack", l.stack.setupObject())
|
||||
o.Set("memory", l.memory.setupObject())
|
||||
o.Set("contract", l.contract.setupObject())
|
||||
return o
|
||||
}
|
||||
|
||||
func min(a, b int64) int64 {
|
||||
if a < b {
|
||||
return a
|
||||
}
|
||||
return b
|
||||
}
|
@ -204,7 +204,6 @@
|
||||
gasUsed: '0x' + bigInt(ctx.gasUsed).toString(16),
|
||||
input: toHex(ctx.input),
|
||||
output: toHex(ctx.output),
|
||||
time: ctx.time,
|
||||
};
|
||||
if (this.callstack[0].calls !== undefined) {
|
||||
result.calls = this.callstack[0].calls;
|
||||
@ -234,7 +233,6 @@
|
||||
input: call.input,
|
||||
output: call.output,
|
||||
error: call.error,
|
||||
time: call.time,
|
||||
calls: call.calls,
|
||||
}
|
||||
for (var key in sorted) {
|
||||
@ -249,4 +247,4 @@
|
||||
}
|
||||
return sorted;
|
||||
}
|
||||
}
|
||||
}
|
@ -76,7 +76,7 @@
|
||||
var outsize = log.stack.peek(1).valueOf();
|
||||
frame.return = log.memory.slice(out, out + outsize);
|
||||
break;
|
||||
case "STOP": case "SUICIDE":
|
||||
case "STOP": case "SELFDESTRUCT":
|
||||
frame.return = log.memory.slice(0, 0);
|
||||
break;
|
||||
case "JUMPDEST":
|
@ -62,7 +62,7 @@
|
||||
var toBal = bigInt(this.prestate[toHex(ctx.to)].balance.slice(2), 16);
|
||||
|
||||
this.prestate[toHex(ctx.to)].balance = '0x'+toBal.subtract(ctx.value).toString(16);
|
||||
this.prestate[toHex(ctx.from)].balance = '0x'+fromBal.add(ctx.value).add((ctx.gasUsed + ctx.intrinsicGas) * ctx.gasPrice).toString(16);
|
||||
this.prestate[toHex(ctx.from)].balance = '0x'+fromBal.add(ctx.value).add(ctx.gasUsed * ctx.gasPrice).toString(16);
|
||||
|
||||
// Decrement the caller's nonce, and remove empty create targets
|
||||
this.prestate[toHex(ctx.from)].nonce--;
|
59
eth/tracers/js/internal/tracers/tracers.go
Normal file
59
eth/tracers/js/internal/tracers/tracers.go
Normal file
@ -0,0 +1,59 @@
|
||||
// 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 tracers contains the actual JavaScript tracer assets.
|
||||
package tracers
|
||||
|
||||
import (
|
||||
"embed"
|
||||
"io/fs"
|
||||
"strings"
|
||||
"unicode"
|
||||
)
|
||||
|
||||
//go:embed *.js
|
||||
var files embed.FS
|
||||
|
||||
// Load reads the built-in JS tracer files embedded in the binary and
|
||||
// returns a mapping of tracer name to source.
|
||||
func Load() (map[string]string, error) {
|
||||
var assetTracers = make(map[string]string)
|
||||
err := fs.WalkDir(files, ".", func(path string, d fs.DirEntry, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if d.IsDir() {
|
||||
return nil
|
||||
}
|
||||
b, err := fs.ReadFile(files, path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
name := camel(strings.TrimSuffix(path, ".js"))
|
||||
assetTracers[name] = string(b)
|
||||
return nil
|
||||
})
|
||||
return assetTracers, err
|
||||
}
|
||||
|
||||
// camel converts a snake cased input string into a camel cased output.
|
||||
func camel(str string) string {
|
||||
pieces := strings.Split(str, "_")
|
||||
for i := 1; i < len(pieces); i++ {
|
||||
pieces[i] = string(unicode.ToUpper(rune(pieces[i][0]))) + pieces[i][1:]
|
||||
}
|
||||
return strings.Join(pieces, "")
|
||||
}
|
321
eth/tracers/js/tracer_test.go
Normal file
321
eth/tracers/js/tracer_test.go
Normal file
@ -0,0 +1,321 @@
|
||||
// Copyright 2021 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
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"math/big"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/holiman/uint256"
|
||||
"github.com/ledgerwatch/erigon/common"
|
||||
"github.com/ledgerwatch/erigon/core/state"
|
||||
"github.com/ledgerwatch/erigon/core/vm"
|
||||
"github.com/ledgerwatch/erigon/core/vm/evmtypes"
|
||||
"github.com/ledgerwatch/erigon/eth/tracers"
|
||||
"github.com/ledgerwatch/erigon/params"
|
||||
)
|
||||
|
||||
type account struct{}
|
||||
|
||||
func (account) SubBalance(amount *big.Int) {}
|
||||
func (account) AddBalance(amount *big.Int) {}
|
||||
func (account) SetAddress(common.Address) {}
|
||||
func (account) Value() *big.Int { return nil }
|
||||
func (account) SetBalance(*big.Int) {}
|
||||
func (account) SetNonce(uint64) {}
|
||||
func (account) Balance() *uint256.Int { return &uint256.Int{} }
|
||||
func (account) Address() common.Address { return common.Address{} }
|
||||
func (account) SetCode(common.Hash, []byte) {}
|
||||
func (account) ForEachStorage(cb func(key, value common.Hash) bool) {}
|
||||
|
||||
type dummyStatedb struct {
|
||||
state.IntraBlockState
|
||||
}
|
||||
|
||||
func (*dummyStatedb) GetRefund() uint64 { return 1337 }
|
||||
func (*dummyStatedb) GetBalance(addr common.Address) *uint256.Int { return &uint256.Int{} }
|
||||
|
||||
type vmContext struct {
|
||||
blockCtx evmtypes.BlockContext
|
||||
txCtx evmtypes.TxContext
|
||||
}
|
||||
|
||||
func testCtx() *vmContext {
|
||||
return &vmContext{blockCtx: evmtypes.BlockContext{BlockNumber: 1}, txCtx: evmtypes.TxContext{GasPrice: uint256.NewInt(100000)}}
|
||||
}
|
||||
|
||||
func runTrace(tracer tracers.Tracer, vmctx *vmContext, chaincfg *params.ChainConfig, contractCode []byte) (json.RawMessage, error) {
|
||||
var (
|
||||
env = vm.NewEVM(vmctx.blockCtx, vmctx.txCtx, &dummyStatedb{}, chaincfg, vm.Config{Debug: true, Tracer: tracer})
|
||||
gasLimit uint64 = 31000
|
||||
startGas uint64 = 10000
|
||||
value = uint256.NewInt(0)
|
||||
contract = vm.NewContract(account{}, account{}, value, startGas, false /* skipAnalysis */)
|
||||
)
|
||||
contract.Code = []byte{byte(vm.PUSH1), 0x1, byte(vm.PUSH1), 0x1, 0x0}
|
||||
if contractCode != nil {
|
||||
contract.Code = contractCode
|
||||
}
|
||||
|
||||
tracer.CaptureTxStart(gasLimit)
|
||||
tracer.CaptureStart(env, contract.Caller(), contract.Address(), false /* precompile */, false /* create */, []byte{}, startGas, value, []byte{} /* code */)
|
||||
ret, err := env.Interpreter().Run(contract, []byte{}, false)
|
||||
tracer.CaptureEnd(ret, startGas-contract.Gas, err)
|
||||
// Rest gas assumes no refund
|
||||
tracer.CaptureTxEnd(contract.Gas)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return tracer.GetResult()
|
||||
}
|
||||
|
||||
func TestTracer(t *testing.T) {
|
||||
execTracer := func(code string, contract []byte) ([]byte, string) {
|
||||
t.Helper()
|
||||
tracer, err := newJsTracer(code, nil, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ret, err := runTrace(tracer, testCtx(), params.TestChainConfig, contract)
|
||||
if err != nil {
|
||||
return nil, err.Error() // Stringify to allow comparison without nil checks
|
||||
}
|
||||
return ret, ""
|
||||
}
|
||||
for i, tt := range []struct {
|
||||
code string
|
||||
want string
|
||||
fail string
|
||||
contract []byte
|
||||
}{
|
||||
{ // 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; }}",
|
||||
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
|
||||
code: "{depths: [], step: function(log) { this.depths.push(log.stack.peek(-1)); }, fault: function() {}, result: function() { return this.depths; }}",
|
||||
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
|
||||
code: "{ depths: [], step: function(log, db) { this.depths.push(log.memory.getUint(-64));}, fault: function() {}, result: function() { return this.depths; }}",
|
||||
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
|
||||
code: "{count: 0, step: function() { this.count += 1; }, fault: function() {}, result: function() { return this.count; }}",
|
||||
want: `3`,
|
||||
}, { // tests that depth is reported correctly
|
||||
code: "{depths: [], step: function(log) { this.depths.push(log.stack.length()); }, fault: function() {}, result: function() { return this.depths; }}",
|
||||
want: `[0,1,2]`,
|
||||
}, { // tests memory length
|
||||
code: "{lengths: [], step: function(log) { this.lengths.push(log.memory.length()); }, fault: function() {}, result: function() { return this.lengths; }}",
|
||||
want: `[0,0,0]`,
|
||||
}, { // tests to-string of opcodes
|
||||
code: "{opcodes: [], step: function(log) { this.opcodes.push(log.op.toString()); }, fault: function() {}, result: function() { return this.opcodes; }}",
|
||||
want: `["PUSH1","PUSH1","STOP"]`,
|
||||
}, { // tests gasUsed
|
||||
code: "{depths: [], step: function() {}, fault: function() {}, result: function(ctx) { return ctx.gasPrice+'.'+ctx.gasUsed; }}",
|
||||
want: `"100000.21006"`,
|
||||
}, {
|
||||
code: "{res: null, step: function(log) {}, fault: function() {}, result: function() { return toWord('0xffaa') }}",
|
||||
want: `{"0":0,"1":0,"2":0,"3":0,"4":0,"5":0,"6":0,"7":0,"8":0,"9":0,"10":0,"11":0,"12":0,"13":0,"14":0,"15":0,"16":0,"17":0,"18":0,"19":0,"20":0,"21":0,"22":0,"23":0,"24":0,"25":0,"26":0,"27":0,"28":0,"29":0,"30":255,"31":170}`,
|
||||
}, { // test feeding a buffer back into go
|
||||
code: "{res: null, step: function(log) { var address = log.contract.getAddress(); this.res = toAddress(address); }, fault: function() {}, result: function() { return this.res }}",
|
||||
want: `{"0":0,"1":0,"2":0,"3":0,"4":0,"5":0,"6":0,"7":0,"8":0,"9":0,"10":0,"11":0,"12":0,"13":0,"14":0,"15":0,"16":0,"17":0,"18":0,"19":0}`,
|
||||
}, {
|
||||
code: "{res: null, step: function(log) { var address = '0x0000000000000000000000000000000000000000'; this.res = toAddress(address); }, fault: function() {}, result: function() { return this.res }}",
|
||||
want: `{"0":0,"1":0,"2":0,"3":0,"4":0,"5":0,"6":0,"7":0,"8":0,"9":0,"10":0,"11":0,"12":0,"13":0,"14":0,"15":0,"16":0,"17":0,"18":0,"19":0}`,
|
||||
}, {
|
||||
code: "{res: null, step: function(log) { var address = Array.prototype.slice.call(log.contract.getAddress()); this.res = toAddress(address); }, fault: function() {}, result: function() { return this.res }}",
|
||||
want: `{"0":0,"1":0,"2":0,"3":0,"4":0,"5":0,"6":0,"7":0,"8":0,"9":0,"10":0,"11":0,"12":0,"13":0,"14":0,"15":0,"16":0,"17":0,"18":0,"19":0}`,
|
||||
}, {
|
||||
code: "{res: [], step: function(log) { var op = log.op.toString(); if (op === 'MSTORE8' || op === 'STOP') { this.res.push(log.memory.slice(0, 2)) } }, fault: function() {}, result: function() { return this.res }}",
|
||||
want: `[{"0":0,"1":0},{"0":255,"1":0}]`,
|
||||
contract: []byte{byte(vm.PUSH1), byte(0xff), byte(vm.PUSH1), byte(0x00), byte(vm.MSTORE8), byte(vm.STOP)},
|
||||
}, {
|
||||
code: "{res: [], step: function(log) { if (log.op.toString() === 'STOP') { this.res.push(log.memory.slice(5, 1025 * 1024)) } }, fault: function() {}, result: function() { return this.res }}",
|
||||
want: "",
|
||||
fail: "tracer reached limit for padding memory slice: end 1049600, memorySize 32 at step (<eval>:1:83(23)) in server-side tracer function 'step'",
|
||||
contract: []byte{byte(vm.PUSH1), byte(0xff), byte(vm.PUSH1), byte(0x00), byte(vm.MSTORE8), byte(vm.STOP)},
|
||||
},
|
||||
} {
|
||||
if have, err := execTracer(tt.code, tt.contract); tt.want != string(have) || tt.fail != err {
|
||||
t.Errorf("testcase %d: expected return value to be '%s' got '%s', error to be '%s' got '%s'\n\tcode: %v", i, tt.want, string(have), tt.fail, err, tt.code)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestHalt(t *testing.T) {
|
||||
timeout := errors.New("stahp")
|
||||
tracer, err := newJsTracer("{step: function() { while(1); }, result: function() { return null; }, fault: function(){}}", nil, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
go func() {
|
||||
time.Sleep(1 * time.Second)
|
||||
tracer.Stop(timeout)
|
||||
}()
|
||||
if _, err = runTrace(tracer, testCtx(), params.TestChainConfig, nil); !strings.Contains(err.Error(), "stahp") {
|
||||
t.Errorf("Expected timeout error, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHaltBetweenSteps(t *testing.T) {
|
||||
tracer, err := newJsTracer("{step: function() {}, fault: function() {}, result: function() { return null; }}", nil, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
env := vm.NewEVM(evmtypes.BlockContext{BlockNumber: 1}, evmtypes.TxContext{GasPrice: uint256.NewInt(1)}, &dummyStatedb{}, params.TestChainConfig, vm.Config{Debug: true, Tracer: tracer})
|
||||
scope := &vm.ScopeContext{
|
||||
Contract: vm.NewContract(&account{}, &account{}, uint256.NewInt(0), 0, false /* skipAnalysis */),
|
||||
}
|
||||
tracer.CaptureStart(env, common.Address{}, common.Address{}, false /* precompile */, false /* create */, []byte{}, 0, uint256.NewInt(0), []byte{} /* code */)
|
||||
tracer.CaptureState(0, 0, 0, 0, scope, nil, 0, nil)
|
||||
timeout := errors.New("stahp")
|
||||
tracer.Stop(timeout)
|
||||
tracer.CaptureState(0, 0, 0, 0, scope, nil, 0, nil)
|
||||
|
||||
if _, err := tracer.GetResult(); !strings.Contains(err.Error(), timeout.Error()) {
|
||||
t.Errorf("Expected timeout error, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// testNoStepExec tests a regular value transfer (no exec), and accessing the statedb
|
||||
// in 'result'
|
||||
func TestNoStepExec(t *testing.T) {
|
||||
execTracer := func(code string) []byte {
|
||||
t.Helper()
|
||||
tracer, err := newJsTracer(code, nil, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
env := vm.NewEVM(evmtypes.BlockContext{BlockNumber: 1}, evmtypes.TxContext{GasPrice: uint256.NewInt(100)}, &dummyStatedb{}, params.TestChainConfig, vm.Config{Debug: true, Tracer: tracer})
|
||||
tracer.CaptureStart(env, common.Address{}, common.Address{}, false /* precompile */, false /* create */, []byte{}, 1000, uint256.NewInt(0), []byte{} /* code */)
|
||||
tracer.CaptureEnd(nil, 0, nil)
|
||||
ret, err := tracer.GetResult()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return ret
|
||||
}
|
||||
for i, tt := range []struct {
|
||||
code string
|
||||
want string
|
||||
}{
|
||||
{ // tests that we don't panic on accessing the db methods
|
||||
code: "{depths: [], step: function() {}, fault: function() {}, result: function(ctx, db){ return db.getBalance(ctx.to)} }",
|
||||
want: `"0"`,
|
||||
},
|
||||
} {
|
||||
if have := execTracer(tt.code); tt.want != string(have) {
|
||||
t.Errorf("testcase %d: expected return value to be %s got %s\n\tcode: %v", i, tt.want, string(have), tt.code)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsPrecompile(t *testing.T) {
|
||||
chaincfg := ¶ms.ChainConfig{ChainID: big.NewInt(1), HomesteadBlock: big.NewInt(0), DAOForkBlock: nil, DAOForkSupport: false, TangerineWhistleBlock: big.NewInt(0), TangerineWhistleHash: common.Hash{}, SpuriousDragonBlock: 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.IstanbulBlock = big.NewInt(200)
|
||||
chaincfg.BerlinBlock = big.NewInt(300)
|
||||
txCtx := evmtypes.TxContext{GasPrice: uint256.NewInt(100000)}
|
||||
tracer, err := newJsTracer("{addr: toAddress('0000000000000000000000000000000000000009'), res: null, step: function() { this.res = isPrecompiled(this.addr); }, fault: function() {}, result: function() { return this.res; }}", nil, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
blockCtx := evmtypes.BlockContext{BlockNumber: 150}
|
||||
res, err := runTrace(tracer, &vmContext{blockCtx, txCtx}, chaincfg, nil)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if string(res) != "false" {
|
||||
t.Errorf("tracer should not consider blake2f as precompile in byzantium")
|
||||
}
|
||||
|
||||
tracer, _ = newJsTracer("{addr: toAddress('0000000000000000000000000000000000000009'), res: null, step: function() { this.res = isPrecompiled(this.addr); }, fault: function() {}, result: function() { return this.res; }}", nil, nil)
|
||||
blockCtx = evmtypes.BlockContext{BlockNumber: 250}
|
||||
res, err = runTrace(tracer, &vmContext{blockCtx, txCtx}, chaincfg, nil)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if string(res) != "true" {
|
||||
t.Errorf("tracer should consider blake2f as precompile in istanbul")
|
||||
}
|
||||
}
|
||||
|
||||
func TestEnterExit(t *testing.T) {
|
||||
// test that either both or none of enter() and exit() are defined
|
||||
if _, err := newJsTracer("{step: function() {}, fault: function() {}, result: function() { return null; }, enter: function() {}}", new(tracers.Context), nil); err == nil {
|
||||
t.Fatal("tracer creation should've failed without exit() definition")
|
||||
}
|
||||
if _, err := newJsTracer("{step: function() {}, fault: function() {}, result: function() { return null; }, enter: function() {}, exit: function() {}}", new(tracers.Context), nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// test that the enter and exit method are correctly invoked and the values passed
|
||||
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), nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
scope := &vm.ScopeContext{
|
||||
Contract: vm.NewContract(&account{}, &account{}, uint256.NewInt(0), 0, false /* skipAnalysis */),
|
||||
}
|
||||
tracer.CaptureEnter(vm.CALL, scope.Contract.Caller(), scope.Contract.Address(), false, false, []byte{}, 1000, new(uint256.Int), []byte{})
|
||||
tracer.CaptureExit([]byte{}, 400, nil)
|
||||
|
||||
have, err := tracer.GetResult()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
want := `{"enters":1,"exits":1,"enterGas":1000,"gasUsed":400}`
|
||||
if string(have) != want {
|
||||
t.Errorf("Number of invocations of enter() and exit() is wrong. Have %s, want %s\n", have, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetup(t *testing.T) {
|
||||
// Test empty config
|
||||
_, err := newJsTracer(`{setup: function(cfg) { if (cfg !== "{}") { throw("invalid empty config") } }, fault: function() {}, result: function() {}}`, new(tracers.Context), nil)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
cfg, err := json.Marshal(map[string]string{"foo": "bar"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// Test no setup func
|
||||
_, err = newJsTracer(`{fault: function() {}, result: function() {}}`, new(tracers.Context), cfg)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// Test config value
|
||||
tracer, err := newJsTracer("{config: null, setup: function(cfg) { this.config = JSON.parse(cfg) }, step: function() {}, fault: function() {}, result: function() { return this.config.foo }}", new(tracers.Context), cfg)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
have, err := tracer.GetResult()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if string(have) != `"bar"` {
|
||||
t.Errorf("tracer returned wrong result. have: %s, want: \"bar\"\n", string(have))
|
||||
}
|
||||
}
|
@ -1,240 +0,0 @@
|
||||
package tracers
|
||||
|
||||
import (
|
||||
"unsafe"
|
||||
|
||||
"github.com/dop251/goja"
|
||||
)
|
||||
|
||||
type JSVM struct {
|
||||
vm *goja.Runtime
|
||||
stack []goja.Value
|
||||
}
|
||||
|
||||
func JSVMNew() *JSVM {
|
||||
return &JSVM{
|
||||
vm: goja.New(),
|
||||
stack: make([]goja.Value, 0, 100),
|
||||
}
|
||||
}
|
||||
|
||||
func (vm *JSVM) Pop() {
|
||||
vm.stack = vm.stack[:len(vm.stack)-1]
|
||||
}
|
||||
|
||||
func (vm *JSVM) Swap(index1 int, index2 int) {
|
||||
vm.stack[len(vm.stack)+index1], vm.stack[len(vm.stack)+index2] = vm.stack[len(vm.stack)+index2], vm.stack[len(vm.stack)+index1]
|
||||
}
|
||||
|
||||
func (vm *JSVM) pushAny(val interface{}) {
|
||||
vm.stack = append(vm.stack, vm.vm.ToValue(val))
|
||||
}
|
||||
|
||||
func (vm *JSVM) PushBoolean(val bool) {
|
||||
vm.pushAny(val)
|
||||
}
|
||||
|
||||
func (vm *JSVM) PushInt(val int) {
|
||||
vm.pushAny(val)
|
||||
}
|
||||
|
||||
func (vm *JSVM) PushUint(val uint) {
|
||||
vm.pushAny(val)
|
||||
}
|
||||
|
||||
func (vm *JSVM) PushString(val string) string {
|
||||
vm.pushAny(val)
|
||||
return val
|
||||
}
|
||||
|
||||
func (vm *JSVM) PushFixedBuffer(size int) unsafe.Pointer {
|
||||
buf := make([]byte, size)
|
||||
vm.pushAny(buf)
|
||||
if size == 0 {
|
||||
return unsafe.Pointer(nil)
|
||||
}
|
||||
return unsafe.Pointer(&buf[0])
|
||||
}
|
||||
|
||||
func (vm *JSVM) PushGoFunction(fn0 func(*JSVM) int) {
|
||||
fn := func(this goja.Value, args ...goja.Value) (goja.Value, error) {
|
||||
vm.stack = append(vm.stack, this)
|
||||
vm.stack = append(vm.stack, args...)
|
||||
_ = fn0(vm)
|
||||
result := vm.stack[len(vm.stack)-1]
|
||||
vm.Pop()
|
||||
return result, nil
|
||||
}
|
||||
vm.pushAny(fn)
|
||||
}
|
||||
|
||||
func (vm *JSVM) PushObject() int {
|
||||
vm.stack = append(vm.stack, vm.vm.ToValue(vm.vm.NewObject()))
|
||||
return len(vm.stack) - 1
|
||||
}
|
||||
|
||||
func (vm *JSVM) PushUndefined() {
|
||||
vm.stack = append(vm.stack, goja.Undefined())
|
||||
}
|
||||
|
||||
func (vm *JSVM) GetInt(index int) int {
|
||||
return int(vm.stack[len(vm.stack)+index].Export().(int64))
|
||||
}
|
||||
|
||||
func (vm *JSVM) GetString(index int) string {
|
||||
return vm.stack[len(vm.stack)+index].Export().(string)
|
||||
}
|
||||
|
||||
func (vm *JSVM) GetBuffer(index int) (rawPtr unsafe.Pointer, outSize uint) {
|
||||
v := vm.stack[len(vm.stack)+index]
|
||||
expValue := v.Export()
|
||||
|
||||
// toAddress() and some others are passed a string, but try to parse it with GetBuffer
|
||||
if _, ok := expValue.(string); ok {
|
||||
return nil, 0
|
||||
}
|
||||
|
||||
buf, ok := expValue.([]byte)
|
||||
if !ok {
|
||||
return nil, 0
|
||||
}
|
||||
if len(buf) == 0 {
|
||||
return unsafe.Pointer(nil), 0
|
||||
}
|
||||
return unsafe.Pointer(&buf[0]), uint(len(buf))
|
||||
}
|
||||
|
||||
func (vm *JSVM) GetPropString(objIndex int, key string) bool {
|
||||
obj := vm.stack[objIndex].ToObject(vm.vm)
|
||||
v := obj.Get(key)
|
||||
vm.stack = append(vm.stack, v)
|
||||
return !goja.IsUndefined(v)
|
||||
}
|
||||
|
||||
func (vm *JSVM) PutPropString(objIndex int, key string) {
|
||||
v := vm.stack[len(vm.stack)-1]
|
||||
vm.Pop()
|
||||
|
||||
obj := vm.stack[objIndex].ToObject(vm.vm)
|
||||
err := obj.Set(key, v)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
func (vm *JSVM) GetGlobalString(key string) bool {
|
||||
v := vm.vm.GlobalObject().Get(key)
|
||||
vm.stack = append(vm.stack, v)
|
||||
return !goja.IsUndefined(v)
|
||||
}
|
||||
|
||||
func (vm *JSVM) PutGlobalString(key string) {
|
||||
v := vm.stack[len(vm.stack)-1]
|
||||
vm.Pop()
|
||||
|
||||
obj := vm.vm.GlobalObject()
|
||||
err := obj.Set(key, v)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
func (vm *JSVM) PushGlobalGoFunction(name string, fn0 func(*JSVM) int) {
|
||||
fn := func(this goja.Value, args ...goja.Value) (goja.Value, error) {
|
||||
vm.stack = append(vm.stack, this)
|
||||
vm.stack = append(vm.stack, args...)
|
||||
_ = fn0(vm)
|
||||
result := vm.stack[len(vm.stack)-1]
|
||||
vm.Pop()
|
||||
return result, nil
|
||||
}
|
||||
err := vm.vm.GlobalObject().Set(name, goja.Callable(fn))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
func (vm *JSVM) PushGlobalObject() int {
|
||||
vm.stack = append(vm.stack, vm.vm.GlobalObject())
|
||||
return len(vm.stack) - 1
|
||||
}
|
||||
|
||||
func (vm *JSVM) Call(numArgs int) {
|
||||
if vm.Pcall(numArgs) != 0 {
|
||||
err := vm.stack[len(vm.stack)-1]
|
||||
vm.Pop()
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
func (vm *JSVM) Pcall(numArgs int) int {
|
||||
fnValue := vm.stack[len(vm.stack)-numArgs-1]
|
||||
args := vm.stack[len(vm.stack)-numArgs:]
|
||||
vm.stack = vm.stack[:len(vm.stack)-numArgs-1]
|
||||
|
||||
fn, ok := goja.AssertFunction(fnValue)
|
||||
if !ok {
|
||||
panic("AssertFunction")
|
||||
}
|
||||
|
||||
v, err := fn(goja.Undefined(), args...)
|
||||
if err != nil {
|
||||
vm.stack = append(vm.stack, vm.vm.ToValue(err))
|
||||
return 1
|
||||
} else {
|
||||
vm.stack = append(vm.stack, v)
|
||||
return 0
|
||||
}
|
||||
}
|
||||
|
||||
func (vm *JSVM) PcallProp(objIndex int, numArgs int) int {
|
||||
key := vm.stack[len(vm.stack)-numArgs-1].String()
|
||||
args := vm.stack[len(vm.stack)-numArgs:]
|
||||
vm.stack = vm.stack[:len(vm.stack)-numArgs-1]
|
||||
|
||||
obj := vm.stack[objIndex].ToObject(vm.vm)
|
||||
fnValue := obj.Get(key)
|
||||
|
||||
fn, ok := goja.AssertFunction(fnValue)
|
||||
if !ok {
|
||||
panic("AssertFunction")
|
||||
}
|
||||
|
||||
v, err := fn(obj, args...)
|
||||
if err != nil {
|
||||
vm.stack = append(vm.stack, vm.vm.ToValue(err))
|
||||
return 1
|
||||
} else {
|
||||
vm.stack = append(vm.stack, v)
|
||||
return 0
|
||||
}
|
||||
}
|
||||
|
||||
func (vm *JSVM) SafeToString(index int) string {
|
||||
v := vm.stack[len(vm.stack)+index]
|
||||
return v.ToString().String()
|
||||
}
|
||||
|
||||
func (vm *JSVM) Eval() {
|
||||
src := vm.GetString(-1)
|
||||
vm.Pop()
|
||||
vm.EvalString(src)
|
||||
}
|
||||
|
||||
func (vm *JSVM) EvalString(src string) {
|
||||
v, err := vm.vm.RunString(src)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
vm.stack = append(vm.stack, v)
|
||||
}
|
||||
|
||||
func (vm *JSVM) PevalString(src string) error {
|
||||
v, err := vm.vm.RunString(src)
|
||||
if err != nil {
|
||||
vm.stack = append(vm.stack, vm.vm.ToValue(err))
|
||||
} else {
|
||||
vm.stack = append(vm.stack, v)
|
||||
}
|
||||
return err
|
||||
}
|
@ -1,178 +0,0 @@
|
||||
package tracers
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestSwap(t *testing.T) {
|
||||
vm := JSVMNew()
|
||||
vm.PushInt(1)
|
||||
vm.PushInt(2)
|
||||
vm.Swap(-1, -2)
|
||||
assert.Equal(t, 1, vm.GetInt(-1))
|
||||
vm.Pop()
|
||||
assert.Equal(t, 2, vm.GetInt(-1))
|
||||
}
|
||||
|
||||
func TestPushAndGetInt(t *testing.T) {
|
||||
vm := JSVMNew()
|
||||
vm.PushInt(123)
|
||||
assert.Equal(t, 123, vm.GetInt(-1))
|
||||
}
|
||||
|
||||
func TestPushAndGetString(t *testing.T) {
|
||||
vm := JSVMNew()
|
||||
vm.PushString("hello")
|
||||
assert.Equal(t, "hello", vm.GetString(-1))
|
||||
}
|
||||
|
||||
func TestPushAndGetBuffer(t *testing.T) {
|
||||
vm := JSVMNew()
|
||||
vm.PushFixedBuffer(1)
|
||||
p, s := vm.GetBuffer(-1)
|
||||
assert.Equal(t, uint(1), s)
|
||||
assert.Equal(t, byte(0), *(*byte)(p))
|
||||
}
|
||||
|
||||
func TestPutAndGetPropString(t *testing.T) {
|
||||
vm := JSVMNew()
|
||||
objIndex := vm.PushObject()
|
||||
vm.PushString("hello")
|
||||
vm.PutPropString(objIndex, "x")
|
||||
exists := vm.GetPropString(objIndex, "x")
|
||||
assert.Equal(t, true, exists)
|
||||
x := vm.GetString(-1)
|
||||
assert.Equal(t, "hello", x)
|
||||
}
|
||||
|
||||
func TestGetGlobalString(t *testing.T) {
|
||||
vm := JSVMNew()
|
||||
vm.EvalString("x = 'hello'")
|
||||
exists := vm.GetGlobalString("x")
|
||||
assert.Equal(t, true, exists)
|
||||
x := vm.GetString(-1)
|
||||
assert.Equal(t, "hello", x)
|
||||
}
|
||||
|
||||
func TestPutGlobalString(t *testing.T) {
|
||||
vm := JSVMNew()
|
||||
vm.PushString("hello")
|
||||
vm.PutGlobalString("x")
|
||||
exists := vm.GetGlobalString("x")
|
||||
assert.Equal(t, true, exists)
|
||||
x := vm.GetString(-1)
|
||||
assert.Equal(t, "hello", x)
|
||||
}
|
||||
|
||||
func TestCall0(t *testing.T) {
|
||||
vm := JSVMNew()
|
||||
vm.PushGoFunction(func(ctx *JSVM) int {
|
||||
ctx.PushInt(123)
|
||||
return 1
|
||||
})
|
||||
vm.Call(0)
|
||||
x := vm.GetInt(-1)
|
||||
assert.Equal(t, 123, x)
|
||||
}
|
||||
|
||||
func TestCall1(t *testing.T) {
|
||||
vm := JSVMNew()
|
||||
vm.PushGoFunction(func(ctx *JSVM) int {
|
||||
arg := ctx.GetInt(-1)
|
||||
ctx.Pop()
|
||||
ctx.PushInt(arg + 120)
|
||||
return 1
|
||||
})
|
||||
vm.PushInt(3)
|
||||
vm.Call(1)
|
||||
x := vm.GetInt(-1)
|
||||
assert.Equal(t, 123, x)
|
||||
}
|
||||
|
||||
func TestCallPropWithGoFunction(t *testing.T) {
|
||||
vm := JSVMNew()
|
||||
vm.PushGlobalGoFunction("f", func(ctx *JSVM) int {
|
||||
ctx.PushInt(123)
|
||||
return 1
|
||||
})
|
||||
objIndex := vm.PushGlobalObject()
|
||||
vm.PushString("f")
|
||||
errCode := vm.PcallProp(objIndex, 0)
|
||||
assert.Equal(t, 0, errCode)
|
||||
x := vm.GetInt(-1)
|
||||
assert.Equal(t, 123, x)
|
||||
}
|
||||
|
||||
func TestCallProp0(t *testing.T) {
|
||||
vm := JSVMNew()
|
||||
vm.EvalString("function f() { return 'hello' }")
|
||||
objIndex := vm.PushGlobalObject()
|
||||
vm.PushString("f")
|
||||
errCode := vm.PcallProp(objIndex, 0)
|
||||
assert.Equal(t, 0, errCode)
|
||||
x := vm.GetString(-1)
|
||||
assert.Equal(t, "hello", x)
|
||||
}
|
||||
|
||||
func TestCallProp1(t *testing.T) {
|
||||
vm := JSVMNew()
|
||||
vm.EvalString("function f(s) { return s + '123' }")
|
||||
objIndex := vm.PushGlobalObject()
|
||||
vm.PushString("f")
|
||||
vm.PushString("hello")
|
||||
errCode := vm.PcallProp(objIndex, 1)
|
||||
assert.Equal(t, 0, errCode)
|
||||
x := vm.GetString(-1)
|
||||
assert.Equal(t, "hello123", x)
|
||||
}
|
||||
|
||||
func TestCallPropWithObj(t *testing.T) {
|
||||
vm := JSVMNew()
|
||||
vm.EvalString("function f(opts) { return opts.name + '123' }")
|
||||
globalIndex := vm.PushGlobalObject()
|
||||
vm.PushString("f")
|
||||
optsIndex := vm.PushObject()
|
||||
vm.PushString("hello")
|
||||
vm.PutPropString(optsIndex, "name")
|
||||
errCode := vm.PcallProp(globalIndex, 1)
|
||||
assert.Equal(t, 0, errCode)
|
||||
x := vm.GetString(-1)
|
||||
assert.Equal(t, "hello123", x)
|
||||
}
|
||||
|
||||
func TestCallPropWithJSObj(t *testing.T) {
|
||||
vm := JSVMNew()
|
||||
vm.EvalString(`
|
||||
function Options() { }
|
||||
Options.prototype.name = function () { return 'hello' }
|
||||
function makeOptions() { return new Options() }
|
||||
function f(opts) { return opts.name() + '123' }
|
||||
`)
|
||||
globalIndex := vm.PushGlobalObject()
|
||||
vm.PushString("f")
|
||||
|
||||
vm.PushString("makeOptions")
|
||||
errCode := vm.PcallProp(globalIndex, 0)
|
||||
assert.Equal(t, 0, errCode)
|
||||
|
||||
errCode = vm.PcallProp(globalIndex, 1)
|
||||
assert.Equal(t, 0, errCode)
|
||||
x := vm.GetString(-1)
|
||||
assert.Equal(t, "hello123", x)
|
||||
}
|
||||
|
||||
func TestSafeToString(t *testing.T) {
|
||||
vm := JSVMNew()
|
||||
vm.PushInt(5)
|
||||
assert.Equal(t, "5", vm.SafeToString(-1))
|
||||
}
|
||||
|
||||
func TestEval(t *testing.T) {
|
||||
vm := JSVMNew()
|
||||
vm.PushString("2 + 3")
|
||||
vm.Eval()
|
||||
x := vm.GetInt(-1)
|
||||
assert.Equal(t, 5, x)
|
||||
}
|
2
eth/tracers/testdata/call_tracer_create.json
vendored
2
eth/tracers/testdata/call_tracer_create.json
vendored
@ -48,7 +48,7 @@
|
||||
"result": {
|
||||
"from": "0x13e4acefe6a6700604929946e70e6443e4e73447",
|
||||
"gas": "0x5e106",
|
||||
"gasUsed": "0x5e106",
|
||||
"gasUsed": "0x897be",
|
||||
"input": "0x606060405260405160208061077c83398101604052808051906020019091905050600160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff161415151561007d57600080fd5b336000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff16021790555080600160006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055506001600460006101000a81548160ff02191690831515021790555050610653806101296000396000f300606060405260043610610083576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806305e4382a146100855780631c02708d146100ae5780632e1a7d4d146100c35780635114cb52146100e6578063a37dda2c146100fe578063ae200e7914610153578063b5769f70146101a8575b005b341561009057600080fd5b6100986101d1565b6040518082815260200191505060405180910390f35b34156100b957600080fd5b6100c16101d7565b005b34156100ce57600080fd5b6100e460048080359060200190919050506102eb565b005b6100fc6004808035906020019091905050610513565b005b341561010957600080fd5b6101116105d6565b604051808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b341561015e57600080fd5b6101666105fc565b604051808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b34156101b357600080fd5b6101bb610621565b6040518082815260200191505060405180910390f35b60025481565b60011515600460009054906101000a900460ff1615151415156101f957600080fd5b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff1614806102a15750600160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff16145b15156102ac57600080fd5b6000600460006101000a81548160ff0219169083151502179055506003543073ffffffffffffffffffffffffffffffffffffffff163103600281905550565b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff1614806103935750600160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff16145b151561039e57600080fd5b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff16141561048357600060025411801561040757506002548111155b151561041257600080fd5b80600254036002819055506000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff166108fc829081150290604051600060405180830381858888f19350505050151561047e57600080fd5b610510565b600060035411801561049757506003548111155b15156104a257600080fd5b8060035403600381905550600160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff166108fc829081150290604051600060405180830381858888f19350505050151561050f57600080fd5b5b50565b60011515600460009054906101000a900460ff16151514151561053557600080fd5b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff1614801561059657506003548160035401115b80156105bd575080600354013073ffffffffffffffffffffffffffffffffffffffff163110155b15156105c857600080fd5b806003540160038190555050565b600160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b600354815600a165627a7a72305820c3b849e8440987ce43eae3097b77672a69234d516351368b03fe5b7de03807910029000000000000000000000000c65e620a3a55451316168d57e268f5702ef56a11",
|
||||
"output": "0x606060405260043610610083576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806305e4382a146100855780631c02708d146100ae5780632e1a7d4d146100c35780635114cb52146100e6578063a37dda2c146100fe578063ae200e7914610153578063b5769f70146101a8575b005b341561009057600080fd5b6100986101d1565b6040518082815260200191505060405180910390f35b34156100b957600080fd5b6100c16101d7565b005b34156100ce57600080fd5b6100e460048080359060200190919050506102eb565b005b6100fc6004808035906020019091905050610513565b005b341561010957600080fd5b6101116105d6565b604051808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b341561015e57600080fd5b6101666105fc565b604051808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b34156101b357600080fd5b6101bb610621565b6040518082815260200191505060405180910390f35b60025481565b60011515600460009054906101000a900460ff1615151415156101f957600080fd5b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff1614806102a15750600160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff16145b15156102ac57600080fd5b6000600460006101000a81548160ff0219169083151502179055506003543073ffffffffffffffffffffffffffffffffffffffff163103600281905550565b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff1614806103935750600160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff16145b151561039e57600080fd5b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff16141561048357600060025411801561040757506002548111155b151561041257600080fd5b80600254036002819055506000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff166108fc829081150290604051600060405180830381858888f19350505050151561047e57600080fd5b610510565b600060035411801561049757506003548111155b15156104a257600080fd5b8060035403600381905550600160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff166108fc829081150290604051600060405180830381858888f19350505050151561050f57600080fd5b5b50565b60011515600460009054906101000a900460ff16151514151561053557600080fd5b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff1614801561059657506003548160035401115b80156105bd575080600354013073ffffffffffffffffffffffffffffffffffffffff163110155b15156105c857600080fd5b806003540160038190555050565b600160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b600354815600a165627a7a72305820c3b849e8440987ce43eae3097b77672a69234d516351368b03fe5b7de03807910029",
|
||||
"to": "0x7dc9c9730689ff0b0fd506c67db815f12d90a448",
|
||||
|
@ -405,7 +405,7 @@
|
||||
],
|
||||
"from": "0x70c9217d814985faef62b124420f8dfbddd96433",
|
||||
"gas": "0x37b38",
|
||||
"gasUsed": "0x12bb3",
|
||||
"gasUsed": "0x1810b",
|
||||
"input": "0x51a34eb80000000000000000000000000000000000000000000000280faf689c35ac0000",
|
||||
"output": "0x",
|
||||
"to": "0xc212e03b9e060e36facad5fd8f4435412ca22e6b",
|
||||
|
@ -87,7 +87,7 @@
|
||||
],
|
||||
"from": "0xa529806c67cc6486d4d62024471772f47f6fd672",
|
||||
"gas": "0x2d6e28",
|
||||
"gasUsed": "0x64bd",
|
||||
"gasUsed": "0xbd55",
|
||||
"input": "0x7065cb480000000000000000000000001523e55a1ca4efbae03355775ae89f8d7699ad9e",
|
||||
"output": "0x",
|
||||
"to": "0x269296dddce321a6bcbaa2f0181127593d732cba",
|
||||
|
@ -68,7 +68,7 @@
|
||||
"error": "invalid jump destination",
|
||||
"from": "0xe4a13bc304682a903e9472f469c33801dd18d9e8",
|
||||
"gas": "0x435c8",
|
||||
"gasUsed": "0x435c8",
|
||||
"gasUsed": "0x493e0",
|
||||
"input": "0x3b91f506000000000000000000000000a14bdd7e5666d784dcce98ad24d383a6b1cd4182000000000000000000000000e4a13bc304682a903e9472f469c33801dd18d9e8",
|
||||
"to": "0x1d3ddf7caf024f253487e18bc4a15b1a360c170a",
|
||||
"type": "CALL",
|
||||
|
@ -55,7 +55,7 @@
|
||||
"to": "0x6c06b16512b332e6cd8293a2974872674716ce18",
|
||||
"value": "0x0",
|
||||
"gas": "0x1a466",
|
||||
"gasUsed": "0x1dc6",
|
||||
"gasUsed": "0x72de",
|
||||
"input": "0x2e1a7d4d00000000000000000000000000000000000000000000000014d1120d7b160000",
|
||||
"output": "0x",
|
||||
"calls": [
|
||||
|
@ -72,7 +72,7 @@
|
||||
"error": "execution reverted",
|
||||
"from": "0xd4fcab9f0a6dc0493af47c864f6f17a8a5e2e826",
|
||||
"gas": "0x78d9e",
|
||||
"gasUsed": "0x76fc0",
|
||||
"gasUsed": "0x7c1c8",
|
||||
"input": "0x",
|
||||
"to": "0x33056b5dcac09a9b4becad0e1dcf92c19bd0af76",
|
||||
"type": "CALL",
|
||||
|
2
eth/tracers/testdata/call_tracer_oog.json
vendored
2
eth/tracers/testdata/call_tracer_oog.json
vendored
@ -51,7 +51,7 @@
|
||||
"error": "out of gas",
|
||||
"from": "0x94194bc2aaf494501d7880b61274a169f6502a54",
|
||||
"gas": "0x7045",
|
||||
"gasUsed": "0x7045",
|
||||
"gasUsed": "0xca1d",
|
||||
"input": "0xa9059cbb000000000000000000000000e77b1ac803616503510bed0086e3a7be2627a69900000000000000000000000000000000000000000000000000000009502f9000",
|
||||
"to": "0x43064693d3d38ad6a7cb579e0d6d9718c8aa6b62",
|
||||
"type": "CALL",
|
||||
|
2
eth/tracers/testdata/call_tracer_revert.json
vendored
2
eth/tracers/testdata/call_tracer_revert.json
vendored
@ -49,7 +49,7 @@
|
||||
"error": "execution reverted",
|
||||
"from": "0x0f6cef2b7fbb504782e35aa82a2207e816a2b7a9",
|
||||
"gas": "0x2d55e8",
|
||||
"gasUsed": "0xc3",
|
||||
"gasUsed": "0x719b",
|
||||
"input": "0x73b40a5c000000000000000000000000400de2e016bda6577407dfc379faba9899bc73ef0000000000000000000000002cc31912b2b0f3075a87b3640923d45a26cef3ee000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000064d79d8e6c7265636f76657279416464726573730000000000000000000000000000000000000000000000000000000000383e3ec32dc0f66d8fe60dbdc2f6815bdf73a988383e3ec32dc0f66d8fe60dbdc2f6815bdf73a98800000000000000000000000000000000000000000000000000000000000000000000000000000000",
|
||||
"to": "0xabbcd5b340c80b5f1c0545c04c987b87310296ae",
|
||||
"type": "CALL",
|
||||
|
@ -54,7 +54,7 @@
|
||||
"error": "execution reverted",
|
||||
"from": "0xf7579c3d8a669c89d5ed246a22eb6db8f6fedbf1",
|
||||
"gas": "0x2d7308",
|
||||
"gasUsed": "0x588",
|
||||
"gasUsed": "0x5940",
|
||||
"input": "0x5c19a95c000000000000000000000000f7579c3d8a669c89d5ed246a22eb6db8f6fedbf1",
|
||||
"to": "0xf58833cf0c791881b494eb79d461e08a1f043f52",
|
||||
"type": "CALL",
|
||||
|
2
eth/tracers/testdata/call_tracer_simple.json
vendored
2
eth/tracers/testdata/call_tracer_simple.json
vendored
@ -68,7 +68,7 @@
|
||||
],
|
||||
"from": "0xb436ba50d378d4bbc8660d312a13df6af6e89dfb",
|
||||
"gas": "0x10738",
|
||||
"gasUsed": "0x3ef9",
|
||||
"gasUsed": "0x9751",
|
||||
"input": "0x63e4bff40000000000000000000000000024f658a46fbb89d8ac105e98d7ac7cbbaf27c5",
|
||||
"output": "0x0000000000000000000000000000000000000000000000000000000000000001",
|
||||
"to": "0x3b873a919aa0512d5a0f09e6dcceaa4a6727fafe",
|
||||
|
2
eth/tracers/testdata/call_tracer_throw.json
vendored
2
eth/tracers/testdata/call_tracer_throw.json
vendored
@ -53,7 +53,7 @@
|
||||
"error": "invalid jump destination",
|
||||
"from": "0x70c9217d814985faef62b124420f8dfbddd96433",
|
||||
"gas": "0x37b38",
|
||||
"gasUsed": "0x37b38",
|
||||
"gasUsed": "0x3d090",
|
||||
"input": "0x51a34eb8000000000000000000000000000000000000000000000027fad02094277c0000",
|
||||
"to": "0xc212e03b9e060e36facad5fd8f4435412ca22e6b",
|
||||
"type": "CALL",
|
||||
|
File diff suppressed because one or more lines are too long
@ -1,212 +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 tracers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"math/big"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/holiman/uint256"
|
||||
"github.com/ledgerwatch/erigon/common"
|
||||
"github.com/ledgerwatch/erigon/core/state"
|
||||
"github.com/ledgerwatch/erigon/core/vm"
|
||||
"github.com/ledgerwatch/erigon/core/vm/evmtypes"
|
||||
"github.com/ledgerwatch/erigon/params"
|
||||
)
|
||||
|
||||
type account struct{}
|
||||
|
||||
func (account) SubBalance(amount *big.Int) {}
|
||||
func (account) AddBalance(amount *big.Int) {}
|
||||
func (account) SetAddress(common.Address) {}
|
||||
func (account) Value() *big.Int { return nil }
|
||||
func (account) SetBalance(*big.Int) {}
|
||||
func (account) SetNonce(uint64) {}
|
||||
func (account) Balance() *big.Int { return nil }
|
||||
func (account) Address() common.Address { return common.Address{} }
|
||||
func (account) ReturnGas(*big.Int) {}
|
||||
func (account) SetCode(common.Hash, []byte) {}
|
||||
func (account) ForEachStorage(cb func(key, value common.Hash) bool) {}
|
||||
|
||||
type dummyStatedb struct {
|
||||
state.IntraBlockState
|
||||
}
|
||||
|
||||
func (*dummyStatedb) GetRefund() uint64 { return 1337 }
|
||||
func (*dummyStatedb) GetBalance(addr common.Address) *uint256.Int { return uint256.NewInt(0) }
|
||||
|
||||
type vmContext struct {
|
||||
blockCtx evmtypes.BlockContext
|
||||
txCtx evmtypes.TxContext
|
||||
}
|
||||
|
||||
func testCtx() *vmContext {
|
||||
return &vmContext{blockCtx: evmtypes.BlockContext{
|
||||
BlockNumber: 1,
|
||||
}, txCtx: evmtypes.TxContext{GasPrice: uint256.NewInt(100000)}}
|
||||
}
|
||||
|
||||
func runTrace(tracer *Tracer, vmctx *vmContext) (json.RawMessage, error) {
|
||||
env := vm.NewEVM(vmctx.blockCtx, vmctx.txCtx, &dummyStatedb{}, params.TestChainConfig, vm.Config{Debug: true, Tracer: tracer})
|
||||
var (
|
||||
startGas uint64 = 10000
|
||||
value = uint256.NewInt(0)
|
||||
)
|
||||
contract := vm.NewContract(account{}, account{}, value, startGas, false)
|
||||
contract.Code = []byte{byte(vm.PUSH1), 0x1, byte(vm.PUSH1), 0x1, 0x0}
|
||||
|
||||
tracer.CaptureStart(env, contract.Caller(), contract.Address(), false, false, []byte{}, startGas, uint256.NewInt(value.Uint64()), contract.Code)
|
||||
ret, err := env.Interpreter().Run(contract, []byte{}, false)
|
||||
tracer.CaptureEnd(ret, startGas-contract.Gas, err)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return tracer.GetResult()
|
||||
}
|
||||
|
||||
func TestTracer(t *testing.T) {
|
||||
execTracer := func(code string) ([]byte, string) {
|
||||
t.Helper()
|
||||
ctx := &vmContext{blockCtx: evmtypes.BlockContext{
|
||||
BlockNumber: 1,
|
||||
}, txCtx: evmtypes.TxContext{GasPrice: uint256.NewInt(100000)}}
|
||||
tracer, err := New(code, new(Context))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ret, err := runTrace(tracer, ctx)
|
||||
if err != nil {
|
||||
return nil, err.Error()
|
||||
}
|
||||
return ret, ""
|
||||
}
|
||||
for i, tt := range []struct {
|
||||
code string
|
||||
want string
|
||||
fail string
|
||||
}{
|
||||
{ // 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; }}",
|
||||
want: `[[],[],[]]`,
|
||||
}, { // 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; }}",
|
||||
want: `["0","0","0"]`,
|
||||
}, { // 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; }}",
|
||||
want: `["0","0","0"]`,
|
||||
}, { // tests some general counting
|
||||
code: "{count: 0, step: function() { this.count += 1; }, fault: function() {}, result: function() { return this.count; }}",
|
||||
want: `3`,
|
||||
}, { // tests that depth is reported correctly
|
||||
code: "{depths: [], step: function(log) { this.depths.push(log.stack.length()); }, fault: function() {}, result: function() { return this.depths; }}",
|
||||
want: `[0,1,2]`,
|
||||
}, { // tests to-string of opcodes
|
||||
code: "{opcodes: [], step: function(log) { this.opcodes.push(log.op.toString()); }, fault: function() {}, result: function() { return this.opcodes; }}",
|
||||
want: `["PUSH1","PUSH1","STOP"]`,
|
||||
}, { // tests intrinsic gas
|
||||
code: "{depths: [], step: function() {}, fault: function() {}, result: function(ctx) { return ctx.gasPrice+'.'+ctx.gasUsed+'.'+ctx.intrinsicGas; }}",
|
||||
want: `"100000.6.21000"`,
|
||||
},
|
||||
} {
|
||||
if have, err := execTracer(tt.code); tt.want != string(have) || tt.fail != err {
|
||||
t.Errorf("testcase %d: expected return value to be '%s' got '%s', error to be '%s' got '%s'\n\tcode: %v", i, tt.want, string(have), tt.fail, err, tt.code)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestHalt(t *testing.T) {
|
||||
t.Skip("duktape doesn't support abortion")
|
||||
|
||||
timeout := errors.New("stahp")
|
||||
vmctx := testCtx()
|
||||
tracer, err := New("{step: function() { while(1); }, result: function() { return null; }}", new(Context))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
go func() {
|
||||
time.Sleep(1 * time.Second)
|
||||
tracer.Stop(timeout)
|
||||
}()
|
||||
|
||||
if _, err = runTrace(tracer, vmctx); err.Error() != "stahp in server-side tracer function 'step'" {
|
||||
t.Errorf("Expected timeout error, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHaltBetweenSteps(t *testing.T) {
|
||||
tracer, err := New("{step: function() {}, fault: function() {}, result: function() { return null; }}", new(Context))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
env := vm.NewEVM(evmtypes.BlockContext{
|
||||
BlockNumber: 1,
|
||||
}, evmtypes.TxContext{GasPrice: &uint256.Int{}}, &dummyStatedb{}, params.TestChainConfig, vm.Config{Debug: true, Tracer: tracer})
|
||||
contract := vm.NewContract(&account{}, &account{}, uint256.NewInt(0), 0, false)
|
||||
|
||||
tracer.CaptureStart(env, common.Address{}, common.Address{}, false, false, nil, 0, &uint256.Int{}, nil)
|
||||
tracer.CaptureState(0, 0, 0, 0, &vm.ScopeContext{Contract: contract}, nil, 0, nil) //nolint:errcheck
|
||||
timeout := errors.New("stahp")
|
||||
tracer.Stop(timeout)
|
||||
tracer.CaptureState(0, 0, 0, 0, &vm.ScopeContext{Contract: contract}, nil, 0, nil) //nolint:errcheck
|
||||
|
||||
if _, err := tracer.GetResult(); err.Error() != timeout.Error() {
|
||||
t.Errorf("Expected timeout error, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestNoStepExec tests a regular value transfer (no exec), and accessing the statedb
|
||||
// in 'result'
|
||||
func TestNoStepExec(t *testing.T) {
|
||||
runEmptyTrace := func(tracer *Tracer, vmctx *vmContext) (json.RawMessage, error) {
|
||||
env := vm.NewEVM(vmctx.blockCtx, vmctx.txCtx, &dummyStatedb{}, params.TestChainConfig, vm.Config{Debug: true, Tracer: tracer})
|
||||
startGas := uint64(10000)
|
||||
contract := vm.NewContract(account{}, account{}, uint256.NewInt(1), startGas, true)
|
||||
tracer.CaptureStart(env, contract.Caller(), contract.Address(), false, false, nil, 0, uint256.NewInt(0), nil)
|
||||
tracer.CaptureEnd(nil, startGas-contract.Gas, nil)
|
||||
return tracer.GetResult()
|
||||
}
|
||||
execTracer := func(code string) []byte {
|
||||
t.Helper()
|
||||
ctx := &vmContext{blockCtx: evmtypes.BlockContext{BlockNumber: 1}, txCtx: evmtypes.TxContext{GasPrice: uint256.NewInt(100000)}}
|
||||
tracer, err := New(code, new(Context))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ret, err := runEmptyTrace(tracer, ctx)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return ret
|
||||
}
|
||||
for i, tt := range []struct {
|
||||
code string
|
||||
want string
|
||||
}{
|
||||
{ // tests that we don't panic on accessing the db methods
|
||||
code: "{depths: [], step: function() {}, fault: function() {}, result: function(ctx, db){ return db.getBalance(ctx.to)} }",
|
||||
want: `"0"`,
|
||||
},
|
||||
} {
|
||||
if have := execTracer(tt.code); tt.want != string(have) {
|
||||
t.Errorf("testcase %d: expected return value to be %s got %s\n\tcode: %v", i, tt.want, string(have), tt.code)
|
||||
}
|
||||
}
|
||||
}
|
@ -14,40 +14,59 @@
|
||||
// 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 tracers is a collection of JavaScript transaction tracers.
|
||||
// Package tracers is a manager for transaction tracing engines.
|
||||
package tracers
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"unicode"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
|
||||
"github.com/ledgerwatch/erigon/eth/tracers/internal/tracers"
|
||||
"github.com/ledgerwatch/erigon/common"
|
||||
"github.com/ledgerwatch/erigon/core/vm"
|
||||
)
|
||||
|
||||
// all contains all the built in JavaScript tracers by name.
|
||||
var all = make(map[string]string)
|
||||
|
||||
// camel converts a snake cased input string into a camel cased output.
|
||||
func camel(str string) string {
|
||||
pieces := strings.Split(str, "_")
|
||||
for i := 1; i < len(pieces); i++ {
|
||||
pieces[i] = string(unicode.ToUpper(rune(pieces[i][0]))) + pieces[i][1:]
|
||||
}
|
||||
return strings.Join(pieces, "")
|
||||
// Context contains some contextual infos for a transaction execution that is not
|
||||
// available from within the EVM object.
|
||||
type Context struct {
|
||||
BlockHash common.Hash // Hash of the block the tx is contained within (zero if dangling tx or call)
|
||||
TxIndex int // Index of the transaction within a block (zero if dangling tx or call)
|
||||
TxHash common.Hash // Hash of the transaction being traced (zero if dangling call)
|
||||
}
|
||||
|
||||
// init retrieves the JavaScript transaction tracers included in go-ethereum.
|
||||
func init() {
|
||||
for _, file := range tracers.AssetNames() {
|
||||
name := camel(strings.TrimSuffix(file, ".js"))
|
||||
all[name] = string(tracers.MustAsset(file))
|
||||
// Tracer interface extends vm.EVMLogger and additionally
|
||||
// allows collecting the tracing result.
|
||||
type Tracer interface {
|
||||
vm.EVMLogger
|
||||
GetResult() (json.RawMessage, error)
|
||||
// Stop terminates execution of the tracer at the first opportune moment.
|
||||
Stop(err error)
|
||||
}
|
||||
|
||||
type lookupFunc func(string, *Context, json.RawMessage) (Tracer, error)
|
||||
|
||||
var (
|
||||
lookups []lookupFunc
|
||||
)
|
||||
|
||||
// RegisterLookup registers a method as a lookup for tracers, meaning that
|
||||
// users can invoke a named tracer through that lookup. If 'wildcard' is true,
|
||||
// then the lookup will be placed last. This is typically meant for interpreted
|
||||
// engines (js) which can evaluate dynamic user-supplied code.
|
||||
func RegisterLookup(wildcard bool, lookup lookupFunc) {
|
||||
if wildcard {
|
||||
lookups = append(lookups, lookup)
|
||||
} else {
|
||||
lookups = append([]lookupFunc{lookup}, lookups...)
|
||||
}
|
||||
}
|
||||
|
||||
// tracer retrieves a specific JavaScript tracer by name.
|
||||
func tracer(name string) (string, bool) {
|
||||
if tracer, ok := all[name]; ok {
|
||||
return tracer, true
|
||||
// New returns a new instance of a tracer, by iterating through the
|
||||
// registered lookups.
|
||||
func New(code string, ctx *Context, cfg json.RawMessage) (Tracer, error) {
|
||||
for _, lookup := range lookups {
|
||||
if tracer, err := lookup(code, ctx, cfg); err == nil {
|
||||
return tracer, nil
|
||||
}
|
||||
}
|
||||
return "", false
|
||||
return nil, errors.New("tracer not found")
|
||||
}
|
||||
|
@ -14,7 +14,7 @@
|
||||
// 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 tracers
|
||||
package tracers_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
@ -27,6 +27,7 @@ import (
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
"unicode"
|
||||
|
||||
"github.com/ledgerwatch/erigon-lib/kv/memdb"
|
||||
"github.com/ledgerwatch/erigon/common"
|
||||
@ -43,6 +44,10 @@ import (
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/holiman/uint256"
|
||||
|
||||
// Force-load native and js packages, to trigger registration
|
||||
"github.com/ledgerwatch/erigon/eth/tracers"
|
||||
_ "github.com/ledgerwatch/erigon/eth/tracers/js"
|
||||
)
|
||||
|
||||
// To generate a new callTracer test, copy paste the makeTest method below into
|
||||
@ -95,6 +100,15 @@ var makeTest = function(tx, rewind) {
|
||||
}
|
||||
*/
|
||||
|
||||
// camel converts a snake cased input string into a camel cased output.
|
||||
func camel(str string) string {
|
||||
pieces := strings.Split(str, "_")
|
||||
for i := 1; i < len(pieces); i++ {
|
||||
pieces[i] = string(unicode.ToUpper(rune(pieces[i][0]))) + pieces[i][1:]
|
||||
}
|
||||
return strings.Join(pieces, "")
|
||||
}
|
||||
|
||||
// callTrace is the result of a callTracer run.
|
||||
type callTrace struct {
|
||||
Type string `json:"type"`
|
||||
@ -181,7 +195,7 @@ func TestPrestateTracerCreate2(t *testing.T) {
|
||||
statedb, _ := tests.MakePreState(rules, tx, alloc, context.BlockNumber)
|
||||
|
||||
// Create the tracer, the EVM environment and run it
|
||||
tracer, err := New("prestateTracer", new(Context))
|
||||
tracer, err := tracers.New("prestateTracer", new(tracers.Context), json.RawMessage{})
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create call tracer: %v", err)
|
||||
}
|
||||
@ -260,7 +274,7 @@ func TestCallTracer(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
|
||||
// Create the tracer, the EVM environment and run it
|
||||
tracer, err := New("callTracer", new(Context))
|
||||
tracer, err := tracers.New("callTracer", new(tracers.Context), json.RawMessage{})
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create call tracer: %v", err)
|
||||
}
|
||||
|
@ -3,6 +3,7 @@ package transactions
|
||||
import (
|
||||
"context"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"sort"
|
||||
@ -151,7 +152,7 @@ func TraceTx(
|
||||
// Construct the JavaScript tracer to execute with
|
||||
if tracer, err = tracers.New(*config.Tracer, &tracers.Context{
|
||||
TxHash: txCtx.TxHash,
|
||||
}); err != nil {
|
||||
}, json.RawMessage{}); err != nil {
|
||||
stream.WriteNil()
|
||||
return err
|
||||
}
|
||||
@ -159,7 +160,7 @@ func TraceTx(
|
||||
deadlineCtx, cancel := context.WithTimeout(ctx, timeout)
|
||||
go func() {
|
||||
<-deadlineCtx.Done()
|
||||
tracer.(*tracers.Tracer).Stop(errors.New("execution timeout"))
|
||||
tracer.(tracers.Tracer).Stop(errors.New("execution timeout"))
|
||||
}()
|
||||
defer cancel()
|
||||
streaming = false
|
||||
@ -210,7 +211,7 @@ func TraceTx(
|
||||
stream.WriteString(returnVal)
|
||||
stream.WriteObjectEnd()
|
||||
} else {
|
||||
if r, err1 := tracer.(*tracers.Tracer).GetResult(); err1 == nil {
|
||||
if r, err1 := tracer.(tracers.Tracer).GetResult(); err1 == nil {
|
||||
stream.Write(r)
|
||||
} else {
|
||||
return err1
|
||||
|
Loading…
Reference in New Issue
Block a user