Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(submission): Added another loop to not revert to submission while waiting for batch #840

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 11 additions & 16 deletions settlement/dymension/dymension.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,18 +221,16 @@ func (d *HubClient) PostBatch(batch *types.Batch, daClient da.Client, daResult *
// Try submitting the batch to the settlement layer. If submission (i.e. only submission, not acceptance) fails we emit an unhealthy event
// and try again in the next loop. If submission succeeds we wait for the batch to be accepted by the settlement layer.
// If it is not accepted we emit an unhealthy event and start again the submission loop.
submitLoop:
for {
select {
case <-d.ctx.Done():
return d.ctx.Err()
default:
err := d.submitBatch(msgUpdateState)
if err != nil {

err = fmt.Errorf("submit batch: %w", err)

uevent.MustPublish(d.ctx, d.pubsub, &settlement.EventDataHealth{Error: err}, settlement.EventHealthStatusList)

d.logger.Error(
"Submitted bad health event: trying again.",
"startHeight",
Expand All @@ -242,31 +240,33 @@ func (d *HubClient) PostBatch(batch *types.Batch, daClient da.Client, daResult *
"error",
err,
)

// Sleep to allow context cancellation to take effect before retrying
time.Sleep(100 * time.Millisecond)
continue
}
// Break out of the loop after successful submission
break submitLoop
}
}

// Batch was submitted successfully. Wait for it to be accepted by the settlement layer.
timer := time.NewTimer(d.batchAcceptanceTimeout)
defer timer.Stop()
// Batch was submitted successfully. Wait for it to be accepted by the settlement layer.
timer := time.NewTimer(d.batchAcceptanceTimeout)
defer timer.Stop()

for {
select {
case <-d.ctx.Done():
return d.ctx.Err()

case <-subscription.Cancelled():
return fmt.Errorf("subscription cancelled: %w", err)
return fmt.Errorf("subscription cancelled")

case event := <-subscription.Out():
eventData := event.Data().(*settlement.EventDataNewBatchAccepted)
if eventData.EndHeight != batch.EndHeight {
d.logger.Error("Received event for a different batch, ignoring.", "event", eventData)
continue
}

uevent.MustPublish(d.ctx, d.pubsub, &settlement.EventDataHealth{}, settlement.EventHealthStatusList)
d.logger.Debug("Batch accepted: emitted healthy event.", "startHeight", batch.StartHeight, "endHeight", batch.EndHeight)
return nil
Expand All @@ -276,10 +276,8 @@ func (d *HubClient) PostBatch(batch *types.Batch, daClient da.Client, daResult *
// layer, and we've just missed the event.
includedBatch, err := d.pollForBatchInclusion(batch.EndHeight)
if err != nil || !includedBatch {
err = fmt.Errorf("wait for batch inclusion: %w: %w", settlement.ErrBatchNotAccepted, err)

err = fmt.Errorf("wait for batch inclusion: %w", settlement.ErrBatchNotAccepted)
uevent.MustPublish(d.ctx, d.pubsub, &settlement.EventDataHealth{Error: err}, settlement.EventHealthStatusList)

d.logger.Error(
"Submitted bad health event: trying again.",
"startHeight",
Expand All @@ -289,15 +287,12 @@ func (d *HubClient) PostBatch(batch *types.Batch, daClient da.Client, daResult *
"error",
err,
)

timer.Stop() // we don't forget to clean up
timer.Reset(d.batchAcceptanceTimeout)
continue
}

// all good
uevent.MustPublish(d.ctx, d.pubsub, &settlement.EventDataHealth{}, settlement.EventHealthStatusList)
d.logger.Info("Batch accepted, emitted healthy event.", "startHeight", batch.StartHeight, "endHeight", batch.EndHeight)

return nil
}
}
Expand Down
Loading