4
4
"context"
5
5
"fmt"
6
6
"math/big"
7
+ "math/rand/v2"
7
8
"os"
9
+ "time"
8
10
9
11
"github.com/ethereum/go-ethereum"
10
12
"github.com/ethereum/go-ethereum/accounts/abi"
@@ -34,7 +36,7 @@ type Operator[Input any, Output any] struct {
34
36
taskResponseHashFn TaskResponseHashFunction [Output ]
35
37
}
36
38
37
- type ResponseCalculationFunction [Input any , Output any ] func (task sdktypes. GenericInputTask [ Input ], taskIndex uint32 ) (sdktypes. GenericOutputTaskResponse [ Output ] , error )
39
+ type ResponseCalculationFunction [Input any , Output any ] func (taskIndex uint32 , input Input ) (Output , error )
38
40
39
41
type TaskResponseHashFunction [Output any ] func (taskResponse sdktypes.GenericOutputTaskResponse [Output ]) ([32 ]byte , error )
40
42
@@ -79,8 +81,8 @@ func getDefaultHashFunction[Output any](taskResponseType abi.Type) TaskResponseH
79
81
80
82
func NewOperatorFromConfig [Input any , Output any ](
81
83
c OperatorConfig ,
82
- ResponseCalculationFn ResponseCalculationFunction [Input , Output ],
83
- TaskResponseHashFn TaskResponseHashFunction [Output ],
84
+ responseCalculationFn ResponseCalculationFunction [Input , Output ],
85
+ taskResponseHashFn TaskResponseHashFunction [Output ],
84
86
) (* Operator [Input , Output ], error ) {
85
87
avs_config := avsregistry.Config {
86
88
RegistryCoordinatorAddress : common .HexToAddress (c .AVSRegistryCoordinatorAddress ),
@@ -153,14 +155,41 @@ func NewOperatorFromConfig[Input any, Output any](
153
155
c .Logger .Fatal ("error subscribing to newTaskCreated events" , "err" , err )
154
156
}
155
157
156
- if TaskResponseHashFn == nil {
158
+ if taskResponseHashFn == nil {
157
159
taskResponseType , err := extractTypeFromAbi (c .TaskManagerAbi )
158
160
if err != nil {
159
161
c .Logger .Error ("Failed to get task response type in default abi." , "err" , err )
160
162
return nil , err
161
163
}
162
164
163
- TaskResponseHashFn = getDefaultHashFunction [Output ](taskResponseType )
165
+ taskResponseHashFn = getDefaultHashFunction [Output ](taskResponseType )
166
+ }
167
+
168
+ if c .TestingOpts .FailingPercentage != 0 {
169
+ failPercentage := c .TestingOpts .FailingPercentage
170
+ if failPercentage > 100 {
171
+ return nil , fmt .Errorf ("failing percentage must be between 0 and 100" )
172
+ }
173
+ computeOutput := responseCalculationFn
174
+
175
+ failSeed := c .TestingOpts .FailingSeed
176
+ if failSeed == 0 {
177
+ // If the seed is not set, we use the current time as the seed
178
+ failSeed = uint64 (time .Now ().UnixNano ())
179
+ }
180
+ rng := rand .New (rand .NewPCG (42 , failSeed ))
181
+
182
+ c .Logger .Warn ("FailingPercentage option was set. This operator will randomly fail tasks." )
183
+ c .Logger .Info ("Using seed:" , failSeed )
184
+
185
+ responseCalculationFn = func (taskIndex uint32 , input Input ) (Output , error ) {
186
+ randomNumber := rng .UintN (100 )
187
+ if randomNumber < failPercentage {
188
+ var emptyOutput Output
189
+ return emptyOutput , nil
190
+ }
191
+ return computeOutput (taskIndex , input )
192
+ }
164
193
}
165
194
166
195
operator := & Operator [Input , Output ]{
@@ -170,8 +199,8 @@ func NewOperatorFromConfig[Input any, Output any](
170
199
operatorId : operatorId ,
171
200
newTaskCreatedLogs : newTaskCreatedLogs ,
172
201
taskManagerAbi : c .TaskManagerAbi ,
173
- responseCalculationFn : ResponseCalculationFn ,
174
- taskResponseHashFn : TaskResponseHashFn ,
202
+ responseCalculationFn : responseCalculationFn ,
203
+ taskResponseHashFn : taskResponseHashFn ,
175
204
}
176
205
177
206
c .Logger .Info ("Operator info" ,
@@ -231,12 +260,15 @@ func (o *Operator[Input, Output]) processNewTaskCreatedLog(
231
260
"QuorumThresholdPercentage" , newTaskCreatedLog .Task .QuorumThresholdPercentage ,
232
261
)
233
262
234
- taskResponse , err := o .responseCalculationFn (newTaskCreatedLog .Task , newTaskIndex )
263
+ output , err := o .responseCalculationFn (newTaskIndex , newTaskCreatedLog .Task . InputValue )
235
264
if err != nil {
236
265
return nil , fmt .Errorf ("error calculating task response: %w" , err )
237
266
}
238
-
239
- return & taskResponse , nil
267
+ taskResponse := & sdktypes.GenericOutputTaskResponse [Output ]{
268
+ ReferenceTaskIndex : newTaskIndex ,
269
+ OutputValue : output ,
270
+ }
271
+ return taskResponse , nil
240
272
}
241
273
242
274
func (o * Operator [Input , Output ]) signTaskResponse (
0 commit comments