Skip to content

Commit 4ef6253

Browse files
committed
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.
1 parent 472f377 commit 4ef6253

File tree

4 files changed

+26
-26
lines changed

4 files changed

+26
-26
lines changed

test/functional/feature_utxo_set_hash.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def test_muhash_implementation(self):
5555
if (coinbase and n > 0):
5656
continue
5757

58-
data = COutPoint(int(tx.txid_hex, 16), n).serialize()
58+
data = COutPoint(tx.txid_int, n).serialize()
5959
data += (height * 2 + coinbase).to_bytes(4, "little")
6060
data += tx_out.serialize()
6161

test/functional/mempool_reorg.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def test_reorg_relay(self):
7373
# request very recent, unanounced transactions.
7474
assert_equal(len(peer1.get_invs()), 0)
7575
# It's too early to request these two transactions
76-
requests_too_recent = msg_getdata([CInv(t=MSG_WTX, h=int(tx["tx"].wtxid_hex, 16)) for tx in [tx_before_reorg, tx_child]])
76+
requests_too_recent = msg_getdata([CInv(t=MSG_WTX, h=tx["tx"].wtxid_int) for tx in [tx_before_reorg, tx_child]])
7777
peer1.send_and_ping(requests_too_recent)
7878
for _ in range(len(requests_too_recent.inv)):
7979
peer1.sync_with_ping()
@@ -82,7 +82,7 @@ def test_reorg_relay(self):
8282
assert "notfound" in peer1.last_message
8383

8484
# Request the tx from the disconnected block
85-
request_disconnected_tx = msg_getdata([CInv(t=MSG_WTX, h=int(tx_disconnected["tx"].wtxid_hex, 16))])
85+
request_disconnected_tx = msg_getdata([CInv(t=MSG_WTX, h=tx_disconnected["tx"].wtxid_int)])
8686
peer1.send_and_ping(request_disconnected_tx)
8787

8888
# The tx from the disconnected block was never announced, and it entered the mempool later
@@ -102,7 +102,7 @@ def test_reorg_relay(self):
102102
last_tx_received = peer1.last_message["tx"]
103103

104104
tx_after_reorg = self.wallet.send_self_transfer(from_node=self.nodes[1])
105-
request_after_reorg = msg_getdata([CInv(t=MSG_WTX, h=int(tx_after_reorg["tx"].wtxid_hex, 16))])
105+
request_after_reorg = msg_getdata([CInv(t=MSG_WTX, h=tx_after_reorg["tx"].wtxid_int)])
106106
assert tx_after_reorg["txid"] in self.nodes[1].getrawmempool()
107107
peer1.send_and_ping(request_after_reorg)
108108
with p2p_lock:

test/functional/p2p_opportunistic_1p1c.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ def test_basic_child_then_parent(self):
8383
peer_sender = node.add_p2p_connection(P2PInterface())
8484

8585
# 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.
86-
high_child_wtxid_int = int(high_fee_child["tx"].wtxid_hex, 16)
86+
high_child_wtxid_int = high_fee_child["tx"].wtxid_int
8787
peer_sender.send_and_ping(msg_inv([CInv(t=MSG_WTX, h=high_child_wtxid_int)]))
8888
peer_sender.wait_for_getdata([high_child_wtxid_int])
8989
peer_sender.send_and_ping(msg_tx(high_fee_child["tx"]))
@@ -112,7 +112,7 @@ def test_basic_parent_then_child(self, wallet):
112112
peer_ignored = node.add_outbound_p2p_connection(P2PInterface(), p2p_idx=2, connection_type="outbound-full-relay")
113113

114114
# 1. Parent is relayed first. It is too low feerate.
115-
parent_wtxid_int = int(low_fee_parent["tx"].wtxid_hex, 16)
115+
parent_wtxid_int = low_fee_parent["tx"].wtxid_int
116116
peer_sender.send_and_ping(msg_inv([CInv(t=MSG_WTX, h=parent_wtxid_int)]))
117117
peer_sender.wait_for_getdata([parent_wtxid_int])
118118
peer_sender.send_and_ping(msg_tx(low_fee_parent["tx"]))
@@ -123,7 +123,7 @@ def test_basic_parent_then_child(self, wallet):
123123
assert "getdata" not in peer_ignored.last_message
124124

125125
# 2. Child is relayed next. It is missing an input.
126-
high_child_wtxid_int = int(high_fee_child["tx"].wtxid_hex, 16)
126+
high_child_wtxid_int = high_fee_child["tx"].wtxid_int
127127
peer_sender.send_and_ping(msg_inv([CInv(t=MSG_WTX, h=high_child_wtxid_int)]))
128128
peer_sender.wait_for_getdata([high_child_wtxid_int])
129129
peer_sender.send_and_ping(msg_tx(high_fee_child["tx"]))
@@ -156,7 +156,7 @@ def test_low_and_high_child(self, wallet):
156156
self.log.info("Check that tx caches low fee parent + low fee child package rejections")
157157

158158
# 1. Send parent, rejected for being low feerate.
159-
parent_wtxid_int = int(low_fee_parent["tx"].wtxid_hex, 16)
159+
parent_wtxid_int = low_fee_parent["tx"].wtxid_int
160160
peer_sender.send_and_ping(msg_inv([CInv(t=MSG_WTX, h=parent_wtxid_int)]))
161161
peer_sender.wait_for_getdata([parent_wtxid_int])
162162
peer_sender.send_and_ping(msg_tx(low_fee_parent["tx"]))
@@ -167,7 +167,7 @@ def test_low_and_high_child(self, wallet):
167167
assert "getdata" not in peer_ignored.last_message
168168

169169
# 2. Send an (orphan) child that has a higher feerate, but not enough to bump the parent.
170-
med_child_wtxid_int = int(med_fee_child["tx"].wtxid_hex, 16)
170+
med_child_wtxid_int = med_fee_child["tx"].wtxid_int
171171
peer_sender.send_and_ping(msg_inv([CInv(t=MSG_WTX, h=med_child_wtxid_int)]))
172172
peer_sender.wait_for_getdata([med_child_wtxid_int])
173173
peer_sender.send_and_ping(msg_tx(med_fee_child["tx"]))
@@ -193,7 +193,7 @@ def test_low_and_high_child(self, wallet):
193193
assert med_fee_child["txid"] not in node.getrawmempool()
194194

195195
# 5. Send the high feerate (orphan) child
196-
high_child_wtxid_int = int(high_fee_child["tx"].wtxid_hex, 16)
196+
high_child_wtxid_int = high_fee_child["tx"].wtxid_int
197197
peer_sender.send_and_ping(msg_inv([CInv(t=MSG_WTX, h=high_child_wtxid_int)]))
198198
peer_sender.wait_for_getdata([high_child_wtxid_int])
199199
peer_sender.send_and_ping(msg_tx(high_fee_child["tx"]))
@@ -229,7 +229,7 @@ def test_orphan_consensus_failure(self):
229229
parent_sender = node.add_p2p_connection(P2PInterface())
230230

231231
# 1. Child is received first. It is missing an input.
232-
child_wtxid_int = int(tx_orphan_bad_wit.wtxid_hex, 16)
232+
child_wtxid_int = tx_orphan_bad_wit.wtxid_int
233233
bad_orphan_sender.send_and_ping(msg_inv([CInv(t=MSG_WTX, h=child_wtxid_int)]))
234234
bad_orphan_sender.wait_for_getdata([child_wtxid_int])
235235
bad_orphan_sender.send_and_ping(msg_tx(tx_orphan_bad_wit))
@@ -270,13 +270,13 @@ def test_parent_consensus_failure(self):
270270
fake_parent_sender = node.add_p2p_connection(P2PInterface())
271271

272272
# 1. Child is received first. It is missing an input.
273-
child_wtxid_int = int(high_fee_child["tx"].wtxid_hex, 16)
273+
child_wtxid_int = high_fee_child["tx"].wtxid_int
274274
package_sender.send_and_ping(msg_inv([CInv(t=MSG_WTX, h=child_wtxid_int)]))
275275
package_sender.wait_for_getdata([child_wtxid_int])
276276
package_sender.send_and_ping(msg_tx(high_fee_child["tx"]))
277277

278278
# 2. Node requests the missing parent by txid.
279-
parent_txid_int = int(tx_parent_bad_wit.txid_hex, 16)
279+
parent_txid_int = tx_parent_bad_wit.txid_int
280280
package_sender.wait_for_getdata([parent_txid_int])
281281

282282
# 3. A different node relays the parent. The parent is first evaluated by itself and
@@ -292,7 +292,7 @@ def test_parent_consensus_failure(self):
292292
self.log.info("Check that fake parent does not cause orphan to be deleted and real package can still be submitted")
293293
# 5. Child-sending should not have been punished and the orphan should remain in orphanage.
294294
# It can send the "real" parent transaction, and the package is accepted.
295-
parent_wtxid_int = int(low_fee_parent["tx"].wtxid_hex, 16)
295+
parent_wtxid_int = low_fee_parent["tx"].wtxid_int
296296
package_sender.send_and_ping(msg_inv([CInv(t=MSG_WTX, h=parent_wtxid_int)]))
297297
package_sender.wait_for_getdata([parent_wtxid_int])
298298
package_sender.send_and_ping(msg_tx(low_fee_parent["tx"]))

test/functional/p2p_orphan_handling.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ def create_parent_and_child(self):
132132

133133
def relay_transaction(self, peer, tx):
134134
"""Relay transaction using MSG_WTX"""
135-
wtxid = int(tx.wtxid_hex, 16)
135+
wtxid = tx.wtxid_int
136136
peer.send_and_ping(msg_inv([CInv(t=MSG_WTX, h=wtxid)]))
137137
self.nodes[0].bumpmocktime(TXREQUEST_TIME_SKIP)
138138
peer.wait_for_getdata([wtxid])
@@ -184,7 +184,7 @@ def test_arrival_timing_orphan(self):
184184

185185
# Spy peer should not be able to query the node for the parent yet, since it hasn't been
186186
# announced / insufficient time has elapsed.
187-
parent_inv = CInv(t=MSG_WTX, h=int(tx_parent_arrives["tx"].wtxid_hex, 16))
187+
parent_inv = CInv(t=MSG_WTX, h=tx_parent_arrives["tx"].wtxid_int)
188188
assert_equal(len(peer_spy.get_invs()), 0)
189189
peer_spy.assert_no_immediate_response(msg_getdata([parent_inv]))
190190

@@ -242,7 +242,7 @@ def test_orphan_rejected_parents_exceptions(self):
242242
# The parent should be requested because even though the txid commits to the fee, it doesn't
243243
# commit to the feerate. Delayed because it's by txid and this is not a preferred relay peer.
244244
self.nodes[0].bumpmocktime(NONPREF_PEER_TX_DELAY + TXID_RELAY_DELAY)
245-
peer2.wait_for_getdata([int(parent_low_fee["tx"].txid_hex, 16)])
245+
peer2.wait_for_getdata([parent_low_fee["tx"].txid_int])
246246

247247
self.log.info("Test orphan handling when a parent was previously downloaded with witness stripped")
248248
parent_normal = self.wallet.create_self_transfer()
@@ -262,7 +262,7 @@ def test_orphan_rejected_parents_exceptions(self):
262262
# The parent should be requested since the unstripped wtxid would differ. Delayed because
263263
# it's by txid and this is not a preferred relay peer.
264264
self.nodes[0].bumpmocktime(NONPREF_PEER_TX_DELAY + TXID_RELAY_DELAY)
265-
peer2.wait_for_getdata([int(parent_normal["tx"].txid_hex, 16)])
265+
peer2.wait_for_getdata([parent_normal["tx"].txid_int])
266266

267267
# parent_normal can be relayed again even though parent1_witness_stripped was rejected
268268
self.relay_transaction(peer1, parent_normal["tx"])
@@ -348,9 +348,9 @@ def test_orphans_overlapping_parents(self):
348348
assert_equal(inflight_parent_AB["txid"], inflight_parent_AB["wtxid"])
349349

350350
# Announce inflight_parent_AB and wait for getdata
351-
peer_txrequest.send_and_ping(msg_inv([CInv(t=MSG_WTX, h=int(inflight_parent_AB["tx"].wtxid_hex, 16))]))
351+
peer_txrequest.send_and_ping(msg_inv([CInv(t=MSG_WTX, h=inflight_parent_AB["tx"].wtxid_int)]))
352352
self.nodes[0].bumpmocktime(NONPREF_PEER_TX_DELAY)
353-
peer_txrequest.wait_for_getdata([int(inflight_parent_AB["tx"].wtxid_hex, 16)])
353+
peer_txrequest.wait_for_getdata([inflight_parent_AB["tx"].wtxid_int])
354354

355355
self.log.info("Test that the node does not request a parent if it has an in-flight txrequest")
356356
# Relay orphan child_A
@@ -661,15 +661,15 @@ def test_orphan_handling_prefer_outbound(self):
661661

662662
# The outbound peer should be preferred for getting orphan parents
663663
self.nodes[0].bumpmocktime(TXID_RELAY_DELAY)
664-
peer_outbound.wait_for_parent_requests([int(parent_tx.txid_hex, 16)])
664+
peer_outbound.wait_for_parent_requests([parent_tx.txid_int])
665665

666666
# There should be no request to the inbound peer
667-
peer_inbound.assert_never_requested(int(parent_tx.txid_hex, 16))
667+
peer_inbound.assert_never_requested(parent_tx.txid_int)
668668

669669
self.log.info("Test that, if the preferred peer doesn't respond, the node sends another request")
670670
self.nodes[0].bumpmocktime(GETDATA_TX_INTERVAL)
671671
peer_inbound.sync_with_ping()
672-
peer_inbound.wait_for_parent_requests([int(parent_tx.txid_hex, 16)])
672+
peer_inbound.wait_for_parent_requests([parent_tx.txid_int])
673673

674674
@cleanup
675675
def test_announcers_before_and_after(self):
@@ -701,7 +701,7 @@ def test_announcers_before_and_after(self):
701701

702702
# Peer disconnects before responding to request
703703
self.nodes[0].bumpmocktime(TXID_RELAY_DELAY)
704-
peer_early_disconnected.wait_for_parent_requests([int(parent_tx.txid_hex, 16)])
704+
peer_early_disconnected.wait_for_parent_requests([parent_tx.txid_int])
705705
peer_early_disconnected.peer_disconnect()
706706

707707
# The orphan should have 1 announcer left after the node finishes disconnecting peer_early_disconnected.
@@ -710,7 +710,7 @@ def test_announcers_before_and_after(self):
710710
# The node should retry with the other peer that announced the orphan earlier.
711711
# This node's request was additionally delayed because it's an inbound peer.
712712
self.nodes[0].bumpmocktime(NONPREF_PEER_TX_DELAY)
713-
peer_early_unresponsive.wait_for_parent_requests([int(parent_tx.txid_hex, 16)])
713+
peer_early_unresponsive.wait_for_parent_requests([parent_tx.txid_int])
714714

715715
self.log.info("Test that the node uses peers who announce the tx after realizing it's an orphan")
716716
peer_late_announcer.send_and_ping(msg_inv([orphan_inv]))
@@ -721,7 +721,7 @@ def test_announcers_before_and_after(self):
721721
assert_equal(len(orphanage[0]["from"]), 2)
722722

723723
self.nodes[0].bumpmocktime(GETDATA_TX_INTERVAL)
724-
peer_late_announcer.wait_for_parent_requests([int(parent_tx.txid_hex, 16)])
724+
peer_late_announcer.wait_for_parent_requests([parent_tx.txid_int])
725725

726726
@cleanup
727727
def test_parents_change(self):

0 commit comments

Comments
 (0)