From 9e2897d020b114a10c860f90c5405be029afddba Mon Sep 17 00:00:00 2001 From: John Newbery Date: Sun, 19 Jul 2020 14:47:05 +0700 Subject: scripted-diff: Rename mininode_lock to p2p_lock -BEGIN VERIFY SCRIPT- sed -i 's/mininode_lock/p2p_lock/g' $(git grep -l "mininode_lock") -END VERIFY SCRIPT- --- test/functional/example_test.py | 6 +- test/functional/feature_versionbits_warning.py | 4 +- test/functional/p2p_compactblocks.py | 68 +++++++++++----------- test/functional/p2p_feefilter.py | 6 +- test/functional/p2p_filter.py | 10 ++-- test/functional/p2p_getaddr_caching.py | 4 +- test/functional/p2p_leak.py | 8 +-- test/functional/p2p_node_network_limited.py | 4 +- test/functional/p2p_segwit.py | 18 +++--- test/functional/p2p_sendheaders.py | 28 ++++----- test/functional/p2p_tx_download.py | 14 ++--- test/functional/p2p_unrequested_blocks.py | 6 +- test/functional/test_framework/mininode.py | 12 ++-- test/functional/wallet_resendwallettransactions.py | 6 +- 14 files changed, 97 insertions(+), 97 deletions(-) (limited to 'test') diff --git a/test/functional/example_test.py b/test/functional/example_test.py index 34e4999329..c997e47c5e 100755 --- a/test/functional/example_test.py +++ b/test/functional/example_test.py @@ -18,7 +18,7 @@ from test_framework.blocktools import (create_block, create_coinbase) from test_framework.messages import CInv, MSG_BLOCK from test_framework.mininode import ( P2PInterface, - mininode_lock, + p2p_lock, msg_block, msg_getdata, ) @@ -203,13 +203,13 @@ class ExampleTest(BitcoinTestFramework): # wait_until() will loop until a predicate condition is met. Use it to test properties of the # P2PInterface objects. - wait_until(lambda: sorted(blocks) == sorted(list(self.nodes[2].p2p.block_receive_map.keys())), timeout=5, lock=mininode_lock) + wait_until(lambda: sorted(blocks) == sorted(list(self.nodes[2].p2p.block_receive_map.keys())), timeout=5, lock=p2p_lock) self.log.info("Check that each block was received only once") # The network thread uses a global lock on data access to the P2PConnection objects when sending and receiving # messages. The test thread should acquire the global lock before accessing any P2PConnection data to avoid locking # and synchronization issues. Note wait_until() acquires this global lock when testing the predicate. - with mininode_lock: + with p2p_lock: for block in self.nodes[2].p2p.block_receive_map.values(): assert_equal(block, 1) diff --git a/test/functional/feature_versionbits_warning.py b/test/functional/feature_versionbits_warning.py index 0713925141..b99a6220fe 100755 --- a/test/functional/feature_versionbits_warning.py +++ b/test/functional/feature_versionbits_warning.py @@ -12,7 +12,7 @@ import re from test_framework.blocktools import create_block, create_coinbase from test_framework.messages import msg_block -from test_framework.mininode import P2PInterface, mininode_lock +from test_framework.mininode import P2PInterface, p2p_lock from test_framework.test_framework import BitcoinTestFramework from test_framework.util import wait_until @@ -91,7 +91,7 @@ class VersionBitsWarningTest(BitcoinTestFramework): # Generating one block guarantees that we'll get out of IBD node.generatetoaddress(1, node_deterministic_address) - wait_until(lambda: not node.getblockchaininfo()['initialblockdownload'], timeout=10, lock=mininode_lock) + wait_until(lambda: not node.getblockchaininfo()['initialblockdownload'], timeout=10, lock=p2p_lock) # Generating one more block will be enough to generate an error. node.generatetoaddress(1, node_deterministic_address) # Check that get*info() shows the versionbits unknown rules warning diff --git a/test/functional/p2p_compactblocks.py b/test/functional/p2p_compactblocks.py index 225d393e1b..9a85632513 100755 --- a/test/functional/p2p_compactblocks.py +++ b/test/functional/p2p_compactblocks.py @@ -11,7 +11,7 @@ import random from test_framework.blocktools import create_block, create_coinbase, add_witness_commitment from test_framework.messages import BlockTransactions, BlockTransactionsRequest, calculate_shortid, CBlock, CBlockHeader, CInv, COutPoint, CTransaction, CTxIn, CTxInWitness, CTxOut, FromHex, HeaderAndShortIDs, msg_no_witness_block, msg_no_witness_blocktxn, msg_cmpctblock, msg_getblocktxn, msg_getdata, msg_getheaders, msg_headers, msg_inv, msg_sendcmpct, msg_sendheaders, msg_tx, msg_block, msg_blocktxn, MSG_BLOCK, MSG_CMPCT_BLOCK, MSG_WITNESS_FLAG, NODE_NETWORK, P2PHeaderAndShortIDs, PrefilledTransaction, ser_uint256, ToHex -from test_framework.mininode import mininode_lock, P2PInterface +from test_framework.mininode import p2p_lock, P2PInterface from test_framework.script import CScript, OP_TRUE, OP_DROP from test_framework.test_framework import BitcoinTestFramework from test_framework.util import assert_equal, wait_until, softfork_active @@ -48,12 +48,12 @@ class TestP2PConn(P2PInterface): self.block_announced = True self.announced_blockhashes.add(x.hash) - # Requires caller to hold mininode_lock + # Requires caller to hold p2p_lock def received_block_announcement(self): return self.block_announced def clear_block_announcement(self): - with mininode_lock: + with p2p_lock: self.block_announced = False self.last_message.pop("inv", None) self.last_message.pop("headers", None) @@ -73,7 +73,7 @@ class TestP2PConn(P2PInterface): def request_headers_and_sync(self, locator, hashstop=0): self.clear_block_announcement() self.get_headers(locator, hashstop) - wait_until(self.received_block_announcement, timeout=30, lock=mininode_lock) + wait_until(self.received_block_announcement, timeout=30, lock=p2p_lock) self.clear_block_announcement() # Block until a block announcement for a particular block hash is @@ -81,7 +81,7 @@ class TestP2PConn(P2PInterface): def wait_for_block_announcement(self, block_hash, timeout=30): def received_hash(): return (block_hash in self.announced_blockhashes) - wait_until(received_hash, timeout=timeout, lock=mininode_lock) + wait_until(received_hash, timeout=timeout, lock=p2p_lock) def send_await_disconnect(self, message, timeout=30): """Sends a message to the node and wait for disconnect. @@ -89,7 +89,7 @@ class TestP2PConn(P2PInterface): This is used when we want to send a message into the node that we expect will get us disconnected, eg an invalid block.""" self.send_message(message) - wait_until(lambda: not self.is_connected, timeout=timeout, lock=mininode_lock) + wait_until(lambda: not self.is_connected, timeout=timeout, lock=p2p_lock) class CompactBlocksTest(BitcoinTestFramework): def set_test_params(self): @@ -154,8 +154,8 @@ class CompactBlocksTest(BitcoinTestFramework): # Make sure we get a SENDCMPCT message from our peer def received_sendcmpct(): return (len(test_node.last_sendcmpct) > 0) - wait_until(received_sendcmpct, timeout=30, lock=mininode_lock) - with mininode_lock: + wait_until(received_sendcmpct, timeout=30, lock=p2p_lock) + with p2p_lock: # Check that the first version received is the preferred one assert_equal(test_node.last_sendcmpct[0].version, preferred_version) # And that we receive versions down to 1. @@ -170,7 +170,7 @@ class CompactBlocksTest(BitcoinTestFramework): peer.wait_for_block_announcement(block_hash, timeout=30) assert peer.block_announced - with mininode_lock: + with p2p_lock: assert predicate(peer), ( "block_hash={!r}, cmpctblock={!r}, inv={!r}".format( block_hash, peer.last_message.get("cmpctblock", None), peer.last_message.get("inv", None))) @@ -294,11 +294,11 @@ class CompactBlocksTest(BitcoinTestFramework): block.rehash() # Wait until the block was announced (via compact blocks) - wait_until(lambda: "cmpctblock" in test_node.last_message, timeout=30, lock=mininode_lock) + wait_until(lambda: "cmpctblock" in test_node.last_message, timeout=30, lock=p2p_lock) # Now fetch and check the compact block header_and_shortids = None - with mininode_lock: + with p2p_lock: # Convert the on-the-wire representation to absolute indexes header_and_shortids = HeaderAndShortIDs(test_node.last_message["cmpctblock"].header_and_shortids) self.check_compactblock_construction_from_block(version, header_and_shortids, block_hash, block) @@ -308,11 +308,11 @@ class CompactBlocksTest(BitcoinTestFramework): inv = CInv(MSG_CMPCT_BLOCK, block_hash) test_node.send_message(msg_getdata([inv])) - wait_until(lambda: "cmpctblock" in test_node.last_message, timeout=30, lock=mininode_lock) + wait_until(lambda: "cmpctblock" in test_node.last_message, timeout=30, lock=p2p_lock) # Now fetch and check the compact block header_and_shortids = None - with mininode_lock: + with p2p_lock: # Convert the on-the-wire representation to absolute indexes header_and_shortids = HeaderAndShortIDs(test_node.last_message["cmpctblock"].header_and_shortids) self.check_compactblock_construction_from_block(version, header_and_shortids, block_hash, block) @@ -378,7 +378,7 @@ class CompactBlocksTest(BitcoinTestFramework): if announce == "inv": test_node.send_message(msg_inv([CInv(MSG_BLOCK, block.sha256)])) - wait_until(lambda: "getheaders" in test_node.last_message, timeout=30, lock=mininode_lock) + wait_until(lambda: "getheaders" in test_node.last_message, timeout=30, lock=p2p_lock) test_node.send_header_for_blocks([block]) else: test_node.send_header_for_blocks([block]) @@ -397,7 +397,7 @@ class CompactBlocksTest(BitcoinTestFramework): test_node.send_and_ping(msg_cmpctblock(comp_block.to_p2p())) assert_equal(int(node.getbestblockhash(), 16), block.hashPrevBlock) # Expect a getblocktxn message. - with mininode_lock: + with p2p_lock: assert "getblocktxn" in test_node.last_message absolute_indexes = test_node.last_message["getblocktxn"].block_txn_request.to_absolute() assert_equal(absolute_indexes, [0]) # should be a coinbase request @@ -439,7 +439,7 @@ class CompactBlocksTest(BitcoinTestFramework): def test_getblocktxn_response(compact_block, peer, expected_result): msg = msg_cmpctblock(compact_block.to_p2p()) peer.send_and_ping(msg) - with mininode_lock: + with p2p_lock: assert "getblocktxn" in peer.last_message absolute_indexes = peer.last_message["getblocktxn"].block_txn_request.to_absolute() assert_equal(absolute_indexes, expected_result) @@ -504,13 +504,13 @@ class CompactBlocksTest(BitcoinTestFramework): assert tx.hash in mempool # Clear out last request. - with mininode_lock: + with p2p_lock: test_node.last_message.pop("getblocktxn", None) # Send compact block comp_block.initialize_from_block(block, prefill_list=[0], use_witness=with_witness) test_tip_after_message(node, test_node, msg_cmpctblock(comp_block.to_p2p()), block.sha256) - with mininode_lock: + with p2p_lock: # Shouldn't have gotten a request for any transaction assert "getblocktxn" not in test_node.last_message @@ -537,7 +537,7 @@ class CompactBlocksTest(BitcoinTestFramework): comp_block.initialize_from_block(block, prefill_list=[0], use_witness=(version == 2)) test_node.send_and_ping(msg_cmpctblock(comp_block.to_p2p())) absolute_indexes = [] - with mininode_lock: + with p2p_lock: assert "getblocktxn" in test_node.last_message absolute_indexes = test_node.last_message["getblocktxn"].block_txn_request.to_absolute() assert_equal(absolute_indexes, [6, 7, 8, 9, 10]) @@ -588,10 +588,10 @@ class CompactBlocksTest(BitcoinTestFramework): num_to_request = random.randint(1, len(block.vtx)) msg.block_txn_request.from_absolute(sorted(random.sample(range(len(block.vtx)), num_to_request))) test_node.send_message(msg) - wait_until(lambda: "blocktxn" in test_node.last_message, timeout=10, lock=mininode_lock) + wait_until(lambda: "blocktxn" in test_node.last_message, timeout=10, lock=p2p_lock) [tx.calc_sha256() for tx in block.vtx] - with mininode_lock: + with p2p_lock: assert_equal(test_node.last_message["blocktxn"].block_transactions.blockhash, int(block_hash, 16)) all_indices = msg.block_txn_request.to_absolute() for index in all_indices: @@ -611,11 +611,11 @@ class CompactBlocksTest(BitcoinTestFramework): # allowed depth for a blocktxn response. block_hash = node.getblockhash(current_height) msg.block_txn_request = BlockTransactionsRequest(int(block_hash, 16), [0]) - with mininode_lock: + with p2p_lock: test_node.last_message.pop("block", None) test_node.last_message.pop("blocktxn", None) test_node.send_and_ping(msg) - with mininode_lock: + with p2p_lock: test_node.last_message["block"].block.calc_sha256() assert_equal(test_node.last_message["block"].block.sha256, int(block_hash, 16)) assert "blocktxn" not in test_node.last_message @@ -628,21 +628,21 @@ class CompactBlocksTest(BitcoinTestFramework): for _ in range(MAX_CMPCTBLOCK_DEPTH + 1): test_node.clear_block_announcement() new_blocks.append(node.generate(1)[0]) - wait_until(test_node.received_block_announcement, timeout=30, lock=mininode_lock) + wait_until(test_node.received_block_announcement, timeout=30, lock=p2p_lock) test_node.clear_block_announcement() test_node.send_message(msg_getdata([CInv(MSG_CMPCT_BLOCK, int(new_blocks[0], 16))])) - wait_until(lambda: "cmpctblock" in test_node.last_message, timeout=30, lock=mininode_lock) + wait_until(lambda: "cmpctblock" in test_node.last_message, timeout=30, lock=p2p_lock) test_node.clear_block_announcement() node.generate(1) - wait_until(test_node.received_block_announcement, timeout=30, lock=mininode_lock) + wait_until(test_node.received_block_announcement, timeout=30, lock=p2p_lock) test_node.clear_block_announcement() - with mininode_lock: + with p2p_lock: test_node.last_message.pop("block", None) test_node.send_message(msg_getdata([CInv(MSG_CMPCT_BLOCK, int(new_blocks[0], 16))])) - wait_until(lambda: "block" in test_node.last_message, timeout=30, lock=mininode_lock) - with mininode_lock: + wait_until(lambda: "block" in test_node.last_message, timeout=30, lock=p2p_lock) + with p2p_lock: test_node.last_message["block"].block.calc_sha256() assert_equal(test_node.last_message["block"].block.sha256, int(new_blocks[0], 16)) @@ -670,10 +670,10 @@ class CompactBlocksTest(BitcoinTestFramework): # (to avoid fingerprinting attacks). msg = msg_getblocktxn() msg.block_txn_request = BlockTransactionsRequest(block.sha256, [0]) - with mininode_lock: + with p2p_lock: test_node.last_message.pop("blocktxn", None) test_node.send_and_ping(msg) - with mininode_lock: + with p2p_lock: assert "blocktxn" not in test_node.last_message def test_end_to_end_block_relay(self, listeners): @@ -689,8 +689,8 @@ class CompactBlocksTest(BitcoinTestFramework): node.submitblock(ToHex(block)) for l in listeners: - wait_until(lambda: "cmpctblock" in l.last_message, timeout=30, lock=mininode_lock) - with mininode_lock: + wait_until(lambda: "cmpctblock" in l.last_message, timeout=30, lock=p2p_lock) + with p2p_lock: for l in listeners: l.last_message["cmpctblock"].header_and_shortids.header.calc_sha256() assert_equal(l.last_message["cmpctblock"].header_and_shortids.header.sha256, block.sha256) @@ -747,7 +747,7 @@ class CompactBlocksTest(BitcoinTestFramework): cmpct_block.initialize_from_block(block) msg = msg_cmpctblock(cmpct_block.to_p2p()) peer.send_and_ping(msg) - with mininode_lock: + with p2p_lock: assert "getblocktxn" in peer.last_message return block, cmpct_block diff --git a/test/functional/p2p_feefilter.py b/test/functional/p2p_feefilter.py index 3a9b8dfbd7..97880b6848 100755 --- a/test/functional/p2p_feefilter.py +++ b/test/functional/p2p_feefilter.py @@ -7,7 +7,7 @@ from decimal import Decimal from test_framework.messages import MSG_TX, MSG_WTX, msg_feefilter -from test_framework.mininode import mininode_lock, P2PInterface +from test_framework.mininode import p2p_lock, P2PInterface from test_framework.test_framework import BitcoinTestFramework from test_framework.util import assert_equal @@ -23,7 +23,7 @@ class FeefilterConn(P2PInterface): self.feefilter_received = True def assert_feefilter_received(self, recv: bool): - with mininode_lock: + with p2p_lock: assert_equal(self.feefilter_received, recv) @@ -42,7 +42,7 @@ class TestP2PConn(P2PInterface): self.wait_until(lambda: invs_expected == sorted(self.txinvs), timeout=60) def clear_invs(self): - with mininode_lock: + with p2p_lock: self.txinvs = [] diff --git a/test/functional/p2p_filter.py b/test/functional/p2p_filter.py index ce3856fc95..608f0186d3 100755 --- a/test/functional/p2p_filter.py +++ b/test/functional/p2p_filter.py @@ -19,7 +19,7 @@ from test_framework.messages import ( msg_mempool, msg_version, ) -from test_framework.mininode import P2PInterface, mininode_lock +from test_framework.mininode import P2PInterface, p2p_lock from test_framework.script import MAX_SCRIPT_ELEMENT_SIZE from test_framework.test_framework import BitcoinTestFramework @@ -60,22 +60,22 @@ class P2PBloomFilter(P2PInterface): @property def tx_received(self): - with mininode_lock: + with p2p_lock: return self._tx_received @tx_received.setter def tx_received(self, value): - with mininode_lock: + with p2p_lock: self._tx_received = value @property def merkleblock_received(self): - with mininode_lock: + with p2p_lock: return self._merkleblock_received @merkleblock_received.setter def merkleblock_received(self, value): - with mininode_lock: + with p2p_lock: self._merkleblock_received = value diff --git a/test/functional/p2p_getaddr_caching.py b/test/functional/p2p_getaddr_caching.py index c9278eab92..aeeff2e4de 100755 --- a/test/functional/p2p_getaddr_caching.py +++ b/test/functional/p2p_getaddr_caching.py @@ -14,7 +14,7 @@ from test_framework.messages import ( ) from test_framework.mininode import ( P2PInterface, - mininode_lock + p2p_lock ) from test_framework.test_framework import BitcoinTestFramework from test_framework.util import ( @@ -44,7 +44,7 @@ class AddrReceiver(P2PInterface): self.received_addrs = None def get_received_addrs(self): - with mininode_lock: + with p2p_lock: return self.received_addrs def on_addr(self, message): diff --git a/test/functional/p2p_leak.py b/test/functional/p2p_leak.py index 79bf7b2e7c..0dbf6b3957 100755 --- a/test/functional/p2p_leak.py +++ b/test/functional/p2p_leak.py @@ -17,7 +17,7 @@ from test_framework.messages import ( msg_ping, msg_version, ) -from test_framework.mininode import mininode_lock, P2PInterface +from test_framework.mininode import p2p_lock, P2PInterface from test_framework.test_framework import BitcoinTestFramework from test_framework.util import ( assert_equal, @@ -114,9 +114,9 @@ class P2PLeakTest(BitcoinTestFramework): # verack, since we never sent one no_verack_idle_peer.wait_for_verack() - wait_until(lambda: no_version_disconnect_peer.ever_connected, timeout=10, lock=mininode_lock) - wait_until(lambda: no_version_idle_peer.ever_connected, timeout=10, lock=mininode_lock) - wait_until(lambda: no_verack_idle_peer.version_received, timeout=10, lock=mininode_lock) + wait_until(lambda: no_version_disconnect_peer.ever_connected, timeout=10, lock=p2p_lock) + wait_until(lambda: no_version_idle_peer.ever_connected, timeout=10, lock=p2p_lock) + wait_until(lambda: no_verack_idle_peer.version_received, timeout=10, lock=p2p_lock) # Mine a block and make sure that it's not sent to the connected peers self.nodes[0].generate(nblocks=1) diff --git a/test/functional/p2p_node_network_limited.py b/test/functional/p2p_node_network_limited.py index a2f6ea538c..5574cf643f 100755 --- a/test/functional/p2p_node_network_limited.py +++ b/test/functional/p2p_node_network_limited.py @@ -9,7 +9,7 @@ and that it responds to getdata requests for blocks correctly: - send a block within 288 + 2 of the tip - disconnect peers who request blocks older than that.""" from test_framework.messages import CInv, MSG_BLOCK, msg_getdata, msg_verack, NODE_NETWORK_LIMITED, NODE_WITNESS -from test_framework.mininode import P2PInterface, mininode_lock +from test_framework.mininode import P2PInterface, p2p_lock from test_framework.test_framework import BitcoinTestFramework from test_framework.util import ( assert_equal, @@ -28,7 +28,7 @@ class P2PIgnoreInv(P2PInterface): self.firstAddrnServices = message.addrs[0].nServices def wait_for_addr(self, timeout=5): test_function = lambda: self.last_message.get("addr") - wait_until(test_function, timeout=timeout, lock=mininode_lock) + wait_until(test_function, timeout=timeout, lock=p2p_lock) def send_getdata_for_block(self, blockhash): getdata_request = msg_getdata() getdata_request.inv.append(CInv(MSG_BLOCK, int(blockhash, 16))) diff --git a/test/functional/p2p_segwit.py b/test/functional/p2p_segwit.py index 564e49f3d8..ffb4e6d94b 100755 --- a/test/functional/p2p_segwit.py +++ b/test/functional/p2p_segwit.py @@ -44,7 +44,7 @@ from test_framework.messages import ( ) from test_framework.mininode import ( P2PInterface, - mininode_lock, + p2p_lock, ) from test_framework.script import ( CScript, @@ -177,7 +177,7 @@ class TestP2PConn(P2PInterface): if success: # sanity check assert (self.wtxidrelay and use_wtxid) or (not self.wtxidrelay and not use_wtxid) - with mininode_lock: + with p2p_lock: self.last_message.pop("getdata", None) if use_wtxid: wtxid = tx.calc_sha256(True) @@ -195,7 +195,7 @@ class TestP2PConn(P2PInterface): assert not self.last_message.get("getdata") def announce_block_and_wait_for_getdata(self, block, use_header, timeout=60): - with mininode_lock: + with p2p_lock: self.last_message.pop("getdata", None) self.last_message.pop("getheaders", None) msg = msg_headers() @@ -209,7 +209,7 @@ class TestP2PConn(P2PInterface): self.wait_for_getdata([block.sha256]) def request_block(self, blockhash, inv_type, timeout=60): - with mininode_lock: + with p2p_lock: self.last_message.pop("block", None) self.send_message(msg_getdata(inv=[CInv(inv_type, blockhash)])) self.wait_for_block(blockhash, timeout) @@ -2114,7 +2114,7 @@ class SegWitTest(BitcoinTestFramework): # Check wtxidrelay feature negotiation message through connecting a new peer def received_wtxidrelay(): return (len(self.wtx_node.last_wtxidrelay) > 0) - wait_until(received_wtxidrelay, timeout=60, lock=mininode_lock) + wait_until(received_wtxidrelay, timeout=60, lock=p2p_lock) # Create a Segwit output from the latest UTXO # and announce it to the network @@ -2138,25 +2138,25 @@ class SegWitTest(BitcoinTestFramework): # Announce Segwit transaction with wtxid # and wait for getdata self.wtx_node.announce_tx_and_wait_for_getdata(tx2, use_wtxid=True) - with mininode_lock: + with p2p_lock: lgd = self.wtx_node.lastgetdata[:] assert_equal(lgd, [CInv(MSG_WTX, tx2.calc_sha256(True))]) # Announce Segwit transaction from non wtxidrelay peer # and wait for getdata self.tx_node.announce_tx_and_wait_for_getdata(tx2, use_wtxid=False) - with mininode_lock: + with p2p_lock: lgd = self.tx_node.lastgetdata[:] assert_equal(lgd, [CInv(MSG_TX|MSG_WITNESS_FLAG, tx2.sha256)]) # Send tx2 through; it's an orphan so won't be accepted - with mininode_lock: + with p2p_lock: self.wtx_node.last_message.pop("getdata", None) test_transaction_acceptance(self.nodes[0], self.wtx_node, tx2, with_witness=True, accepted=False) # Expect a request for parent (tx) by txid despite use of WTX peer self.wtx_node.wait_for_getdata([tx.sha256], 60) - with mininode_lock: + with p2p_lock: lgd = self.wtx_node.lastgetdata[:] assert_equal(lgd, [CInv(MSG_TX|MSG_WITNESS_FLAG, tx.sha256)]) diff --git a/test/functional/p2p_sendheaders.py b/test/functional/p2p_sendheaders.py index 126a46bd53..cef57de2b8 100755 --- a/test/functional/p2p_sendheaders.py +++ b/test/functional/p2p_sendheaders.py @@ -91,7 +91,7 @@ from test_framework.mininode import ( CBlockHeader, NODE_WITNESS, P2PInterface, - mininode_lock, + p2p_lock, MSG_BLOCK, msg_block, msg_getblocks, @@ -147,7 +147,7 @@ class BaseNode(P2PInterface): def wait_for_block_announcement(self, block_hash, timeout=60): test_function = lambda: self.last_blockhash_announced == block_hash - wait_until(test_function, timeout=timeout, lock=mininode_lock) + wait_until(test_function, timeout=timeout, lock=p2p_lock) def on_inv(self, message): self.block_announced = True @@ -163,7 +163,7 @@ class BaseNode(P2PInterface): self.last_blockhash_announced = message.headers[-1].sha256 def clear_block_announcements(self): - with mininode_lock: + with p2p_lock: self.block_announced = False self.last_message.pop("inv", None) self.last_message.pop("headers", None) @@ -174,8 +174,8 @@ class BaseNode(P2PInterface): """Test whether the last headers announcements received are right. Headers may be announced across more than one message.""" test_function = lambda: (len(self.recent_headers_announced) >= len(headers)) - wait_until(test_function, timeout=60, lock=mininode_lock) - with mininode_lock: + wait_until(test_function, timeout=60, lock=p2p_lock) + with p2p_lock: assert_equal(self.recent_headers_announced, headers) self.block_announced = False self.last_message.pop("headers", None) @@ -186,9 +186,9 @@ class BaseNode(P2PInterface): inv should be a list of block hashes.""" test_function = lambda: self.block_announced - wait_until(test_function, timeout=60, lock=mininode_lock) + wait_until(test_function, timeout=60, lock=p2p_lock) - with mininode_lock: + with p2p_lock: compare_inv = [] if "inv" in self.last_message: compare_inv = [x.hash for x in self.last_message["inv"].inv] @@ -298,7 +298,7 @@ class SendHeadersTest(BitcoinTestFramework): test_node.send_header_for_blocks([new_block]) test_node.wait_for_getdata([new_block.sha256]) test_node.send_and_ping(msg_block(new_block)) # make sure this block is processed - wait_until(lambda: inv_node.block_announced, timeout=60, lock=mininode_lock) + wait_until(lambda: inv_node.block_announced, timeout=60, lock=p2p_lock) inv_node.clear_block_announcements() test_node.clear_block_announcements() @@ -456,7 +456,7 @@ class SendHeadersTest(BitcoinTestFramework): test_node.send_header_for_blocks(blocks) test_node.sync_with_ping() # should not have received any getdata messages - with mininode_lock: + with p2p_lock: assert "getdata" not in test_node.last_message # This time, direct fetch should work @@ -494,7 +494,7 @@ class SendHeadersTest(BitcoinTestFramework): test_node.last_message.pop("getdata", None) test_node.send_header_for_blocks(blocks[0:1]) test_node.sync_with_ping() - with mininode_lock: + with p2p_lock: assert "getdata" not in test_node.last_message # Announcing one more block on fork should trigger direct fetch for @@ -513,7 +513,7 @@ class SendHeadersTest(BitcoinTestFramework): test_node.last_message.pop("getdata", None) test_node.send_header_for_blocks(blocks[18:19]) test_node.sync_with_ping() - with mininode_lock: + with p2p_lock: assert "getdata" not in test_node.last_message self.log.info("Part 4: success!") @@ -536,7 +536,7 @@ class SendHeadersTest(BitcoinTestFramework): block_time += 1 height += 1 # Send the header of the second block -> this won't connect. - with mininode_lock: + with p2p_lock: test_node.last_message.pop("getheaders", None) test_node.send_header_for_blocks([blocks[1]]) test_node.wait_for_getheaders() @@ -559,7 +559,7 @@ class SendHeadersTest(BitcoinTestFramework): for i in range(1, MAX_UNCONNECTING_HEADERS): # Send a header that doesn't connect, check that we get a getheaders. - with mininode_lock: + with p2p_lock: test_node.last_message.pop("getheaders", None) test_node.send_header_for_blocks([blocks[i]]) test_node.wait_for_getheaders() @@ -574,7 +574,7 @@ class SendHeadersTest(BitcoinTestFramework): # before we get disconnected. Should be 5*MAX_UNCONNECTING_HEADERS for i in range(5 * MAX_UNCONNECTING_HEADERS - 1): # Send a header that doesn't connect, check that we get a getheaders. - with mininode_lock: + with p2p_lock: test_node.last_message.pop("getheaders", None) test_node.send_header_for_blocks([blocks[i % len(blocks)]]) test_node.wait_for_getheaders() diff --git a/test/functional/p2p_tx_download.py b/test/functional/p2p_tx_download.py index 3ea1c6e5e7..06dab60729 100755 --- a/test/functional/p2p_tx_download.py +++ b/test/functional/p2p_tx_download.py @@ -18,7 +18,7 @@ from test_framework.messages import ( ) from test_framework.mininode import ( P2PInterface, - mininode_lock, + p2p_lock, ) from test_framework.test_framework import BitcoinTestFramework from test_framework.util import ( @@ -73,7 +73,7 @@ class TxDownloadTest(BitcoinTestFramework): def getdata_found(peer_index): p = self.nodes[0].p2ps[peer_index] - with mininode_lock: + with p2p_lock: return p.last_message.get("getdata") and p.last_message["getdata"].inv[-1].hash == txid node_0_mocktime = int(time.time()) @@ -134,18 +134,18 @@ class TxDownloadTest(BitcoinTestFramework): p = self.nodes[0].p2ps[0] - with mininode_lock: + with p2p_lock: p.tx_getdata_count = 0 p.send_message(msg_inv([CInv(t=MSG_WTX, h=i) for i in txids])) - wait_until(lambda: p.tx_getdata_count >= MAX_GETDATA_IN_FLIGHT, lock=mininode_lock) - with mininode_lock: + wait_until(lambda: p.tx_getdata_count >= MAX_GETDATA_IN_FLIGHT, lock=p2p_lock) + with p2p_lock: assert_equal(p.tx_getdata_count, MAX_GETDATA_IN_FLIGHT) self.log.info("Now check that if we send a NOTFOUND for a transaction, we'll get one more request") p.send_message(msg_notfound(vec=[CInv(t=MSG_WTX, h=txids[0])])) - wait_until(lambda: p.tx_getdata_count >= MAX_GETDATA_IN_FLIGHT + 1, timeout=10, lock=mininode_lock) - with mininode_lock: + wait_until(lambda: p.tx_getdata_count >= MAX_GETDATA_IN_FLIGHT + 1, timeout=10, lock=p2p_lock) + with p2p_lock: assert_equal(p.tx_getdata_count, MAX_GETDATA_IN_FLIGHT + 1) WAIT_TIME = TX_EXPIRY_INTERVAL // 2 + TX_EXPIRY_INTERVAL diff --git a/test/functional/p2p_unrequested_blocks.py b/test/functional/p2p_unrequested_blocks.py index 71b0b0f63a..6888e90323 100755 --- a/test/functional/p2p_unrequested_blocks.py +++ b/test/functional/p2p_unrequested_blocks.py @@ -55,7 +55,7 @@ import time from test_framework.blocktools import create_block, create_coinbase, create_tx_with_script from test_framework.messages import CBlockHeader, CInv, MSG_BLOCK, msg_block, msg_headers, msg_inv -from test_framework.mininode import mininode_lock, P2PInterface +from test_framework.mininode import p2p_lock, P2PInterface from test_framework.test_framework import BitcoinTestFramework from test_framework.util import ( assert_equal, @@ -199,13 +199,13 @@ class AcceptBlockTest(BitcoinTestFramework): # 6. Try to get node to request the missing block. # Poke the node with an inv for block at height 3 and see if that # triggers a getdata on block 2 (it should if block 2 is missing). - with mininode_lock: + with p2p_lock: # Clear state so we can check the getdata request test_node.last_message.pop("getdata", None) test_node.send_message(msg_inv([CInv(MSG_BLOCK, block_h3.sha256)])) test_node.sync_with_ping() - with mininode_lock: + with p2p_lock: getdata = test_node.last_message["getdata"] # Check that the getdata includes the right block diff --git a/test/functional/test_framework/mininode.py b/test/functional/test_framework/mininode.py index eaf637fbb8..5a5a94f0d6 100755 --- a/test/functional/test_framework/mininode.py +++ b/test/functional/test_framework/mininode.py @@ -320,7 +320,7 @@ class P2PInterface(P2PConnection): We keep a count of how many of each message type has been received and the most recent message of each type.""" - with mininode_lock: + with p2p_lock: try: msgtype = message.msgtype.decode('ascii') self.message_count[msgtype] += 1 @@ -394,7 +394,7 @@ class P2PInterface(P2PConnection): assert self.is_connected return test_function_in() - wait_until(test_function, timeout=timeout, lock=mininode_lock, timeout_factor=self.timeout_factor) + wait_until(test_function, timeout=timeout, lock=p2p_lock, timeout_factor=self.timeout_factor) def wait_for_disconnect(self, timeout=60): test_function = lambda: not self.is_connected @@ -498,7 +498,7 @@ class P2PInterface(P2PConnection): # P2PConnection acquires this lock whenever delivering a message to a P2PInterface. # This lock should be acquired in the thread running the test logic to synchronize # access to any data shared with the P2PInterface or P2PConnection. -mininode_lock = threading.Lock() +p2p_lock = threading.Lock() class NetworkThread(threading.Thread): @@ -592,7 +592,7 @@ class P2PDataStore(P2PInterface): - if success is False: assert that the node's tip doesn't advance - if reject_reason is set: assert that the correct reject message is logged""" - with mininode_lock: + with p2p_lock: for block in blocks: self.block_store[block.sha256] = block self.last_block_hash = block.sha256 @@ -629,7 +629,7 @@ class P2PDataStore(P2PInterface): - if expect_disconnect is True: Skip the sync with ping - if reject_reason is set: assert that the correct reject message is logged.""" - with mininode_lock: + with p2p_lock: for tx in txs: self.tx_store[tx.sha256] = tx @@ -668,7 +668,7 @@ class P2PTxInvStore(P2PInterface): self.tx_invs_received[i.hash] += 1 def get_invs(self): - with mininode_lock: + with p2p_lock: return list(self.tx_invs_received.keys()) def wait_for_broadcast(self, txns, timeout=60): diff --git a/test/functional/wallet_resendwallettransactions.py b/test/functional/wallet_resendwallettransactions.py index 3417616d77..36c4748a09 100755 --- a/test/functional/wallet_resendwallettransactions.py +++ b/test/functional/wallet_resendwallettransactions.py @@ -7,7 +7,7 @@ import time from test_framework.blocktools import create_block, create_coinbase from test_framework.messages import ToHex -from test_framework.mininode import P2PTxInvStore, mininode_lock +from test_framework.mininode import P2PTxInvStore, p2p_lock from test_framework.test_framework import BitcoinTestFramework from test_framework.util import assert_equal, wait_until @@ -33,7 +33,7 @@ class ResendWalletTransactionsTest(BitcoinTestFramework): time.sleep(1.1) # Can take a few seconds due to transaction trickling - wait_until(lambda: node.p2p.tx_invs_received[txid] >= 1, lock=mininode_lock) + wait_until(lambda: node.p2p.tx_invs_received[txid] >= 1, lock=p2p_lock) # Add a second peer since txs aren't rebroadcast to the same peer (see filterInventoryKnown) node.add_p2p_connection(P2PTxInvStore()) @@ -64,7 +64,7 @@ class ResendWalletTransactionsTest(BitcoinTestFramework): # Transaction should be rebroadcast approximately 24 hours in the future, # but can range from 12-36. So bump 36 hours to be sure. node.setmocktime(now + 36 * 60 * 60) - wait_until(lambda: node.p2ps[1].tx_invs_received[txid] >= 1, lock=mininode_lock) + wait_until(lambda: node.p2ps[1].tx_invs_received[txid] >= 1, lock=p2p_lock) if __name__ == '__main__': -- cgit v1.2.3 From 85165d4332b0f72d30e0c584b476249b542338e6 Mon Sep 17 00:00:00 2001 From: John Newbery Date: Sun, 19 Jul 2020 14:47:05 +0700 Subject: scripted-diff: Rename mininode to p2p -BEGIN VERIFY SCRIPT- sed -i 's/\.mininode/\.p2p/g' $(git grep -l "mininode") git mv test/functional/test_framework/mininode.py test/functional/test_framework/p2p.py -END VERIFY SCRIPT- --- test/functional/example_test.py | 2 +- test/functional/feature_assumevalid.py | 2 +- test/functional/feature_block.py | 2 +- test/functional/feature_cltv.py | 2 +- test/functional/feature_csv_activation.py | 2 +- test/functional/feature_dersig.py | 2 +- test/functional/feature_maxuploadtarget.py | 2 +- test/functional/feature_versionbits_warning.py | 2 +- test/functional/mempool_packages.py | 2 +- test/functional/mempool_persist.py | 2 +- test/functional/mempool_unbroadcast.py | 2 +- test/functional/mining_basic.py | 2 +- test/functional/p2p_addr_relay.py | 2 +- test/functional/p2p_blockfilters.py | 2 +- test/functional/p2p_blocksonly.py | 2 +- test/functional/p2p_compactblocks.py | 2 +- test/functional/p2p_dos_header_tree.py | 2 +- test/functional/p2p_eviction.py | 2 +- test/functional/p2p_feefilter.py | 2 +- test/functional/p2p_filter.py | 2 +- test/functional/p2p_fingerprint.py | 2 +- test/functional/p2p_getaddr_caching.py | 2 +- test/functional/p2p_getdata.py | 2 +- test/functional/p2p_invalid_block.py | 2 +- test/functional/p2p_invalid_locator.py | 2 +- test/functional/p2p_invalid_messages.py | 2 +- test/functional/p2p_invalid_tx.py | 2 +- test/functional/p2p_leak.py | 2 +- test/functional/p2p_leak_tx.py | 2 +- test/functional/p2p_nobloomfilter_messages.py | 2 +- test/functional/p2p_node_network_limited.py | 2 +- test/functional/p2p_permissions.py | 2 +- test/functional/p2p_ping.py | 2 +- test/functional/p2p_segwit.py | 2 +- test/functional/p2p_sendheaders.py | 2 +- test/functional/p2p_timeouts.py | 2 +- test/functional/p2p_tx_download.py | 2 +- test/functional/p2p_unrequested_blocks.py | 2 +- test/functional/rpc_blockchain.py | 2 +- test/functional/rpc_net.py | 2 +- test/functional/test_framework/mininode.py | 681 --------------------- test/functional/test_framework/p2p.py | 681 +++++++++++++++++++++ test/functional/test_framework/test_framework.py | 2 +- test/functional/wallet_resendwallettransactions.py | 2 +- 44 files changed, 723 insertions(+), 723 deletions(-) delete mode 100755 test/functional/test_framework/mininode.py create mode 100755 test/functional/test_framework/p2p.py (limited to 'test') diff --git a/test/functional/example_test.py b/test/functional/example_test.py index c997e47c5e..c69b078c6e 100755 --- a/test/functional/example_test.py +++ b/test/functional/example_test.py @@ -16,7 +16,7 @@ from collections import defaultdict # Avoid wildcard * imports from test_framework.blocktools import (create_block, create_coinbase) from test_framework.messages import CInv, MSG_BLOCK -from test_framework.mininode import ( +from test_framework.p2p import ( P2PInterface, p2p_lock, msg_block, diff --git a/test/functional/feature_assumevalid.py b/test/functional/feature_assumevalid.py index f19ee12f95..603d7f5d3b 100755 --- a/test/functional/feature_assumevalid.py +++ b/test/functional/feature_assumevalid.py @@ -42,7 +42,7 @@ from test_framework.messages import ( msg_block, msg_headers, ) -from test_framework.mininode import P2PInterface +from test_framework.p2p import P2PInterface from test_framework.script import (CScript, OP_TRUE) from test_framework.test_framework import BitcoinTestFramework from test_framework.util import assert_equal diff --git a/test/functional/feature_block.py b/test/functional/feature_block.py index c74761869b..81483b5a87 100755 --- a/test/functional/feature_block.py +++ b/test/functional/feature_block.py @@ -26,7 +26,7 @@ from test_framework.messages import ( uint256_from_compact, uint256_from_str, ) -from test_framework.mininode import P2PDataStore +from test_framework.p2p import P2PDataStore from test_framework.script import ( CScript, MAX_SCRIPT_ELEMENT_SIZE, diff --git a/test/functional/feature_cltv.py b/test/functional/feature_cltv.py index fd0330924d..2919b0ea0b 100755 --- a/test/functional/feature_cltv.py +++ b/test/functional/feature_cltv.py @@ -10,7 +10,7 @@ Test that the CHECKLOCKTIMEVERIFY soft-fork activates at (regtest) block height from test_framework.blocktools import create_coinbase, create_block, create_transaction from test_framework.messages import CTransaction, msg_block, ToHex -from test_framework.mininode import P2PInterface +from test_framework.p2p import P2PInterface from test_framework.script import CScript, OP_1NEGATE, OP_CHECKLOCKTIMEVERIFY, OP_DROP, CScriptNum from test_framework.test_framework import BitcoinTestFramework from test_framework.util import ( diff --git a/test/functional/feature_csv_activation.py b/test/functional/feature_csv_activation.py index dfb3683143..38e95f00e9 100755 --- a/test/functional/feature_csv_activation.py +++ b/test/functional/feature_csv_activation.py @@ -44,7 +44,7 @@ import time from test_framework.blocktools import create_coinbase, create_block, create_transaction from test_framework.messages import ToHex, CTransaction -from test_framework.mininode import P2PDataStore +from test_framework.p2p import P2PDataStore from test_framework.script import ( CScript, OP_CHECKSEQUENCEVERIFY, diff --git a/test/functional/feature_dersig.py b/test/functional/feature_dersig.py index 05fdacd451..f263c93c8a 100755 --- a/test/functional/feature_dersig.py +++ b/test/functional/feature_dersig.py @@ -9,7 +9,7 @@ Test that the DERSIG soft-fork activates at (regtest) height 1251. from test_framework.blocktools import create_coinbase, create_block, create_transaction from test_framework.messages import msg_block -from test_framework.mininode import P2PInterface +from test_framework.p2p import P2PInterface from test_framework.script import CScript from test_framework.test_framework import BitcoinTestFramework from test_framework.util import ( diff --git a/test/functional/feature_maxuploadtarget.py b/test/functional/feature_maxuploadtarget.py index 0dc2839191..e5c62d1ea7 100755 --- a/test/functional/feature_maxuploadtarget.py +++ b/test/functional/feature_maxuploadtarget.py @@ -14,7 +14,7 @@ from collections import defaultdict import time from test_framework.messages import CInv, MSG_BLOCK, msg_getdata -from test_framework.mininode import P2PInterface +from test_framework.p2p import P2PInterface from test_framework.test_framework import BitcoinTestFramework from test_framework.util import assert_equal, mine_large_block diff --git a/test/functional/feature_versionbits_warning.py b/test/functional/feature_versionbits_warning.py index b99a6220fe..376a27e5f5 100755 --- a/test/functional/feature_versionbits_warning.py +++ b/test/functional/feature_versionbits_warning.py @@ -12,7 +12,7 @@ import re from test_framework.blocktools import create_block, create_coinbase from test_framework.messages import msg_block -from test_framework.mininode import P2PInterface, p2p_lock +from test_framework.p2p import P2PInterface, p2p_lock from test_framework.test_framework import BitcoinTestFramework from test_framework.util import wait_until diff --git a/test/functional/mempool_packages.py b/test/functional/mempool_packages.py index 98dac30ace..c68bda6579 100755 --- a/test/functional/mempool_packages.py +++ b/test/functional/mempool_packages.py @@ -7,7 +7,7 @@ from decimal import Decimal from test_framework.messages import COIN -from test_framework.mininode import P2PTxInvStore +from test_framework.p2p import P2PTxInvStore from test_framework.test_framework import BitcoinTestFramework from test_framework.util import ( assert_equal, diff --git a/test/functional/mempool_persist.py b/test/functional/mempool_persist.py index 85c4d6d570..cf2cd611bd 100755 --- a/test/functional/mempool_persist.py +++ b/test/functional/mempool_persist.py @@ -40,7 +40,7 @@ import os import time from test_framework.test_framework import BitcoinTestFramework -from test_framework.mininode import P2PTxInvStore +from test_framework.p2p import P2PTxInvStore from test_framework.util import ( assert_equal, assert_greater_than_or_equal, diff --git a/test/functional/mempool_unbroadcast.py b/test/functional/mempool_unbroadcast.py index 365d011157..abd5a03d95 100755 --- a/test/functional/mempool_unbroadcast.py +++ b/test/functional/mempool_unbroadcast.py @@ -7,7 +7,7 @@ to peers until a GETDATA is received.""" import time -from test_framework.mininode import P2PTxInvStore +from test_framework.p2p import P2PTxInvStore from test_framework.test_framework import BitcoinTestFramework from test_framework.util import ( assert_equal, diff --git a/test/functional/mining_basic.py b/test/functional/mining_basic.py index 63d1ccfb36..b13740750f 100755 --- a/test/functional/mining_basic.py +++ b/test/functional/mining_basic.py @@ -20,7 +20,7 @@ from test_framework.messages import ( CBlockHeader, BLOCK_HEADER_SIZE, ) -from test_framework.mininode import P2PDataStore +from test_framework.p2p import P2PDataStore from test_framework.test_framework import BitcoinTestFramework from test_framework.util import ( assert_equal, diff --git a/test/functional/p2p_addr_relay.py b/test/functional/p2p_addr_relay.py index 5c7e27a3a8..caefe86a67 100755 --- a/test/functional/p2p_addr_relay.py +++ b/test/functional/p2p_addr_relay.py @@ -12,7 +12,7 @@ from test_framework.messages import ( NODE_WITNESS, msg_addr, ) -from test_framework.mininode import ( +from test_framework.p2p import ( P2PInterface, ) from test_framework.test_framework import BitcoinTestFramework diff --git a/test/functional/p2p_blockfilters.py b/test/functional/p2p_blockfilters.py index a9e86bd2fc..6475466d79 100755 --- a/test/functional/p2p_blockfilters.py +++ b/test/functional/p2p_blockfilters.py @@ -18,7 +18,7 @@ from test_framework.messages import ( ser_uint256, uint256_from_str, ) -from test_framework.mininode import P2PInterface +from test_framework.p2p import P2PInterface from test_framework.test_framework import BitcoinTestFramework from test_framework.util import ( assert_equal, diff --git a/test/functional/p2p_blocksonly.py b/test/functional/p2p_blocksonly.py index 27e6b669f6..65259f1869 100755 --- a/test/functional/p2p_blocksonly.py +++ b/test/functional/p2p_blocksonly.py @@ -5,7 +5,7 @@ """Test p2p blocksonly""" from test_framework.messages import msg_tx, CTransaction, FromHex -from test_framework.mininode import P2PInterface +from test_framework.p2p import P2PInterface from test_framework.test_framework import BitcoinTestFramework from test_framework.util import assert_equal diff --git a/test/functional/p2p_compactblocks.py b/test/functional/p2p_compactblocks.py index 9a85632513..725390bdd8 100755 --- a/test/functional/p2p_compactblocks.py +++ b/test/functional/p2p_compactblocks.py @@ -11,7 +11,7 @@ import random from test_framework.blocktools import create_block, create_coinbase, add_witness_commitment from test_framework.messages import BlockTransactions, BlockTransactionsRequest, calculate_shortid, CBlock, CBlockHeader, CInv, COutPoint, CTransaction, CTxIn, CTxInWitness, CTxOut, FromHex, HeaderAndShortIDs, msg_no_witness_block, msg_no_witness_blocktxn, msg_cmpctblock, msg_getblocktxn, msg_getdata, msg_getheaders, msg_headers, msg_inv, msg_sendcmpct, msg_sendheaders, msg_tx, msg_block, msg_blocktxn, MSG_BLOCK, MSG_CMPCT_BLOCK, MSG_WITNESS_FLAG, NODE_NETWORK, P2PHeaderAndShortIDs, PrefilledTransaction, ser_uint256, ToHex -from test_framework.mininode import p2p_lock, P2PInterface +from test_framework.p2p import p2p_lock, P2PInterface from test_framework.script import CScript, OP_TRUE, OP_DROP from test_framework.test_framework import BitcoinTestFramework from test_framework.util import assert_equal, wait_until, softfork_active diff --git a/test/functional/p2p_dos_header_tree.py b/test/functional/p2p_dos_header_tree.py index f8552cf53d..7dd8c3146b 100755 --- a/test/functional/p2p_dos_header_tree.py +++ b/test/functional/p2p_dos_header_tree.py @@ -8,7 +8,7 @@ from test_framework.messages import ( CBlockHeader, FromHex, ) -from test_framework.mininode import ( +from test_framework.p2p import ( P2PInterface, msg_headers, ) diff --git a/test/functional/p2p_eviction.py b/test/functional/p2p_eviction.py index b2b3a89aab..e076f8d8df 100755 --- a/test/functional/p2p_eviction.py +++ b/test/functional/p2p_eviction.py @@ -16,7 +16,7 @@ Therefore, this test is limited to the remaining protection criteria. import time from test_framework.test_framework import BitcoinTestFramework -from test_framework.mininode import P2PInterface, P2PDataStore +from test_framework.p2p import P2PInterface, P2PDataStore from test_framework.util import assert_equal, wait_until from test_framework.blocktools import create_block, create_coinbase from test_framework.messages import CTransaction, FromHex, msg_pong, msg_tx diff --git a/test/functional/p2p_feefilter.py b/test/functional/p2p_feefilter.py index 97880b6848..238502951f 100755 --- a/test/functional/p2p_feefilter.py +++ b/test/functional/p2p_feefilter.py @@ -7,7 +7,7 @@ from decimal import Decimal from test_framework.messages import MSG_TX, MSG_WTX, msg_feefilter -from test_framework.mininode import p2p_lock, P2PInterface +from test_framework.p2p import p2p_lock, P2PInterface from test_framework.test_framework import BitcoinTestFramework from test_framework.util import assert_equal diff --git a/test/functional/p2p_filter.py b/test/functional/p2p_filter.py index 608f0186d3..613d96eaad 100755 --- a/test/functional/p2p_filter.py +++ b/test/functional/p2p_filter.py @@ -19,7 +19,7 @@ from test_framework.messages import ( msg_mempool, msg_version, ) -from test_framework.mininode import P2PInterface, p2p_lock +from test_framework.p2p import P2PInterface, p2p_lock from test_framework.script import MAX_SCRIPT_ELEMENT_SIZE from test_framework.test_framework import BitcoinTestFramework diff --git a/test/functional/p2p_fingerprint.py b/test/functional/p2p_fingerprint.py index d743abe681..32a9445e0d 100755 --- a/test/functional/p2p_fingerprint.py +++ b/test/functional/p2p_fingerprint.py @@ -12,7 +12,7 @@ import time from test_framework.blocktools import (create_block, create_coinbase) from test_framework.messages import CInv, MSG_BLOCK -from test_framework.mininode import ( +from test_framework.p2p import ( P2PInterface, msg_headers, msg_block, diff --git a/test/functional/p2p_getaddr_caching.py b/test/functional/p2p_getaddr_caching.py index aeeff2e4de..6622ea9ec2 100755 --- a/test/functional/p2p_getaddr_caching.py +++ b/test/functional/p2p_getaddr_caching.py @@ -12,7 +12,7 @@ from test_framework.messages import ( msg_addr, msg_getaddr, ) -from test_framework.mininode import ( +from test_framework.p2p import ( P2PInterface, p2p_lock ) diff --git a/test/functional/p2p_getdata.py b/test/functional/p2p_getdata.py index d1b11c2c61..51921a8ab5 100755 --- a/test/functional/p2p_getdata.py +++ b/test/functional/p2p_getdata.py @@ -9,7 +9,7 @@ from test_framework.messages import ( CInv, msg_getdata, ) -from test_framework.mininode import P2PInterface +from test_framework.p2p import P2PInterface from test_framework.test_framework import BitcoinTestFramework diff --git a/test/functional/p2p_invalid_block.py b/test/functional/p2p_invalid_block.py index e280a62997..b2c3c5d45f 100755 --- a/test/functional/p2p_invalid_block.py +++ b/test/functional/p2p_invalid_block.py @@ -14,7 +14,7 @@ import copy from test_framework.blocktools import create_block, create_coinbase, create_tx_with_script from test_framework.messages import COIN -from test_framework.mininode import P2PDataStore +from test_framework.p2p import P2PDataStore from test_framework.test_framework import BitcoinTestFramework from test_framework.util import assert_equal diff --git a/test/functional/p2p_invalid_locator.py b/test/functional/p2p_invalid_locator.py index 0155eb21f0..24328c2919 100755 --- a/test/functional/p2p_invalid_locator.py +++ b/test/functional/p2p_invalid_locator.py @@ -6,7 +6,7 @@ """ from test_framework.messages import msg_getheaders, msg_getblocks, MAX_LOCATOR_SZ -from test_framework.mininode import P2PInterface +from test_framework.p2p import P2PInterface from test_framework.test_framework import BitcoinTestFramework diff --git a/test/functional/p2p_invalid_messages.py b/test/functional/p2p_invalid_messages.py index d9a9ae5188..23e573c19a 100755 --- a/test/functional/p2p_invalid_messages.py +++ b/test/functional/p2p_invalid_messages.py @@ -17,7 +17,7 @@ from test_framework.messages import ( MSG_TX, ser_string, ) -from test_framework.mininode import ( +from test_framework.p2p import ( P2PDataStore, P2PInterface, ) diff --git a/test/functional/p2p_invalid_tx.py b/test/functional/p2p_invalid_tx.py index c70a892463..3cd85bc4a3 100755 --- a/test/functional/p2p_invalid_tx.py +++ b/test/functional/p2p_invalid_tx.py @@ -13,7 +13,7 @@ from test_framework.messages import ( CTxIn, CTxOut, ) -from test_framework.mininode import P2PDataStore +from test_framework.p2p import P2PDataStore from test_framework.test_framework import BitcoinTestFramework from test_framework.util import ( assert_equal, diff --git a/test/functional/p2p_leak.py b/test/functional/p2p_leak.py index 0dbf6b3957..f4f883a36d 100755 --- a/test/functional/p2p_leak.py +++ b/test/functional/p2p_leak.py @@ -17,7 +17,7 @@ from test_framework.messages import ( msg_ping, msg_version, ) -from test_framework.mininode import p2p_lock, P2PInterface +from test_framework.p2p import p2p_lock, P2PInterface from test_framework.test_framework import BitcoinTestFramework from test_framework.util import ( assert_equal, diff --git a/test/functional/p2p_leak_tx.py b/test/functional/p2p_leak_tx.py index da30ad5977..9e761db03f 100755 --- a/test/functional/p2p_leak_tx.py +++ b/test/functional/p2p_leak_tx.py @@ -5,7 +5,7 @@ """Test that we don't leak txs to inbound peers that we haven't yet announced to""" from test_framework.messages import msg_getdata, CInv, MSG_TX -from test_framework.mininode import P2PDataStore +from test_framework.p2p import P2PDataStore from test_framework.test_framework import BitcoinTestFramework from test_framework.util import ( assert_equal, diff --git a/test/functional/p2p_nobloomfilter_messages.py b/test/functional/p2p_nobloomfilter_messages.py index accc5dc23c..c2311cb197 100755 --- a/test/functional/p2p_nobloomfilter_messages.py +++ b/test/functional/p2p_nobloomfilter_messages.py @@ -12,7 +12,7 @@ Test that, when bloom filters are not enabled, peers are disconnected if: """ from test_framework.messages import msg_mempool, msg_filteradd, msg_filterload, msg_filterclear -from test_framework.mininode import P2PInterface +from test_framework.p2p import P2PInterface from test_framework.test_framework import BitcoinTestFramework from test_framework.util import assert_equal diff --git a/test/functional/p2p_node_network_limited.py b/test/functional/p2p_node_network_limited.py index 5574cf643f..5d06481d8c 100755 --- a/test/functional/p2p_node_network_limited.py +++ b/test/functional/p2p_node_network_limited.py @@ -9,7 +9,7 @@ and that it responds to getdata requests for blocks correctly: - send a block within 288 + 2 of the tip - disconnect peers who request blocks older than that.""" from test_framework.messages import CInv, MSG_BLOCK, msg_getdata, msg_verack, NODE_NETWORK_LIMITED, NODE_WITNESS -from test_framework.mininode import P2PInterface, p2p_lock +from test_framework.p2p import P2PInterface, p2p_lock from test_framework.test_framework import BitcoinTestFramework from test_framework.util import ( assert_equal, diff --git a/test/functional/p2p_permissions.py b/test/functional/p2p_permissions.py index 254352c816..85ebc0e5a4 100755 --- a/test/functional/p2p_permissions.py +++ b/test/functional/p2p_permissions.py @@ -13,7 +13,7 @@ from test_framework.messages import ( CTxInWitness, FromHex, ) -from test_framework.mininode import P2PDataStore +from test_framework.p2p import P2PDataStore from test_framework.script import ( CScript, OP_TRUE, diff --git a/test/functional/p2p_ping.py b/test/functional/p2p_ping.py index 5f5fd3e104..888e986fba 100755 --- a/test/functional/p2p_ping.py +++ b/test/functional/p2p_ping.py @@ -8,7 +8,7 @@ import time from test_framework.messages import msg_pong -from test_framework.mininode import P2PInterface +from test_framework.p2p import P2PInterface from test_framework.test_framework import BitcoinTestFramework from test_framework.util import assert_equal diff --git a/test/functional/p2p_segwit.py b/test/functional/p2p_segwit.py index ffb4e6d94b..2a559e69fc 100755 --- a/test/functional/p2p_segwit.py +++ b/test/functional/p2p_segwit.py @@ -42,7 +42,7 @@ from test_framework.messages import ( uint256_from_str, FromHex, ) -from test_framework.mininode import ( +from test_framework.p2p import ( P2PInterface, p2p_lock, ) diff --git a/test/functional/p2p_sendheaders.py b/test/functional/p2p_sendheaders.py index cef57de2b8..bc1c182966 100755 --- a/test/functional/p2p_sendheaders.py +++ b/test/functional/p2p_sendheaders.py @@ -87,7 +87,7 @@ e. Announce one more that doesn't connect. """ from test_framework.blocktools import create_block, create_coinbase from test_framework.messages import CInv -from test_framework.mininode import ( +from test_framework.p2p import ( CBlockHeader, NODE_WITNESS, P2PInterface, diff --git a/test/functional/p2p_timeouts.py b/test/functional/p2p_timeouts.py index 5a4fa42988..ce12ce26ce 100755 --- a/test/functional/p2p_timeouts.py +++ b/test/functional/p2p_timeouts.py @@ -24,7 +24,7 @@ from time import sleep from test_framework.messages import msg_ping -from test_framework.mininode import P2PInterface +from test_framework.p2p import P2PInterface from test_framework.test_framework import BitcoinTestFramework diff --git a/test/functional/p2p_tx_download.py b/test/functional/p2p_tx_download.py index 06dab60729..59fab9de0c 100755 --- a/test/functional/p2p_tx_download.py +++ b/test/functional/p2p_tx_download.py @@ -16,7 +16,7 @@ from test_framework.messages import ( msg_inv, msg_notfound, ) -from test_framework.mininode import ( +from test_framework.p2p import ( P2PInterface, p2p_lock, ) diff --git a/test/functional/p2p_unrequested_blocks.py b/test/functional/p2p_unrequested_blocks.py index 6888e90323..36b434bce3 100755 --- a/test/functional/p2p_unrequested_blocks.py +++ b/test/functional/p2p_unrequested_blocks.py @@ -55,7 +55,7 @@ import time from test_framework.blocktools import create_block, create_coinbase, create_tx_with_script from test_framework.messages import CBlockHeader, CInv, MSG_BLOCK, msg_block, msg_headers, msg_inv -from test_framework.mininode import p2p_lock, P2PInterface +from test_framework.p2p import p2p_lock, P2PInterface from test_framework.test_framework import BitcoinTestFramework from test_framework.util import ( assert_equal, diff --git a/test/functional/rpc_blockchain.py b/test/functional/rpc_blockchain.py index 7f4241fb5f..687222dfef 100755 --- a/test/functional/rpc_blockchain.py +++ b/test/functional/rpc_blockchain.py @@ -42,7 +42,7 @@ from test_framework.messages import ( FromHex, msg_block, ) -from test_framework.mininode import ( +from test_framework.p2p import ( P2PInterface, ) diff --git a/test/functional/rpc_net.py b/test/functional/rpc_net.py index 192b60e5d2..f952f91c74 100755 --- a/test/functional/rpc_net.py +++ b/test/functional/rpc_net.py @@ -19,7 +19,7 @@ from test_framework.util import ( p2p_port, wait_until, ) -from test_framework.mininode import P2PInterface +from test_framework.p2p import P2PInterface import test_framework.messages from test_framework.messages import ( NODE_NETWORK, diff --git a/test/functional/test_framework/mininode.py b/test/functional/test_framework/mininode.py deleted file mode 100755 index 5a5a94f0d6..0000000000 --- a/test/functional/test_framework/mininode.py +++ /dev/null @@ -1,681 +0,0 @@ -#!/usr/bin/env python3 -# Copyright (c) 2010 ArtForz -- public domain half-a-node -# Copyright (c) 2012 Jeff Garzik -# Copyright (c) 2010-2020 The Bitcoin Core developers -# Distributed under the MIT software license, see the accompanying -# file COPYING or http://www.opensource.org/licenses/mit-license.php. -"""Bitcoin P2P network half-a-node. - -This python code was modified from ArtForz' public domain half-a-node, as -found in the mini-node branch of http://github.com/jgarzik/pynode. - -P2PConnection: A low-level connection object to a node's P2P interface -P2PInterface: A high-level interface object for communicating to a node over P2P -P2PDataStore: A p2p interface class that keeps a store of transactions and blocks - and can respond correctly to getdata and getheaders messages -P2PTxInvStore: A p2p interface class that inherits from P2PDataStore, and keeps - a count of how many times each txid has been announced.""" - -import asyncio -from collections import defaultdict -from io import BytesIO -import logging -import struct -import sys -import threading - -from test_framework.messages import ( - CBlockHeader, - MAX_HEADERS_RESULTS, - MIN_VERSION_SUPPORTED, - msg_addr, - msg_block, - MSG_BLOCK, - msg_blocktxn, - msg_cfcheckpt, - msg_cfheaders, - msg_cfilter, - msg_cmpctblock, - msg_feefilter, - msg_filteradd, - msg_filterclear, - msg_filterload, - msg_getaddr, - msg_getblocks, - msg_getblocktxn, - msg_getdata, - msg_getheaders, - msg_headers, - msg_inv, - msg_mempool, - msg_merkleblock, - msg_notfound, - msg_ping, - msg_pong, - msg_sendcmpct, - msg_sendheaders, - msg_tx, - MSG_TX, - MSG_TYPE_MASK, - msg_verack, - msg_version, - MSG_WTX, - msg_wtxidrelay, - NODE_NETWORK, - NODE_WITNESS, - sha256, -) -from test_framework.util import wait_until - -logger = logging.getLogger("TestFramework.mininode") - -MESSAGEMAP = { - b"addr": msg_addr, - b"block": msg_block, - b"blocktxn": msg_blocktxn, - b"cfcheckpt": msg_cfcheckpt, - b"cfheaders": msg_cfheaders, - b"cfilter": msg_cfilter, - b"cmpctblock": msg_cmpctblock, - b"feefilter": msg_feefilter, - b"filteradd": msg_filteradd, - b"filterclear": msg_filterclear, - b"filterload": msg_filterload, - b"getaddr": msg_getaddr, - b"getblocks": msg_getblocks, - b"getblocktxn": msg_getblocktxn, - b"getdata": msg_getdata, - b"getheaders": msg_getheaders, - b"headers": msg_headers, - b"inv": msg_inv, - b"mempool": msg_mempool, - b"merkleblock": msg_merkleblock, - b"notfound": msg_notfound, - b"ping": msg_ping, - b"pong": msg_pong, - b"sendcmpct": msg_sendcmpct, - b"sendheaders": msg_sendheaders, - b"tx": msg_tx, - b"verack": msg_verack, - b"version": msg_version, - b"wtxidrelay": msg_wtxidrelay, -} - -MAGIC_BYTES = { - "mainnet": b"\xf9\xbe\xb4\xd9", # mainnet - "testnet3": b"\x0b\x11\x09\x07", # testnet3 - "regtest": b"\xfa\xbf\xb5\xda", # regtest -} - - -class P2PConnection(asyncio.Protocol): - """A low-level connection object to a node's P2P interface. - - This class is responsible for: - - - opening and closing the TCP connection to the node - - reading bytes from and writing bytes to the socket - - deserializing and serializing the P2P message header - - logging messages as they are sent and received - - This class contains no logic for handing the P2P message payloads. It must be - sub-classed and the on_message() callback overridden.""" - - def __init__(self): - # The underlying transport of the connection. - # Should only call methods on this from the NetworkThread, c.f. call_soon_threadsafe - self._transport = None - - @property - def is_connected(self): - return self._transport is not None - - def peer_connect(self, dstaddr, dstport, *, net, timeout_factor): - assert not self.is_connected - self.timeout_factor = timeout_factor - self.dstaddr = dstaddr - self.dstport = dstport - # The initial message to send after the connection was made: - self.on_connection_send_msg = None - self.recvbuf = b"" - self.magic_bytes = MAGIC_BYTES[net] - logger.debug('Connecting to Bitcoin Node: %s:%d' % (self.dstaddr, self.dstport)) - - loop = NetworkThread.network_event_loop - conn_gen_unsafe = loop.create_connection(lambda: self, host=self.dstaddr, port=self.dstport) - conn_gen = lambda: loop.call_soon_threadsafe(loop.create_task, conn_gen_unsafe) - return conn_gen - - def peer_disconnect(self): - # Connection could have already been closed by other end. - NetworkThread.network_event_loop.call_soon_threadsafe(lambda: self._transport and self._transport.abort()) - - # Connection and disconnection methods - - def connection_made(self, transport): - """asyncio callback when a connection is opened.""" - assert not self._transport - logger.debug("Connected & Listening: %s:%d" % (self.dstaddr, self.dstport)) - self._transport = transport - if self.on_connection_send_msg: - self.send_message(self.on_connection_send_msg) - self.on_connection_send_msg = None # Never used again - self.on_open() - - def connection_lost(self, exc): - """asyncio callback when a connection is closed.""" - if exc: - logger.warning("Connection lost to {}:{} due to {}".format(self.dstaddr, self.dstport, exc)) - else: - logger.debug("Closed connection to: %s:%d" % (self.dstaddr, self.dstport)) - self._transport = None - self.recvbuf = b"" - self.on_close() - - # Socket read methods - - def data_received(self, t): - """asyncio callback when data is read from the socket.""" - if len(t) > 0: - self.recvbuf += t - self._on_data() - - def _on_data(self): - """Try to read P2P messages from the recv buffer. - - This method reads data from the buffer in a loop. It deserializes, - parses and verifies the P2P header, then passes the P2P payload to - the on_message callback for processing.""" - try: - while True: - if len(self.recvbuf) < 4: - return - if self.recvbuf[:4] != self.magic_bytes: - raise ValueError("magic bytes mismatch: {} != {}".format(repr(self.magic_bytes), repr(self.recvbuf))) - if len(self.recvbuf) < 4 + 12 + 4 + 4: - return - msgtype = self.recvbuf[4:4+12].split(b"\x00", 1)[0] - msglen = struct.unpack(" 500: - log_message += "... (msg truncated)" - logger.debug(log_message) - - -class P2PInterface(P2PConnection): - """A high-level P2P interface class for communicating with a Bitcoin node. - - This class provides high-level callbacks for processing P2P message - payloads, as well as convenience methods for interacting with the - node over P2P. - - Individual testcases should subclass this and override the on_* methods - if they want to alter message handling behaviour.""" - def __init__(self): - super().__init__() - - # Track number of messages of each type received. - # Should be read-only in a test. - self.message_count = defaultdict(int) - - # Track the most recent message of each type. - # To wait for a message to be received, pop that message from - # this and use wait_until. - self.last_message = {} - - # A count of the number of ping messages we've sent to the node - self.ping_counter = 1 - - # The network services received from the peer - self.nServices = 0 - - def peer_connect(self, *args, services=NODE_NETWORK|NODE_WITNESS, send_version=True, **kwargs): - create_conn = super().peer_connect(*args, **kwargs) - - if send_version: - # Send a version msg - vt = msg_version() - vt.nServices = services - vt.addrTo.ip = self.dstaddr - vt.addrTo.port = self.dstport - vt.addrFrom.ip = "0.0.0.0" - vt.addrFrom.port = 0 - self.on_connection_send_msg = vt # Will be sent soon after connection_made - - return create_conn - - # Message receiving methods - - def on_message(self, message): - """Receive message and dispatch message to appropriate callback. - - We keep a count of how many of each message type has been received - and the most recent message of each type.""" - with p2p_lock: - try: - msgtype = message.msgtype.decode('ascii') - self.message_count[msgtype] += 1 - self.last_message[msgtype] = message - getattr(self, 'on_' + msgtype)(message) - except: - print("ERROR delivering %s (%s)" % (repr(message), sys.exc_info()[0])) - raise - - # Callback methods. Can be overridden by subclasses in individual test - # cases to provide custom message handling behaviour. - - def on_open(self): - pass - - def on_close(self): - pass - - def on_addr(self, message): pass - def on_block(self, message): pass - def on_blocktxn(self, message): pass - def on_cfcheckpt(self, message): pass - def on_cfheaders(self, message): pass - def on_cfilter(self, message): pass - def on_cmpctblock(self, message): pass - def on_feefilter(self, message): pass - def on_filteradd(self, message): pass - def on_filterclear(self, message): pass - def on_filterload(self, message): pass - def on_getaddr(self, message): pass - def on_getblocks(self, message): pass - def on_getblocktxn(self, message): pass - def on_getdata(self, message): pass - def on_getheaders(self, message): pass - def on_headers(self, message): pass - def on_mempool(self, message): pass - def on_merkleblock(self, message): pass - def on_notfound(self, message): pass - def on_pong(self, message): pass - def on_sendcmpct(self, message): pass - def on_sendheaders(self, message): pass - def on_tx(self, message): pass - def on_wtxidrelay(self, message): pass - - def on_inv(self, message): - want = msg_getdata() - for i in message.inv: - if i.type != 0: - want.inv.append(i) - if len(want.inv): - self.send_message(want) - - def on_ping(self, message): - self.send_message(msg_pong(message.nonce)) - - def on_verack(self, message): - pass - - def on_version(self, message): - assert message.nVersion >= MIN_VERSION_SUPPORTED, "Version {} received. Test framework only supports versions greater than {}".format(message.nVersion, MIN_VERSION_SUPPORTED) - if message.nVersion >= 70016: - self.send_message(msg_wtxidrelay()) - self.send_message(msg_verack()) - self.nServices = message.nServices - - # Connection helper methods - - def wait_until(self, test_function_in, *, timeout=60, check_connected=True): - def test_function(): - if check_connected: - assert self.is_connected - return test_function_in() - - wait_until(test_function, timeout=timeout, lock=p2p_lock, timeout_factor=self.timeout_factor) - - def wait_for_disconnect(self, timeout=60): - test_function = lambda: not self.is_connected - self.wait_until(test_function, timeout=timeout, check_connected=False) - - # Message receiving helper methods - - def wait_for_tx(self, txid, timeout=60): - def test_function(): - if not self.last_message.get('tx'): - return False - return self.last_message['tx'].tx.rehash() == txid - - self.wait_until(test_function, timeout=timeout) - - def wait_for_block(self, blockhash, timeout=60): - def test_function(): - return self.last_message.get("block") and self.last_message["block"].block.rehash() == blockhash - - self.wait_until(test_function, timeout=timeout) - - def wait_for_header(self, blockhash, timeout=60): - def test_function(): - last_headers = self.last_message.get('headers') - if not last_headers: - return False - return last_headers.headers[0].rehash() == int(blockhash, 16) - - self.wait_until(test_function, timeout=timeout) - - def wait_for_merkleblock(self, blockhash, timeout=60): - def test_function(): - last_filtered_block = self.last_message.get('merkleblock') - if not last_filtered_block: - return False - return last_filtered_block.merkleblock.header.rehash() == int(blockhash, 16) - - self.wait_until(test_function, timeout=timeout) - - def wait_for_getdata(self, hash_list, timeout=60): - """Waits for a getdata message. - - The object hashes in the inventory vector must match the provided hash_list.""" - def test_function(): - last_data = self.last_message.get("getdata") - if not last_data: - return False - return [x.hash for x in last_data.inv] == hash_list - - self.wait_until(test_function, timeout=timeout) - - def wait_for_getheaders(self, timeout=60): - """Waits for a getheaders message. - - Receiving any getheaders message will satisfy the predicate. the last_message["getheaders"] - value must be explicitly cleared before calling this method, or this will return - immediately with success. TODO: change this method to take a hash value and only - return true if the correct block header has been requested.""" - def test_function(): - return self.last_message.get("getheaders") - - self.wait_until(test_function, timeout=timeout) - - def wait_for_inv(self, expected_inv, timeout=60): - """Waits for an INV message and checks that the first inv object in the message was as expected.""" - if len(expected_inv) > 1: - raise NotImplementedError("wait_for_inv() will only verify the first inv object") - - def test_function(): - return self.last_message.get("inv") and \ - self.last_message["inv"].inv[0].type == expected_inv[0].type and \ - self.last_message["inv"].inv[0].hash == expected_inv[0].hash - - self.wait_until(test_function, timeout=timeout) - - def wait_for_verack(self, timeout=60): - def test_function(): - return "verack" in self.last_message - - self.wait_until(test_function, timeout=timeout) - - # Message sending helper functions - - def send_and_ping(self, message, timeout=60): - self.send_message(message) - self.sync_with_ping(timeout=timeout) - - # Sync up with the node - def sync_with_ping(self, timeout=60): - self.send_message(msg_ping(nonce=self.ping_counter)) - - def test_function(): - return self.last_message.get("pong") and self.last_message["pong"].nonce == self.ping_counter - - self.wait_until(test_function, timeout=timeout) - self.ping_counter += 1 - - -# One lock for synchronizing all data access between the network event loop (see -# NetworkThread below) and the thread running the test logic. For simplicity, -# P2PConnection acquires this lock whenever delivering a message to a P2PInterface. -# This lock should be acquired in the thread running the test logic to synchronize -# access to any data shared with the P2PInterface or P2PConnection. -p2p_lock = threading.Lock() - - -class NetworkThread(threading.Thread): - network_event_loop = None - - def __init__(self): - super().__init__(name="NetworkThread") - # There is only one event loop and no more than one thread must be created - assert not self.network_event_loop - - NetworkThread.network_event_loop = asyncio.new_event_loop() - - def run(self): - """Start the network thread.""" - self.network_event_loop.run_forever() - - def close(self, timeout=10): - """Close the connections and network event loop.""" - self.network_event_loop.call_soon_threadsafe(self.network_event_loop.stop) - wait_until(lambda: not self.network_event_loop.is_running(), timeout=timeout) - self.network_event_loop.close() - self.join(timeout) - # Safe to remove event loop. - NetworkThread.network_event_loop = None - -class P2PDataStore(P2PInterface): - """A P2P data store class. - - Keeps a block and transaction store and responds correctly to getdata and getheaders requests.""" - - def __init__(self): - super().__init__() - # store of blocks. key is block hash, value is a CBlock object - self.block_store = {} - self.last_block_hash = '' - # store of txs. key is txid, value is a CTransaction object - self.tx_store = {} - self.getdata_requests = [] - - def on_getdata(self, message): - """Check for the tx/block in our stores and if found, reply with an inv message.""" - for inv in message.inv: - self.getdata_requests.append(inv.hash) - if (inv.type & MSG_TYPE_MASK) == MSG_TX and inv.hash in self.tx_store.keys(): - self.send_message(msg_tx(self.tx_store[inv.hash])) - elif (inv.type & MSG_TYPE_MASK) == MSG_BLOCK and inv.hash in self.block_store.keys(): - self.send_message(msg_block(self.block_store[inv.hash])) - else: - logger.debug('getdata message type {} received.'.format(hex(inv.type))) - - def on_getheaders(self, message): - """Search back through our block store for the locator, and reply with a headers message if found.""" - - locator, hash_stop = message.locator, message.hashstop - - # Assume that the most recent block added is the tip - if not self.block_store: - return - - headers_list = [self.block_store[self.last_block_hash]] - while headers_list[-1].sha256 not in locator.vHave: - # Walk back through the block store, adding headers to headers_list - # as we go. - prev_block_hash = headers_list[-1].hashPrevBlock - if prev_block_hash in self.block_store: - prev_block_header = CBlockHeader(self.block_store[prev_block_hash]) - headers_list.append(prev_block_header) - if prev_block_header.sha256 == hash_stop: - # if this is the hashstop header, stop here - break - else: - logger.debug('block hash {} not found in block store'.format(hex(prev_block_hash))) - break - - # Truncate the list if there are too many headers - headers_list = headers_list[:-MAX_HEADERS_RESULTS - 1:-1] - response = msg_headers(headers_list) - - if response is not None: - self.send_message(response) - - def send_blocks_and_test(self, blocks, node, *, success=True, force_send=False, reject_reason=None, expect_disconnect=False, timeout=60): - """Send blocks to test node and test whether the tip advances. - - - add all blocks to our block_store - - send a headers message for the final block - - the on_getheaders handler will ensure that any getheaders are responded to - - if force_send is False: wait for getdata for each of the blocks. The on_getdata handler will - ensure that any getdata messages are responded to. Otherwise send the full block unsolicited. - - if success is True: assert that the node's tip advances to the most recent block - - if success is False: assert that the node's tip doesn't advance - - if reject_reason is set: assert that the correct reject message is logged""" - - with p2p_lock: - for block in blocks: - self.block_store[block.sha256] = block - self.last_block_hash = block.sha256 - - reject_reason = [reject_reason] if reject_reason else [] - with node.assert_debug_log(expected_msgs=reject_reason): - if force_send: - for b in blocks: - self.send_message(msg_block(block=b)) - else: - self.send_message(msg_headers([CBlockHeader(block) for block in blocks])) - self.wait_until( - lambda: blocks[-1].sha256 in self.getdata_requests, - timeout=timeout, - check_connected=success, - ) - - if expect_disconnect: - self.wait_for_disconnect(timeout=timeout) - else: - self.sync_with_ping(timeout=timeout) - - if success: - self.wait_until(lambda: node.getbestblockhash() == blocks[-1].hash, timeout=timeout) - else: - assert node.getbestblockhash() != blocks[-1].hash - - def send_txs_and_test(self, txs, node, *, success=True, expect_disconnect=False, reject_reason=None): - """Send txs to test node and test whether they're accepted to the mempool. - - - add all txs to our tx_store - - send tx messages for all txs - - if success is True/False: assert that the txs are/are not accepted to the mempool - - if expect_disconnect is True: Skip the sync with ping - - if reject_reason is set: assert that the correct reject message is logged.""" - - with p2p_lock: - for tx in txs: - self.tx_store[tx.sha256] = tx - - reject_reason = [reject_reason] if reject_reason else [] - with node.assert_debug_log(expected_msgs=reject_reason): - for tx in txs: - self.send_message(msg_tx(tx)) - - if expect_disconnect: - self.wait_for_disconnect() - else: - self.sync_with_ping() - - raw_mempool = node.getrawmempool() - if success: - # Check that all txs are now in the mempool - for tx in txs: - assert tx.hash in raw_mempool, "{} not found in mempool".format(tx.hash) - else: - # Check that none of the txs are now in the mempool - for tx in txs: - assert tx.hash not in raw_mempool, "{} tx found in mempool".format(tx.hash) - -class P2PTxInvStore(P2PInterface): - """A P2PInterface which stores a count of how many times each txid has been announced.""" - def __init__(self): - super().__init__() - self.tx_invs_received = defaultdict(int) - - def on_inv(self, message): - super().on_inv(message) # Send getdata in response. - # Store how many times invs have been received for each tx. - for i in message.inv: - if (i.type == MSG_TX) or (i.type == MSG_WTX): - # save txid - self.tx_invs_received[i.hash] += 1 - - def get_invs(self): - with p2p_lock: - return list(self.tx_invs_received.keys()) - - def wait_for_broadcast(self, txns, timeout=60): - """Waits for the txns (list of txids) to complete initial broadcast. - The mempool should mark unbroadcast=False for these transactions. - """ - # Wait until invs have been received (and getdatas sent) for each txid. - self.wait_until(lambda: set(self.tx_invs_received.keys()) == set([int(tx, 16) for tx in txns]), timeout=timeout) - # Flush messages and wait for the getdatas to be processed - self.sync_with_ping() diff --git a/test/functional/test_framework/p2p.py b/test/functional/test_framework/p2p.py new file mode 100755 index 0000000000..38c3c5551a --- /dev/null +++ b/test/functional/test_framework/p2p.py @@ -0,0 +1,681 @@ +#!/usr/bin/env python3 +# Copyright (c) 2010 ArtForz -- public domain half-a-node +# Copyright (c) 2012 Jeff Garzik +# Copyright (c) 2010-2020 The Bitcoin Core developers +# Distributed under the MIT software license, see the accompanying +# file COPYING or http://www.opensource.org/licenses/mit-license.php. +"""Bitcoin P2P network half-a-node. + +This python code was modified from ArtForz' public domain half-a-node, as +found in the mini-node branch of http://github.com/jgarzik/pynode. + +P2PConnection: A low-level connection object to a node's P2P interface +P2PInterface: A high-level interface object for communicating to a node over P2P +P2PDataStore: A p2p interface class that keeps a store of transactions and blocks + and can respond correctly to getdata and getheaders messages +P2PTxInvStore: A p2p interface class that inherits from P2PDataStore, and keeps + a count of how many times each txid has been announced.""" + +import asyncio +from collections import defaultdict +from io import BytesIO +import logging +import struct +import sys +import threading + +from test_framework.messages import ( + CBlockHeader, + MAX_HEADERS_RESULTS, + MIN_VERSION_SUPPORTED, + msg_addr, + msg_block, + MSG_BLOCK, + msg_blocktxn, + msg_cfcheckpt, + msg_cfheaders, + msg_cfilter, + msg_cmpctblock, + msg_feefilter, + msg_filteradd, + msg_filterclear, + msg_filterload, + msg_getaddr, + msg_getblocks, + msg_getblocktxn, + msg_getdata, + msg_getheaders, + msg_headers, + msg_inv, + msg_mempool, + msg_merkleblock, + msg_notfound, + msg_ping, + msg_pong, + msg_sendcmpct, + msg_sendheaders, + msg_tx, + MSG_TX, + MSG_TYPE_MASK, + msg_verack, + msg_version, + MSG_WTX, + msg_wtxidrelay, + NODE_NETWORK, + NODE_WITNESS, + sha256, +) +from test_framework.util import wait_until + +logger = logging.getLogger("TestFramework.p2p") + +MESSAGEMAP = { + b"addr": msg_addr, + b"block": msg_block, + b"blocktxn": msg_blocktxn, + b"cfcheckpt": msg_cfcheckpt, + b"cfheaders": msg_cfheaders, + b"cfilter": msg_cfilter, + b"cmpctblock": msg_cmpctblock, + b"feefilter": msg_feefilter, + b"filteradd": msg_filteradd, + b"filterclear": msg_filterclear, + b"filterload": msg_filterload, + b"getaddr": msg_getaddr, + b"getblocks": msg_getblocks, + b"getblocktxn": msg_getblocktxn, + b"getdata": msg_getdata, + b"getheaders": msg_getheaders, + b"headers": msg_headers, + b"inv": msg_inv, + b"mempool": msg_mempool, + b"merkleblock": msg_merkleblock, + b"notfound": msg_notfound, + b"ping": msg_ping, + b"pong": msg_pong, + b"sendcmpct": msg_sendcmpct, + b"sendheaders": msg_sendheaders, + b"tx": msg_tx, + b"verack": msg_verack, + b"version": msg_version, + b"wtxidrelay": msg_wtxidrelay, +} + +MAGIC_BYTES = { + "mainnet": b"\xf9\xbe\xb4\xd9", # mainnet + "testnet3": b"\x0b\x11\x09\x07", # testnet3 + "regtest": b"\xfa\xbf\xb5\xda", # regtest +} + + +class P2PConnection(asyncio.Protocol): + """A low-level connection object to a node's P2P interface. + + This class is responsible for: + + - opening and closing the TCP connection to the node + - reading bytes from and writing bytes to the socket + - deserializing and serializing the P2P message header + - logging messages as they are sent and received + + This class contains no logic for handing the P2P message payloads. It must be + sub-classed and the on_message() callback overridden.""" + + def __init__(self): + # The underlying transport of the connection. + # Should only call methods on this from the NetworkThread, c.f. call_soon_threadsafe + self._transport = None + + @property + def is_connected(self): + return self._transport is not None + + def peer_connect(self, dstaddr, dstport, *, net, timeout_factor): + assert not self.is_connected + self.timeout_factor = timeout_factor + self.dstaddr = dstaddr + self.dstport = dstport + # The initial message to send after the connection was made: + self.on_connection_send_msg = None + self.recvbuf = b"" + self.magic_bytes = MAGIC_BYTES[net] + logger.debug('Connecting to Bitcoin Node: %s:%d' % (self.dstaddr, self.dstport)) + + loop = NetworkThread.network_event_loop + conn_gen_unsafe = loop.create_connection(lambda: self, host=self.dstaddr, port=self.dstport) + conn_gen = lambda: loop.call_soon_threadsafe(loop.create_task, conn_gen_unsafe) + return conn_gen + + def peer_disconnect(self): + # Connection could have already been closed by other end. + NetworkThread.network_event_loop.call_soon_threadsafe(lambda: self._transport and self._transport.abort()) + + # Connection and disconnection methods + + def connection_made(self, transport): + """asyncio callback when a connection is opened.""" + assert not self._transport + logger.debug("Connected & Listening: %s:%d" % (self.dstaddr, self.dstport)) + self._transport = transport + if self.on_connection_send_msg: + self.send_message(self.on_connection_send_msg) + self.on_connection_send_msg = None # Never used again + self.on_open() + + def connection_lost(self, exc): + """asyncio callback when a connection is closed.""" + if exc: + logger.warning("Connection lost to {}:{} due to {}".format(self.dstaddr, self.dstport, exc)) + else: + logger.debug("Closed connection to: %s:%d" % (self.dstaddr, self.dstport)) + self._transport = None + self.recvbuf = b"" + self.on_close() + + # Socket read methods + + def data_received(self, t): + """asyncio callback when data is read from the socket.""" + if len(t) > 0: + self.recvbuf += t + self._on_data() + + def _on_data(self): + """Try to read P2P messages from the recv buffer. + + This method reads data from the buffer in a loop. It deserializes, + parses and verifies the P2P header, then passes the P2P payload to + the on_message callback for processing.""" + try: + while True: + if len(self.recvbuf) < 4: + return + if self.recvbuf[:4] != self.magic_bytes: + raise ValueError("magic bytes mismatch: {} != {}".format(repr(self.magic_bytes), repr(self.recvbuf))) + if len(self.recvbuf) < 4 + 12 + 4 + 4: + return + msgtype = self.recvbuf[4:4+12].split(b"\x00", 1)[0] + msglen = struct.unpack(" 500: + log_message += "... (msg truncated)" + logger.debug(log_message) + + +class P2PInterface(P2PConnection): + """A high-level P2P interface class for communicating with a Bitcoin node. + + This class provides high-level callbacks for processing P2P message + payloads, as well as convenience methods for interacting with the + node over P2P. + + Individual testcases should subclass this and override the on_* methods + if they want to alter message handling behaviour.""" + def __init__(self): + super().__init__() + + # Track number of messages of each type received. + # Should be read-only in a test. + self.message_count = defaultdict(int) + + # Track the most recent message of each type. + # To wait for a message to be received, pop that message from + # this and use wait_until. + self.last_message = {} + + # A count of the number of ping messages we've sent to the node + self.ping_counter = 1 + + # The network services received from the peer + self.nServices = 0 + + def peer_connect(self, *args, services=NODE_NETWORK|NODE_WITNESS, send_version=True, **kwargs): + create_conn = super().peer_connect(*args, **kwargs) + + if send_version: + # Send a version msg + vt = msg_version() + vt.nServices = services + vt.addrTo.ip = self.dstaddr + vt.addrTo.port = self.dstport + vt.addrFrom.ip = "0.0.0.0" + vt.addrFrom.port = 0 + self.on_connection_send_msg = vt # Will be sent soon after connection_made + + return create_conn + + # Message receiving methods + + def on_message(self, message): + """Receive message and dispatch message to appropriate callback. + + We keep a count of how many of each message type has been received + and the most recent message of each type.""" + with p2p_lock: + try: + msgtype = message.msgtype.decode('ascii') + self.message_count[msgtype] += 1 + self.last_message[msgtype] = message + getattr(self, 'on_' + msgtype)(message) + except: + print("ERROR delivering %s (%s)" % (repr(message), sys.exc_info()[0])) + raise + + # Callback methods. Can be overridden by subclasses in individual test + # cases to provide custom message handling behaviour. + + def on_open(self): + pass + + def on_close(self): + pass + + def on_addr(self, message): pass + def on_block(self, message): pass + def on_blocktxn(self, message): pass + def on_cfcheckpt(self, message): pass + def on_cfheaders(self, message): pass + def on_cfilter(self, message): pass + def on_cmpctblock(self, message): pass + def on_feefilter(self, message): pass + def on_filteradd(self, message): pass + def on_filterclear(self, message): pass + def on_filterload(self, message): pass + def on_getaddr(self, message): pass + def on_getblocks(self, message): pass + def on_getblocktxn(self, message): pass + def on_getdata(self, message): pass + def on_getheaders(self, message): pass + def on_headers(self, message): pass + def on_mempool(self, message): pass + def on_merkleblock(self, message): pass + def on_notfound(self, message): pass + def on_pong(self, message): pass + def on_sendcmpct(self, message): pass + def on_sendheaders(self, message): pass + def on_tx(self, message): pass + def on_wtxidrelay(self, message): pass + + def on_inv(self, message): + want = msg_getdata() + for i in message.inv: + if i.type != 0: + want.inv.append(i) + if len(want.inv): + self.send_message(want) + + def on_ping(self, message): + self.send_message(msg_pong(message.nonce)) + + def on_verack(self, message): + pass + + def on_version(self, message): + assert message.nVersion >= MIN_VERSION_SUPPORTED, "Version {} received. Test framework only supports versions greater than {}".format(message.nVersion, MIN_VERSION_SUPPORTED) + if message.nVersion >= 70016: + self.send_message(msg_wtxidrelay()) + self.send_message(msg_verack()) + self.nServices = message.nServices + + # Connection helper methods + + def wait_until(self, test_function_in, *, timeout=60, check_connected=True): + def test_function(): + if check_connected: + assert self.is_connected + return test_function_in() + + wait_until(test_function, timeout=timeout, lock=p2p_lock, timeout_factor=self.timeout_factor) + + def wait_for_disconnect(self, timeout=60): + test_function = lambda: not self.is_connected + self.wait_until(test_function, timeout=timeout, check_connected=False) + + # Message receiving helper methods + + def wait_for_tx(self, txid, timeout=60): + def test_function(): + if not self.last_message.get('tx'): + return False + return self.last_message['tx'].tx.rehash() == txid + + self.wait_until(test_function, timeout=timeout) + + def wait_for_block(self, blockhash, timeout=60): + def test_function(): + return self.last_message.get("block") and self.last_message["block"].block.rehash() == blockhash + + self.wait_until(test_function, timeout=timeout) + + def wait_for_header(self, blockhash, timeout=60): + def test_function(): + last_headers = self.last_message.get('headers') + if not last_headers: + return False + return last_headers.headers[0].rehash() == int(blockhash, 16) + + self.wait_until(test_function, timeout=timeout) + + def wait_for_merkleblock(self, blockhash, timeout=60): + def test_function(): + last_filtered_block = self.last_message.get('merkleblock') + if not last_filtered_block: + return False + return last_filtered_block.merkleblock.header.rehash() == int(blockhash, 16) + + self.wait_until(test_function, timeout=timeout) + + def wait_for_getdata(self, hash_list, timeout=60): + """Waits for a getdata message. + + The object hashes in the inventory vector must match the provided hash_list.""" + def test_function(): + last_data = self.last_message.get("getdata") + if not last_data: + return False + return [x.hash for x in last_data.inv] == hash_list + + self.wait_until(test_function, timeout=timeout) + + def wait_for_getheaders(self, timeout=60): + """Waits for a getheaders message. + + Receiving any getheaders message will satisfy the predicate. the last_message["getheaders"] + value must be explicitly cleared before calling this method, or this will return + immediately with success. TODO: change this method to take a hash value and only + return true if the correct block header has been requested.""" + def test_function(): + return self.last_message.get("getheaders") + + self.wait_until(test_function, timeout=timeout) + + def wait_for_inv(self, expected_inv, timeout=60): + """Waits for an INV message and checks that the first inv object in the message was as expected.""" + if len(expected_inv) > 1: + raise NotImplementedError("wait_for_inv() will only verify the first inv object") + + def test_function(): + return self.last_message.get("inv") and \ + self.last_message["inv"].inv[0].type == expected_inv[0].type and \ + self.last_message["inv"].inv[0].hash == expected_inv[0].hash + + self.wait_until(test_function, timeout=timeout) + + def wait_for_verack(self, timeout=60): + def test_function(): + return "verack" in self.last_message + + self.wait_until(test_function, timeout=timeout) + + # Message sending helper functions + + def send_and_ping(self, message, timeout=60): + self.send_message(message) + self.sync_with_ping(timeout=timeout) + + # Sync up with the node + def sync_with_ping(self, timeout=60): + self.send_message(msg_ping(nonce=self.ping_counter)) + + def test_function(): + return self.last_message.get("pong") and self.last_message["pong"].nonce == self.ping_counter + + self.wait_until(test_function, timeout=timeout) + self.ping_counter += 1 + + +# One lock for synchronizing all data access between the network event loop (see +# NetworkThread below) and the thread running the test logic. For simplicity, +# P2PConnection acquires this lock whenever delivering a message to a P2PInterface. +# This lock should be acquired in the thread running the test logic to synchronize +# access to any data shared with the P2PInterface or P2PConnection. +p2p_lock = threading.Lock() + + +class NetworkThread(threading.Thread): + network_event_loop = None + + def __init__(self): + super().__init__(name="NetworkThread") + # There is only one event loop and no more than one thread must be created + assert not self.network_event_loop + + NetworkThread.network_event_loop = asyncio.new_event_loop() + + def run(self): + """Start the network thread.""" + self.network_event_loop.run_forever() + + def close(self, timeout=10): + """Close the connections and network event loop.""" + self.network_event_loop.call_soon_threadsafe(self.network_event_loop.stop) + wait_until(lambda: not self.network_event_loop.is_running(), timeout=timeout) + self.network_event_loop.close() + self.join(timeout) + # Safe to remove event loop. + NetworkThread.network_event_loop = None + +class P2PDataStore(P2PInterface): + """A P2P data store class. + + Keeps a block and transaction store and responds correctly to getdata and getheaders requests.""" + + def __init__(self): + super().__init__() + # store of blocks. key is block hash, value is a CBlock object + self.block_store = {} + self.last_block_hash = '' + # store of txs. key is txid, value is a CTransaction object + self.tx_store = {} + self.getdata_requests = [] + + def on_getdata(self, message): + """Check for the tx/block in our stores and if found, reply with an inv message.""" + for inv in message.inv: + self.getdata_requests.append(inv.hash) + if (inv.type & MSG_TYPE_MASK) == MSG_TX and inv.hash in self.tx_store.keys(): + self.send_message(msg_tx(self.tx_store[inv.hash])) + elif (inv.type & MSG_TYPE_MASK) == MSG_BLOCK and inv.hash in self.block_store.keys(): + self.send_message(msg_block(self.block_store[inv.hash])) + else: + logger.debug('getdata message type {} received.'.format(hex(inv.type))) + + def on_getheaders(self, message): + """Search back through our block store for the locator, and reply with a headers message if found.""" + + locator, hash_stop = message.locator, message.hashstop + + # Assume that the most recent block added is the tip + if not self.block_store: + return + + headers_list = [self.block_store[self.last_block_hash]] + while headers_list[-1].sha256 not in locator.vHave: + # Walk back through the block store, adding headers to headers_list + # as we go. + prev_block_hash = headers_list[-1].hashPrevBlock + if prev_block_hash in self.block_store: + prev_block_header = CBlockHeader(self.block_store[prev_block_hash]) + headers_list.append(prev_block_header) + if prev_block_header.sha256 == hash_stop: + # if this is the hashstop header, stop here + break + else: + logger.debug('block hash {} not found in block store'.format(hex(prev_block_hash))) + break + + # Truncate the list if there are too many headers + headers_list = headers_list[:-MAX_HEADERS_RESULTS - 1:-1] + response = msg_headers(headers_list) + + if response is not None: + self.send_message(response) + + def send_blocks_and_test(self, blocks, node, *, success=True, force_send=False, reject_reason=None, expect_disconnect=False, timeout=60): + """Send blocks to test node and test whether the tip advances. + + - add all blocks to our block_store + - send a headers message for the final block + - the on_getheaders handler will ensure that any getheaders are responded to + - if force_send is False: wait for getdata for each of the blocks. The on_getdata handler will + ensure that any getdata messages are responded to. Otherwise send the full block unsolicited. + - if success is True: assert that the node's tip advances to the most recent block + - if success is False: assert that the node's tip doesn't advance + - if reject_reason is set: assert that the correct reject message is logged""" + + with p2p_lock: + for block in blocks: + self.block_store[block.sha256] = block + self.last_block_hash = block.sha256 + + reject_reason = [reject_reason] if reject_reason else [] + with node.assert_debug_log(expected_msgs=reject_reason): + if force_send: + for b in blocks: + self.send_message(msg_block(block=b)) + else: + self.send_message(msg_headers([CBlockHeader(block) for block in blocks])) + self.wait_until( + lambda: blocks[-1].sha256 in self.getdata_requests, + timeout=timeout, + check_connected=success, + ) + + if expect_disconnect: + self.wait_for_disconnect(timeout=timeout) + else: + self.sync_with_ping(timeout=timeout) + + if success: + self.wait_until(lambda: node.getbestblockhash() == blocks[-1].hash, timeout=timeout) + else: + assert node.getbestblockhash() != blocks[-1].hash + + def send_txs_and_test(self, txs, node, *, success=True, expect_disconnect=False, reject_reason=None): + """Send txs to test node and test whether they're accepted to the mempool. + + - add all txs to our tx_store + - send tx messages for all txs + - if success is True/False: assert that the txs are/are not accepted to the mempool + - if expect_disconnect is True: Skip the sync with ping + - if reject_reason is set: assert that the correct reject message is logged.""" + + with p2p_lock: + for tx in txs: + self.tx_store[tx.sha256] = tx + + reject_reason = [reject_reason] if reject_reason else [] + with node.assert_debug_log(expected_msgs=reject_reason): + for tx in txs: + self.send_message(msg_tx(tx)) + + if expect_disconnect: + self.wait_for_disconnect() + else: + self.sync_with_ping() + + raw_mempool = node.getrawmempool() + if success: + # Check that all txs are now in the mempool + for tx in txs: + assert tx.hash in raw_mempool, "{} not found in mempool".format(tx.hash) + else: + # Check that none of the txs are now in the mempool + for tx in txs: + assert tx.hash not in raw_mempool, "{} tx found in mempool".format(tx.hash) + +class P2PTxInvStore(P2PInterface): + """A P2PInterface which stores a count of how many times each txid has been announced.""" + def __init__(self): + super().__init__() + self.tx_invs_received = defaultdict(int) + + def on_inv(self, message): + super().on_inv(message) # Send getdata in response. + # Store how many times invs have been received for each tx. + for i in message.inv: + if (i.type == MSG_TX) or (i.type == MSG_WTX): + # save txid + self.tx_invs_received[i.hash] += 1 + + def get_invs(self): + with p2p_lock: + return list(self.tx_invs_received.keys()) + + def wait_for_broadcast(self, txns, timeout=60): + """Waits for the txns (list of txids) to complete initial broadcast. + The mempool should mark unbroadcast=False for these transactions. + """ + # Wait until invs have been received (and getdatas sent) for each txid. + self.wait_until(lambda: set(self.tx_invs_received.keys()) == set([int(tx, 16) for tx in txns]), timeout=timeout) + # Flush messages and wait for the getdatas to be processed + self.sync_with_ping() diff --git a/test/functional/test_framework/test_framework.py b/test/functional/test_framework/test_framework.py index 8d402d4888..3555343095 100755 --- a/test/functional/test_framework/test_framework.py +++ b/test/functional/test_framework/test_framework.py @@ -21,7 +21,7 @@ import time from .authproxy import JSONRPCException from . import coverage from .test_node import TestNode -from .mininode import NetworkThread +from .p2p import NetworkThread from .util import ( MAX_NODES, PortSeed, diff --git a/test/functional/wallet_resendwallettransactions.py b/test/functional/wallet_resendwallettransactions.py index 36c4748a09..ab266dd883 100755 --- a/test/functional/wallet_resendwallettransactions.py +++ b/test/functional/wallet_resendwallettransactions.py @@ -7,7 +7,7 @@ import time from test_framework.blocktools import create_block, create_coinbase from test_framework.messages import ToHex -from test_framework.mininode import P2PTxInvStore, p2p_lock +from test_framework.p2p import P2PTxInvStore, p2p_lock from test_framework.test_framework import BitcoinTestFramework from test_framework.util import assert_equal, wait_until -- cgit v1.2.3 From 5e8df3312e47a73e747ee892face55ed9ababeea Mon Sep 17 00:00:00 2001 From: John Newbery Date: Mon, 17 Aug 2020 10:45:44 +0100 Subject: test: resort imports --- test/functional/example_test.py | 2 +- test/functional/feature_versionbits_warning.py | 2 +- test/functional/mempool_persist.py | 2 +- test/functional/p2p_addr_relay.py | 4 +--- test/functional/p2p_eviction.py | 6 +++--- test/functional/p2p_feefilter.py | 2 +- test/functional/rpc_blockchain.py | 22 ++++++++++------------ test/functional/rpc_net.py | 12 ++++++------ test/functional/test_framework/test_framework.py | 2 +- 9 files changed, 25 insertions(+), 29 deletions(-) (limited to 'test') diff --git a/test/functional/example_test.py b/test/functional/example_test.py index c69b078c6e..32a167bcd5 100755 --- a/test/functional/example_test.py +++ b/test/functional/example_test.py @@ -18,9 +18,9 @@ from test_framework.blocktools import (create_block, create_coinbase) from test_framework.messages import CInv, MSG_BLOCK from test_framework.p2p import ( P2PInterface, - p2p_lock, msg_block, msg_getdata, + p2p_lock, ) from test_framework.test_framework import BitcoinTestFramework from test_framework.util import ( diff --git a/test/functional/feature_versionbits_warning.py b/test/functional/feature_versionbits_warning.py index 376a27e5f5..78c295b6e8 100755 --- a/test/functional/feature_versionbits_warning.py +++ b/test/functional/feature_versionbits_warning.py @@ -12,7 +12,7 @@ import re from test_framework.blocktools import create_block, create_coinbase from test_framework.messages import msg_block -from test_framework.p2p import P2PInterface, p2p_lock +from test_framework.p2p import p2p_lock, P2PInterface from test_framework.test_framework import BitcoinTestFramework from test_framework.util import wait_until diff --git a/test/functional/mempool_persist.py b/test/functional/mempool_persist.py index cf2cd611bd..00854656f8 100755 --- a/test/functional/mempool_persist.py +++ b/test/functional/mempool_persist.py @@ -39,8 +39,8 @@ from decimal import Decimal import os import time -from test_framework.test_framework import BitcoinTestFramework from test_framework.p2p import P2PTxInvStore +from test_framework.test_framework import BitcoinTestFramework from test_framework.util import ( assert_equal, assert_greater_than_or_equal, diff --git a/test/functional/p2p_addr_relay.py b/test/functional/p2p_addr_relay.py index caefe86a67..80f262d0d3 100755 --- a/test/functional/p2p_addr_relay.py +++ b/test/functional/p2p_addr_relay.py @@ -12,9 +12,7 @@ from test_framework.messages import ( NODE_WITNESS, msg_addr, ) -from test_framework.p2p import ( - P2PInterface, -) +from test_framework.p2p import P2PInterface from test_framework.test_framework import BitcoinTestFramework from test_framework.util import ( assert_equal, diff --git a/test/functional/p2p_eviction.py b/test/functional/p2p_eviction.py index e076f8d8df..848df4a585 100755 --- a/test/functional/p2p_eviction.py +++ b/test/functional/p2p_eviction.py @@ -15,11 +15,11 @@ Therefore, this test is limited to the remaining protection criteria. import time -from test_framework.test_framework import BitcoinTestFramework -from test_framework.p2p import P2PInterface, P2PDataStore -from test_framework.util import assert_equal, wait_until from test_framework.blocktools import create_block, create_coinbase from test_framework.messages import CTransaction, FromHex, msg_pong, msg_tx +from test_framework.p2p import P2PDataStore, P2PInterface +from test_framework.test_framework import BitcoinTestFramework +from test_framework.util import assert_equal, wait_until class SlowP2PDataStore(P2PDataStore): diff --git a/test/functional/p2p_feefilter.py b/test/functional/p2p_feefilter.py index 238502951f..63b6107df6 100755 --- a/test/functional/p2p_feefilter.py +++ b/test/functional/p2p_feefilter.py @@ -7,7 +7,7 @@ from decimal import Decimal from test_framework.messages import MSG_TX, MSG_WTX, msg_feefilter -from test_framework.p2p import p2p_lock, P2PInterface +from test_framework.p2p import P2PInterface, p2p_lock from test_framework.test_framework import BitcoinTestFramework from test_framework.util import assert_equal diff --git a/test/functional/rpc_blockchain.py b/test/functional/rpc_blockchain.py index 687222dfef..c005584485 100755 --- a/test/functional/rpc_blockchain.py +++ b/test/functional/rpc_blockchain.py @@ -22,16 +22,6 @@ from decimal import Decimal import http.client import subprocess -from test_framework.test_framework import BitcoinTestFramework -from test_framework.util import ( - assert_equal, - assert_greater_than, - assert_greater_than_or_equal, - assert_raises, - assert_raises_rpc_error, - assert_is_hex_string, - assert_is_hash_string, -) from test_framework.blocktools import ( create_block, create_coinbase, @@ -42,8 +32,16 @@ from test_framework.messages import ( FromHex, msg_block, ) -from test_framework.p2p import ( - P2PInterface, +from test_framework.p2p import P2PInterface +from test_framework.test_framework import BitcoinTestFramework +from test_framework.util import ( + assert_equal, + assert_greater_than, + assert_greater_than_or_equal, + assert_raises, + assert_raises_rpc_error, + assert_is_hex_string, + assert_is_hash_string, ) diff --git a/test/functional/rpc_net.py b/test/functional/rpc_net.py index f952f91c74..068a76f36b 100755 --- a/test/functional/rpc_net.py +++ b/test/functional/rpc_net.py @@ -9,6 +9,12 @@ Tests correspond to code in rpc/net.cpp. from decimal import Decimal +from test_framework.p2p import P2PInterface +import test_framework.messages +from test_framework.messages import ( + NODE_NETWORK, + NODE_WITNESS, +) from test_framework.test_framework import BitcoinTestFramework from test_framework.util import ( assert_equal, @@ -19,12 +25,6 @@ from test_framework.util import ( p2p_port, wait_until, ) -from test_framework.p2p import P2PInterface -import test_framework.messages -from test_framework.messages import ( - NODE_NETWORK, - NODE_WITNESS, -) def assert_net_servicesnames(servicesflag, servicenames): diff --git a/test/functional/test_framework/test_framework.py b/test/functional/test_framework/test_framework.py index 3555343095..2a60f8e0c1 100755 --- a/test/functional/test_framework/test_framework.py +++ b/test/functional/test_framework/test_framework.py @@ -20,8 +20,8 @@ import time from .authproxy import JSONRPCException from . import coverage -from .test_node import TestNode from .p2p import NetworkThread +from .test_node import TestNode from .util import ( MAX_NODES, PortSeed, -- cgit v1.2.3 From d5800da5199527a366024bc80cad7fcca17d5c4a Mon Sep 17 00:00:00 2001 From: John Newbery Date: Mon, 17 Aug 2020 10:10:44 +0100 Subject: [test] Remove final references to mininode --- test/functional/README.md | 4 ++-- test/functional/example_test.py | 2 +- test/functional/feature_block.py | 2 +- test/functional/p2p_permissions.py | 2 +- test/functional/p2p_segwit.py | 4 ++-- test/functional/test_framework/messages.py | 2 +- test/functional/test_framework/p2p.py | 12 ++++++++---- test/functional/test_framework/test_node.py | 4 ++-- 8 files changed, 18 insertions(+), 14 deletions(-) (limited to 'test') diff --git a/test/functional/README.md b/test/functional/README.md index aff5f714f2..0d85a74074 100644 --- a/test/functional/README.md +++ b/test/functional/README.md @@ -127,8 +127,8 @@ Base class for functional tests. #### [util.py](test_framework/util.py) Generally useful functions. -#### [mininode.py](test_framework/mininode.py) -Basic code to support P2P connectivity to a bitcoind. +#### [p2p.py](test_framework/p2p.py) +Test objects for interacting with a bitcoind node over the p2p interface. #### [script.py](test_framework/script.py) Utilities for manipulating transaction scripts (originally from python-bitcoinlib) diff --git a/test/functional/example_test.py b/test/functional/example_test.py index 32a167bcd5..ae69371984 100755 --- a/test/functional/example_test.py +++ b/test/functional/example_test.py @@ -167,7 +167,7 @@ class ExampleTest(BitcoinTestFramework): height = self.nodes[0].getblockcount() for _ in range(10): - # Use the mininode and blocktools functionality to manually build a block + # Use the blocktools functionality to manually build a block. # Calling the generate() rpc is easier, but this allows us to exactly # control the blocks and transactions. block = create_block(self.tip, create_coinbase(height+1), self.block_time) diff --git a/test/functional/feature_block.py b/test/functional/feature_block.py index 81483b5a87..efafcfaec3 100755 --- a/test/functional/feature_block.py +++ b/test/functional/feature_block.py @@ -53,7 +53,7 @@ from test_framework.test_framework import BitcoinTestFramework from test_framework.util import assert_equal from data import invalid_txs -# Use this class for tests that require behavior other than normal "mininode" behavior. +# Use this class for tests that require behavior other than normal p2p behavior. # For now, it is used to serialize a bloated varint (b64). class CBrokenBlock(CBlock): def initialize(self, base_block): diff --git a/test/functional/p2p_permissions.py b/test/functional/p2p_permissions.py index 85ebc0e5a4..b467ee174e 100755 --- a/test/functional/p2p_permissions.py +++ b/test/functional/p2p_permissions.py @@ -109,7 +109,7 @@ class P2PPermissionsTests(BitcoinTestFramework): self.sync_all() self.log.debug("Create a connection from a forcerelay peer that rebroadcasts raw txs") - # A python mininode is needed to send the raw transaction directly. If a full node was used, it could only + # A test framework p2p connection is needed to send the raw transaction directly. If a full node was used, it could only # rebroadcast via the inv-getdata mechanism. However, even for forcerelay connections, a full node would # currently not request a txid that is already in the mempool. self.restart_node(1, extra_args=["-whitelist=forcerelay@127.0.0.1"]) diff --git a/test/functional/p2p_segwit.py b/test/functional/p2p_segwit.py index 2a559e69fc..49b083b998 100755 --- a/test/functional/p2p_segwit.py +++ b/test/functional/p2p_segwit.py @@ -153,8 +153,8 @@ class TestP2PConn(P2PInterface): self.lastgetdata = [] self.wtxidrelay = wtxidrelay - # Avoid sending out msg_getdata in the mininode thread as a reply to invs. - # They are not needed and would only lead to races because we send msg_getdata out in the test thread + # Don't send getdata message replies to invs automatically. + # We'll send the getdata messages explicitly in the test logic. def on_inv(self, message): pass diff --git a/test/functional/test_framework/messages.py b/test/functional/test_framework/messages.py index 5207b563a1..bd4a53876e 100755 --- a/test/functional/test_framework/messages.py +++ b/test/functional/test_framework/messages.py @@ -32,7 +32,7 @@ from test_framework.util import hex_str_to_bytes, assert_equal MIN_VERSION_SUPPORTED = 60001 MY_VERSION = 70016 # past wtxid relay -MY_SUBVERSION = b"/python-mininode-tester:0.0.3/" +MY_SUBVERSION = b"/python-p2p-tester:0.0.3/" MY_RELAY = 1 # from version 70001 onwards, fRelay should be appended to version messages (BIP37) MAX_LOCATOR_SZ = 101 diff --git a/test/functional/test_framework/p2p.py b/test/functional/test_framework/p2p.py index 38c3c5551a..57c77e60b5 100755 --- a/test/functional/test_framework/p2p.py +++ b/test/functional/test_framework/p2p.py @@ -4,10 +4,14 @@ # Copyright (c) 2010-2020 The Bitcoin Core developers # Distributed under the MIT software license, see the accompanying # file COPYING or http://www.opensource.org/licenses/mit-license.php. -"""Bitcoin P2P network half-a-node. - -This python code was modified from ArtForz' public domain half-a-node, as -found in the mini-node branch of http://github.com/jgarzik/pynode. +"""Test objects for interacting with a bitcoind node over the p2p protocol. + +The P2PInterface objects interact with the bitcoind nodes under test using the +node's p2p interface. They can be used to send messages to the node, and +callbacks can be registered that execute when messages are received from the +node. Messages are sent to/received from the node on an asyncio event loop. +State held inside the objects must be guarded by the p2p_lock to avoid data +races between the main testing thread and the event loop. P2PConnection: A low-level connection object to a node's P2P interface P2PInterface: A high-level interface object for communicating to a node over P2P diff --git a/test/functional/test_framework/test_node.py b/test/functional/test_framework/test_node.py index 5eba554a42..5c7a883c43 100755 --- a/test/functional/test_framework/test_node.py +++ b/test/functional/test_framework/test_node.py @@ -551,7 +551,7 @@ class TestNode(): assert self.p2ps, self._node_msg("No p2p connection") return self.p2ps[0] - def num_connected_mininodes(self): + def num_test_p2p_connections(self): """Return number of test framework p2p connections to the node.""" return len([peer for peer in self.getpeerinfo() if peer['subver'] == MY_SUBVERSION]) @@ -560,7 +560,7 @@ class TestNode(): for p in self.p2ps: p.peer_disconnect() del self.p2ps[:] - wait_until(lambda: self.num_connected_mininodes() == 0) + wait_until(lambda: self.num_test_p2p_connections() == 0) class TestNodeCLIAttr: -- cgit v1.2.3