Skip to content

Commit dfaec7c

Browse files
Add precompile impl for constraints control
1 parent 3fa2297 commit dfaec7c

File tree

3 files changed

+49
-1
lines changed

3 files changed

+49
-1
lines changed

arbos/constraints/storage.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"fmt"
88
"io"
99

10+
"github.com/ethereum/go-ethereum/arbitrum/multigas"
1011
"github.com/ethereum/go-ethereum/rlp"
1112
)
1213

@@ -129,3 +130,40 @@ func (src *StorageResourceConstraints) Write(rc *ResourceConstraints) error {
129130
}
130131
return src.storage.Set(data)
131132
}
133+
134+
// SetConstraint adds or updates a resource constraint in storage.
135+
func (src *StorageResourceConstraints) SetConstraint(resourceKinds []uint8, resourceWeights []uint64, periodSecs uint32, targetPerSec uint64) error {
136+
if len(resourceKinds) != len(resourceWeights) {
137+
return fmt.Errorf("resourceKinds and resourceWeights must have the same length")
138+
}
139+
if len(resourceKinds) == 0 {
140+
return fmt.Errorf("at least one resourceKind is required")
141+
}
142+
resSet := EmptyResourceSet()
143+
for i, r := range resourceKinds {
144+
resSet = resSet.WithResource(multigas.ResourceKind(r), ResourceWeight(resourceWeights[i]))
145+
}
146+
rc, err := src.Load()
147+
if err != nil {
148+
return err
149+
}
150+
rc.Set(resSet, PeriodSecs(periodSecs), targetPerSec)
151+
return src.Write(rc)
152+
}
153+
154+
func (src *StorageResourceConstraints) ClearConstraint(resourceKinds []uint8, periodSecs uint32) error {
155+
if len(resourceKinds) == 0 {
156+
return fmt.Errorf("at least one resourceKind is required")
157+
}
158+
159+
resSet := EmptyResourceSet()
160+
for _, r := range resourceKinds {
161+
resSet = resSet.WithResource(multigas.ResourceKind(r), 1)
162+
}
163+
rc, err := src.Load()
164+
if err != nil {
165+
return err
166+
}
167+
rc.Clear(resSet, PeriodSecs(periodSecs))
168+
return src.Write(rc)
169+
}

precompiles/ArbOwner.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,3 +453,13 @@ func (con ArbOwner) SetChainConfig(c ctx, evm mech, serializedChainConfig []byte
453453
func (con ArbOwner) SetCalldataPriceIncrease(c ctx, _ mech, enable bool) error {
454454
return c.State.Features().SetCalldataPriceIncrease(enable)
455455
}
456+
457+
// SetResourceConstraint adds or updates a resource constraint with specified resource kinds and weights
458+
func (con ArbOwner) SetResourceConstraint(c ctx, evm mech, resourceKinds []uint8, resourceWeights []uint64, periodSecs uint32, targetPerSec uint64) error {
459+
return c.State.ResourceConstraints().SetConstraint(resourceKinds, resourceWeights, periodSecs, targetPerSec)
460+
}
461+
462+
// ClearConstraint removes a resource constraint
463+
func (con ArbOwner) ClearConstraint(c ctx, evm mech, resourceKinds []uint8, periodSecs uint32) error {
464+
return c.State.ResourceConstraints().ClearConstraint(resourceKinds, periodSecs)
465+
}

0 commit comments

Comments
 (0)