-
Notifications
You must be signed in to change notification settings - Fork 122
sweepbatcher: presign transactions in parallel #1022
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,6 +12,7 @@ import ( | |
| "github.com/btcsuite/btcd/wire" | ||
| "github.com/lightningnetwork/lnd/lntypes" | ||
| "github.com/lightningnetwork/lnd/lnwallet/chainfee" | ||
| "golang.org/x/sync/errgroup" | ||
| ) | ||
|
|
||
| // ensurePresigned checks that there is a presigned transaction spending the | ||
|
|
@@ -371,6 +372,8 @@ func presign(ctx context.Context, presigner presigner, destAddr btcutil.Address, | |
| // Set LockTime to 0. It is not critical. | ||
| const currentHeight = 0 | ||
|
|
||
| eg, grCtx := errgroup.WithContext(ctx) | ||
|
|
||
| for fr := start; fr <= stop; fr = (fr * factorPPM) / 1_000_000 { | ||
| // Construct an unsigned transaction for this fee rate. | ||
| tx, _, feeForWeight, fee, err := constructUnsignedTx( | ||
|
|
@@ -389,21 +392,31 @@ func presign(ctx context.Context, presigner presigner, destAddr btcutil.Address, | |
|
|
||
| // Try to presign this transaction. | ||
| const loadOnly = false | ||
| _, err = presigner.SignTx( | ||
| ctx, primarySweepID, tx, batchAmt, minRelayFeeRate, fr, | ||
| loadOnly, | ||
| ) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to presign unsigned tx %v "+ | ||
| "for feeRate %v: %w", tx.TxHash(), fr, err) | ||
| } | ||
| eg.Go(func() error { | ||
| _, err := presigner.SignTx( | ||
| grCtx, primarySweepID, tx, batchAmt, | ||
| minRelayFeeRate, fr, loadOnly, | ||
| ) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to presign unsigned "+ | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. before we return, should we cancel There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. +1, if you're signing with sighash_all then I believe you should terminate early. If signatures in an otherwise "failed" batch can be re-used (i.e sighash_single?) then you should still sign them. Although I believe the former is the case. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. grCtx is canceled automatically if an error is returned. https://pkg.go.dev/golang.org/x/sync/errgroup#WithContext
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @GeorgeTsagk Each goroutine signs a separate transaction (they are the same except that they have different fee rates). We need all of them to proceed. If some of them fail to sign, we cancel the whole process. |
||
| "tx %v for feeRate %v: %w", tx.TxHash(), | ||
| fr, err) | ||
| } | ||
|
|
||
| return nil | ||
| }) | ||
|
|
||
| // If fee was clamped, stop here, because fee rate won't grow. | ||
| if fee < feeForWeight { | ||
| break | ||
| } | ||
| } | ||
|
|
||
| if err := eg.Wait(); err != nil { | ||
| return fmt.Errorf("presigning of batch of primarySweepID %v "+ | ||
| "failed: %w", primarySweepID, err) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
We'll soon tag our rc2 so eventually you might want to bump again to a tagged version, if preferred.