Skip to content

Commit 0914074

Browse files
committed
loop: integrate sweepbatcher to loopout flow
1 parent 849d26b commit 0914074

File tree

7 files changed

+215
-472
lines changed

7 files changed

+215
-472
lines changed

client.go

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"github.com/lightninglabs/loop/loopdb"
1818
"github.com/lightninglabs/loop/swap"
1919
"github.com/lightninglabs/loop/sweep"
20+
"github.com/lightninglabs/loop/sweepbatcher"
2021
"github.com/lightninglabs/loop/utils"
2122
"github.com/lightningnetwork/lnd/lntypes"
2223
"github.com/lightningnetwork/lnd/routing/route"
@@ -61,7 +62,7 @@ var (
6162
// probeTimeout is the maximum time until a probe is allowed to take.
6263
probeTimeout = 3 * time.Minute
6364

64-
republishDelay = 10 * time.Second
65+
repushDelay = 1 * time.Second
6566

6667
// MinerFeeEstimationFailed is a magic number that is returned in a
6768
// quote call as the miner fee if the fee estimation in lnd's wallet
@@ -134,7 +135,8 @@ type ClientConfig struct {
134135

135136
// NewClient returns a new instance to initiate swaps with.
136137
func NewClient(dbDir string, loopDB loopdb.SwapStore,
137-
cfg *ClientConfig) (*Client, func(), error) {
138+
sweeperDb sweepbatcher.BatcherStore, cfg *ClientConfig) (
139+
*Client, func(), error) {
138140

139141
lsatStore, err := lsat.NewFileStore(dbDir)
140142
if err != nil {
@@ -162,27 +164,36 @@ func NewClient(dbDir string, loopDB loopdb.SwapStore,
162164
Lnd: cfg.Lnd,
163165
}
164166

167+
verifySchnorrSig := func(pubKey *btcec.PublicKey, hash, sig []byte) error {
168+
schnorrSig, err := schnorr.ParseSignature(sig)
169+
if err != nil {
170+
return err
171+
}
172+
173+
if !schnorrSig.Verify(hash, pubKey) {
174+
return fmt.Errorf("invalid signature")
175+
}
176+
177+
return nil
178+
}
179+
180+
batcher := sweepbatcher.NewBatcher(
181+
cfg.Lnd.WalletKit, cfg.Lnd.ChainNotifier, cfg.Lnd.Signer,
182+
swapServerClient.MultiMuSig2SignSweep, verifySchnorrSig,
183+
cfg.Lnd.ChainParams, sweeperDb, loopDB,
184+
)
185+
165186
executor := newExecutor(&executorConfig{
166187
lnd: cfg.Lnd,
167188
store: loopDB,
168189
sweeper: sweeper,
190+
batcher: batcher,
169191
createExpiryTimer: config.CreateExpiryTimer,
170192
loopOutMaxParts: cfg.LoopOutMaxParts,
171193
totalPaymentTimeout: cfg.TotalPaymentTimeout,
172194
maxPaymentRetries: cfg.MaxPaymentRetries,
173195
cancelSwap: swapServerClient.CancelLoopOutSwap,
174-
verifySchnorrSig: func(pubKey *btcec.PublicKey, hash, sig []byte) error {
175-
schnorrSig, err := schnorr.ParseSignature(sig)
176-
if err != nil {
177-
return err
178-
}
179-
180-
if !schnorrSig.Verify(hash, pubKey) {
181-
return fmt.Errorf("invalid signature")
182-
}
183-
184-
return nil
185-
},
196+
verifySchnorrSig: verifySchnorrSig,
186197
})
187198

188199
client := &Client{

executor.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/lightninglabs/lndclient"
1414
"github.com/lightninglabs/loop/loopdb"
1515
"github.com/lightninglabs/loop/sweep"
16+
"github.com/lightninglabs/loop/sweepbatcher"
1617
"github.com/lightningnetwork/lnd/lntypes"
1718
"github.com/lightningnetwork/lnd/queue"
1819
)
@@ -23,6 +24,8 @@ type executorConfig struct {
2324

2425
sweeper *sweep.Sweeper
2526

27+
batcher *sweepbatcher.Batcher
28+
2629
store loopdb.SwapStore
2730

2831
createExpiryTimer func(expiry time.Duration) <-chan time.Time
@@ -71,6 +74,7 @@ func (s *executor) run(mainCtx context.Context,
7174
err error
7275
blockEpochChan <-chan int32
7376
blockErrorChan <-chan error
77+
batcherErrChan chan error
7478
)
7579

7680
for {
@@ -121,6 +125,21 @@ func (s *executor) run(mainCtx context.Context,
121125
return mainCtx.Err()
122126
}
123127

128+
batcherErrChan = make(chan error, 1)
129+
130+
s.wg.Add(1)
131+
go func() {
132+
defer s.wg.Done()
133+
134+
err := s.batcher.Run(mainCtx)
135+
if err != nil {
136+
select {
137+
case batcherErrChan <- err:
138+
case <-mainCtx.Done():
139+
}
140+
}
141+
}()
142+
124143
// Start main event loop.
125144
log.Infof("Starting event loop at height %v", height)
126145

@@ -156,6 +175,7 @@ func (s *executor) run(mainCtx context.Context,
156175
err := newSwap.execute(mainCtx, &executeConfig{
157176
statusChan: statusChan,
158177
sweeper: s.sweeper,
178+
batcher: s.batcher,
159179
blockEpochChan: queue.ChanOut(),
160180
timerFactory: s.executorConfig.createExpiryTimer,
161181
loopOutMaxParts: s.executorConfig.loopOutMaxParts,
@@ -211,6 +231,9 @@ func (s *executor) run(mainCtx context.Context,
211231
case err := <-blockErrorChan:
212232
return fmt.Errorf("block error: %v", err)
213233

234+
case err := <-batcherErrChan:
235+
return fmt.Errorf("batcher error: %v", err)
236+
214237
case <-mainCtx.Done():
215238
return mainCtx.Err()
216239
}

loopd/daemon.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"github.com/lightninglabs/loop"
1919
"github.com/lightninglabs/loop/loopd/perms"
2020
"github.com/lightninglabs/loop/loopdb"
21+
"github.com/lightninglabs/loop/sweepbatcher"
2122

2223
"github.com/lightninglabs/loop/instantout/reservation"
2324
loop_looprpc "github.com/lightninglabs/loop/looprpc"
@@ -412,9 +413,11 @@ func (d *Daemon) initialize(withMacaroonService bool) error {
412413
return err
413414
}
414415

416+
sweeperDb := sweepbatcher.NewSQLStore(baseDb, chainParams)
417+
415418
// Create an instance of the loop client library.
416419
swapClient, clientCleanup, err := getClient(
417-
d.cfg, swapDb, &d.lnd.LndServices,
420+
d.cfg, swapDb, sweeperDb, &d.lnd.LndServices,
418421
)
419422
if err != nil {
420423
return err

loopd/utils.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@ import (
1111
"github.com/lightninglabs/loop/liquidity"
1212
"github.com/lightninglabs/loop/loopdb"
1313
"github.com/lightninglabs/loop/swap"
14+
"github.com/lightninglabs/loop/sweepbatcher"
1415
"github.com/lightningnetwork/lnd/clock"
1516
"github.com/lightningnetwork/lnd/ticker"
1617
)
1718

1819
// getClient returns an instance of the swap client.
1920
func getClient(cfg *Config, swapDb loopdb.SwapStore,
20-
lnd *lndclient.LndServices) (*loop.Client, func(), error) {
21+
sweeperDb sweepbatcher.BatcherStore, lnd *lndclient.LndServices) (
22+
*loop.Client, func(), error) {
2123

2224
clientConfig := &loop.ClientConfig{
2325
ServerAddress: cfg.Server.Host,
@@ -33,7 +35,7 @@ func getClient(cfg *Config, swapDb loopdb.SwapStore,
3335
}
3436

3537
swapClient, cleanUp, err := loop.NewClient(
36-
cfg.DataDir, swapDb, clientConfig,
38+
cfg.DataDir, swapDb, sweeperDb, clientConfig,
3739
)
3840
if err != nil {
3941
return nil, nil, err

loopd/view.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/lightninglabs/lndclient"
99
"github.com/lightninglabs/loop"
1010
"github.com/lightninglabs/loop/loopdb"
11+
"github.com/lightninglabs/loop/sweepbatcher"
1112
"github.com/lightninglabs/loop/utils"
1213
)
1314

@@ -26,12 +27,16 @@ func view(config *Config, lisCfg *ListenerCfg) error {
2627
return err
2728
}
2829

29-
swapDb, _, err := openDatabase(config, chainParams)
30+
swapDb, baseDb, err := openDatabase(config, chainParams)
3031
if err != nil {
3132
return err
3233
}
3334

34-
swapClient, cleanup, err := getClient(config, swapDb, &lnd.LndServices)
35+
sweeperDb := sweepbatcher.NewSQLStore(baseDb, chainParams)
36+
37+
swapClient, cleanup, err := getClient(
38+
config, swapDb, sweeperDb, &lnd.LndServices,
39+
)
3540
if err != nil {
3641
return err
3742
}

0 commit comments

Comments
 (0)