Skip to content
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

sweepbatcher: fix some minor bugs #871

Merged
merged 3 commits into from
Jan 17, 2025

Conversation

starius
Copy link
Collaborator

@starius starius commented Jan 9, 2025

Fix minor bugs identified during testing.

  1. Populate batch.currentHeight beforehand, ensuring it's set before monitorSpend is launched. Without this, the entire batcher could crash with the error: "a height hint greater than 0 must be provided."
  2. Add a check to verify that the spending transaction has at least one output before inspecting it. In testing, a transaction with no outputs triggered a panic.
  3. Remove the unused batch.blockEpochChan field.

Pull Request Checklist

  • Update release_notes.md if your PR contains major features, breaking changes or bugfixes

@starius starius marked this pull request as ready for review January 16, 2025 14:31
@starius starius changed the title [WIP] sweepbatcher: fix some minor bugs sweepbatcher: fix some minor bugs Jan 16, 2025
@starius starius requested review from hieblmi and bhandras January 16, 2025 14:31
Copy link
Collaborator

@hieblmi hieblmi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left a question regarding the fee calculation for a batch.

@@ -1872,7 +1876,10 @@ func (b *batch) monitorConfirmations(ctx context.Context) error {
func getFeePortionForSweep(spendTx *wire.MsgTx, numSweeps int,
totalSweptAmt btcutil.Amount) (btcutil.Amount, btcutil.Amount) {

totalFee := int64(totalSweptAmt) - spendTx.TxOut[0].Value
totalFee := int64(totalSweptAmt)
if len(spendTx.TxOut) > 0 {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if there is no TxOut then totalSweptAmt would be considered as totalFee which doesn't seem right. Should we handle an error in an else clause?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If there are no outputs, it means the entire amount was used for miner fees, so the calculation is correct.

That said, this scenario is undesirable, and the user should be warned if it occurs. The function is invoked from handleSpend, which already logs a warning in such cases.

For this situation to arise, the sweeping side would need to be offline (not sweeping) for the entire period, and the other side would have to intentionally choose to burn the coin instead of collecting it. While this is highly unlikely, it is technically possible.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for the clarification.

// If initialDelay is 0, it does not fire to prevent race with
// blockChan which also fires immediately with current tip. Such a race
// may result in double publishing if batchPublishDelay is also 0.
// If initialDelay is 0, it does not fire not to install timerChan twice
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: "it does not fire to not install timerChan"

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rephrased the whole sentence:

        // initialDelayChan is a timer which fires upon initial delay end.
        // If initialDelay is set to 0, it will not trigger to avoid setting up
        // timerChan twice, which could lead to double publishing if
        // batchPublishDelay is also 0.

@starius starius force-pushed the sweepbatcher-fixes branch from e1ceb9d to a7bd90a Compare January 16, 2025 16:41
@hieblmi hieblmi self-requested a review January 16, 2025 21:20
Copy link
Collaborator

@hieblmi hieblmi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 💾

Copy link
Member

@bhandras bhandras left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 🎉

Previously the code handling spending tx crashed if it doesn't have an output.
This is likely to occur only in tests.
Prevent a crash with "a height hint greater than 0 must be provided" error when
monitorSpend starts at the beginning of batch.Run.

The timer timerChan is now initialized at the start, because it was previously
initialized after the first block (the current tip) was read from blockChan and
now the first block is read before the main for-select loop to fill the field
currentHeight in advance.
@starius starius force-pushed the sweepbatcher-fixes branch from a7bd90a to 6efd010 Compare January 17, 2025 15:39
@starius
Copy link
Collaborator Author

starius commented Jan 17, 2025

Fixed a typo in the commit message.

@starius starius merged commit 188e567 into lightninglabs:master Jan 17, 2025
4 checks passed
@starius starius deleted the sweepbatcher-fixes branch January 17, 2025 15:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants