Skip to content

Commit

Permalink
[refactor] clarify tests by referencing p2p objects directly
Browse files Browse the repository at this point in the history
Use object returned from add_p2p_connection to refer to
p2ps. Add a test class attribute if it needs to be used across
many methods. Don't use the p2p property.
  • Loading branch information
glozow committed Sep 10, 2020
1 parent bd60a9a commit 784f757
Show file tree
Hide file tree
Showing 17 changed files with 73 additions and 73 deletions.
12 changes: 6 additions & 6 deletions test/functional/example_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def run_test(self):
"""Main test logic"""

# Create P2P connections will wait for a verack to make sure the connection is fully up
self.nodes[0].add_p2p_connection(BaseNode())
peer_messaging = self.nodes[0].add_p2p_connection(BaseNode())

# Generating a block on one of the nodes will get us out of IBD
blocks = [int(self.nodes[0].generate(nblocks=1)[0], 16)]
Expand Down Expand Up @@ -173,7 +173,7 @@ def run_test(self):
block.solve()
block_message = msg_block(block)
# Send message is used to send a P2P message to the node over our P2PInterface
self.nodes[0].p2p.send_message(block_message)
peer_messaging.send_message(block_message)
self.tip = block.sha256
blocks.append(self.tip)
self.block_time += 1
Expand All @@ -191,25 +191,25 @@ def run_test(self):
self.log.info("Add P2P connection to node2")
self.nodes[0].disconnect_p2ps()

self.nodes[2].add_p2p_connection(BaseNode())
peer_receiving = self.nodes[2].add_p2p_connection(BaseNode())

self.log.info("Test that node2 propagates all the blocks to us")

getdata_request = msg_getdata()
for block in blocks:
getdata_request.inv.append(CInv(MSG_BLOCK, block))
self.nodes[2].p2p.send_message(getdata_request)
peer_receiving.send_message(getdata_request)

# wait_until() will loop until a predicate condition is met. Use it to test properties of the
# P2PInterface objects.
self.nodes[2].p2p.wait_until(lambda: sorted(blocks) == sorted(list(self.nodes[2].p2p.block_receive_map.keys())), timeout=5)
peer_receiving.wait_until(lambda: sorted(blocks) == sorted(list(peer_receiving.block_receive_map.keys())), timeout=5)

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 p2p.wait_until() acquires this global lock internally when testing the predicate.
with p2p_lock:
for block in self.nodes[2].p2p.block_receive_map.values():
for block in peer_receiving.block_receive_map.values():
assert_equal(block, 1)


Expand Down
6 changes: 3 additions & 3 deletions test/functional/feature_block.py
Original file line number Diff line number Diff line change
Expand Up @@ -1386,14 +1386,14 @@ def bootstrap_p2p(self, timeout=10):
"""Add a P2P connection to the node.
Helper to connect and wait for version handshake."""
self.nodes[0].add_p2p_connection(P2PDataStore())
self.helper_peer = self.nodes[0].add_p2p_connection(P2PDataStore())
# We need to wait for the initial getheaders from the peer before we
# start populating our blockstore. If we don't, then we may run ahead
# to the next subtest before we receive the getheaders. We'd then send
# an INV for the next block and receive two getheaders - one for the
# IBD and one for the INV. We'd respond to both and could get
# unexpectedly disconnected if the DoS score for that error is 50.
self.nodes[0].p2p.wait_for_getheaders(timeout=timeout)
self.helper_peer.wait_for_getheaders(timeout=timeout)

def reconnect_p2p(self, timeout=60):
"""Tear down and bootstrap the P2P connection to the node.
Expand All @@ -1407,7 +1407,7 @@ def send_blocks(self, blocks, success=True, reject_reason=None, force_send=False
"""Sends blocks to test node. Syncs and verifies that tip has advanced to most recent block.
Call with success = False if the tip shouldn't advance to the most recent block."""
self.nodes[0].p2p.send_blocks_and_test(blocks, self.nodes[0], success=success, reject_reason=reject_reason, force_send=force_send, timeout=timeout, expect_disconnect=reconnect)
self.helper_peer.send_blocks_and_test(blocks, self.nodes[0], success=success, reject_reason=reject_reason, force_send=force_send, timeout=timeout, expect_disconnect=reconnect)

if reconnect:
self.reconnect_p2p(timeout=timeout)
Expand Down
14 changes: 7 additions & 7 deletions test/functional/feature_cltv.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def test_cltv_info(self, *, is_active):
)

def run_test(self):
self.nodes[0].add_p2p_connection(P2PInterface())
peer = self.nodes[0].add_p2p_connection(P2PInterface())

self.test_cltv_info(is_active=False)

Expand All @@ -99,7 +99,7 @@ def run_test(self):
block.solve()

self.test_cltv_info(is_active=False) # Not active as of current tip and next block does not need to obey rules
self.nodes[0].p2p.send_and_ping(msg_block(block))
peer.send_and_ping(msg_block(block))
self.test_cltv_info(is_active=True) # Not active as of current tip, but next block must obey rules
assert_equal(self.nodes[0].getbestblockhash(), block.hash)

Expand All @@ -111,9 +111,9 @@ def run_test(self):
block.solve()

with self.nodes[0].assert_debug_log(expected_msgs=['{}, bad-version(0x00000003)'.format(block.hash)]):
self.nodes[0].p2p.send_and_ping(msg_block(block))
peer.send_and_ping(msg_block(block))
assert_equal(int(self.nodes[0].getbestblockhash(), 16), tip)
self.nodes[0].p2p.sync_with_ping()
peer.sync_with_ping()

self.log.info("Test that invalid-according-to-cltv transactions cannot appear in a block")
block.nVersion = 4
Expand All @@ -136,9 +136,9 @@ def run_test(self):
block.solve()

with self.nodes[0].assert_debug_log(expected_msgs=['CheckInputScripts on {} failed with non-mandatory-script-verify-flag (Negative locktime)'.format(block.vtx[-1].hash)]):
self.nodes[0].p2p.send_and_ping(msg_block(block))
peer.send_and_ping(msg_block(block))
assert_equal(int(self.nodes[0].getbestblockhash(), 16), tip)
self.nodes[0].p2p.sync_with_ping()
peer.sync_with_ping()

self.log.info("Test that a version 4 block with a valid-according-to-CLTV transaction is accepted")
spendtx = cltv_validate(self.nodes[0], spendtx, CLTV_HEIGHT - 1)
Expand All @@ -150,7 +150,7 @@ def run_test(self):
block.solve()

self.test_cltv_info(is_active=True) # Not active as of current tip, but next block must obey rules
self.nodes[0].p2p.send_and_ping(msg_block(block))
peer.send_and_ping(msg_block(block))
self.test_cltv_info(is_active=True) # Active as of current tip
assert_equal(int(self.nodes[0].getbestblockhash(), 16), block.sha256)

Expand Down
4 changes: 2 additions & 2 deletions test/functional/feature_csv_activation.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,10 @@ def send_blocks(self, blocks, success=True, reject_reason=None):
"""Sends blocks to test node. Syncs and verifies that tip has advanced to most recent block.
Call with success = False if the tip shouldn't advance to the most recent block."""
self.nodes[0].p2p.send_blocks_and_test(blocks, self.nodes[0], success=success, reject_reason=reject_reason)
self.helper_peer.send_blocks_and_test(blocks, self.nodes[0], success=success, reject_reason=reject_reason)

def run_test(self):
self.nodes[0].add_p2p_connection(P2PDataStore())
self.helper_peer = self.nodes[0].add_p2p_connection(P2PDataStore())

self.log.info("Generate blocks in the past for coinbase outputs.")
long_past_time = int(time.time()) - 600 * 1000 # enough to build up to 1000 blocks 10 minutes apart without worrying about getting into the future
Expand Down
14 changes: 7 additions & 7 deletions test/functional/feature_dersig.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def test_dersig_info(self, *, is_active):
)

def run_test(self):
self.nodes[0].add_p2p_connection(P2PInterface())
peer = self.nodes[0].add_p2p_connection(P2PInterface())

self.test_dersig_info(is_active=False)

Expand All @@ -84,7 +84,7 @@ def run_test(self):
block.solve()

self.test_dersig_info(is_active=False) # Not active as of current tip and next block does not need to obey rules
self.nodes[0].p2p.send_and_ping(msg_block(block))
peer.send_and_ping(msg_block(block))
self.test_dersig_info(is_active=True) # Not active as of current tip, but next block must obey rules
assert_equal(self.nodes[0].getbestblockhash(), block.hash)

Expand All @@ -97,9 +97,9 @@ def run_test(self):
block.solve()

with self.nodes[0].assert_debug_log(expected_msgs=['{}, bad-version(0x00000002)'.format(block.hash)]):
self.nodes[0].p2p.send_and_ping(msg_block(block))
peer.send_and_ping(msg_block(block))
assert_equal(int(self.nodes[0].getbestblockhash(), 16), tip)
self.nodes[0].p2p.sync_with_ping()
peer.sync_with_ping()

self.log.info("Test that transactions with non-DER signatures cannot appear in a block")
block.nVersion = 3
Expand All @@ -123,9 +123,9 @@ def run_test(self):
block.solve()

with self.nodes[0].assert_debug_log(expected_msgs=['CheckInputScripts on {} failed with non-mandatory-script-verify-flag (Non-canonical DER signature)'.format(block.vtx[-1].hash)]):
self.nodes[0].p2p.send_and_ping(msg_block(block))
peer.send_and_ping(msg_block(block))
assert_equal(int(self.nodes[0].getbestblockhash(), 16), tip)
self.nodes[0].p2p.sync_with_ping()
peer.sync_with_ping()

self.log.info("Test that a version 3 block with a DERSIG-compliant transaction is accepted")
block.vtx[1] = create_transaction(self.nodes[0], self.coinbase_txids[1], self.nodeaddress, amount=1.0)
Expand All @@ -134,7 +134,7 @@ def run_test(self):
block.solve()

self.test_dersig_info(is_active=True) # Not active as of current tip, but next block must obey rules
self.nodes[0].p2p.send_and_ping(msg_block(block))
peer.send_and_ping(msg_block(block))
self.test_dersig_info(is_active=True) # Active as of current tip
assert_equal(int(self.nodes[0].getbestblockhash(), 16), block.sha256)

Expand Down
8 changes: 4 additions & 4 deletions test/functional/feature_maxuploadtarget.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,16 +145,16 @@ def run_test(self):
self.restart_node(0, ["-whitelist=download@127.0.0.1", "-maxuploadtarget=1"])

# Reconnect to self.nodes[0]
self.nodes[0].add_p2p_connection(TestP2PConn())
peer = self.nodes[0].add_p2p_connection(TestP2PConn())

#retrieve 20 blocks which should be enough to break the 1MB limit
getdata_request.inv = [CInv(MSG_BLOCK, big_new_block)]
for i in range(20):
self.nodes[0].p2p.send_and_ping(getdata_request)
assert_equal(self.nodes[0].p2p.block_receive_map[big_new_block], i+1)
peer.send_and_ping(getdata_request)
assert_equal(peer.block_receive_map[big_new_block], i+1)

getdata_request.inv = [CInv(MSG_BLOCK, big_old_block)]
self.nodes[0].p2p.send_and_ping(getdata_request)
peer.send_and_ping(getdata_request)

self.log.info("Peer still connected after trying to download old block (download permission)")
peer_info = self.nodes[0].getpeerinfo()
Expand Down
6 changes: 3 additions & 3 deletions test/functional/feature_versionbits_warning.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,23 +61,23 @@ def versionbits_in_alert_file(self):

def run_test(self):
node = self.nodes[0]
node.add_p2p_connection(P2PInterface())
peer = node.add_p2p_connection(P2PInterface())

node_deterministic_address = node.get_deterministic_priv_key().address
# Mine one period worth of blocks
node.generatetoaddress(VB_PERIOD, node_deterministic_address)

self.log.info("Check that there is no warning if previous VB_BLOCKS have <VB_THRESHOLD blocks with unknown versionbits version.")
# Build one period of blocks with < VB_THRESHOLD blocks signaling some unknown bit
self.send_blocks_with_version(node.p2p, VB_THRESHOLD - 1, VB_UNKNOWN_VERSION)
self.send_blocks_with_version(peer, VB_THRESHOLD - 1, VB_UNKNOWN_VERSION)
node.generatetoaddress(VB_PERIOD - VB_THRESHOLD + 1, node_deterministic_address)

# Check that we're not getting any versionbit-related errors in get*info()
assert not VB_PATTERN.match(node.getmininginfo()["warnings"])
assert not VB_PATTERN.match(node.getnetworkinfo()["warnings"])

# Build one period of blocks with VB_THRESHOLD blocks signaling some unknown bit
self.send_blocks_with_version(node.p2p, VB_THRESHOLD, VB_UNKNOWN_VERSION)
self.send_blocks_with_version(peer, VB_THRESHOLD, VB_UNKNOWN_VERSION)
node.generatetoaddress(VB_PERIOD - VB_THRESHOLD, node_deterministic_address)

self.log.info("Check that there is a warning if previous VB_BLOCKS have >=VB_THRESHOLD blocks with unknown versionbits version.")
Expand Down
4 changes: 2 additions & 2 deletions test/functional/mempool_packages.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def chain_transaction(self, node, parent_txid, vout, value, fee, num_outputs):

def run_test(self):
# Mine some blocks and have them mature.
self.nodes[0].add_p2p_connection(P2PTxInvStore()) # keep track of invs
peer_inv_store = self.nodes[0].add_p2p_connection(P2PTxInvStore()) # keep track of invs
self.nodes[0].generate(101)
utxo = self.nodes[0].listunspent(10)
txid = utxo[0]['txid']
Expand All @@ -80,7 +80,7 @@ def run_test(self):

# Wait until mempool transactions have passed initial broadcast (sent inv and received getdata)
# Otherwise, getrawmempool may be inconsistent with getmempoolentry if unbroadcast changes in between
self.nodes[0].p2p.wait_for_broadcast(witness_chain)
peer_inv_store.wait_for_broadcast(witness_chain)

# Check mempool has MAX_ANCESTORS transactions in it, and descendant and ancestor
# count and fees should look correct
Expand Down
6 changes: 3 additions & 3 deletions test/functional/mining_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,9 +234,9 @@ def chain_tip(b_hash, *, status='headers-only', branchlen=1):
assert_raises_rpc_error(-25, 'time-too-old', lambda: node.submitheader(hexdata=CBlockHeader(bad_block_time).serialize().hex()))

# Should ask for the block from a p2p node, if they announce the header as well:
node.add_p2p_connection(P2PDataStore())
node.p2p.wait_for_getheaders(timeout=5) # Drop the first getheaders
node.p2p.send_blocks_and_test(blocks=[block], node=node)
peer = node.add_p2p_connection(P2PDataStore())
peer.wait_for_getheaders(timeout=5) # Drop the first getheaders
peer.send_blocks_and_test(blocks=[block], node=node)
# Must be active now:
assert chain_tip(block.hash, status='active', branchlen=0) in node.getchaintips()

Expand Down
10 changes: 5 additions & 5 deletions test/functional/p2p_blocksonly.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def set_test_params(self):
self.extra_args = [["-blocksonly"]]

def run_test(self):
self.nodes[0].add_p2p_connection(P2PInterface())
block_relay_peer = self.nodes[0].add_p2p_connection(P2PInterface())

self.log.info('Check that txs from p2p are rejected and result in disconnect')
prevtx = self.nodes[0].getblock(self.nodes[0].getblockhash(1), 2)['tx'][0]
Expand All @@ -41,20 +41,20 @@ def run_test(self):
)['hex']
assert_equal(self.nodes[0].getnetworkinfo()['localrelay'], False)
with self.nodes[0].assert_debug_log(['transaction sent in violation of protocol peer=0']):
self.nodes[0].p2p.send_message(msg_tx(FromHex(CTransaction(), sigtx)))
self.nodes[0].p2p.wait_for_disconnect()
block_relay_peer.send_message(msg_tx(FromHex(CTransaction(), sigtx)))
block_relay_peer.wait_for_disconnect()
assert_equal(self.nodes[0].getmempoolinfo()['size'], 0)

# Remove the disconnected peer and add a new one.
del self.nodes[0].p2ps[0]
self.nodes[0].add_p2p_connection(P2PInterface())
tx_relay_peer = self.nodes[0].add_p2p_connection(P2PInterface())

self.log.info('Check that txs from rpc are not rejected and relayed to other peers')
assert_equal(self.nodes[0].getpeerinfo()[0]['relaytxes'], True)
txid = self.nodes[0].testmempoolaccept([sigtx])[0]['txid']
with self.nodes[0].assert_debug_log(['received getdata for: wtx {} peer=1'.format(txid)]):
self.nodes[0].sendrawtransaction(sigtx)
self.nodes[0].p2p.wait_for_tx(txid)
tx_relay_peer.wait_for_tx(txid)
assert_equal(self.nodes[0].getmempoolinfo()['size'], 1)

self.log.info('Check that txs from forcerelay peers are not rejected and relayed to others')
Expand Down
16 changes: 8 additions & 8 deletions test/functional/p2p_dos_header_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ def run_test(self):
self.headers_fork = [FromHex(CBlockHeader(), h) for h in self.headers_fork]

self.log.info("Feed all non-fork headers, including and up to the first checkpoint")
self.nodes[0].add_p2p_connection(P2PInterface())
self.nodes[0].p2p.send_and_ping(msg_headers(self.headers))
peer_checkpoint = self.nodes[0].add_p2p_connection(P2PInterface())
peer_checkpoint.send_and_ping(msg_headers(self.headers))
assert {
'height': 546,
'hash': '000000002a936ca763904c3c35fce2f3556c559c0214345d31b1bcebf76acb70',
Expand All @@ -57,14 +57,14 @@ def run_test(self):

self.log.info("Feed all fork headers (fails due to checkpoint)")
with self.nodes[0].assert_debug_log(['bad-fork-prior-to-checkpoint']):
self.nodes[0].p2p.send_message(msg_headers(self.headers_fork))
self.nodes[0].p2p.wait_for_disconnect()
peer_checkpoint.send_message(msg_headers(self.headers_fork))
peer_checkpoint.wait_for_disconnect()

self.log.info("Feed all fork headers (succeeds without checkpoint)")
# On node 0 it succeeds because checkpoints are disabled
self.restart_node(0, extra_args=['-nocheckpoints'])
self.nodes[0].add_p2p_connection(P2PInterface())
self.nodes[0].p2p.send_and_ping(msg_headers(self.headers_fork))
peer_no_checkpoint = self.nodes[0].add_p2p_connection(P2PInterface())
peer_no_checkpoint.send_and_ping(msg_headers(self.headers_fork))
assert {
"height": 2,
"hash": "00000000b0494bd6c3d5ff79c497cfce40831871cbf39b1bc28bd1dac817dc39",
Expand All @@ -73,8 +73,8 @@ def run_test(self):
} in self.nodes[0].getchaintips()

# On node 1 it succeeds because no checkpoint has been reached yet by a chain tip
self.nodes[1].add_p2p_connection(P2PInterface())
self.nodes[1].p2p.send_and_ping(msg_headers(self.headers_fork))
peer_before_checkpoint = self.nodes[1].add_p2p_connection(P2PInterface())
peer_before_checkpoint.send_and_ping(msg_headers(self.headers_fork))
assert {
"height": 2,
"hash": "00000000b0494bd6c3d5ff79c497cfce40831871cbf39b1bc28bd1dac817dc39",
Expand Down
2 changes: 1 addition & 1 deletion test/functional/p2p_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ def test_msg_mempool(self):
self.log.debug("Send a mempool msg after connecting and check that the tx is received")
self.nodes[0].add_p2p_connection(filter_peer)
filter_peer.send_and_ping(filter_peer.watch_filter_init)
self.nodes[0].p2p.send_message(msg_mempool())
filter_peer.send_message(msg_mempool())
filter_peer.wait_for_tx(txid)

def test_frelay_false(self, filter_peer):
Expand Down
Loading

0 comments on commit 784f757

Please sign in to comment.