Simplify IsFinalTx logic. #19518

pull roconnor-blockstream wants to merge 1 commits into bitcoin:master from roconnor-blockstream:IsFinalTx changing 1 files +0 −2
  1. roconnor-blockstream commented at 5:17 PM on July 14, 2020: contributor

    There is no need to explicitly check tx.nLockTime == 0. If tx.nLockTime is 0, then it will be compared with nBlockHeight, which will only fail if nBlockHeight is 0. nBlockHeight is only 0 for the Genesis block (see https://github.com/bitcoin/bitcoin/blob/f4de89edfa8be4501534fec0c662c650a4ce7ef2/src/validation.cpp#L3544) The Genesis block is fixed and its sequence number is final. Therefore the IsFinalTx will still return true, even if it is called on the Genesis block.

    <!-- *** Please remove the following help text before submitting: *** Pull requests without a rationale and clear improvement may be closed immediately. GUI-related pull requests should be opened against https://github.com/bitcoin-core/gui first. See CONTRIBUTING.md -->

    <!-- Please provide clear motivation for your patch and explain how it improves Bitcoin Core user experience or Bitcoin Core developer experience significantly: * Any test improvements or new tests that improve coverage are always welcome. * All other changes should have accompanying unit tests (see `src/test/`) or functional tests (see `test/`). Contributors should note which tests cover modified code. If no tests exist for a region of modified code, new tests should accompany the change. * Bug fixes are most welcome when they come with steps to reproduce or an explanation of the potential issue as well as reasoning for the way the bug was fixed. * Features are welcome, but might be rejected due to design or scope issues. If a feature is based on a lot of dependencies, contributors should first consider building the system outside of Bitcoin Core, if possible. * Refactoring changes are only accepted if they are required for a feature or bug fix or otherwise improve developer experience significantly. For example, most "code style" refactoring changes require a thorough explanation why they are useful, what downsides they have and why they *significantly* improve developer experience or avoid serious programming bugs. Note that code style is often a subjective matter. Unless they are explicitly mentioned to be preferred in the [developer notes](/doc/developer-notes.md), stylistic code changes are usually rejected. -->

    <!-- Bitcoin Core has a thorough review process and even the most trivial change needs to pass a lot of eyes and requires non-zero or even substantial time effort to review. There is a huge lack of active reviewers on the project, so patches often sit for a long time. -->

  2. Simplify IsFinalTx logic.
    There is no need to explicitly check tx.nLockTime == 0.
    If tx.nLockTime is 0, then it will be compared with nBlockHeight, which will only fail if nBlockHeight is 0.
    nBlockHeight is only 0 for the Genesis block (see https://github.com/bitcoin/bitcoin/blob/f4de89edfa8be4501534fec0c662c650a4ce7ef2/src/validation.cpp#L3544)
    The Genesis block is fixed and its sequence number is final.
    Therefore the IsFinalTx will still return true, even if it is called on the Genesis block.
    b97ee24178
  3. JeremyRubin commented at 5:42 PM on July 14, 2020: contributor

    This change appears to implement identical logic except in the cases noted.

    My preference would be if we're going to be cleaning up this code, to clean up the gross ternary expression to be easier to read rather than a (modified/validated genesis block excluded) mostly redundant check.

    Is there a motivating reason for this change other than code cleanup?

  4. roconnor-blockstream commented at 5:51 PM on July 14, 2020: contributor

    My personal motivation is that I was trying to interpret the locktime rules in order to work with them. I replicated the logic as it was written here originally, but upon reflection it seemed unnecessarily complex. I cleaned up the logic in my own work and thought I might as well suggest the same clean up for Bitcoin Core while I was on the topic.

  5. JeremyRubin commented at 6:01 PM on July 14, 2020: contributor

    Might be interesting to check out a previous PR I opened which cleaned up consensus logic for similar reasons. #15969 (comment), as it discusses a large number of contributors perceived priorities around simplifying consensus logic.

  6. laanwj added the label Validation on Jul 15, 2020
  7. sdaftuar commented at 11:40 PM on July 28, 2020: member

    I'm a Concept NACK on changes like this.

    While this patch seems correct to me, I think that changing consensus code should have a pretty high bar -- if there is no discernible benefit to the change (such as performance, or allowing for development of new features), then I'd prefer that we (at most) document our understanding of the logic in cases like this, rather than change it.

    If our documentation ends up being wrong, no harm done; if instead an incorrect change to consensus code slips through, we risk splitting the network. As a project, I don't think it makes sense to spend the effort that would be required to make the latter a sufficiently low-risk choice when compared with the former. Our efforts are better spent on adding features and fixing bugs.

  8. MarcoFalke commented at 4:29 AM on July 29, 2020: member

    Too controversial for a simple fix like this. Closing for now.

  9. MarcoFalke closed this on Jul 29, 2020

  10. MarcoFalke added the label Refactoring on Jul 29, 2020
  11. DrahtBot locked this on Feb 15, 2022

github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2026-04-19 15:14 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me