From c7777586885dae647e9c4954355024fa59dc779e Mon Sep 17 00:00:00 2001 From: j-rafique Date: Tue, 25 Jun 2024 16:23:23 +0500 Subject: [PATCH] added check for task registration error attempts --- walletnode/api/services/cascade.go | 4 +- .../services/cascaderegister/service.go | 69 +++++++++++++++++++ walletnode/services/cascaderegister/task.go | 9 +++ 3 files changed, 80 insertions(+), 2 deletions(-) diff --git a/walletnode/api/services/cascade.go b/walletnode/api/services/cascade.go index 901b2aed8..450c3a216 100644 --- a/walletnode/api/services/cascade.go +++ b/walletnode/api/services/cascade.go @@ -141,11 +141,11 @@ func (service *CascadeAPIHandler) StartProcessing(ctx context.Context, p *cascad return nil, cascade.MakeInternalServerError(errors.New("ticket registration attempts have been exceeded")) default: regAttemptID, err := service.register.InsertRegistrationAttempts(types.RegistrationAttempt{ - FileID: p.FileID, + FileID: baseFile.FileID, RegStartedAt: time.Now().UTC(), }) if err != nil { - log.WithContext(ctx).WithField("file_id", p.FileID).WithError(err).Error("error inserting registration attempt") + log.WithContext(ctx).WithField("file_id", baseFile.FileID).WithError(err).Error("error inserting registration attempt") return nil, err } diff --git a/walletnode/services/cascaderegister/service.go b/walletnode/services/cascaderegister/service.go index f4f3bcf11..3991ea338 100644 --- a/walletnode/services/cascaderegister/service.go +++ b/walletnode/services/cascaderegister/service.go @@ -2,6 +2,7 @@ package cascaderegister import ( "context" + "database/sql" "os" "path/filepath" "strconv" @@ -9,6 +10,8 @@ import ( "time" "github.com/google/uuid" + + "github.com/pastelnetwork/gonode/common/log" "github.com/pastelnetwork/gonode/common/storage/queries" "github.com/pastelnetwork/gonode/common/storage/ticketstore" "github.com/pastelnetwork/gonode/common/types" @@ -325,6 +328,72 @@ func (service *CascadeRegistrationService) UpsertFile(file types.File) error { return nil } +func (service *CascadeRegistrationService) HandleTaskRegistrationErrorAttempts(ctx context.Context, taskID, regTxid, actTxid string, regAttemptID int64, actAttemptID int64, taskError error) error { + doneBlock, err := service.pastelHandler.PastelClient.GetBlockCount(ctx) + if err != nil { + log.WithContext(ctx).WithField("task_id", taskID).WithError(err).Error("error retrieving block count") + return err + } + + switch { + case regTxid == "": + ra, err := service.GetRegistrationAttemptsByID(int(regAttemptID)) + if err != nil { + log.WithContext(ctx).WithField("task_id", taskID).WithError(err).Error("error retrieving file reg attempt") + return err + } + + ra.FinishedAt = time.Now().UTC() + ra.IsSuccessful = false + ra.ErrorMessage = taskError.Error() + _, err = service.UpdateRegistrationAttempts(*ra) + if err != nil { + log.WithContext(ctx).WithField("task_id", taskID).WithError(err).Error("error updating file reg attempt") + return err + } + + return nil + case actTxid == "": + file, err := service.GetFileByTaskID(taskID) + if err != nil { + log.WithContext(ctx).WithError(err).Error("error retrieving file") + return nil + } + + file.DoneBlock = int(doneBlock) + file.RegTxid = regTxid + file.IsConcluded = false + err = service.ticketDB.UpsertFile(*file) + if err != nil { + log.Errorf("Error in file upsert: %v", err.Error()) + return nil + } + + actAttempt, err := service.GetActivationAttemptByID(int(actAttemptID)) + if err != nil && !errors.Is(err, sql.ErrNoRows) { + log.Errorf("Error retrieving file act attempt: %v", err.Error()) + return err + } + + if actAttempt == nil { + return nil + } + + actAttempt.IsSuccessful = false + actAttempt.ActivationAttemptAt = time.Now().UTC() + actAttempt.ErrorMessage = taskError.Error() + _, err = service.UpdateActivationAttempts(*actAttempt) + if err != nil { + log.Errorf("Error in activation attempts upsert: %v", err.Error()) + return err + } + + return err + } + + return nil +} + // NewService returns a new Service instance func NewService(config *Config, pastelClient pastel.Client, nodeClient node.ClientInterface, fileStorage storage.FileStorageInterface, db storage.KeyValue, diff --git a/walletnode/services/cascaderegister/task.go b/walletnode/services/cascaderegister/task.go index 77d3be382..7ccc9f2be 100644 --- a/walletnode/services/cascaderegister/task.go +++ b/walletnode/services/cascaderegister/task.go @@ -63,6 +63,11 @@ func (task *CascadeRegistrationTask) Run(ctx context.Context) error { func (task *CascadeRegistrationTask) run(ctx context.Context) error { regTxid, actTxid, err := task.runTicketRegActTask(ctx) if err != nil { + attemptErr := task.service.HandleTaskRegistrationErrorAttempts(ctx, task.ID(), regTxid, actTxid, task.Request.RegAttemptID, task.actAttemptID, err) + if attemptErr != nil { + return attemptErr + } + return err } @@ -350,6 +355,10 @@ func (task *CascadeRegistrationTask) runTicketRegActTask(ctx context.Context) (r FileID: task.Request.FileID, ActivationAttemptAt: time.Now().UTC(), }) + if err != nil { + log.WithContext(ctx).WithError(err).Error("error inserting activation attempt") + return task.regCascadeTxid, "", errors.Errorf("error inserting activation attempt: %w", err) + } task.actAttemptID = id // activate cascade ticket registered at previous step by SN activateTxID, err := task.activateActionTicket(ctx)