44package main
55
66import (
7+ "encoding/json"
78 "fmt"
89 "io"
10+ "os"
11+ "strings"
12+ "unicode"
913
1014 "github.com/spf13/pflag"
1115
@@ -99,26 +103,26 @@ func (c *LegacyConfig) Print(w io.Writer) {
99103 fmt .Fprintln (w , "Backlog tolerance:\t " , c .BacklogTolerance )
100104}
101105
102- type ConstraintsConfig struct {
106+ type SingleGasConfig struct {
103107 CommonConfig
104108 Targets []int64
105109 Windows []int64
106110 Backlogs []int64
107111}
108112
109- var DefaultConstraintConfig = ConstraintsConfig {
113+ var DefaultConstraintConfig = SingleGasConfig {
110114 Targets : []int64 {60_000_000 , 41_000_000 , 29_000_000 , 20_000_000 , 14_000_000 , 10_000_000 },
111115 Windows : []int64 {9 , 52 , 329 , 2_105 , 13_485 , 86_400 },
112116}
113117
114- func (c * ConstraintsConfig ) AddFlags (flags * pflag.FlagSet ) {
118+ func (c * SingleGasConfig ) AddFlags (flags * pflag.FlagSet ) {
115119 c .CommonConfig .AddFlags (flags )
116120 flags .Int64SliceVar (& c .Targets , "targets" , DefaultConstraintConfig .Targets , "List of constraints' targets; previously speed-limit" )
117121 flags .Int64SliceVar (& c .Windows , "windows" , DefaultConstraintConfig .Windows , "List of constraints' adjustment windows; previously inertia" )
118122 flags .Int64SliceVar (& c .Backlogs , "backlogs" , DefaultConstraintConfig .Backlogs , "List of constraints' initial backlogs" )
119123}
120124
121- func (c * ConstraintsConfig ) Print (w io.Writer ) {
125+ func (c * SingleGasConfig ) Print (w io.Writer ) {
122126 c .CommonConfig .Print (w )
123127 fmt .Fprintln (w , "Number of constraints:\t " , len (c .Targets ))
124128 for i := range len (c .Targets ) {
@@ -132,7 +136,7 @@ func (c *ConstraintsConfig) Print(w io.Writer) {
132136 }
133137}
134138
135- func (c * ConstraintsConfig ) Validate () error {
139+ func (c * SingleGasConfig ) Validate () error {
136140 for _ , target := range c .Targets {
137141 if target < 0 {
138142 return fmt .Errorf ("invalid negative target" )
@@ -159,3 +163,66 @@ func (c *ConstraintsConfig) Validate() error {
159163 }
160164 return nil
161165}
166+
167+ type MultiGasConfig struct {
168+ CommonConfig
169+ Constraints []MultiGasConstraint
170+ constraintsJson string
171+ constraintsJsonPath string
172+ printConstraints bool
173+ }
174+
175+ var DefaultMultiGasConfig = MultiGasConfig {
176+ constraintsJson : DefaultMultiGasConstraints ,
177+ printConstraints : false ,
178+ }
179+
180+ func (c * MultiGasConfig ) AddFlags (flags * pflag.FlagSet ) {
181+ c .CommonConfig .AddFlags (flags )
182+ defaultConstraints := strings .Map (func (r rune ) rune {
183+ if unicode .IsSpace (r ) {
184+ return - 1
185+ }
186+ return r
187+ }, DefaultMultiGasConstraints )
188+ flags .StringVar (& c .constraintsJson , "constraints" , defaultConstraints , "Multi-gas constraints specified as JSON" )
189+ flags .StringVar (& c .constraintsJsonPath , "constraints-path" , "" , "If set, load the constraints from the file" )
190+ flags .BoolVar (& c .printConstraints , "print-constraints" , DefaultMultiGasConfig .printConstraints , "print the constraints" )
191+ }
192+
193+ func (c * MultiGasConfig ) Print (w io.Writer ) {
194+ c .CommonConfig .Print (w )
195+ if c .printConstraints {
196+ result , err := json .MarshalIndent (c .Constraints , " " , " " )
197+ if err != nil {
198+ fmt .Fprintf (w , "Failed to print constraints: %v\n " , err .Error ())
199+ } else {
200+ fmt .Fprintf (w , "Constraints: %v\n " , string (result ))
201+ }
202+ } else {
203+ fmt .Fprintf (w , "Number of constraints: %v\n " , len (c .Constraints ))
204+ }
205+ }
206+
207+ func (c * MultiGasConfig ) Validate () error {
208+ var constraintsJson []byte
209+ if c .constraintsJsonPath != "" {
210+ var err error
211+ constraintsJson , err = os .ReadFile (c .constraintsJsonPath )
212+ if err != nil {
213+ return fmt .Errorf ("failed to read constraints file: %w" , err )
214+ }
215+ } else {
216+ constraintsJson = []byte (c .constraintsJson )
217+ }
218+ err := json .Unmarshal (constraintsJson , & c .Constraints )
219+ if err != nil {
220+ return fmt .Errorf ("failed to parse constraints: %w" , err )
221+ }
222+ for _ , constraint := range c .Constraints {
223+ if err := constraint .Validate (); err != nil {
224+ return err
225+ }
226+ }
227+ return nil
228+ }
0 commit comments