Skip to content

Commit b339904

Browse files
committed
sweepbatcher: remove method Presign
Method Presign is not as reliable as SignTx, because it checks transaction by txid and can miss for example if LockTime is different. SignTx can do everything Presign was used for.
1 parent ea312b8 commit b339904

File tree

5 files changed

+39
-68
lines changed

5 files changed

+39
-68
lines changed

sweepbatcher/presigned.go

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,7 @@ func (b *batch) ensurePresigned(ctx context.Context, newSweeps []*sweep,
3636
// presignedTxChecker has methods to check if the inputs are presigned.
3737
type presignedTxChecker interface {
3838
destPkScripter
39-
40-
// SignTx signs an unsigned transaction or returns a pre-signed tx.
41-
// It is only called with loadOnly=true by ensurePresigned.
42-
SignTx(ctx context.Context, primarySweepID wire.OutPoint,
43-
tx *wire.MsgTx, inputAmt btcutil.Amount,
44-
minRelayFee, feeRate chainfee.SatPerKWeight,
45-
loadOnly bool) (*wire.MsgTx, error)
39+
presigner
4640
}
4741

4842
// ensurePresigned checks that there is a presigned transaction spending the
@@ -287,11 +281,12 @@ func (b *batch) presign(ctx context.Context, newSweeps []*sweep) error {
287281

288282
// presigner tries to presign a batch transaction.
289283
type presigner interface {
290-
// Presign tries to presign a batch transaction. If the method returns
291-
// nil, it is guaranteed that future calls to SignTx on this set of
292-
// sweeps return valid signed transactions.
293-
Presign(ctx context.Context, primarySweepID wire.OutPoint,
294-
tx *wire.MsgTx, inputAmt btcutil.Amount) error
284+
// SignTx signs an unsigned transaction or returns a pre-signed tx.
285+
// It is only called with loadOnly=true by ensurePresigned.
286+
SignTx(ctx context.Context, primarySweepID wire.OutPoint,
287+
tx *wire.MsgTx, inputAmt btcutil.Amount,
288+
minRelayFee, feeRate chainfee.SatPerKWeight,
289+
loadOnly bool) (*wire.MsgTx, error)
295290
}
296291

297292
// presign tries to presign batch sweep transactions of the sweeps. It signs
@@ -370,7 +365,14 @@ func presign(ctx context.Context, presigner presigner, destAddr btcutil.Address,
370365
}
371366

372367
// Try to presign this transaction.
373-
err = presigner.Presign(ctx, primarySweepID, tx, batchAmt)
368+
const (
369+
loadOnly = false
370+
minRelayFee = chainfee.AbsoluteFeePerKwFloor
371+
)
372+
_, err = presigner.SignTx(
373+
ctx, primarySweepID, tx, batchAmt, minRelayFee, fr,
374+
loadOnly,
375+
)
374376
if err != nil {
375377
return fmt.Errorf("failed to presign unsigned tx %v "+
376378
"for feeRate %v: %w", tx.TxHash(), fr, err)

sweepbatcher/presigned_test.go

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -553,24 +553,30 @@ type mockPresigner struct {
553553
failAt int
554554
}
555555

556-
// Presign memorizes the value of the output and fails if the number of
556+
// SignTx memorizes the value of the output and fails if the number of
557557
// calls previously made is failAt.
558-
func (p *mockPresigner) Presign(ctx context.Context,
559-
primarySweepID wire.OutPoint, tx *wire.MsgTx,
560-
inputAmt btcutil.Amount) error {
558+
func (p *mockPresigner) SignTx(ctx context.Context,
559+
primarySweepID wire.OutPoint, tx *wire.MsgTx, inputAmt btcutil.Amount,
560+
minRelayFee, feeRate chainfee.SatPerKWeight,
561+
loadOnly bool) (*wire.MsgTx, error) {
562+
563+
if ctx.Err() != nil {
564+
return nil, ctx.Err()
565+
}
561566

562567
if !hasInput(tx, primarySweepID) {
563-
return fmt.Errorf("primarySweepID %v not in tx", primarySweepID)
568+
return nil, fmt.Errorf("primarySweepID %v not in tx",
569+
primarySweepID)
564570
}
565571

566572
if len(p.outputs)+1 == p.failAt {
567-
return fmt.Errorf("test error in Presign")
573+
return nil, fmt.Errorf("test error in SignTx")
568574
}
569575

570576
p.outputs = append(p.outputs, btcutil.Amount(tx.TxOut[0].Value))
571577
p.lockTimes = append(p.lockTimes, tx.LockTime)
572578

573-
return nil
579+
return tx, nil
574580
}
575581

576582
// TestPresign checks that function presign presigns correct set of transactions

sweepbatcher/sweep_batch.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,7 @@ func (b *batch) Errorf(format string, params ...interface{}) {
481481
// checkSweepToAdd checks if a sweep can be added or updated in the batch. The
482482
// caller must lock the event loop using scheduleNextCall. The function returns
483483
// if the sweep already exists in the batch. If presigned mode is enabled, the
484-
// result depends on the outcome of the method presignedHelper.Presign for a
484+
// result depends on the outcome of the method presignedHelper.SignTx for a
485485
// non-empty batch. For an empty batch, the input needs to pass
486486
// PresignSweepsGroup.
487487
func (b *batch) checkSweepToAdd(_ context.Context, sweep *sweep) (bool, error) {

sweepbatcher/sweep_batcher.go

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -158,18 +158,11 @@ type SignMuSig2 func(ctx context.Context, muSig2Version input.MuSig2Version,
158158
// fails (e.g. because one of the inputs is offline), an input can't be added to
159159
// a batch.
160160
type PresignedHelper interface {
161-
// Presign tries to presign a batch transaction. If the method returns
162-
// nil, it is guaranteed that future calls to SignTx on this set of
163-
// sweeps return valid signed transactions. The implementation should
164-
// first check if this transaction already exists in the store to skip
165-
// cosigning if possible.
166-
Presign(ctx context.Context, primarySweepID wire.OutPoint,
167-
tx *wire.MsgTx, inputAmt btcutil.Amount) error
168-
169161
// DestPkScript returns destination pkScript used by the sweep batch
170162
// with the primary outpoint specified. Returns an error, if such tx
171163
// doesn't exist. If there are many such transactions, returns any of
172164
// pkScript's; all of them should have the same destination pkScript.
165+
// TODO: embed this data into SweepInfo.
173166
DestPkScript(ctx context.Context,
174167
primarySweepID wire.OutPoint) ([]byte, error)
175168

@@ -905,7 +898,7 @@ func (b *Batcher) handleSweeps(ctx context.Context, sweeps []*sweep,
905898

906899
// spinUpNewBatch creates new batch, starts it and adds the sweeps to it. If
907900
// presigned mode is enabled, the result also depends on outcome of
908-
// presignedHelper.Presign.
901+
// presignedHelper.SignTx.
909902
func (b *Batcher) spinUpNewBatch(ctx context.Context, sweeps []*sweep) error {
910903
// Spin up a fresh batch.
911904
newBatch, err := b.spinUpBatch(ctx)

sweepbatcher/sweep_batcher_presigned_test.go

Lines changed: 8 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -96,42 +96,6 @@ func (h *mockPresignedHelper) getTxFeerate(tx *wire.MsgTx,
9696
return chainfee.NewSatPerKWeight(fee, weight)
9797
}
9898

99-
// Presign tries to presign the transaction. It succeeds if all the inputs
100-
// are online. In case of success it adds the transaction to presignedBatches.
101-
func (h *mockPresignedHelper) Presign(ctx context.Context,
102-
primarySweepID wire.OutPoint, tx *wire.MsgTx,
103-
inputAmt btcutil.Amount) error {
104-
105-
h.mu.Lock()
106-
defer h.mu.Unlock()
107-
108-
// Check if such a transaction already exists. This is not only an
109-
// optimization, but also enables re-adding multiple groups if sweeps
110-
// are offline.
111-
wantTxHash := tx.TxHash()
112-
for _, candidate := range h.presignedBatches[primarySweepID] {
113-
if candidate.TxHash() == wantTxHash {
114-
return nil
115-
}
116-
}
117-
118-
if !hasInput(tx, primarySweepID) {
119-
return fmt.Errorf("primarySweepID %v not in tx", primarySweepID)
120-
}
121-
122-
if offline := h.offlineInputs(tx); len(offline) != 0 {
123-
return fmt.Errorf("some inputs of tx are offline: %v", offline)
124-
}
125-
126-
tx = tx.Copy()
127-
h.sign(tx)
128-
h.presignedBatches[primarySweepID] = append(
129-
h.presignedBatches[primarySweepID], tx,
130-
)
131-
132-
return nil
133-
}
134-
13599
// DestPkScript returns destination pkScript used in presigned tx sweeping
136100
// these inputs.
137101
func (h *mockPresignedHelper) DestPkScript(ctx context.Context,
@@ -164,6 +128,11 @@ func (h *mockPresignedHelper) SignTx(ctx context.Context,
164128
feeRate, minRelayFee)
165129
}
166130

131+
if !hasInput(tx, primarySweepID) {
132+
return nil, fmt.Errorf("primarySweepID %v not in tx",
133+
primarySweepID)
134+
}
135+
167136
// If all the inputs are online and loadOnly is not set, sign this exact
168137
// transaction.
169138
if offline := h.offlineInputs(tx); len(offline) == 0 && !loadOnly {
@@ -205,7 +174,8 @@ func (h *mockPresignedHelper) SignTx(ctx context.Context,
205174
}
206175

207176
if bestTx == nil {
208-
return nil, fmt.Errorf("no such presigned tx found")
177+
return nil, fmt.Errorf("some outpoint is offline and no " +
178+
"suitable presigned tx found")
209179
}
210180

211181
return bestTx.Copy(), nil
@@ -1025,7 +995,7 @@ func testPresigned_presigned_group(t *testing.T,
1025995

1026996
// An attempt to presign must fail.
1027997
err = batcher.PresignSweepsGroup(ctx, group1, sweepTimeout, destAddr)
1028-
require.ErrorContains(t, err, "some inputs of tx are offline")
998+
require.ErrorContains(t, err, "some outpoint is offline")
1029999

10301000
// Enable both outpoints.
10311001
presignedHelper.SetOutpointOnline(op2, true)

0 commit comments

Comments
 (0)