Skip to content

Commit 49b87bf

Browse files
committed
Merge bitcoin/bitcoin#27389: test: refactor: replace unnecessary BytesIO uses
f842ed9 test: refactor: replace unnecessary `BytesIO` uses (Sebastian Falbesoner) Pull request description: Rather than needing to create intermediate stream variables, we can use helper functions like `tx_from_hex` instead or access the result directly, leading both to increased readability and less code. ACKs for top commit: stickies-v: ACK f842ed9 brunoerg: crACK f842ed9 aureleoules: ACK f842ed9 - It seems that these are the only instances that can be changed and it simplifies test code. Tree-SHA512: 7f4fd7a26720d1988bf27f66c817ff6cd7060350a3be62d28e7848c768fd43578719ca475193d4057ccf4f2458af18564fd513fe3a1d458a11c799927c12bd65
2 parents 369d4c0 + f842ed9 commit 49b87bf

File tree

3 files changed

+9
-15
lines changed

3 files changed

+9
-15
lines changed

test/functional/feature_taproot.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
CTxInWitness,
2020
CTxOut,
2121
SEQUENCE_FINAL,
22+
tx_from_hex,
2223
)
2324
from test_framework.script import (
2425
ANNEX_TAG,
@@ -109,7 +110,6 @@
109110
program_to_witness,
110111
)
111112
from collections import OrderedDict, namedtuple
112-
from io import BytesIO
113113
import json
114114
import hashlib
115115
import os
@@ -1386,8 +1386,7 @@ def test_spenders(self, node, spenders, input_counts):
13861386
# Add change
13871387
fund_tx.vout.append(CTxOut(balance - 10000, random.choice(host_spks)))
13881388
# Ask the wallet to sign
1389-
ss = BytesIO(bytes.fromhex(node.signrawtransactionwithwallet(fund_tx.serialize().hex())["hex"]))
1390-
fund_tx.deserialize(ss)
1389+
fund_tx = tx_from_hex(node.signrawtransactionwithwallet(fund_tx.serialize().hex())["hex"])
13911390
# Construct UTXOData entries
13921391
fund_tx.rehash()
13931392
for i in range(count_this_tx):

test/functional/interface_rest.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@
77
from decimal import Decimal
88
from enum import Enum
99
import http.client
10-
from io import BytesIO
1110
import json
12-
from struct import pack, unpack
1311
import typing
1412
import urllib.parse
1513

@@ -160,12 +158,11 @@ def run_test(self):
160158
bin_request = b'\x01\x02'
161159
for txid, n in [spending, spent]:
162160
bin_request += bytes.fromhex(txid)
163-
bin_request += pack("i", n)
161+
bin_request += n.to_bytes(4, 'little')
164162

165163
bin_response = self.test_rest_request("/getutxos", http_method='POST', req_type=ReqType.BIN, body=bin_request, ret_type=RetType.BYTES)
166-
output = BytesIO(bin_response)
167-
chain_height, = unpack("<i", output.read(4))
168-
response_hash = output.read(32)[::-1].hex()
164+
chain_height = int.from_bytes(bin_response[0:4], 'little')
165+
response_hash = bin_response[4:36][::-1].hex()
169166

170167
assert_equal(bb_hash, response_hash) # check if getutxo's chaintip during calculation was fine
171168
assert_equal(chain_height, 201) # chain height must be 201 (pre-mined chain [200] + generated block [1])

test/functional/interface_zmq.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
55
"""Test the ZMQ notification interface."""
66
import struct
7+
from time import sleep
78

89
from test_framework.address import (
910
ADDRESS_BCRT1_P2WSH_OP_TRUE,
@@ -16,8 +17,8 @@
1617
)
1718
from test_framework.test_framework import BitcoinTestFramework
1819
from test_framework.messages import (
19-
CTransaction,
2020
hash256,
21+
tx_from_hex,
2122
)
2223
from test_framework.util import (
2324
assert_equal,
@@ -28,8 +29,7 @@
2829
MiniWallet,
2930
)
3031
from test_framework.netutil import test_ipv6_local
31-
from io import BytesIO
32-
from time import sleep
32+
3333

3434
# Test may be skipped and not have zmq installed
3535
try:
@@ -198,9 +198,7 @@ def test_basic(self):
198198
txid = hashtx.receive()
199199

200200
# Should receive the coinbase raw transaction.
201-
hex = rawtx.receive()
202-
tx = CTransaction()
203-
tx.deserialize(BytesIO(hex))
201+
tx = tx_from_hex(rawtx.receive().hex())
204202
tx.calc_sha256()
205203
assert_equal(tx.hash, txid.hex())
206204

0 commit comments

Comments
 (0)