Skip to content

Commit fe2819d

Browse files
committed
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.
1 parent f27444f commit fe2819d

File tree

3 files changed

+9
-13
lines changed

3 files changed

+9
-13
lines changed

test/functional/feature_segwit.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ def run_test(self):
267267
tx1 = tx_from_hex(tx1_hex)
268268

269269
# Check that wtxid is properly reported in mempool entry (txid1)
270-
assert_equal(int(self.nodes[0].getmempoolentry(txid1)["wtxid"], 16), tx1.calc_sha256(True))
270+
assert_equal(self.nodes[0].getmempoolentry(txid1)["wtxid"], tx1.getwtxid())
271271

272272
# Check that weight and vsize are properly reported in mempool entry (txid1)
273273
assert_equal(self.nodes[0].getmempoolentry(txid1)["vsize"], tx1.get_vsize())
@@ -283,7 +283,7 @@ def run_test(self):
283283
assert not tx.wit.is_null()
284284

285285
# Check that wtxid is properly reported in mempool entry (txid2)
286-
assert_equal(int(self.nodes[0].getmempoolentry(txid2)["wtxid"], 16), tx.calc_sha256(True))
286+
assert_equal(self.nodes[0].getmempoolentry(txid2)["wtxid"], tx.getwtxid())
287287

288288
# Check that weight and vsize are properly reported in mempool entry (txid2)
289289
assert_equal(self.nodes[0].getmempoolentry(txid2)["vsize"], tx.get_vsize())
@@ -306,7 +306,7 @@ def run_test(self):
306306
assert txid3 in template_txids
307307

308308
# Check that wtxid is properly reported in mempool entry (txid3)
309-
assert_equal(int(self.nodes[0].getmempoolentry(txid3)["wtxid"], 16), tx.calc_sha256(True))
309+
assert_equal(self.nodes[0].getmempoolentry(txid3)["wtxid"], tx.getwtxid())
310310

311311
# Check that weight and vsize are properly reported in mempool entry (txid3)
312312
assert_equal(self.nodes[0].getmempoolentry(txid3)["vsize"], tx.get_vsize())

test/functional/p2p_compactblocks.py

+4-7
Original file line numberDiff line numberDiff line change
@@ -347,13 +347,11 @@ def check_compactblock_construction_from_block(self, header_and_shortids, block_
347347

348348
# Check that all prefilled_txn entries match what's in the block.
349349
for entry in header_and_shortids.prefilled_txn:
350-
entry.tx.calc_sha256()
351350
# This checks the non-witness parts of the tx agree
352-
assert_equal(entry.tx.sha256, block.vtx[entry.index].sha256)
351+
assert_equal(entry.tx.rehash(), block.vtx[entry.index].rehash())
353352

354353
# And this checks the witness
355-
wtxid = entry.tx.calc_sha256(True)
356-
assert_equal(wtxid, block.vtx[entry.index].calc_sha256(True))
354+
assert_equal(entry.tx.getwtxid(), block.vtx[entry.index].getwtxid())
357355

358356
# Check that the cmpctblock message announced all the transactions.
359357
assert_equal(len(header_and_shortids.prefilled_txn) + len(header_and_shortids.shortids), len(block.vtx))
@@ -590,10 +588,9 @@ def test_getblocktxn_handler(self, test_node):
590588
all_indices = msg.block_txn_request.to_absolute()
591589
for index in all_indices:
592590
tx = test_node.last_message["blocktxn"].block_transactions.transactions.pop(0)
593-
tx.calc_sha256()
594-
assert_equal(tx.sha256, block.vtx[index].sha256)
591+
assert_equal(tx.rehash(), block.vtx[index].rehash())
595592
# Check that the witness matches
596-
assert_equal(tx.calc_sha256(True), block.vtx[index].calc_sha256(True))
593+
assert_equal(tx.getwtxid(), block.vtx[index].getwtxid())
597594
test_node.last_message.pop("blocktxn", None)
598595
current_height -= 1
599596

test/functional/p2p_segwit.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -336,8 +336,7 @@ def test_unnecessary_witness_before_segwit_activation(self):
336336

337337
# Verify the hash with witness differs from the txid
338338
# (otherwise our testing framework must be broken!)
339-
tx.rehash()
340-
assert tx.sha256 != tx.calc_sha256(with_witness=True)
339+
assert tx.rehash() != tx.getwtxid()
341340

342341
# Construct a block that includes the transaction.
343342
block = self.build_next_block()
@@ -1293,7 +1292,7 @@ def test_tx_relay_after_segwit_activation(self):
12931292
# Test that getrawtransaction returns correct witness information
12941293
# hash, size, vsize
12951294
raw_tx = self.nodes[0].getrawtransaction(tx3.hash, 1)
1296-
assert_equal(int(raw_tx["hash"], 16), tx3.calc_sha256(True))
1295+
assert_equal(raw_tx["hash"], tx3.getwtxid())
12971296
assert_equal(raw_tx["size"], len(tx3.serialize_with_witness()))
12981297
vsize = tx3.get_vsize()
12991298
assert_equal(raw_tx["vsize"], vsize)

0 commit comments

Comments
 (0)