Skip to content

Commit 54931eb

Browse files
committed
gas dimension tracing: handle gas refund cap adjustment
1 parent 9f81c5c commit 54931eb

5 files changed

+53
-2
lines changed

eth/tracers/native/base_gas_dimension_tracer.go

+28
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ type BaseGasDimensionTracer struct {
3636
prevAccessListSlots []map[common.Hash]struct{}
3737
// the amount of refund accumulated at the current step of execution
3838
refundAccumulated uint64
39+
// in order to calculate the refund adjusted, we need to know the total execution gas
40+
// of just the opcodes of the transaction with no refunds
41+
executionGasAccumulated uint64
42+
// the amount of refund allowed at the end of the transaction, adjusted by EIP-3529
43+
refundAdjusted uint64
3944
// whether the transaction had an error, like out of gas
4045
err error
4146
// whether the tracer itself was interrupted
@@ -254,6 +259,7 @@ func (t *BaseGasDimensionTracer) OnTxEnd(receipt *types.Receipt, err error) {
254259
t.gasUsedForL1 = receipt.GasUsedForL1
255260
t.gasUsedForL2 = receipt.GasUsedForL2()
256261
t.txHash = receipt.TxHash
262+
t.refundAdjusted = t.adjustRefund(t.executionGasAccumulated+t.intrinsicGas, t.GetRefundAccumulated())
257263
}
258264

259265
// Stop signals the tracer to stop tracing
@@ -300,6 +306,26 @@ func (t *BaseGasDimensionTracer) GetPrevAccessList() (addresses map[common.Addre
300306
// Error returns the VM error captured by the trace
301307
func (t *BaseGasDimensionTracer) Error() error { return t.err }
302308

309+
// Add to the execution gas accumulated, for tracking adjusted refund
310+
func (t *BaseGasDimensionTracer) AddToExecutionGasAccumulated(gas uint64) {
311+
t.executionGasAccumulated += gas
312+
}
313+
314+
// this function implements the EIP-3529 refund adjustment
315+
// this is a copy of the logic in the state transition function
316+
func (t *BaseGasDimensionTracer) adjustRefund(gasUsedByL2BeforeRefunds, refund uint64) uint64 {
317+
var refundAdjusted uint64
318+
if !t.chainConfig.IsLondon(t.env.BlockNumber) {
319+
refundAdjusted = gasUsedByL2BeforeRefunds / params.RefundQuotient
320+
} else {
321+
refundAdjusted = gasUsedByL2BeforeRefunds / params.RefundQuotientEIP3529
322+
}
323+
if refundAdjusted > refund {
324+
return refund
325+
}
326+
return refundAdjusted
327+
}
328+
303329
// ############################################################################
304330
// OUTPUTS
305331
// ############################################################################
@@ -310,6 +336,7 @@ type BaseExecutionResult struct {
310336
GasUsedForL1 uint64 `json:"gasUsedForL1"`
311337
GasUsedForL2 uint64 `json:"gasUsedForL2"`
312338
IntrinsicGas uint64 `json:"intrinsicGas"`
339+
AdjustedRefund uint64 `json:"adjustedRefund"`
313340
Failed bool `json:"failed"`
314341
TxHash string `json:"txHash"`
315342
BlockTimestamp uint64 `json:"blockTimestamp"`
@@ -329,6 +356,7 @@ func (t *BaseGasDimensionTracer) GetBaseExecutionResult() (BaseExecutionResult,
329356
GasUsedForL1: t.gasUsedForL1,
330357
GasUsedForL2: t.gasUsedForL2,
331358
IntrinsicGas: t.intrinsicGas,
359+
AdjustedRefund: t.refundAdjusted,
332360
Failed: failed,
333361
TxHash: t.txHash.Hex(),
334362
BlockTimestamp: t.env.Time,

eth/tracers/native/proto/gas_dimension_by_opcode.pb.go

+13-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

eth/tracers/native/proto/gas_dimension_by_opcode.proto

+2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ message TxGasDimensionByOpcodeExecutionResult {
3232
uint64 gas_used_l2 = 8;
3333
// the intrinsic gas of the transaction, the static cost + calldata bytes cost
3434
uint64 intrinsic_gas = 9;
35+
// the adjusted gas refund amount after EIP-3529
36+
uint64 adjusted_refund = 10;
3537
// whether the transaction had an revert or error, like out of gas
3638
bool failed = 2;
3739
// a map of each opcode to the sum of the gas consumption categorized by dimension for that opcode

eth/tracers/native/tx_gas_dimension_by_opcode.go

+6
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ func (t *TxGasDimensionByOpcodeTracer) OnOpcode(
7373
if WasCallOrCreate(opcode) && err == nil {
7474
t.handleCallStackPush(callStackInfo)
7575
} else {
76+
// track the execution gas of all opcodes (but not the opcodes that do calls)
77+
t.AddToExecutionGasAccumulated(gasesByDimension.OneDimensionalGasCost)
7678

7779
// update the aggregrate map for this opcode
7880
accumulatedDimensions := t.OpcodeToDimensions[opcode]
@@ -97,6 +99,9 @@ func (t *TxGasDimensionByOpcodeTracer) OnOpcode(
9799
return
98100
}
99101

102+
// track the execution gas of all opcodes that do calls
103+
t.AddToExecutionGasAccumulated(finishGasesByDimension.OneDimensionalGasCost)
104+
100105
accumulatedDimensionsCall := t.OpcodeToDimensions[stackInfo.GasDimensionInfo.Op]
101106

102107
accumulatedDimensionsCall.OneDimensionalGasCost += finishGasesByDimension.OneDimensionalGasCost
@@ -157,6 +162,7 @@ func (t *TxGasDimensionByOpcodeTracer) GetProtobufResult() ([]byte, error) {
157162
GasUsedL1: baseExecutionResult.GasUsedForL1,
158163
GasUsedL2: baseExecutionResult.GasUsedForL2,
159164
IntrinsicGas: baseExecutionResult.IntrinsicGas,
165+
AdjustedRefund: baseExecutionResult.AdjustedRefund,
160166
Failed: baseExecutionResult.Failed,
161167
Dimensions: make(map[uint32]*proto.GasesByDimension),
162168
TxHash: baseExecutionResult.TxHash,

eth/tracers/native/tx_gas_dimension_logger.go

+4
Original file line numberDiff line numberDiff line change
@@ -114,11 +114,15 @@ func (t *TxGasDimensionLogger) OnOpcode(
114114
if WasCallOrCreate(opcode) && err == nil {
115115
t.handleCallStackPush(callStackInfo)
116116
} else {
117+
// track the execution gas of all opcodes (but not the opcodes that do calls)
118+
t.AddToExecutionGasAccumulated(gasesByDimension.OneDimensionalGasCost)
117119
if depth < t.depth {
118120
interrupted, gasUsedByCall, stackInfo, finishGasesByDimension := t.callFinishFunction(pc, depth, gas)
119121
if interrupted {
120122
return
121123
}
124+
// track the execution gas of all opcodes that do calls
125+
t.AddToExecutionGasAccumulated(finishGasesByDimension.OneDimensionalGasCost)
122126
callDimensionLog := t.logs[stackInfo.DimensionLogPosition]
123127
callDimensionLog.OneDimensionalGasCost = finishGasesByDimension.OneDimensionalGasCost
124128
callDimensionLog.Computation = finishGasesByDimension.Computation

0 commit comments

Comments
 (0)