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.
This commit is contained in:
Sebastian Falbesoner 2025-06-12 13:04:39 +02:00
parent 0716382c20
commit 8b09cc350a
16 changed files with 0 additions and 32 deletions

View File

@ -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):

View File

@ -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):

View File

@ -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")

View File

@ -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):

View File

@ -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)

View File

@ -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)

View File

@ -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")

View File

@ -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,

View File

@ -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):

View File

@ -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

View File

@ -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()

View File

@ -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):

View File

@ -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

View File

@ -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))

View File

@ -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):

View File

@ -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).