Skip to content

Commit ed3db3b

Browse files
committed
client: fill LastHop in FetchSwaps
Loop used to "forget" last_hop of swaps created before its restart. The commit fixes this. Added test TestFetchSwapsLastHop for this. Filled field abandonChans in test utility function newSwapClient because the field is used in new test. Fixes #798
1 parent 8c97c60 commit ed3db3b

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-0
lines changed

client.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,7 @@ func (s *Client) FetchSwaps(ctx context.Context) ([]*SwapInfo, error) {
283283
SwapStateData: swp.State(),
284284
SwapHash: swp.Hash,
285285
LastUpdate: swp.LastUpdateTime(),
286+
LastHop: swp.Contract.LastHop,
286287
}
287288

288289
htlc, err := utils.GetHtlc(

client_test.go

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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"
@@ -45,6 +46,23 @@ var (
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.
5068
func 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+
}

testcontext_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ func newSwapClient(t *testing.T, config *clientConfig) *Client {
108108
sweeper: sweeper,
109109
executor: executor,
110110
resumeReady: make(chan struct{}),
111+
abandonChans: make(map[lntypes.Hash]chan struct{}),
111112
}
112113
}
113114

0 commit comments

Comments
 (0)