test: Use extra_port() helper in feature_bind_extra.py #33260

pull maflcko wants to merge 1 commits into bitcoin:master from maflcko:2508-test-extra-port-doc changing 1 files +12 −8
  1. maflcko commented at 7:48 am on August 27, 2025: member

    This is a refactor for self-validating and self-documenting code.

    Currently, the test assumes that extra ports are available and just increments them without checking. However, this may not be the case when the test is modified to use more ports. In this case, the tests may fail intermittently and the failure is hard to debug.

    Fix this confusion, by calling p2p_port each time. This ensures the required assert n <= MAX_NODES is checked each time.

    Closes https://github.com/bitcoin/bitcoin/issues/33250

  2. test: Use extra_port() helper in feature_bind_extra.py fabc2615af
  3. DrahtBot added the label Tests on Aug 27, 2025
  4. DrahtBot commented at 7:48 am on August 27, 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/33260.

    Reviews

    See the guideline for information on the review process.

    Type Reviewers
    ACK janb84, w0xlt
    Concept ACK l0rinc

    If your review is incorrectly listed, please react with 👎 to this comment and the bot will ignore it on the next update.

  5. in test/functional/feature_bind_extra.py:40 in fabc2615af
    36@@ -37,37 +37,41 @@ def setup_network(self):
    37         loopback_ipv4 = addr_to_hex("127.0.0.1")
    38 
    39         # Start custom ports by reusing unused p2p ports
    40-        port = p2p_port(self.num_nodes)
    41+        def extra_port():
    


    l0rinc commented at 4:31 pm on August 27, 2025:
    @w0xlt was also modifying these in a different PR, I also considered a similar solution, but suggested using an iterator and lambdas instead #33231 (review)

    maflcko commented at 7:47 am on August 28, 2025:

    iterator

    That doesn’t work, because it is just what the current code does: Take a port and +=1 it. The goal of this pull request is to properly derive (and check) each port by its own.


    janb84 commented at 10:09 am on August 28, 2025:
    0        def new_port():
    

    NIT; wouldn’t new_port() be more suitable ? line 50 is just the use of a new port not an extra port. “extra to what?”


    maflcko commented at 10:32 am on August 28, 2025:
    It is “extra” on top of the normal node p2p ports, so I’ll leave this as-is for now.
  6. in test/functional/feature_bind_extra.py:50 in fabc2615af
    46 
    47         # Array of tuples [command line arguments, expected bind addresses].
    48         self.expected = []
    49 
    50         # Node0, no normal -bind=... with -bind=...=onion, thus only the tor target.
    51+        port = extra_port()
    


    l0rinc commented at 4:33 pm on August 27, 2025:
    I find these reassignments confusing - especially if it contains two ports now. I think we can get rid of that by using lambdas here, which can provide two separate ports as inputs and the loop would generate the new ports

    maflcko commented at 7:48 am on August 28, 2025:
    thx, but I want to keep the changes here minimal, not rewrite the whole test

    l0rinc commented at 11:43 pm on August 29, 2025:
    We’re introducing extra confusion here now with assigning different types to the same variable. I’m not suggesting a “rewrite”, just to make it better than it was before - which it mostly is, hence the concept ack from me.

    maflcko commented at 7:36 am on September 1, 2025:
    No strong opinion on the confusion. Happy to review a follow-up, or separate pull, or push a commit here, but I’ll hold back on this for now, to not invalidate 2 acks.
  7. l0rinc commented at 4:34 pm on August 27, 2025: contributor
    Concept ACK, please see my alternative suggestion which would get rid of variable reuse and would solve the port iteration in a more portable way
  8. w0xlt commented at 6:05 pm on August 27, 2025: contributor
    Concept ACK
  9. janb84 commented at 10:17 am on August 28, 2025: contributor

    crACK fabc2615af26c61a503f23ae4fd0353f90602bbe

    PR adds extra function that returns a port. In this new function the utility function p2p_port(n) is called, which does a basic check and returns a (pseudo) random port number. This to reduce errors of using port numbers that are unavailable.

    This looks like a simple solution without to much churn to solve #33250

  10. DrahtBot requested review from l0rinc on Aug 28, 2025
  11. DrahtBot requested review from w0xlt on Aug 28, 2025
  12. w0xlt commented at 11:36 pm on August 29, 2025: contributor

    ACK fabc2615af26c61a503f23ae4fd0353f90602bbe

    Adding the patch below can confirm that the condition described in #33250 (comment) is reached.

     0diff --git a/test/functional/feature_bind_extra.py b/test/functional/feature_bind_extra.py
     1index 7dfa9c06ee..48b31092d2 100755
     2--- a/test/functional/feature_bind_extra.py
     3+++ b/test/functional/feature_bind_extra.py
     4@@ -27,7 +27,7 @@ class BindExtraTest(BitcoinTestFramework):
     5         # Avoid any -bind= on the command line. Force the framework to avoid
     6         # adding -bind=127.0.0.1.
     7         self.bind_to_localhost_only = False
     8-        self.num_nodes = 3
     9+        self.num_nodes = 6
    10 
    11     def skip_test_if_missing_module(self):
    12         # Due to OS-specific network stats queries, we only run on Linux.
    13@@ -73,6 +73,33 @@ class BindExtraTest(BitcoinTestFramework):
    14             ],
    15         )
    16 
    17+        # Node1, both -bind=... and -bind=...=onion.
    18+        port = [extra_port(), extra_port()]
    19+        self.expected.append(
    20+            [
    21+                [f"-bind=127.0.0.1:{port[0]}", f"-bind=127.0.0.1:{port[1]}=onion"],
    22+                [(loopback_ipv4, port[0]), (loopback_ipv4, port[1])],
    23+            ],
    24+        )
    25+
    26+        # Node1, both -bind=... and -bind=...=onion.
    27+        port = [extra_port(), extra_port()]
    28+        self.expected.append(
    29+            [
    30+                [f"-bind=127.0.0.1:{port[0]}", f"-bind=127.0.0.1:{port[1]}=onion"],
    31+                [(loopback_ipv4, port[0]), (loopback_ipv4, port[1])],
    32+
    33+        # Node1, both -bind=... and -bind=...=onion.
    34+        port = [extra_port(), extra_port()]
    35+        self.expected.append(
    36+            [
    37+                [f"-bind=127.0.0.1:{port[0]}", f"-bind=127.0.0.1:{port[1]}=onion"],
    38+                [(loopback_ipv4, port[0]), (loopback_ipv4, port[1])],
    
  13. in test/functional/feature_bind_extra.py:44 in fabc2615af
    36@@ -37,37 +37,41 @@ def setup_network(self):
    37         loopback_ipv4 = addr_to_hex("127.0.0.1")
    38 
    39         # Start custom ports by reusing unused p2p ports
    40-        port = p2p_port(self.num_nodes)
    41+        def extra_port():
    42+            port = p2p_port(extra_port.index)
    43+            extra_port.index += 1
    44+            return port
    45+        extra_port.index = self.num_nodes
    


    w0xlt commented at 11:42 pm on August 29, 2025:

    If you apply the suggestion below, the patch above will work. Is there a reason why the starting port index should be the same as the number of nodes?

    0        extra_port.index = 0
    

    maflcko commented at 7:36 am on September 1, 2025:
    I guess the nodes aren’t connected, so they don’t use the p2p ports and they can be re-used here. No strong opinion, but this should be a separate commit with proper documentation.

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

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