@@ -16,6 +16,7 @@ import (
1616 "github.com/lightninglabs/loop/utils"
1717 "github.com/lightningnetwork/lnd/lnrpc"
1818 "github.com/lightningnetwork/lnd/lntypes"
19+ "github.com/lightningnetwork/lnd/routing/route"
1920 "github.com/stretchr/testify/require"
2021 "google.golang.org/grpc/codes"
2122 "google.golang.org/grpc/status"
4546 defaultConfirmations = int32 (loopdb .DefaultLoopOutHtlcConfirmations )
4647)
4748
49+ var htlcKeys = func () loopdb.HtlcKeys {
50+ var senderKey , receiverKey [33 ]byte
51+
52+ // Generate keys.
53+ _ , senderPubKey := test .CreateKey (1 )
54+ copy (senderKey [:], senderPubKey .SerializeCompressed ())
55+ _ , receiverPubKey := test .CreateKey (2 )
56+ copy (receiverKey [:], receiverPubKey .SerializeCompressed ())
57+
58+ return loopdb.HtlcKeys {
59+ SenderScriptKey : senderKey ,
60+ ReceiverScriptKey : receiverKey ,
61+ SenderInternalPubKey : senderKey ,
62+ ReceiverInternalPubKey : receiverKey ,
63+ }
64+ }()
65+
4866// TestLoopOutSuccess tests the loop out happy flow, using a custom htlc
4967// confirmation target.
5068func TestLoopOutSuccess (t * testing.T ) {
@@ -437,3 +455,59 @@ func TestWrapGrpcError(t *testing.T) {
437455 })
438456 }
439457}
458+
459+ // TestFetchSwapsLastHop asserts that FetchSwaps loads LastHop for LoopIn's.
460+ func TestFetchSwapsLastHop (t * testing.T ) {
461+ defer test .Guard (t )()
462+
463+ ctx := createClientTestContext (t , nil )
464+
465+ lastHop := route.Vertex {1 , 2 , 3 }
466+
467+ // Create a loop in swap.
468+ swapHash := lntypes.Hash {1 , 1 , 1 }
469+ swap := & loopdb.LoopInContract {
470+ SwapContract : loopdb.SwapContract {
471+ CltvExpiry : 111 ,
472+ AmountRequested : 111 ,
473+ ProtocolVersion : loopdb .ProtocolVersionMuSig2 ,
474+ HtlcKeys : htlcKeys ,
475+ },
476+ LastHop : & lastHop ,
477+ }
478+ err := ctx .store .CreateLoopIn (context .Background (), swapHash , swap )
479+ require .NoError (t , err , "CreateLoopOut failed" )
480+
481+ // Now read all the swaps from the store
482+ swapInfos , err := ctx .swapClient .FetchSwaps (context .Background ())
483+ require .NoError (t , err , "FetchSwaps failed" )
484+
485+ // Find the loop-in and compare with the expected value.
486+ require .Len (t , swapInfos , 1 )
487+ loopInInfo := swapInfos [0 ]
488+ wantLoopInInfo := & SwapInfo {
489+ SwapHash : swapHash ,
490+ SwapContract : loopdb.SwapContract {
491+ CltvExpiry : 111 ,
492+ AmountRequested : 111 ,
493+ ProtocolVersion : loopdb .ProtocolVersionMuSig2 ,
494+ HtlcKeys : htlcKeys ,
495+ },
496+
497+ // Make sure LastHop is filled.
498+ LastHop : & lastHop ,
499+ }
500+
501+ // Calculate HtlcAddressP2TR.
502+ htlc , err := utils .GetHtlc (
503+ swapHash , & wantLoopInInfo .SwapContract ,
504+ & chaincfg .TestNet3Params ,
505+ )
506+ require .NoError (t , err )
507+ wantLoopInInfo .HtlcAddressP2TR = htlc .Address
508+
509+ require .Equal (t , wantLoopInInfo , loopInInfo )
510+
511+ // Shutdown the client not to leak goroutines.
512+ ctx .finish ()
513+ }
0 commit comments