@@ -13,9 +13,9 @@ import (
1313 "github.com/btcsuite/btcd/wire"
1414 "github.com/lightninglabs/lndclient"
1515 "github.com/lightninglabs/loop/fsm"
16- "github.com/lightninglabs/loop/loopdb"
1716 "github.com/lightninglabs/loop/swap"
1817 looprpc "github.com/lightninglabs/loop/swapserverrpc"
18+ "github.com/lightningnetwork/lnd/chainntnfs"
1919 "github.com/lightningnetwork/lnd/lnrpc"
2020 "github.com/lightningnetwork/lnd/lntypes"
2121 "github.com/lightningnetwork/lnd/lnwallet/chainfee"
@@ -36,11 +36,10 @@ var (
3636
3737// initHyperloopContext contains the request parameters for a hyperloop.
3838type 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
4443}
4544
4645// initHyperloopAction is the action that initializes the hyperloop.
@@ -340,13 +339,25 @@ func (f *FSM) waitForHtlcSig(ctx context.Context,
340339 bool , error ) {
341340 return res .FinalHtlcSig != nil , nil
342341 }
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 :
345350 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 )
346357 }
347358
348359 // 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 )
350361 if err != nil {
351362 return f .HandleError (err )
352363 }
@@ -390,9 +401,22 @@ func (f *FSM) waitForReadyForSweepAction(ctx context.Context,
390401 bool , error ) {
391402 return len (res .Participants ) == len (res .SweeplessSweepNonces ), nil
392403 }
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 :
395413 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 )
396420 }
397421
398422 var nonces [][66 ]byte
@@ -404,7 +428,7 @@ func (f *FSM) waitForReadyForSweepAction(ctx context.Context,
404428 nonces = append (nonces , nonce )
405429 }
406430
407- err = f .hyperloop .registerSweeplessSweepNonces (ctx , f .cfg .Signer , nonces )
431+ err : = f .hyperloop .registerSweeplessSweepNonces (ctx , f .cfg .Signer , nonces )
408432 if err != nil {
409433 return f .HandleError (err )
410434 }
@@ -442,6 +466,18 @@ func (f *FSM) pushSweepSigAction(ctx context.Context,
442466 return OnPushedSweeplessSweepSig
443467}
444468
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+
445481// waitForSweeplessSweepConfirmationAction is the action that waits for the
446482// sweepless sweep to be confirmed.
447483func (f * FSM ) waitForSweeplessSweepConfirmationAction (ctx context.Context ,
@@ -621,3 +657,35 @@ func rpcParticipantToHyperloopParticipant(rpcParticipant []*looprpc.
621657
622658 return participants , nil
623659}
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