Skip to content

Commit e7d5172

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 f19f9f5 commit e7d5172

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
@@ -289,11 +283,12 @@ func (b *batch) presign(ctx context.Context, newSweeps []*sweep) error {
289283

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

299294
// presign tries to presign batch sweep transactions of the sweeps. It signs
@@ -372,7 +367,14 @@ func presign(ctx context.Context, presigner presigner, destAddr btcutil.Address,
372367
}
373368

374369
// Try to presign this transaction.
375-
err = presigner.Presign(ctx, primarySweepID, tx, batchAmt)
370+
const (
371+
loadOnly = false
372+
minRelayFee = chainfee.AbsoluteFeePerKwFloor
373+
)
374+
_, err = presigner.SignTx(
375+
ctx, primarySweepID, tx, batchAmt, minRelayFee, fr,
376+
loadOnly,
377+
)
376378
if err != nil {
377379
return fmt.Errorf("failed to presign unsigned tx %v "+
378380
"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
@@ -612,24 +612,30 @@ type mockPresigner struct {
612612
failAt int
613613
}
614614

615-
// Presign memorizes the value of the output and fails if the number of
615+
// SignTx memorizes the value of the output and fails if the number of
616616
// calls previously made is failAt.
617-
func (p *mockPresigner) Presign(ctx context.Context,
618-
primarySweepID wire.OutPoint, tx *wire.MsgTx,
619-
inputAmt btcutil.Amount) error {
617+
func (p *mockPresigner) SignTx(ctx context.Context,
618+
primarySweepID wire.OutPoint, tx *wire.MsgTx, inputAmt btcutil.Amount,
619+
minRelayFee, feeRate chainfee.SatPerKWeight,
620+
loadOnly bool) (*wire.MsgTx, error) {
621+
622+
if ctx.Err() != nil {
623+
return nil, ctx.Err()
624+
}
620625

621626
if !hasInput(tx, primarySweepID) {
622-
return fmt.Errorf("primarySweepID %v not in tx", primarySweepID)
627+
return nil, fmt.Errorf("primarySweepID %v not in tx",
628+
primarySweepID)
623629
}
624630

625631
if len(p.outputs)+1 == p.failAt {
626-
return fmt.Errorf("test error in Presign")
632+
return nil, fmt.Errorf("test error in SignTx")
627633
}
628634

629635
p.outputs = append(p.outputs, btcutil.Amount(tx.TxOut[0].Value))
630636
p.lockTimes = append(p.lockTimes, tx.LockTime)
631637

632-
return nil
638+
return tx, nil
633639
}
634640

635641
// 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
@@ -485,7 +485,7 @@ func (b *batch) Errorf(format string, params ...interface{}) {
485485
// checkSweepToAdd checks if a sweep can be added or updated in the batch. The
486486
// caller must lock the event loop using scheduleNextCall. The function returns
487487
// if the sweep already exists in the batch. If presigned mode is enabled, the
488-
// result depends on the outcome of the method presignedHelper.Presign for a
488+
// result depends on the outcome of the method presignedHelper.SignTx for a
489489
// non-empty batch. For an empty batch, the input needs to pass
490490
// PresignSweepsGroup.
491491
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
@@ -159,18 +159,11 @@ type SignMuSig2 func(ctx context.Context, muSig2Version input.MuSig2Version,
159159
// fails (e.g. because one of the inputs is offline), an input can't be added to
160160
// a batch.
161161
type PresignedHelper interface {
162-
// Presign tries to presign a batch transaction. If the method returns
163-
// nil, it is guaranteed that future calls to SignTx on this set of
164-
// sweeps return valid signed transactions. The implementation should
165-
// first check if this transaction already exists in the store to skip
166-
// cosigning if possible.
167-
Presign(ctx context.Context, primarySweepID wire.OutPoint,
168-
tx *wire.MsgTx, inputAmt btcutil.Amount) error
169-
170162
// DestPkScript returns destination pkScript used by the sweep batch
171163
// with the primary outpoint specified. Returns an error, if such tx
172164
// doesn't exist. If there are many such transactions, returns any of
173165
// pkScript's; all of them should have the same destination pkScript.
166+
// TODO: embed this data into SweepInfo.
174167
DestPkScript(ctx context.Context,
175168
primarySweepID wire.OutPoint) ([]byte, error)
176169

@@ -951,7 +944,7 @@ func (b *Batcher) handleSweeps(ctx context.Context, sweeps []*sweep,
951944

952945
// spinUpNewBatch creates new batch, starts it and adds the sweeps to it. If
953946
// presigned mode is enabled, the result also depends on outcome of
954-
// presignedHelper.Presign.
947+
// presignedHelper.SignTx.
955948
func (b *Batcher) spinUpNewBatch(ctx context.Context, sweeps []*sweep) error {
956949
// Spin up a fresh batch.
957950
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)