Skip to content

Commit ad1820c

Browse files
committed
Merge #11849: [tests] Assert that only one NetworkThread exists
5c8ff26 [tests] Add NetworkThread assertions (John Newbery) 34e08b3 [tests] Fix network threading in functional tests (John Newbery) 74e64f2 [tests] Use network_thread_start() in tests. (John Newbery) 5fc6e71 [tests] Add network_thread_ utility functions. (John Newbery) Pull request description: Add assert that only one NetworkThread exists at any time in functional tests, and fix cases where that wasn't true. fixes #11776 Tree-SHA512: fe5d1c59005f94bf66e11bb23ccf274b1cd9913741b56ea11dbcd21db4cc0b53b4413c0c4c16dbcd6ac611adad5e5cc2baaa39720598ce7b6393889945d06298
2 parents d48ab83 + 5c8ff26 commit ad1820c

23 files changed

+96
-42
lines changed

test/functional/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ contains the higher level logic for processing P2P payloads and connecting to
6868
the Bitcoin Core node application logic. For custom behaviour, subclass the
6969
P2PInterface object and override the callback methods.
7070

71-
- Call `NetworkThread.start()` after all `P2PInterface` objects are created to
71+
- Call `network_thread_start()` after all `P2PInterface` objects are created to
7272
start the networking thread. (Continue with the test logic in your existing
7373
thread.)
7474

test/functional/assumevalid.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@
3838
CTransaction,
3939
CTxIn,
4040
CTxOut,
41-
NetworkThread,
41+
network_thread_join,
42+
network_thread_start,
4243
P2PInterface,
4344
msg_block,
4445
msg_headers)
@@ -98,7 +99,7 @@ def run_test(self):
9899
# Connect to node0
99100
p2p0 = self.nodes[0].add_p2p_connection(BaseNode())
100101

101-
NetworkThread().start() # Start up network handling in another thread
102+
network_thread_start()
102103
self.nodes[0].p2p.wait_for_verack()
103104

104105
# Build the blockchain
@@ -159,13 +160,22 @@ def run_test(self):
159160
self.block_time += 1
160161
height += 1
161162

163+
# We're adding new connections so terminate the network thread
164+
self.nodes[0].disconnect_p2ps()
165+
network_thread_join()
166+
162167
# Start node1 and node2 with assumevalid so they accept a block with a bad signature.
163168
self.start_node(1, extra_args=["-assumevalid=" + hex(block102.sha256)])
164-
p2p1 = self.nodes[1].add_p2p_connection(BaseNode())
165-
p2p1.wait_for_verack()
166-
167169
self.start_node(2, extra_args=["-assumevalid=" + hex(block102.sha256)])
170+
171+
p2p0 = self.nodes[0].add_p2p_connection(BaseNode())
172+
p2p1 = self.nodes[1].add_p2p_connection(BaseNode())
168173
p2p2 = self.nodes[2].add_p2p_connection(BaseNode())
174+
175+
network_thread_start()
176+
177+
p2p0.wait_for_verack()
178+
p2p1.wait_for_verack()
169179
p2p2.wait_for_verack()
170180

171181
# send header lists to all three nodes

test/functional/bip65-cltv-p2p.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def set_test_params(self):
6868
def run_test(self):
6969
self.nodes[0].add_p2p_connection(P2PInterface())
7070

71-
NetworkThread().start() # Start up network handling in another thread
71+
network_thread_start()
7272

7373
# wait_for_verack ensures that the P2P connection is fully up.
7474
self.nodes[0].p2p.wait_for_verack()

test/functional/bip68-112-113-p2p.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545

4646
from test_framework.test_framework import ComparisonTestFramework
4747
from test_framework.util import *
48-
from test_framework.mininode import ToHex, CTransaction, NetworkThread
48+
from test_framework.mininode import ToHex, CTransaction, network_thread_start
4949
from test_framework.blocktools import create_coinbase, create_block
5050
from test_framework.comptool import TestInstance, TestManager
5151
from test_framework.script import *
@@ -100,7 +100,7 @@ def set_test_params(self):
100100
def run_test(self):
101101
test = TestManager(self, self.options.tmpdir)
102102
test.add_all_connections(self.nodes)
103-
NetworkThread().start() # Start up network handling in another thread
103+
network_thread_start()
104104
test.run()
105105

106106
def send_generic_input_tx(self, node, coinbases):

test/functional/bip9-softforks.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
from test_framework.test_framework import ComparisonTestFramework
2424
from test_framework.util import *
25-
from test_framework.mininode import CTransaction, NetworkThread
25+
from test_framework.mininode import CTransaction, network_thread_start
2626
from test_framework.blocktools import create_coinbase, create_block
2727
from test_framework.comptool import TestInstance, TestManager
2828
from test_framework.script import CScript, OP_1NEGATE, OP_CHECKSEQUENCEVERIFY, OP_DROP
@@ -36,7 +36,7 @@ def set_test_params(self):
3636
def run_test(self):
3737
self.test = TestManager(self, self.options.tmpdir)
3838
self.test.add_all_connections(self.nodes)
39-
NetworkThread().start() # Start up network handling in another thread
39+
network_thread_start()
4040
self.test.run()
4141

4242
def create_transaction(self, node, coinbase, to_address, amount):
@@ -245,7 +245,7 @@ def test_BIP(self, bipName, activated_version, invalidate, invalidatePostSignatu
245245
self.setup_chain()
246246
self.setup_network()
247247
self.test.add_all_connections(self.nodes)
248-
NetworkThread().start()
248+
network_thread_start()
249249
self.test.p2p_connections[0].wait_for_verack()
250250

251251
def get_tests(self):

test/functional/bipdersig-p2p.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def set_test_params(self):
5656
def run_test(self):
5757
self.nodes[0].add_p2p_connection(P2PInterface())
5858

59-
NetworkThread().start() # Start up network handling in another thread
59+
network_thread_start()
6060

6161
# wait_for_verack ensures that the P2P connection is fully up.
6262
self.nodes[0].p2p.wait_for_verack()

test/functional/example_test.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,12 @@
1717
from test_framework.blocktools import (create_block, create_coinbase)
1818
from test_framework.mininode import (
1919
CInv,
20-
NetworkThread,
2120
P2PInterface,
2221
mininode_lock,
2322
msg_block,
2423
msg_getdata,
24+
network_thread_join,
25+
network_thread_start,
2526
)
2627
from test_framework.test_framework import BitcoinTestFramework
2728
from test_framework.util import (
@@ -131,12 +132,12 @@ def custom_method(self):
131132
def run_test(self):
132133
"""Main test logic"""
133134

134-
# Create a P2P connection to one of the nodes
135+
# Create P2P connections to two of the nodes
135136
self.nodes[0].add_p2p_connection(BaseNode())
136137

137138
# Start up network handling in another thread. This needs to be called
138139
# after the P2P connections have been created.
139-
NetworkThread().start()
140+
network_thread_start()
140141
# wait_for_verack ensures that the P2P connection is fully up.
141142
self.nodes[0].p2p.wait_for_verack()
142143

@@ -188,7 +189,14 @@ def run_test(self):
188189
connect_nodes(self.nodes[1], 2)
189190

190191
self.log.info("Add P2P connection to node2")
192+
# We can't add additional P2P connections once the network thread has started. Disconnect the connection
193+
# to node0, wait for the network thread to terminate, then connect to node2. This is specific to
194+
# the current implementation of the network thread and may be improved in future.
195+
self.nodes[0].disconnect_p2ps()
196+
network_thread_join()
197+
191198
self.nodes[2].add_p2p_connection(BaseNode())
199+
network_thread_start()
192200
self.nodes[2].p2p.wait_for_verack()
193201

194202
self.log.info("Wait for node2 reach current tip. Test that it has propagated all the blocks to us")

test/functional/invalidblockrequest.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from test_framework.util import *
1616
from test_framework.comptool import TestManager, TestInstance, RejectResult
1717
from test_framework.blocktools import *
18+
from test_framework.mininode import network_thread_start
1819
import copy
1920
import time
2021

@@ -32,7 +33,7 @@ def run_test(self):
3233
test.add_all_connections(self.nodes)
3334
self.tip = None
3435
self.block_time = None
35-
NetworkThread().start() # Start up network handling in another thread
36+
network_thread_start()
3637
test.run()
3738

3839
def get_tests(self):

test/functional/invalidtxrequest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def run_test(self):
2828
test.add_all_connections(self.nodes)
2929
self.tip = None
3030
self.block_time = None
31-
NetworkThread().start() # Start up network handling in another thread
31+
network_thread_start()
3232
test.run()
3333

3434
def get_tests(self):

test/functional/maxuploadtarget.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def run_test(self):
5757
for _ in range(3):
5858
p2p_conns.append(self.nodes[0].add_p2p_connection(TestNode()))
5959

60-
NetworkThread().start() # Start up network handling in another thread
60+
network_thread_start()
6161
for p2pc in p2p_conns:
6262
p2pc.wait_for_verack()
6363

@@ -149,7 +149,7 @@ def run_test(self):
149149
# Reconnect to self.nodes[0]
150150
self.nodes[0].add_p2p_connection(TestNode())
151151

152-
NetworkThread().start() # Start up network handling in another thread
152+
network_thread_start()
153153
self.nodes[0].p2p.wait_for_verack()
154154

155155
#retrieve 20 blocks which should be enough to break the 1MB limit

0 commit comments

Comments
 (0)