Skip to content

Commit 52efdc6

Browse files
authored
Merge pull request #579 from OffchainLabs/add-multi-dimensional-multi-constraint-pricer-algorithm
core: Add multigas to EndTxHook and safeDecrement
2 parents 8558511 + 6a75e60 commit 52efdc6

File tree

4 files changed

+76
-3
lines changed

4 files changed

+76
-3
lines changed

arbitrum/multigas/resources.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,30 @@ 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+
current := res.gas[kind]
331+
var reduced uint64
332+
if current < gas {
333+
reduced = current
334+
res.gas[kind] = 0
335+
} else {
336+
reduced = gas
337+
res.gas[kind] = current - gas
338+
}
339+
340+
if res.total < reduced {
341+
res.total = 0
342+
} else {
343+
res.total -= reduced
344+
}
345+
346+
return res
347+
}
348+
325349
// SaturatingIncrementInto increments the given resource kind and the total
326350
// in place by gas. On overflow, the affected field(s) are clamped to MaxUint64.
327351
// Unlike SaturatingIncrement, this method mutates the receiver directly and

arbitrum/multigas/resources_test.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,55 @@ 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 = MultiGasFromPairs(
404+
Pair{ResourceKindComputation, 10},
405+
Pair{ResourceKindStorageAccess, 10},
406+
)
407+
408+
newGas = gas.SaturatingDecrement(ResourceKindComputation, 20)
409+
if got, want := newGas.Get(ResourceKindComputation), uint64(0); got != want {
410+
t.Errorf("unexpected comp gas: got %v, want %v", got, want)
411+
}
412+
if got, want := newGas.Get(ResourceKindStorageAccess), uint64(10); got != want {
413+
t.Errorf("unexpected storage access gas: got %v, want %v", got, want)
414+
}
415+
if got, want := newGas.SingleGas(), uint64(10); got != want {
416+
t.Errorf("unexpected total (should drop by 10 only): got %v, want %v", got, want)
417+
}
418+
419+
if got, want := newGas.SingleGas(),
420+
newGas.Get(ResourceKindComputation)+newGas.Get(ResourceKindStorageAccess); got != want {
421+
t.Errorf("total/sum mismatch: total=%v sum=%v", got, want)
422+
}
423+
424+
// total-only decrement case
425+
gas = MultiGasFromPairs(
426+
Pair{ResourceKindComputation, math.MaxUint64 - 1},
427+
Pair{ResourceKindHistoryGrowth, 1},
428+
)
429+
430+
newGas = gas.SaturatingDecrement(ResourceKindHistoryGrowth, 1)
431+
if got, want := newGas.Get(ResourceKindHistoryGrowth), uint64(0); got != want {
432+
t.Errorf("unexpected history growth gas: got %v, want %v", got, want)
433+
}
434+
435+
if got, want := newGas.SingleGas(), uint64(math.MaxUint64-1); got != want {
436+
t.Errorf("unexpected total gas: got %v, want %v", got, want)
437+
}
438+
}
439+
391440
func TestMultiGasSingleGasTracking(t *testing.T) {
392441
g := ZeroGas()
393442
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)