test: Allow to set height in create_block #35089

pull maflcko wants to merge 4 commits into bitcoin:master from maflcko:2604-create_block_height changing 28 files +89 −97
  1. maflcko commented at 6:46 AM on April 16, 2026: member

    The create_block helper is often called with create_coinbase: create_block(prev_hash, create_coinbase(new_height))

    This is fine, but a bit verbose and tedious to type each time. Also, it requires an additional import.

    Improve this by allowing to set height in create_block directly, similar to other options, such as ntime, or hashprev.

  2. DrahtBot renamed this:
    test: Allow to set height in create_block
    test: Allow to set height in create_block
    on Apr 16, 2026
  3. DrahtBot added the label Tests on Apr 16, 2026
  4. DrahtBot commented at 6:47 AM on April 16, 2026: contributor

    <!--e57a25ab6845829454e8d69fc972939a-->

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

    <!--021abf342d371248e50ceaed478a90ca-->

    Reviews

    See the guideline for information on the review process.

    Type Reviewers
    ACK l0rinc, theStack

    If your review is incorrectly listed, please copy-paste <code>&lt;!--meta-tag:bot-skip--&gt;</code> into the comment that the bot should ignore.

    <!--174a7506f384e20aa4161008e828411d-->

    Conflicts

    Reviewers, this pull request conflicts with the following ones:

    • #33854 (fix assumevalid is ignored during reindex by Eunovo)

    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.

    <!--5faf32d7da4f0f540f40219e4f7537a3-->

  5. DrahtBot added the label CI failed on Apr 16, 2026
  6. maflcko force-pushed on Apr 16, 2026
  7. in test/functional/test_framework/blocktools.py:111 in eeee853631 outdated
     107 | @@ -108,7 +108,7 @@ def create_block(hashprev=None, coinbase=None, ntime=None, *, version=None, tmpl
     108 |      else:
     109 |          block.nBits = REGTEST_N_BITS
     110 |      if coinbase is None:
     111 | -        coinbase = create_coinbase(height=tmpl['height'])
     112 | +        coinbase = create_coinbase(height=height or tmpl["height"])
    


    l0rinc commented at 9:00 AM on April 16, 2026:

    eeee853 test: Allow to set height in create_block:

    Can we add a simple feature_framework_unit_tests.py test here to exercise this new fallback behavior?

    class TestFrameworkBlockTools(unittest.TestCase):
        def test_create_block_prefers_explicit_height(self):
            block = create_block(
                hashprev=1,
                tmpl={"height": 100, "curtime": 2},
                height=200,
            )
            assert_equal(CScriptNum.decode(block.vtx[0].vin[0].scriptSig), 200)
    

    Nit: this is safe because genesis isn't passed here so a height number won't ever be falsy, right?


    maflcko commented at 10:06 AM on April 16, 2026:

    Nit: this is safe because genesis isn't passed here so a height number won't ever be falsy, right?

    Correct. I am confident that it is neither possible, nor meaningful, to try to re-create the genesis block here.

    feature_framework_unit_tests

    I think all of this is checked by the Bitcoin Core consensus code already, so there shouldn't be a need for a unit test. If you insist, and send a full diff or a full commit that I can push as-is, I may consider it.


    l0rinc commented at 10:45 AM on April 16, 2026:

    send a full diff or a full commit that I can push as-is, I may consider it

    I actually meant just the create_block change here, which can now choose between two provided height values:

    diff --git a/test/functional/test_framework/blocktools.py b/test/functional/test_framework/blocktools.py
    --- a/test/functional/test_framework/blocktools.py	(revision 22ca336e09824c347e89348166d3208ac04d3693)
    +++ b/test/functional/test_framework/blocktools.py	(date 1776336206046)
    @@ -278,6 +278,14 @@
         return node.sendrawtransaction(tx_to_witness)
     
     class TestFrameworkBlockTools(unittest.TestCase):
    +    def test_create_block_prefers_explicit_height(self):
    +        block = create_block(
    +            hashprev=1,
    +            tmpl={"height": 100, "curtime": 2},
    +            height=200,
    +        )
    +        assert_equal(CScriptNum.decode(block.vtx[0].vin[0].scriptSig), 200)
    +
         def test_create_coinbase(self):
             height = 20
             coinbase_tx = create_coinbase(height=height)
    

    maflcko commented at 12:16 PM on April 16, 2026:

    ah nice. Pushed something similar to your diff.

  8. in test/functional/interface_zmq.py:421 in eeee853631 outdated
     417 | @@ -419,7 +418,7 @@ def test_sequence(self):
     418 |          bump_txid = self.nodes[0].sendrawtransaction(orig_tx['tx'].serialize().hex())
     419 |          # Mine the pre-bump tx
     420 |          txs_to_add = [orig_tx['hex']] + [tx['hex'] for tx in more_tx]
     421 | -        block = create_block(int(self.nodes[0].getbestblockhash(), 16), create_coinbase(self.nodes[0].getblockcount()+1), txlist=txs_to_add)
     422 | +        block = create_block(int(self.nodes[0].getbestblockhash(), 16), height=self.nodes[0].getblockcount() + 1, txlist=txs_to_add)
    


    l0rinc commented at 9:05 AM on April 16, 2026:

    eeee853 test: Allow to set height in create_block:

    A positional arg was changed to a named one here, for consistency we could specify hashprev as well:

            block = create_block(hashprev=int(self.nodes[0].getbestblockhash(), 16), height=self.nodes[0].getblockcount() + 1, txlist=txs_to_add)
    

    maflcko commented at 10:06 AM on April 16, 2026:

    Hmm, I think there is no risk here, because it is followed by coinbase, which is a different type. It being followed by coinbase (a positional arg) also means a scripted-diff can not be used here. So I think I'll leave this as-is for now?


    l0rinc commented at 10:40 AM on April 16, 2026:

    which is a different type

    You trust python more than I do :p


    maflcko commented at 12:01 PM on April 16, 2026:

    I am happy to push a new commit with a scripted-diff (or so), or review a follow-up :)

  9. theStack approved
  10. theStack commented at 9:16 AM on April 16, 2026: contributor

    Nice, LGTM ACK fa374715f6db30e35e657f071c3c09f6badd100e

  11. in test/functional/p2p_fingerprint.py:44 in fa374715f6 outdated
      43 |  
      44 |              blocks.append(block)
      45 |              prev_hash = block.hash_hex
      46 |              prev_height += 1
      47 | -            prev_median_time = block_time
      48 | +            prev_median_time += 1
    


    l0rinc commented at 9:36 AM on April 16, 2026:

    fa37471 test: Require named arg for create_block ntime arg:

    I'm fine with this as well, but the previous block_time = prev_median_time + 1 also made sense since it avoided duplicating the calculation of the prev


    maflcko commented at 10:06 AM on April 16, 2026:

    I just think that height and time should follow the same pattern. Every time I read this function, I spend too long thinking about this, because it mixes two patterns. No strong opinion, I just think inside this function the same patter should be used consistently.


    l0rinc commented at 10:41 AM on April 16, 2026:

    I understand, given that + 1 is a trivial calculation, it's also fine to duplicate it

  12. in test/functional/example_test.py:18 in fa4966214b outdated
      16 | @@ -17,7 +17,6 @@
      17 |  # Use lexicographically sorted multi-line imports
      18 |  from test_framework.blocktools import (
    


    l0rinc commented at 9:44 AM on April 16, 2026:

    fa49662 test: Remove unused create_coinbase imports:

    nit: since we're modifying these imports manually we might as well simplify them - in other cases a single create_block import is a one-liner.


    maflcko commented at 10:06 AM on April 16, 2026:

    I think either is fine. I think black will leave this as-is, whereas yapf prefers a single line. Not sure about ruff ... Generally, I think either both are acceptable (as-is now), or a linter should take care of this somehow, if it was important.

  13. l0rinc approved
  14. l0rinc commented at 9:47 AM on April 16, 2026: contributor

    tested ACK fa374715f6db30e35e657f071c3c09f6badd100e

    The positional vs named args are still a bit confusing here, but the situation is definitely better than before. I left a few suggestions, happy to re-ACK if you decide to take them.

  15. l0rinc approved
  16. test: Allow to set height in create_block
    Previously, it was only possible to set the height indirectly by calling
    create_coinbase outside the create_block function. This is fine, but
    verbose and not needed.
    
    Just like hashprev can be set directly (instead of using the value from
    tmpl), allow height to be set directly.
    
    Also, use it in one place. The other places are done in a scripted-diff.
    
    Also, add a unit test for the new feature.
    fa5eb74b96
  17. scripted-diff: Use new create_block height option
    -BEGIN VERIFY SCRIPT-
    
     # Replace single-arg create_coinbase calls ...
     # ... followed by ntime arg
     sed --in-place --regexp-extended 's/create_block\((.+), create_coinbase\(([^,]+)\), /create_block(\1, height=\2, ntime=/g' $( git grep -l 'create_block(' )
     # ... not followed by any other args
     sed --in-place --regexp-extended 's/create_block\((.+), create_coinbase\(([^,]+)\)\)/create_block(\1, height=\2)/g'        $( git grep -l 'create_block(' )
    
    -END VERIFY SCRIPT-
    fad6deb3cb
  18. test: Remove unused create_coinbase imports fab352053d
  19. test: Require named arg for create_block ntime arg
    The named arg is useful, so that the two integral args (possibly
    integral literals) `height` and `ntime` are not confused.
    fa16bc53d7
  20. maflcko force-pushed on Apr 16, 2026
  21. l0rinc commented at 12:22 PM on April 16, 2026: contributor

    reACK fa16bc53d79cb7fe9c39bc94d412730cadd19948

    Rebase + test for height fallback

  22. DrahtBot requested review from theStack on Apr 16, 2026
  23. theStack approved
  24. theStack commented at 12:54 PM on April 16, 2026: contributor

    re-ACK fa16bc53d79cb7fe9c39bc94d412730cadd19948

    (as per $ git range-diff fa374715...fa16bc53)

  25. DrahtBot removed the label CI failed on Apr 16, 2026
  26. sedited merged this on Apr 19, 2026
  27. sedited closed this on Apr 19, 2026

  28. maflcko deleted the branch on Apr 19, 2026

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-21 09:12 UTC

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