mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-08-30 08:01:12 +02:00
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.
This commit is contained in:
@@ -365,7 +365,7 @@ class CompactBlocksTest(BitcoinTestFramework):
|
|||||||
# Already checked prefilled transactions above
|
# Already checked prefilled transactions above
|
||||||
header_and_shortids.prefilled_txn.pop(0)
|
header_and_shortids.prefilled_txn.pop(0)
|
||||||
else:
|
else:
|
||||||
tx_hash = block.vtx[index].calc_sha256(True)
|
tx_hash = block.vtx[index].wtxid_int
|
||||||
shortid = calculate_shortid(k0, k1, tx_hash)
|
shortid = calculate_shortid(k0, k1, tx_hash)
|
||||||
assert_equal(shortid, header_and_shortids.shortids[0])
|
assert_equal(shortid, header_and_shortids.shortids[0])
|
||||||
header_and_shortids.shortids.pop(0)
|
header_and_shortids.shortids.pop(0)
|
||||||
@@ -395,7 +395,7 @@ class CompactBlocksTest(BitcoinTestFramework):
|
|||||||
comp_block.header = CBlockHeader(block)
|
comp_block.header = CBlockHeader(block)
|
||||||
comp_block.nonce = 0
|
comp_block.nonce = 0
|
||||||
[k0, k1] = comp_block.get_siphash_keys()
|
[k0, k1] = comp_block.get_siphash_keys()
|
||||||
coinbase_hash = block.vtx[0].calc_sha256(True)
|
coinbase_hash = block.vtx[0].wtxid_int
|
||||||
comp_block.shortids = [calculate_shortid(k0, k1, coinbase_hash)]
|
comp_block.shortids = [calculate_shortid(k0, k1, coinbase_hash)]
|
||||||
test_node.send_and_ping(msg_cmpctblock(comp_block.to_p2p()))
|
test_node.send_and_ping(msg_cmpctblock(comp_block.to_p2p()))
|
||||||
assert_equal(int(node.getbestblockhash(), 16), block.hashPrevBlock)
|
assert_equal(int(node.getbestblockhash(), 16), block.hashPrevBlock)
|
||||||
|
@@ -174,7 +174,7 @@ class TestP2PConn(P2PInterface):
|
|||||||
with p2p_lock:
|
with p2p_lock:
|
||||||
self.last_message.pop("getdata", None)
|
self.last_message.pop("getdata", None)
|
||||||
if use_wtxid:
|
if use_wtxid:
|
||||||
wtxid = tx.calc_sha256(True)
|
wtxid = tx.wtxid_int
|
||||||
self.send_without_ping(msg_inv(inv=[CInv(MSG_WTX, wtxid)]))
|
self.send_without_ping(msg_inv(inv=[CInv(MSG_WTX, wtxid)]))
|
||||||
else:
|
else:
|
||||||
self.send_without_ping(msg_inv(inv=[CInv(MSG_TX, tx.sha256)]))
|
self.send_without_ping(msg_inv(inv=[CInv(MSG_TX, tx.sha256)]))
|
||||||
@@ -1979,7 +1979,7 @@ class SegWitTest(BitcoinTestFramework):
|
|||||||
self.wtx_node.announce_tx_and_wait_for_getdata(tx2, use_wtxid=True)
|
self.wtx_node.announce_tx_and_wait_for_getdata(tx2, use_wtxid=True)
|
||||||
with p2p_lock:
|
with p2p_lock:
|
||||||
lgd = self.wtx_node.lastgetdata[:]
|
lgd = self.wtx_node.lastgetdata[:]
|
||||||
assert_equal(lgd, [CInv(MSG_WTX, tx2.calc_sha256(True))])
|
assert_equal(lgd, [CInv(MSG_WTX, tx2.wtxid_int)])
|
||||||
|
|
||||||
# Announce Segwit transaction from non wtxidrelay peer
|
# Announce Segwit transaction from non wtxidrelay peer
|
||||||
# and wait for getdata
|
# and wait for getdata
|
||||||
|
@@ -71,7 +71,7 @@ class TxPrivacyTest(BitcoinTestFramework):
|
|||||||
|
|
||||||
# Spy should only get an inv for the second transaction as the first
|
# Spy should only get an inv for the second transaction as the first
|
||||||
# one was received pre-verack with the spy
|
# one was received pre-verack with the spy
|
||||||
spy.wait_for_inv_match(CInv(MSG_WTX, tx2.calc_sha256(True)))
|
spy.wait_for_inv_match(CInv(MSG_WTX, tx2.wtxid_int))
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
TxPrivacyTest(__file__).main()
|
TxPrivacyTest(__file__).main()
|
||||||
|
@@ -107,7 +107,7 @@ def create_block(hashprev=None, coinbase=None, ntime=None, *, version=None, tmpl
|
|||||||
block.vtx.append(coinbase)
|
block.vtx.append(coinbase)
|
||||||
if txlist:
|
if txlist:
|
||||||
for tx in txlist:
|
for tx in txlist:
|
||||||
if not hasattr(tx, 'calc_sha256'):
|
if type(tx) is str:
|
||||||
tx = tx_from_hex(tx)
|
tx = tx_from_hex(tx)
|
||||||
block.vtx.append(tx)
|
block.vtx.append(tx)
|
||||||
block.hashMerkleRoot = block.calc_merkle_root()
|
block.hashMerkleRoot = block.calc_merkle_root()
|
||||||
|
@@ -658,8 +658,14 @@ class CTransaction:
|
|||||||
return self.serialize_with_witness()
|
return self.serialize_with_witness()
|
||||||
|
|
||||||
def getwtxid(self):
|
def getwtxid(self):
|
||||||
|
"""Return wtxid (transaction hash with witness) as hex string."""
|
||||||
return hash256(self.serialize())[::-1].hex()
|
return hash256(self.serialize())[::-1].hex()
|
||||||
|
|
||||||
|
@property
|
||||||
|
def wtxid_int(self):
|
||||||
|
"""Return wtxid (transaction hash with witness) as integer."""
|
||||||
|
return uint256_from_str(hash256(self.serialize_with_witness()))
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def hash(self):
|
def hash(self):
|
||||||
"""Return txid (transaction hash without witness) as hex string."""
|
"""Return txid (transaction hash without witness) as hex string."""
|
||||||
@@ -675,12 +681,6 @@ class CTransaction:
|
|||||||
def rehash(self):
|
def rehash(self):
|
||||||
return self.hash
|
return self.hash
|
||||||
|
|
||||||
# TODO: get rid of this method, replace call-sites by .wtxid_int access (not introduced yet)
|
|
||||||
def calc_sha256(self, with_witness=False):
|
|
||||||
if with_witness:
|
|
||||||
# Don't cache the result, just return it
|
|
||||||
return uint256_from_str(hash256(self.serialize_with_witness()))
|
|
||||||
|
|
||||||
def is_valid(self):
|
def is_valid(self):
|
||||||
for tout in self.vout:
|
for tout in self.vout:
|
||||||
if tout.nValue < 0 or tout.nValue > 21000000 * COIN:
|
if tout.nValue < 0 or tout.nValue > 21000000 * COIN:
|
||||||
@@ -819,7 +819,7 @@ class CBlock(CBlockHeader):
|
|||||||
|
|
||||||
for tx in self.vtx[1:]:
|
for tx in self.vtx[1:]:
|
||||||
# Calculate the hashes with witness data
|
# Calculate the hashes with witness data
|
||||||
hashes.append(ser_uint256(tx.calc_sha256(True)))
|
hashes.append(ser_uint256(tx.wtxid_int))
|
||||||
|
|
||||||
return self.get_merkle_root(hashes)
|
return self.get_merkle_root(hashes)
|
||||||
|
|
||||||
@@ -1003,7 +1003,7 @@ class HeaderAndShortIDs:
|
|||||||
if i not in prefill_list:
|
if i not in prefill_list:
|
||||||
tx_hash = block.vtx[i].sha256
|
tx_hash = block.vtx[i].sha256
|
||||||
if use_witness:
|
if use_witness:
|
||||||
tx_hash = block.vtx[i].calc_sha256(with_witness=True)
|
tx_hash = block.vtx[i].wtxid_int
|
||||||
self.shortids.append(calculate_shortid(k0, k1, tx_hash))
|
self.shortids.append(calculate_shortid(k0, k1, tx_hash))
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
|
Reference in New Issue
Block a user