Skip to content

Commit e91e74a

Browse files
Add multigas to EndTxHook and safeDecrement
1 parent 42a3074 commit e91e74a

File tree

4 files changed

+60
-3
lines changed

4 files changed

+60
-3
lines changed

arbitrum/multigas/resources.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,26 @@ func (z MultiGas) SaturatingIncrement(kind ResourceKind, gas uint64) MultiGas {
322322
return res
323323
}
324324

325+
// SaturatingDecrement returns a copy of z with the given resource kind
326+
// and the total decremented by gas. On underflow, the field(s) are clamped to 0.
327+
func (z MultiGas) SaturatingDecrement(kind ResourceKind, gas uint64) MultiGas {
328+
res := z
329+
330+
if v, c := bits.Sub64(res.gas[kind], gas, 0); c != 0 {
331+
res.gas[kind] = 0 // clamp
332+
} else {
333+
res.gas[kind] = v
334+
}
335+
336+
if t, c := bits.Sub64(res.total, gas, 0); c != 0 {
337+
res.total = 0 // clamp
338+
} else {
339+
res.total = t
340+
}
341+
342+
return res
343+
}
344+
325345
// SaturatingIncrementInto increments the given resource kind and the total
326346
// in place by gas. On overflow, the affected field(s) are clamped to MaxUint64.
327347
// Unlike SaturatingIncrement, this method mutates the receiver directly and

arbitrum/multigas/resources_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,43 @@ func TestSaturatingIncrementIntoClampsOnOverflow(t *testing.T) {
388388
}
389389
}
390390

391+
func TestSaturatingDecrement(t *testing.T) {
392+
// normal decrement
393+
gas := ComputationGas(10)
394+
newGas := gas.SaturatingDecrement(ResourceKindComputation, 5)
395+
if got, want := newGas.Get(ResourceKindComputation), uint64(5); got != want {
396+
t.Errorf("unexpected computation gas: got %v, want %v", got, want)
397+
}
398+
if got, want := newGas.SingleGas(), uint64(5); got != want {
399+
t.Errorf("unexpected single gas: got %v, want %v", got, want)
400+
}
401+
402+
// saturating decrement on kind
403+
gas = ComputationGas(0)
404+
newGas = gas.SaturatingDecrement(ResourceKindComputation, 1)
405+
if got, want := newGas.Get(ResourceKindComputation), uint64(0); got != want {
406+
t.Errorf("expected computation gas to clamp to zero: got %v, want %v", got, want)
407+
}
408+
if got, want := newGas.SingleGas(), uint64(0); got != want {
409+
t.Errorf("expected total to clamp to zero: got %v, want %v", got, want)
410+
}
411+
412+
// total-only decrement case
413+
gas = MultiGasFromPairs(
414+
Pair{ResourceKindComputation, math.MaxUint64 - 1},
415+
Pair{ResourceKindHistoryGrowth, 1},
416+
)
417+
418+
newGas = gas.SaturatingDecrement(ResourceKindHistoryGrowth, 1)
419+
if got, want := newGas.Get(ResourceKindHistoryGrowth), uint64(0); got != want {
420+
t.Errorf("unexpected history growth gas: got %v, want %v", got, want)
421+
}
422+
423+
if got, want := newGas.SingleGas(), uint64(math.MaxUint64-1); got != want {
424+
t.Errorf("unexpected total gas: got %v, want %v", got, want)
425+
}
426+
}
427+
391428
func TestMultiGasSingleGasTracking(t *testing.T) {
392429
g := ZeroGas()
393430
if got := g.SingleGas(); got != 0 {

core/state_transition.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -774,7 +774,7 @@ func (st *stateTransition) execute() (*ExecutionResult, error) {
774774
tracer.CaptureArbitrumTransfer(nil, &tipReceipient, tipAmount, false, tracing.BalanceIncreaseRewardTransactionFee)
775775
}
776776

777-
st.evm.ProcessingHook.EndTxHook(st.gasRemaining, vmerr == nil)
777+
st.evm.ProcessingHook.EndTxHook(st.gasRemaining, usedMultiGas, vmerr == nil)
778778

779779
// Arbitrum: record self destructs
780780
if tracer := st.evm.Config.Tracer; tracer != nil && tracer.CaptureArbitrumTransfer != nil {

core/vm/evm_arbitrum.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ type TxProcessingHook interface {
5050
HeldGas() uint64
5151
NonrefundableGas() uint64
5252
DropTip() bool
53-
EndTxHook(totalGasUsed uint64, evmSuccess bool)
53+
EndTxHook(totalGasUsed uint64, usedMultiGas multigas.MultiGas, evmSuccess bool)
5454
ScheduledTxes() types.Transactions
5555
L1BlockNumber(blockCtx BlockContext) (uint64, error)
5656
L1BlockHash(blockCtx BlockContext, l1BlocKNumber uint64) (common.Hash, error)
@@ -83,7 +83,7 @@ func (p DefaultTxProcessor) NonrefundableGas() uint64 { return 0 }
8383

8484
func (p DefaultTxProcessor) DropTip() bool { return false }
8585

86-
func (p DefaultTxProcessor) EndTxHook(_ uint64, _ bool) {}
86+
func (p DefaultTxProcessor) EndTxHook(_ uint64, _ multigas.MultiGas, _ bool) {}
8787

8888
func (p DefaultTxProcessor) ScheduledTxes() types.Transactions {
8989
return types.Transactions{}

0 commit comments

Comments
 (0)