Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

eth, eth/tracers: expose gas used in tx to js tracer #22038

Merged
merged 7 commits into from
Dec 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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:/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