Skip to content

Commit 3d1d3eb

Browse files
authored
Merge pull request #766 from starius/sweepbatcher-sweep-fetcher
sweepbatcher: factor out loopdb-less version
2 parents 295198a + 0c2ba74 commit 3d1d3eb

File tree

5 files changed

+304
-83
lines changed

5 files changed

+304
-83
lines changed

client.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,10 +177,18 @@ func NewClient(dbDir string, loopDB loopdb.SwapStore,
177177
return nil
178178
}
179179

180+
sweepStore, err := sweepbatcher.NewSweepFetcherFromSwapStore(
181+
loopDB, cfg.Lnd.ChainParams,
182+
)
183+
if err != nil {
184+
return nil, nil, fmt.Errorf("sweepbatcher."+
185+
"NewSweepFetcherFromSwapStore failed: %w", err)
186+
}
187+
180188
batcher := sweepbatcher.NewBatcher(
181189
cfg.Lnd.WalletKit, cfg.Lnd.ChainNotifier, cfg.Lnd.Signer,
182190
swapServerClient.MultiMuSig2SignSweep, verifySchnorrSig,
183-
cfg.Lnd.ChainParams, sweeperDb, loopDB,
191+
cfg.Lnd.ChainParams, sweeperDb, sweepStore,
184192
)
185193

186194
executor := newExecutor(&executorConfig{

loopout_test.go

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -299,10 +299,15 @@ func testCustomSweepConfTarget(t *testing.T) {
299299

300300
batcherStore := sweepbatcher.NewStoreMock()
301301

302+
sweepStore, err := sweepbatcher.NewSweepFetcherFromSwapStore(
303+
cfg.store, lnd.ChainParams,
304+
)
305+
require.NoError(t, err)
306+
302307
batcher := sweepbatcher.NewBatcher(
303308
lnd.WalletKit, lnd.ChainNotifier, lnd.Signer,
304309
mockMuSig2SignSweep, mockVerifySchnorrSigSuccess,
305-
lnd.ChainParams, batcherStore, cfg.store,
310+
lnd.ChainParams, batcherStore, sweepStore,
306311
)
307312

308313
tctx, cancel := context.WithCancel(context.Background())
@@ -532,10 +537,15 @@ func testPreimagePush(t *testing.T) {
532537

533538
batcherStore := sweepbatcher.NewStoreMock()
534539

540+
sweepStore, err := sweepbatcher.NewSweepFetcherFromSwapStore(
541+
cfg.store, lnd.ChainParams,
542+
)
543+
require.NoError(t, err)
544+
535545
batcher := sweepbatcher.NewBatcher(
536546
lnd.WalletKit, lnd.ChainNotifier, lnd.Signer,
537547
mockMuSig2SignSweep, mockVerifySchnorrSigSuccess,
538-
lnd.ChainParams, batcherStore, cfg.store,
548+
lnd.ChainParams, batcherStore, sweepStore,
539549
)
540550

541551
tctx, cancel := context.WithCancel(context.Background())
@@ -953,10 +963,15 @@ func TestLoopOutMuSig2Sweep(t *testing.T) {
953963

954964
batcherStore := sweepbatcher.NewStoreMock()
955965

966+
sweepStore, err := sweepbatcher.NewSweepFetcherFromSwapStore(
967+
cfg.store, lnd.ChainParams,
968+
)
969+
require.NoError(t, err)
970+
956971
batcher := sweepbatcher.NewBatcher(
957972
lnd.WalletKit, lnd.ChainNotifier, lnd.Signer,
958973
mockMuSig2SignSweep, mockVerifySchnorrSigSuccess,
959-
lnd.ChainParams, batcherStore, cfg.store,
974+
lnd.ChainParams, batcherStore, sweepStore,
960975
)
961976

962977
tctx, cancel := context.WithCancel(context.Background())

sweepbatcher/sweep_batcher.go

Lines changed: 134 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ import (
1313
"github.com/btcsuite/btcd/wire"
1414
"github.com/lightninglabs/lndclient"
1515
"github.com/lightninglabs/loop/loopdb"
16+
"github.com/lightninglabs/loop/swap"
1617
"github.com/lightninglabs/loop/utils"
18+
"github.com/lightningnetwork/lnd/input"
1719
"github.com/lightningnetwork/lnd/lntypes"
1820
"github.com/lightningnetwork/lnd/lnwallet/chainfee"
1921
)
@@ -82,12 +84,48 @@ type BatcherStore interface {
8284
TotalSweptAmount(ctx context.Context, id int32) (btcutil.Amount, error)
8385
}
8486

85-
// LoopOutFetcher is used to load LoopOut swaps from the database.
86-
// It is implemented by loopdb.SwapStore.
87-
type LoopOutFetcher interface {
88-
// FetchLoopOutSwap returns the loop out swap with the given hash.
89-
FetchLoopOutSwap(ctx context.Context,
90-
hash lntypes.Hash) (*loopdb.LoopOut, error)
87+
// SweepInfo stores any data related to sweeping a specific outpoint.
88+
type SweepInfo struct {
89+
// ConfTarget is the confirmation target of the sweep.
90+
ConfTarget int32
91+
92+
// Timeout is the timeout of the swap that the sweep belongs to.
93+
Timeout int32
94+
95+
// InitiationHeight is the height at which the swap was initiated.
96+
InitiationHeight int32
97+
98+
// HTLC is the HTLC that is being swept.
99+
HTLC swap.Htlc
100+
101+
// Preimage is the preimage of the HTLC that is being swept.
102+
Preimage lntypes.Preimage
103+
104+
// SwapInvoicePaymentAddr is the payment address of the swap invoice.
105+
SwapInvoicePaymentAddr [32]byte
106+
107+
// HTLCKeys is the set of keys used to sign the HTLC.
108+
HTLCKeys loopdb.HtlcKeys
109+
110+
// HTLCSuccessEstimator is a function that estimates the weight of the
111+
// HTLC success script.
112+
HTLCSuccessEstimator func(*input.TxWeightEstimator) error
113+
114+
// ProtocolVersion is the protocol version of the swap that the sweep
115+
// belongs to.
116+
ProtocolVersion loopdb.ProtocolVersion
117+
118+
// IsExternalAddr is true if the sweep spends to a non-wallet address.
119+
IsExternalAddr bool
120+
121+
// DestAddr is the destination address of the sweep.
122+
DestAddr btcutil.Address
123+
}
124+
125+
// SweepFetcher is used to get details of a sweep.
126+
type SweepFetcher interface {
127+
// FetchSweep returns details of the sweep with the given hash.
128+
FetchSweep(ctx context.Context, hash lntypes.Hash) (*SweepInfo, error)
91129
}
92130

93131
// MuSig2SignSweep is a function that can be used to sign a sweep transaction
@@ -191,8 +229,8 @@ type Batcher struct {
191229
// batcher and the batches.
192230
store BatcherStore
193231

194-
// swapStore is used to load LoopOut swaps from the database.
195-
swapStore LoopOutFetcher
232+
// sweepStore is used to load sweeps from the database.
233+
sweepStore SweepFetcher
196234

197235
// wg is a waitgroup that is used to wait for all the goroutines to
198236
// exit.
@@ -204,7 +242,7 @@ func NewBatcher(wallet lndclient.WalletKitClient,
204242
chainNotifier lndclient.ChainNotifierClient,
205243
signerClient lndclient.SignerClient, musig2ServerSigner MuSig2SignSweep,
206244
verifySchnorrSig VerifySchnorrSig, chainparams *chaincfg.Params,
207-
store BatcherStore, swapStore LoopOutFetcher) *Batcher {
245+
store BatcherStore, sweepStore SweepFetcher) *Batcher {
208246

209247
return &Batcher{
210248
batches: make(map[int32]*batch),
@@ -219,7 +257,7 @@ func NewBatcher(wallet lndclient.WalletKitClient,
219257
VerifySchnorrSig: verifySchnorrSig,
220258
chainParams: chainparams,
221259
store: store,
222-
swapStore: swapStore,
260+
sweepStore: sweepStore,
223261
}
224262
}
225263

@@ -661,87 +699,123 @@ func (b *Batcher) writeToErrChan(ctx context.Context, err error) error {
661699
func (b *Batcher) convertSweep(ctx context.Context, dbSweep *dbSweep) (
662700
*sweep, error) {
663701

664-
swap, err := b.swapStore.FetchLoopOutSwap(ctx, dbSweep.SwapHash)
702+
s, err := b.sweepStore.FetchSweep(ctx, dbSweep.SwapHash)
665703
if err != nil {
666-
return nil, fmt.Errorf("failed to fetch loop out for %x: %w",
704+
return nil, fmt.Errorf("failed to fetch sweep data for %x: %v",
667705
dbSweep.SwapHash[:6], err)
668706
}
669707

670-
htlc, err := utils.GetHtlc(
671-
dbSweep.SwapHash, &swap.Contract.SwapContract, b.chainParams,
672-
)
673-
if err != nil {
674-
return nil, err
675-
}
676-
677-
swapPaymentAddr, err := utils.ObtainSwapPaymentAddr(
678-
swap.Contract.SwapInvoice, b.chainParams,
679-
)
680-
if err != nil {
681-
return nil, err
682-
}
683-
684708
return &sweep{
685-
swapHash: swap.Hash,
709+
swapHash: dbSweep.SwapHash,
686710
outpoint: dbSweep.Outpoint,
687711
value: dbSweep.Amount,
688-
confTarget: swap.Contract.SweepConfTarget,
689-
timeout: swap.Contract.CltvExpiry,
690-
initiationHeight: swap.Contract.InitiationHeight,
691-
htlc: *htlc,
692-
preimage: swap.Contract.Preimage,
693-
swapInvoicePaymentAddr: *swapPaymentAddr,
694-
htlcKeys: swap.Contract.HtlcKeys,
695-
htlcSuccessEstimator: htlc.AddSuccessToEstimator,
696-
protocolVersion: swap.Contract.ProtocolVersion,
697-
isExternalAddr: swap.Contract.IsExternalAddr,
698-
destAddr: swap.Contract.DestAddr,
712+
confTarget: s.ConfTarget,
713+
timeout: s.Timeout,
714+
initiationHeight: s.InitiationHeight,
715+
htlc: s.HTLC,
716+
preimage: s.Preimage,
717+
swapInvoicePaymentAddr: s.SwapInvoicePaymentAddr,
718+
htlcKeys: s.HTLCKeys,
719+
htlcSuccessEstimator: s.HTLCSuccessEstimator,
720+
protocolVersion: s.ProtocolVersion,
721+
isExternalAddr: s.IsExternalAddr,
722+
destAddr: s.DestAddr,
699723
}, nil
700724
}
701725

702-
// fetchSweep fetches the sweep related information from the database.
703-
func (b *Batcher) fetchSweep(ctx context.Context,
704-
sweepReq SweepRequest) (*sweep, error) {
726+
// LoopOutFetcher is used to load LoopOut swaps from the database.
727+
// It is implemented by loopdb.SwapStore.
728+
type LoopOutFetcher interface {
729+
// FetchLoopOutSwap returns the loop out swap with the given hash.
730+
FetchLoopOutSwap(ctx context.Context,
731+
hash lntypes.Hash) (*loopdb.LoopOut, error)
732+
}
705733

706-
swapHash, err := lntypes.MakeHash(sweepReq.SwapHash[:])
707-
if err != nil {
708-
return nil, fmt.Errorf("failed to parse swapHash: %v", err)
709-
}
734+
// SwapStoreWrapper is LoopOutFetcher wrapper providing SweepFetcher interface.
735+
type SwapStoreWrapper struct {
736+
// swapStore is used to load LoopOut swaps from the database.
737+
swapStore LoopOutFetcher
710738

711-
swap, err := b.swapStore.FetchLoopOutSwap(ctx, swapHash)
739+
// chainParams are the chain parameters of the chain that is used by
740+
// batches.
741+
chainParams *chaincfg.Params
742+
}
743+
744+
// FetchSweep returns details of the sweep with the given hash.
745+
// Implements SweepFetcher interface.
746+
func (f *SwapStoreWrapper) FetchSweep(ctx context.Context,
747+
swapHash lntypes.Hash) (*SweepInfo, error) {
748+
749+
swap, err := f.swapStore.FetchLoopOutSwap(ctx, swapHash)
712750
if err != nil {
713751
return nil, fmt.Errorf("failed to fetch loop out for %x: %v",
714752
swapHash[:6], err)
715753
}
716754

717755
htlc, err := utils.GetHtlc(
718-
swapHash, &swap.Contract.SwapContract, b.chainParams,
756+
swapHash, &swap.Contract.SwapContract, f.chainParams,
719757
)
720758
if err != nil {
721759
return nil, fmt.Errorf("failed to get htlc: %v", err)
722760
}
723761

724762
swapPaymentAddr, err := utils.ObtainSwapPaymentAddr(
725-
swap.Contract.SwapInvoice, b.chainParams,
763+
swap.Contract.SwapInvoice, f.chainParams,
726764
)
727765
if err != nil {
728766
return nil, fmt.Errorf("failed to get payment addr: %v", err)
729767
}
730768

769+
return &SweepInfo{
770+
ConfTarget: swap.Contract.SweepConfTarget,
771+
Timeout: swap.Contract.CltvExpiry,
772+
InitiationHeight: swap.Contract.InitiationHeight,
773+
HTLC: *htlc,
774+
Preimage: swap.Contract.Preimage,
775+
SwapInvoicePaymentAddr: *swapPaymentAddr,
776+
HTLCKeys: swap.Contract.HtlcKeys,
777+
HTLCSuccessEstimator: htlc.AddSuccessToEstimator,
778+
ProtocolVersion: swap.Contract.ProtocolVersion,
779+
IsExternalAddr: swap.Contract.IsExternalAddr,
780+
DestAddr: swap.Contract.DestAddr,
781+
}, nil
782+
}
783+
784+
// NewSweepFetcherFromSwapStore accepts swapStore (e.g. loopdb) and returns
785+
// a wrapper implementing SweepFetcher interface (suitable for NewBatcher).
786+
func NewSweepFetcherFromSwapStore(swapStore LoopOutFetcher,
787+
chainParams *chaincfg.Params) (*SwapStoreWrapper, error) {
788+
789+
return &SwapStoreWrapper{
790+
swapStore: swapStore,
791+
chainParams: chainParams,
792+
}, nil
793+
}
794+
795+
// fetchSweep fetches the sweep related information from the database.
796+
func (b *Batcher) fetchSweep(ctx context.Context,
797+
sweepReq SweepRequest) (*sweep, error) {
798+
799+
s, err := b.sweepStore.FetchSweep(ctx, sweepReq.SwapHash)
800+
if err != nil {
801+
return nil, fmt.Errorf("failed to fetch sweep data for %x: %v",
802+
sweepReq.SwapHash[:6], err)
803+
}
804+
731805
return &sweep{
732-
swapHash: swap.Hash,
806+
swapHash: sweepReq.SwapHash,
733807
outpoint: sweepReq.Outpoint,
734808
value: sweepReq.Value,
735-
confTarget: swap.Contract.SweepConfTarget,
736-
timeout: swap.Contract.CltvExpiry,
737-
initiationHeight: swap.Contract.InitiationHeight,
738-
htlc: *htlc,
739-
preimage: swap.Contract.Preimage,
740-
swapInvoicePaymentAddr: *swapPaymentAddr,
741-
htlcKeys: swap.Contract.HtlcKeys,
742-
htlcSuccessEstimator: htlc.AddSuccessToEstimator,
743-
protocolVersion: swap.Contract.ProtocolVersion,
744-
isExternalAddr: swap.Contract.IsExternalAddr,
745-
destAddr: swap.Contract.DestAddr,
809+
confTarget: s.ConfTarget,
810+
timeout: s.Timeout,
811+
initiationHeight: s.InitiationHeight,
812+
htlc: s.HTLC,
813+
preimage: s.Preimage,
814+
swapInvoicePaymentAddr: s.SwapInvoicePaymentAddr,
815+
htlcKeys: s.HTLCKeys,
816+
htlcSuccessEstimator: s.HTLCSuccessEstimator,
817+
protocolVersion: s.ProtocolVersion,
818+
isExternalAddr: s.IsExternalAddr,
819+
destAddr: s.DestAddr,
746820
}, nil
747821
}

0 commit comments

Comments
 (0)