nanonit: Realized self wasn't used and this together with assert_highbandwidth_states() could be hoisted to the outer scope to avoid some tokens.
<details><summary>Diff</summary>
diff --git a/test/functional/p2p_compactblocks.py b/test/functional/p2p_compactblocks.py
index 73c5066448..dad317e864 100755
--- a/test/functional/p2p_compactblocks.py
+++ b/test/functional/p2p_compactblocks.py
@@ -142,6 +142,27 @@ class TestP2PConn(P2PInterface):
self.send_without_ping(message)
self.wait_for_disconnect(timeout=timeout)
+
+def getblocktxn_expected(peer, blockhash, indices=None):
+ with p2p_lock:
+ assert "getblocktxn" in peer.last_message
+ gbt = peer.last_message["getblocktxn"].block_txn_request
+
+ assert_equal(gbt.blockhash, blockhash)
+ if indices is not None:
+ assert_equal(gbt.to_absolute(), indices)
+ if isinstance(peer, TestNode):
+ assert_not_equal(peer.getbestblockhash(), blockhash)
+
+
+# assert the RPC getpeerinfo boolean fields `bip152_hb_{to, from}`
+# match the given parameters for the peer at idx of a given node
+def assert_highbandwidth_states(node, idx, hb_to, hb_from):
+ peerinfo = node.getpeerinfo()[idx]
+ assert_equal(peerinfo['bip152_hb_to'], hb_to)
+ assert_equal(peerinfo['bip152_hb_from'], hb_from)
+
+
class CompactBlocksTest(BitcoinTestFramework):
def set_test_params(self):
self.setup_clean_chain = True
@@ -151,17 +172,6 @@ class CompactBlocksTest(BitcoinTestFramework):
]]
self.utxos = []
- def getblocktxn_expected(self, peer, blockhash, indices=None):
- with p2p_lock:
- assert "getblocktxn" in peer.last_message
- gbt = peer.last_message["getblocktxn"].block_txn_request
-
- assert_equal(gbt.blockhash, blockhash)
- if indices is not None:
- assert_equal(gbt.to_absolute(), indices)
- if isinstance(peer, TestNode):
- assert_not_equal(peer.getbestblockhash(), blockhash)
-
def build_block_on_tip(self, node):
block = create_block(tmpl=node.getblocktemplate(NORMAL_GBT_REQUEST_PARAMS))
block.solve()
@@ -203,7 +213,7 @@ class CompactBlocksTest(BitcoinTestFramework):
peer.clear_getblocktxn()
peer.send_and_ping(msg)
- self.getblocktxn_expected(peer, block.hash_int)
+ getblocktxn_expected(peer, block.hash_int)
return block, cmpct_block
# Test "sendcmpct" (between peers preferring the same version):
@@ -298,12 +308,12 @@ class CompactBlocksTest(BitcoinTestFramework):
hb_peer = self.nodes[0].add_p2p_connection(TestP2PConn())
self.request_cb_announcements(hb_peer)
self.make_peer_hb_to_candidate(self.nodes[0], hb_peer)
- self.assert_highbandwidth_states(self.nodes[0], idx=-1, hb_to=True, hb_from=True)
+ assert_highbandwidth_states(self.nodes[0], idx=-1, hb_to=True, hb_from=True)
# Make a low-bandwidth peer
lb_peer = self.nodes[0].add_p2p_connection(TestP2PConn())
self.request_cb_announcements(lb_peer)
- self.assert_highbandwidth_states(self.nodes[0], idx=-1, hb_to=False, hb_from=True)
+ assert_highbandwidth_states(self.nodes[0], idx=-1, hb_to=False, hb_from=True)
# Construct an invalid cmpctblock message
self.generate(self.nodes[0], COINBASE_MATURITY + 1)
@@ -446,7 +456,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 that requests the coinbase.
- self.getblocktxn_expected(test_node, block.hash_int, indices=[0])
+ getblocktxn_expected(test_node, block.hash_int, indices=[0])
# Send the coinbase, and verify that the tip advances.
msg = msg_blocktxn()
@@ -480,7 +490,7 @@ class CompactBlocksTest(BitcoinTestFramework):
msg = msg_cmpctblock(compact_block.to_p2p())
peer.clear_getblocktxn()
peer.send_and_ping(msg)
- self.getblocktxn_expected(peer, compact_block.header.hash_int, expected_result)
+ getblocktxn_expected(peer, compact_block.header.hash_int, expected_result)
def test_tip_after_message(node, peer, msg, tip):
peer.send_and_ping(msg)
@@ -573,7 +583,7 @@ class CompactBlocksTest(BitcoinTestFramework):
test_node.clear_getblocktxn()
test_node.send_and_ping(msg_cmpctblock(comp_block.to_p2p()))
expected_indices = [6, 7, 8, 9, 10]
- self.getblocktxn_expected(test_node, block.hash_int, expected_indices)
+ getblocktxn_expected(test_node, block.hash_int, expected_indices)
# Now give an incorrect response.
# Note that it's possible for bitcoind to be smart enough to know we're
@@ -614,7 +624,7 @@ class CompactBlocksTest(BitcoinTestFramework):
comp_block.initialize_from_block(block, prefill_list=[0], use_witness=True)
test_node.clear_getblocktxn()
test_node.send_and_ping(msg_cmpctblock(comp_block.to_p2p()))
- self.getblocktxn_expected(test_node, block.hash_int, indices=[1,2])
+ getblocktxn_expected(test_node, block.hash_int, indices=[1,2])
# Send a blocktxn that does not succeed in reconstruction, triggering
# getdata fallback.
@@ -885,14 +895,6 @@ class CompactBlocksTest(BitcoinTestFramework):
stalling_peer.send_and_ping(msg)
assert_equal(node.getbestblockhash(), block.hash_hex)
- # assert the RPC getpeerinfo boolean fields `bip152_hb_{to, from}`
- # match the given parameters for the peer at idx of a given node
- [@staticmethod](/bitcoin-bitcoin/contributor/staticmethod/)
- def assert_highbandwidth_states(node, idx, hb_to, hb_from):
- peerinfo = node.getpeerinfo()[idx]
- assert_equal(peerinfo['bip152_hb_to'], hb_to)
- assert_equal(peerinfo['bip152_hb_from'], hb_from)
-
def test_highbandwidth_mode_states_via_getpeerinfo(self):
# create new p2p connection for a fresh state w/o any prior sendcmpct messages sent
hb_test_node = self.nodes[0].add_p2p_connection(TestP2PConn())
@@ -901,21 +903,21 @@ class CompactBlocksTest(BitcoinTestFramework):
hb_test_node_idx = -1
# initially, neither node has selected the other peer as high-bandwidth yet
- self.assert_highbandwidth_states(self.nodes[0], hb_test_node_idx, hb_to=False, hb_from=False)
+ assert_highbandwidth_states(self.nodes[0], hb_test_node_idx, hb_to=False, hb_from=False)
# peer requests high-bandwidth mode by sending sendcmpct(1)
hb_test_node.send_and_ping(msg_sendcmpct(announce=True, version=2))
- self.assert_highbandwidth_states(self.nodes[0], hb_test_node_idx, hb_to=False, hb_from=True)
+ assert_highbandwidth_states(self.nodes[0], hb_test_node_idx, hb_to=False, hb_from=True)
# peer generates a block and sends it to node, which should
# select the peer as high-bandwidth (up to 3 peers according to BIP 152)
block = self.build_block_on_tip(self.nodes[0])
hb_test_node.send_and_ping(msg_block(block))
- self.assert_highbandwidth_states(self.nodes[0], hb_test_node_idx, hb_to=True, hb_from=True)
+ assert_highbandwidth_states(self.nodes[0], hb_test_node_idx, hb_to=True, hb_from=True)
# peer requests low-bandwidth mode by sending sendcmpct(0)
hb_test_node.send_and_ping(msg_sendcmpct(announce=False, version=2))
- self.assert_highbandwidth_states(self.nodes[0], hb_test_node_idx, hb_to=True, hb_from=False)
+ assert_highbandwidth_states(self.nodes[0], hb_test_node_idx, hb_to=True, hb_from=False)
def test_compactblock_reconstruction_parallel_reconstruction(self, stalling_peer, delivery_peer, inbound_peer, outbound_peer):
""" All p2p connections are inbound except outbound_peer. We test that ultimate parallel slot
@@ -947,7 +949,7 @@ class CompactBlocksTest(BitcoinTestFramework):
delivery_peer.send_and_ping(msg_cmpctblock(cmpct_block.to_p2p()))
# The second peer to announce should still get a getblocktxn
- self.getblocktxn_expected(delivery_peer, block.hash_int)
+ getblocktxn_expected(delivery_peer, block.hash_int)
inbound_peer.send_and_ping(msg_cmpctblock(cmpct_block.to_p2p()))
with p2p_lock:
@@ -957,7 +959,7 @@ class CompactBlocksTest(BitcoinTestFramework):
outbound_peer.send_and_ping(msg_cmpctblock(cmpct_block.to_p2p()))
# The third peer to announce should get a getblocktxn if outbound
- self.getblocktxn_expected(outbound_peer, block.hash_int)
+ getblocktxn_expected(outbound_peer, block.hash_int)
# Second peer completes the compact block first
msg = msg_blocktxn()
@@ -998,7 +1000,7 @@ class CompactBlocksTest(BitcoinTestFramework):
# create new p2p connection for a fresh state w/o any prior sendcmpct messages sent
unsolicited_peer = self.nodes[0].add_p2p_connection(TestP2PConn())
- self.assert_highbandwidth_states(node, idx=-1, hb_to=False, hb_from=False)
+ assert_highbandwidth_states(node, idx=-1, hb_to=False, hb_from=False)
self.log.info("Test that a node ignores unsolicited CMPCTBLOCK messages from peers that have not sent SENDCMPCT.")
assert ignores_compact_block(unsolicited_peer, solicited=False)
@@ -1009,15 +1011,15 @@ class CompactBlocksTest(BitcoinTestFramework):
# Unsolicited peer announces CMPCTBLOCK support with SENDCMPCT message,
# but still non-HB.
unsolicited_peer.send_and_ping(msg_sendcmpct())
- self.assert_highbandwidth_states(node, idx=-1, hb_to=False, hb_from=False)
+ assert_highbandwidth_states(node, idx=-1, hb_to=False, hb_from=False)
self.log.info("Test that a node ignores unsolicited CMPCTBLOCK messages from non-HB peers.")
assert ignores_compact_block(unsolicited_peer, solicited=False)
- self.assert_highbandwidth_states(node, idx=-1, hb_to=False, hb_from=False)
+ assert_highbandwidth_states(node, idx=-1, hb_to=False, hb_from=False)
self.log.info("Test that a node does not ignore solicited CMPCTBLOCK messages from non-HB peers.")
assert not ignores_compact_block(unsolicited_peer, solicited=True)
- self.assert_highbandwidth_states(node, idx=-1, hb_to=False, hb_from=False)
+ assert_highbandwidth_states(node, idx=-1, hb_to=False, hb_from=False)
# The node will ask for transactions from an unsolicited compact block
# it receives from a high bandwidth peer, we need to use one set up earlier,
@@ -1025,7 +1027,7 @@ class CompactBlocksTest(BitcoinTestFramework):
self.log.info("Test that a node does not ignore unsolicited CMPCTBLOCK messages from HB peers.")
hb_peer_idx = -2
- self.assert_highbandwidth_states(node, idx=hb_peer_idx, hb_to=True, hb_from=False)
+ assert_highbandwidth_states(node, idx=hb_peer_idx, hb_to=True, hb_from=False)
hb_peer = self.nodes[0].p2ps[hb_peer_idx]
assert not ignores_compact_block(hb_peer, solicited=False)
</details>