@@ -13,9 +13,9 @@ import (
13
13
"github.com/btcsuite/btcd/wire"
14
14
"github.com/lightninglabs/lndclient"
15
15
"github.com/lightninglabs/loop/fsm"
16
- "github.com/lightninglabs/loop/loopdb"
17
16
"github.com/lightninglabs/loop/swap"
18
17
looprpc "github.com/lightninglabs/loop/swapserverrpc"
18
+ "github.com/lightningnetwork/lnd/chainntnfs"
19
19
"github.com/lightningnetwork/lnd/lnrpc"
20
20
"github.com/lightningnetwork/lnd/lntypes"
21
21
"github.com/lightningnetwork/lnd/lnwallet/chainfee"
@@ -36,11 +36,10 @@ var (
36
36
37
37
// initHyperloopContext contains the request parameters for a hyperloop.
38
38
type initHyperloopContext struct {
39
- hyperloopID ID
40
- swapAmt btcutil.Amount
41
- outgoingChanSet * loopdb.ChannelSet
42
- publishTime time.Time
43
- sweepAddr btcutil.Address
39
+ hyperloopID ID
40
+ swapAmt btcutil.Amount
41
+ publishTime time.Time
42
+ sweepAddr btcutil.Address
44
43
}
45
44
46
45
// initHyperloopAction is the action that initializes the hyperloop.
@@ -340,13 +339,25 @@ func (f *FSM) waitForHtlcSig(ctx context.Context,
340
339
bool , error ) {
341
340
return res .FinalHtlcSig != nil , nil
342
341
}
343
- res , err := f .waitForState (ctx , checkFunc )
344
- if err != nil {
342
+
343
+ resChan , errChan := f .waitForStateChan (ctx , checkFunc )
344
+ var res * looprpc.HyperloopNotificationStreamResponse
345
+ select {
346
+ case <- ctx .Done ():
347
+ return fsm .NoOp
348
+
349
+ case err := <- errChan :
345
350
return f .HandleError (err )
351
+
352
+ case res = <- resChan :
353
+
354
+ // We'll also need to check if the hyperloop has been spent.
355
+ case spend := <- f .spendChan :
356
+ return f .getHyperloopSpendingEvent (spend )
346
357
}
347
358
348
359
// Try to register the htlc sig, this will verify the sig is valid.
349
- err = f .hyperloop .registerHtlcSig (f .cfg .ChainParams , res .FinalHtlcSig )
360
+ err : = f .hyperloop .registerHtlcSig (f .cfg .ChainParams , res .FinalHtlcSig )
350
361
if err != nil {
351
362
return f .HandleError (err )
352
363
}
@@ -390,9 +401,22 @@ func (f *FSM) waitForReadyForSweepAction(ctx context.Context,
390
401
bool , error ) {
391
402
return len (res .Participants ) == len (res .SweeplessSweepNonces ), nil
392
403
}
393
- res , err := f .waitForState (ctx , checkFunc )
394
- if err != nil {
404
+
405
+ resChan , errChan := f .waitForStateChan (ctx , checkFunc )
406
+ var res * looprpc.HyperloopNotificationStreamResponse
407
+
408
+ select {
409
+ case <- ctx .Done ():
410
+ return fsm .NoOp
411
+
412
+ case err := <- errChan :
395
413
return f .HandleError (err )
414
+
415
+ case res = <- resChan :
416
+
417
+ // We'll also need to check if the hyperloop has been spent.
418
+ case spend := <- f .spendChan :
419
+ return f .getHyperloopSpendingEvent (spend )
396
420
}
397
421
398
422
var nonces [][66 ]byte
@@ -404,7 +428,7 @@ func (f *FSM) waitForReadyForSweepAction(ctx context.Context,
404
428
nonces = append (nonces , nonce )
405
429
}
406
430
407
- err = f .hyperloop .registerSweeplessSweepNonces (ctx , f .cfg .Signer , nonces )
431
+ err : = f .hyperloop .registerSweeplessSweepNonces (ctx , f .cfg .Signer , nonces )
408
432
if err != nil {
409
433
return f .HandleError (err )
410
434
}
@@ -442,6 +466,18 @@ func (f *FSM) pushSweepSigAction(ctx context.Context,
442
466
return OnPushedSweeplessSweepSig
443
467
}
444
468
469
+ func (f * FSM ) waitForSweepPublishAction (ctx context.Context ,
470
+ _ fsm.EventContext ) fsm.EventType {
471
+
472
+ select {
473
+ case <- ctx .Done ():
474
+ return fsm .NoOp
475
+
476
+ case spend := <- f .spendChan :
477
+ return f .getHyperloopSpendingEvent (spend )
478
+ }
479
+ }
480
+
445
481
// waitForSweeplessSweepConfirmationAction is the action that waits for the
446
482
// sweepless sweep to be confirmed.
447
483
func (f * FSM ) waitForSweeplessSweepConfirmationAction (ctx context.Context ,
@@ -621,3 +657,35 @@ func rpcParticipantToHyperloopParticipant(rpcParticipant []*looprpc.
621
657
622
658
return participants , nil
623
659
}
660
+
661
+ // getHyperloopSpendingEvent returns the event that should be triggered when the
662
+ // hyperloop output is spent. It returns an error if the spending transaction is
663
+ // unexpected.
664
+ func (f * FSM ) getHyperloopSpendingEvent (spendDetails * chainntnfs.SpendDetail ,
665
+ ) fsm.EventType {
666
+
667
+ spendingTxHash := spendDetails .SpendingTx .TxHash ()
668
+
669
+ htlcTx , err := f .hyperloop .getHyperLoopHtlcTx (f .cfg .ChainParams )
670
+ if err != nil {
671
+ return f .HandleError (err )
672
+ }
673
+ htlcTxHash := htlcTx .TxHash ()
674
+
675
+ if bytes .Equal (spendingTxHash [:], htlcTxHash [:]) {
676
+ return OnHtlcPublished
677
+ }
678
+
679
+ sweeplessSweepTx , err := f .hyperloop .getHyperLoopSweeplessSweepTx ()
680
+ if err != nil {
681
+ return f .HandleError (err )
682
+ }
683
+
684
+ sweeplessSweepTxHash := sweeplessSweepTx .TxHash ()
685
+
686
+ if bytes .Equal (spendingTxHash [:], sweeplessSweepTxHash [:]) {
687
+ return OnSweeplessSweepPublish
688
+ }
689
+
690
+ return f .HandleError (errors .New ("unexpected tx spent" ))
691
+ }
0 commit comments