|
3 | 3 | # Distributed under the MIT software license, see the accompanying
|
4 | 4 | # file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
5 | 5 | """Test the REST API."""
|
| 6 | + |
| 7 | +import binascii |
6 | 8 | from decimal import Decimal
|
7 | 9 | from enum import Enum
|
8 | 10 | from io import BytesIO
|
9 | 11 | import json
|
10 |
| -from codecs import encode |
11 | 12 | from struct import pack, unpack
|
12 | 13 |
|
13 | 14 | import http.client
|
14 | 15 | import urllib.parse
|
15 | 16 |
|
16 |
| -from test_framework.messages import deser_uint256 |
17 | 17 | from test_framework.test_framework import BitcoinTestFramework
|
18 | 18 | from test_framework.util import (
|
19 | 19 | assert_equal,
|
20 | 20 | assert_greater_than,
|
| 21 | + assert_greater_than_or_equal, |
21 | 22 | hex_str_to_bytes,
|
22 | 23 | )
|
23 | 24 |
|
@@ -131,19 +132,15 @@ def run_test(self):
|
131 | 132 |
|
132 | 133 | self.log.info("Query the TXOs using the /getutxos URI with a binary response")
|
133 | 134 |
|
134 |
| - bb_hash = self.nodes[0].getbestblockhash() |
135 |
| - |
136 | 135 | bin_request = b'\x01\x02'
|
137 | 136 | for txid, n in [spending, spent]:
|
138 | 137 | bin_request += hex_str_to_bytes(txid)
|
139 | 138 | bin_request += pack("i", n)
|
140 | 139 |
|
141 | 140 | 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') |
147 | 144 |
|
148 | 145 | assert_equal(bb_hash, response_hash) # check if getutxo's chaintip during calculation was fine
|
149 | 146 | assert_equal(chain_height, 102) # chain height must be 102
|
@@ -199,30 +196,30 @@ def run_test(self):
|
199 | 196 | self.sync_all()
|
200 | 197 |
|
201 | 198 | self.log.info("Test the /block and /headers URIs")
|
| 199 | + bb_hash = self.nodes[0].getbestblockhash() |
202 | 200 |
|
203 | 201 | # Check binary format
|
204 | 202 | response = self.test_rest_request("/block/{}".format(bb_hash), req_type=ReqType.BIN, ret_type=RetType.OBJ)
|
205 | 203 | assert_greater_than(int(response.getheader('content-length')), 80)
|
206 |
| - response_str = response.read() |
| 204 | + response_bytes = response.read() |
207 | 205 |
|
208 | 206 | # Compare with block header
|
209 | 207 | response_header = self.test_rest_request("/headers/1/{}".format(bb_hash), req_type=ReqType.BIN, ret_type=RetType.OBJ)
|
210 | 208 | 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) |
213 | 211 |
|
214 | 212 | # Check block hex format
|
215 | 213 | response_hex = self.test_rest_request("/block/{}".format(bb_hash), req_type=ReqType.HEX, ret_type=RetType.OBJ)
|
216 | 214 | 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) |
219 | 217 |
|
220 | 218 | # Compare with hex block header
|
221 | 219 | response_header_hex = self.test_rest_request("/headers/1/{}".format(bb_hash), req_type=ReqType.HEX, ret_type=RetType.OBJ)
|
222 | 220 | 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) |
226 | 223 |
|
227 | 224 | # Check json format
|
228 | 225 | block_json_obj = self.test_rest_request("/block/{}".format(bb_hash))
|
@@ -252,7 +249,8 @@ def run_test(self):
|
252 | 249 |
|
253 | 250 | # Check hex format response
|
254 | 251 | 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) |
256 | 254 |
|
257 | 255 | self.log.info("Test tx inclusion in the /mempool and /block URIs")
|
258 | 256 |
|
|
0 commit comments