Skip to content

Commit 11dc0da

Browse files
Add ArbOwner impl for resource constraints
1 parent 2688401 commit 11dc0da

File tree

6 files changed

+67
-3
lines changed

6 files changed

+67
-3
lines changed

arbos/constraints/constraints.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ type ResourceSet struct {
2323
weights [multigas.NumResourceKind]ResourceWeight
2424
}
2525

26+
const MaxResourceWeight = 1_000_000
27+
2628
// EmptyResourceSet creates a new set with all weights initialized to zero.
2729
func EmptyResourceSet() ResourceSet {
2830
return ResourceSet{

arbos/constraints/storage.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,3 +129,23 @@ func (src *StorageResourceConstraints) Write(rc *ResourceConstraints) error {
129129
}
130130
return src.storage.Set(data)
131131
}
132+
133+
// SetConstraint adds or updates a resource constraint in storage.
134+
func (src *StorageResourceConstraints) SetConstraint(resources ResourceSet, periodSecs uint32, targetPerSec uint64) error {
135+
rc, err := src.Load()
136+
if err != nil {
137+
return err
138+
}
139+
rc.Set(resources, PeriodSecs(periodSecs), targetPerSec)
140+
return src.Write(rc)
141+
}
142+
143+
// ClearConstraint removes a resource constraint from storage.
144+
func (src *StorageResourceConstraints) ClearConstraint(resources ResourceSet, periodSecs uint32) error {
145+
rc, err := src.Load()
146+
if err != nil {
147+
return err
148+
}
149+
rc.Clear(resources, PeriodSecs(periodSecs))
150+
return src.Write(rc)
151+
}

precompiles/ArbOwner.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@ import (
1010
"fmt"
1111
"math/big"
1212

13+
"github.com/ethereum/go-ethereum/arbitrum/multigas"
1314
"github.com/ethereum/go-ethereum/common"
1415
"github.com/ethereum/go-ethereum/params"
1516

17+
"github.com/offchainlabs/nitro/arbos/constraints"
1618
"github.com/offchainlabs/nitro/arbos/l1pricing"
1719
"github.com/offchainlabs/nitro/arbos/programs"
1820
"github.com/offchainlabs/nitro/util/arbmath"
@@ -453,3 +455,43 @@ func (con ArbOwner) SetChainConfig(c ctx, evm mech, serializedChainConfig []byte
453455
func (con ArbOwner) SetCalldataPriceIncrease(c ctx, _ mech, enable bool) error {
454456
return c.State.Features().SetCalldataPriceIncrease(enable)
455457
}
458+
459+
type resourceWeight struct {
460+
ResourceKind multigas.ResourceKind
461+
Weight uint64
462+
}
463+
464+
func parseResourceWeights(resources []resourceWeight) (constraints.ResourceSet, error) {
465+
rs := constraints.EmptyResourceSet()
466+
if len(resources) == 0 {
467+
return constraints.ResourceSet{}, fmt.Errorf("at least one resource is required")
468+
}
469+
for _, r := range resources {
470+
if r.Weight == 0 || r.Weight > constraints.MaxResourceWeight {
471+
return constraints.ResourceSet{}, fmt.Errorf("resource weight for kind %d must be in range [1, %d]", r.ResourceKind, constraints.MaxResourceWeight)
472+
}
473+
if rs.HasResource(r.ResourceKind) {
474+
return constraints.ResourceSet{}, fmt.Errorf("duplicate resource kind %d", r.ResourceKind)
475+
}
476+
rs = rs.WithResource(r.ResourceKind, constraints.ResourceWeight(r.Weight))
477+
}
478+
return rs, nil
479+
}
480+
481+
// SetResourceConstraint adds or updates a resource constraint with specified resource kinds and weights
482+
func (con ArbOwner) SetResourceConstraint(c ctx, evm mech, resources []resourceWeight, periodSecs uint32, targetPerSec uint64) error {
483+
res, err := parseResourceWeights(resources)
484+
if err != nil {
485+
return err
486+
}
487+
return c.State.ResourceConstraints().SetConstraint(res, periodSecs, targetPerSec)
488+
}
489+
490+
// ClearConstraint removes a resource constraint
491+
func (con ArbOwner) ClearConstraint(c ctx, evm mech, resources []resourceWeight, periodSecs uint32) error {
492+
res, err := parseResourceWeights(resources)
493+
if err != nil {
494+
return err
495+
}
496+
return c.State.ResourceConstraints().ClearConstraint(res, periodSecs)
497+
}

0 commit comments

Comments
 (0)