From a82829f37e1ed298b6c2b6d2859d4bea65fe3dcc Mon Sep 17 00:00:00 2001 From: Sebastian Falbesoner Date: Wed, 12 Mar 2025 20:30:25 +0100 Subject: [PATCH] test: simplify (w)txid checks by avoiding .calc_sha256 calls Calls on the tx.calc_sha256 method can be confusing, as they return the result (either txid or wtxid, depending on the with_witness boolean parameter) as integer rather than as actual (w)txid. Use .rehash() and .getwtxid() instead to improve readability and in some cases avoid a conversion from string-txid to an integer. --- test/functional/feature_segwit.py | 6 +++--- test/functional/p2p_compactblocks.py | 11 ++++------- test/functional/p2p_segwit.py | 5 ++--- 3 files changed, 9 insertions(+), 13 deletions(-) diff --git a/test/functional/feature_segwit.py b/test/functional/feature_segwit.py index f98f326e8f..257bc44d4c 100755 --- a/test/functional/feature_segwit.py +++ b/test/functional/feature_segwit.py @@ -267,7 +267,7 @@ class SegWitTest(BitcoinTestFramework): tx1 = tx_from_hex(tx1_hex) # Check that wtxid is properly reported in mempool entry (txid1) - assert_equal(int(self.nodes[0].getmempoolentry(txid1)["wtxid"], 16), tx1.calc_sha256(True)) + assert_equal(self.nodes[0].getmempoolentry(txid1)["wtxid"], tx1.getwtxid()) # Check that weight and vsize are properly reported in mempool entry (txid1) assert_equal(self.nodes[0].getmempoolentry(txid1)["vsize"], tx1.get_vsize()) @@ -283,7 +283,7 @@ class SegWitTest(BitcoinTestFramework): assert not tx.wit.is_null() # Check that wtxid is properly reported in mempool entry (txid2) - assert_equal(int(self.nodes[0].getmempoolentry(txid2)["wtxid"], 16), tx.calc_sha256(True)) + assert_equal(self.nodes[0].getmempoolentry(txid2)["wtxid"], tx.getwtxid()) # Check that weight and vsize are properly reported in mempool entry (txid2) assert_equal(self.nodes[0].getmempoolentry(txid2)["vsize"], tx.get_vsize()) @@ -306,7 +306,7 @@ class SegWitTest(BitcoinTestFramework): assert txid3 in template_txids # Check that wtxid is properly reported in mempool entry (txid3) - assert_equal(int(self.nodes[0].getmempoolentry(txid3)["wtxid"], 16), tx.calc_sha256(True)) + assert_equal(self.nodes[0].getmempoolentry(txid3)["wtxid"], tx.getwtxid()) # Check that weight and vsize are properly reported in mempool entry (txid3) assert_equal(self.nodes[0].getmempoolentry(txid3)["vsize"], tx.get_vsize()) diff --git a/test/functional/p2p_compactblocks.py b/test/functional/p2p_compactblocks.py index ca36b2fbc0..e6f6288111 100755 --- a/test/functional/p2p_compactblocks.py +++ b/test/functional/p2p_compactblocks.py @@ -347,13 +347,11 @@ class CompactBlocksTest(BitcoinTestFramework): # Check that all prefilled_txn entries match what's in the block. for entry in header_and_shortids.prefilled_txn: - entry.tx.calc_sha256() # This checks the non-witness parts of the tx agree - assert_equal(entry.tx.sha256, block.vtx[entry.index].sha256) + assert_equal(entry.tx.rehash(), block.vtx[entry.index].rehash()) # And this checks the witness - wtxid = entry.tx.calc_sha256(True) - assert_equal(wtxid, block.vtx[entry.index].calc_sha256(True)) + assert_equal(entry.tx.getwtxid(), block.vtx[entry.index].getwtxid()) # Check that the cmpctblock message announced all the transactions. assert_equal(len(header_and_shortids.prefilled_txn) + len(header_and_shortids.shortids), len(block.vtx)) @@ -590,10 +588,9 @@ class CompactBlocksTest(BitcoinTestFramework): all_indices = msg.block_txn_request.to_absolute() for index in all_indices: tx = test_node.last_message["blocktxn"].block_transactions.transactions.pop(0) - tx.calc_sha256() - assert_equal(tx.sha256, block.vtx[index].sha256) + assert_equal(tx.rehash(), block.vtx[index].rehash()) # Check that the witness matches - assert_equal(tx.calc_sha256(True), block.vtx[index].calc_sha256(True)) + assert_equal(tx.getwtxid(), block.vtx[index].getwtxid()) test_node.last_message.pop("blocktxn", None) current_height -= 1 diff --git a/test/functional/p2p_segwit.py b/test/functional/p2p_segwit.py index 9caf5a19aa..7df59fe0d5 100755 --- a/test/functional/p2p_segwit.py +++ b/test/functional/p2p_segwit.py @@ -336,8 +336,7 @@ class SegWitTest(BitcoinTestFramework): # Verify the hash with witness differs from the txid # (otherwise our testing framework must be broken!) - tx.rehash() - assert tx.sha256 != tx.calc_sha256(with_witness=True) + assert tx.rehash() != tx.getwtxid() # Construct a block that includes the transaction. block = self.build_next_block() @@ -1293,7 +1292,7 @@ class SegWitTest(BitcoinTestFramework): # Test that getrawtransaction returns correct witness information # hash, size, vsize raw_tx = self.nodes[0].getrawtransaction(tx3.hash, 1) - assert_equal(int(raw_tx["hash"], 16), tx3.calc_sha256(True)) + assert_equal(raw_tx["hash"], tx3.getwtxid()) assert_equal(raw_tx["size"], len(tx3.serialize_with_witness())) vsize = tx3.get_vsize() assert_equal(raw_tx["vsize"], vsize)