Skip to content

Commit 8d045a0

Browse files
committed
Merge #13003: qa: Add test for orphan handling
fa02c5b qa: Clarify documentation for send_txs_and_test (MarcoFalke) fadfbd3 qa: Add test for orphan handling (MarcoFalke) Pull request description: Tree-SHA512: e0932d6bd03c73e3113f5457a3ffa3bbfc7b6075dfca8de95224d9df875e60ca6eb15cd8baa226f13de965483006559556191630a83c3bb431e79c53a85ef73f
2 parents 826acc9 + fa02c5b commit 8d045a0

File tree

3 files changed

+116
-26
lines changed

3 files changed

+116
-26
lines changed

test/functional/feature_block.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,7 @@ def set_test_params(self):
8282
def run_test(self):
8383
node = self.nodes[0] # convenience reference to the node
8484

85-
# reconnect_p2p() expects the network thread to be running
86-
network_thread_start()
87-
88-
self.reconnect_p2p()
85+
self.bootstrap_p2p() # Add one p2p connection to the node
8986

9087
self.block_heights = {}
9188
self.coinbase_key = CECKey()
@@ -1296,14 +1293,10 @@ def update_block(self, block_number, new_transactions):
12961293
self.blocks[block_number] = block
12971294
return block
12981295

1299-
def reconnect_p2p(self):
1296+
def bootstrap_p2p(self):
13001297
"""Add a P2P connection to the node.
13011298
1302-
The node gets disconnected several times in this test. This helper
1303-
method reconnects the p2p and restarts the network thread."""
1304-
1305-
network_thread_join()
1306-
self.nodes[0].disconnect_p2ps()
1299+
Helper to connect and wait for version handshake."""
13071300
self.nodes[0].add_p2p_connection(P2PDataStore())
13081301
network_thread_start()
13091302
# We need to wait for the initial getheaders from the peer before we
@@ -1314,6 +1307,15 @@ def reconnect_p2p(self):
13141307
# unexpectedly disconnected if the DoS score for that error is 50.
13151308
self.nodes[0].p2p.wait_for_getheaders(timeout=5)
13161309

1310+
def reconnect_p2p(self):
1311+
"""Tear down and bootstrap the P2P connection to the node.
1312+
1313+
The node gets disconnected several times in this test. This helper
1314+
method reconnects the p2p and restarts the network thread."""
1315+
self.nodes[0].disconnect_p2ps()
1316+
network_thread_join()
1317+
self.bootstrap_p2p()
1318+
13171319
def sync_blocks(self, blocks, success=True, reject_code=None, reject_reason=None, request_block=True, reconnect=False, timeout=60):
13181320
"""Sends blocks to test node. Syncs and verifies that tip has advanced to most recent block.
13191321

test/functional/p2p_invalid_tx.py

Lines changed: 97 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,48 @@
66
77
In this test we connect to one node over p2p, and test tx requests."""
88
from test_framework.blocktools import create_block, create_coinbase, create_transaction
9-
from test_framework.messages import COIN
10-
from test_framework.mininode import network_thread_start, P2PDataStore
9+
from test_framework.messages import (
10+
COIN,
11+
COutPoint,
12+
CTransaction,
13+
CTxIn,
14+
CTxOut,
15+
)
16+
from test_framework.mininode import network_thread_start, P2PDataStore, network_thread_join
1117
from test_framework.test_framework import BitcoinTestFramework
18+
from test_framework.util import (
19+
assert_equal,
20+
wait_until,
21+
)
1222

13-
class InvalidTxRequestTest(BitcoinTestFramework):
1423

24+
class InvalidTxRequestTest(BitcoinTestFramework):
1525
def set_test_params(self):
1626
self.num_nodes = 1
1727
self.setup_clean_chain = True
18-
self.extra_args = [["-whitelist=127.0.0.1"]]
28+
29+
def bootstrap_p2p(self, *, num_connections=1):
30+
"""Add a P2P connection to the node.
31+
32+
Helper to connect and wait for version handshake."""
33+
for _ in range(num_connections):
34+
self.nodes[0].add_p2p_connection(P2PDataStore())
35+
network_thread_start()
36+
self.nodes[0].p2p.wait_for_verack()
37+
38+
def reconnect_p2p(self, **kwargs):
39+
"""Tear down and bootstrap the P2P connection to the node.
40+
41+
The node gets disconnected several times in this test. This helper
42+
method reconnects the p2p and restarts the network thread."""
43+
self.nodes[0].disconnect_p2ps()
44+
network_thread_join()
45+
self.bootstrap_p2p(**kwargs)
1946

2047
def run_test(self):
21-
# Add p2p connection to node0
2248
node = self.nodes[0] # convenience reference to the node
23-
node.add_p2p_connection(P2PDataStore())
2449

25-
network_thread_start()
26-
node.p2p.wait_for_verack()
50+
self.bootstrap_p2p() # Add one p2p connection to the node
2751

2852
best_block = self.nodes[0].getbestblockhash()
2953
tip = int(best_block, 16)
@@ -44,12 +68,73 @@ def run_test(self):
4468

4569
# b'\x64' is OP_NOTIF
4670
# Transaction will be rejected with code 16 (REJECT_INVALID)
71+
# and we get disconnected immediately
72+
self.log.info('Test a transaction that is rejected')
4773
tx1 = create_transaction(block1.vtx[0], 0, b'\x64', 50 * COIN - 12000)
48-
node.p2p.send_txs_and_test([tx1], node, success=False, reject_code=16, reject_reason=b'mandatory-script-verify-flag-failed (Invalid OP_IF construction)')
74+
node.p2p.send_txs_and_test([tx1], node, success=False, expect_disconnect=True)
75+
76+
# Make two p2p connections to provide the node with orphans
77+
# * p2ps[0] will send valid orphan txs (one with low fee)
78+
# * p2ps[1] will send an invalid orphan tx (and is later disconnected for that)
79+
self.reconnect_p2p(num_connections=2)
80+
81+
self.log.info('Test orphan transaction handling ... ')
82+
# Create a root transaction that we withold until all dependend transactions
83+
# are sent out and in the orphan cache
84+
tx_withhold = CTransaction()
85+
tx_withhold.vin.append(CTxIn(outpoint=COutPoint(block1.vtx[0].sha256, 0)))
86+
tx_withhold.vout.append(CTxOut(nValue=50 * COIN - 12000, scriptPubKey=b'\x51'))
87+
tx_withhold.calc_sha256()
88+
89+
# Our first orphan tx with some outputs to create further orphan txs
90+
tx_orphan_1 = CTransaction()
91+
tx_orphan_1.vin.append(CTxIn(outpoint=COutPoint(tx_withhold.sha256, 0)))
92+
tx_orphan_1.vout = [CTxOut(nValue=10 * COIN, scriptPubKey=b'\x51')] * 3
93+
tx_orphan_1.calc_sha256()
94+
95+
# A valid transaction with low fee
96+
tx_orphan_2_no_fee = CTransaction()
97+
tx_orphan_2_no_fee.vin.append(CTxIn(outpoint=COutPoint(tx_orphan_1.sha256, 0)))
98+
tx_orphan_2_no_fee.vout.append(CTxOut(nValue=10 * COIN, scriptPubKey=b'\x51'))
99+
100+
# A valid transaction with sufficient fee
101+
tx_orphan_2_valid = CTransaction()
102+
tx_orphan_2_valid.vin.append(CTxIn(outpoint=COutPoint(tx_orphan_1.sha256, 1)))
103+
tx_orphan_2_valid.vout.append(CTxOut(nValue=10 * COIN - 12000, scriptPubKey=b'\x51'))
104+
tx_orphan_2_valid.calc_sha256()
105+
106+
# An invalid transaction with negative fee
107+
tx_orphan_2_invalid = CTransaction()
108+
tx_orphan_2_invalid.vin.append(CTxIn(outpoint=COutPoint(tx_orphan_1.sha256, 2)))
109+
tx_orphan_2_invalid.vout.append(CTxOut(nValue=11 * COIN, scriptPubKey=b'\x51'))
110+
111+
self.log.info('Send the orphans ... ')
112+
# Send valid orphan txs from p2ps[0]
113+
node.p2p.send_txs_and_test([tx_orphan_1, tx_orphan_2_no_fee, tx_orphan_2_valid], node, success=False)
114+
# Send invalid tx from p2ps[1]
115+
node.p2ps[1].send_txs_and_test([tx_orphan_2_invalid], node, success=False)
116+
117+
assert_equal(0, node.getmempoolinfo()['size']) # Mempool should be empty
118+
assert_equal(2, len(node.getpeerinfo())) # p2ps[1] is still connected
119+
120+
self.log.info('Send the withhold tx ... ')
121+
node.p2p.send_txs_and_test([tx_withhold], node, success=True)
122+
123+
# Transactions that should end up in the mempool
124+
expected_mempool = {
125+
t.hash
126+
for t in [
127+
tx_withhold, # The transaction that is the root for all orphans
128+
tx_orphan_1, # The orphan transaction that splits the coins
129+
tx_orphan_2_valid, # The valid transaction (with sufficient fee)
130+
]
131+
}
132+
# Transactions that do not end up in the mempool
133+
# tx_orphan_no_fee, because it has too low fee (p2ps[0] is not disconnected for relaying that tx)
134+
# tx_orphan_invaid, because it has negative fee (p2ps[1] is disconnected for relaying that tx)
49135

50-
# Verify valid transaction
51-
tx1 = create_transaction(block1.vtx[0], 0, b'', 50 * COIN - 12000)
52-
node.p2p.send_txs_and_test([tx1], node, success=True)
136+
wait_until(lambda: 1 == len(node.getpeerinfo()), timeout=12) # p2ps[1] is no longer connected
137+
assert_equal(expected_mempool, set(node.getrawmempool()))
53138

54139

55140
if __name__ == '__main__':

test/functional/test_framework/mininode.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -554,13 +554,13 @@ def send_blocks_and_test(self, blocks, rpc, success=True, request_block=True, re
554554
if reject_reason is not None:
555555
wait_until(lambda: self.reject_reason_received == reject_reason, lock=mininode_lock)
556556

557-
def send_txs_and_test(self, txs, rpc, success=True, reject_code=None, reject_reason=None):
557+
def send_txs_and_test(self, txs, rpc, success=True, expect_disconnect=False, reject_code=None, reject_reason=None):
558558
"""Send txs to test node and test whether they're accepted to the mempool.
559559
560560
- add all txs to our tx_store
561561
- send tx messages for all txs
562-
- if success is True: assert that the tx is accepted to the mempool
563-
- if success is False: assert that the tx is not accepted to the mempool
562+
- if success is True/False: assert that the txs are/are not accepted to the mempool
563+
- if expect_disconnect is True: Skip the sync with ping
564564
- if reject_code and reject_reason are set: assert that the correct reject message is received."""
565565

566566
with mininode_lock:
@@ -573,7 +573,10 @@ def send_txs_and_test(self, txs, rpc, success=True, reject_code=None, reject_rea
573573
for tx in txs:
574574
self.send_message(msg_tx(tx))
575575

576-
self.sync_with_ping()
576+
if expect_disconnect:
577+
self.wait_for_disconnect()
578+
else:
579+
self.sync_with_ping()
577580

578581
raw_mempool = rpc.getrawmempool()
579582
if success:

0 commit comments

Comments
 (0)