Skip to content

Commit 55efc1f

Browse files
committed
[tests] simplify binary and hex response parsing in interface_rest.py
We use assert_greater_than_or_equal(), since the hex response contains an extra b'\n' traling byte.
1 parent ade5964 commit 55efc1f

File tree

1 file changed

+16
-18
lines changed

1 file changed

+16
-18
lines changed

test/functional/interface_rest.py

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,22 @@
33
# Distributed under the MIT software license, see the accompanying
44
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
55
"""Test the REST API."""
6+
7+
import binascii
68
from decimal import Decimal
79
from enum import Enum
810
from io import BytesIO
911
import json
10-
from codecs import encode
1112
from struct import pack, unpack
1213

1314
import http.client
1415
import urllib.parse
1516

16-
from test_framework.messages import deser_uint256
1717
from test_framework.test_framework import BitcoinTestFramework
1818
from test_framework.util import (
1919
assert_equal,
2020
assert_greater_than,
21+
assert_greater_than_or_equal,
2122
hex_str_to_bytes,
2223
)
2324

@@ -131,19 +132,15 @@ def run_test(self):
131132

132133
self.log.info("Query the TXOs using the /getutxos URI with a binary response")
133134

134-
bb_hash = self.nodes[0].getbestblockhash()
135-
136135
bin_request = b'\x01\x02'
137136
for txid, n in [spending, spent]:
138137
bin_request += hex_str_to_bytes(txid)
139138
bin_request += pack("i", n)
140139

141140
bin_response = self.test_rest_request("/getutxos", http_method='POST', req_type=ReqType.BIN, body=bin_request, ret_type=RetType.BYTES)
142-
output = BytesIO()
143-
output.write(bin_response)
144-
output.seek(0)
145-
chain_height = unpack("i", output.read(4))[0]
146-
response_hash = hex(deser_uint256(output))[2:].zfill(64)
141+
output = BytesIO(bin_response)
142+
chain_height, = unpack("i", output.read(4))
143+
response_hash = binascii.hexlify(output.read(32)[::-1]).decode('ascii')
147144

148145
assert_equal(bb_hash, response_hash) # check if getutxo's chaintip during calculation was fine
149146
assert_equal(chain_height, 102) # chain height must be 102
@@ -199,30 +196,30 @@ def run_test(self):
199196
self.sync_all()
200197

201198
self.log.info("Test the /block and /headers URIs")
199+
bb_hash = self.nodes[0].getbestblockhash()
202200

203201
# Check binary format
204202
response = self.test_rest_request("/block/{}".format(bb_hash), req_type=ReqType.BIN, ret_type=RetType.OBJ)
205203
assert_greater_than(int(response.getheader('content-length')), 80)
206-
response_str = response.read()
204+
response_bytes = response.read()
207205

208206
# Compare with block header
209207
response_header = self.test_rest_request("/headers/1/{}".format(bb_hash), req_type=ReqType.BIN, ret_type=RetType.OBJ)
210208
assert_equal(int(response_header.getheader('content-length')), 80)
211-
response_header_str = response_header.read()
212-
assert_equal(response_str[0:80], response_header_str)
209+
response_header_bytes = response_header.read()
210+
assert_equal(response_bytes[:80], response_header_bytes)
213211

214212
# Check block hex format
215213
response_hex = self.test_rest_request("/block/{}".format(bb_hash), req_type=ReqType.HEX, ret_type=RetType.OBJ)
216214
assert_greater_than(int(response_hex.getheader('content-length')), 160)
217-
response_hex_str = response_hex.read()
218-
assert_equal(encode(response_str, "hex_codec")[0:160], response_hex_str[0:160])
215+
response_hex_bytes = response_hex.read().strip(b'\n')
216+
assert_equal(binascii.hexlify(response_bytes), response_hex_bytes)
219217

220218
# Compare with hex block header
221219
response_header_hex = self.test_rest_request("/headers/1/{}".format(bb_hash), req_type=ReqType.HEX, ret_type=RetType.OBJ)
222220
assert_greater_than(int(response_header_hex.getheader('content-length')), 160)
223-
response_header_hex_str = response_header_hex.read()
224-
assert_equal(response_hex_str[0:160], response_header_hex_str[0:160])
225-
assert_equal(encode(response_header_str, "hex_codec")[0:160], response_header_hex_str[0:160])
221+
response_header_hex_bytes = response_header_hex.read(160)
222+
assert_equal(binascii.hexlify(response_bytes[:80]), response_header_hex_bytes)
226223

227224
# Check json format
228225
block_json_obj = self.test_rest_request("/block/{}".format(bb_hash))
@@ -252,7 +249,8 @@ def run_test(self):
252249

253250
# Check hex format response
254251
hex_response = self.test_rest_request("/tx/{}".format(tx_hash), req_type=ReqType.HEX, ret_type=RetType.OBJ)
255-
assert_greater_than(int(hex_response.getheader('content-length')), 10)
252+
assert_greater_than_or_equal(int(hex_response.getheader('content-length')),
253+
json_obj['size']*2)
256254

257255
self.log.info("Test tx inclusion in the /mempool and /block URIs")
258256

0 commit comments

Comments
 (0)