Skip to content

Commit dcb3b50

Browse files
authored
Merge pull request #242 from joostjager/user-message
multi: expose server message to clients
2 parents d76959d + 1869ad6 commit dcb3b50

16 files changed

+305
-193
lines changed

client.go

+13-7
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import (
1414
"github.com/lightninglabs/loop/lsat"
1515
"github.com/lightninglabs/loop/swap"
1616
"github.com/lightninglabs/loop/sweep"
17-
"github.com/lightningnetwork/lnd/lntypes"
1817
)
1918

2019
var (
@@ -354,32 +353,37 @@ func (s *Client) resumeSwaps(ctx context.Context,
354353
//
355354
// The return value is a hash that uniquely identifies the new swap.
356355
func (s *Client) LoopOut(globalCtx context.Context,
357-
request *OutRequest) (*lntypes.Hash, btcutil.Address, error) {
356+
request *OutRequest) (*LoopOutSwapInfo, error) {
358357

359358
log.Infof("LoopOut %v to %v (channels: %v)",
360359
request.Amount, request.DestAddr, request.OutgoingChanSet,
361360
)
362361

363362
if err := s.waitForInitialized(globalCtx); err != nil {
364-
return nil, nil, err
363+
return nil, err
365364
}
366365

367366
// Create a new swap object for this swap.
368367
initiationHeight := s.executor.height()
369368
swapCfg := newSwapConfig(s.lndServices, s.Store, s.Server)
370-
swap, err := newLoopOutSwap(
369+
initResult, err := newLoopOutSwap(
371370
globalCtx, swapCfg, initiationHeight, request,
372371
)
373372
if err != nil {
374-
return nil, nil, err
373+
return nil, err
375374
}
375+
swap := initResult.swap
376376

377377
// Post swap to the main loop.
378378
s.executor.initiateSwap(globalCtx, swap)
379379

380380
// Return hash so that the caller can identify this swap in the updates
381381
// stream.
382-
return &swap.hash, swap.htlc.Address, nil
382+
return &LoopOutSwapInfo{
383+
SwapHash: swap.hash,
384+
HtlcAddressP2WSH: swap.htlc.Address,
385+
ServerMessage: initResult.serverMessage,
386+
}, nil
383387
}
384388

385389
// LoopOutQuote takes a LoopOut amount and returns a break down of estimated
@@ -480,12 +484,13 @@ func (s *Client) LoopIn(globalCtx context.Context,
480484
// Create a new swap object for this swap.
481485
initiationHeight := s.executor.height()
482486
swapCfg := newSwapConfig(s.lndServices, s.Store, s.Server)
483-
swap, err := newLoopInSwap(
487+
initResult, err := newLoopInSwap(
484488
globalCtx, swapCfg, initiationHeight, request,
485489
)
486490
if err != nil {
487491
return nil, err
488492
}
493+
swap := initResult.swap
489494

490495
// Post swap to the main loop.
491496
s.executor.initiateSwap(globalCtx, swap)
@@ -496,6 +501,7 @@ func (s *Client) LoopIn(globalCtx context.Context,
496501
SwapHash: swap.hash,
497502
HtlcAddressP2WSH: swap.htlcP2WSH.Address,
498503
HtlcAddressNP2WSH: swap.htlcNP2WSH.Address,
504+
ServerMessage: initResult.serverMessage,
499505
}
500506
return swapInfo, nil
501507
}

client_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func TestSuccess(t *testing.T) {
4545
ctx := createClientTestContext(t, nil)
4646

4747
// Initiate loop out.
48-
hash, _, err := ctx.swapClient.LoopOut(context.Background(), testRequest)
48+
info, err := ctx.swapClient.LoopOut(context.Background(), testRequest)
4949
if err != nil {
5050
t.Fatal(err)
5151
}
@@ -59,7 +59,7 @@ func TestSuccess(t *testing.T) {
5959
// Expect client to register for conf.
6060
confIntent := ctx.AssertRegisterConf(false)
6161

62-
testSuccess(ctx, testRequest.Amount, *hash,
62+
testSuccess(ctx, testRequest.Amount, info.SwapHash,
6363
signalPrepaymentResult, signalSwapPaymentResult, false,
6464
confIntent,
6565
)
@@ -72,7 +72,7 @@ func TestFailOffchain(t *testing.T) {
7272

7373
ctx := createClientTestContext(t, nil)
7474

75-
_, _, err := ctx.swapClient.LoopOut(context.Background(), testRequest)
75+
_, err := ctx.swapClient.LoopOut(context.Background(), testRequest)
7676
if err != nil {
7777
t.Fatal(err)
7878
}
@@ -110,7 +110,7 @@ func TestFailWrongAmount(t *testing.T) {
110110
// Modify mock for this subtest.
111111
modifier(ctx.serverMock)
112112

113-
_, _, err := ctx.swapClient.LoopOut(
113+
_, err := ctx.swapClient.LoopOut(
114114
context.Background(), testRequest,
115115
)
116116
if err != expectedErr {

cmd/loop/loopin.go

+3
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,9 @@ func loopIn(ctx *cli.Context) error {
157157
fmt.Printf("HTLC address (NP2WSH): %v\n", resp.HtlcAddressNp2Wsh)
158158
}
159159
fmt.Printf("HTLC address (P2WSH): %v\n", resp.HtlcAddressP2Wsh)
160+
if resp.ServerMessage != "" {
161+
fmt.Printf("Server message: %v\n", resp.ServerMessage)
162+
}
160163
fmt.Println()
161164
fmt.Printf("Run `loop monitor` to monitor progress.\n")
162165

cmd/loop/loopout.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,11 @@ func loopOut(ctx *cli.Context) error {
180180
}
181181

182182
fmt.Printf("Swap initiated\n")
183-
fmt.Printf("ID: %v\n", resp.Id)
184-
fmt.Printf("HTLC address: %v\n", resp.HtlcAddress)
183+
fmt.Printf("ID: %v\n", resp.Id)
184+
fmt.Printf("HTLC address: %v\n", resp.HtlcAddress)
185+
if resp.ServerMessage != "" {
186+
fmt.Printf("Server message: %v\n", resp.ServerMessage)
187+
}
185188
fmt.Println()
186189
fmt.Printf("Run `loop monitor` to monitor progress.\n")
187190

interface.go

+19
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,25 @@ type LoopInSwapInfo struct { // nolint
248248
// HtlcAddressNP2WSH contains the nested segwit swap htlc address,
249249
// where the loop-in funds may be paid.
250250
HtlcAddressNP2WSH btcutil.Address
251+
252+
// ServerMessages is the human-readable message received from the loop
253+
// server.
254+
ServerMessage string
255+
}
256+
257+
// LoopOutSwapInfo contains essential information of a loop-out swap after the
258+
// swap is initiated.
259+
type LoopOutSwapInfo struct { // nolint:golint
260+
// SwapHash contains the sha256 hash of the swap preimage.
261+
SwapHash lntypes.Hash
262+
263+
// HtlcAddressP2WSH contains the native segwit swap htlc address that
264+
// the server will publish to.
265+
HtlcAddressP2WSH btcutil.Address
266+
267+
// ServerMessages is the human-readable message received from the loop
268+
// server.
269+
ServerMessage string
251270
}
252271

253272
// SwapInfoKit contains common swap info fields.

loopd/swapclient_server.go

+7-5
Original file line numberDiff line numberDiff line change
@@ -103,17 +103,18 @@ func (s *swapClientServer) LoopOut(ctx context.Context,
103103
req.OutgoingChanSet = in.OutgoingChanSet
104104
}
105105

106-
hash, htlc, err := s.impl.LoopOut(ctx, req)
106+
info, err := s.impl.LoopOut(ctx, req)
107107
if err != nil {
108108
log.Errorf("LoopOut: %v", err)
109109
return nil, err
110110
}
111111

112112
return &looprpc.SwapResponse{
113-
Id: hash.String(),
114-
IdBytes: hash[:],
115-
HtlcAddress: htlc.String(),
116-
HtlcAddressP2Wsh: htlc.String(),
113+
Id: info.SwapHash.String(),
114+
IdBytes: info.SwapHash[:],
115+
HtlcAddress: info.HtlcAddressP2WSH.String(),
116+
HtlcAddressP2Wsh: info.HtlcAddressP2WSH.String(),
117+
ServerMessage: info.ServerMessage,
117118
}, nil
118119
}
119120

@@ -456,6 +457,7 @@ func (s *swapClientServer) LoopIn(ctx context.Context,
456457
Id: swapInfo.SwapHash.String(),
457458
IdBytes: swapInfo.SwapHash[:],
458459
HtlcAddressP2Wsh: swapInfo.HtlcAddressP2WSH.String(),
460+
ServerMessage: swapInfo.ServerMessage,
459461
}
460462

461463
if req.ExternalHtlc {

loopin.go

+16-2
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,16 @@ type loopInSwap struct {
6363
timeoutAddr btcutil.Address
6464
}
6565

66+
// loopInInitResult contains information about a just-initiated loop in swap.
67+
type loopInInitResult struct {
68+
swap *loopInSwap
69+
serverMessage string
70+
}
71+
6672
// newLoopInSwap initiates a new loop in swap.
6773
func newLoopInSwap(globalCtx context.Context, cfg *swapConfig,
68-
currentHeight int32, request *LoopInRequest) (*loopInSwap, error) {
74+
currentHeight int32, request *LoopInRequest) (*loopInInitResult,
75+
error) {
6976

7077
// Request current server loop in terms and use these to calculate the
7178
// swap fee that we should subtract from the swap amount in the payment
@@ -180,7 +187,14 @@ func newLoopInSwap(globalCtx context.Context, cfg *swapConfig,
180187
return nil, fmt.Errorf("cannot store swap: %v", err)
181188
}
182189

183-
return swap, nil
190+
if swapResp.serverMessage != "" {
191+
swap.log.Infof("Server message: %v", swapResp.serverMessage)
192+
}
193+
194+
return &loopInInitResult{
195+
swap: swap,
196+
serverMessage: swapResp.serverMessage,
197+
}, nil
184198
}
185199

186200
// resumeLoopInSwap returns a swap object representing a pending swap that has

loopin_test.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,14 @@ func TestLoopInSuccess(t *testing.T) {
3535

3636
cfg := newSwapConfig(&ctx.lnd.LndServices, ctx.store, ctx.server)
3737

38-
swap, err := newLoopInSwap(
38+
initResult, err := newLoopInSwap(
3939
context.Background(), cfg,
4040
height, &testLoopInRequest,
4141
)
4242
if err != nil {
4343
t.Fatal(err)
4444
}
45+
swap := initResult.swap
4546

4647
ctx.store.assertLoopInStored()
4748

@@ -159,13 +160,14 @@ func testLoopInTimeout(t *testing.T,
159160
req.ExternalHtlc = true
160161
}
161162

162-
s, err := newLoopInSwap(
163+
initResult, err := newLoopInSwap(
163164
context.Background(), cfg,
164165
height, &req,
165166
)
166167
if err != nil {
167168
t.Fatal(err)
168169
}
170+
s := initResult.swap
169171

170172
ctx.store.assertLoopInStored()
171173

loopout.go

+15-2
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,16 @@ type executeConfig struct {
7373
loopOutMaxParts uint32
7474
}
7575

76+
// loopOutInitResult contains information about a just-initiated loop out swap.
77+
type loopOutInitResult struct {
78+
swap *loopOutSwap
79+
serverMessage string
80+
}
81+
7682
// newLoopOutSwap initiates a new swap with the server and returns a
7783
// corresponding swap object.
7884
func newLoopOutSwap(globalCtx context.Context, cfg *swapConfig,
79-
currentHeight int32, request *OutRequest) (*loopOutSwap, error) {
85+
currentHeight int32, request *OutRequest) (*loopOutInitResult, error) {
8086

8187
// Generate random preimage.
8288
var swapPreimage [32]byte
@@ -176,7 +182,14 @@ func newLoopOutSwap(globalCtx context.Context, cfg *swapConfig,
176182
return nil, fmt.Errorf("cannot store swap: %v", err)
177183
}
178184

179-
return swap, nil
185+
if swapResp.serverMessage != "" {
186+
swap.log.Infof("Server message: %v", swapResp.serverMessage)
187+
}
188+
189+
return &loopOutInitResult{
190+
swap: swap,
191+
serverMessage: swapResp.serverMessage,
192+
}, nil
180193
}
181194

182195
// resumeLoopOutSwap returns a swap object representing a pending swap that has

loopout_test.go

+8-4
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,13 @@ func TestLoopOutPaymentParameters(t *testing.T) {
5454
req := *testRequest
5555
req.OutgoingChanSet = loopdb.ChannelSet{2, 3}
5656

57-
swap, err := newLoopOutSwap(
57+
initResult, err := newLoopOutSwap(
5858
context.Background(), cfg, height, &req,
5959
)
6060
if err != nil {
6161
t.Fatal(err)
6262
}
63+
swap := initResult.swap
6364

6465
// Execute the swap in its own goroutine.
6566
errChan := make(chan error)
@@ -150,12 +151,13 @@ func TestLateHtlcPublish(t *testing.T) {
150151

151152
cfg := newSwapConfig(&lnd.LndServices, store, server)
152153

153-
swap, err := newLoopOutSwap(
154+
initResult, err := newLoopOutSwap(
154155
context.Background(), cfg, height, testRequest,
155156
)
156157
if err != nil {
157158
t.Fatal(err)
158159
}
160+
swap := initResult.swap
159161

160162
sweeper := &sweep.Sweeper{Lnd: &lnd.LndServices}
161163

@@ -235,12 +237,13 @@ func TestCustomSweepConfTarget(t *testing.T) {
235237
&lnd.LndServices, newStoreMock(t), server,
236238
)
237239

238-
swap, err := newLoopOutSwap(
240+
initResult, err := newLoopOutSwap(
239241
context.Background(), cfg, ctx.Lnd.Height, testRequest,
240242
)
241243
if err != nil {
242244
t.Fatal(err)
243245
}
246+
swap := initResult.swap
244247

245248
// Set up the required dependencies to execute the swap.
246249
//
@@ -442,10 +445,11 @@ func TestPreimagePush(t *testing.T) {
442445
&lnd.LndServices, newStoreMock(t), server,
443446
)
444447

445-
swap, err := newLoopOutSwap(
448+
initResult, err := newLoopOutSwap(
446449
context.Background(), cfg, ctx.Lnd.Height, testRequest,
447450
)
448451
require.NoError(t, err)
452+
swap := initResult.swap
449453

450454
// Set up the required dependencies to execute the swap.
451455
sweeper := &sweep.Sweeper{Lnd: &lnd.LndServices}

0 commit comments

Comments
 (0)