tracer.go 10.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
// Copyright 2016 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 ethapi

import (
20
	"encoding/json"
21 22 23
	"errors"
	"fmt"
	"math/big"
24
	"time"
25 26

	"github.com/ethereum/go-ethereum/common"
27
	"github.com/ethereum/go-ethereum/common/hexutil"
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
	"github.com/ethereum/go-ethereum/core/vm"
	"github.com/robertkrimen/otto"
)

// fakeBig is used to provide an interface to Javascript for 'big.NewInt'
type fakeBig struct{}

// NewInt creates a new big.Int with the specified int64 value.
func (fb *fakeBig) NewInt(x int64) *big.Int {
	return big.NewInt(x)
}

// OpCodeWrapper provides a JavaScript-friendly wrapper around OpCode, to convince Otto to treat it
// as an object, instead of a number.
type opCodeWrapper struct {
	op vm.OpCode
}

// toNumber returns the ID of this opcode as an integer
func (ocw *opCodeWrapper) toNumber() int {
	return int(ocw.op)
}

// toString returns the string representation of the opcode
func (ocw *opCodeWrapper) toString() string {
	return ocw.op.String()
}

// isPush returns true if the op is a Push
func (ocw *opCodeWrapper) isPush() bool {
	return ocw.op.IsPush()
}

61 62 63 64 65
// MarshalJSON serializes the opcode as JSON
func (ocw *opCodeWrapper) MarshalJSON() ([]byte, error) {
	return json.Marshal(ocw.op.String())
}

66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
// toValue returns an otto.Value for the opCodeWrapper
func (ocw *opCodeWrapper) toValue(vm *otto.Otto) otto.Value {
	value, _ := vm.ToValue(ocw)
	obj := value.Object()
	obj.Set("toNumber", ocw.toNumber)
	obj.Set("toString", ocw.toString)
	obj.Set("isPush", ocw.isPush)
	return value
}

// memoryWrapper provides a JS wrapper around vm.Memory
type memoryWrapper struct {
	memory *vm.Memory
}

// slice returns the requested range of memory as a byte slice
func (mw *memoryWrapper) slice(begin, end int64) []byte {
	return mw.memory.Get(begin, end-begin)
}

// getUint returns the 32 bytes at the specified address interpreted
// as an unsigned integer
func (mw *memoryWrapper) getUint(addr int64) *big.Int {
	ret := big.NewInt(0)
	ret.SetBytes(mw.memory.GetPtr(addr, 32))
	return ret
}

// toValue returns an otto.Value for the memoryWrapper
func (mw *memoryWrapper) toValue(vm *otto.Otto) otto.Value {
	value, _ := vm.ToValue(mw)
	obj := value.Object()
	obj.Set("slice", mw.slice)
	obj.Set("getUint", mw.getUint)
	return value
}

// stackWrapper provides a JS wrapper around vm.Stack
type stackWrapper struct {
	stack *vm.Stack
}

// peek returns the nth-from-the-top element of the stack.
func (sw *stackWrapper) peek(idx int) *big.Int {
	return sw.stack.Data()[len(sw.stack.Data())-idx-1]
}

// length returns the length of the stack
func (sw *stackWrapper) length() int {
	return len(sw.stack.Data())
}

// toValue returns an otto.Value for the stackWrapper
func (sw *stackWrapper) toValue(vm *otto.Otto) otto.Value {
	value, _ := vm.ToValue(sw)
	obj := value.Object()
	obj.Set("peek", sw.peek)
	obj.Set("length", sw.length)
	return value
}

// dbWrapper provides a JS wrapper around vm.Database
type dbWrapper struct {
129
	db vm.StateDB
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
}

// getBalance retrieves an account's balance
func (dw *dbWrapper) getBalance(addr common.Address) *big.Int {
	return dw.db.GetBalance(addr)
}

// getNonce retrieves an account's nonce
func (dw *dbWrapper) getNonce(addr common.Address) uint64 {
	return dw.db.GetNonce(addr)
}

// getCode retrieves an account's code
func (dw *dbWrapper) getCode(addr common.Address) []byte {
	return dw.db.GetCode(addr)
}

// getState retrieves an account's state data for the given hash
func (dw *dbWrapper) getState(addr common.Address, hash common.Hash) common.Hash {
	return dw.db.GetState(addr, hash)
}

// exists returns true iff the account exists
func (dw *dbWrapper) exists(addr common.Address) bool {
	return dw.db.Exist(addr)
}

// toValue returns an otto.Value for the dbWrapper
func (dw *dbWrapper) toValue(vm *otto.Otto) otto.Value {
	value, _ := vm.ToValue(dw)
	obj := value.Object()
	obj.Set("getBalance", dw.getBalance)
	obj.Set("getNonce", dw.getNonce)
	obj.Set("getCode", dw.getCode)
	obj.Set("getState", dw.getState)
	obj.Set("exists", dw.exists)
	return value
}

169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
// contractWrapper provides a JS wrapper around vm.Contract
type contractWrapper struct {
	contract *vm.Contract
}

func (c *contractWrapper) caller() common.Address {
	return c.contract.Caller()
}

func (c *contractWrapper) address() common.Address {
	return c.contract.Address()
}

func (c *contractWrapper) value() *big.Int {
	return c.contract.Value()
}

func (c *contractWrapper) calldata() []byte {
	return c.contract.Input
}

func (c *contractWrapper) toValue(vm *otto.Otto) otto.Value {
	value, _ := vm.ToValue(c)
	obj := value.Object()
	obj.Set("caller", c.caller)
	obj.Set("address", c.address)
	obj.Set("value", c.value)
	obj.Set("calldata", c.calldata)
	return value
}

200 201 202
// JavascriptTracer provides an implementation of Tracer that evaluates a
// Javascript function for each VM execution step.
type JavascriptTracer struct {
203 204 205 206 207 208 209 210 211 212 213 214 215
	vm            *otto.Otto             // Javascript VM instance
	traceobj      *otto.Object           // User-supplied object to call
	log           map[string]interface{} // (Reusable) map for the `log` arg to `step`
	logvalue      otto.Value             // JS view of `log`
	memory        *memoryWrapper         // Wrapper around the VM memory
	memvalue      otto.Value             // JS view of `memory`
	stack         *stackWrapper          // Wrapper around the VM stack
	stackvalue    otto.Value             // JS view of `stack`
	db            *dbWrapper             // Wrapper around the VM environment
	dbvalue       otto.Value             // JS view of `db`
	contract      *contractWrapper       // Wrapper around the contract object
	contractvalue otto.Value             // JS view of `contract`
	err           error                  // Error, if one has occurred
216 217 218 219 220 221 222 223 224 225 226
}

// NewJavascriptTracer instantiates a new JavascriptTracer instance.
// code specifies a Javascript snippet, which must evaluate to an expression
// returning an object with 'step' and 'result' functions.
func NewJavascriptTracer(code string) (*JavascriptTracer, error) {
	vm := otto.New()
	vm.Interrupt = make(chan func(), 1)

	// Set up builtins for this environment
	vm.Set("big", &fakeBig{})
227
	vm.Set("toHex", hexutil.Encode)
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258

	jstracer, err := vm.Object("(" + code + ")")
	if err != nil {
		return nil, err
	}

	// Check the required functions exist
	step, err := jstracer.Get("step")
	if err != nil {
		return nil, err
	}
	if !step.IsFunction() {
		return nil, fmt.Errorf("Trace object must expose a function step()")
	}

	result, err := jstracer.Get("result")
	if err != nil {
		return nil, err
	}
	if !result.IsFunction() {
		return nil, fmt.Errorf("Trace object must expose a function result()")
	}

	// Create the persistent log object
	log := make(map[string]interface{})
	logvalue, _ := vm.ToValue(log)

	// Create persistent wrappers for memory and stack
	mem := &memoryWrapper{}
	stack := &stackWrapper{}
	db := &dbWrapper{}
259
	contract := &contractWrapper{}
260 261

	return &JavascriptTracer{
262 263 264 265 266 267 268 269 270 271 272 273 274
		vm:            vm,
		traceobj:      jstracer,
		log:           log,
		logvalue:      logvalue,
		memory:        mem,
		memvalue:      mem.toValue(vm),
		stack:         stack,
		stackvalue:    stack.toValue(vm),
		db:            db,
		dbvalue:       db.toValue(vm),
		contract:      contract,
		contractvalue: contract.toValue(vm),
		err:           nil,
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
	}, nil
}

// Stop terminates execution of any JavaScript
func (jst *JavascriptTracer) Stop(err error) {
	jst.vm.Interrupt <- func() {
		panic(err)
	}
}

// callSafely executes a method on a JS object, catching any panics and
// returning them as error objects.
func (jst *JavascriptTracer) callSafely(method string, argumentList ...interface{}) (ret interface{}, err error) {
	defer func() {
		if caught := recover(); caught != nil {
			switch caught := caught.(type) {
			case error:
				err = caught
			case string:
				err = errors.New(caught)
			case fmt.Stringer:
				err = errors.New(caught.String())
			default:
				panic(caught)
			}
		}
	}()

	value, err := jst.traceobj.Call(method, argumentList...)
	ret, _ = value.Export()
	return ret, err
}

308 309 310 311 312 313 314 315 316 317 318
func wrapError(context string, err error) error {
	var message string
	switch err := err.(type) {
	case *otto.Error:
		message = err.String()
	default:
		message = err.Error()
	}
	return fmt.Errorf("%v    in server-side tracer function '%v'", message, context)
}

319
// CaptureState implements the Tracer interface to trace a single step of VM execution
320
func (jst *JavascriptTracer) CaptureState(env *vm.EVM, pc uint64, op vm.OpCode, gas, cost uint64, memory *vm.Memory, stack *vm.Stack, contract *vm.Contract, depth int, err error) error {
321 322 323
	if jst.err == nil {
		jst.memory.memory = memory
		jst.stack.stack = stack
324
		jst.db.db = env.StateDB
325
		jst.contract.contract = contract
326

327 328
		ocw := &opCodeWrapper{op}

329
		jst.log["pc"] = pc
330
		jst.log["op"] = ocw.toValue(jst.vm)
331 332
		jst.log["gas"] = gas
		jst.log["gasPrice"] = cost
333 334
		jst.log["memory"] = jst.memvalue
		jst.log["stack"] = jst.stackvalue
335
		jst.log["contract"] = jst.contractvalue
336 337 338 339 340 341
		jst.log["depth"] = depth
		jst.log["account"] = contract.Address()
		jst.log["err"] = err

		_, err := jst.callSafely("step", jst.logvalue, jst.dbvalue)
		if err != nil {
342
			jst.err = wrapError("step", err)
343 344
		}
	}
345
	return nil
346 347
}

348 349 350 351 352 353
// CaptureEnd is called after the call finishes
func (jst *JavascriptTracer) CaptureEnd(output []byte, gasUsed uint64, t time.Duration) error {
	//TODO! @Arachnid please figure out of there's anything we can use this method for
	return nil
}

354 355 356 357 358 359
// GetResult calls the Javascript 'result' function and returns its value, or any accumulated error
func (jst *JavascriptTracer) GetResult() (result interface{}, err error) {
	if jst.err != nil {
		return nil, jst.err
	}

360 361 362 363 364
	result, err = jst.callSafely("result")
	if err != nil {
		err = wrapError("result", err)
	}
	return
365
}