Skip to content

Commit efb6f75

Browse files
committed
swap: add lasthop, outgoingchanids to swapinfo
1 parent e98d813 commit efb6f75

File tree

5 files changed

+50
-3
lines changed

5 files changed

+50
-3
lines changed

interface.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,15 @@ type SwapInfo struct {
353353

354354
// ExternalHtlc is set to true for external loop-in swaps.
355355
ExternalHtlc bool
356+
357+
// LastHop optionally specifies the last hop to use for the loop in
358+
// payment. On a loop out this field is nil.
359+
LastHop *route.Vertex
360+
361+
// OutgoingChanSet optionally specifies the short channel ids of the
362+
// channels that may be used to loop out. On a loop in this field
363+
// is nil.
364+
OutgoingChanSet loopdb.ChannelSet
356365
}
357366

358367
// LastUpdate returns the last update time of the swap

loopin.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,14 @@ func (s *loopInSwap) sendUpdate(ctx context.Context) error {
431431
info.HtlcAddressNP2WSH = s.htlcNP2WSH.Address
432432
info.ExternalHtlc = s.ExternalHtlc
433433

434+
// In order to avoid potentially dangerous ownership sharing
435+
// we copy the last hop vertex.
436+
if s.LastHop != nil {
437+
lastHop := &route.Vertex{}
438+
copy(lastHop[:], s.LastHop[:])
439+
info.LastHop = lastHop
440+
}
441+
434442
select {
435443
case s.statusChan <- *info:
436444
case <-ctx.Done():

loopin_test.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/lightninglabs/loop/test"
1212
"github.com/lightningnetwork/lnd/chainntnfs"
1313
"github.com/lightningnetwork/lnd/channeldb"
14+
"github.com/lightningnetwork/lnd/routing/route"
1415
"github.com/stretchr/testify/require"
1516
)
1617

@@ -34,9 +35,14 @@ func TestLoopInSuccess(t *testing.T) {
3435

3536
cfg := newSwapConfig(&ctx.lnd.LndServices, ctx.store, ctx.server)
3637

38+
expectedLastHop := &route.Vertex{0x02}
39+
40+
req := &testLoopInRequest
41+
req.LastHop = expectedLastHop
42+
3743
initResult, err := newLoopInSwap(
3844
context.Background(), cfg,
39-
height, &testLoopInRequest,
45+
height, req,
4046
)
4147
if err != nil {
4248
t.Fatal(err)
@@ -54,7 +60,14 @@ func TestLoopInSuccess(t *testing.T) {
5460
errChan <- err
5561
}()
5662

57-
ctx.assertState(loopdb.StateInitiated)
63+
swapInfo := <-ctx.statusChan
64+
require.Equal(t, loopdb.StateInitiated, swapInfo.State)
65+
66+
// Check that the SwapInfo contains the provided last hop.
67+
require.Equal(t, expectedLastHop, swapInfo.LastHop)
68+
69+
// Check that the SwapInfo does not contain an outgoing chan set.
70+
require.Nil(t, swapInfo.OutgoingChanSet)
5871

5972
ctx.assertState(loopdb.StateHtlcPublished)
6073
ctx.store.assertLoopInState(loopdb.StateHtlcPublished)

loopout.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,15 @@ func (s *loopOutSwap) sendUpdate(ctx context.Context) error {
306306

307307
info.HtlcAddressP2WSH = s.htlc.Address
308308

309+
// In order to avoid potentially dangerous ownership sharing
310+
// we copy the outgoing channel set.
311+
if s.OutgoingChanSet != nil {
312+
outgoingChanSet := make(loopdb.ChannelSet, len(s.OutgoingChanSet))
313+
copy(outgoingChanSet[:], s.OutgoingChanSet[:])
314+
315+
info.OutgoingChanSet = outgoingChanSet
316+
}
317+
309318
select {
310319
case s.statusChan <- *info:
311320
case <-ctx.Done():

loopout_test.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,11 @@ func TestLoopOutPaymentParameters(t *testing.T) {
5252

5353
const maxParts = 5
5454

55+
chanSet := loopdb.ChannelSet{2, 3}
56+
5557
// Initiate the swap.
5658
req := *testRequest
57-
req.OutgoingChanSet = loopdb.ChannelSet{2, 3}
59+
req.OutgoingChanSet = chanSet
5860

5961
initResult, err := newLoopOutSwap(
6062
context.Background(), cfg, height, &req,
@@ -90,6 +92,12 @@ func TestLoopOutPaymentParameters(t *testing.T) {
9092
t.Fatal("unexpected state")
9193
}
9294

95+
// Check that the SwapInfo contains the outgoing chan set
96+
require.Equal(t, chanSet, state.OutgoingChanSet)
97+
98+
// Check that the SwapInfo does not contain a last hop
99+
require.Nil(t, state.LastHop)
100+
93101
// Intercept the swap and prepay payments. Order is undefined.
94102
payments := []test.RouterPaymentChannelMessage{
95103
<-ctx.Lnd.RouterSendPaymentChannel,

0 commit comments

Comments
 (0)