Skip to content

Commit 8b215ed

Browse files
committed
multi: create init result structs
1 parent 68012f0 commit 8b215ed

File tree

5 files changed

+35
-12
lines changed

5 files changed

+35
-12
lines changed

client.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -366,12 +366,13 @@ func (s *Client) LoopOut(globalCtx context.Context,
366366
// Create a new swap object for this swap.
367367
initiationHeight := s.executor.height()
368368
swapCfg := newSwapConfig(s.lndServices, s.Store, s.Server)
369-
swap, err := newLoopOutSwap(
369+
initResult, err := newLoopOutSwap(
370370
globalCtx, swapCfg, initiationHeight, request,
371371
)
372372
if err != nil {
373373
return nil, err
374374
}
375+
swap := initResult.swap
375376

376377
// Post swap to the main loop.
377378
s.executor.initiateSwap(globalCtx, swap)
@@ -482,12 +483,13 @@ func (s *Client) LoopIn(globalCtx context.Context,
482483
// Create a new swap object for this swap.
483484
initiationHeight := s.executor.height()
484485
swapCfg := newSwapConfig(s.lndServices, s.Store, s.Server)
485-
swap, err := newLoopInSwap(
486+
initResult, err := newLoopInSwap(
486487
globalCtx, swapCfg, initiationHeight, request,
487488
)
488489
if err != nil {
489490
return nil, err
490491
}
492+
swap := initResult.swap
491493

492494
// Post swap to the main loop.
493495
s.executor.initiateSwap(globalCtx, swap)

loopin.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,15 @@ 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+
}
70+
6671
// newLoopInSwap initiates a new loop in swap.
6772
func newLoopInSwap(globalCtx context.Context, cfg *swapConfig,
68-
currentHeight int32, request *LoopInRequest) (*loopInSwap, error) {
73+
currentHeight int32, request *LoopInRequest) (*loopInInitResult,
74+
error) {
6975

7076
// Request current server loop in terms and use these to calculate the
7177
// swap fee that we should subtract from the swap amount in the payment
@@ -184,7 +190,9 @@ func newLoopInSwap(globalCtx context.Context, cfg *swapConfig,
184190
swap.log.Infof("Server message: %v", swapResp.serverMessage)
185191
}
186192

187-
return swap, nil
193+
return &loopInInitResult{
194+
swap: swap,
195+
}, nil
188196
}
189197

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

loopin_test.go

Lines changed: 4 additions & 2 deletions
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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,15 @@ 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+
}
80+
7681
// newLoopOutSwap initiates a new swap with the server and returns a
7782
// corresponding swap object.
7883
func newLoopOutSwap(globalCtx context.Context, cfg *swapConfig,
79-
currentHeight int32, request *OutRequest) (*loopOutSwap, error) {
84+
currentHeight int32, request *OutRequest) (*loopOutInitResult, error) {
8085

8186
// Generate random preimage.
8287
var swapPreimage [32]byte
@@ -180,7 +185,9 @@ func newLoopOutSwap(globalCtx context.Context, cfg *swapConfig,
180185
swap.log.Infof("Server message: %v", swapResp.serverMessage)
181186
}
182187

183-
return swap, nil
188+
return &loopOutInitResult{
189+
swap: swap,
190+
}, nil
184191
}
185192

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

loopout_test.go

Lines changed: 8 additions & 4 deletions
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)