Skip to content

Commit e9cdaef

Browse files
committed
test: introduce and use CTransaction .wtxid_int property
This commits removes the `.calc_sha256` method from the CTransaction and introduces a property `.wtxid_int` property as replacement.
1 parent 9b3dce2 commit e9cdaef

File tree

5 files changed

+14
-14
lines changed

5 files changed

+14
-14
lines changed

test/functional/p2p_compactblocks.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ def check_compactblock_construction_from_block(self, header_and_shortids, block_
365365
# Already checked prefilled transactions above
366366
header_and_shortids.prefilled_txn.pop(0)
367367
else:
368-
tx_hash = block.vtx[index].calc_sha256(True)
368+
tx_hash = block.vtx[index].wtxid_int
369369
shortid = calculate_shortid(k0, k1, tx_hash)
370370
assert_equal(shortid, header_and_shortids.shortids[0])
371371
header_and_shortids.shortids.pop(0)
@@ -395,7 +395,7 @@ def test_compactblock_requests(self, test_node):
395395
comp_block.header = CBlockHeader(block)
396396
comp_block.nonce = 0
397397
[k0, k1] = comp_block.get_siphash_keys()
398-
coinbase_hash = block.vtx[0].calc_sha256(True)
398+
coinbase_hash = block.vtx[0].wtxid_int
399399
comp_block.shortids = [calculate_shortid(k0, k1, coinbase_hash)]
400400
test_node.send_and_ping(msg_cmpctblock(comp_block.to_p2p()))
401401
assert_equal(int(node.getbestblockhash(), 16), block.hashPrevBlock)

test/functional/p2p_segwit.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ def announce_tx_and_wait_for_getdata(self, tx, success=True, use_wtxid=False):
174174
with p2p_lock:
175175
self.last_message.pop("getdata", None)
176176
if use_wtxid:
177-
wtxid = tx.calc_sha256(True)
177+
wtxid = tx.wtxid_int
178178
self.send_without_ping(msg_inv(inv=[CInv(MSG_WTX, wtxid)]))
179179
else:
180180
self.send_without_ping(msg_inv(inv=[CInv(MSG_TX, tx.sha256)]))
@@ -1979,7 +1979,7 @@ def received_wtxidrelay():
19791979
self.wtx_node.announce_tx_and_wait_for_getdata(tx2, use_wtxid=True)
19801980
with p2p_lock:
19811981
lgd = self.wtx_node.lastgetdata[:]
1982-
assert_equal(lgd, [CInv(MSG_WTX, tx2.calc_sha256(True))])
1982+
assert_equal(lgd, [CInv(MSG_WTX, tx2.wtxid_int)])
19831983

19841984
# Announce Segwit transaction from non wtxidrelay peer
19851985
# and wait for getdata

test/functional/p2p_tx_privacy.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ def run_test(self):
7171

7272
# Spy should only get an inv for the second transaction as the first
7373
# one was received pre-verack with the spy
74-
spy.wait_for_inv_match(CInv(MSG_WTX, tx2.calc_sha256(True)))
74+
spy.wait_for_inv_match(CInv(MSG_WTX, tx2.wtxid_int))
7575

7676
if __name__ == '__main__':
7777
TxPrivacyTest(__file__).main()

test/functional/test_framework/blocktools.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ def create_block(hashprev=None, coinbase=None, ntime=None, *, version=None, tmpl
107107
block.vtx.append(coinbase)
108108
if txlist:
109109
for tx in txlist:
110-
if not hasattr(tx, 'calc_sha256'):
110+
if type(tx) is str:
111111
tx = tx_from_hex(tx)
112112
block.vtx.append(tx)
113113
block.hashMerkleRoot = block.calc_merkle_root()

test/functional/test_framework/messages.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -658,8 +658,14 @@ def serialize(self):
658658
return self.serialize_with_witness()
659659

660660
def getwtxid(self):
661+
"""Return wtxid (transaction hash with witness) as hex string."""
661662
return hash256(self.serialize())[::-1].hex()
662663

664+
@property
665+
def wtxid_int(self):
666+
"""Return wtxid (transaction hash with witness) as integer."""
667+
return uint256_from_str(hash256(self.serialize_with_witness()))
668+
663669
@property
664670
def hash(self):
665671
"""Return txid (transaction hash without witness) as hex string."""
@@ -675,12 +681,6 @@ def sha256(self):
675681
def rehash(self):
676682
return self.hash
677683

678-
# TODO: get rid of this method, replace call-sites by .wtxid_int access (not introduced yet)
679-
def calc_sha256(self, with_witness=False):
680-
if with_witness:
681-
# Don't cache the result, just return it
682-
return uint256_from_str(hash256(self.serialize_with_witness()))
683-
684684
def is_valid(self):
685685
for tout in self.vout:
686686
if tout.nValue < 0 or tout.nValue > 21000000 * COIN:
@@ -819,7 +819,7 @@ def calc_witness_merkle_root(self):
819819

820820
for tx in self.vtx[1:]:
821821
# Calculate the hashes with witness data
822-
hashes.append(ser_uint256(tx.calc_sha256(True)))
822+
hashes.append(ser_uint256(tx.wtxid_int))
823823

824824
return self.get_merkle_root(hashes)
825825

@@ -1003,7 +1003,7 @@ def initialize_from_block(self, block, nonce=0, prefill_list=None, use_witness=F
10031003
if i not in prefill_list:
10041004
tx_hash = block.vtx[i].sha256
10051005
if use_witness:
1006-
tx_hash = block.vtx[i].calc_sha256(with_witness=True)
1006+
tx_hash = block.vtx[i].wtxid_int
10071007
self.shortids.append(calculate_shortid(k0, k1, tx_hash))
10081008

10091009
def __repr__(self):

0 commit comments

Comments
 (0)