Skip to content

Commit

Permalink
eth, eth/tracers: include intrinsic gas in calltracer, expose for all…
Browse files Browse the repository at this point in the history
… tracers (#22038)

* eth/tracers: share tx gas price with js tracer

* eth/tracers: use `go generate`

* eth/tracers: try with another version of go-bindata

* eth/tracers: export txGas

* eth, eth/tracers: pass intrinsic gas to js tracers

eth/tracers: include tx gas in tracers usedGas

eth/tracers: fix prestate tracer's sender balance

eth/tracers: rm unnecessary import

eth/tracers: pass intrinsicGas separately to tracer

eth/tracers: fix tests broken by lack of txdata

eth, eth/tracers: minor fix

* eth/tracers: regenerate assets + unexport test-struct + add testcase

* eth/tracers: simplify tests + make table-driven

Co-authored-by: Guillaume Ballet <gballet@gmail.com>
Co-authored-by: Martin Holst Swende <martin@swende.se>
  • Loading branch information
3 people authored Dec 27, 2020
1 parent 25c0bd9 commit 9c6b5b9
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 94 deletions.
2 changes: 1 addition & 1 deletion eth/api_tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -794,7 +794,7 @@ func (api *PrivateDebugAPI) traceTx(ctx context.Context, message core.Message, v
}
}
// Constuct the JavaScript tracer to execute with
if tracer, err = tracers.New(*config.Tracer); err != nil {
if tracer, err = tracers.New(*config.Tracer, txContext); err != nil {
return nil, err
}
// Handle timeouts and RPC cancellations
Expand Down
6 changes: 3 additions & 3 deletions eth/tracers/internal/tracers/assets.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion eth/tracers/internal/tracers/prestate_tracer.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,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).toString(16);
this.prestate[toHex(ctx.from)].balance = '0x'+fromBal.add(ctx.value).add((ctx.gasUsed + ctx.intrinsicGas) * ctx.gasPrice).toString(16);

// Decrement the caller's nonce, and remove empty create targets
this.prestate[toHex(ctx.from)].nonce--;
Expand Down
21 changes: 18 additions & 3 deletions eth/tracers/tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@ import (

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/log"
"gopkg.in/olebedev/go-duktape.v3"
duktape "gopkg.in/olebedev/go-duktape.v3"
)

// bigIntegerJS is the minified version of https://github.com/peterolson/BigInteger.js.
Expand Down Expand Up @@ -316,7 +317,7 @@ type Tracer struct {
// New instantiates a new tracer instance. code specifies a Javascript snippet,
// which must evaluate to an expression returning an object with 'step', 'fault'
// and 'result' functions.
func New(code string) (*Tracer, error) {
func New(code string, txCtx vm.TxContext) (*Tracer, error) {
// Resolve any tracers by name and assemble the tracer object
if tracer, ok := tracer(code); ok {
code = tracer
Expand All @@ -335,6 +336,8 @@ func New(code string) (*Tracer, error) {
depthValue: new(uint),
refundValue: new(uint),
}
tracer.ctx["gasPrice"] = txCtx.GasPrice

// Set up builtins for this environment
tracer.vm.PushGlobalGoFunction("toHex", func(ctx *duktape.Context) int {
ctx.PushString(hexutil.Encode(popSlice(ctx)))
Expand Down Expand Up @@ -546,6 +549,18 @@ func (jst *Tracer) CaptureState(env *vm.EVM, pc uint64, op vm.OpCode, gas, cost
// Initialize the context if it wasn't done yet
if !jst.inited {
jst.ctx["block"] = env.Context.BlockNumber.Uint64()
// Compute intrinsic gas
isHomestead := env.ChainConfig().IsHomestead(env.Context.BlockNumber)
isIstanbul := env.ChainConfig().IsIstanbul(env.Context.BlockNumber)
var input []byte
if data, ok := jst.ctx["input"].([]byte); ok {
input = data
}
intrinsicGas, err := core.IntrinsicGas(input, jst.ctx["type"] == "CREATE", isHomestead, isIstanbul)
if err != nil {
return err
}
jst.ctx["intrinsicGas"] = intrinsicGas
jst.inited = true
}
// If tracing was interrupted, set the error and stop
Expand Down Expand Up @@ -597,8 +612,8 @@ func (jst *Tracer) CaptureFault(env *vm.EVM, pc uint64, op vm.OpCode, gas, cost
// CaptureEnd is called after the call finishes to finalize the tracing.
func (jst *Tracer) CaptureEnd(output []byte, gasUsed uint64, t time.Duration, err error) error {
jst.ctx["output"] = output
jst.ctx["gasUsed"] = gasUsed
jst.ctx["time"] = t.String()
jst.ctx["gasUsed"] = gasUsed

if err != nil {
jst.ctx["error"] = err.Error()
Expand Down
Loading

0 comments on commit 9c6b5b9

Please sign in to comment.