|
7 | 7 | "fmt" |
8 | 8 | "io" |
9 | 9 |
|
| 10 | + "github.com/ethereum/go-ethereum/arbitrum/multigas" |
10 | 11 | "github.com/ethereum/go-ethereum/rlp" |
11 | 12 | ) |
12 | 13 |
|
@@ -129,3 +130,40 @@ func (src *StorageResourceConstraints) Write(rc *ResourceConstraints) error { |
129 | 130 | } |
130 | 131 | return src.storage.Set(data) |
131 | 132 | } |
| 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 | +} |
0 commit comments