Skip to content

Commit c551410

Browse files
committed
. presignedOnly argument of SignTx
This is needed to implement ensurePresigned as read-only, so it only checks if a presigned transaction exists, not tries to sign it.
1 parent 334b5f9 commit c551410

File tree

3 files changed

+37
-13
lines changed

3 files changed

+37
-13
lines changed

sweepbatcher/presigned.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,11 @@ func (b *batch) ensurePresigned(ctx context.Context, newSweep *sweep) error {
5151
"for feeRate %v: %w", feeRate, err)
5252
}
5353

54-
// Try to presign this transaction.
54+
// Check of a presigned transaction exists.
5555
batchAmt := newSweep.value
56+
const presignedOnly = true
5657
signedTx, err := b.cfg.presignedHelper.SignTx(
57-
ctx, tx, batchAmt, feeRate, feeRate,
58+
ctx, tx, batchAmt, feeRate, feeRate, presignedOnly,
5859
)
5960
if err != nil {
6061
return fmt.Errorf("failed to sign unsigned tx %v "+
@@ -271,8 +272,9 @@ func (b *batch) publishPresigned(ctx context.Context) (btcutil.Amount, error,
271272

272273
// Get a signed transaction. It may be either new transaction or a
273274
// pre-signed one.
275+
const presignedOnly = false
274276
signedTx, err := b.cfg.presignedHelper.SignTx(
275-
ctx, tx, batchAmt, minRelayFee, feeRate,
277+
ctx, tx, batchAmt, minRelayFee, feeRate, presignedOnly,
276278
)
277279
if err != nil {
278280
return 0, fmt.Errorf("failed to sign tx: %w", err),

sweepbatcher/sweep_batcher.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -189,10 +189,11 @@ type PresignedHelper interface {
189189
// - transaction version must be the same;
190190
// - Sequence numbers in the inputs must be preserved.
191191
// When choosing a presigned transaction, a transaction with fee rate
192-
// closer to the fee rate passed is selected.
192+
// closer to the fee rate passed is selected. If presignedOnly is set,
193+
// it doesn't try to sign the transaction and only loads a presigned tx.
193194
SignTx(ctx context.Context, tx *wire.MsgTx, inputAmt btcutil.Amount,
194-
minRelayFee, feeRate chainfee.SatPerKWeight) (*wire.MsgTx,
195-
error)
195+
minRelayFee, feeRate chainfee.SatPerKWeight,
196+
presignedOnly bool) (*wire.MsgTx, error)
196197

197198
// CleanupTransactions removes all transactions related to any of the
198199
// outpoints. Should be called after sweep batch tx is fully confirmed.

sweepbatcher/sweep_batcher_test.go

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4152,14 +4152,15 @@ func (h *mockPresignedHelper) DestPkScript(ctx context.Context,
41524152
// the exact transaction passed and adds it to presignedBatches. Otherwise it
41534153
// looks for a transaction in presignedBatches satisfying the criteria.
41544154
func (h *mockPresignedHelper) SignTx(ctx context.Context, tx *wire.MsgTx,
4155-
inputAmt btcutil.Amount,
4156-
minRelayFee, feeRate chainfee.SatPerKWeight) (*wire.MsgTx, error) {
4155+
inputAmt btcutil.Amount, minRelayFee, feeRate chainfee.SatPerKWeight,
4156+
presignedOnly bool) (*wire.MsgTx, error) {
41574157

41584158
h.mu.Lock()
41594159
defer h.mu.Unlock()
41604160

4161-
// If all the inputs are online, sign this exact transaction.
4162-
if offline := h.offlineInputs(tx); len(offline) == 0 {
4161+
// If all the inputs are online and presignedOnly is not set, sign
4162+
// this exact transaction.
4163+
if offline := h.offlineInputs(tx); len(offline) == 0 && !presignedOnly {
41634164
tx = tx.Copy()
41644165
h.sign(tx)
41654166

@@ -4298,9 +4299,9 @@ func testPresigned_input1_offline_then_input2(t *testing.T,
42984299
WithCustomFeeRate(customFeeRate),
42994300
WithPresignedHelper(presignedHelper))
43004301

4302+
batcherErrChan := make(chan error)
43014303
go func() {
4302-
err := batcher.Run(ctx)
4303-
checkBatcherError(t, err)
4304+
batcherErrChan <- batcher.Run(ctx)
43044305
}()
43054306

43064307
setFeeRate(feeRateLow)
@@ -4318,6 +4319,25 @@ func testPresigned_input1_offline_then_input2(t *testing.T,
43184319
Notifier: &dummyNotifier,
43194320
}
43204321

4322+
// Make sure that the batcher crashes if AddSweep is called before
4323+
// PresignSweep even if the input is online.
4324+
presignedHelper.SetOutpointOnline(op1, true)
4325+
require.NoError(t, batcher.AddSweep(&sweepReq1))
4326+
err = <-batcherErrChan
4327+
require.Error(t, err)
4328+
require.ErrorContains(t, err, "PresignSweep was not called")
4329+
4330+
// Start the batcher again.
4331+
batcher = NewBatcher(lnd.WalletKit, lnd.ChainNotifier, lnd.Signer,
4332+
testMuSig2SignSweep, testVerifySchnorrSig, lnd.ChainParams,
4333+
batcherStore, &dummySweepFetcherMock{},
4334+
WithCustomFeeRate(customFeeRate),
4335+
WithPresignedHelper(presignedHelper))
4336+
go func() {
4337+
err := batcher.Run(ctx)
4338+
checkBatcherError(t, err)
4339+
}()
4340+
43214341
// This should fail, because the input is offline.
43224342
presignedHelper.SetOutpointOnline(op1, false)
43234343
err = batcher.PresignSweep(ctx, op1, 1_000_000, sweepTimeout, destAddr)
@@ -4429,9 +4449,10 @@ func testPresigned_input1_offline_then_input2(t *testing.T,
44294449

44304450
// Make sure we still have presigned transactions for the second batch.
44314451
presignedHelper.SetOutpointOnline(op2, false)
4452+
const presignedOnly = true
44324453
_, err = presignedHelper.SignTx(
44334454
ctx, batch2, 2_000_000, chainfee.FeePerKwFloor,
4434-
chainfee.FeePerKwFloor,
4455+
chainfee.FeePerKwFloor, presignedOnly,
44354456
)
44364457
require.NoError(t, err)
44374458
}

0 commit comments

Comments
 (0)