Skip to content

Commit

Permalink
eth, eth/tracers: pass intrinsic gas to js tracers
Browse files Browse the repository at this point in the history
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
  • Loading branch information
s1na committed Dec 18, 2020
1 parent ad40b6a commit c4e1371
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 91 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, vmctx); err != nil {
if tracer, err = tracers.New(*config.Tracer, txContext); err != nil {
return nil, err
}
// Handle timeouts and RPC cancellations
Expand Down
138 changes: 54 additions & 84 deletions eth/tracers/internal/tracers/assets.go

Large diffs are not rendered by default.

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).add(ctx.txGas * ctx.gasPrice).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
19 changes: 16 additions & 3 deletions eth/tracers/tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ 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"
"github.com/ethereum/go-ethereum/params"
duktape "gopkg.in/olebedev/go-duktape.v3"
)

Expand Down Expand Up @@ -337,7 +337,6 @@ func New(code string, txCtx vm.TxContext) (*Tracer, error) {
refundValue: new(uint),
}
tracer.ctx["gasPrice"] = txCtx.GasPrice
tracer.ctx["txGas"] = params.TxGas

// Set up builtins for this environment
tracer.vm.PushGlobalGoFunction("toHex", func(ctx *duktape.Context) int {
Expand Down Expand Up @@ -550,6 +549,20 @@ 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)
input, ok := jst.ctx["input"].([]byte)
if !ok {
return errors.New("Tracer received invalid 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 @@ -601,8 +614,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
5 changes: 5 additions & 0 deletions eth/tracers/tracer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ func runTrace(tracer *Tracer, vmctx VMContext) (json.RawMessage, error) {
contract := vm.NewContract(account{}, account{}, big.NewInt(0), 10000)
contract.Code = []byte{byte(vm.PUSH1), 0x1, byte(vm.PUSH1), 0x1, 0x0}

// Needed for intrinsic gas computation
tracer.ctx["input"] = []byte{}

_, err := env.Interpreter().Run(contract, []byte{}, false)
if err != nil {
return nil, err
Expand Down Expand Up @@ -182,6 +185,8 @@ func TestHaltBetweenSteps(t *testing.T) {
if err != nil {
t.Fatal(err)
}
// Needed for intrinsic gas computation
tracer.ctx["input"] = []byte{}

env := vm.NewEVM(vm.BlockContext{BlockNumber: big.NewInt(1)}, vm.TxContext{}, &dummyStatedb{}, params.TestChainConfig, vm.Config{Debug: true, Tracer: tracer})
contract := vm.NewContract(&account{}, &account{}, big.NewInt(0), 0)
Expand Down
4 changes: 2 additions & 2 deletions eth/tracers/tracers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ func TestPrestateTracerCreate2(t *testing.T) {
_, statedb := tests.MakePreState(rawdb.NewMemoryDatabase(), alloc, false)

// Create the tracer, the EVM environment and run it
tracer, err := New("prestateTracer", context)
tracer, err := New("prestateTracer", txContext)
if err != nil {
t.Fatalf("failed to create call tracer: %v", err)
}
Expand Down Expand Up @@ -248,7 +248,7 @@ func TestCallTracer(t *testing.T) {
_, statedb := tests.MakePreState(rawdb.NewMemoryDatabase(), test.Genesis.Alloc, false)

// Create the tracer, the EVM environment and run it
tracer, err := New("callTracer", context)
tracer, err := New("callTracer", txContext)
if err != nil {
t.Fatalf("failed to create call tracer: %v", err)
}
Expand Down

0 comments on commit c4e1371

Please sign in to comment.