|
| 1 | +// Copyright 2025, Offchain Labs, Inc. |
| 2 | +// For license information, see https://github.com/OffchainLabs/nitro/blob/master/LICENSE.md |
| 3 | + |
| 4 | +package constraints |
| 5 | + |
| 6 | +import ( |
| 7 | + "math/big" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/stretchr/testify/require" |
| 11 | + |
| 12 | + "github.com/ethereum/go-ethereum/arbitrum/multigas" |
| 13 | + |
| 14 | + "github.com/offchainlabs/nitro/arbos/burn" |
| 15 | + "github.com/offchainlabs/nitro/arbos/l2pricing" |
| 16 | + "github.com/offchainlabs/nitro/arbos/storage" |
| 17 | +) |
| 18 | + |
| 19 | +func TestConstraintsModelTwoResources(t *testing.T) { |
| 20 | + // Params: target = 5M/sec, Δ = 10s |
| 21 | + var target uint64 = 5_000_000 |
| 22 | + const periodSecs = PeriodSecs(10) |
| 23 | + const iterations = 30 |
| 24 | + |
| 25 | + // Setup new constraint-based pricing model with 2 resources |
| 26 | + constraints := NewResourceConstraints() |
| 27 | + resources := EmptyResourceSet(). |
| 28 | + WithResources( |
| 29 | + multigas.ResourceKindComputation, |
| 30 | + multigas.ResourceKindStorageAccess, |
| 31 | + ) |
| 32 | + constraints.Set(resources, periodSecs, target) |
| 33 | + |
| 34 | + model := PricingState{constraints: *constraints} |
| 35 | + |
| 36 | + // Base fee floor |
| 37 | + baseFee := big.NewInt(100_000_000) |
| 38 | + |
| 39 | + // Phase 1: exceed target to force fee increase |
| 40 | + for i := 0; i < iterations; i++ { |
| 41 | + mg := multigas.MultiGasFromPairs( |
| 42 | + multigas.Pair{Kind: multigas.ResourceKindComputation, Amount: 5}, |
| 43 | + multigas.Pair{Kind: multigas.ResourceKindStorageAccess, Amount: 2}, |
| 44 | + ) |
| 45 | + model.constraints.Get(resources, periodSecs).AddToBacklog(mg) |
| 46 | + |
| 47 | + newFee := model.UpdatePricingModel(baseFee, 1) |
| 48 | + |
| 49 | + // Fee should never fall below baseFee during surge |
| 50 | + require.GreaterOrEqualf(t, newFee.Cmp(baseFee), 0, |
| 51 | + "fee dropped below base fee at iter %d", i) |
| 52 | + } |
| 53 | + |
| 54 | + // Phase 2: no usage, backlog drains and fee should decay back |
| 55 | + for i := 0; i < iterations*2; i++ { |
| 56 | + newFee := model.UpdatePricingModel(baseFee, 1) |
| 57 | + |
| 58 | + // Fee must eventually reach the floor |
| 59 | + if i == iterations*2-1 { |
| 60 | + require.Equal(t, 0, newFee.Cmp(baseFee), |
| 61 | + "fee should decay back to base fee") |
| 62 | + } |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +func TestConstraintsModelVersusLegacy(t *testing.T) { |
| 67 | + // Test parameters |
| 68 | + var gasUsedPerSecond int64 = 8_000_000 // >7M target to accumulate backlog |
| 69 | + var iterations int = 50 |
| 70 | + var periodSecs = PeriodSecs(12) |
| 71 | + |
| 72 | + // Initialize L2PricingState with legacy pricing model |
| 73 | + burner := burn.NewSystemBurner(nil, false) |
| 74 | + storage := storage.NewMemoryBacked(burner) |
| 75 | + require.NoError(t, l2pricing.InitializeL2PricingState(storage)) |
| 76 | + l2PricingState := l2pricing.OpenL2PricingState(storage) |
| 77 | + |
| 78 | + // Match new model |
| 79 | + _ = l2PricingState.SetBacklogTolerance(0) // no tolerance |
| 80 | + require.NoError(t, l2PricingState.SetSpeedLimitPerSecond(l2pricing.InitialSpeedLimitPerSecondV6)) |
| 81 | + |
| 82 | + // Setup constraint-based pricing model with a single gas constraint |
| 83 | + constraints := NewResourceConstraints() |
| 84 | + resources := EmptyResourceSet(). |
| 85 | + WithResources( |
| 86 | + multigas.ResourceKindComputation, |
| 87 | + multigas.ResourceKindStorageAccess, |
| 88 | + multigas.ResourceKindStorageGrowth, |
| 89 | + multigas.ResourceKindHistoryGrowth, |
| 90 | + multigas.ResourceKindWasmComputation, |
| 91 | + ) |
| 92 | + constraints.Set(resources, periodSecs, l2pricing.InitialSpeedLimitPerSecondV6) |
| 93 | + model := PricingState{ |
| 94 | + constraints: *constraints, |
| 95 | + } |
| 96 | + |
| 97 | + minBaseFee, _ := l2PricingState.MinBaseFeeWei() |
| 98 | + |
| 99 | + for i := 1; i < iterations+1; i++ { |
| 100 | + // L2PricingState model update |
| 101 | + baseFeeLegacy, _ := l2PricingState.BaseFeeWei() |
| 102 | + burner.Restrict(l2PricingState.AddToGasPool(-gasUsedPerSecond)) // negative = gas consumed |
| 103 | + l2PricingState.UpdatePricingModel(baseFeeLegacy, 1, false) |
| 104 | + legacyFee, _ := l2PricingState.BaseFeeWei() |
| 105 | + |
| 106 | + // Constraint-based model update |
| 107 | + // #nosec G115 -- gasUsedPerSecond is a fixed positive constant for testing |
| 108 | + mg := multigas.ComputationGas(uint64(gasUsedPerSecond)) |
| 109 | + model.constraints.Get(resources, periodSecs).AddToBacklog(mg) |
| 110 | + newFee := model.UpdatePricingModel(minBaseFee, 1) |
| 111 | + |
| 112 | + // Expect new fee to be slightly lower (slower growth) than legacy, |
| 113 | + // within about 7% after 50 iterations due to inertia differences. |
| 114 | + diff := new(big.Float).Quo( |
| 115 | + new(big.Float).SetInt(legacyFee), |
| 116 | + new(big.Float).SetInt(newFee), |
| 117 | + ) |
| 118 | + val, _ := diff.Float64() |
| 119 | + |
| 120 | + require.InEpsilonf(t, 1.0, val, 0.015, // within 1.5% tolerance |
| 121 | + "fees differ too much at iteration %d: legacy=%s new=%s", |
| 122 | + i, legacyFee.String(), newFee.String()) |
| 123 | + |
| 124 | + // Uncomment for debug output |
| 125 | + // fmt.Printf("%-4d %-15s %-15s %-10.4f\n", i, legacyFee.String(), newFee.String(), val) |
| 126 | + } |
| 127 | +} |
0 commit comments