From df2b89b78d67dfcc487a3b832177b2e42e01e58d Mon Sep 17 00:00:00 2001 From: bap2pecs <111917526+bap2pecs@users.noreply.github.com> Date: Mon, 16 Dec 2024 21:04:31 -0500 Subject: [PATCH] backport: #217 - shouldn't wait SubmissionRetryInterval at the first iteration (#227) --- CHANGELOG.md | 1 + finality-provider/service/fp_instance.go | 92 ++++++++++++------------ 2 files changed, 48 insertions(+), 45 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1080566d..2637c866 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -46,6 +46,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) * [#216](https://github.com/babylonlabs-io/finality-provider/pull/216) Add multiple fpd connecting to one eotsd in e2e tests * [#218](https://github.com/babylonlabs-io/finality-provider/pull/218) Prune used merkle proof * [#221](https://github.com/babylonlabs-io/finality-provider/pull/221) Cleanup TODOs +* [#227](https://github.com/babylonlabs-io/finality-provider/pull/227) Fix FP submission loop ## v0.13.1 diff --git a/finality-provider/service/fp_instance.go b/finality-provider/service/fp_instance.go index 89e3801c..71360687 100644 --- a/finality-provider/service/fp_instance.go +++ b/finality-provider/service/fp_instance.go @@ -339,58 +339,60 @@ func (fp *FinalityProviderInstance) retrySubmitSigsUntilFinalized(targetBlocks [ var failedCycles uint32 targetHeight := targetBlocks[len(targetBlocks)-1].Height - // we break the for loop if the block is finalized or the signature is successfully submitted - // error will be returned if maximum retries have been reached or the query to the consumer chain fails + // First iteration happens before the loop for { - select { - case <-time.After(fp.cfg.SubmissionRetryInterval): - // error will be returned if max retries have been reached - var res *types.TxResponse - var err error - res, err = fp.SubmitBatchFinalitySignatures(targetBlocks) - if err != nil { - fp.logger.Debug( - "failed to submit finality signature to the consumer chain", - zap.String("pk", fp.GetBtcPkHex()), - zap.Uint32("current_failures", failedCycles), - zap.Uint64("target_start_height", targetBlocks[0].Height), - zap.Uint64("target_end_height", targetHeight), - zap.Error(err), - ) - - if clientcontroller.IsUnrecoverable(err) { - return nil, err - } - - if clientcontroller.IsExpected(err) { - return nil, nil - } + // Attempt submission immediately + // error will be returned if max retries have been reached + var res *types.TxResponse + var err error + res, err = fp.SubmitBatchFinalitySignatures(targetBlocks) + if err != nil { + fp.logger.Debug( + "failed to submit finality signature to the consumer chain", + zap.String("pk", fp.GetBtcPkHex()), + zap.Uint32("current_failures", failedCycles), + zap.Uint64("target_start_height", targetBlocks[0].Height), + zap.Uint64("target_end_height", targetHeight), + zap.Error(err), + ) - failedCycles++ - if failedCycles > fp.cfg.MaxSubmissionRetries { - return nil, fmt.Errorf("reached max failed cycles with err: %w", err) - } - } else { - // the signature has been successfully submitted - return res, nil + if clientcontroller.IsUnrecoverable(err) { + return nil, err } - // periodically query the index block to be later checked whether it is Finalized - finalized, err := fp.checkBlockFinalization(targetHeight) - if err != nil { - return nil, fmt.Errorf("failed to query block finalization at height %v: %w", targetHeight, err) - } - if finalized { - fp.logger.Debug( - "the block is already finalized, skip submission", - zap.String("pk", fp.GetBtcPkHex()), - zap.Uint64("target_height", targetHeight), - ) - // returning nil here is to safely break the loop - // the error still exists + if clientcontroller.IsExpected(err) { return nil, nil } + failedCycles++ + if failedCycles > fp.cfg.MaxSubmissionRetries { + return nil, fmt.Errorf("reached max failed cycles with err: %w", err) + } + } else { + // the signature has been successfully submitted + return res, nil + } + + // periodically query the index block to be later checked whether it is Finalized + finalized, err := fp.checkBlockFinalization(targetHeight) + if err != nil { + return nil, fmt.Errorf("failed to query block finalization at height %v: %w", targetHeight, err) + } + if finalized { + fp.logger.Debug( + "the block is already finalized, skip submission", + zap.String("pk", fp.GetBtcPkHex()), + zap.Uint64("target_height", targetHeight), + ) + // TODO: returning nil here is to safely break the loop + // the error still exists + return nil, nil + } + + // Wait for the retry interval + select { + case <-time.After(fp.cfg.SubmissionRetryInterval): + // Continue to next retry iteration case <-fp.quit: fp.logger.Debug("the finality-provider instance is closing", zap.String("pk", fp.GetBtcPkHex())) return nil, ErrFinalityProviderShutDown