Skip to content

Commit 99fcad1

Browse files
committed
[PSL-1214] handle duplicate burn-txid edge cases
1 parent 9b5a2bd commit 99fcad1

File tree

3 files changed

+98
-2
lines changed

3 files changed

+98
-2
lines changed

mixins/pasteld_handler.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ func (pt *PastelHandler) GetBurnAddress() string {
302302
func (pt *PastelHandler) ValidateBurnTxID(ctx context.Context, burnTxnID string, estimatedFee float64) error {
303303
var err error
304304

305-
if err := pt.checkBurnTxID(ctx, burnTxnID); err != nil {
305+
if err := pt.CheckBurnTxID(ctx, burnTxnID); err != nil {
306306
log.WithContext(ctx).WithError(err).Errorf("duplicate burnTXID")
307307
err = errors.Errorf("validated burnTXID :%w", err)
308308
return err
@@ -329,7 +329,7 @@ func (pt *PastelHandler) ValidateBurnTxID(ctx context.Context, burnTxnID string,
329329
return nil
330330
}
331331

332-
func (pt *PastelHandler) checkBurnTxID(ctx context.Context, burnTXID string) error {
332+
func (pt *PastelHandler) CheckBurnTxID(ctx context.Context, burnTXID string) error {
333333
actionTickets, err := pt.PastelClient.FindActionRegTicketsByLabel(ctx, burnTXID)
334334
if err != nil {
335335
return fmt.Errorf("action reg tickets by label: %w", err)

walletnode/api/services/cascade.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,15 @@ func (service *CascadeAPIHandler) StartProcessing(ctx context.Context, p *cascad
171171
}
172172
sortedRelatedFiles := service.register.SortFilesWithHigherAmounts(relatedFiles)
173173

174+
err = service.checkBurnTxIDsValidForRegistration(ctx, sortedBurnTxids, sortedRelatedFiles)
175+
if err != nil {
176+
log.WithContext(ctx).WithField("related_volumes", len(relatedFiles)).
177+
WithError(err).
178+
WithField("burn_txids", len(p.BurnTxids)).
179+
Error("given burn-tx-ids are not valid")
180+
return nil, cascade.MakeBadRequest(errors.New("given burn txids are not valid"))
181+
}
182+
174183
var taskIDs []string
175184
for index, file := range sortedRelatedFiles {
176185
burnTxID := sortedBurnTxids[index]
@@ -558,12 +567,30 @@ func (service *CascadeAPIHandler) Restore(ctx context.Context, p *cascade.Restor
558567
if unConcludedVolume.RegTxid == "" {
559568
logger.WithField("volume_name", unConcludedVolume.FileID).Info("find a volume with no registration, trying again...")
560569

570+
<<<<<<< Updated upstream
561571
burnTxId, err := service.register.GetBurnTxIdByAmount(ctx, int64(unConcludedVolume.ReqBurnTxnAmount))
562572
if err != nil {
563573
log.WithContext(ctx).WithField("amount", int64(unConcludedVolume.ReqBurnTxnAmount)).WithError(err).Error("error getting burn TxId for amount")
564574
return nil, cascade.MakeInternalServerError(err)
565575
}
566576
logger.WithField("volume_name", unConcludedVolume.FileID).Info("estimated fee has been burned, sending for registration")
577+
=======
578+
var burnTxId string
579+
if service.IsBurnTxIDValidForRecovery(ctx, v.BurnTxnID, v.ReqAmount-10) {
580+
log.WithContext(ctx).WithField("burn_txid", v.BurnTxnID).Info("existing burn-txid is valid")
581+
burnTxId = v.BurnTxnID
582+
} else {
583+
log.WithContext(ctx).WithField("burn_txid", v.BurnTxnID).Info("existing burn-txid is not valid, burning the new txid")
584+
585+
burnTxId, err = service.register.GetBurnTxIdByAmount(ctx, int64(v.ReqBurnTxnAmount))
586+
if err != nil {
587+
log.WithContext(ctx).WithField("amount", int64(v.ReqBurnTxnAmount)).WithError(err).Error("error getting burn TxId for amount")
588+
return nil, cascade.MakeInternalServerError(err)
589+
}
590+
591+
logger.WithField("volume_name", v.FileID).Info("estimated fee has been burned, sending for registration")
592+
}
593+
>>>>>>> Stashed changes
567594

568595
addTaskPayload := &common.AddTaskPayload{
569596
FileID: unConcludedVolume.FileID,
@@ -577,7 +604,13 @@ func (service *CascadeAPIHandler) Restore(ctx context.Context, p *cascade.Restor
577604
addTaskPayload.SpendableAddress = p.SpendableAddress
578605
}
579606

607+
<<<<<<< Updated upstream
580608
_, err = service.register.ProcessFile(ctx, *unConcludedVolume, addTaskPayload)
609+
=======
610+
service.register.Worker.Tasks()
611+
612+
_, err = service.register.ProcessFile(ctx, *v, addTaskPayload)
613+
>>>>>>> Stashed changes
581614
if err != nil {
582615
log.WithContext(ctx).WithField("file_id", unConcludedVolume.FileID).WithError(err).Error("error processing un-concluded volume")
583616
continue
@@ -633,6 +666,40 @@ func (service *CascadeAPIHandler) Restore(ctx context.Context, p *cascade.Restor
633666
}, nil
634667
}
635668

669+
func (service *CascadeAPIHandler) checkBurnTxIDsValidForRegistration(ctx context.Context, burnTxIDs []string, files types.Files) error {
670+
if isDuplicateExists(burnTxIDs) {
671+
return errors.New("duplicate burn-tx-ids provided")
672+
}
673+
674+
for _, bid := range burnTxIDs {
675+
if err := service.register.CheckBurnTxIDTicketDuplication(ctx, bid); err != nil {
676+
return err
677+
}
678+
}
679+
680+
for i := 0; i < len(files); i++ {
681+
err := service.register.ValidBurnTxnAmount(ctx, burnTxIDs[i], files[i].ReqBurnTxnAmount)
682+
if err != nil {
683+
return err
684+
}
685+
}
686+
687+
return nil
688+
}
689+
690+
func (service *CascadeAPIHandler) IsBurnTxIDValidForRecovery(ctx context.Context, burnTxID string, estimatedFee float64) bool {
691+
if err := service.register.CheckBurnTxIDTicketDuplication(ctx, burnTxID); err != nil {
692+
return false
693+
}
694+
695+
err := service.register.ValidateBurnTxn(ctx, burnTxID, estimatedFee)
696+
if err != nil {
697+
return false
698+
}
699+
700+
return true
701+
}
702+
636703
// NewCascadeAPIHandler returns the swagger OpenAPI implementation.
637704
func NewCascadeAPIHandler(config *Config, filesMap *sync.Map, register *cascaderegister.CascadeRegistrationService, download *download.NftDownloadingService) *CascadeAPIHandler {
638705
return &CascadeAPIHandler{

walletnode/services/cascaderegister/service.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -714,6 +714,35 @@ func (service *CascadeRegistrationService) RegisterVolumeTicket(ctx context.Cont
714714
return txID, nil
715715
}
716716

717+
func (service *CascadeRegistrationService) CheckBurnTxIDTicketDuplication(ctx context.Context, burnTxID string) error {
718+
err := service.pastelHandler.CheckBurnTxID(ctx, burnTxID)
719+
if err != nil {
720+
return err
721+
}
722+
723+
return nil
724+
}
725+
726+
func (service *CascadeRegistrationService) ValidBurnTxnAmount(ctx context.Context, burnTxID string, estimatedFee float64) error {
727+
txnDetail, err := service.pastelHandler.PastelClient.GetTransaction(ctx, burnTxID)
728+
if err != nil {
729+
return err
730+
}
731+
732+
burnTxnAmount := math.Round(math.Abs(txnDetail.Amount)*100) / 100
733+
estimatedFeeAmount := math.Round(estimatedFee*100) / 100
734+
735+
if burnTxnAmount >= estimatedFeeAmount {
736+
return nil
737+
}
738+
739+
return errors.New("the amounts are not equal")
740+
}
741+
742+
func (service *CascadeRegistrationService) ValidateBurnTxn(ctx context.Context, burnTxID string, estimatedFee float64) error {
743+
return service.pastelHandler.ValidateBurnTxID(ctx, burnTxID, estimatedFee)
744+
}
745+
717746
// NewService returns a new Service instance
718747
func NewService(config *Config, pastelClient pastel.Client, nodeClient node.ClientInterface,
719748
fileStorage storage.FileStorageInterface, db storage.KeyValue,

0 commit comments

Comments
 (0)