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

Fix off-by-one error checking coinbase maturity in optional UTxOs #1830

Conversation

nymius
Copy link
Contributor

@nymius nymius commented Feb 10, 2025

Description

As I was developing the changes in #1798 I discover issue #1810. So I introduced the fixes in that PR but later I split them in two to ease the review by suggestion of @oleonardolima .

The preselect_utxos method has an off-by-one error that is making the selection of optional UTxOs too restrictive, by
requiring the coinbase outputs to surpass or equal coinbase maturity time at the current height of the selection, and
not in the block in which the transaction may be included in the blockchain.

The changes in this commit fix it by considering the maturity of the coinbase output at the spending height and not
the transaction creation height, this means, a +1 at the considered height at the moment of building the
transaction.

Fixes #1810.

Notes to the reviewers

Tests for issue #1810 have not been explicitly added, as there already was a text_spend_coinbase test which was corrected to ensure coinbase maturation is considered in alignment with the new logic.

Changes are not breaking but I'm modifying slightly the documentation for the public method TxBuilder::current_height to adjust to the fixed code. Does this merit an entry in the CHANGELOG?

Changelog notice

Wallet now considers a utxo originated from a coinbase transaction (coinbase utxo) as available for selection if it will mature in the next block after the height provided to the selection, the current height by default. The previous behavior allowed selecting a coinbase utxo only when the height at the moment of selection was equal to maturity height or greater.

Checklists

  • I've signed all my commits
  • I followed the contribution guidelines
  • I ran cargo fmt and cargo clippy before committing
  • I've updated existing tests to match the fix
  • I've updated docs to match the fix logic
  • This pull request DOES NOT break the existing API
  • I'm linking the issue being fixed by this PR

@nymius nymius marked this pull request as ready for review February 10, 2025 14:22
@nymius nymius self-assigned this Feb 10, 2025
Copy link
Contributor

@oleonardolima oleonardolima left a comment

Choose a reason for hiding this comment

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

cACK efe9b4a

Copy link
Contributor

@LagginTimes LagginTimes left a comment

Choose a reason for hiding this comment

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

ACK 03b7eca

@nymius nymius force-pushed the fix/off-by-one-error-in-coinbase-coin-selection branch from efe9b4a to 7d0dd3a Compare February 11, 2025 19:09
@nymius nymius changed the title fix(wallet): off-by-one error checking coinbase maturity in optional … Fix off-by-one error checking coinbase maturity in optional UTxOs Feb 11, 2025
@ValuedMammal ValuedMammal added this to the 1.2.0 milestone Feb 12, 2025
@ValuedMammal
Copy link
Contributor

Looks good to me. I would add to Changelog something like this

Wallet now considers a coinbase output available for selection if it will mature in the next block after the current height; the previous behavior allowed selecting a coinbase output only when the current height reached the maturity height or greater.

@nymius nymius force-pushed the fix/off-by-one-error-in-coinbase-coin-selection branch from 7d0dd3a to bb18fa7 Compare February 12, 2025 19:40
…UTxOs

The `preselect_utxos` method has an off-by-one error that is making the
selection of optional UTxOs too restrictive, by requiring the coinbase
outputs to surpass or equal coinbase maturity time at the current height
of the selection, and not in the block in which the transaction may be
included in the blockchain.

The changes in this commit fix it by considering the maturity of the
coinbase output at the spending height and not the transaction creation
height, this means, a +1 at the considered height at the moment of
building the transaction.
@nymius nymius force-pushed the fix/off-by-one-error-in-coinbase-coin-selection branch from bb18fa7 to 03b7eca Compare February 12, 2025 19:47
@nymius
Copy link
Contributor Author

nymius commented Feb 12, 2025

Looks good to me. I would add to Changelog something like this

Wallet now considers a coinbase output available for selection if it will mature in the next block after the current height; the previous behavior allowed selecting a coinbase output only when the current height reached the maturity height or greater.

I got confused and added an entry to crates/wallet/CHANGELOG.md. Fortunately, I checked the CHANGELOG policy again, remove it and added the comment to the CHANGELOG section of the PR header.

Copy link
Member

@evanlinjin evanlinjin left a comment

Choose a reason for hiding this comment

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

ACK 03b7eca

spendable &= (current_height
.saturating_sub(anchor.block_id.height))
>= COINBASE_MATURITY;
let spend_height = current_height + 1;
Copy link
Member

Choose a reason for hiding this comment

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

Nit: I think a better name would be inclusion_height.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Won't be needed if #1798 is merged.

@evanlinjin evanlinjin merged commit a4978af into bitcoindevkit:master Feb 14, 2025
23 checks passed
@nymius nymius deleted the fix/off-by-one-error-in-coinbase-coin-selection branch February 14, 2025 20:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

Off-by-one error is making optional UTxOs selection too restrictive for coinbase outputs
5 participants