From 8b09cc350afac05d22c7c87433d376654f3ce403 Mon Sep 17 00:00:00 2001 From: Sebastian Falbesoner Date: Thu, 12 Jun 2025 13:04:39 +0200 Subject: [PATCH] test: remove bare CBlockHeader `.rehash()`/`.calc_sha256()` calls Since the previous commit, CBlockHeader/CBlock object calls to the methods `.rehash()` and `.calc_sha256()` are effectively no-ops if the returned value is not used, so we can just remove them. --- contrib/signet/miner | 1 - test/functional/example_test.py | 1 - test/functional/feature_block.py | 1 - test/functional/feature_maxuploadtarget.py | 1 - test/functional/feature_taproot.py | 1 - test/functional/mining_mainnet.py | 1 - test/functional/mining_template_verification.py | 1 - test/functional/p2p_compactblocks.py | 7 ------- test/functional/p2p_compactblocks_blocksonly.py | 1 - test/functional/p2p_getdata.py | 1 - test/functional/p2p_invalid_messages.py | 3 --- test/functional/p2p_segwit.py | 1 - test/functional/p2p_sendheaders.py | 1 - test/functional/rpc_blockchain.py | 1 - test/functional/test_framework/blocktools.py | 2 -- test/functional/test_framework/messages.py | 8 -------- 16 files changed, 32 deletions(-) diff --git a/contrib/signet/miner b/contrib/signet/miner index c6ce7f92730..4a5058afb29 100755 --- a/contrib/signet/miner +++ b/contrib/signet/miner @@ -95,7 +95,6 @@ def finish_block(block, signet_solution, grind_cmd): newheadhex = subprocess.run(cmd, stdout=subprocess.PIPE, input=b"", check=True).stdout.strip() newhead = from_hex(CBlockHeader(), newheadhex.decode('utf8')) block.nNonce = newhead.nNonce - block.rehash() return block def new_block(tmpl, reward_spk, *, blocktime=None, poolid=None): diff --git a/test/functional/example_test.py b/test/functional/example_test.py index a6eca81a58c..8e88fe00018 100755 --- a/test/functional/example_test.py +++ b/test/functional/example_test.py @@ -57,7 +57,6 @@ class BaseNode(P2PInterface): """Override the standard on_block callback Store the hash of a received block in the dictionary.""" - message.block.calc_sha256() self.block_receive_map[message.block.sha256] += 1 def on_inv(self, message): diff --git a/test/functional/feature_block.py b/test/functional/feature_block.py index 34a69d4fe8e..db4e53e4ee1 100755 --- a/test/functional/feature_block.py +++ b/test/functional/feature_block.py @@ -661,7 +661,6 @@ class FullBlockTest(BitcoinTestFramework): while b47.sha256 <= target: # Rehash nonces until an invalid too-high-hash block is found. b47.nNonce += 1 - b47.rehash() self.send_blocks([b47], False, force_send=True, reject_reason='high-hash', reconnect=True) self.log.info("Reject a block with a timestamp >2 hours in the future") diff --git a/test/functional/feature_maxuploadtarget.py b/test/functional/feature_maxuploadtarget.py index 526f2ffa904..7d408f7fc1f 100755 --- a/test/functional/feature_maxuploadtarget.py +++ b/test/functional/feature_maxuploadtarget.py @@ -41,7 +41,6 @@ class TestP2PConn(P2PInterface): pass def on_block(self, message): - message.block.calc_sha256() self.block_receive_map[message.block.sha256] += 1 class MaxUploadTest(BitcoinTestFramework): diff --git a/test/functional/feature_taproot.py b/test/functional/feature_taproot.py index f367649aaaa..8df423422d5 100755 --- a/test/functional/feature_taproot.py +++ b/test/functional/feature_taproot.py @@ -1574,7 +1574,6 @@ class TaprootTest(BitcoinTestFramework): assert coinbase.txid_hex == "f60c73405d499a956d3162e3483c395526ef78286458a4cb17b125aa92e49b20" # Mine it block = create_block(hashprev=int(self.nodes[0].getbestblockhash(), 16), coinbase=coinbase) - block.rehash() block.solve() self.nodes[0].submitblock(block.serialize().hex()) assert_equal(self.nodes[0].getblockcount(), 1) diff --git a/test/functional/mining_mainnet.py b/test/functional/mining_mainnet.py index 25632fbc8e7..e9aaa36da35 100755 --- a/test/functional/mining_mainnet.py +++ b/test/functional/mining_mainnet.py @@ -66,7 +66,6 @@ class MiningMainnetTest(BitcoinTestFramework): block.vtx[0].nLockTime = 0 block.vtx[0].vin[0].nSequence = SEQUENCE_FINAL block.hashMerkleRoot = block.calc_merkle_root() - block.rehash() block_hex = block.serialize(with_witness=False).hex() self.log.debug(block_hex) assert_equal(node.submitblock(block_hex), None) diff --git a/test/functional/mining_template_verification.py b/test/functional/mining_template_verification.py index 7abbf0f1d28..526a9139b95 100755 --- a/test/functional/mining_template_verification.py +++ b/test/functional/mining_template_verification.py @@ -158,7 +158,6 @@ class MiningTemplateVerificationTest(BitcoinTestFramework): # Ensure that it doesn't meet the target by coincidence while block.sha256 <= target: block.nNonce += 1 - block.rehash() self.log.debug("Found a nonce") self.log.info("A block template doesn't need PoW") diff --git a/test/functional/p2p_compactblocks.py b/test/functional/p2p_compactblocks.py index 7b816fe2720..8f5c5ad8bbe 100755 --- a/test/functional/p2p_compactblocks.py +++ b/test/functional/p2p_compactblocks.py @@ -80,13 +80,11 @@ class TestP2PConn(P2PInterface): def on_cmpctblock(self, message): self.block_announced = True - self.last_message["cmpctblock"].header_and_shortids.header.calc_sha256() self.announced_blockhashes.add(self.last_message["cmpctblock"].header_and_shortids.header.sha256) def on_headers(self, message): self.block_announced = True for x in self.last_message["headers"].headers: - x.calc_sha256() self.announced_blockhashes.add(x.sha256) def on_inv(self, message): @@ -308,7 +306,6 @@ class CompactBlocksTest(BitcoinTestFramework): # Store the raw block in our internal format. block = from_hex(CBlock(), node.getblock("%064x" % block_hash, False)) - block.rehash() # Wait until the block was announced (via compact blocks) test_node.wait_until(lambda: "cmpctblock" in test_node.last_message, timeout=30) @@ -336,7 +333,6 @@ class CompactBlocksTest(BitcoinTestFramework): def check_compactblock_construction_from_block(self, header_and_shortids, block_hash, block): # Check that we got the right block! - header_and_shortids.header.calc_sha256() assert_equal(header_and_shortids.header.sha256, block_hash) # Make sure the prefilled_txn appears to have included the coinbase @@ -599,7 +595,6 @@ class CompactBlocksTest(BitcoinTestFramework): test_node.last_message.pop("blocktxn", None) test_node.send_and_ping(msg) 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 @@ -656,7 +651,6 @@ 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: - test_node.last_message["block"].block.calc_sha256() assert_equal(test_node.last_message["block"].block.sha256, int(new_blocks[0], 16)) # Generate an old compactblock, and verify that it's not accepted. @@ -705,7 +699,6 @@ class CompactBlocksTest(BitcoinTestFramework): l.wait_until(lambda: "cmpctblock" in l.last_message, timeout=30) 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) # Test that we don't get disconnected if we relay a compact block with valid header, diff --git a/test/functional/p2p_compactblocks_blocksonly.py b/test/functional/p2p_compactblocks_blocksonly.py index 954175b772c..c4c19e19278 100755 --- a/test/functional/p2p_compactblocks_blocksonly.py +++ b/test/functional/p2p_compactblocks_blocksonly.py @@ -36,7 +36,6 @@ class P2PCompactBlocksBlocksOnly(BitcoinTestFramework): blockhash = self.generate(self.nodes[2], 1, sync_fun=self.no_op)[0] block_hex = self.nodes[2].getblock(blockhash=blockhash, verbosity=0) block = from_hex(CBlock(), block_hex) - block.rehash() return block def run_test(self): diff --git a/test/functional/p2p_getdata.py b/test/functional/p2p_getdata.py index 6153d08d9b4..8d3c5bf4a0b 100755 --- a/test/functional/p2p_getdata.py +++ b/test/functional/p2p_getdata.py @@ -19,7 +19,6 @@ class P2PStoreBlock(P2PInterface): self.blocks = defaultdict(int) def on_block(self, message): - message.block.calc_sha256() self.blocks[message.block.sha256] += 1 diff --git a/test/functional/p2p_invalid_messages.py b/test/functional/p2p_invalid_messages.py index c8228df8026..1685eebd4a4 100755 --- a/test/functional/p2p_invalid_messages.py +++ b/test/functional/p2p_invalid_messages.py @@ -288,10 +288,8 @@ class InvalidMessagesTest(BitcoinTestFramework): blockheader.hashPrevBlock = int(blockheader_tip_hash, 16) blockheader.nTime = int(time.time()) blockheader.nBits = blockheader_tip.nBits - blockheader.rehash() while not blockheader.hash.startswith('0'): blockheader.nNonce += 1 - blockheader.rehash() peer = self.nodes[0].add_p2p_connection(P2PInterface()) peer.send_and_ping(msg_headers([blockheader])) assert_equal(self.nodes[0].getblockchaininfo()['headers'], 1) @@ -302,7 +300,6 @@ class InvalidMessagesTest(BitcoinTestFramework): # invalidate PoW while not blockheader.hash.startswith('f'): blockheader.nNonce += 1 - blockheader.rehash() with self.nodes[0].assert_debug_log(['Misbehaving', 'header with invalid proof of work']): peer.send_without_ping(msg_headers([blockheader])) peer.wait_for_disconnect() diff --git a/test/functional/p2p_segwit.py b/test/functional/p2p_segwit.py index d2790f03828..fd296c2c86a 100755 --- a/test/functional/p2p_segwit.py +++ b/test/functional/p2p_segwit.py @@ -230,7 +230,6 @@ class SegWitTest(BitcoinTestFramework): height = self.nodes[0].getblockcount() + 1 block_time = self.nodes[0].getblockheader(tip)["mediantime"] + 1 block = create_block(int(tip, 16), create_coinbase(height), block_time) - block.rehash() return block def update_witness_block_with_transactions(self, block, tx_list, nonce=0): diff --git a/test/functional/p2p_sendheaders.py b/test/functional/p2p_sendheaders.py index 7dc91305f3b..aaf2e953f6a 100755 --- a/test/functional/p2p_sendheaders.py +++ b/test/functional/p2p_sendheaders.py @@ -150,7 +150,6 @@ class BaseNode(P2PInterface): if len(message.headers): self.block_announced = True for x in message.headers: - x.calc_sha256() # append because headers may be announced over multiple messages. self.recent_headers_announced.append(x.sha256) self.last_blockhash_announced = message.headers[-1].sha256 diff --git a/test/functional/rpc_blockchain.py b/test/functional/rpc_blockchain.py index 5f6edfd94b1..936126b8c5b 100755 --- a/test/functional/rpc_blockchain.py +++ b/test/functional/rpc_blockchain.py @@ -480,7 +480,6 @@ class BlockchainTest(BitcoinTestFramework): assert_is_hex_string(header_hex) header = from_hex(CBlockHeader(), header_hex) - header.calc_sha256() assert_equal(header.hash, besthash) assert 'previousblockhash' not in node.getblockheader(node.getblockhash(0)) diff --git a/test/functional/test_framework/blocktools.py b/test/functional/test_framework/blocktools.py index 70bd5190c47..fa4182c6cf9 100644 --- a/test/functional/test_framework/blocktools.py +++ b/test/functional/test_framework/blocktools.py @@ -111,7 +111,6 @@ def create_block(hashprev=None, coinbase=None, ntime=None, *, version=None, tmpl tx = tx_from_hex(tx) block.vtx.append(tx) block.hashMerkleRoot = block.calc_merkle_root() - block.calc_sha256() return block def get_witness_script(witness_root, witness_nonce): @@ -135,7 +134,6 @@ def add_witness_commitment(block, nonce=0): # witness commitment is the last OP_RETURN output in coinbase block.vtx[0].vout.append(CTxOut(0, get_witness_script(witness_root, witness_nonce))) block.hashMerkleRoot = block.calc_merkle_root() - block.rehash() def script_BIP34_coinbase_height(height): diff --git a/test/functional/test_framework/messages.py b/test/functional/test_framework/messages.py index 876ba7126d1..94068924094 100755 --- a/test/functional/test_framework/messages.py +++ b/test/functional/test_framework/messages.py @@ -717,7 +717,6 @@ class CBlockHeader: self.nTime = header.nTime self.nBits = header.nBits self.nNonce = header.nNonce - self.calc_sha256() def set_null(self): self.nVersion = 4 @@ -758,10 +757,6 @@ class CBlockHeader: """Return block header hash as integer.""" return uint256_from_str(hash256(self._serialize_header())) - # TODO: get rid of this method, remove call-sites - def calc_sha256(self): - pass - # TODO: get rid of this method, replace call-sites by .sha256 access (if return value is used) def rehash(self): return self.sha256 @@ -823,7 +818,6 @@ class CBlock(CBlockHeader): return self.get_merkle_root(hashes) def is_valid(self): - self.calc_sha256() target = uint256_from_compact(self.nBits) if self.sha256 > target: return False @@ -835,11 +829,9 @@ class CBlock(CBlockHeader): return True def solve(self): - self.rehash() target = uint256_from_compact(self.nBits) while self.sha256 > target: self.nNonce += 1 - self.rehash() # Calculate the block weight using witness and non-witness # serialization size (does NOT use sigops).