go-ethereum/rollup/da_syncer/block_queue.go
Jonas Theis ac8164f5a4
feat: follower node sync from DA (#1098)
* port changes from #1013

* port changes from #1068

* go.mod tidy

* fix compile error

* fix goimports

* fix log

* address review comments

* upgrade golang.org/x/net to 0.23.0

* bump version

* remove unused flag

* update da-codec commit

---------

Co-authored-by: Péter Garamvölgyi <peter@scroll.io>
2024-12-18 17:27:51 +01:00

56 lines
1.3 KiB
Go

package da_syncer
import (
"context"
"fmt"
"github.com/scroll-tech/go-ethereum/rollup/da_syncer/da"
)
// BlockQueue is a pipeline stage that reads batches from BatchQueue, extracts all da.PartialBlock from it and
// provides them to the next stage one-by-one.
type BlockQueue struct {
batchQueue *BatchQueue
blocks []*da.PartialBlock
}
func NewBlockQueue(batchQueue *BatchQueue) *BlockQueue {
return &BlockQueue{
batchQueue: batchQueue,
blocks: make([]*da.PartialBlock, 0),
}
}
func (bq *BlockQueue) NextBlock(ctx context.Context) (*da.PartialBlock, error) {
for len(bq.blocks) == 0 {
err := bq.getBlocksFromBatch(ctx)
if err != nil {
return nil, err
}
}
block := bq.blocks[0]
bq.blocks = bq.blocks[1:]
return block, nil
}
func (bq *BlockQueue) getBlocksFromBatch(ctx context.Context) error {
daEntry, err := bq.batchQueue.NextBatch(ctx)
if err != nil {
return err
}
entryWithBlocks, ok := daEntry.(da.EntryWithBlocks)
// this should never happen because we only receive CommitBatch entries
if !ok {
return fmt.Errorf("unexpected type of daEntry: %T", daEntry)
}
bq.blocks = entryWithBlocks.Blocks()
return nil
}
func (bq *BlockQueue) Reset(height uint64) {
bq.blocks = make([]*da.PartialBlock, 0)
bq.batchQueue.Reset(height)
}