Skip to content

Commit cd2b08a

Browse files
committed
multi: consume and log sever state updates
1 parent a6539b6 commit cd2b08a

File tree

8 files changed

+819
-65
lines changed

8 files changed

+819
-65
lines changed

client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ func NewClient(dbDir string, cfg *ClientConfig) (*Client, func(), error) {
162162
}
163163

164164
cleanup := func() {
165-
swapServerClient.Close()
165+
swapServerClient.stop()
166166
}
167167

168168
return client, cleanup, nil

loopin.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"crypto/rand"
66
"crypto/sha256"
77
"fmt"
8+
"sync"
89
"time"
910

1011
"github.com/btcsuite/btcutil"
@@ -61,6 +62,8 @@ type loopInSwap struct {
6162
htlcTxHash *chainhash.Hash
6263

6364
timeoutAddr btcutil.Address
65+
66+
wg sync.WaitGroup
6467
}
6568

6669
// loopInInitResult contains information about a just-initiated loop in swap.
@@ -293,9 +296,25 @@ func (s *loopInSwap) sendUpdate(ctx context.Context) error {
293296
func (s *loopInSwap) execute(mainCtx context.Context,
294297
cfg *executeConfig, height int32) error {
295298

299+
defer s.wg.Wait()
300+
296301
s.executeConfig = *cfg
297302
s.height = height
298303

304+
// Create context for our state subscription which we will cancel once
305+
// swap execution has completed, ensuring that we kill the subscribe
306+
// goroutine.
307+
subCtx, cancel := context.WithCancel(mainCtx)
308+
defer cancel()
309+
310+
s.wg.Add(1)
311+
go func() {
312+
defer s.wg.Done()
313+
subscribeAndLogUpdates(
314+
subCtx, s.hash, s.log, s.server.SubscribeLoopInUpdates,
315+
)
316+
}()
317+
299318
// Announce swap by sending out an initial update.
300319
err := s.sendUpdate(mainCtx)
301320
if err != nil {

loopout.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"crypto/sha256"
77
"errors"
88
"fmt"
9+
"sync"
910
"time"
1011

1112
"github.com/btcsuite/btcd/chaincfg/chainhash"
@@ -62,6 +63,8 @@ type loopOutSwap struct {
6263

6364
swapPaymentChan chan lndclient.PaymentResult
6465
prePaymentChan chan lndclient.PaymentResult
66+
67+
wg sync.WaitGroup
6568
}
6669

6770
// executeConfig contains extra configuration to execute the swap.
@@ -254,9 +257,25 @@ func (s *loopOutSwap) sendUpdate(ctx context.Context) error {
254257
func (s *loopOutSwap) execute(mainCtx context.Context,
255258
cfg *executeConfig, height int32) error {
256259

260+
defer s.wg.Wait()
261+
257262
s.executeConfig = *cfg
258263
s.height = height
259264

265+
// Create context for our state subscription which we will cancel once
266+
// swap execution has completed, ensuring that we kill the subscribe
267+
// goroutine.
268+
subCtx, cancel := context.WithCancel(mainCtx)
269+
defer cancel()
270+
271+
s.wg.Add(1)
272+
go func() {
273+
defer s.wg.Done()
274+
subscribeAndLogUpdates(
275+
subCtx, s.hash, s.log, s.server.SubscribeLoopOutUpdates,
276+
)
277+
}()
278+
260279
// Execute swap.
261280
err := s.executeAndFinalize(mainCtx)
262281

0 commit comments

Comments
 (0)