Skip to content

Commit

Permalink
eth/tracers: trace system tx should add intrinsicGas (#1855)
Browse files Browse the repository at this point in the history
  • Loading branch information
zlacfzy committed Sep 7, 2023
1 parent a437355 commit a0cb4d0
Show file tree
Hide file tree
Showing 11 changed files with 32 additions and 0 deletions.
1 change: 1 addition & 0 deletions core/vm/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type EVMLogger interface {
// Transaction level
CaptureTxStart(gasLimit uint64)
CaptureTxEnd(restGas uint64)
CaptureSystemTxEnd(intrinsicGas uint64)
// Top call frame
CaptureStart(env *EVM, from common.Address, to common.Address, create bool, input []byte, gas uint64, value *big.Int)
CaptureEnd(output []byte, gasUsed uint64, err error)
Expand Down
3 changes: 3 additions & 0 deletions eth/tracers/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -927,6 +927,7 @@ func (api *API) traceTx(ctx context.Context, message core.Message, txctx *Contex
}()
defer cancel()

var intrinsicGas uint64 = 0
// Run the transaction with tracing enabled.
if posa, ok := api.backend.Engine().(consensus.PoSA); ok && message.From() == vmctx.Coinbase &&
posa.IsSystemContract(message.To()) && message.GasPrice().Cmp(big.NewInt(0)) == 0 {
Expand All @@ -935,13 +936,15 @@ func (api *API) traceTx(ctx context.Context, message core.Message, txctx *Contex
statedb.SetBalance(consensus.SystemAddress, big.NewInt(0))
statedb.AddBalance(vmctx.Coinbase, balance)
}
intrinsicGas, _ = core.IntrinsicGas(message.Data(), message.AccessList(), false, true, true)
}

// Call Prepare to clear out the statedb access list
statedb.Prepare(txctx.TxHash, txctx.TxIndex)
if _, err = core.ApplyMessage(vmenv, message, new(core.GasPool).AddGas(message.Gas())); err != nil {
return nil, fmt.Errorf("tracing failed: %w", err)
}
tracer.CaptureSystemTxEnd(intrinsicGas)
return tracer.GetResult()
}

Expand Down
2 changes: 2 additions & 0 deletions eth/tracers/js/goja.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,8 @@ func (t *jsTracer) CaptureTxStart(gasLimit uint64) {
// transaction processing.
func (t *jsTracer) CaptureTxEnd(restGas uint64) {}

func (t *jsTracer) CaptureSystemTxEnd(intrinsicGas uint64) {}

// CaptureStart implements the Tracer interface to initialize the tracing operation.
func (t *jsTracer) CaptureStart(env *vm.EVM, from common.Address, to common.Address, create bool, input []byte, gas uint64, value *big.Int) {
t.env = env
Expand Down
2 changes: 2 additions & 0 deletions eth/tracers/logger/access_list_tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,8 @@ func (*AccessListTracer) CaptureTxStart(gasLimit uint64) {}

func (*AccessListTracer) CaptureTxEnd(restGas uint64) {}

func (*AccessListTracer) CaptureSystemTxEnd(intrinsicGas uint64) {}

// AccessList returns the current accesslist maintained by the tracer.
func (a *AccessListTracer) AccessList() types.AccessList {
return a.list.accessList()
Expand Down
6 changes: 6 additions & 0 deletions eth/tracers/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,10 @@ func (l *StructLogger) CaptureTxEnd(restGas uint64) {
l.usedGas = l.gasLimit - restGas
}

func (l *StructLogger) CaptureSystemTxEnd(intrinsicGas uint64) {
l.usedGas -= intrinsicGas
}

// StructLogs returns the captured log entries.
func (l *StructLogger) StructLogs() []StructLog { return l.logs }

Expand Down Expand Up @@ -398,6 +402,8 @@ func (*mdLogger) CaptureTxStart(gasLimit uint64) {}

func (*mdLogger) CaptureTxEnd(restGas uint64) {}

func (*mdLogger) CaptureSystemTxEnd(intrinsicGas uint64) {}

// ExecutionResult groups all structured logs emitted by the EVM
// while replaying a transaction in debug mode as well as transaction
// execution status, the amount of gas used and the return value
Expand Down
2 changes: 2 additions & 0 deletions eth/tracers/logger/logger_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,5 @@ func (l *JSONLogger) CaptureExit(output []byte, gasUsed uint64, err error) {}
func (l *JSONLogger) CaptureTxStart(gasLimit uint64) {}

func (l *JSONLogger) CaptureTxEnd(restGas uint64) {}

func (l *JSONLogger) CaptureSystemTxEnd(intrinsicGas uint64) {}
2 changes: 2 additions & 0 deletions eth/tracers/native/4byte.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ func (*fourByteTracer) CaptureTxStart(gasLimit uint64) {}

func (*fourByteTracer) CaptureTxEnd(restGas uint64) {}

func (*fourByteTracer) CaptureSystemTxEnd(intrinsicGas uint64) {}

// GetResult returns the json-encoded nested list of call traces, and any
// error arising from the encoding or forceful termination (via `Stop`).
func (t *fourByteTracer) GetResult() (json.RawMessage, error) {
Expand Down
4 changes: 4 additions & 0 deletions eth/tracers/native/call.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,10 @@ func (t *callTracer) CaptureTxEnd(restGas uint64) {
}
}

func (t *callTracer) CaptureSystemTxEnd(intrinsicGas uint64) {
t.callstack[0].GasUsed -= intrinsicGas
}

// GetResult returns the json-encoded nested list of call traces, and any
// error arising from the encoding or forceful termination (via `Stop`).
func (t *callTracer) GetResult() (json.RawMessage, error) {
Expand Down
6 changes: 6 additions & 0 deletions eth/tracers/native/mux.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,12 @@ func (t *muxTracer) CaptureTxEnd(restGas uint64) {
}
}

func (t *muxTracer) CaptureSystemTxEnd(intrinsicGas uint64) {
for _, t := range t.tracers {
t.CaptureSystemTxEnd(intrinsicGas)
}
}

// GetResult returns an empty json object.
func (t *muxTracer) GetResult() (json.RawMessage, error) {
resObject := make(map[string]json.RawMessage)
Expand Down
2 changes: 2 additions & 0 deletions eth/tracers/native/noop.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ func (*noopTracer) CaptureTxStart(gasLimit uint64) {}

func (*noopTracer) CaptureTxEnd(restGas uint64) {}

func (*noopTracer) CaptureSystemTxEnd(intrinsicGas uint64) {}

// GetResult returns an empty json object.
func (t *noopTracer) GetResult() (json.RawMessage, error) {
return json.RawMessage(`{}`), nil
Expand Down
2 changes: 2 additions & 0 deletions eth/tracers/native/prestate.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ func (t *prestateTracer) CaptureTxStart(gasLimit uint64) {

func (t *prestateTracer) CaptureTxEnd(restGas uint64) {}

func (t *prestateTracer) CaptureSystemTxEnd(intrinsicGas uint64) {}

// GetResult returns the json-encoded nested list of call traces, and any
// error arising from the encoding or forceful termination (via `Stop`).
func (t *prestateTracer) GetResult() (json.RawMessage, error) {
Expand Down

0 comments on commit a0cb4d0

Please sign in to comment.