Skip to content

Commit cb8e988

Browse files
committed
(to squash) make IsPresigned a field of SweepInfo
1 parent 3320ab7 commit cb8e988

File tree

2 files changed

+53
-45
lines changed

2 files changed

+53
-45
lines changed

sweepbatcher/sweep_batcher.go

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,11 @@ type SweepInfo struct {
132132
// has to be spent using preimage. This is only used in fee estimations
133133
// when selecting a batch for the sweep to minimize fees.
134134
NonCoopHint bool
135+
136+
// IsPresigned stores if presigned mode is enabled for the sweep. This
137+
// value should be stable for a sweep. Currently presigned and
138+
// non-presigned sweeps never appear in the same batch.
139+
IsPresigned bool
135140
}
136141

137142
// SweepFetcher is used to get details of a sweep.
@@ -164,12 +169,6 @@ type SignMuSig2 func(ctx context.Context, muSig2Version input.MuSig2Version,
164169
// fails (e.g. because one of the inputs is offline), an input can't be added to
165170
// a batch.
166171
type PresignedHelper interface {
167-
// IsPresigned returns if presigned mode is enabled for a particular
168-
// sweep. This method should always return the same value for the same
169-
// sweep. Currently presigned and non-presigned sweeps never appear in
170-
// the same batch.
171-
IsPresigned(ctx context.Context, input wire.OutPoint) (bool, error)
172-
173172
// Presign tries to presign a batch transaction. If the method returns
174173
// nil, it is guaranteed that future calls to SignTx on this set of
175174
// sweeps return valid signed transactions. The implementation should
@@ -1325,16 +1324,6 @@ func (b *Batcher) loadSweep(ctx context.Context, swapHash lntypes.Hash,
13251324
swapHash[:6], err)
13261325
}
13271326

1328-
// Determine if presigned mode is used for this sweep.
1329-
var presigned bool
1330-
if b.presignedHelper != nil {
1331-
presigned, err = b.presignedHelper.IsPresigned(ctx, outpoint)
1332-
if err != nil {
1333-
return nil, fmt.Errorf("failed to determine presigned "+
1334-
"status for sweep %x: %w", swapHash[:6], err)
1335-
}
1336-
}
1337-
13381327
// Find minimum fee rate for the sweep. Use customFeeRate if it is
13391328
// provided, otherwise use wallet's EstimateFeeRate.
13401329
var minFeeRate chainfee.SatPerKWeight
@@ -1378,7 +1367,7 @@ func (b *Batcher) loadSweep(ctx context.Context, swapHash lntypes.Hash,
13781367
destAddr: s.DestAddr,
13791368
minFeeRate: minFeeRate,
13801369
nonCoopHint: s.NonCoopHint,
1381-
presigned: presigned,
1370+
presigned: s.IsPresigned,
13821371
}, nil
13831372
}
13841373

sweepbatcher/sweep_batcher_presigned_test.go

Lines changed: 47 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -95,19 +95,6 @@ func (h *mockPresignedHelper) getTxFeerate(tx *wire.MsgTx,
9595
return chainfee.NewSatPerKWeight(fee, weight)
9696
}
9797

98-
// IsPresigned returns if the input was previously used in any call to the
99-
// SetOutpointOnline method.
100-
func (h *mockPresignedHelper) IsPresigned(ctx context.Context,
101-
input wire.OutPoint) (bool, error) {
102-
103-
h.mu.Lock()
104-
defer h.mu.Unlock()
105-
106-
_, has := h.onlineOutpoints[input]
107-
108-
return has, nil
109-
}
110-
11198
// Presign tries to presign the transaction. It succeeds if all the inputs
11299
// are online. In case of success it adds the transaction to presignedBatches.
113100
func (h *mockPresignedHelper) Presign(ctx context.Context,
@@ -237,18 +224,21 @@ func (h *mockPresignedHelper) CleanupTransactions(ctx context.Context,
237224
// sweepTimeout is swap timeout block height used in tests of presigned mode.
238225
const sweepTimeout = 1000
239226

240-
// dummySweepFetcherMock implements SweepFetcher by returning blank SweepInfo.
241-
// It is used in TestPresigned, because it doesn't use any fields of SweepInfo.
242-
type dummySweepFetcherMock struct {
243-
}
244-
245227
// FetchSweep returns blank SweepInfo.
246-
func (f *dummySweepFetcherMock) FetchSweep(_ context.Context,
247-
_ lntypes.Hash, _ wire.OutPoint) (*SweepInfo, error) {
228+
// This method implements SweepFetcher interface.
229+
func (h *mockPresignedHelper) FetchSweep(_ context.Context,
230+
_ lntypes.Hash, utxo wire.OutPoint) (*SweepInfo, error) {
231+
232+
h.mu.Lock()
233+
defer h.mu.Unlock()
234+
235+
_, has := h.onlineOutpoints[utxo]
248236

249237
return &SweepInfo{
250238
// Set Timeout to prevent warning messages about timeout=0.
251239
Timeout: sweepTimeout,
240+
241+
IsPresigned: has,
252242
}, nil
253243
}
254244

@@ -274,7 +264,7 @@ func testPresigned_forgotten_presign(t *testing.T,
274264

275265
batcher := NewBatcher(lnd.WalletKit, lnd.ChainNotifier, lnd.Signer,
276266
testMuSig2SignSweep, testVerifySchnorrSig, lnd.ChainParams,
277-
batcherStore, &dummySweepFetcherMock{},
267+
batcherStore, presignedHelper,
278268
WithCustomFeeRate(customFeeRate),
279269
WithPresignedHelper(presignedHelper))
280270

@@ -350,7 +340,7 @@ func testPresigned_input1_offline_then_input2(t *testing.T,
350340

351341
batcher := NewBatcher(lnd.WalletKit, lnd.ChainNotifier, lnd.Signer,
352342
testMuSig2SignSweep, testVerifySchnorrSig, lnd.ChainParams,
353-
batcherStore, &dummySweepFetcherMock{},
343+
batcherStore, presignedHelper,
354344
WithCustomFeeRate(customFeeRate),
355345
WithPresignedHelper(presignedHelper))
356346
go func() {
@@ -532,7 +522,7 @@ func testPresigned_two_inputs_one_goes_offline(t *testing.T,
532522
batcher := NewBatcher(
533523
lnd.WalletKit, lnd.ChainNotifier, lnd.Signer,
534524
testMuSig2SignSweep, testVerifySchnorrSig, lnd.ChainParams,
535-
batcherStore, &dummySweepFetcherMock{},
525+
batcherStore, presignedHelper,
536526
WithCustomFeeRate(customFeeRate),
537527
WithPresignedHelper(presignedHelper),
538528
)
@@ -668,7 +658,7 @@ func testPresigned_first_publish_fails(t *testing.T,
668658
batcher := NewBatcher(
669659
lnd.WalletKit, lnd.ChainNotifier, lnd.Signer,
670660
testMuSig2SignSweep, testVerifySchnorrSig, lnd.ChainParams,
671-
batcherStore, &dummySweepFetcherMock{},
661+
batcherStore, presignedHelper,
672662
WithCustomFeeRate(customFeeRate),
673663
WithPresignedHelper(presignedHelper),
674664
)
@@ -791,7 +781,7 @@ func testPresigned_locktime(t *testing.T,
791781
batcher := NewBatcher(
792782
lnd.WalletKit, lnd.ChainNotifier, lnd.Signer,
793783
testMuSig2SignSweep, testVerifySchnorrSig, lnd.ChainParams,
794-
batcherStore, &dummySweepFetcherMock{},
784+
batcherStore, presignedHelper,
795785
WithCustomFeeRate(customFeeRate),
796786
WithPresignedHelper(presignedHelper),
797787
)
@@ -875,7 +865,7 @@ func testPresigned_presigned_group(t *testing.T,
875865
batcher := NewBatcher(
876866
lnd.WalletKit, lnd.ChainNotifier, lnd.Signer,
877867
testMuSig2SignSweep, testVerifySchnorrSig, lnd.ChainParams,
878-
batcherStore, &dummySweepFetcherMock{},
868+
batcherStore, presignedHelper,
879869
WithCustomFeeRate(customFeeRate),
880870
WithPresignedHelper(presignedHelper),
881871
)
@@ -1052,6 +1042,30 @@ func testPresigned_presigned_group(t *testing.T,
10521042
require.Equal(t, batchPkScript, tx.TxOut[0].PkScript)
10531043
}
10541044

1045+
// wrappedStoreWithPresignedFlag wraps a SweepFetcher store adding IsPresigned
1046+
// flag to the returned sweeps, taking it from mockPresignedHelper.
1047+
type wrappedStoreWithPresignedFlag struct {
1048+
backend SweepFetcher
1049+
helper *mockPresignedHelper
1050+
}
1051+
1052+
// // FetchSweep returns details of the sweep.
1053+
func (s *wrappedStoreWithPresignedFlag) FetchSweep(ctx context.Context,
1054+
swap lntypes.Hash, utxo wire.OutPoint) (*SweepInfo, error) {
1055+
1056+
sweepInfo, err := s.backend.FetchSweep(ctx, swap, utxo)
1057+
if err != nil {
1058+
return nil, err
1059+
}
1060+
1061+
// Attach IsPresigned flag.
1062+
s.helper.mu.Lock()
1063+
defer s.helper.mu.Unlock()
1064+
_, sweepInfo.IsPresigned = s.helper.onlineOutpoints[utxo]
1065+
1066+
return sweepInfo, nil
1067+
}
1068+
10551069
// testPresigned_presigned_and_regular_sweeps tests a combination of presigned
10561070
// mode and regular mode for the following scenario: one regular input is added,
10571071
// then a presigned input is added and it goes to another batch, because they
@@ -1088,10 +1102,15 @@ func testPresigned_presigned_and_regular_sweeps(t *testing.T, store testStore,
10881102
sweepStore, err := NewSweepFetcherFromSwapStore(store, lnd.ChainParams)
10891103
require.NoError(t, err)
10901104

1105+
sweepFetcher := &wrappedStoreWithPresignedFlag{
1106+
backend: sweepStore,
1107+
helper: presignedHelper,
1108+
}
1109+
10911110
batcher := NewBatcher(
10921111
lnd.WalletKit, lnd.ChainNotifier, lnd.Signer,
10931112
testMuSig2SignSweep, testVerifySchnorrSig, lnd.ChainParams,
1094-
batcherStore, sweepStore,
1113+
batcherStore, sweepFetcher,
10951114
WithCustomFeeRate(customFeeRate),
10961115
WithPresignedHelper(presignedHelper),
10971116
)
@@ -1359,7 +1378,7 @@ func testPresigned_purging(t *testing.T, numSwaps, numConfirmedSwaps int,
13591378
batcher := NewBatcher(
13601379
lnd.WalletKit, lnd.ChainNotifier, lnd.Signer,
13611380
testMuSig2SignSweep, testVerifySchnorrSig, lnd.ChainParams,
1362-
batcherStore, &dummySweepFetcherMock{},
1381+
batcherStore, presignedHelper,
13631382
WithCustomFeeRate(customFeeRate),
13641383
WithPresignedHelper(presignedHelper),
13651384
)

0 commit comments

Comments
 (0)