test: avoid unneeded block header hash -> integer conversions

This commit is contained in:
Sebastian Falbesoner
2025-06-15 01:23:09 +02:00
parent 2118301d77
commit 5fa34951ea
6 changed files with 23 additions and 23 deletions

View File

@@ -196,7 +196,7 @@ class BIP65Test(BitcoinTestFramework):
self.test_cltv_info(is_active=True) # Not active as of current tip, but next block must obey rules
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.hash_int)
assert_equal(self.nodes[0].getbestblockhash(), block.hash_hex)
if __name__ == '__main__':

View File

@@ -146,7 +146,7 @@ class BIP66Test(BitcoinTestFramework):
self.test_dersig_info(is_active=True) # Not active as of current tip, but next block must obey rules
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.hash_int)
assert_equal(self.nodes[0].getbestblockhash(), block.hash_hex)
if __name__ == '__main__':

View File

@@ -158,7 +158,7 @@ class CompactBlocksTest(BitcoinTestFramework):
def make_utxos(self):
block = self.build_block_on_tip(self.nodes[0])
self.segwit_node.send_and_ping(msg_no_witness_block(block))
assert int(self.nodes[0].getbestblockhash(), 16) == block.hash_int
assert_equal(self.nodes[0].getbestblockhash(), block.hash_hex)
self.generate(self.wallet, COINBASE_MATURITY)
total_value = block.vtx[0].vout[0].nValue
@@ -173,7 +173,7 @@ class CompactBlocksTest(BitcoinTestFramework):
block2.hashMerkleRoot = block2.calc_merkle_root()
block2.solve()
self.segwit_node.send_and_ping(msg_no_witness_block(block2))
assert_equal(int(self.nodes[0].getbestblockhash(), 16), block2.hash_int)
assert_equal(self.nodes[0].getbestblockhash(), block2.hash_hex)
self.utxos.extend([[tx.txid_int, i, out_value] for i in range(10)])
@@ -406,7 +406,7 @@ class CompactBlocksTest(BitcoinTestFramework):
msg.block_transactions.blockhash = block.hash_int
msg.block_transactions.transactions = [block.vtx[0]]
test_node.send_and_ping(msg)
assert_equal(int(node.getbestblockhash(), 16), block.hash_int)
assert_equal(node.getbestblockhash(), block.hash_hex)
# Create a chain of transactions from given utxo, and add to a new block.
def build_block_with_transactions(self, node, utxo, num_transactions):
@@ -555,7 +555,7 @@ class CompactBlocksTest(BitcoinTestFramework):
# Deliver the block
test_node.send_and_ping(msg_block(block))
assert_equal(int(node.getbestblockhash(), 16), block.hash_int)
assert_equal(node.getbestblockhash(), block.hash_hex)
def test_getblocktxn_handler(self, test_node):
node = self.nodes[0]
@@ -595,7 +595,7 @@ class CompactBlocksTest(BitcoinTestFramework):
test_node.last_message.pop("blocktxn", None)
test_node.send_and_ping(msg)
with p2p_lock:
assert_equal(test_node.last_message["block"].block.hash_int, int(block_hash, 16))
assert_equal(test_node.last_message["block"].block.hash_hex, block_hash)
assert "blocktxn" not in test_node.last_message
# Request with out-of-bounds tx index results in disconnect
@@ -651,7 +651,7 @@ class CompactBlocksTest(BitcoinTestFramework):
test_node.send_without_ping(msg_getdata([CInv(MSG_CMPCT_BLOCK, int(new_blocks[0], 16))]))
test_node.wait_until(lambda: "block" in test_node.last_message, timeout=30)
with p2p_lock:
assert_equal(test_node.last_message["block"].block.hash_int, int(new_blocks[0], 16))
assert_equal(test_node.last_message["block"].block.hash_hex, new_blocks[0])
# Generate an old compactblock, and verify that it's not accepted.
cur_height = node.getblockcount()
@@ -724,7 +724,7 @@ class CompactBlocksTest(BitcoinTestFramework):
test_node.send_and_ping(msg)
# Check that the tip didn't advance
assert_not_equal(int(node.getbestblockhash(), 16), block.hash_int)
assert_not_equal(node.getbestblockhash(), block.hash_hex)
test_node.sync_with_ping()
# Helper for enabling cb announcements
@@ -761,7 +761,7 @@ class CompactBlocksTest(BitcoinTestFramework):
assert tx.txid_hex in mempool
delivery_peer.send_and_ping(msg_cmpctblock(cmpct_block.to_p2p()))
assert_equal(int(node.getbestblockhash(), 16), block.hash_int)
assert_equal(node.getbestblockhash(), block.hash_hex)
self.utxos.append([block.vtx[-1].txid_int, 0, block.vtx[-1].vout[0].nValue])
@@ -777,13 +777,13 @@ class CompactBlocksTest(BitcoinTestFramework):
cmpct_block.use_witness = True
delivery_peer.send_and_ping(msg_cmpctblock(cmpct_block.to_p2p()))
assert_not_equal(int(node.getbestblockhash(), 16), block.hash_int)
assert_not_equal(node.getbestblockhash(), block.hash_hex)
msg = msg_no_witness_blocktxn()
msg.block_transactions.blockhash = block.hash_int
msg.block_transactions.transactions = block.vtx[1:]
stalling_peer.send_and_ping(msg)
assert_equal(int(node.getbestblockhash(), 16), block.hash_int)
assert_equal(node.getbestblockhash(), block.hash_hex)
def test_highbandwidth_mode_states_via_getpeerinfo(self):
# create new p2p connection for a fresh state w/o any prior sendcmpct messages sent
@@ -839,7 +839,7 @@ class CompactBlocksTest(BitcoinTestFramework):
msg.block_transactions.blockhash = block.hash_int
msg.block_transactions.transactions = block.vtx[1:]
peer.send_and_ping(msg)
assert_equal(int(node.getbestblockhash(), 16), block.hash_int)
assert_equal(node.getbestblockhash(), block.hash_hex)
peer.clear_getblocktxn()
# Test the simple parallel download case...
@@ -854,26 +854,26 @@ class CompactBlocksTest(BitcoinTestFramework):
with p2p_lock:
# The second peer to announce should still get a getblocktxn
assert "getblocktxn" in delivery_peer.last_message
assert_not_equal(int(node.getbestblockhash(), 16), block.hash_int)
assert_not_equal(node.getbestblockhash(), block.hash_hex)
inbound_peer.send_and_ping(msg_cmpctblock(cmpct_block.to_p2p()))
with p2p_lock:
# The third inbound peer to announce should *not* get a getblocktxn
assert "getblocktxn" not in inbound_peer.last_message
assert_not_equal(int(node.getbestblockhash(), 16), block.hash_int)
assert_not_equal(node.getbestblockhash(), block.hash_hex)
outbound_peer.send_and_ping(msg_cmpctblock(cmpct_block.to_p2p()))
with p2p_lock:
# The third peer to announce should get a getblocktxn if outbound
assert "getblocktxn" in outbound_peer.last_message
assert_not_equal(int(node.getbestblockhash(), 16), block.hash_int)
assert_not_equal(node.getbestblockhash(), block.hash_hex)
# Second peer completes the compact block first
msg = msg_blocktxn()
msg.block_transactions.blockhash = block.hash_int
msg.block_transactions.transactions = block.vtx[1:]
delivery_peer.send_and_ping(msg)
assert_equal(int(node.getbestblockhash(), 16), block.hash_int)
assert_equal(node.getbestblockhash(), block.hash_hex)
# Nothing bad should happen if we get a late fill from the first peer...
stalling_peer.send_and_ping(msg)

View File

@@ -72,14 +72,14 @@ class P2PCompactBlocksBlocksOnly(BitcoinTestFramework):
# A -blocksonly node should not request BIP152 high bandwidth mode upon
# receiving a new valid block at the tip.
p2p_conn_blocksonly.send_and_ping(msg_block(block0))
assert_equal(int(self.nodes[0].getbestblockhash(), 16), block0.hash_int)
assert_equal(self.nodes[0].getbestblockhash(), block0.hash_hex)
assert_equal(p2p_conn_blocksonly.message_count['sendcmpct'], 1)
assert_equal(p2p_conn_blocksonly.last_message['sendcmpct'].announce, False)
# A normal node participating in transaction relay should request BIP152
# high bandwidth mode upon receiving a new valid block at the tip.
p2p_conn_high_bw.send_and_ping(msg_block(block0))
assert_equal(int(self.nodes[1].getbestblockhash(), 16), block0.hash_int)
assert_equal(self.nodes[1].getbestblockhash(), block0.hash_hex)
p2p_conn_high_bw.wait_until(lambda: p2p_conn_high_bw.message_count['sendcmpct'] == 2)
assert_equal(p2p_conn_high_bw.last_message['sendcmpct'].announce, True)

View File

@@ -115,7 +115,7 @@ class InvalidBlockRequestTest(BitcoinTestFramework):
# Update tip info
height += 1
block_time += 1
tip = int(block2_orig.hash_hex, 16)
tip = block2_orig.hash_int
# Complete testing of CVE-2018-17144, by checking for the inflation bug.
# Create a block that spends the output of a tx in a previous block.

View File

@@ -247,12 +247,12 @@ class SendHeadersTest(BitcoinTestFramework):
block.solve()
test_node.send_header_for_blocks([block])
test_node.clear_block_announcements()
test_node.send_get_headers(locator=[], hashstop=int(block.hash_hex, 16))
test_node.send_get_headers(locator=[], hashstop=block.hash_int)
test_node.sync_with_ping()
assert_equal(test_node.block_announced, False)
inv_node.clear_block_announcements()
test_node.send_without_ping(msg_block(block))
inv_node.check_last_inv_announcement(inv=[int(block.hash_hex, 16)])
inv_node.check_last_inv_announcement(inv=[block.hash_int])
def test_nonnull_locators(self, test_node, inv_node):
tip = int(self.nodes[0].getbestblockhash(), 16)
@@ -538,7 +538,7 @@ class SendHeadersTest(BitcoinTestFramework):
test_node.wait_for_getdata([x.hash_int for x in blocks])
[test_node.send_without_ping(msg_block(x)) for x in blocks]
test_node.sync_with_ping()
assert_equal(int(self.nodes[0].getbestblockhash(), 16), blocks[1].hash_int)
assert_equal(self.nodes[0].getbestblockhash(), blocks[1].hash_hex)
expected_hash = blocks[1].hash_int
blocks = []