refactor: Merge announce_cmpct_block() defs into one
What changed, and why it matters
This commit is a simple code cleanup in a test file. It merges three nearly identical helper functions into one shared method used by Bitcoin Core's automated compact block tests. There is no change to the actual Bitcoin network software or its security behavior.
No security action needed. This is a routine test-only refactor.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The commit refactors p2p_compactblocks.py, a functional test script. It replaces two local announce_cmpct_block() closures and one existing method-like usage with a single class method announce_cmpct_block(self, node, peer, txn_count=5). The logic is identical: pop a UTXO, build a block with transactions, construct a compact block message, send it to a test peer, assert a getblocktxn response, and return the block objects. No protocol, consensus, or production code is modified.
Changed components
test/functional/p2p_compactblocks.pyInspect captured patch +15 / −28
diff --git a/test/functional/p2p_compactblocks.py b/test/functional/p2p_compactblocks.py
index ebc83964..f83aa1bf 100755
--- a/test/functional/p2p_compactblocks.py
+++ b/test/functional/p2p_compactblocks.py
@@ -177,6 +177,17 @@ class CompactBlocksTest(BitcoinTestFramework):
assert_equal(self.nodes[0].getbestblockhash(), block2.hash_hex)
self.utxos.extend([[tx.txid_int, i, out_value] for i in range(10)])
+ def announce_cmpct_block(self, node, peer, txn_count=5):
+ utxo = self.utxos.pop(0)
+ block = self.build_block_with_transactions(node, utxo, txn_count)
+
+ cmpct_block = HeaderAndShortIDs()
+ cmpct_block.initialize_from_block(block)
+ msg = msg_cmpctblock(cmpct_block.to_p2p())
+ peer.send_and_ping(msg)
+ with p2p_lock:
+ assert "getblocktxn" in peer.last_message
+ return block, cmpct_block
# Test "sendcmpct" (between peers preferring the same version):
# - No compact block announcements unless sendcmpct is sent.
@@ -804,19 +815,7 @@ class CompactBlocksTest(BitcoinTestFramework):
node = self.nodes[0]
assert len(self.utxos)
- def announce_cmpct_block(node, peer):
- utxo = self.utxos.pop(0)
- block = self.build_block_with_transactions(node, utxo, 5)
-
- cmpct_block = HeaderAndShortIDs()
- cmpct_block.initialize_from_block(block)
- msg = msg_cmpctblock(cmpct_block.to_p2p())
- peer.send_and_ping(msg)
- with p2p_lock:
- assert "getblocktxn" in peer.last_message
- return block, cmpct_block
-
- block, cmpct_block = announce_cmpct_block(node, stalling_peer)
+ block, cmpct_block = self.announce_cmpct_block(node, stalling_peer)
for tx in block.vtx[1:]:
delivery_peer.send_without_ping(msg_tx(tx))
@@ -832,7 +831,7 @@ class CompactBlocksTest(BitcoinTestFramework):
# Now test that delivering an invalid compact block won't break relay
- block, cmpct_block = announce_cmpct_block(node, stalling_peer)
+ block, cmpct_block = self.announce_cmpct_block(node, stalling_peer)
for tx in block.vtx[1:]:
delivery_peer.send_without_ping(msg_tx(tx))
delivery_peer.sync_with_ping()
@@ -885,21 +884,9 @@ class CompactBlocksTest(BitcoinTestFramework):
node = self.nodes[0]
assert len(self.utxos)
- def announce_cmpct_block(node, peer, txn_count):
- utxo = self.utxos.pop(0)
- block = self.build_block_with_transactions(node, utxo, txn_count)
-
- cmpct_block = HeaderAndShortIDs()
- cmpct_block.initialize_from_block(block)
- msg = msg_cmpctblock(cmpct_block.to_p2p())
- peer.send_and_ping(msg)
- with p2p_lock:
- assert "getblocktxn" in peer.last_message
- return block, cmpct_block
-
for name, peer in [("delivery", delivery_peer), ("inbound", inbound_peer), ("outbound", outbound_peer)]:
self.log.info(f"Setting {name} as high bandwidth peer")
- block, cmpct_block = announce_cmpct_block(node, peer, 1)
+ block, cmpct_block = self.announce_cmpct_block(node, peer, 1)
msg = msg_blocktxn()
msg.block_transactions.blockhash = block.hash_int
msg.block_transactions.transactions = block.vtx[1:]
@@ -913,7 +900,7 @@ class CompactBlocksTest(BitcoinTestFramework):
# Remaining low-bandwidth peer is stalling_peer, who announces first
assert_equal([peer['bip152_hb_to'] for peer in node.getpeerinfo()], [False, True, True, True])
- block, cmpct_block = announce_cmpct_block(node, stalling_peer, num_missing)
+ block, cmpct_block = self.announce_cmpct_block(node, stalling_peer, num_missing)
delivery_peer.send_and_ping(msg_cmpctblock(cmpct_block.to_p2p()))
with p2p_lock:
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.