Skip to content

Commit a6ed99a

Browse files
committed
Merge #13517: qa: Remove need to handle the network thread in tests
fa87da2 qa: Avoid start/stop of the network thread mid-test (MarcoFalke) Pull request description: This simplifies test writing by removing the need to handle the network thread in tests. E.g. start thread, join thread, restart thread mid-test, adding p2p connections at the "right" time, ... Tree-SHA512: 533642f12fef5496f1933855edcdab1a7ed901d088d34911749cd0f9e044c8a6cb1f89985ac3a7f41a512943663e4e270a61978f6f072143ae050cd102d4eab8
2 parents f3c9c40 + fa87da2 commit a6ed99a

26 files changed

+98
-223
lines changed

test/functional/README.md

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,18 +76,14 @@ over the network (`CBlock`, `CTransaction`, etc, along with the network-level
7676
wrappers for them, `msg_block`, `msg_tx`, etc).
7777

7878
- P2P tests have two threads. One thread handles all network communication
79-
with the bitcoind(s) being tested (using python's asyncore package); the other
79+
with the bitcoind(s) being tested in a callback-based event loop; the other
8080
implements the test logic.
8181

8282
- `P2PConnection` is the class used to connect to a bitcoind. `P2PInterface`
8383
contains the higher level logic for processing P2P payloads and connecting to
8484
the Bitcoin Core node application logic. For custom behaviour, subclass the
8585
P2PInterface object and override the callback methods.
8686

87-
- Call `network_thread_start()` after all `P2PInterface` objects are created to
88-
start the networking thread. (Continue with the test logic in your existing
89-
thread.)
90-
9187
- Can be used to write tests where specific P2P protocol behavior is tested.
9288
Examples tests are `p2p_unrequested_blocks.py`, `p2p_compactblocks.py`.
9389

test/functional/example_test.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121
mininode_lock,
2222
msg_block,
2323
msg_getdata,
24-
network_thread_join,
25-
network_thread_start,
2624
)
2725
from test_framework.test_framework import BitcoinTestFramework
2826
from test_framework.util import (
@@ -135,9 +133,6 @@ def run_test(self):
135133
# Create P2P connections to two of the nodes
136134
self.nodes[0].add_p2p_connection(BaseNode())
137135

138-
# Start up network handling in another thread. This needs to be called
139-
# after the P2P connections have been created.
140-
network_thread_start()
141136
# wait_for_verack ensures that the P2P connection is fully up.
142137
self.nodes[0].p2p.wait_for_verack()
143138

@@ -189,14 +184,9 @@ def run_test(self):
189184
connect_nodes(self.nodes[1], 2)
190185

191186
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.
195187
self.nodes[0].disconnect_p2ps()
196-
network_thread_join()
197188

198189
self.nodes[2].add_p2p_connection(BaseNode())
199-
network_thread_start()
200190
self.nodes[2].p2p.wait_for_verack()
201191

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

test/functional/feature_assumevalid.py

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,16 @@
3333

3434
from test_framework.blocktools import (create_block, create_coinbase)
3535
from test_framework.key import CECKey
36-
from test_framework.mininode import (CBlockHeader,
37-
COutPoint,
38-
CTransaction,
39-
CTxIn,
40-
CTxOut,
41-
network_thread_join,
42-
network_thread_start,
43-
P2PInterface,
44-
msg_block,
45-
msg_headers)
36+
from test_framework.messages import (
37+
CBlockHeader,
38+
COutPoint,
39+
CTransaction,
40+
CTxIn,
41+
CTxOut,
42+
msg_block,
43+
msg_headers
44+
)
45+
from test_framework.mininode import P2PInterface
4646
from test_framework.script import (CScript, OP_TRUE)
4747
from test_framework.test_framework import BitcoinTestFramework
4848
from test_framework.util import assert_equal
@@ -98,8 +98,6 @@ def run_test(self):
9898

9999
# Connect to node0
100100
p2p0 = self.nodes[0].add_p2p_connection(BaseNode())
101-
102-
network_thread_start()
103101
self.nodes[0].p2p.wait_for_verack()
104102

105103
# Build the blockchain
@@ -160,9 +158,7 @@ def run_test(self):
160158
self.block_time += 1
161159
height += 1
162160

163-
# We're adding new connections so terminate the network thread
164161
self.nodes[0].disconnect_p2ps()
165-
network_thread_join()
166162

167163
# Start node1 and node2 with assumevalid so they accept a block with a bad signature.
168164
self.start_node(1, extra_args=["-assumevalid=" + hex(block102.sha256)])
@@ -172,8 +168,6 @@ def run_test(self):
172168
p2p1 = self.nodes[1].add_p2p_connection(BaseNode())
173169
p2p2 = self.nodes[2].add_p2p_connection(BaseNode())
174170

175-
network_thread_start()
176-
177171
p2p0.wait_for_verack()
178172
p2p1.wait_for_verack()
179173
p2p2.wait_for_verack()

test/functional/feature_block.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
uint256_from_compact,
2121
uint256_from_str,
2222
)
23-
from test_framework.mininode import P2PDataStore, network_thread_start, network_thread_join
23+
from test_framework.mininode import P2PDataStore
2424
from test_framework.script import (
2525
CScript,
2626
MAX_SCRIPT_ELEMENT_SIZE,
@@ -1299,7 +1299,6 @@ def bootstrap_p2p(self):
12991299
13001300
Helper to connect and wait for version handshake."""
13011301
self.nodes[0].add_p2p_connection(P2PDataStore())
1302-
network_thread_start()
13031302
# We need to wait for the initial getheaders from the peer before we
13041303
# start populating our blockstore. If we don't, then we may run ahead
13051304
# to the next subtest before we receive the getheaders. We'd then send
@@ -1314,7 +1313,6 @@ def reconnect_p2p(self):
13141313
The node gets disconnected several times in this test. This helper
13151314
method reconnects the p2p and restarts the network thread."""
13161315
self.nodes[0].disconnect_p2ps()
1317-
network_thread_join()
13181316
self.bootstrap_p2p()
13191317

13201318
def sync_blocks(self, blocks, success=True, reject_code=None, reject_reason=None, request_block=True, reconnect=False, timeout=60):

test/functional/feature_cltv.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,6 @@ def set_test_params(self):
6767

6868
def run_test(self):
6969
self.nodes[0].add_p2p_connection(P2PInterface())
70-
71-
network_thread_start()
72-
73-
# wait_for_verack ensures that the P2P connection is fully up.
7470
self.nodes[0].p2p.wait_for_verack()
7571

7672
self.log.info("Mining %d blocks", CLTV_HEIGHT - 2)

test/functional/feature_csv_activation.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949

5050
from test_framework.blocktools import create_coinbase, create_block
5151
from test_framework.messages import ToHex, CTransaction
52-
from test_framework.mininode import network_thread_start, P2PDataStore
52+
from test_framework.mininode import P2PDataStore
5353
from test_framework.script import (
5454
CScript,
5555
OP_CHECKSEQUENCEVERIFY,
@@ -183,7 +183,6 @@ def sync_blocks(self, blocks, success=True, reject_code=None, reject_reason=None
183183

184184
def run_test(self):
185185
self.nodes[0].add_p2p_connection(P2PDataStore())
186-
network_thread_start()
187186
self.nodes[0].p2p.wait_for_verack()
188187

189188
self.log.info("Generate blocks in the past for coinbase outputs.")

test/functional/feature_dersig.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,6 @@ def set_test_params(self):
5656
def run_test(self):
5757
self.nodes[0].add_p2p_connection(P2PInterface())
5858

59-
network_thread_start()
60-
6159
# wait_for_verack ensures that the P2P connection is fully up.
6260
self.nodes[0].p2p.wait_for_verack()
6361

test/functional/feature_maxuploadtarget.py

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

60-
network_thread_start()
6160
for p2pc in p2p_conns:
6261
p2pc.wait_for_verack()
6362

@@ -148,8 +147,6 @@ def run_test(self):
148147

149148
# Reconnect to self.nodes[0]
150149
self.nodes[0].add_p2p_connection(TestP2PConn())
151-
152-
network_thread_start()
153150
self.nodes[0].p2p.wait_for_verack()
154151

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

test/functional/feature_nulldummy.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
from test_framework.test_framework import BitcoinTestFramework
1717
from test_framework.util import *
18-
from test_framework.mininode import CTransaction, network_thread_start
18+
from test_framework.messages import CTransaction
1919
from test_framework.blocktools import create_coinbase, create_block, add_witness_commitment
2020
from test_framework.script import CScript
2121
from io import BytesIO
@@ -50,7 +50,6 @@ def run_test(self):
5050
self.wit_address = self.nodes[0].addwitnessaddress(self.address)
5151
self.wit_ms_address = self.nodes[0].addmultisigaddress(1, [self.address], '', 'p2sh-segwit')['address']
5252

53-
network_thread_start()
5453
self.coinbase_blocks = self.nodes[0].generate(2) # Block 2
5554
coinbase_txid = []
5655
for i in self.coinbase_blocks:

test/functional/feature_versionbits_warning.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
from test_framework.blocktools import create_block, create_coinbase
1414
from test_framework.messages import msg_block
15-
from test_framework.mininode import P2PInterface, network_thread_start, mininode_lock
15+
from test_framework.mininode import P2PInterface, mininode_lock
1616
from test_framework.test_framework import BitcoinTestFramework
1717
from test_framework.util import wait_until
1818

@@ -65,7 +65,6 @@ def run_test(self):
6565
# Handy alias
6666
node = self.nodes[0]
6767
node.add_p2p_connection(P2PInterface())
68-
network_thread_start()
6968
node.p2p.wait_for_verack()
7069

7170
# Mine one period worth of blocks

0 commit comments

Comments
 (0)