Skip to content

Commit 14dfdc8

Browse files
authored
Merge pull request #500 from sputn1ck/update_listswaps_api
Include provided last_hop and outgoing_chan_set to listswaps rpc
2 parents e98d813 + ece0f76 commit 14dfdc8

File tree

9 files changed

+423
-325
lines changed

9 files changed

+423
-325
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

loopd/swapclient_server.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,8 @@ func (s *swapClientServer) marshallSwap(loopSwap *loop.SwapInfo) (
220220

221221
var swapType clientrpc.SwapType
222222
var htlcAddress, htlcAddressP2WSH, htlcAddressNP2WSH string
223+
var outGoingChanSet []uint64
224+
var lastHop []byte
223225

224226
switch loopSwap.SwapType {
225227
case swap.TypeIn:
@@ -233,11 +235,17 @@ func (s *swapClientServer) marshallSwap(loopSwap *loop.SwapInfo) (
233235
htlcAddress = htlcAddressP2WSH
234236
}
235237

238+
if loopSwap.LastHop != nil {
239+
lastHop = loopSwap.LastHop[:]
240+
}
241+
236242
case swap.TypeOut:
237243
swapType = clientrpc.SwapType_LOOP_OUT
238244
htlcAddressP2WSH = loopSwap.HtlcAddressP2WSH.EncodeAddress()
239245
htlcAddress = htlcAddressP2WSH
240246

247+
outGoingChanSet = loopSwap.OutgoingChanSet
248+
241249
default:
242250
return nil, errors.New("unknown swap type")
243251
}
@@ -258,6 +266,8 @@ func (s *swapClientServer) marshallSwap(loopSwap *loop.SwapInfo) (
258266
CostOnchain: int64(loopSwap.Cost.Onchain),
259267
CostOffchain: int64(loopSwap.Cost.Offchain),
260268
Label: loopSwap.Label,
269+
LastHop: lastHop,
270+
OutgoingChanSet: outGoingChanSet,
261271
}, nil
262272
}
263273

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)