-
Notifications
You must be signed in to change notification settings - Fork 127
Expand file tree
/
Copy pathmanager.go
More file actions
1053 lines (880 loc) · 29.7 KB
/
manager.go
File metadata and controls
1053 lines (880 loc) · 29.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package loopin
import (
"bytes"
"context"
"fmt"
"slices"
"sort"
"sync/atomic"
"time"
"github.com/btcsuite/btcd/btcec/v2/schnorr/musig2"
"github.com/btcsuite/btcd/btcutil"
"github.com/btcsuite/btcd/btcutil/psbt"
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/txscript"
"github.com/btcsuite/btcd/wire"
"github.com/lightninglabs/lndclient"
"github.com/lightninglabs/loop"
"github.com/lightninglabs/loop/fsm"
"github.com/lightninglabs/loop/labels"
"github.com/lightninglabs/loop/staticaddr/address"
"github.com/lightninglabs/loop/staticaddr/deposit"
"github.com/lightninglabs/loop/staticaddr/staticutil"
"github.com/lightninglabs/loop/swapserverrpc"
"github.com/lightningnetwork/lnd/input"
"github.com/lightningnetwork/lnd/lntypes"
"github.com/lightningnetwork/lnd/lnwallet"
"github.com/lightningnetwork/lnd/routing/route"
)
const (
// SwapNotFinishedMsg is the message that is sent to the server if a
// swap is not considered finished yet.
SwapNotFinishedMsg = "swap not finished yet"
)
var (
dustLimit = lnwallet.DustLimitForSize(input.P2TRSize)
)
// Config contains the services required for the loop-in manager.
type Config struct {
// Server is the client that is used to communicate with the static
// address server.
Server swapserverrpc.StaticAddressServerClient
// AddressManager gives the withdrawal manager access to static address
// parameters.
AddressManager AddressManager
// DepositManager gives the withdrawal manager access to the deposits
// enabling it to create and manage loop-ins.
DepositManager DepositManager
// LndClient is used to add invoices and select hop hints.
LndClient lndclient.LightningClient
// InvoicesClient is used to subscribe to invoice settlements and
// cancel invoices.
InvoicesClient lndclient.InvoicesClient
// QuoteGetter is used to get loop-in quotes.
QuoteGetter QuoteGetter
// NodePubkey is used to get a loop-in quote.
NodePubkey route.Vertex
// WalletKit is the wallet client that is used to derive new keys from
// lnd's wallet.
WalletKit lndclient.WalletKitClient
// ChainParams is the chain configuration(mainnet, testnet...) this
// manager uses.
ChainParams *chaincfg.Params
// ChainNotifier is the chain notifier that is used to listen for new
// blocks.
ChainNotifier lndclient.ChainNotifierClient
// Signer is the signer client that is used to sign transactions.
Signer lndclient.SignerClient
// Store is the database store that is used to store static address
// loop-in related records.
Store StaticAddressLoopInStore
// NotificationManager is the manager that handles the notification
// subscriptions.
NotificationManager NotificationManager
// ValidateLoopInContract validates the contract parameters against our
// request.
ValidateLoopInContract ValidateLoopInContract
// MaxStaticAddrHtlcFeePercentage is the percentage of the swap amount
// that we allow the server to charge for the htlc transaction.
// Although highly unlikely, this is a defense against the server
// publishing the htlc without paying the swap invoice, forcing us to
// sweep the timeout path.
MaxStaticAddrHtlcFeePercentage float64
// MaxStaticAddrHtlcBackupFeePercentage is the percentage of the swap
// amount that we allow the server to charge for the htlc backup
// transactions. This is a defense against the server publishing the
// htlc backup without paying the swap invoice, forcing us to sweep the
// timeout path. This value is elevated compared to
// MaxStaticAddrHtlcFeePercentage since it serves the server as backup
// transaction in case of fee spikes.
MaxStaticAddrHtlcBackupFeePercentage float64
}
// newSwapRequest is used to send a loop-in request to the manager main loop.
type newSwapRequest struct {
loopInRequest *loop.StaticAddressLoopInRequest
respChan chan *newSwapResponse
}
// newSwapResponse is used to return the loop-in swap and error to the server.
type newSwapResponse struct {
loopIn *StaticAddressLoopIn
err error
}
// Manager manages the address state machines.
type Manager struct {
cfg *Config
// newLoopInChan receives swap requests from the server and initiates
// loop-in swaps.
newLoopInChan chan *newSwapRequest
// exitChan signals the manager's subroutines that the main looop ctx
// has been canceled.
exitChan chan struct{}
// currentHeight stores the currently best known block height.
currentHeight atomic.Uint32
}
// NewManager creates a new deposit withdrawal manager.
func NewManager(cfg *Config, currentHeight uint32) (*Manager, error) {
if currentHeight == 0 {
return nil, fmt.Errorf("invalid current height %d",
currentHeight)
}
m := &Manager{
cfg: cfg,
newLoopInChan: make(chan *newSwapRequest),
exitChan: make(chan struct{}),
}
m.currentHeight.Store(currentHeight)
return m, nil
}
// Run runs the static address loop-in manager.
func (m *Manager) Run(ctx context.Context, initChan chan struct{}) error {
registerBlockNtfn := m.cfg.ChainNotifier.RegisterBlockEpochNtfn
newBlockChan, newBlockErrChan, err := registerBlockNtfn(ctx)
if err != nil {
log.Errorf("unable to register for block notifications: %v",
err)
return err
}
// Upon start of the loop-in manager we reinstate all previous loop-ins
// that are not yet completed.
err = m.recoverLoopIns(ctx)
if err != nil {
log.Errorf("unable to recover loop-ins: %v", err)
return err
}
// Register for notifications of loop-in sweep requests.
sweepReqs := m.cfg.NotificationManager.
SubscribeStaticLoopInSweepRequests(ctx)
// Communicate to the caller that the address manager has completed its
// initialization.
close(initChan)
var loopIn *StaticAddressLoopIn
for {
select {
case height := <-newBlockChan:
m.currentHeight.Store(uint32(height))
case err = <-newBlockErrChan:
return err
case request := <-m.newLoopInChan:
loopIn, err = m.initiateLoopIn(
ctx, request.loopInRequest,
)
if err != nil {
log.Errorf("Error initiating loop-in swap: %v",
err)
}
// We forward the initialized loop-in and error to
// DeliverLoopInRequest.
resp := &newSwapResponse{
loopIn: loopIn,
err: err,
}
select {
case request.respChan <- resp:
case <-ctx.Done():
// Notify subroutines that the main loop has
// been canceled.
close(m.exitChan)
return ctx.Err()
}
case sweepReq, ok := <-sweepReqs:
if !ok {
// The channel has been closed, we'll stop the
// loop-in manager.
log.Debugf("Stopping loop-in manager " +
"(ntfnChan closed)")
close(m.exitChan)
return fmt.Errorf("ntfnChan closed")
}
err = m.handleLoopInSweepReq(ctx, sweepReq)
if err != nil {
log.Errorf("Error handling loop-in sweep "+
"request: %v", err)
}
case <-ctx.Done():
return ctx.Err()
}
}
}
// notifyNotFinished notifies the server that a swap is not finished by
// sending the defined error message.
func (m *Manager) notifyNotFinished(ctx context.Context, swapHash lntypes.Hash,
txId chainhash.Hash) error {
_, err := m.cfg.Server.PushStaticAddressSweeplessSigs(
ctx, &swapserverrpc.PushStaticAddressSweeplessSigsRequest{
SwapHash: swapHash[:],
Txid: txId[:],
ErrorMessage: SwapNotFinishedMsg,
})
return err
}
// handleLoopInSweepReq handles a loop-in sweep request from the server.
// It first checks if the requested loop-in is finished as expected and if
// yes will send signature to the server for the provided psbt.
func (m *Manager) handleLoopInSweepReq(ctx context.Context,
req *swapserverrpc.ServerStaticLoopInSweepNotification) error {
// First we'll check if the loop-ins are known to us and in
// the expected state.
swapHash, err := lntypes.MakeHash(req.SwapHash)
if err != nil {
return err
}
// Fetch the loop-in from the store.
loopIn, err := m.cfg.Store.GetLoopInByHash(ctx, swapHash)
if err != nil {
return err
}
loopIn.AddressParams, err =
m.cfg.AddressManager.GetStaticAddressParameters(ctx)
if err != nil {
return err
}
loopIn.Address, err = m.cfg.AddressManager.GetStaticAddress(ctx)
if err != nil {
return err
}
ignoreUnknownOutpoints := false
deposits, err := m.cfg.DepositManager.DepositsForOutpoints(
ctx, loopIn.DepositOutpoints, ignoreUnknownOutpoints,
)
if err != nil {
return err
}
loopIn.Deposits = deposits
reader := bytes.NewReader(req.SweepTxPsbt)
sweepPacket, err := psbt.NewFromRawBytes(reader, false)
if err != nil {
return err
}
sweepTx := sweepPacket.UnsignedTx
// If the loop-in is not in the Succeeded state we return an
// error.
if !loopIn.IsInState(Succeeded) {
// We'll notify the server that we don't consider the swap
// finished yet, so it can retry later.
_ = m.notifyNotFinished(ctx, swapHash, sweepTx.TxHash())
return fmt.Errorf("loop-in %v not in Succeeded state",
swapHash)
}
// Perform a sanity check on the number of unsigned tx inputs and
// prevout info.
if len(sweepTx.TxIn) != len(req.PrevoutInfo) {
return fmt.Errorf("expected %v inputs, got %v",
len(req.PrevoutInfo), len(sweepTx.TxIn))
}
// If the user selected an amount that is less than the total deposit
// amount we'll check that the server sends us the correct change amount
// back to our static address.
err = m.checkChange(ctx, sweepTx, loopIn.AddressParams)
if err != nil {
return err
}
// Check if all the deposits requested are part of the loop-in and
// find them in the requested sweep.
depositToIdxMap, err := mapDepositsToIndices(req, loopIn, sweepTx)
if err != nil {
return err
}
prevoutMap := make(map[wire.OutPoint]*wire.TxOut, len(req.PrevoutInfo))
// Set all the prevouts in the prevout map.
for _, prevout := range req.PrevoutInfo {
txid, err := chainhash.NewHash(prevout.TxidBytes)
if err != nil {
return err
}
prevoutMap[wire.OutPoint{
Hash: *txid,
Index: prevout.OutputIndex,
}] = &wire.TxOut{
Value: int64(prevout.Value),
PkScript: prevout.PkScript,
}
}
prevOutputFetcher := txscript.NewMultiPrevOutFetcher(
prevoutMap,
)
sigHashes := txscript.NewTxSigHashes(
sweepPacket.UnsignedTx, prevOutputFetcher,
)
// We'll now sign for every deposit that is part of the loop-in.
responseMap := make(
map[string]*swapserverrpc.ClientSweeplessSigningInfo,
len(req.DepositToNonces),
)
for depositOutpoint, nonce := range req.DepositToNonces {
taprootSigHash, err := txscript.CalcTaprootSignatureHash(
sigHashes, txscript.SigHashDefault,
sweepPacket.UnsignedTx,
depositToIdxMap[depositOutpoint], prevOutputFetcher,
)
if err != nil {
return err
}
var (
serverNonce [musig2.PubNonceSize]byte
sigHash [32]byte
)
copy(serverNonce[:], nonce)
musig2Session, err := staticutil.CreateMusig2Session(
ctx, m.cfg.Signer, loopIn.AddressParams, loopIn.Address,
)
if err != nil {
return err
}
// We'll clean up the session if we don't get to signing.
defer func() {
err = m.cfg.Signer.MuSig2Cleanup(
context.WithoutCancel(ctx),
musig2Session.SessionID,
)
if err != nil {
log.Errorf("Error cleaning up musig2 session: "+
" %v", err)
}
}()
haveAllNonces, err := m.cfg.Signer.MuSig2RegisterNonces(
ctx, musig2Session.SessionID,
[][musig2.PubNonceSize]byte{serverNonce},
)
if err != nil {
return err
}
if !haveAllNonces {
return fmt.Errorf("expected all nonces to be " +
"registered")
}
copy(sigHash[:], taprootSigHash)
// Since our MuSig2 session has all nonces, we can now create
// the local partial signature by signing the sig hash.
sig, err := m.cfg.Signer.MuSig2Sign(
ctx, musig2Session.SessionID, sigHash, false,
)
if err != nil {
return err
}
signingInfo := &swapserverrpc.ClientSweeplessSigningInfo{
Nonce: musig2Session.PublicNonce[:],
Sig: sig,
}
responseMap[depositOutpoint] = signingInfo
}
txHash := sweepTx.TxHash()
_, err = m.cfg.Server.PushStaticAddressSweeplessSigs(
ctx, &swapserverrpc.PushStaticAddressSweeplessSigsRequest{
SwapHash: loopIn.SwapHash[:],
Txid: txHash[:],
SigningInfo: responseMap,
},
)
return err
}
// checkChange ensures that the server sends us the correct change amount
// back to our static address. An edge case arises if a batch contains two
// swaps with identical change outputs. The client needs to ensure that any
// swap referenced by the inputs has a respective change output in the batch.
func (m *Manager) checkChange(ctx context.Context,
sweepTx *wire.MsgTx, changeAddr *address.Parameters) error {
prevOuts := make([]string, len(sweepTx.TxIn))
for i, in := range sweepTx.TxIn {
prevOuts[i] = in.PreviousOutPoint.String()
}
ignoreUnknownOutpoints := true
deposits, err := m.cfg.DepositManager.DepositsForOutpoints(
ctx, prevOuts, ignoreUnknownOutpoints,
)
if err != nil {
return err
}
depositIDs := make([]deposit.ID, len(deposits))
for i, d := range deposits {
depositIDs[i] = d.ID
}
swapHashes, err := m.cfg.Store.SwapHashesForDepositIDs(ctx, depositIDs)
if err != nil {
return err
}
var expectedChange btcutil.Amount
for swapHash := range swapHashes {
loopIn, err := m.cfg.Store.GetLoopInByHash(ctx, swapHash)
if err != nil {
return err
}
totalDepositAmount := loopIn.TotalDepositAmount()
changeAmt := totalDepositAmount - loopIn.SelectedAmount
if changeAmt > 0 && changeAmt < totalDepositAmount {
log.Debugf("expected change output to our "+
"static address, total_deposit_amount=%v, "+
"selected_amount=%v, "+
"expected_change_amount=%v ",
totalDepositAmount, loopIn.SelectedAmount,
changeAmt)
expectedChange += changeAmt
}
}
if expectedChange == 0 {
return nil
}
for _, out := range sweepTx.TxOut {
if out.Value == int64(expectedChange) &&
bytes.Equal(out.PkScript, changeAddr.PkScript) {
// We found the expected change output.
return nil
}
}
return fmt.Errorf("couldn't find expected change of %v "+
"satoshis sent to our static address", expectedChange)
}
// recover stars a loop-in state machine for each non-final loop-in to pick up
// work where it was left off before the restart.
func (m *Manager) recoverLoopIns(ctx context.Context) error {
log.Infof("Recovering static address loop-ins...")
// Recover loop-ins.
// Recover pending static address loop-ins.
pendingLoopIns, err := m.cfg.Store.GetStaticAddressLoopInSwapsByStates(
ctx, PendingStates,
)
if err != nil {
return err
}
for _, loopIn := range pendingLoopIns {
log.Debugf("Recovering loopIn %x", loopIn.SwapHash[:])
// Retrieve all deposits regardless of deposit state. If any of
// the deposits is not active in the in-mem map of the deposits
// manager we log it, but continue to recover the loop-in.
var allActive bool
loopIn.Deposits, allActive =
m.cfg.DepositManager.AllStringOutpointsActiveDeposits(
loopIn.DepositOutpoints, fsm.EmptyState,
)
if !allActive {
log.Errorf("one or more deposits are not active")
}
loopIn.AddressParams, err =
m.cfg.AddressManager.GetStaticAddressParameters(ctx)
if err != nil {
return err
}
loopIn.Address, err = m.cfg.AddressManager.GetStaticAddress(
ctx,
)
if err != nil {
return err
}
// Create a state machine for a given loop-in.
recovery := true
fsm, err := NewFSM(ctx, loopIn, m.cfg, recovery)
if err != nil {
return err
}
// Send the OnRecover event to the state machine.
go func() {
err := fsm.SendEvent(ctx, OnRecover, nil)
if err != nil {
log.Errorf("Error sending OnRecover "+
"event: %v", err)
}
}()
}
return nil
}
// DeliverLoopInRequest forwards a loop-in request from the server to the
// manager run loop to initiate a new loop-in swap.
func (m *Manager) DeliverLoopInRequest(ctx context.Context,
req *loop.StaticAddressLoopInRequest) (*StaticAddressLoopIn, error) {
request := &newSwapRequest{
loopInRequest: req,
respChan: make(chan *newSwapResponse),
}
// Send the new loop-in request to the manager run loop.
select {
case m.newLoopInChan <- request:
case <-m.exitChan:
return nil, fmt.Errorf("loop-in manager has been canceled")
case <-ctx.Done():
return nil, fmt.Errorf("context canceled while initiating " +
"a loop-in swap")
}
// Wait for the response from the manager run loop.
select {
case resp := <-request.respChan:
return resp.loopIn, resp.err
case <-m.exitChan:
return nil, fmt.Errorf("loop-in manager has been canceled")
case <-ctx.Done():
return nil, fmt.Errorf("context canceled while waiting for " +
"loop-in swap response")
}
}
// initiateLoopIn initiates a loop-in swap. It passes the request to the server
// along with all relevant loop-in information.
func (m *Manager) initiateLoopIn(ctx context.Context,
req *loop.StaticAddressLoopInRequest) (*StaticAddressLoopIn, error) {
var (
err error
selectedOutpoints = req.DepositOutpoints
selectedDeposits []*deposit.Deposit
)
// Determine which deposits to use for the loop-in swap. If none are
// selected by the client, we will coin-select them based on the amount.
switch {
case len(selectedOutpoints) == 0 && req.SelectedAmount == 0:
return nil, fmt.Errorf("neither deposit outpoints nor amount " +
"provided")
case len(selectedOutpoints) > 0:
// Retrieve all deposits referenced by the outpoints and ensure
// that they are in state Deposited.
var active bool
selectedDeposits, active = m.cfg.DepositManager.
AllStringOutpointsActiveDeposits(
selectedOutpoints, deposit.Deposited,
)
if !active {
return nil, fmt.Errorf("one or more deposits are not in "+
"state %s", deposit.Deposited)
}
case len(selectedOutpoints) == 0:
// If an amount was provided, we'll coin-select deposits to
// cover for the amount.
allDeposits, err := m.cfg.DepositManager.
GetActiveDepositsInState(deposit.Deposited)
if err != nil {
return nil, fmt.Errorf("unable to retrieve all "+
"deposits: %w", err)
}
// TODO(hieblmi): add params to deposit for multi-address
// support.
params, err := m.cfg.AddressManager.GetStaticAddressParameters(
ctx,
)
if err != nil {
return nil, fmt.Errorf("unable to retrieve static "+
"address parameters: %w", err)
}
selectedDeposits, err = SelectDeposits(
req.SelectedAmount, allDeposits, params.Expiry,
m.currentHeight.Load(),
)
if err != nil {
return nil, fmt.Errorf("unable to select deposits: %w",
err)
}
selectedOutpoints = make([]string, 0, len(selectedDeposits))
for _, deposit := range selectedDeposits {
selectedOutpoints = append(selectedOutpoints,
deposit.String())
}
}
// Calculate the total deposit amount and check if the selected amount
// would leave a dust output.
swapAmount, err := DeduceSwapAmount(
sumOfDeposits(selectedDeposits), req.SelectedAmount,
)
if err != nil {
return nil, fmt.Errorf("unable to determine swap amount: %w",
err)
}
// Check that the label is valid.
err = labels.Validate(req.Label)
if err != nil {
return nil, fmt.Errorf("invalid label: %w", err)
}
// Private and route hints are mutually exclusive as setting private
// means we retrieve our own route hints from the connected node.
if len(req.RouteHints) != 0 && req.Private {
return nil, fmt.Errorf("private and route hints are mutually " +
"exclusive")
}
// If private is set, we generate route hints.
if req.Private {
// If last_hop is set, we'll only add channels with peers set to
// the last_hop parameter.
includeNodes := make(map[route.Vertex]struct{})
if req.LastHop != nil {
includeNodes[*req.LastHop] = struct{}{}
}
// Because the Private flag is set, we'll generate our own set
// of hop hints.
req.RouteHints, err = loop.SelectHopHints(
ctx, m.cfg.LndClient, swapAmount,
loop.DefaultMaxHopHints, includeNodes,
)
if err != nil {
return nil, fmt.Errorf("unable to generate hop "+
"hints: %w", err)
}
}
// Request the current server loop in terms and use these to calculate
// the swap fee that we should subtract from the swap amount in the
// payment request that we send to the server. We pass nil as optional
// route hints as hop hint selection when generating invoices with
// private channels is an LND side black box feature. Advanced users
// will quote directly anyway, and there they are able to add specific
// route hints.
// The quote call will also request a probe from the server to ensure
// feasibility of a loop-in for the selected.
numDeposits := uint32(len(selectedDeposits))
quote, err := m.cfg.QuoteGetter.GetLoopInQuote(
ctx, swapAmount, m.cfg.NodePubkey, req.LastHop, req.RouteHints,
req.Initiator, numDeposits, req.Fast,
)
if err != nil {
return nil, fmt.Errorf("unable to get loop in quote: %w", err)
}
// If the previously accepted quote fee is lower than what is quoted, we
// abort the swap.
if quote.SwapFee > req.MaxSwapFee {
log.Warnf("Swap fee %v exceeding maximum of %v",
quote.SwapFee, req.MaxSwapFee)
return nil, loop.ErrSwapFeeTooHigh
}
paymentTimeoutSeconds := uint32(DefaultPaymentTimeoutSeconds)
if req.PaymentTimeoutSeconds != 0 {
paymentTimeoutSeconds = req.PaymentTimeoutSeconds
}
swap := &StaticAddressLoopIn{
SelectedAmount: req.SelectedAmount,
DepositOutpoints: selectedOutpoints,
Deposits: selectedDeposits,
Label: req.Label,
Initiator: req.Initiator,
InitiationTime: time.Now(),
RouteHints: req.RouteHints,
QuotedSwapFee: quote.SwapFee,
MaxSwapFee: req.MaxSwapFee,
PaymentTimeoutSeconds: paymentTimeoutSeconds,
Fast: req.Fast,
}
if req.LastHop != nil {
swap.LastHop = req.LastHop[:]
}
swap.InitiationHeight = m.currentHeight.Load()
return m.startLoopInFsm(ctx, swap)
}
// startLoopInFsm initiates a loop-in state machine based on the user-provided
// swap information, sends that info to the server and waits for the server to
// return htlc signature information. It then creates the loop-in object in the
// database.
func (m *Manager) startLoopInFsm(ctx context.Context,
loopIn *StaticAddressLoopIn) (*StaticAddressLoopIn, error) {
// Create a state machine for a given deposit.
recovery := false
loopInFsm, err := NewFSM(ctx, loopIn, m.cfg, recovery)
if err != nil {
return nil, err
}
// Send the start event to the state machine.
go func() {
err := loopInFsm.SendEvent(ctx, OnInitHtlc, nil)
if err != nil {
log.Errorf("Error sending OnInitHtlc event: %v", err)
}
}()
// If an error occurs before SignHtlcTx is reached we consider the swap
// failed and abort early.
err = loopInFsm.DefaultObserver.WaitForState(
ctx, time.Minute, SignHtlcTx,
fsm.WithAbortEarlyOnErrorOption(),
)
if err != nil {
return nil, err
}
return loopIn, nil
}
// GetAllSwaps returns all static address loop-in swaps from the database store.
func (m *Manager) GetAllSwaps(ctx context.Context) ([]*StaticAddressLoopIn,
error) {
swaps, err := m.cfg.Store.GetStaticAddressLoopInSwapsByStates(
ctx, AllStates,
)
if err != nil {
return nil, err
}
allDeposits, err := m.cfg.DepositManager.GetAllDeposits(ctx)
if err != nil {
return nil, err
}
var depositLookup = make(map[string]*deposit.Deposit)
for i, d := range allDeposits {
depositLookup[d.OutPoint.String()] = allDeposits[i]
}
for i, s := range swaps {
var deposits []*deposit.Deposit
for _, outpoint := range s.DepositOutpoints {
if d, ok := depositLookup[outpoint]; ok {
deposits = append(deposits, d)
}
}
swaps[i].Deposits = deposits
}
return swaps, nil
}
// SelectDeposits sorts the deposits to optimize for successful swaps with
// dynamic confirmation requirements: 1) more confirmations first (higher chance
// of server acceptance), 2) larger amounts first (to minimize number of deposits
// used), 3) expiring sooner first (prioritize time-sensitive deposits).
// Deposits that are too close to expiry are filtered out before sorting.
// It then selects the deposits that are needed to cover the amount requested
// without leaving a dust change. It returns an error if the sum of deposits
// minus dust is less than the requested amount.
func SelectDeposits(targetAmount btcutil.Amount,
unfilteredDeposits []*deposit.Deposit, csvExpiry uint32,
blockHeight uint32) ([]*deposit.Deposit, error) {
// Filter out deposits that are too close to expiry to be swapped.
var deposits []*deposit.Deposit
for _, d := range unfilteredDeposits {
if !IsSwappable(
uint32(d.ConfirmationHeight), blockHeight, csvExpiry,
) {
log.Debugf("Skipping deposit %s as it expires before "+
"the htlc", d.OutPoint.String())
continue
}
deposits = append(deposits, d)
}
// Sort deposits to optimize for successful swaps with dynamic
// confirmation requirements:
// 1. More confirmations first (higher chance of server acceptance).
// 2. Larger amounts first (to minimize number of deposits used).
// 3. Expiring sooner first (prioritize time-sensitive deposits).
sort.Slice(deposits, func(i, j int) bool {
// Primary: more confirmations first. Guard against the
// theoretical case where ConfirmationHeight > blockHeight
// (e.g. during a transient reorg inconsistency).
var iConfs, jConfs uint32
if blockHeight > uint32(deposits[i].ConfirmationHeight) {
iConfs = blockHeight -
uint32(deposits[i].ConfirmationHeight)
}
if blockHeight > uint32(deposits[j].ConfirmationHeight) {
jConfs = blockHeight -
uint32(deposits[j].ConfirmationHeight)
}
if iConfs != jConfs {
return iConfs > jConfs
}
// Secondary: larger amounts first.
if deposits[i].Value != deposits[j].Value {
return deposits[i].Value > deposits[j].Value
}
// Tertiary: expiring sooner first.
iExp := uint32(deposits[i].ConfirmationHeight) +
csvExpiry - blockHeight
jExp := uint32(deposits[j].ConfirmationHeight) +
csvExpiry - blockHeight
return iExp < jExp
})
// Select the deposits that are needed to cover the swap amount without
// leaving a dust change.
var selectedDeposits []*deposit.Deposit
var selectedAmount btcutil.Amount
for _, deposit := range deposits {
selectedDeposits = append(selectedDeposits, deposit)
selectedAmount += deposit.Value
if selectedAmount == targetAmount {
return selectedDeposits, nil
}
if selectedAmount > targetAmount {
if selectedAmount-targetAmount >= dustLimit {
return selectedDeposits, nil
}
}
}
return nil, fmt.Errorf("not enough deposits to cover "+
"requested amount or prevent dust change, have %d but need %d",
selectedAmount, targetAmount)
}
// IsSwappable checks if a deposit is swappable. It returns true if the deposit
// is not expired and the htlc is not too close to expiry.
func IsSwappable(confirmationHeight, blockHeight, csvExpiry uint32) bool {
// The deposit expiry height is the confirmation height plus the csv
// expiry.
depositExpiryHeight := confirmationHeight + csvExpiry
// The htlc expiry height is the current height plus the htlc
// cltv delta.
htlcExpiryHeight := blockHeight + DefaultLoopInOnChainCltvDelta
// Ensure that the deposit doesn't expire before the htlc.
if depositExpiryHeight < htlcExpiryHeight+DepositHtlcDelta {
return false
}
return true
}
// DeduceSwapAmount calculates the swap amount based on the selected amount and
// the total deposit amount. It checks if the selected amount leaves a dust
// change output or exceeds the total deposits value. Note that if the selected
// amount is 0, the swap amount is the total deposit value. If the selected
// amount is equal to the total deposit value, the total deposit value will be
// swapped.
func DeduceSwapAmount(totalDepositAmount btcutil.Amount,
selectedAmount btcutil.Amount) (btcutil.Amount, error) {
// If the selected amount leaves a dust change output or exceeds the
// total deposits value, we return an error.
swapAmount := selectedAmount
remainingAmount := totalDepositAmount - selectedAmount
switch {
case selectedAmount < 0:
return 0, fmt.Errorf("selected amount %v is negative",
selectedAmount)
case selectedAmount > 0 && selectedAmount < dustLimit:
return 0, fmt.Errorf("selected amount %v is dust, "+
"need at least %v", selectedAmount, dustLimit)
case totalDepositAmount < dustLimit:
return 0, fmt.Errorf("total deposit value %v is dust, "+
"need at least %v", totalDepositAmount, dustLimit)
case remainingAmount < 0:
return 0, fmt.Errorf("selected amount %v exceeds total "+
"deposit value %v", selectedAmount, totalDepositAmount)
case remainingAmount > 0 && remainingAmount < dustLimit:
return 0, fmt.Errorf("selected amount %v leaves dust change "+
"%v", selectedAmount, remainingAmount)
default:
// If the remaining amount is 0 or equal or greater than the
// dust limit, we can proceed with the swap.
}
// If the client didn't select an amount, we quote for the total
// deposits value.
if selectedAmount == 0 {
swapAmount = totalDepositAmount