@@ -178,6 +178,15 @@ func (service *CascadeAPIHandler) StartProcessing(ctx context.Context, p *cascad
178
178
}
179
179
sortedRelatedFiles := service .register .SortFilesWithHigherAmounts (relatedFiles )
180
180
181
+ err = service .checkBurnTxIDsValidForRegistration (ctx , sortedBurnTxids , sortedRelatedFiles )
182
+ if err != nil {
183
+ log .WithContext (ctx ).WithField ("related_volumes" , len (relatedFiles )).
184
+ WithError (err ).
185
+ WithField ("burn_txids" , len (p .BurnTxids )).
186
+ Error ("given burn-tx-ids are not valid" )
187
+ return nil , cascade .MakeBadRequest (errors .New ("given burn txids are not valid" ))
188
+ }
189
+
181
190
var taskIDs []string
182
191
for index , file := range sortedRelatedFiles {
183
192
burnTxID := sortedBurnTxids [index ]
@@ -587,12 +596,21 @@ func (service *CascadeAPIHandler) Restore(ctx context.Context, p *cascade.Restor
587
596
volumesWithPendingRegistration ++
588
597
logger .WithField ("volume_name" , v .FileID ).Info ("find a volume with no registration, trying again..." )
589
598
590
- burnTxId , err := service .register .GetBurnTxIdByAmount (ctx , int64 (v .ReqBurnTxnAmount ))
591
- if err != nil {
592
- log .WithContext (ctx ).WithField ("amount" , int64 (v .ReqBurnTxnAmount )).WithError (err ).Error ("error getting burn TxId for amount" )
593
- return nil , cascade .MakeInternalServerError (err )
599
+ var burnTxId string
600
+ if service .IsBurnTxIDValidForRecovery (ctx , v .BurnTxnID , v .ReqAmount - 10 ) {
601
+ log .WithContext (ctx ).WithField ("burn_txid" , v .BurnTxnID ).Info ("existing burn-txid is valid" )
602
+ burnTxId = v .BurnTxnID
603
+ } else {
604
+ log .WithContext (ctx ).WithField ("burn_txid" , v .BurnTxnID ).Info ("existing burn-txid is not valid, burning the new txid" )
605
+
606
+ burnTxId , err = service .register .GetBurnTxIdByAmount (ctx , int64 (v .ReqBurnTxnAmount ))
607
+ if err != nil {
608
+ log .WithContext (ctx ).WithField ("amount" , int64 (v .ReqBurnTxnAmount )).WithError (err ).Error ("error getting burn TxId for amount" )
609
+ return nil , cascade .MakeInternalServerError (err )
610
+ }
611
+
612
+ logger .WithField ("volume_name" , v .FileID ).Info ("estimated fee has been burned, sending for registration" )
594
613
}
595
- logger .WithField ("volume_name" , v .FileID ).Info ("estimated fee has been burned, sending for registration" )
596
614
597
615
addTaskPayload := & common.AddTaskPayload {
598
616
FileID : v .FileID ,
@@ -668,6 +686,40 @@ func (service *CascadeAPIHandler) Restore(ctx context.Context, p *cascade.Restor
668
686
}, nil
669
687
}
670
688
689
+ func (service * CascadeAPIHandler ) checkBurnTxIDsValidForRegistration (ctx context.Context , burnTxIDs []string , files types.Files ) error {
690
+ if isDuplicateExists (burnTxIDs ) {
691
+ return errors .New ("duplicate burn-tx-ids provided" )
692
+ }
693
+
694
+ for _ , bid := range burnTxIDs {
695
+ if err := service .register .CheckBurnTxIDTicketDuplication (ctx , bid ); err != nil {
696
+ return err
697
+ }
698
+ }
699
+
700
+ for i := 0 ; i < len (files ); i ++ {
701
+ err := service .register .ValidBurnTxnAmount (ctx , burnTxIDs [i ], files [i ].ReqBurnTxnAmount )
702
+ if err != nil {
703
+ return err
704
+ }
705
+ }
706
+
707
+ return nil
708
+ }
709
+
710
+ func (service * CascadeAPIHandler ) IsBurnTxIDValidForRecovery (ctx context.Context , burnTxID string , estimatedFee float64 ) bool {
711
+ if err := service .register .CheckBurnTxIDTicketDuplication (ctx , burnTxID ); err != nil {
712
+ return false
713
+ }
714
+
715
+ err := service .register .ValidateBurnTxn (ctx , burnTxID , estimatedFee )
716
+ if err != nil {
717
+ return false
718
+ }
719
+
720
+ return true
721
+ }
722
+
671
723
// NewCascadeAPIHandler returns the swagger OpenAPI implementation.
672
724
func NewCascadeAPIHandler (config * Config , filesMap * sync.Map , register * cascaderegister.CascadeRegistrationService , download * download.NftDownloadingService ) * CascadeAPIHandler {
673
725
return & CascadeAPIHandler {
0 commit comments