test: avoid unneeded (w)txid hex -> integer conversions

Rather than determining a CTransaction's (w)txid as an integer by
converting it's hex value, it can be directly accessed via the
introduced `.{w,}txid_int` property.
This commit is contained in:
Sebastian Falbesoner committed 2025-06-11 00:52:25 +02:00
1 parent 472f3770ae
commit 4ef6253017
4 files changed
+26 -26

No files matched your search

+1 -1
View File
@@ -55,7 +55,7 @@ class UTXOSetHashTest(BitcoinTestFramework):
if (coinbase and n > 0): if (coinbase and n > 0):
continue continue
data = COutPoint(int(tx.txid_hex, 16), n).serialize() data = COutPoint(tx.txid_int, n).serialize()
data += (height * 2 + coinbase).to_bytes(4, "little") data += (height * 2 + coinbase).to_bytes(4, "little")
data += tx_out.serialize() data += tx_out.serialize()
+3 -3
View File
@@ -73,7 +73,7 @@ class MempoolCoinbaseTest(BitcoinTestFramework):
# request very recent, unanounced transactions. # request very recent, unanounced transactions.
assert_equal(len(peer1.get_invs()), 0) assert_equal(len(peer1.get_invs()), 0)
# It's too early to request these two transactions # It's too early to request these two transactions
requests_too_recent = msg_getdata([CInv(t=MSG_WTX, h=int(tx["tx"].wtxid_hex, 16)) for tx in [tx_before_reorg, tx_child]]) requests_too_recent = msg_getdata([CInv(t=MSG_WTX, h=tx["tx"].wtxid_int) for tx in [tx_before_reorg, tx_child]])
peer1.send_and_ping(requests_too_recent) peer1.send_and_ping(requests_too_recent)
for _ in range(len(requests_too_recent.inv)): for _ in range(len(requests_too_recent.inv)):
peer1.sync_with_ping() peer1.sync_with_ping()
@@ -82,7 +82,7 @@ class MempoolCoinbaseTest(BitcoinTestFramework):
assert "notfound" in peer1.last_message assert "notfound" in peer1.last_message
# Request the tx from the disconnected block # Request the tx from the disconnected block
request_disconnected_tx = msg_getdata([CInv(t=MSG_WTX, h=int(tx_disconnected["tx"].wtxid_hex, 16))]) request_disconnected_tx = msg_getdata([CInv(t=MSG_WTX, h=tx_disconnected["tx"].wtxid_int)])
peer1.send_and_ping(request_disconnected_tx) peer1.send_and_ping(request_disconnected_tx)
# The tx from the disconnected block was never announced, and it entered the mempool later # The tx from the disconnected block was never announced, and it entered the mempool later
@@ -102,7 +102,7 @@ class MempoolCoinbaseTest(BitcoinTestFramework):
last_tx_received = peer1.last_message["tx"] last_tx_received = peer1.last_message["tx"]
tx_after_reorg = self.wallet.send_self_transfer(from_node=self.nodes[1]) tx_after_reorg = self.wallet.send_self_transfer(from_node=self.nodes[1])
request_after_reorg = msg_getdata([CInv(t=MSG_WTX, h=int(tx_after_reorg["tx"].wtxid_hex, 16))]) request_after_reorg = msg_getdata([CInv(t=MSG_WTX, h=tx_after_reorg["tx"].wtxid_int)])
assert tx_after_reorg["txid"] in self.nodes[1].getrawmempool() assert tx_after_reorg["txid"] in self.nodes[1].getrawmempool()
peer1.send_and_ping(request_after_reorg) peer1.send_and_ping(request_after_reorg)
with p2p_lock: with p2p_lock:
+10 -10
View File
@@ -83,7 +83,7 @@ class PackageRelayTest(BitcoinTestFramework):
peer_sender = node.add_p2p_connection(P2PInterface()) peer_sender = node.add_p2p_connection(P2PInterface())
# 1. Child is received first (perhaps the low feerate parent didn't meet feefilter or the requests were sent to different nodes). It is missing an input. # 1. Child is received first (perhaps the low feerate parent didn't meet feefilter or the requests were sent to different nodes). It is missing an input.
high_child_wtxid_int = int(high_fee_child["tx"].wtxid_hex, 16) high_child_wtxid_int = high_fee_child["tx"].wtxid_int
peer_sender.send_and_ping(msg_inv([CInv(t=MSG_WTX, h=high_child_wtxid_int)])) peer_sender.send_and_ping(msg_inv([CInv(t=MSG_WTX, h=high_child_wtxid_int)]))
peer_sender.wait_for_getdata([high_child_wtxid_int]) peer_sender.wait_for_getdata([high_child_wtxid_int])
peer_sender.send_and_ping(msg_tx(high_fee_child["tx"])) peer_sender.send_and_ping(msg_tx(high_fee_child["tx"]))
@@ -112,7 +112,7 @@ class PackageRelayTest(BitcoinTestFramework):
peer_ignored = node.add_outbound_p2p_connection(P2PInterface(), p2p_idx=2, connection_type="outbound-full-relay") peer_ignored = node.add_outbound_p2p_connection(P2PInterface(), p2p_idx=2, connection_type="outbound-full-relay")
# 1. Parent is relayed first. It is too low feerate. # 1. Parent is relayed first. It is too low feerate.
parent_wtxid_int = int(low_fee_parent["tx"].wtxid_hex, 16) parent_wtxid_int = low_fee_parent["tx"].wtxid_int
peer_sender.send_and_ping(msg_inv([CInv(t=MSG_WTX, h=parent_wtxid_int)])) peer_sender.send_and_ping(msg_inv([CInv(t=MSG_WTX, h=parent_wtxid_int)]))
peer_sender.wait_for_getdata([parent_wtxid_int]) peer_sender.wait_for_getdata([parent_wtxid_int])
peer_sender.send_and_ping(msg_tx(low_fee_parent["tx"])) peer_sender.send_and_ping(msg_tx(low_fee_parent["tx"]))
@@ -123,7 +123,7 @@ class PackageRelayTest(BitcoinTestFramework):
assert "getdata" not in peer_ignored.last_message assert "getdata" not in peer_ignored.last_message
# 2. Child is relayed next. It is missing an input. # 2. Child is relayed next. It is missing an input.
high_child_wtxid_int = int(high_fee_child["tx"].wtxid_hex, 16) high_child_wtxid_int = high_fee_child["tx"].wtxid_int
peer_sender.send_and_ping(msg_inv([CInv(t=MSG_WTX, h=high_child_wtxid_int)])) peer_sender.send_and_ping(msg_inv([CInv(t=MSG_WTX, h=high_child_wtxid_int)]))
peer_sender.wait_for_getdata([high_child_wtxid_int]) peer_sender.wait_for_getdata([high_child_wtxid_int])
peer_sender.send_and_ping(msg_tx(high_fee_child["tx"])) peer_sender.send_and_ping(msg_tx(high_fee_child["tx"]))
@@ -156,7 +156,7 @@ class PackageRelayTest(BitcoinTestFramework):
self.log.info("Check that tx caches low fee parent + low fee child package rejections") self.log.info("Check that tx caches low fee parent + low fee child package rejections")
# 1. Send parent, rejected for being low feerate. # 1. Send parent, rejected for being low feerate.
parent_wtxid_int = int(low_fee_parent["tx"].wtxid_hex, 16) parent_wtxid_int = low_fee_parent["tx"].wtxid_int
peer_sender.send_and_ping(msg_inv([CInv(t=MSG_WTX, h=parent_wtxid_int)])) peer_sender.send_and_ping(msg_inv([CInv(t=MSG_WTX, h=parent_wtxid_int)]))
peer_sender.wait_for_getdata([parent_wtxid_int]) peer_sender.wait_for_getdata([parent_wtxid_int])
peer_sender.send_and_ping(msg_tx(low_fee_parent["tx"])) peer_sender.send_and_ping(msg_tx(low_fee_parent["tx"]))
@@ -167,7 +167,7 @@ class PackageRelayTest(BitcoinTestFramework):
assert "getdata" not in peer_ignored.last_message assert "getdata" not in peer_ignored.last_message
# 2. Send an (orphan) child that has a higher feerate, but not enough to bump the parent. # 2. Send an (orphan) child that has a higher feerate, but not enough to bump the parent.
med_child_wtxid_int = int(med_fee_child["tx"].wtxid_hex, 16) med_child_wtxid_int = med_fee_child["tx"].wtxid_int
peer_sender.send_and_ping(msg_inv([CInv(t=MSG_WTX, h=med_child_wtxid_int)])) peer_sender.send_and_ping(msg_inv([CInv(t=MSG_WTX, h=med_child_wtxid_int)]))
peer_sender.wait_for_getdata([med_child_wtxid_int]) peer_sender.wait_for_getdata([med_child_wtxid_int])
peer_sender.send_and_ping(msg_tx(med_fee_child["tx"])) peer_sender.send_and_ping(msg_tx(med_fee_child["tx"]))
@@ -193,7 +193,7 @@ class PackageRelayTest(BitcoinTestFramework):
assert med_fee_child["txid"] not in node.getrawmempool() assert med_fee_child["txid"] not in node.getrawmempool()
# 5. Send the high feerate (orphan) child # 5. Send the high feerate (orphan) child
high_child_wtxid_int = int(high_fee_child["tx"].wtxid_hex, 16) high_child_wtxid_int = high_fee_child["tx"].wtxid_int
peer_sender.send_and_ping(msg_inv([CInv(t=MSG_WTX, h=high_child_wtxid_int)])) peer_sender.send_and_ping(msg_inv([CInv(t=MSG_WTX, h=high_child_wtxid_int)]))
peer_sender.wait_for_getdata([high_child_wtxid_int]) peer_sender.wait_for_getdata([high_child_wtxid_int])
peer_sender.send_and_ping(msg_tx(high_fee_child["tx"])) peer_sender.send_and_ping(msg_tx(high_fee_child["tx"]))
@@ -229,7 +229,7 @@ class PackageRelayTest(BitcoinTestFramework):
parent_sender = node.add_p2p_connection(P2PInterface()) parent_sender = node.add_p2p_connection(P2PInterface())
# 1. Child is received first. It is missing an input. # 1. Child is received first. It is missing an input.
child_wtxid_int = int(tx_orphan_bad_wit.wtxid_hex, 16) child_wtxid_int = tx_orphan_bad_wit.wtxid_int
bad_orphan_sender.send_and_ping(msg_inv([CInv(t=MSG_WTX, h=child_wtxid_int)])) bad_orphan_sender.send_and_ping(msg_inv([CInv(t=MSG_WTX, h=child_wtxid_int)]))
bad_orphan_sender.wait_for_getdata([child_wtxid_int]) bad_orphan_sender.wait_for_getdata([child_wtxid_int])
bad_orphan_sender.send_and_ping(msg_tx(tx_orphan_bad_wit)) bad_orphan_sender.send_and_ping(msg_tx(tx_orphan_bad_wit))
@@ -270,13 +270,13 @@ class PackageRelayTest(BitcoinTestFramework):
fake_parent_sender = node.add_p2p_connection(P2PInterface()) fake_parent_sender = node.add_p2p_connection(P2PInterface())
# 1. Child is received first. It is missing an input. # 1. Child is received first. It is missing an input.
child_wtxid_int = int(high_fee_child["tx"].wtxid_hex, 16) child_wtxid_int = high_fee_child["tx"].wtxid_int
package_sender.send_and_ping(msg_inv([CInv(t=MSG_WTX, h=child_wtxid_int)])) package_sender.send_and_ping(msg_inv([CInv(t=MSG_WTX, h=child_wtxid_int)]))
package_sender.wait_for_getdata([child_wtxid_int]) package_sender.wait_for_getdata([child_wtxid_int])
package_sender.send_and_ping(msg_tx(high_fee_child["tx"])) package_sender.send_and_ping(msg_tx(high_fee_child["tx"]))
# 2. Node requests the missing parent by txid. # 2. Node requests the missing parent by txid.
parent_txid_int = int(tx_parent_bad_wit.txid_hex, 16) parent_txid_int = tx_parent_bad_wit.txid_int
package_sender.wait_for_getdata([parent_txid_int]) package_sender.wait_for_getdata([parent_txid_int])
# 3. A different node relays the parent. The parent is first evaluated by itself and # 3. A different node relays the parent. The parent is first evaluated by itself and
@@ -292,7 +292,7 @@ class PackageRelayTest(BitcoinTestFramework):
self.log.info("Check that fake parent does not cause orphan to be deleted and real package can still be submitted") self.log.info("Check that fake parent does not cause orphan to be deleted and real package can still be submitted")
# 5. Child-sending should not have been punished and the orphan should remain in orphanage. # 5. Child-sending should not have been punished and the orphan should remain in orphanage.
# It can send the "real" parent transaction, and the package is accepted. # It can send the "real" parent transaction, and the package is accepted.
parent_wtxid_int = int(low_fee_parent["tx"].wtxid_hex, 16) parent_wtxid_int = low_fee_parent["tx"].wtxid_int
package_sender.send_and_ping(msg_inv([CInv(t=MSG_WTX, h=parent_wtxid_int)])) package_sender.send_and_ping(msg_inv([CInv(t=MSG_WTX, h=parent_wtxid_int)]))
package_sender.wait_for_getdata([parent_wtxid_int]) package_sender.wait_for_getdata([parent_wtxid_int])
package_sender.send_and_ping(msg_tx(low_fee_parent["tx"])) package_sender.send_and_ping(msg_tx(low_fee_parent["tx"]))
+12 -12
View File
@@ -132,7 +132,7 @@ class OrphanHandlingTest(BitcoinTestFramework):
def relay_transaction(self, peer, tx): def relay_transaction(self, peer, tx):
"""Relay transaction using MSG_WTX""" """Relay transaction using MSG_WTX"""
wtxid = int(tx.wtxid_hex, 16) wtxid = tx.wtxid_int
peer.send_and_ping(msg_inv([CInv(t=MSG_WTX, h=wtxid)])) peer.send_and_ping(msg_inv([CInv(t=MSG_WTX, h=wtxid)]))
self.nodes[0].bumpmocktime(TXREQUEST_TIME_SKIP) self.nodes[0].bumpmocktime(TXREQUEST_TIME_SKIP)
peer.wait_for_getdata([wtxid]) peer.wait_for_getdata([wtxid])
@@ -184,7 +184,7 @@ class OrphanHandlingTest(BitcoinTestFramework):
# Spy peer should not be able to query the node for the parent yet, since it hasn't been # Spy peer should not be able to query the node for the parent yet, since it hasn't been
# announced / insufficient time has elapsed. # announced / insufficient time has elapsed.
parent_inv = CInv(t=MSG_WTX, h=int(tx_parent_arrives["tx"].wtxid_hex, 16)) parent_inv = CInv(t=MSG_WTX, h=tx_parent_arrives["tx"].wtxid_int)
assert_equal(len(peer_spy.get_invs()), 0) assert_equal(len(peer_spy.get_invs()), 0)
peer_spy.assert_no_immediate_response(msg_getdata([parent_inv])) peer_spy.assert_no_immediate_response(msg_getdata([parent_inv]))
@@ -242,7 +242,7 @@ class OrphanHandlingTest(BitcoinTestFramework):
# The parent should be requested because even though the txid commits to the fee, it doesn't # The parent should be requested because even though the txid commits to the fee, it doesn't
# commit to the feerate. Delayed because it's by txid and this is not a preferred relay peer. # commit to the feerate. Delayed because it's by txid and this is not a preferred relay peer.
self.nodes[0].bumpmocktime(NONPREF_PEER_TX_DELAY + TXID_RELAY_DELAY) self.nodes[0].bumpmocktime(NONPREF_PEER_TX_DELAY + TXID_RELAY_DELAY)
peer2.wait_for_getdata([int(parent_low_fee["tx"].txid_hex, 16)]) peer2.wait_for_getdata([parent_low_fee["tx"].txid_int])
self.log.info("Test orphan handling when a parent was previously downloaded with witness stripped") self.log.info("Test orphan handling when a parent was previously downloaded with witness stripped")
parent_normal = self.wallet.create_self_transfer() parent_normal = self.wallet.create_self_transfer()
@@ -262,7 +262,7 @@ class OrphanHandlingTest(BitcoinTestFramework):
# The parent should be requested since the unstripped wtxid would differ. Delayed because # The parent should be requested since the unstripped wtxid would differ. Delayed because
# it's by txid and this is not a preferred relay peer. # it's by txid and this is not a preferred relay peer.
self.nodes[0].bumpmocktime(NONPREF_PEER_TX_DELAY + TXID_RELAY_DELAY) self.nodes[0].bumpmocktime(NONPREF_PEER_TX_DELAY + TXID_RELAY_DELAY)
peer2.wait_for_getdata([int(parent_normal["tx"].txid_hex, 16)]) peer2.wait_for_getdata([parent_normal["tx"].txid_int])
# parent_normal can be relayed again even though parent1_witness_stripped was rejected # parent_normal can be relayed again even though parent1_witness_stripped was rejected
self.relay_transaction(peer1, parent_normal["tx"]) self.relay_transaction(peer1, parent_normal["tx"])
@@ -348,9 +348,9 @@ class OrphanHandlingTest(BitcoinTestFramework):
assert_equal(inflight_parent_AB["txid"], inflight_parent_AB["wtxid"]) assert_equal(inflight_parent_AB["txid"], inflight_parent_AB["wtxid"])
# Announce inflight_parent_AB and wait for getdata # Announce inflight_parent_AB and wait for getdata
peer_txrequest.send_and_ping(msg_inv([CInv(t=MSG_WTX, h=int(inflight_parent_AB["tx"].wtxid_hex, 16))])) peer_txrequest.send_and_ping(msg_inv([CInv(t=MSG_WTX, h=inflight_parent_AB["tx"].wtxid_int)]))
self.nodes[0].bumpmocktime(NONPREF_PEER_TX_DELAY) self.nodes[0].bumpmocktime(NONPREF_PEER_TX_DELAY)
peer_txrequest.wait_for_getdata([int(inflight_parent_AB["tx"].wtxid_hex, 16)]) peer_txrequest.wait_for_getdata([inflight_parent_AB["tx"].wtxid_int])
self.log.info("Test that the node does not request a parent if it has an in-flight txrequest") self.log.info("Test that the node does not request a parent if it has an in-flight txrequest")
# Relay orphan child_A # Relay orphan child_A
@@ -661,15 +661,15 @@ class OrphanHandlingTest(BitcoinTestFramework):
# The outbound peer should be preferred for getting orphan parents # The outbound peer should be preferred for getting orphan parents
self.nodes[0].bumpmocktime(TXID_RELAY_DELAY) self.nodes[0].bumpmocktime(TXID_RELAY_DELAY)
peer_outbound.wait_for_parent_requests([int(parent_tx.txid_hex, 16)]) peer_outbound.wait_for_parent_requests([parent_tx.txid_int])
# There should be no request to the inbound peer # There should be no request to the inbound peer
peer_inbound.assert_never_requested(int(parent_tx.txid_hex, 16)) peer_inbound.assert_never_requested(parent_tx.txid_int)
self.log.info("Test that, if the preferred peer doesn't respond, the node sends another request") self.log.info("Test that, if the preferred peer doesn't respond, the node sends another request")
self.nodes[0].bumpmocktime(GETDATA_TX_INTERVAL) self.nodes[0].bumpmocktime(GETDATA_TX_INTERVAL)
peer_inbound.sync_with_ping() peer_inbound.sync_with_ping()
peer_inbound.wait_for_parent_requests([int(parent_tx.txid_hex, 16)]) peer_inbound.wait_for_parent_requests([parent_tx.txid_int])
@cleanup @cleanup
def test_announcers_before_and_after(self): def test_announcers_before_and_after(self):
@@ -701,7 +701,7 @@ class OrphanHandlingTest(BitcoinTestFramework):
# Peer disconnects before responding to request # Peer disconnects before responding to request
self.nodes[0].bumpmocktime(TXID_RELAY_DELAY) self.nodes[0].bumpmocktime(TXID_RELAY_DELAY)
peer_early_disconnected.wait_for_parent_requests([int(parent_tx.txid_hex, 16)]) peer_early_disconnected.wait_for_parent_requests([parent_tx.txid_int])
peer_early_disconnected.peer_disconnect() peer_early_disconnected.peer_disconnect()
# The orphan should have 1 announcer left after the node finishes disconnecting peer_early_disconnected. # The orphan should have 1 announcer left after the node finishes disconnecting peer_early_disconnected.
@@ -710,7 +710,7 @@ class OrphanHandlingTest(BitcoinTestFramework):
# The node should retry with the other peer that announced the orphan earlier. # The node should retry with the other peer that announced the orphan earlier.
# This node's request was additionally delayed because it's an inbound peer. # This node's request was additionally delayed because it's an inbound peer.
self.nodes[0].bumpmocktime(NONPREF_PEER_TX_DELAY) self.nodes[0].bumpmocktime(NONPREF_PEER_TX_DELAY)
peer_early_unresponsive.wait_for_parent_requests([int(parent_tx.txid_hex, 16)]) peer_early_unresponsive.wait_for_parent_requests([parent_tx.txid_int])
self.log.info("Test that the node uses peers who announce the tx after realizing it's an orphan") self.log.info("Test that the node uses peers who announce the tx after realizing it's an orphan")
peer_late_announcer.send_and_ping(msg_inv([orphan_inv])) peer_late_announcer.send_and_ping(msg_inv([orphan_inv]))
@@ -721,7 +721,7 @@ class OrphanHandlingTest(BitcoinTestFramework):
assert_equal(len(orphanage[0]["from"]), 2) assert_equal(len(orphanage[0]["from"]), 2)
self.nodes[0].bumpmocktime(GETDATA_TX_INTERVAL) self.nodes[0].bumpmocktime(GETDATA_TX_INTERVAL)
peer_late_announcer.wait_for_parent_requests([int(parent_tx.txid_hex, 16)]) peer_late_announcer.wait_for_parent_requests([parent_tx.txid_int])
@cleanup @cleanup
def test_parents_change(self): def test_parents_change(self):