Skip to content

Commit 9c6b5b9

Browse files
s1nagballetholiman
authored
eth, eth/tracers: include intrinsic gas in calltracer, expose for all 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>
1 parent 25c0bd9 commit 9c6b5b9

6 files changed

Lines changed: 92 additions & 94 deletions

File tree

‎eth/api_tracer.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -794,7 +794,7 @@ func (api *PrivateDebugAPI) traceTx(ctx context.Context, message core.Message, v
794794
}
795795
}
796796
// Constuct the JavaScript tracer to execute with
797-
if tracer, err = tracers.New(*config.Tracer); err != nil {
797+
if tracer, err = tracers.New(*config.Tracer, txContext); err != nil {
798798
return nil, err
799799
}
800800
// Handle timeouts and RPC cancellations

‎eth/tracers/internal/tracers/assets.go‎

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎eth/tracers/internal/tracers/prestate_tracer.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
var toBal = bigInt(this.prestate[toHex(ctx.to)].balance.slice(2), 16);
5656

5757
this.prestate[toHex(ctx.to)].balance = '0x'+toBal.subtract(ctx.value).toString(16);
58-
this.prestate[toHex(ctx.from)].balance = '0x'+fromBal.add(ctx.value).toString(16);
58+
this.prestate[toHex(ctx.from)].balance = '0x'+fromBal.add(ctx.value).add((ctx.gasUsed + ctx.intrinsicGas) * ctx.gasPrice).toString(16);
5959

6060
// Decrement the caller's nonce, and remove empty create targets
6161
this.prestate[toHex(ctx.from)].nonce--;

‎eth/tracers/tracer.go‎

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,11 @@ import (
2727

2828
"github.com/ethereum/go-ethereum/common"
2929
"github.com/ethereum/go-ethereum/common/hexutil"
30+
"github.com/ethereum/go-ethereum/core"
3031
"github.com/ethereum/go-ethereum/core/vm"
3132
"github.com/ethereum/go-ethereum/crypto"
3233
"github.com/ethereum/go-ethereum/log"
33-
"gopkg.in/olebedev/go-duktape.v3"
34+
duktape "gopkg.in/olebedev/go-duktape.v3"
3435
)
3536

3637
// bigIntegerJS is the minified version of https://github.com/peterolson/BigInteger.js.
@@ -316,7 +317,7 @@ type Tracer struct {
316317
// New instantiates a new tracer instance. code specifies a Javascript snippet,
317318
// which must evaluate to an expression returning an object with 'step', 'fault'
318319
// and 'result' functions.
319-
func New(code string) (*Tracer, error) {
320+
func New(code string, txCtx vm.TxContext) (*Tracer, error) {
320321
// Resolve any tracers by name and assemble the tracer object
321322
if tracer, ok := tracer(code); ok {
322323
code = tracer
@@ -335,6 +336,8 @@ func New(code string) (*Tracer, error) {
335336
depthValue: new(uint),
336337
refundValue: new(uint),
337338
}
339+
tracer.ctx["gasPrice"] = txCtx.GasPrice
340+
338341
// Set up builtins for this environment
339342
tracer.vm.PushGlobalGoFunction("toHex", func(ctx *duktape.Context) int {
340343
ctx.PushString(hexutil.Encode(popSlice(ctx)))
@@ -546,6 +549,18 @@ func (jst *Tracer) CaptureState(env *vm.EVM, pc uint64, op vm.OpCode, gas, cost
546549
// Initialize the context if it wasn't done yet
547550
if !jst.inited {
548551
jst.ctx["block"] = env.Context.BlockNumber.Uint64()
552+
// Compute intrinsic gas
553+
isHomestead := env.ChainConfig().IsHomestead(env.Context.BlockNumber)
554+
isIstanbul := env.ChainConfig().IsIstanbul(env.Context.BlockNumber)
555+
var input []byte
556+
if data, ok := jst.ctx["input"].([]byte); ok {
557+
input = data
558+
}
559+
intrinsicGas, err := core.IntrinsicGas(input, jst.ctx["type"] == "CREATE", isHomestead, isIstanbul)
560+
if err != nil {
561+
return err
562+
}
563+
jst.ctx["intrinsicGas"] = intrinsicGas
549564
jst.inited = true
550565
}
551566
// If tracing was interrupted, set the error and stop
@@ -597,8 +612,8 @@ func (jst *Tracer) CaptureFault(env *vm.EVM, pc uint64, op vm.OpCode, gas, cost
597612
// CaptureEnd is called after the call finishes to finalize the tracing.
598613
func (jst *Tracer) CaptureEnd(output []byte, gasUsed uint64, t time.Duration, err error) error {
599614
jst.ctx["output"] = output
600-
jst.ctx["gasUsed"] = gasUsed
601615
jst.ctx["time"] = t.String()
616+
jst.ctx["gasUsed"] = gasUsed
602617

603618
if err != nil {
604619
jst.ctx["error"] = err.Error()

0 commit comments

Comments
 (0)