Skip to content

Commit 49f1e84

Browse files
committed
internal/ethapi: Add support for fetching information about the current call in JS traces
1 parent cc13d57 commit 49f1e84

1 file changed

Lines changed: 62 additions & 22 deletions

File tree

‎internal/ethapi/tracer.go‎

Lines changed: 62 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"math/big"
2424

2525
"github.com/ethereum/go-ethereum/common"
26+
"github.com/ethereum/go-ethereum/common/hexutil"
2627
"github.com/ethereum/go-ethereum/core/vm"
2728
"github.com/robertkrimen/otto"
2829
)
@@ -164,20 +165,53 @@ func (dw *dbWrapper) toValue(vm *otto.Otto) otto.Value {
164165
return value
165166
}
166167

168+
// contractWrapper provides a JS wrapper around vm.Contract
169+
type contractWrapper struct {
170+
contract *vm.Contract
171+
}
172+
173+
func (c *contractWrapper) caller() common.Address {
174+
return c.contract.Caller()
175+
}
176+
177+
func (c *contractWrapper) address() common.Address {
178+
return c.contract.Address()
179+
}
180+
181+
func (c *contractWrapper) value() *big.Int {
182+
return c.contract.Value()
183+
}
184+
185+
func (c *contractWrapper) calldata() []byte {
186+
return c.contract.Input
187+
}
188+
189+
func (c *contractWrapper) toValue(vm *otto.Otto) otto.Value {
190+
value, _ := vm.ToValue(c)
191+
obj := value.Object()
192+
obj.Set("caller", c.caller)
193+
obj.Set("address", c.address)
194+
obj.Set("value", c.value)
195+
obj.Set("calldata", c.calldata)
196+
return value
197+
}
198+
167199
// JavascriptTracer provides an implementation of Tracer that evaluates a
168200
// Javascript function for each VM execution step.
169201
type JavascriptTracer struct {
170-
vm *otto.Otto // Javascript VM instance
171-
traceobj *otto.Object // User-supplied object to call
172-
log map[string]interface{} // (Reusable) map for the `log` arg to `step`
173-
logvalue otto.Value // JS view of `log`
174-
memory *memoryWrapper // Wrapper around the VM memory
175-
memvalue otto.Value // JS view of `memory`
176-
stack *stackWrapper // Wrapper around the VM stack
177-
stackvalue otto.Value // JS view of `stack`
178-
db *dbWrapper // Wrapper around the VM environment
179-
dbvalue otto.Value // JS view of `db`
180-
err error // Error, if one has occurred
202+
vm *otto.Otto // Javascript VM instance
203+
traceobj *otto.Object // User-supplied object to call
204+
log map[string]interface{} // (Reusable) map for the `log` arg to `step`
205+
logvalue otto.Value // JS view of `log`
206+
memory *memoryWrapper // Wrapper around the VM memory
207+
memvalue otto.Value // JS view of `memory`
208+
stack *stackWrapper // Wrapper around the VM stack
209+
stackvalue otto.Value // JS view of `stack`
210+
db *dbWrapper // Wrapper around the VM environment
211+
dbvalue otto.Value // JS view of `db`
212+
contract *contractWrapper // Wrapper around the contract object
213+
contractvalue otto.Value // JS view of `contract`
214+
err error // Error, if one has occurred
181215
}
182216

183217
// NewJavascriptTracer instantiates a new JavascriptTracer instance.
@@ -189,6 +223,7 @@ func NewJavascriptTracer(code string) (*JavascriptTracer, error) {
189223

190224
// Set up builtins for this environment
191225
vm.Set("big", &fakeBig{})
226+
vm.Set("toHex", hexutil.Encode)
192227

193228
jstracer, err := vm.Object("(" + code + ")")
194229
if err != nil {
@@ -220,19 +255,22 @@ func NewJavascriptTracer(code string) (*JavascriptTracer, error) {
220255
mem := &memoryWrapper{}
221256
stack := &stackWrapper{}
222257
db := &dbWrapper{}
258+
contract := &contractWrapper{}
223259

224260
return &JavascriptTracer{
225-
vm: vm,
226-
traceobj: jstracer,
227-
log: log,
228-
logvalue: logvalue,
229-
memory: mem,
230-
memvalue: mem.toValue(vm),
231-
stack: stack,
232-
stackvalue: stack.toValue(vm),
233-
db: db,
234-
dbvalue: db.toValue(vm),
235-
err: nil,
261+
vm: vm,
262+
traceobj: jstracer,
263+
log: log,
264+
logvalue: logvalue,
265+
memory: mem,
266+
memvalue: mem.toValue(vm),
267+
stack: stack,
268+
stackvalue: stack.toValue(vm),
269+
db: db,
270+
dbvalue: db.toValue(vm),
271+
contract: contract,
272+
contractvalue: contract.toValue(vm),
273+
err: nil,
236274
}, nil
237275
}
238276

@@ -283,6 +321,7 @@ func (jst *JavascriptTracer) CaptureState(env *vm.EVM, pc uint64, op vm.OpCode,
283321
jst.memory.memory = memory
284322
jst.stack.stack = stack
285323
jst.db.db = env.StateDB
324+
jst.contract.contract = contract
286325

287326
ocw := &opCodeWrapper{op}
288327

@@ -292,6 +331,7 @@ func (jst *JavascriptTracer) CaptureState(env *vm.EVM, pc uint64, op vm.OpCode,
292331
jst.log["gasPrice"] = cost
293332
jst.log["memory"] = jst.memvalue
294333
jst.log["stack"] = jst.stackvalue
334+
jst.log["contract"] = jst.contractvalue
295335
jst.log["depth"] = depth
296336
jst.log["account"] = contract.Address()
297337
jst.log["err"] = err

0 commit comments

Comments
 (0)