From 4ef6253017672a74c584e6e9b449ffff79f67273 Mon Sep 17 00:00:00 2001 From: Sebastian Falbesoner Date: Mon, 5 May 2025 18:25:08 +0200 Subject: [PATCH] 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. --- test/functional/feature_utxo_set_hash.py | 2 +- test/functional/mempool_reorg.py | 6 +++--- test/functional/p2p_opportunistic_1p1c.py | 20 +++++++++---------- test/functional/p2p_orphan_handling.py | 24 +++++++++++------------ 4 files changed, 26 insertions(+), 26 deletions(-) diff --git a/test/functional/feature_utxo_set_hash.py b/test/functional/feature_utxo_set_hash.py index e2985627b88..1b49a7d855f 100755 --- a/test/functional/feature_utxo_set_hash.py +++ b/test/functional/feature_utxo_set_hash.py @@ -55,7 +55,7 @@ class UTXOSetHashTest(BitcoinTestFramework): if (coinbase and n > 0): 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 += tx_out.serialize() diff --git a/test/functional/mempool_reorg.py b/test/functional/mempool_reorg.py index c1de3d0d185..818dd2cae86 100755 --- a/test/functional/mempool_reorg.py +++ b/test/functional/mempool_reorg.py @@ -73,7 +73,7 @@ class MempoolCoinbaseTest(BitcoinTestFramework): # request very recent, unanounced transactions. assert_equal(len(peer1.get_invs()), 0) # 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) for _ in range(len(requests_too_recent.inv)): peer1.sync_with_ping() @@ -82,7 +82,7 @@ class MempoolCoinbaseTest(BitcoinTestFramework): assert "notfound" in peer1.last_message # 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) # 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"] 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() peer1.send_and_ping(request_after_reorg) with p2p_lock: diff --git a/test/functional/p2p_opportunistic_1p1c.py b/test/functional/p2p_opportunistic_1p1c.py index 49171de82f2..267a878323d 100755 --- a/test/functional/p2p_opportunistic_1p1c.py +++ b/test/functional/p2p_opportunistic_1p1c.py @@ -83,7 +83,7 @@ class PackageRelayTest(BitcoinTestFramework): 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. - 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.wait_for_getdata([high_child_wtxid_int]) 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") # 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.wait_for_getdata([parent_wtxid_int]) 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 # 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.wait_for_getdata([high_child_wtxid_int]) 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") # 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.wait_for_getdata([parent_wtxid_int]) 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 # 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.wait_for_getdata([med_child_wtxid_int]) 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() # 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.wait_for_getdata([high_child_wtxid_int]) 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()) # 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.wait_for_getdata([child_wtxid_int]) 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()) # 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.wait_for_getdata([child_wtxid_int]) package_sender.send_and_ping(msg_tx(high_fee_child["tx"])) # 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]) # 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") # 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. - 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.wait_for_getdata([parent_wtxid_int]) package_sender.send_and_ping(msg_tx(low_fee_parent["tx"])) diff --git a/test/functional/p2p_orphan_handling.py b/test/functional/p2p_orphan_handling.py index b0cb1331d36..d10c796acf7 100755 --- a/test/functional/p2p_orphan_handling.py +++ b/test/functional/p2p_orphan_handling.py @@ -132,7 +132,7 @@ class OrphanHandlingTest(BitcoinTestFramework): def relay_transaction(self, peer, tx): """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)])) self.nodes[0].bumpmocktime(TXREQUEST_TIME_SKIP) 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 # 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) 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 # 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) - 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") 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 # it's by txid and this is not a preferred relay peer. 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 self.relay_transaction(peer1, parent_normal["tx"]) @@ -348,9 +348,9 @@ class OrphanHandlingTest(BitcoinTestFramework): assert_equal(inflight_parent_AB["txid"], inflight_parent_AB["wtxid"]) # 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) - 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") # Relay orphan child_A @@ -661,15 +661,15 @@ class OrphanHandlingTest(BitcoinTestFramework): # The outbound peer should be preferred for getting orphan parents 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 - 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.nodes[0].bumpmocktime(GETDATA_TX_INTERVAL) 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 def test_announcers_before_and_after(self): @@ -701,7 +701,7 @@ class OrphanHandlingTest(BitcoinTestFramework): # Peer disconnects before responding to request 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() # 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. # This node's request was additionally delayed because it's an inbound peer. 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") peer_late_announcer.send_and_ping(msg_inv([orphan_inv])) @@ -721,7 +721,7 @@ class OrphanHandlingTest(BitcoinTestFramework): assert_equal(len(orphanage[0]["from"]), 2) 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 def test_parents_change(self):