Add checkBlock() to Mining interface #31981

pull Sjors wants to merge 4 commits into bitcoin:master from Sjors:2025/03/checkBlock changing 15 files +440 −122
  1. Sjors commented at 11:04 am on March 4, 2025: member

    This PR adds the IPC equivalent of the getblocktemplate RPC in proposal mode.

    In order to do so it moves TestBlockValidity to ChainstateManager and has is return error reasons as a string instead of BlockValidationState. This avoids complexity in IPC code for handling the latter struct.

    The new Mining interface method is used in miner_tests and the getblocktemplate and generateblock RPC calls, so it has test coverage.

    The inconclusive-not-best-prevblk check is moved from RPC code to TestBlockValidity.

    Test coverage is increased by mining_template_verification.py.

    Superseedes #31564

    Background

    Verifying block templates (no PoW)

    Stratum v2 allows miners to generate their own block template. Pools may wish (or need) to verify these templates. This typically involves comparing mempools, asking miners to providing missing transactions and then reconstructing the proposed block.1 This is not sufficient to ensure a proposed block is actually valid. In some schemes miners could take advantage of incomplete validation2.

    The Stratum Reference Implementation (SRI), currently the only Stratum v2 implementation, collects all missing mempool transactions, but does not yet fully verify the block.3. It could use the getblocktemplate RPC in proposal mode, but using IPC is more performant, as it avoids serialising up to 4 MB of transaction data as JSON.

    (although SRI could use this PR, the Template Provider role doesn’t need it, so this is not part of #31098)

  2. DrahtBot commented at 11:04 am on March 4, 2025: contributor

    The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

    Code Coverage & Benchmarks

    For details see: https://corecheck.dev/bitcoin/bitcoin/pulls/31981.

    Reviews

    See the guideline for information on the review process. A summary of reviews will appear here.

    Conflicts

    Reviewers, this pull request conflicts with the following ones:

    • #29700 (kernel, refactor: return error status on all fatal errors by ryanofsky)
    • #29415 (Broadcast own transactions only via short-lived Tor or I2P connections by vasild)

    If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

  3. Sjors commented at 11:39 am on March 4, 2025: member
    I also salvaged the functional tests from 15d841e41bb61f435dd3e60e99fa4db47d34a3a6.
  4. laanwj added the label Mining on Mar 4, 2025
  5. DrahtBot added the label Needs rebase on Mar 12, 2025
  6. Sjors force-pushed on Mar 13, 2025
  7. Sjors commented at 8:30 am on March 13, 2025: member
    Rebased after #31283.
  8. DrahtBot removed the label Needs rebase on Mar 13, 2025
  9. DrahtBot added the label Needs rebase on Mar 14, 2025
  10. validation: refactor TestBlockValidity
    A later commit adds checkBlock() to the Mining interface. In order to
    avoid passing BlockValidationState over IPC, this commit first
    refactors TestBlockValidity to return a boolean instead, and pass failure
    reasons via a string.
    
    TestBlockValidity is moved into ChainstateManager which allows some
    simplifications.
    
    Comments are expanded.
    
    The ContextualCheckBlockHeader check is moved to after CheckBlock,
    which is more similar to normal validation where context-free checks
    are done first.
    
    Validation failure reasons are no longer printed through LogError(),
    since it depends on the caller whether this implies an actual bug
    in the node, or an externally sourced block that happens to be invalid.
    When called from getblocktemplate, via BlockAssembler::CreateNewBlock(),
    this method already throws an std::runtime_error if validation fails.
    
    Additionally it moves the inconclusive-not-best-prevblk check from RPC
    code to TestBlockValidity.
    1d029c23a1
  11. ipc: drop BlockValidationState special handling
    The Mining interface avoids using BlockValidationState.
    cca5993b26
  12. Add checkBlock to Mining interface
    And use it in miner_tests, getblocktemplate and generateblock.
    f6a6711177
  13. test: more template verification tests e6e170cf6c
  14. Sjors force-pushed on Mar 14, 2025
  15. Sjors commented at 9:03 am on March 14, 2025: member
    Rebased after #31974 (trivial conflict with 3b33fd9424a2b4edbdc1745339e3541ab4b90af5).
  16. DrahtBot removed the label Needs rebase on Mar 14, 2025


Sjors DrahtBot

Labels
Mining


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: 2025-03-31 09:12 UTC

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