From 732836aca8da2dca0ff9d94554359c29868d062f Mon Sep 17 00:00:00 2001 From: Omri Date: Thu, 18 Apr 2024 16:32:25 +0200 Subject: [PATCH] refactor: refactored `handleSubmissionTrigger` (#711) --- block/produce.go | 1 - block/submit.go | 29 +++++++++++------------------ 2 files changed, 11 insertions(+), 19 deletions(-) diff --git a/block/produce.go b/block/produce.go index 8fc295948..989ba5f2f 100644 --- a/block/produce.go +++ b/block/produce.go @@ -127,7 +127,6 @@ func (m *Manager) produceBlock(allowEmpty bool) (*types.Block, *types.Commit, er pendingBlock, err := m.store.LoadBlock(newHeight) if err == nil { // Using an existing block - block = pendingBlock commit, err = m.store.LoadCommit(newHeight) if err != nil { diff --git a/block/submit.go b/block/submit.go index 7f778ac25..dce22d41b 100644 --- a/block/submit.go +++ b/block/submit.go @@ -26,30 +26,23 @@ func (m *Manager) SubmitLoop(ctx context.Context) { } } +// handleSubmissionTrigger processes the submission trigger event. It checks if there are new blocks produced since the last submission. +// If there are, it attempts to submit a batch of blocks. It then attempts to produce an empty block to ensure IBC messages +// pass through during the batch submission process due to proofs requires for ibc messages only exist on the next block. +// 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) { - // SyncTarget is the height of the last block in the last batch as seen by this node. - syncTarget := m.syncTarget.Load() - height := m.store.Height() - if height <= syncTarget { // no new blocks produced yet - return - } - - // Submit batch if we've reached the batch size and there isn't another batch currently in submission process. - - if !m.batchInProcess.TryLock() { + if !m.batchInProcess.TryLock() { // Attempt to lock for batch processing m.logger.Debug("Batch submission already in process, skipping submission") return } + defer m.batchInProcess.Unlock() // Ensure unlocking at the end - defer m.batchInProcess.Unlock() - - // check again, might have changed since locking - syncTarget = m.syncTarget.Load() - height = m.store.Height() - if height <= syncTarget { - return + // Load current sync target and height to determine if new blocks are available for submission. + syncTarget, height := m.syncTarget.Load(), m.store.Height() + if height <= syncTarget { // Check if there are new blocks since last sync target. + return // Exit if no new blocks are produced. } - // We try and produce an empty block to make sure relevant ibc messages will pass through during the batch submission: https://github.com/dymensionxyz/research/issues/173. err := m.produceAndGossipBlock(ctx, true) if err != nil {