From 1850c6633944062e824c672bb58fbd6adba650be Mon Sep 17 00:00:00 2001 From: danwt <30197399+danwt@users.noreply.github.com> Date: Wed, 8 May 2024 16:58:55 +0100 Subject: [PATCH] inlines create batch --- block/submit.go | 39 +++++++++++++++------------------------ 1 file changed, 15 insertions(+), 24 deletions(-) diff --git a/block/submit.go b/block/submit.go index a95c45744..5d93075ce 100644 --- a/block/submit.go +++ b/block/submit.go @@ -102,13 +102,21 @@ func (m *Manager) AccumulatedDataLoop(ctx context.Context, toSubmit chan bool) { // Finally, it submits the next batch of blocks and updates the sync target to the height of the last block in the submitted batch. func (m *Manager) HandleSubmissionTrigger(ctx context.Context) error { // Load current sync target and height to determine if new blocks are available for submission. - if m.Store.Height() <= m.SyncTarget.Load() { + + startHeight := m.SyncTarget.Load() + 1 + endHeightInclusive := m.Store.Height() + + if endHeightInclusive < startHeight { return nil // No new blocks have been produced } - nextBatch, err := m.createNextBatch() + nextBatch, err := m.CreateNextBatchToSubmit(startHeight, endHeightInclusive) if err != nil { - return fmt.Errorf("create next batch: %w", err) + return fmt.Errorf("create next batch to submit: %w", err) + } + + if err := m.ValidateBatch(nextBatch); err != nil { + return fmt.Errorf("validate batch: %w", err) } resultSubmitToDA, err := m.submitNextBatchToDA(nextBatch) @@ -126,23 +134,6 @@ func (m *Manager) HandleSubmissionTrigger(ctx context.Context) error { return nil } -func (m *Manager) createNextBatch() (*types.Batch, error) { - // Create the batch - startHeight := m.SyncTarget.Load() + 1 - endHeight := m.Store.Height() - nextBatch, err := m.CreateNextBatchToSubmit(startHeight, endHeight) - if err != nil { - m.logger.Error("create next batch", "startHeight", startHeight, "endHeight", endHeight, "error", err) - return nil, err - } - - if err := m.ValidateBatch(nextBatch); err != nil { - return nil, err - } - - return nextBatch, nil -} - func (m *Manager) submitNextBatchToDA(nextBatch *types.Batch) (*da.ResultSubmitBatch, error) { startHeight := nextBatch.StartHeight actualEndHeight := nextBatch.EndHeight @@ -178,19 +169,19 @@ func (m *Manager) ValidateBatch(batch *types.Batch) error { return nil } -func (m *Manager) CreateNextBatchToSubmit(startHeight uint64, endHeight uint64) (*types.Batch, error) { +func (m *Manager) CreateNextBatchToSubmit(startHeight uint64, endHeightInclusive uint64) (*types.Batch, error) { var height uint64 // Create the batch - batchSize := endHeight - startHeight + 1 + batchSize := endHeightInclusive - startHeight + 1 batch := &types.Batch{ StartHeight: startHeight, - EndHeight: endHeight, + EndHeight: endHeightInclusive, Blocks: make([]*types.Block, 0, batchSize), Commits: make([]*types.Commit, 0, batchSize), } // Populate the batch - for height = startHeight; height <= endHeight; height++ { + for height = startHeight; height <= endHeightInclusive; height++ { block, err := m.Store.LoadBlock(height) if err != nil { m.logger.Error("load block", "height", height)