-
Notifications
You must be signed in to change notification settings - Fork 396
Expand file tree
/
Copy pathavs_subscriber.go
More file actions
392 lines (325 loc) · 13.1 KB
/
avs_subscriber.go
File metadata and controls
392 lines (325 loc) · 13.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
package chainio
import (
"context"
"encoding/hex"
"sync"
"time"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
ethcommon "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
servicemanager "github.com/yetanotherco/aligned_layer/contracts/bindings/AlignedLayerServiceManager"
retry "github.com/yetanotherco/aligned_layer/core"
"github.com/yetanotherco/aligned_layer/core/config"
sdklogging "github.com/Layr-Labs/eigensdk-go/logging"
"github.com/ethereum/go-ethereum/crypto"
)
const (
MaxRetries = 100
RetryInterval = 1 * time.Second
BlockInterval uint64 = 1000
PollLatestBatchInterval = 5 * time.Second
RemoveBatchFromSetInterval = 5 * time.Minute
)
// NOTE(marian): Leaving this commented code here as it may be useful in the short term.
// type AvsSubscriberer interface {
// SubscribeToNewTasks(newTaskCreatedChan chan *cstaskmanager.ContractAlignedLayerTaskManagerNewTaskCreated) event.Subscription
// SubscribeToTaskResponses(taskResponseLogs chan *cstaskmanager.ContractAlignedLayerTaskManagerTaskResponded) event.Subscription
// ParseTaskResponded(rawLog types.Log) (*cstaskmanager.ContractAlignedLayerTaskManagerTaskResponded, error)
// }
// Subscribers use a ws connection instead of http connection like Readers
// kind of stupid that the geth client doesn't have a unified interface for both...
// it takes a single url, so the bindings, even though they have watcher functions, those can't be used
// with the http connection... seems very very stupid. Am I missing something?
type AvsSubscriber struct {
AvsContractBindings *AvsServiceBindings
AlignedLayerServiceManagerAddr ethcommon.Address
logger sdklogging.Logger
}
func NewAvsSubscriberFromConfig(baseConfig *config.BaseConfig) (*AvsSubscriber, error) {
avsContractBindings, err := NewAvsServiceBindings(
baseConfig.AlignedLayerDeploymentConfig.AlignedLayerServiceManagerAddr,
baseConfig.AlignedLayerDeploymentConfig.AlignedLayerOperatorStateRetrieverAddr,
baseConfig.EthWsClient, baseConfig.EthWsClientFallback, baseConfig.Logger)
if err != nil {
baseConfig.Logger.Errorf("Failed to create contract bindings", "err", err)
return nil, err
}
return &AvsSubscriber{
AvsContractBindings: avsContractBindings,
AlignedLayerServiceManagerAddr: baseConfig.AlignedLayerDeploymentConfig.AlignedLayerServiceManagerAddr,
logger: baseConfig.Logger,
}, nil
}
func (s *AvsSubscriber) SubscribeToNewTasksV2(newTaskCreatedChan chan *servicemanager.ContractAlignedLayerServiceManagerNewBatchV2) (chan error, error) {
// Create a new channel to receive new tasks
internalChannel := make(chan *servicemanager.ContractAlignedLayerServiceManagerNewBatchV2)
// Subscribe to new tasks
sub, err := SubscribeToNewTasksV2Retryable(&bind.WatchOpts{}, s.AvsContractBindings.ServiceManager, internalChannel, nil)
if err != nil {
s.logger.Error("Primary failed to subscribe to new AlignedLayer V2 tasks after %d retries", retry.DefaultMaxNumRetries, "err", err)
return nil, err
}
subFallback, err := SubscribeToNewTasksV2Retryable(&bind.WatchOpts{}, s.AvsContractBindings.ServiceManagerFallback, internalChannel, nil)
if err != nil {
s.logger.Error("Fallback failed to subscribe to new AlignedLayer V2 tasks after %d retries", retry.DefaultMaxNumRetries, "err", err)
return nil, err
}
s.logger.Info("Subscribed to new AlignedLayer V2 tasks")
// create a new channel to foward errors
errorChannel := make(chan error)
pollLatestBatchTicker := time.NewTicker(PollLatestBatchInterval)
// Forward the new tasks to the provided channel
go func() {
defer pollLatestBatchTicker.Stop()
newBatchMutex := &sync.Mutex{}
batchesSet := make(map[[32]byte]struct{})
for {
select {
case newBatch := <-internalChannel:
s.processNewBatchV2(newBatch, batchesSet, newBatchMutex, newTaskCreatedChan)
case <-pollLatestBatchTicker.C:
latestBatch, err := s.getLatestNotRespondedTaskFromEthereumV2()
if err != nil {
s.logger.Debug("Failed to get latest task from blockchain", "err", err)
continue
}
if latestBatch != nil {
s.processNewBatchV2(latestBatch, batchesSet, newBatchMutex, newTaskCreatedChan)
}
}
}
}()
// Handle errors and resubscribe
go func() {
for {
select {
case err := <-sub.Err():
s.logger.Warn("Error in new task subscription", "err", err)
sub.Unsubscribe()
sub, err = SubscribeToNewTasksV2Retryable(&bind.WatchOpts{}, s.AvsContractBindings.ServiceManager, internalChannel, nil)
if err != nil {
errorChannel <- err
}
case err := <-subFallback.Err():
s.logger.Warn("Error in fallback new task subscription", "err", err)
subFallback.Unsubscribe()
subFallback, err = SubscribeToNewTasksV2Retryable(&bind.WatchOpts{}, s.AvsContractBindings.ServiceManagerFallback, internalChannel, nil)
if err != nil {
errorChannel <- err
}
}
}
}()
return errorChannel, nil
}
func (s *AvsSubscriber) SubscribeToNewTasksV3(newTaskCreatedChan chan *servicemanager.ContractAlignedLayerServiceManagerNewBatchV3) (chan error, error) {
// Create a new channel to receive new tasks
internalChannel := make(chan *servicemanager.ContractAlignedLayerServiceManagerNewBatchV3)
// Subscribe to new tasks
sub, err := SubscribeToNewTasksV3Retryable(&bind.WatchOpts{}, s.AvsContractBindings.ServiceManager, internalChannel, nil)
if err != nil {
s.logger.Error("Primary failed to subscribe to new AlignedLayer V3 tasks after %d retries", MaxRetries, "err", err)
return nil, err
}
subFallback, err := SubscribeToNewTasksV3Retryable(&bind.WatchOpts{}, s.AvsContractBindings.ServiceManagerFallback, internalChannel, nil)
if err != nil {
s.logger.Error("Fallback failed to subscribe to new AlignedLayer V3 tasks after %d retries", MaxRetries, "err", err)
return nil, err
}
s.logger.Info("Subscribed to new AlignedLayer V3 tasks")
// create a new channel to foward errors
errorChannel := make(chan error)
pollLatestBatchTicker := time.NewTicker(PollLatestBatchInterval)
// Forward the new tasks to the provided channel
go func() {
defer pollLatestBatchTicker.Stop()
newBatchMutex := &sync.Mutex{}
batchesSet := make(map[[32]byte]struct{})
for {
select {
case newBatch := <-internalChannel:
s.processNewBatchV3(newBatch, batchesSet, newBatchMutex, newTaskCreatedChan)
case <-pollLatestBatchTicker.C:
latestBatch, err := s.getLatestNotRespondedTaskFromEthereumV3()
if err != nil {
s.logger.Debug("Failed to get latest task from blockchain", "err", err)
continue
}
if latestBatch != nil {
s.processNewBatchV3(latestBatch, batchesSet, newBatchMutex, newTaskCreatedChan)
}
}
}
}()
// Handle errors and resubscribe
go func() {
for {
select {
case err := <-sub.Err():
s.logger.Warn("Error in new task subscription", "err", err)
sub.Unsubscribe()
sub, err = SubscribeToNewTasksV3Retryable(&bind.WatchOpts{}, s.AvsContractBindings.ServiceManager, internalChannel, nil)
if err != nil {
errorChannel <- err
}
case err := <-subFallback.Err():
s.logger.Warn("Error in fallback new task subscription", "err", err)
subFallback.Unsubscribe()
subFallback, err = SubscribeToNewTasksV3Retryable(&bind.WatchOpts{}, s.AvsContractBindings.ServiceManagerFallback, internalChannel, nil)
if err != nil {
errorChannel <- err
}
}
}
}()
return errorChannel, nil
}
func (s *AvsSubscriber) processNewBatchV2(batch *servicemanager.ContractAlignedLayerServiceManagerNewBatchV2, batchesSet map[[32]byte]struct{}, newBatchMutex *sync.Mutex, newTaskCreatedChan chan<- *servicemanager.ContractAlignedLayerServiceManagerNewBatchV2) {
newBatchMutex.Lock()
defer newBatchMutex.Unlock()
batchIdentifier := append(batch.BatchMerkleRoot[:], batch.SenderAddress[:]...)
var batchIdentifierHash = *(*[32]byte)(crypto.Keccak256(batchIdentifier))
if _, ok := batchesSet[batchIdentifierHash]; !ok {
s.logger.Info("Received new task",
"batchMerkleRoot", hex.EncodeToString(batch.BatchMerkleRoot[:]),
"senderAddress", hex.EncodeToString(batch.SenderAddress[:]),
"batchIdentifierHash", hex.EncodeToString(batchIdentifierHash[:]))
batchesSet[batchIdentifierHash] = struct{}{}
newTaskCreatedChan <- batch
// Remove the batch from the set after RemoveBatchFromSetInterval time
go func() {
time.Sleep(RemoveBatchFromSetInterval)
newBatchMutex.Lock()
delete(batchesSet, batchIdentifierHash)
newBatchMutex.Unlock()
}()
}
}
func (s *AvsSubscriber) processNewBatchV3(batch *servicemanager.ContractAlignedLayerServiceManagerNewBatchV3, batchesSet map[[32]byte]struct{}, newBatchMutex *sync.Mutex, newTaskCreatedChan chan<- *servicemanager.ContractAlignedLayerServiceManagerNewBatchV3) {
newBatchMutex.Lock()
defer newBatchMutex.Unlock()
batchIdentifier := append(batch.BatchMerkleRoot[:], batch.SenderAddress[:]...)
var batchIdentifierHash = *(*[32]byte)(crypto.Keccak256(batchIdentifier))
if _, ok := batchesSet[batchIdentifierHash]; !ok {
s.logger.Info("Received new task",
"batchMerkleRoot", hex.EncodeToString(batch.BatchMerkleRoot[:]),
"senderAddress", hex.EncodeToString(batch.SenderAddress[:]),
"batchIdentifierHash", hex.EncodeToString(batchIdentifierHash[:]))
batchesSet[batchIdentifierHash] = struct{}{}
newTaskCreatedChan <- batch
// Remove the batch from the set after RemoveBatchFromSetInterval time
go func() {
time.Sleep(RemoveBatchFromSetInterval)
newBatchMutex.Lock()
delete(batchesSet, batchIdentifierHash)
newBatchMutex.Unlock()
}()
}
}
// getLatestNotRespondedTaskFromEthereum queries the blockchain for the latest not responded task using the FilterNewBatch method.
func (s *AvsSubscriber) getLatestNotRespondedTaskFromEthereumV2() (*servicemanager.ContractAlignedLayerServiceManagerNewBatchV2, error) {
latestBlock, err := s.BlockNumberRetryable(context.Background())
if err != nil {
return nil, err
}
var fromBlock uint64
if latestBlock < BlockInterval {
fromBlock = 0
} else {
fromBlock = latestBlock - BlockInterval
}
logs, err := s.FilterBatchV2Retryable(&bind.FilterOpts{Start: fromBlock, End: nil, Context: context.Background()}, nil)
if err != nil {
return nil, err
}
var lastLog *servicemanager.ContractAlignedLayerServiceManagerNewBatchV2
// Iterate over the logs until the end
for logs.Next() {
lastLog = logs.Event
}
if err := logs.Error(); err != nil {
return nil, err
}
if lastLog == nil {
return nil, nil
}
batchIdentifier := append(lastLog.BatchMerkleRoot[:], lastLog.SenderAddress[:]...)
batchIdentifierHash := *(*[32]byte)(crypto.Keccak256(batchIdentifier))
state, err := s.BatchesStateRetryable(nil, batchIdentifierHash)
if err != nil {
return nil, err
}
if state.Responded {
return nil, nil
}
return lastLog, nil
}
// getLatestNotRespondedTaskFromEthereum queries the blockchain for the latest not responded task using the FilterNewBatch method.
func (s *AvsSubscriber) getLatestNotRespondedTaskFromEthereumV3() (*servicemanager.ContractAlignedLayerServiceManagerNewBatchV3, error) {
latestBlock, err := s.BlockNumberRetryable(context.Background())
if err != nil {
return nil, err
}
var fromBlock uint64
if latestBlock < BlockInterval {
fromBlock = 0
} else {
fromBlock = latestBlock - BlockInterval
}
logs, err := s.FilterBatchV3Retryable(&bind.FilterOpts{Start: fromBlock, End: nil, Context: context.Background()}, nil)
if err != nil {
return nil, err
}
var lastLog *servicemanager.ContractAlignedLayerServiceManagerNewBatchV3
// Iterate over the logs until the end
for logs.Next() {
lastLog = logs.Event
}
if err := logs.Error(); err != nil {
return nil, err
}
if lastLog == nil {
return nil, nil
}
batchIdentifier := append(lastLog.BatchMerkleRoot[:], lastLog.SenderAddress[:]...)
batchIdentifierHash := *(*[32]byte)(crypto.Keccak256(batchIdentifier))
state, err := s.BatchesStateRetryable(nil, batchIdentifierHash)
if err != nil {
return nil, err
}
if state.Responded {
return nil, nil
}
return lastLog, nil
}
func (s *AvsSubscriber) WaitForOneBlock(startBlock uint64) error {
currentBlock, err := s.BlockNumberRetryable(context.Background())
if err != nil {
return err
}
if currentBlock <= startBlock { // should really be == but just in case
// Subscribe to new head
c := make(chan *types.Header)
sub, err := s.SubscribeNewHeadRetryable(context.Background(), c)
if err != nil {
return err
}
// Read channel for the new block
<-c
(sub).Unsubscribe()
}
return nil
}
// func (s *AvsSubscriber) SubscribeToTaskResponses(taskResponseChan chan *cstaskmanager.ContractAlignedLayerTaskManagerTaskResponded) event.Subscription {
// sub, err := s.AvsContractBindings.TaskManager.WatchTaskResponded(
// &bind.WatchOpts{}, taskResponseChan,
// )
// if err != nil {
// s.logger.Error("Failed to subscribe to TaskResponded events", "err", err)
// }
// s.logger.Infof("Subscribed to TaskResponded events")
// return sub
// }
// func (s *AvsSubscriber) ParseTaskResponded(rawLog types.Log) (*cstaskmanager.ContractAlignedLayerTaskManagerTaskResponded, error) {
// return s.AvsContractBindings.TaskManager.ContractAlignedLayerTaskManagerFilterer.ParseTaskResponded(rawLog)
// }