Skip to content
Closed
Show file tree
Hide file tree
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
7 changes: 5 additions & 2 deletions bridge/setu/listener/heimdall.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,11 @@ func (hl *HeimdallListener) Start() error {

checkpointPollInterval := helper.GetConfig().CheckpointerPollInterval

// fetch initial checkpoint params (will retry up to 10 times or exit service)
checkpointParams := util.GetCheckpointParamsWithRetry(hl.cliCtx)
checkpointParams, err := util.GetCheckpointParamsWithRetry(hl.cliCtx)
if err != nil {
hl.Logger.Error("Failed to fetch checkpoint params", "error", err)
return err
}
if checkpointParams.CheckpointPollInterval > 0 {
checkpointPollInterval = checkpointParams.CheckpointPollInterval
}
Expand Down
6 changes: 5 additions & 1 deletion bridge/setu/listener/maticchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ func (ml *MaticChainListener) Start() error {
go ml.StartHeaderProcess(headerCtx)

pollInterval := helper.GetConfig().CheckpointerPollInterval
params := util.GetCheckpointParamsWithRetry(ml.cliCtx)
params, err := util.GetCheckpointParamsWithRetry(ml.cliCtx)
if err != nil {
ml.Logger.Error("Failed to fetch checkpoint params", "error", err)
return err
}
if params.CheckpointPollInterval > 0 {
pollInterval = params.CheckpointPollInterval
}
Expand Down
13 changes: 9 additions & 4 deletions bridge/setu/processor/checkpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,17 @@ func NewCheckpointProcessor(rootchainAbi, stakingInfoAbi *abi.ABI) *CheckpointPr
// Start - consumes messages from checkpoint queue and call processMsg
func (cp *CheckpointProcessor) Start() error {
cp.Logger.Info("Starting")

checkpointParams, err := util.GetCheckpointParamsWithRetry(cp.cliCtx)
if err != nil {
cp.Logger.Error("Failed to fetch checkpoint params", "error", err)
return err
}

// no-ack
ackCtx, cancelNoACKPolling := context.WithCancel(context.Background())
cp.cancelNoACKPolling = cancelNoACKPolling
go cp.startPolling(ackCtx)
go cp.startPolling(ackCtx, checkpointParams)
return nil
}

Expand All @@ -95,14 +102,12 @@ func (cp *CheckpointProcessor) RegisterTasks() {
}
}

func (cp *CheckpointProcessor) startPolling(ctx context.Context) {
func (cp *CheckpointProcessor) startPolling(ctx context.Context, checkpointParams *checkpointTypes.Params) {
now := time.Now()
baseTime := time.Unix(0, 0)
// no-ack ticker interval keep same with checkpoint interval
checkpointPollInterval := helper.GetConfig().CheckpointerPollInterval

// fetch initial checkpoint params (will retry up to 10 times or exit service)
checkpointParams := util.GetCheckpointParamsWithRetry(cp.cliCtx)
if checkpointParams.CheckpointPollInterval > 0 {
checkpointPollInterval = checkpointParams.CheckpointPollInterval
}
Expand Down
23 changes: 8 additions & 15 deletions bridge/setu/util/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -427,26 +427,21 @@ func GetCheckpointParams(cliCtx cliContext.CLIContext) (*checkpointTypes.Params,
}

// GetCheckpointParamsWithRetry guarantees successful retrieval of checkpoint parameters
// by retrying up to 10 times. If it fails after 10 attempts, the service will exit.
func GetCheckpointParamsWithRetry(cliCtx cliContext.CLIContext) *checkpointTypes.Params {
// by retrying up to 10 times. If it fails after 10 attempts, returns an error.
// The caller should handle the error appropriately (e.g., return error to trigger graceful shutdown).
func GetCheckpointParamsWithRetry(cliCtx cliContext.CLIContext) (*checkpointTypes.Params, error) {
const maxRetries = 10
retryDelay := 1 * time.Second
maxRetryDelay := 30 * time.Second

for attempt := 1; attempt <= maxRetries; attempt++ {
params, err := GetCheckpointParams(cliCtx)
if err == nil {
if err == nil && params != nil {
logger.Info("Successfully fetched checkpoint params", "attempt", attempt)
return params
return params, nil
}

if attempt == maxRetries {
logger.Error("Failed to fetch checkpoint params after maximum retries, exiting service",
"err", err, "maxRetries", maxRetries)
os.Exit(1)
}

logger.Error("Failed to fetch checkpoint params, retrying...",
logger.Warn("Failed to fetch checkpoint params, retrying...",
"err", err, "attempt", attempt, "maxRetries", maxRetries, "retryAfter", retryDelay)
time.Sleep(retryDelay)

Expand All @@ -457,10 +452,8 @@ func GetCheckpointParamsWithRetry(cliCtx cliContext.CLIContext) *checkpointTypes
}
}

// This line should never be reached, but added for completeness
logger.Error("Unexpected: exceeded retry loop without returning or exiting")
os.Exit(1)
return nil
logger.Error("Failed to fetch checkpoint params after maximum retries", "maxRetries", maxRetries)
return nil, errors.New("failed to fetch checkpoint params after maximum retries")
}

// GetBufferedCheckpoint return checkpoint from bueffer
Expand Down
Loading