44package constraints
55
66import (
7+ "fmt"
78 "io"
89
910 "github.com/ethereum/go-ethereum/rlp"
1011)
1112
13+ // Version of the RLP serialization format for ResourceConstraints.
14+ const ResourceConstraintsVersion uint8 = 1
15+
1216// storageBytes defines the interface for ArbOS storage.
1317type storageBytes interface {
1418 Get () ([]byte , error )
@@ -27,19 +31,25 @@ func NewStorageResourceConstraints(storage storageBytes) *StorageResourceConstra
2731 }
2832}
2933
30- type resourceConstraintRLP struct {
34+ type rlpResourceConstraint struct {
3135 Resources []ResourceWeight
3236 Period PeriodSecs
3337 TargetPerSec uint64
3438 Backlog uint64
3539}
3640
41+ // rlpConstraints is the RLP-encoded wrapper used for persistence.
42+ type rlpConstraints struct {
43+ Version uint8
44+ Constraints []* ResourceConstraint
45+ }
46+
3747// EncodeRLP encodes ResourceConstraint deterministically,
3848// ensuring the fixed-length weights array is preserved.
3949func (c * ResourceConstraint ) EncodeRLP (w io.Writer ) error {
4050 weights := make ([]ResourceWeight , len (c .Resources .weights ))
4151 copy (weights , c .Resources .weights [:])
42- return rlp .Encode (w , resourceConstraintRLP {
52+ return rlp .Encode (w , rlpResourceConstraint {
4353 Resources : weights ,
4454 Period : c .Period ,
4555 TargetPerSec : c .TargetPerSec ,
@@ -50,7 +60,7 @@ func (c *ResourceConstraint) EncodeRLP(w io.Writer) error {
5060// DecodeRLP decodes ResourceConstraint deterministically,
5161// padding or truncating the weights slice to the correct array length.
5262func (c * ResourceConstraint ) DecodeRLP (s * rlp.Stream ) error {
53- var raw resourceConstraintRLP
63+ var raw rlpResourceConstraint
5464 if err := s .Decode (& raw ); err != nil {
5565 return err
5666 }
@@ -79,18 +89,20 @@ func (src *StorageResourceConstraints) Load() (*ResourceConstraints, error) {
7989 return NewResourceConstraints (), nil
8090 }
8191
82- var list [] * ResourceConstraint
83- if err := rlp .DecodeBytes (data , & list ); err != nil {
92+ var payload rlpConstraints
93+ if err := rlp .DecodeBytes (data , & payload ); err != nil {
8494 return nil , err
8595 }
96+ if payload .Version != ResourceConstraintsVersion {
97+ return nil , fmt .Errorf ("unsupported constraints version %d" , payload .Version )
98+ }
8699
87100 rc := NewResourceConstraints ()
88- for _ , c := range list {
101+ for _ , c := range payload . Constraints {
89102 rc .Set (c .Resources , c .Period , c .TargetPerSec )
90103 ptr := rc .Get (c .Resources , c .Period )
91104 ptr .Backlog = c .Backlog
92105 }
93-
94106 return rc , nil
95107}
96108
@@ -106,7 +118,12 @@ func (src *StorageResourceConstraints) Write(rc *ResourceConstraints) error {
106118 return src .storage .Set (nil )
107119 }
108120
109- data , err := rlp .EncodeToBytes (list )
121+ payload := rlpConstraints {
122+ Version : ResourceConstraintsVersion ,
123+ Constraints : list ,
124+ }
125+
126+ data , err := rlp .EncodeToBytes (& payload )
110127 if err != nil {
111128 return err
112129 }
0 commit comments