-
Notifications
You must be signed in to change notification settings - Fork 170
/
encoding_streamer.go
724 lines (621 loc) · 24.7 KB
/
encoding_streamer.go
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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
package batcher
import (
"context"
"encoding/binary"
"errors"
"fmt"
"strings"
"sync"
"time"
"github.com/Layr-Labs/eigenda/common"
"github.com/Layr-Labs/eigenda/core"
"github.com/Layr-Labs/eigenda/disperser"
"github.com/Layr-Labs/eigenda/encoding"
"github.com/Layr-Labs/eigensdk-go/logging"
lru "github.com/hashicorp/golang-lru/v2"
"github.com/wealdtech/go-merkletree/v2"
grpc_metadata "google.golang.org/grpc/metadata"
)
const encodingInterval = 2 * time.Second
const operatorStateCacheSize = 32
var errNoEncodedResults = errors.New("no encoded results")
type EncodedSizeNotifier struct {
mu sync.Mutex
Notify chan struct{}
// threshold is the size of the total encoded blob results in bytes that triggers the notifier
threshold uint64
// active is set to false after the notifier is triggered to prevent it from triggering again for the same batch
// This is reset when CreateBatch is called and the encoded results have been consumed
active bool
}
type StreamerConfig struct {
// SRSOrder is the order of the SRS used for encoding
SRSOrder int
// EncodingRequestTimeout is the timeout for each encoding request
EncodingRequestTimeout time.Duration
// ChainStateTimeout is the timeout used for getting the chainstate
ChainStateTimeout time.Duration
// EncodingQueueLimit is the maximum number of encoding requests that can be queued
EncodingQueueLimit int
// TargetNumChunks is the target number of chunks per encoded blob
TargetNumChunks uint
// Maximum number of Blobs to fetch from store
MaxBlobsToFetchFromStore int
FinalizationBlockDelay uint
}
type EncodingStreamer struct {
StreamerConfig
mu sync.RWMutex
EncodedBlobstore *encodedBlobStore
ReferenceBlockNumber uint
Pool common.WorkerPool
EncodedSizeNotifier *EncodedSizeNotifier
blobStore disperser.BlobStore
chainState core.IndexedChainState
encoderClient disperser.EncoderClient
assignmentCoordinator core.AssignmentCoordinator
encodingCtxCancelFuncs []context.CancelFunc
metrics *EncodingStreamerMetrics
batcherMetrics *Metrics
logger logging.Logger
// Used to keep track of the last evaluated key for fetching metadatas
exclusiveStartKey *disperser.BlobStoreExclusiveStartKey
operatorStateCache *lru.Cache[string, *core.IndexedOperatorState]
}
type batch struct {
EncodedBlobs []core.EncodedBlob
BlobMetadata []*disperser.BlobMetadata
BlobHeaders []*core.BlobHeader
BatchHeader *core.BatchHeader
State *core.IndexedOperatorState
MerkleTree *merkletree.MerkleTree
}
func NewEncodedSizeNotifier(notify chan struct{}, threshold uint64) *EncodedSizeNotifier {
return &EncodedSizeNotifier{
Notify: notify,
threshold: threshold,
active: true,
}
}
func NewEncodingStreamer(
config StreamerConfig,
blobStore disperser.BlobStore,
chainState core.IndexedChainState,
encoderClient disperser.EncoderClient,
assignmentCoordinator core.AssignmentCoordinator,
encodedSizeNotifier *EncodedSizeNotifier,
workerPool common.WorkerPool,
metrics *EncodingStreamerMetrics,
batcherMetrics *Metrics,
logger logging.Logger) (*EncodingStreamer, error) {
if config.EncodingQueueLimit <= 0 {
return nil, errors.New("EncodingQueueLimit should be greater than 0")
}
operatorStateCache, err := lru.New[string, *core.IndexedOperatorState](operatorStateCacheSize)
if err != nil {
return nil, err
}
return &EncodingStreamer{
StreamerConfig: config,
EncodedBlobstore: newEncodedBlobStore(logger),
ReferenceBlockNumber: uint(0),
Pool: workerPool,
EncodedSizeNotifier: encodedSizeNotifier,
blobStore: blobStore,
chainState: chainState,
encoderClient: encoderClient,
assignmentCoordinator: assignmentCoordinator,
encodingCtxCancelFuncs: make([]context.CancelFunc, 0),
metrics: metrics,
batcherMetrics: batcherMetrics,
logger: logger.With("component", "EncodingStreamer"),
exclusiveStartKey: nil,
operatorStateCache: operatorStateCache,
}, nil
}
func (e *EncodingStreamer) Start(ctx context.Context) error {
encoderChan := make(chan EncodingResultOrStatus)
// goroutine for handling blob encoding responses
go func() {
for {
select {
case <-ctx.Done():
return
case response := <-encoderChan:
err := e.ProcessEncodedBlobs(ctx, response)
if err != nil {
if strings.Contains(err.Error(), context.Canceled.Error()) {
// ignore canceled errors because canceled encoding requests are normal
continue
}
if strings.Contains(err.Error(), "too many requests") {
e.logger.Warn("encoding request ratelimited", "err", err)
} else if strings.Contains(err.Error(), "connection reset by peer") {
e.logger.Warn("encoder connection reset by peer", "err", err)
} else if strings.Contains(err.Error(), "error reading from server: EOF") {
e.logger.Warn("encoder request dropped", "err", err)
} else if strings.Contains(err.Error(), "connection refused") {
e.logger.Warn("encoder connection refused", "err", err)
} else {
e.logger.Error("error processing encoded blobs", "err", err)
}
}
}
}
}()
// goroutine for making blob encoding requests
go func() {
ticker := time.NewTicker(encodingInterval)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
err := e.RequestEncoding(ctx, encoderChan)
if err != nil {
e.logger.Warn("error requesting encoding", "err", err)
}
}
}
}()
return nil
}
func (e *EncodingStreamer) dedupRequests(metadatas []*disperser.BlobMetadata, referenceBlockNumber uint) []*disperser.BlobMetadata {
res := make([]*disperser.BlobMetadata, 0)
for _, meta := range metadatas {
allQuorumsRequested := true
// check if the blob has been requested for all quorums
for _, quorum := range meta.RequestMetadata.SecurityParams {
if !e.EncodedBlobstore.HasEncodingRequested(meta.GetBlobKey(), quorum.QuorumID, referenceBlockNumber) {
allQuorumsRequested = false
break
}
}
if !allQuorumsRequested {
res = append(res, meta)
}
}
return res
}
func (e *EncodingStreamer) RequestEncoding(ctx context.Context, encoderChan chan EncodingResultOrStatus) error {
stageTimer := time.Now()
// pull new blobs and send to encoder
e.mu.Lock()
metadatas, newExclusiveStartKey, err := e.blobStore.GetBlobMetadataByStatusWithPagination(ctx, disperser.Processing, int32(e.StreamerConfig.MaxBlobsToFetchFromStore), e.exclusiveStartKey)
e.exclusiveStartKey = newExclusiveStartKey
e.mu.Unlock()
if err != nil {
return fmt.Errorf("error getting blob metadatas: %w", err)
}
if len(metadatas) == 0 {
e.logger.Info("no new metadatas to encode")
return nil
}
// read lock to access e.ReferenceBlockNumber
e.mu.RLock()
referenceBlockNumber := e.ReferenceBlockNumber
e.mu.RUnlock()
if referenceBlockNumber == 0 {
// Update the reference block number for the next iteration
blockNumber, err := e.chainState.GetCurrentBlockNumber()
if err != nil {
return fmt.Errorf("failed to get current block number, won't request encoding: %w", err)
} else {
if blockNumber > e.FinalizationBlockDelay {
blockNumber -= e.FinalizationBlockDelay
}
e.mu.Lock()
e.ReferenceBlockNumber = blockNumber
e.mu.Unlock()
referenceBlockNumber = blockNumber
}
}
e.logger.Debug("metadata in processing status", "numMetadata", len(metadatas))
metadatas = e.dedupRequests(metadatas, referenceBlockNumber)
if len(metadatas) == 0 {
e.logger.Info("no new metadatas to encode")
return nil
}
waitingQueueSize := e.Pool.WaitingQueueSize()
numMetadatastoProcess := e.EncodingQueueLimit - waitingQueueSize
if numMetadatastoProcess > len(metadatas) {
numMetadatastoProcess = len(metadatas)
}
if numMetadatastoProcess <= 0 {
// encoding queue is full
e.logger.Warn("worker pool queue is full. skipping this round of encoding requests", "waitingQueueSize", waitingQueueSize, "encodingQueueLimit", e.EncodingQueueLimit)
return nil
}
// only process subset of blobs so it doesn't exceed the EncodingQueueLimit
// TODO: this should be done at the request time and keep the cursor so that we don't fetch the same metadata every time
metadatas = metadatas[:numMetadatastoProcess]
e.logger.Debug("new metadatas to encode", "numMetadata", len(metadatas), "duration", time.Since(stageTimer))
// Get the operator state
timeoutCtx, cancel := context.WithTimeout(ctx, e.ChainStateTimeout)
defer cancel()
state, err := e.getOperatorState(timeoutCtx, metadatas, referenceBlockNumber)
if err != nil {
return fmt.Errorf("error getting operator state: %w", err)
}
metadatas = e.validateMetadataQuorums(metadatas, state)
metadataByKey := make(map[disperser.BlobKey]*disperser.BlobMetadata, 0)
for _, metadata := range metadatas {
metadataByKey[metadata.GetBlobKey()] = metadata
}
stageTimer = time.Now()
blobs, err := e.blobStore.GetBlobsByMetadata(ctx, metadatas)
if err != nil {
return fmt.Errorf("error getting blobs from blob store: %w", err)
}
e.logger.Debug("retrieved blobs to encode", "numBlobs", len(blobs), "duration", time.Since(stageTimer))
e.logger.Debug("encoding blobs...", "numBlobs", len(blobs), "blockNumber", referenceBlockNumber)
for i := range metadatas {
metadata := metadatas[i]
e.RequestEncodingForBlob(ctx, metadata, blobs[metadata.GetBlobKey()], state, referenceBlockNumber, encoderChan)
}
return nil
}
type pendingRequestInfo struct {
BlobQuorumInfo *core.BlobQuorumInfo
EncodingParams encoding.EncodingParams
Assignments map[core.OperatorID]core.Assignment
}
func (e *EncodingStreamer) RequestEncodingForBlob(ctx context.Context, metadata *disperser.BlobMetadata, blob *core.Blob, state *core.IndexedOperatorState, referenceBlockNumber uint, encoderChan chan EncodingResultOrStatus) {
// Validate the encoding parameters for each quorum
blobKey := metadata.GetBlobKey()
pending := make([]pendingRequestInfo, 0, len(metadata.RequestMetadata.SecurityParams))
for ind := range metadata.RequestMetadata.SecurityParams {
quorum := metadata.RequestMetadata.SecurityParams[ind]
// Check if the blob has already been encoded for this quorum
if e.EncodedBlobstore.HasEncodingRequested(blobKey, quorum.QuorumID, referenceBlockNumber) {
continue
}
blobLength := encoding.GetBlobLength(metadata.RequestMetadata.BlobSize)
chunkLength, err := e.assignmentCoordinator.CalculateChunkLength(state.OperatorState, blobLength, e.StreamerConfig.TargetNumChunks, quorum)
if err != nil {
e.logger.Error("error calculating chunk length", "err", err)
continue
}
blobQuorumInfo := &core.BlobQuorumInfo{
SecurityParam: core.SecurityParam{
QuorumID: quorum.QuorumID,
AdversaryThreshold: quorum.AdversaryThreshold,
ConfirmationThreshold: quorum.ConfirmationThreshold,
QuorumRate: quorum.QuorumRate,
},
ChunkLength: chunkLength,
}
assignments, info, err := e.assignmentCoordinator.GetAssignments(state.OperatorState, blobLength, blobQuorumInfo)
if err != nil {
e.logger.Error("error getting assignments", "err", err)
continue
}
params := encoding.ParamsFromMins(chunkLength, info.TotalChunks)
err = encoding.ValidateEncodingParamsAndBlobLength(params, uint64(blobLength), uint64(e.SRSOrder))
if err != nil {
e.logger.Error("invalid encoding params", "err", err)
// Cancel the blob
err := e.blobStore.MarkBlobFailed(ctx, blobKey)
if err != nil {
e.logger.Error("error marking blob failed", "err", err)
}
return
}
pending = append(pending, pendingRequestInfo{
BlobQuorumInfo: blobQuorumInfo,
EncodingParams: params,
Assignments: assignments,
})
}
if len(pending) > 0 {
requestTime := time.Unix(0, int64(metadata.RequestMetadata.RequestedAt))
e.batcherMetrics.ObserveBlobAge("encoding_requested", float64(time.Since(requestTime).Milliseconds()))
}
// Execute the encoding requests
for ind := range pending {
res := pending[ind]
// Create a new context for each encoding request
// This allows us to cancel all outstanding encoding requests when we create a new batch
// This is necessary because an encoding request is dependent on the reference block number
// If the reference block number changes, we need to cancel all outstanding encoding requests
// and re-request them with the new reference block number
encodingCtx, cancel := context.WithTimeout(ctx, e.EncodingRequestTimeout)
e.mu.Lock()
e.encodingCtxCancelFuncs = append(e.encodingCtxCancelFuncs, cancel)
e.mu.Unlock()
// Add headers for routing
md := grpc_metadata.New(map[string]string{
"content-type": "application/grpc",
"x-payload-size": fmt.Sprintf("%d", len(blob.Data)),
})
encodingCtx = grpc_metadata.NewOutgoingContext(encodingCtx, md)
e.Pool.Submit(func() {
defer cancel()
start := time.Now()
commits, chunks, err := e.encoderClient.EncodeBlob(encodingCtx, blob.Data, res.EncodingParams)
if err != nil {
encoderChan <- EncodingResultOrStatus{Err: err, EncodingResult: EncodingResult{
BlobMetadata: metadata,
BlobQuorumInfo: res.BlobQuorumInfo,
}}
e.metrics.ObserveEncodingLatency("failed", res.BlobQuorumInfo.QuorumID, len(blob.Data), float64(time.Since(start).Milliseconds()))
return
}
encoderChan <- EncodingResultOrStatus{
EncodingResult: EncodingResult{
BlobMetadata: metadata,
ReferenceBlockNumber: referenceBlockNumber,
BlobQuorumInfo: res.BlobQuorumInfo,
Commitment: commits,
ChunksData: chunks,
Assignments: res.Assignments,
},
Err: nil,
}
e.metrics.ObserveEncodingLatency("success", res.BlobQuorumInfo.QuorumID, len(blob.Data), float64(time.Since(start).Milliseconds()))
})
e.EncodedBlobstore.PutEncodingRequest(blobKey, res.BlobQuorumInfo.QuorumID)
}
}
func (e *EncodingStreamer) ProcessEncodedBlobs(ctx context.Context, result EncodingResultOrStatus) error {
if result.Err != nil {
e.EncodedBlobstore.DeleteEncodingRequest(result.BlobMetadata.GetBlobKey(), result.BlobQuorumInfo.QuorumID)
return fmt.Errorf("error encoding blob: %w", result.Err)
}
err := e.EncodedBlobstore.PutEncodingResult(&result.EncodingResult)
if err != nil {
return fmt.Errorf("failed to putEncodedBlob: %w", err)
}
requestTime := time.Unix(0, int64(result.BlobMetadata.RequestMetadata.RequestedAt))
e.batcherMetrics.ObserveBlobAge("encoded", float64(time.Since(requestTime).Milliseconds()))
e.batcherMetrics.IncrementBlobSize("encoded", result.BlobQuorumInfo.QuorumID, int(result.BlobMetadata.RequestMetadata.BlobSize))
count, encodedSize := e.EncodedBlobstore.GetEncodedResultSize()
e.metrics.UpdateEncodedBlobs(count, encodedSize)
if e.EncodedSizeNotifier.threshold > 0 && encodedSize >= e.EncodedSizeNotifier.threshold {
e.EncodedSizeNotifier.mu.Lock()
if e.EncodedSizeNotifier.active {
e.logger.Info("encoded size threshold reached", "size", encodedSize)
e.EncodedSizeNotifier.Notify <- struct{}{}
// make sure this doesn't keep triggering before encoded blob store is reset
e.EncodedSizeNotifier.active = false
}
e.EncodedSizeNotifier.mu.Unlock()
}
return nil
}
func (e *EncodingStreamer) UpdateReferenceBlock(currentBlockNumber uint) error {
blockNumber := currentBlockNumber
if blockNumber > e.FinalizationBlockDelay {
blockNumber -= e.FinalizationBlockDelay
}
if e.ReferenceBlockNumber > blockNumber {
return fmt.Errorf("reference block number is being updated to a lower value: from %d to %d", e.ReferenceBlockNumber, blockNumber)
}
e.mu.Lock()
defer e.mu.Unlock()
if e.ReferenceBlockNumber < blockNumber {
// Wipe out the encoding results based on previous reference block number
_ = e.EncodedBlobstore.PopLatestEncodingResults(e.ReferenceBlockNumber)
}
e.ReferenceBlockNumber = blockNumber
return nil
}
// CreateBatch makes a batch from all blobs in the encoded blob store.
// If successful, it returns a batch, and updates the reference block number for next batch to use.
// Otherwise, it returns an error and keeps the blobs in the encoded blob store.
// This function is meant to be called periodically in a single goroutine as it resets the state of the encoded blob store.
func (e *EncodingStreamer) CreateBatch(ctx context.Context) (*batch, error) {
// lock to update e.ReferenceBlockNumber
e.mu.Lock()
defer e.mu.Unlock()
// Cancel outstanding encoding requests
// Assumption: `CreateBatch` will be called at an interval longer than time it takes to encode a single blob
if len(e.encodingCtxCancelFuncs) > 0 {
e.logger.Info("canceling outstanding encoding requests", "count", len(e.encodingCtxCancelFuncs))
for _, cancel := range e.encodingCtxCancelFuncs {
cancel()
}
e.encodingCtxCancelFuncs = make([]context.CancelFunc, 0)
}
// If there were no requested blobs between the last batch and now, there is no need to create a new batch
if e.ReferenceBlockNumber == 0 {
blockNumber, err := e.chainState.GetCurrentBlockNumber()
if err != nil {
e.logger.Error("failed to get current block number. will not clean up the encoded blob store.", "err", err)
} else {
_ = e.EncodedBlobstore.GetNewAndDeleteStaleEncodingResults(blockNumber)
}
return nil, errNoEncodedResults
}
// Delete any encoded results that are not from the current batching iteration (i.e. that has different reference block number)
// If any pending encoded results are discarded here, it will be re-requested in the next iteration
encodedResults := e.EncodedBlobstore.GetNewAndDeleteStaleEncodingResults(e.ReferenceBlockNumber)
// Reset the notifier
e.EncodedSizeNotifier.mu.Lock()
e.EncodedSizeNotifier.active = true
e.EncodedSizeNotifier.mu.Unlock()
e.logger.Info("creating a batch...", "numBlobs", len(encodedResults), "refblockNumber", e.ReferenceBlockNumber)
if len(encodedResults) == 0 {
return nil, errNoEncodedResults
}
encodedBlobByKey := make(map[disperser.BlobKey]core.EncodedBlob)
blobQuorums := make(map[disperser.BlobKey][]*core.BlobQuorumInfo)
blobHeaderByKey := make(map[disperser.BlobKey]*core.BlobHeader)
metadataByKey := make(map[disperser.BlobKey]*disperser.BlobMetadata)
for i := range encodedResults {
// each result represent an encoded result per (blob, quorum param)
// if the same blob has been dispersed multiple time with different security params,
// there will be multiple encoded results for that (blob, quorum)
result := encodedResults[i]
blobKey := result.BlobMetadata.GetBlobKey()
if _, ok := encodedBlobByKey[blobKey]; !ok {
metadataByKey[blobKey] = result.BlobMetadata
blobQuorums[blobKey] = make([]*core.BlobQuorumInfo, 0)
blobHeader := &core.BlobHeader{
BlobCommitments: *result.Commitment,
}
blobHeaderByKey[blobKey] = blobHeader
encodedBlobByKey[blobKey] = core.EncodedBlob{
BlobHeader: blobHeader,
EncodedBundlesByOperator: make(map[core.OperatorID]core.EncodedBundles),
}
}
// Populate the assigned bundles
for opID, assignment := range result.Assignments {
bundles, ok := encodedBlobByKey[blobKey].EncodedBundlesByOperator[opID]
if !ok {
encodedBlobByKey[blobKey].EncodedBundlesByOperator[opID] = make(core.EncodedBundles)
bundles = encodedBlobByKey[blobKey].EncodedBundlesByOperator[opID]
}
bundles[result.BlobQuorumInfo.QuorumID] = new(core.ChunksData)
bundles[result.BlobQuorumInfo.QuorumID].Format = result.ChunksData.Format
bundles[result.BlobQuorumInfo.QuorumID].Chunks = append(bundles[result.BlobQuorumInfo.QuorumID].Chunks, result.ChunksData.Chunks[assignment.StartIndex:assignment.StartIndex+assignment.NumChunks]...)
bundles[result.BlobQuorumInfo.QuorumID].ChunkLen = result.ChunksData.ChunkLen
}
blobQuorums[blobKey] = append(blobQuorums[blobKey], result.BlobQuorumInfo)
}
// Populate the blob quorum infos
for blobKey, encodedBlob := range encodedBlobByKey {
encodedBlob.BlobHeader.QuorumInfos = blobQuorums[blobKey]
}
for blobKey, metadata := range metadataByKey {
quorumPresent := make(map[core.QuorumID]bool)
for _, quorum := range blobQuorums[blobKey] {
quorumPresent[quorum.QuorumID] = true
}
// Check if the blob has valid quorums. If any of the quorums are not valid, delete the blobKey
for _, quorum := range metadata.RequestMetadata.SecurityParams {
_, ok := quorumPresent[quorum.QuorumID]
if !ok {
// Delete the blobKey. These encoded blobs will be automatically removed by the next run of
// RequestEncoding
delete(metadataByKey, blobKey)
break
}
}
}
if len(metadataByKey) == 0 {
return nil, errNoEncodedResults
}
// Transform maps to slices so orders in different slices match
encodedBlobs := make([]core.EncodedBlob, 0, len(metadataByKey))
blobHeaders := make([]*core.BlobHeader, 0, len(metadataByKey))
metadatas := make([]*disperser.BlobMetadata, 0, len(metadataByKey))
for key := range metadataByKey {
err := e.transitionBlobToDispersing(ctx, metadataByKey[key])
if err != nil {
continue
}
encodedBlobs = append(encodedBlobs, encodedBlobByKey[key])
blobHeaders = append(blobHeaders, blobHeaderByKey[key])
metadatas = append(metadatas, metadataByKey[key])
}
timeoutCtx, cancel := context.WithTimeout(context.Background(), e.ChainStateTimeout)
defer cancel()
state, err := e.getOperatorState(timeoutCtx, metadatas, e.ReferenceBlockNumber)
if err != nil {
for _, metadata := range metadatas {
_ = e.handleFailedMetadata(ctx, metadata)
}
return nil, err
}
// Populate the batch header
batchHeader := &core.BatchHeader{
ReferenceBlockNumber: e.ReferenceBlockNumber,
BatchRoot: [32]byte{},
}
tree, err := batchHeader.SetBatchRoot(blobHeaders)
if err != nil {
for _, metadata := range metadatas {
_ = e.handleFailedMetadata(ctx, metadata)
}
return nil, err
}
e.ReferenceBlockNumber = 0
return &batch{
EncodedBlobs: encodedBlobs,
BatchHeader: batchHeader,
BlobHeaders: blobHeaders,
BlobMetadata: metadatas,
State: state,
MerkleTree: tree,
}, nil
}
func (e *EncodingStreamer) handleFailedMetadata(ctx context.Context, metadata *disperser.BlobMetadata) error {
err := e.blobStore.MarkBlobProcessing(ctx, metadata.GetBlobKey())
if err != nil {
e.logger.Error("error marking blob as processing", "err", err)
}
return err
}
func (e *EncodingStreamer) transitionBlobToDispersing(ctx context.Context, metadata *disperser.BlobMetadata) error {
blobKey := metadata.GetBlobKey()
err := e.blobStore.MarkBlobDispersing(ctx, blobKey)
if err != nil {
e.logger.Error("error marking blob as dispersing", "err", err, "blobKey", blobKey.String())
return err
}
// remove encoded blob from storage so we don't disperse it again
e.RemoveEncodedBlob(metadata)
return nil
}
func (e *EncodingStreamer) RemoveEncodedBlob(metadata *disperser.BlobMetadata) {
for _, sp := range metadata.RequestMetadata.SecurityParams {
e.EncodedBlobstore.DeleteEncodingResult(metadata.GetBlobKey(), sp.QuorumID)
}
}
// getOperatorState returns the operator state for the blobs that have valid quorums
func (e *EncodingStreamer) getOperatorState(ctx context.Context, metadatas []*disperser.BlobMetadata, blockNumber uint) (*core.IndexedOperatorState, error) {
quorums := make(map[core.QuorumID]QuorumInfo, 0)
for _, metadata := range metadatas {
for _, quorum := range metadata.RequestMetadata.SecurityParams {
quorums[quorum.QuorumID] = QuorumInfo{}
}
}
quorumIds := make([]core.QuorumID, len(quorums))
i := 0
for id := range quorums {
quorumIds[i] = id
i++
}
cacheKey := computeCacheKey(blockNumber, quorumIds)
if val, ok := e.operatorStateCache.Get(cacheKey); ok {
return val, nil
}
// GetIndexedOperatorState should return state for valid quorums only
state, err := e.chainState.GetIndexedOperatorState(ctx, blockNumber, quorumIds)
if err != nil {
return nil, fmt.Errorf("error getting operator state at block number %d: %w", blockNumber, err)
}
e.operatorStateCache.Add(cacheKey, state)
return state, nil
}
// It also returns the list of valid blob metadatas (i.e. blobs that have valid quorums)
func (e *EncodingStreamer) validateMetadataQuorums(metadatas []*disperser.BlobMetadata, state *core.IndexedOperatorState) []*disperser.BlobMetadata {
validMetadata := make([]*disperser.BlobMetadata, 0)
for _, metadata := range metadatas {
valid := true
for _, quorum := range metadata.RequestMetadata.SecurityParams {
if aggKey, ok := state.AggKeys[quorum.QuorumID]; !ok || aggKey == nil {
e.logger.Warn("got blob with a quorum without APK. Will skip.", "blobKey", metadata.GetBlobKey(), "quorum", quorum.QuorumID)
valid = false
}
}
if valid {
validMetadata = append(validMetadata, metadata)
} else {
_, err := e.blobStore.HandleBlobFailure(context.Background(), metadata, 0)
if err != nil {
e.logger.Error("error handling blob failure", "err", err)
}
}
}
return validMetadata
}
func computeCacheKey(blockNumber uint, quorumIDs []uint8) string {
bytes := make([]byte, 8+len(quorumIDs))
binary.LittleEndian.PutUint64(bytes, uint64(blockNumber))
copy(bytes[8:], quorumIDs)
return string(bytes)
}