Skip to content

Commit 9be9106

Browse files
committed
gas dimension tracing: linter issues, exhaustruct on gas dimension work
1 parent 4bfd741 commit 9be9106

5 files changed

+77
-24
lines changed

eth/tracers/live/tx_gas_dimension_logger.go

-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ func (t *txGasDimensionLiveTraceLogger) OnTxEnd(
7979
receipt *types.Receipt,
8080
err error,
8181
) {
82-
8382
// first call the native tracer's OnTxEnd
8483
t.GasDimensionTracer.OnTxEnd(receipt, err)
8584

eth/tracers/native/base_gas_dimension_tracer.go

+60-11
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,18 @@ func NewBaseGasDimensionTracer(chainConfig *params.ChainConfig) BaseGasDimension
5858
refundAccumulated: 0,
5959
prevAccessListAddresses: map[common.Address]int{},
6060
prevAccessListSlots: []map[common.Hash]struct{}{},
61+
env: nil,
62+
txHash: common.Hash{},
63+
gasUsed: 0,
64+
gasUsedForL1: 0,
65+
gasUsedForL2: 0,
66+
intrinsicGas: 0,
67+
callStack: CallGasDimensionStack{},
68+
executionGasAccumulated: 0,
69+
refundAdjusted: 0,
70+
err: nil,
71+
interrupt: atomic.Bool{},
72+
reason: nil,
6173
}
6274
}
6375

@@ -96,7 +108,7 @@ func (t *BaseGasDimensionTracer) onOpcodeStart(
96108
opcode vm.OpCode,
97109
) {
98110
if t.interrupt.Load() {
99-
return true, GasesByDimension{}, nil, vm.OpCode(op)
111+
return true, zeroGasesByDimension(), nil, vm.OpCode(op)
100112
}
101113
if depth != t.depth && depth != t.depth-1 {
102114
t.interrupt.Store(true)
@@ -107,7 +119,7 @@ func (t *BaseGasDimensionTracer) onOpcodeStart(
107119
depth,
108120
t.callStack,
109121
)
110-
return true, GasesByDimension{}, nil, vm.OpCode(op)
122+
return true, zeroGasesByDimension(), nil, vm.OpCode(op)
111123
}
112124
if t.depth != len(t.callStack)+1 {
113125
t.interrupt.Store(true)
@@ -118,18 +130,18 @@ func (t *BaseGasDimensionTracer) onOpcodeStart(
118130
len(t.callStack),
119131
t.callStack,
120132
)
121-
return true, GasesByDimension{}, nil, vm.OpCode(op)
133+
return true, zeroGasesByDimension(), nil, vm.OpCode(op)
122134
}
123135

124136
// get the gas dimension function
125137
// if it's not a call, directly calculate the gas dimensions for the opcode
126138
f := GetCalcGasDimensionFunc(vm.OpCode(op))
127-
var fErr error = nil
139+
var fErr error
128140
gasesByDimension, callStackInfo, fErr = f(t, pc, op, gas, cost, scope, rData, depth, err)
129141
if fErr != nil {
130142
t.interrupt.Store(true)
131143
t.reason = fErr
132-
return true, GasesByDimension{}, nil, vm.OpCode(op)
144+
return true, zeroGasesByDimension(), nil, vm.OpCode(op)
133145
}
134146
opcode = vm.OpCode(op)
135147

@@ -141,7 +153,7 @@ func (t *BaseGasDimensionTracer) onOpcodeStart(
141153
opcode.String(),
142154
callStackInfo,
143155
)
144-
return true, GasesByDimension{}, nil, vm.OpCode(op)
156+
return true, zeroGasesByDimension(), nil, vm.OpCode(op)
145157
}
146158
return false, gasesByDimension, callStackInfo, opcode
147159
}
@@ -181,7 +193,7 @@ func (t *BaseGasDimensionTracer) callFinishFunction(
181193
if !ok {
182194
t.interrupt.Store(true)
183195
t.reason = fmt.Errorf("call stack is unexpectedly empty %d %d %d", pc, depth, t.depth)
184-
return true, 0, CallGasDimensionStackInfo{}, GasesByDimension{}
196+
return true, 0, zeroCallGasDimensionStackInfo(), zeroGasesByDimension()
185197
}
186198
finishFunction := GetFinishCalcGasDimensionFunc(stackInfo.GasDimensionInfo.Op)
187199
if finishFunction == nil {
@@ -191,18 +203,18 @@ func (t *BaseGasDimensionTracer) callFinishFunction(
191203
stackInfo.GasDimensionInfo.Op.String(),
192204
pc,
193205
)
194-
return true, 0, CallGasDimensionStackInfo{}, GasesByDimension{}
206+
return true, 0, zeroCallGasDimensionStackInfo(), zeroGasesByDimension()
195207
}
196208
// IMPORTANT NOTE: for some reason the only reliable way to actually get the gas cost of the call
197209
// is to subtract gas at time of call from gas at opcode AFTER return
198210
// you can't trust the `gas` field on the call itself. I wonder if the gas field is an estimation
199211
gasUsedByCall = stackInfo.GasDimensionInfo.GasCounterAtTimeOfCall - gas
200-
var finishErr error = nil
212+
var finishErr error
201213
finishGasesByDimension, finishErr = finishFunction(gasUsedByCall, stackInfo.ExecutionCost, stackInfo.GasDimensionInfo)
202214
if finishErr != nil {
203215
t.interrupt.Store(true)
204216
t.reason = finishErr
205-
return true, 0, CallGasDimensionStackInfo{}, GasesByDimension{}
217+
return true, 0, zeroCallGasDimensionStackInfo(), zeroGasesByDimension()
206218
}
207219
return false, gasUsedByCall, stackInfo, finishGasesByDimension
208220
}
@@ -211,7 +223,7 @@ func (t *BaseGasDimensionTracer) callFinishFunction(
211223
// then we need to track the execution gas
212224
// of our own code so that when the call returns,
213225
// we can write the gas dimensions for the call opcode itself
214-
func (t *BaseGasDimensionTracer) updateExecutionCost(cost uint64) {
226+
func (t *BaseGasDimensionTracer) updateCallChildExecutionCost(cost uint64) {
215227
if len(t.callStack) > 0 {
216228
t.callStack.UpdateExecutionCost(cost)
217229
}
@@ -326,6 +338,43 @@ func (t *BaseGasDimensionTracer) adjustRefund(gasUsedByL2BeforeRefunds, refund u
326338
return refundAdjusted
327339
}
328340

341+
// zeroGasesByDimension returns a GasesByDimension struct with all fields set to zero
342+
func zeroGasesByDimension() GasesByDimension {
343+
return GasesByDimension{
344+
OneDimensionalGasCost: 0,
345+
Computation: 0,
346+
StateAccess: 0,
347+
StateGrowth: 0,
348+
HistoryGrowth: 0,
349+
StateGrowthRefund: 0,
350+
ChildExecutionCost: 0,
351+
}
352+
}
353+
354+
// zeroCallGasDimensionInfo returns a CallGasDimensionInfo struct with all fields set to zero
355+
func zeroCallGasDimensionInfo() CallGasDimensionInfo {
356+
return CallGasDimensionInfo{
357+
Pc: 0,
358+
Op: 0,
359+
GasCounterAtTimeOfCall: 0,
360+
MemoryExpansionCost: 0,
361+
AccessListComputationCost: 0,
362+
AccessListStateAccessCost: 0,
363+
IsValueSentWithCall: false,
364+
InitCodeCost: 0,
365+
HashCost: 0,
366+
}
367+
}
368+
369+
// zeroCallGasDimensionStackInfo returns a CallGasDimensionStackInfo struct with all fields set to zero
370+
func zeroCallGasDimensionStackInfo() CallGasDimensionStackInfo {
371+
return CallGasDimensionStackInfo{
372+
GasDimensionInfo: zeroCallGasDimensionInfo(),
373+
ExecutionCost: 0,
374+
DimensionLogPosition: 0,
375+
}
376+
}
377+
329378
// ############################################################################
330379
// OUTPUTS
331380
// ############################################################################

eth/tracers/native/gas_dimension_calc.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func (c *CallGasDimensionStack) Push(info CallGasDimensionStackInfo) { // gasDim
6161
// Pop a CallGasDimensionStackInfo from the stack, returning false if the stack is empty
6262
func (c *CallGasDimensionStack) Pop() (CallGasDimensionStackInfo, bool) {
6363
if len(*c) == 0 {
64-
return CallGasDimensionStackInfo{}, false
64+
return zeroCallGasDimensionStackInfo(), false
6565
}
6666
last := (*c)[len(*c)-1]
6767
*c = (*c)[:len(*c)-1]
@@ -389,8 +389,8 @@ func calcStateReadCallGas(
389389
}
390390

391391
var memExpansionCost uint64 = 0
392-
var memErr error = nil
393392
if memExpansionOffset+memExpansionSize != 0 {
393+
var memErr error
394394
memExpansionCost, memErr = memoryExpansionCost(scope.MemoryData(), memExpansionOffset, memExpansionSize)
395395
if memErr != nil {
396396
return GasesByDimension{}, nil, memErr
@@ -668,8 +668,8 @@ func calcReadAndStoreCallGas(
668668
}
669669

670670
var memExpansionCost uint64 = 0
671-
var memErr error = nil
672671
if memExpansionOffset+memExpansionSize != 0 {
672+
var memErr error
673673
memExpansionCost, memErr = memoryExpansionCost(scope.MemoryData(), memExpansionOffset, memExpansionSize)
674674
if memErr != nil {
675675
return GasesByDimension{}, nil, memErr
@@ -836,9 +836,8 @@ func calcSStoreGas(
836836
}
837837
t.SetRefundAccumulated(currentRefund)
838838
}
839-
ret := GasesByDimension{
840-
OneDimensionalGasCost: cost,
841-
}
839+
ret := zeroGasesByDimension()
840+
ret.OneDimensionalGasCost = cost
842841
if cost >= params.SstoreSetGas { // 22100 case and 20000 case
843842
accessCost := cost - params.SstoreSetGas
844843
ret = GasesByDimension{
@@ -1138,5 +1137,6 @@ func outOfGas(gas uint64) (GasesByDimension, *CallGasDimensionInfo, error) {
11381137
StateGrowth: 0,
11391138
HistoryGrowth: 0,
11401139
StateGrowthRefund: 0,
1140+
ChildExecutionCost: 0,
11411141
}, nil, nil
11421142
}

eth/tracers/native/tx_gas_dimension_by_opcode.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ func NewTxGasDimensionByOpcodeTracer(
3131
_ json.RawMessage,
3232
chainConfig *params.ChainConfig,
3333
) (*tracers.Tracer, error) {
34-
3534
t := &TxGasDimensionByOpcodeTracer{
3635
BaseGasDimensionTracer: NewBaseGasDimensionTracer(chainConfig),
3736
OpcodeToDimensions: make(map[vm.OpCode]GasesByDimension),
@@ -114,7 +113,7 @@ func (t *TxGasDimensionByOpcodeTracer) OnOpcode(
114113

115114
t.depth -= 1
116115
}
117-
t.updateExecutionCost(gasesByDimension.OneDimensionalGasCost)
116+
t.updateCallChildExecutionCost(gasesByDimension.OneDimensionalGasCost)
118117
}
119118
addresses, slots := t.env.StateDB.GetAccessList()
120119
t.updatePrevAccessList(addresses, slots)

eth/tracers/native/tx_gas_dimension_logger.go

+10-4
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ func NewTxGasDimensionLogger(
5757
_ json.RawMessage,
5858
chainConfig *params.ChainConfig,
5959
) (*tracers.Tracer, error) {
60-
6160
t := &TxGasDimensionLogger{
6261
BaseGasDimensionTracer: NewBaseGasDimensionTracer(chainConfig),
6362
logs: make([]DimensionLog, 0),
@@ -89,7 +88,7 @@ func (t *TxGasDimensionLogger) OnOpcode(
8988
err error,
9089
) {
9190
interrupted, gasesByDimension, callStackInfo, opcode := t.onOpcodeStart(pc, op, gas, cost, scope, rData, depth, err)
92-
// if an error occured, it was stored in the tracer's reason field
91+
// if an error occurred, it was stored in the tracer's reason field
9392
// and we should return immediately
9493
if interrupted {
9594
return
@@ -105,7 +104,14 @@ func (t *TxGasDimensionLogger) OnOpcode(
105104
StateGrowth: gasesByDimension.StateGrowth,
106105
HistoryGrowth: gasesByDimension.HistoryGrowth,
107106
StateGrowthRefund: gasesByDimension.StateGrowthRefund,
108-
Err: err,
107+
ChildExecutionCost: gasesByDimension.ChildExecutionCost,
108+
// the following are considered unknown at this point in the tracer lifecycle
109+
// and are only filled in after the finish function is called
110+
CallRealGas: 0,
111+
CallMemoryExpansion: 0,
112+
CreateInitCodeCost: 0,
113+
Create2HashCost: 0,
114+
Err: err,
109115
})
110116

111117
// if callStackInfo is not nil then we need to take a note of the index of the
@@ -140,7 +146,7 @@ func (t *TxGasDimensionLogger) OnOpcode(
140146
t.depth -= 1
141147
}
142148

143-
t.updateExecutionCost(gasesByDimension.OneDimensionalGasCost)
149+
t.updateCallChildExecutionCost(gasesByDimension.OneDimensionalGasCost)
144150
}
145151
addresses, slots := t.env.StateDB.GetAccessList()
146152
t.updatePrevAccessList(addresses, slots)

0 commit comments

Comments
 (0)