Skip to content

Commit 21079e2

Browse files
Adjust pricing formula with weight normalisation by max
1 parent 1420803 commit 21079e2

File tree

4 files changed

+28
-19
lines changed

4 files changed

+28
-19
lines changed

arbos/l2pricing/l2pricing.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,15 @@ func (ps *L2PricingState) setMultiGasConstraintsFromSingleGasConstraints() error
201201
return fmt.Errorf("failed to read backlog from constraint %d: %w", i, err)
202202
}
203203

204-
// Transfer with only computation resource weight to match single-gas constraint scale.
205-
weights := map[uint8]uint64{uint8(multigas.ResourceKindComputation): 1}
204+
// Transfer to multi-gas constraint with equal weights
205+
weights := map[uint8]uint64{
206+
uint8(multigas.ResourceKindComputation): 1,
207+
uint8(multigas.ResourceKindHistoryGrowth): 1,
208+
uint8(multigas.ResourceKindStorageAccess): 1,
209+
uint8(multigas.ResourceKindStorageGrowth): 1,
210+
uint8(multigas.ResourceKindL2Calldata): 1,
211+
uint8(multigas.ResourceKindWasmComputation): 1,
212+
}
206213

207214
var adjustmentWindow uint32
208215
if window > math.MaxUint32 {

arbos/l2pricing/model.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -305,14 +305,14 @@ func (ps *L2PricingState) CalcMultiGasConstraintsExponents() ([multigas.NumResou
305305
if err != nil {
306306
return [multigas.NumResourceKind]arbmath.Bips{}, err
307307
}
308-
sumWeights, err := constraint.SumWeights()
308+
maxWeight, err := constraint.MaxWeight()
309309
if err != nil {
310310
return [multigas.NumResourceKind]arbmath.Bips{}, err
311311
}
312312

313313
divisor := arbmath.SaturatingCastToBips(
314314
arbmath.SaturatingUMul(uint64(adjustmentWindow),
315-
arbmath.SaturatingUMul(target, sumWeights)))
315+
arbmath.SaturatingUMul(target, maxWeight)))
316316

317317
usedResources, err := constraint.UsedResources()
318318
if err != nil {

arbos/l2pricing/multi_gas_constraint.go

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const (
2121
targetOffset uint64 = iota
2222
adjustmentWindowOffset
2323
backlogOffset
24-
sumWeightsOffset
24+
maxWeightOffset
2525
weightedResourcesBaseOffset
2626
)
2727

@@ -31,7 +31,7 @@ type MultiGasConstraint struct {
3131
target storage.StorageBackedUint64
3232
adjustmentWindow storage.StorageBackedUint32
3333
backlog storage.StorageBackedUint64
34-
sumWeights storage.StorageBackedUint64
34+
maxWeight storage.StorageBackedUint64
3535
weightedResources [multigas.NumResourceKind]storage.StorageBackedUint64
3636
}
3737

@@ -41,7 +41,7 @@ func OpenMultiGasConstraint(sto *storage.Storage) *MultiGasConstraint {
4141
target: sto.OpenStorageBackedUint64(targetOffset),
4242
adjustmentWindow: sto.OpenStorageBackedUint32(adjustmentWindowOffset),
4343
backlog: sto.OpenStorageBackedUint64(backlogOffset),
44-
sumWeights: sto.OpenStorageBackedUint64(sumWeightsOffset),
44+
maxWeight: sto.OpenStorageBackedUint64(maxWeightOffset),
4545
}
4646
for i := range int(multigas.NumResourceKind) {
4747
// #nosec G115 safe: NumResourceKind < 2^32
@@ -62,7 +62,7 @@ func (c *MultiGasConstraint) Clear() error {
6262
if err := c.backlog.Clear(); err != nil {
6363
return err
6464
}
65-
if err := c.sumWeights.Clear(); err != nil {
65+
if err := c.maxWeight.Clear(); err != nil {
6666
return err
6767
}
6868
for i := range int(multigas.NumResourceKind) {
@@ -75,12 +75,14 @@ func (c *MultiGasConstraint) Clear() error {
7575

7676
// SetResourceWeights assigns per-resource weight multipliers for this constraint.
7777
func (c *MultiGasConstraint) SetResourceWeights(weights map[uint8]uint64) error {
78-
var total uint64
78+
var maxWeight uint64
7979
for kind, weight := range weights {
8080
if _, err := multigas.CheckResourceKind(kind); err != nil {
8181
return err
8282
}
83-
total = arbmath.SaturatingUAdd(total, weight)
83+
if weight > maxWeight {
84+
maxWeight = weight
85+
}
8486
}
8587
for i := range int(multigas.NumResourceKind) {
8688
// #nosec G115 safe: NumResourceKind < 2^32
@@ -89,7 +91,7 @@ func (c *MultiGasConstraint) SetResourceWeights(weights map[uint8]uint64) error
8991
return err
9092
}
9193
}
92-
return c.sumWeights.Set(total)
94+
return c.maxWeight.Set(maxWeight)
9395
}
9496

9597
// GrowBacklog adds the resource usage in multiGas to this constraint's backlog.
@@ -156,8 +158,8 @@ func (c *MultiGasConstraint) ResourceWeight(kind uint8) (uint64, error) {
156158
return c.weightedResources[kind].Get()
157159
}
158160

159-
func (c *MultiGasConstraint) SumWeights() (uint64, error) {
160-
return c.sumWeights.Get()
161+
func (c *MultiGasConstraint) MaxWeight() (uint64, error) {
162+
return c.maxWeight.Get()
161163
}
162164

163165
func (c *MultiGasConstraint) ResourcesWithWeights() (map[multigas.ResourceKind]uint64, error) {

arbos/l2pricing/multi_gas_constraint_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@ func TestMultiGasConstraint(t *testing.T) {
5252
require.Contains(t, used, multigas.ResourceKindComputation)
5353
require.Contains(t, used, multigas.ResourceKindStorageAccess)
5454

55-
weights, err := c.SumWeights()
55+
maxWeight, err := c.MaxWeight()
5656
require.NoError(t, err)
57-
require.Equal(t, uint64(30), weights)
57+
require.Equal(t, uint64(20), maxWeight)
5858

5959
require.NoError(t, c.Clear())
6060
target, _ = c.Target()
@@ -68,9 +68,9 @@ func TestMultiGasConstraint(t *testing.T) {
6868
require.NoError(t, err)
6969
require.Len(t, used, 0)
7070

71-
weights, err = c.SumWeights()
71+
maxWeight, err = c.MaxWeight()
7272
require.NoError(t, err)
73-
require.Equal(t, uint64(0), weights)
73+
require.Equal(t, uint64(0), maxWeight)
7474
}
7575

7676
func TestMultiGasConstraintResourceWeightsValidation(t *testing.T) {
@@ -90,9 +90,9 @@ func TestMultiGasConstraintResourceWeightsValidation(t *testing.T) {
9090
}
9191
require.NoError(t, c.SetResourceWeights(valid))
9292

93-
total, err := c.sumWeights.Get()
93+
maxWeight, err := c.maxWeight.Get()
9494
require.NoError(t, err)
95-
require.Equal(t, uint64(10), total)
95+
require.Equal(t, uint64(7), maxWeight)
9696
}
9797

9898
func TestMultiGasConstraintBacklogAggregation(t *testing.T) {

0 commit comments

Comments
 (0)