From abf190e4e7d79be6f4749dec24a3933e7a8a4507 Mon Sep 17 00:00:00 2001 From: John Newbery Date: Thu, 22 Mar 2018 12:36:37 -0400 Subject: [PATCH 1/6] [tests] fix flake8 warnings in interface_rest.py test --- test/functional/interface_rest.py | 241 ++++++++++++++---------------- 1 file changed, 114 insertions(+), 127 deletions(-) diff --git a/test/functional/interface_rest.py b/test/functional/interface_rest.py index 6f585f6825a..4f39dcd0245 100755 --- a/test/functional/interface_rest.py +++ b/test/functional/interface_rest.py @@ -3,25 +3,26 @@ # Distributed under the MIT software license, see the accompanying # file COPYING or http://www.opensource.org/licenses/mit-license.php. """Test the REST API.""" - -from test_framework.test_framework import BitcoinTestFramework -from test_framework.util import * -from struct import * +from decimal import Decimal from io import BytesIO +import json from codecs import encode +from struct import pack, unpack import http.client import urllib.parse -def deser_uint256(f): - r = 0 - for i in range(8): - t = unpack(b" Date: Thu, 22 Mar 2018 13:24:37 -0400 Subject: [PATCH 2/6] [tests] improve logging and documentation in interface_rest.py --- test/functional/interface_rest.py | 60 ++++++++++++++++--------------- 1 file changed, 31 insertions(+), 29 deletions(-) diff --git a/test/functional/interface_rest.py b/test/functional/interface_rest.py index 4f39dcd0245..7e0fe6080a2 100755 --- a/test/functional/interface_rest.py +++ b/test/functional/interface_rest.py @@ -55,7 +55,8 @@ class RESTTest (BitcoinTestFramework): def run_test(self): url = urllib.parse.urlparse(self.nodes[0].url) - self.log.info("Mining blocks...") + + self.log.info("Mine blocks and send Bitcoin to node 1") self.nodes[0].generate(1) self.sync_all() @@ -70,9 +71,10 @@ class RESTTest (BitcoinTestFramework): self.sync_all() bb_hash = self.nodes[0].getbestblockhash() - assert_equal(self.nodes[1].getbalance(), Decimal("0.1")) # balance now should be 0.1 on node 1 + assert_equal(self.nodes[1].getbalance(), Decimal("0.1")) + + self.log.info("Load the transaction using the /tx URI") - # load the latest 0.1 tx over the REST API json_string = http_get_call(url.hostname, url.port, '/rest/tx/' + txid + self.FORMAT_SEPARATOR + "json") json_obj = json.loads(json_string) vintx = json_obj['vin'][0]['txid'] # get the vin to later check for utxo (should be spent by then) @@ -82,9 +84,8 @@ class RESTTest (BitcoinTestFramework): if vout['value'] == 0.1: n = vout['n'] - ####################################### - # GETUTXOS: query an unspent outpoint # - ####################################### + self.log.info("Query an unspent TXO using the /getutxos URI") + json_request = '/' + txid + '-' + str(n) json_string = http_get_call(url.hostname, url.port, '/rest/getutxos' + json_request + self.FORMAT_SEPARATOR + 'json') json_obj = json.loads(json_string) @@ -96,9 +97,8 @@ class RESTTest (BitcoinTestFramework): assert_equal(len(json_obj['utxos']), 1) assert_equal(json_obj['utxos'][0]['value'], 0.1) - ################################################# - # GETUTXOS: now query an already spent outpoint # - ################################################# + self.log.info("Query a spent TXO using the /getutxos URI") + json_request = '/' + vintx + '-0' json_string = http_get_call(url.hostname, url.port, '/rest/getutxos' + json_request + self.FORMAT_SEPARATOR + 'json') json_obj = json.loads(json_string) @@ -112,16 +112,16 @@ class RESTTest (BitcoinTestFramework): # Check bitmap assert_equal(json_obj['bitmap'], "0") - ################################################## - # GETUTXOS: now check both with the same request # - ################################################## + self.log.info("Query two TXOs using the /getutxos URI") + json_request = '/' + txid + '-' + str(n) + '/' + vintx + '-0' json_string = http_get_call(url.hostname, url.port, '/rest/getutxos' + json_request + self.FORMAT_SEPARATOR + 'json') json_obj = json.loads(json_string) assert_equal(len(json_obj['utxos']), 1) assert_equal(json_obj['bitmap'], "10") - # Test binary response + self.log.info("Query the TXOs using the /getutxos URI with a binary response") + bb_hash = self.nodes[0].getbestblockhash() bin_request = b'\x01\x02' @@ -140,9 +140,10 @@ class RESTTest (BitcoinTestFramework): assert_equal(bb_hash, response_hash) # check if getutxo's chaintip during calculation was fine assert_equal(chain_height, 102) # chain height must be 102 - ############################ - # GETUTXOS: mempool checks # - ############################ + self.log.info("Test the /getutxos URI with and without /checkmempool") + # Create a transaction, check that it's found with /checkmempool, but + # not found without. Then confirm the transaction and check that it's + # found with or without /checkmempool. # do a tx and don't sync txid = self.nodes[0].sendtoaddress(self.nodes[1].getnewaddress(), 0.1) @@ -160,22 +161,22 @@ class RESTTest (BitcoinTestFramework): json_request = '/' + spending json_string = http_get_call(url.hostname, url.port, '/rest/getutxos' + json_request + self.FORMAT_SEPARATOR + 'json') json_obj = json.loads(json_string) - assert_equal(len(json_obj['utxos']), 0) # there should be no outpoint because it has just added to the mempool + assert_equal(len(json_obj['utxos']), 0) json_request = '/checkmempool/' + spending json_string = http_get_call(url.hostname, url.port, '/rest/getutxos' + json_request + self.FORMAT_SEPARATOR + 'json') json_obj = json.loads(json_string) - assert_equal(len(json_obj['utxos']), 1) # there should be an outpoint because it has just added to the mempool + assert_equal(len(json_obj['utxos']), 1) json_request = '/' + spent json_string = http_get_call(url.hostname, url.port, '/rest/getutxos' + json_request + self.FORMAT_SEPARATOR + 'json') json_obj = json.loads(json_string) - assert_equal(len(json_obj['utxos']), 1) # there should be an outpoint because its spending tx is not confirmed + assert_equal(len(json_obj['utxos']), 1) json_request = '/checkmempool/' + spent json_string = http_get_call(url.hostname, url.port, '/rest/getutxos' + json_request + self.FORMAT_SEPARATOR + 'json') json_obj = json.loads(json_string) - assert_equal(len(json_obj['utxos']), 0) # there should be no outpoint because it has just spent (by mempool tx) + assert_equal(len(json_obj['utxos']), 0) self.nodes[0].generate(1) self.sync_all() @@ -183,12 +184,12 @@ class RESTTest (BitcoinTestFramework): json_request = '/' + spending json_string = http_get_call(url.hostname, url.port, '/rest/getutxos' + json_request + self.FORMAT_SEPARATOR + 'json') json_obj = json.loads(json_string) - assert_equal(len(json_obj['utxos']), 1) # there should be an outpoint because it was mined + assert_equal(len(json_obj['utxos']), 1) json_request = '/checkmempool/' + spending json_string = http_get_call(url.hostname, url.port, '/rest/getutxos' + json_request + self.FORMAT_SEPARATOR + 'json') json_obj = json.loads(json_string) - assert_equal(len(json_obj['utxos']), 1) # there should be an outpoint because it was mined + assert_equal(len(json_obj['utxos']), 1) # Do some invalid requests json_request = '{"checkmempool' @@ -220,9 +221,7 @@ class RESTTest (BitcoinTestFramework): self.nodes[0].generate(1) # generate block to not affect upcoming tests self.sync_all() - ################ - # /rest/block/ # - ################ + self.log.info("Test the /block and /headers URIs") # Check binary format response = http_get_call(url.hostname, url.port, '/rest/block/' + bb_hash + self.FORMAT_SEPARATOR + "bin", True) @@ -279,7 +278,8 @@ class RESTTest (BitcoinTestFramework): json_obj = json.loads(response_header_json_str) assert_equal(len(json_obj), 5) # now we should have 5 header objects - # Do tx test + self.log.info("Test the /tx URI") + tx_hash = block_json_obj['tx'][0]['txid'] json_string = http_get_call(url.hostname, url.port, '/rest/tx/' + tx_hash + self.FORMAT_SEPARATOR + "json") json_obj = json.loads(json_string) @@ -290,8 +290,9 @@ class RESTTest (BitcoinTestFramework): assert_equal(hex_string.status, 200) assert_greater_than(int(response.getheader('content-length')), 10) - # Check block tx details - # Let's make 3 tx and mine them on node 1 + self.log.info("Test tx inclusion in the /mempool and /block URIs") + + # Make 3 tx and mine them on node 1 txs = [] txs.append(self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(), 11)) txs.append(self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(), 11)) @@ -330,7 +331,8 @@ class RESTTest (BitcoinTestFramework): for tx in txs: assert_equal(tx in json_obj['tx'], True) - # Test rest bestblock + self.log.info("Test the /chaininfo URI") + bb_hash = self.nodes[0].getbestblockhash() json_string = http_get_call(url.hostname, url.port, '/rest/chaininfo.json') From 7a3181a7676c822b2a369c745e0c45b0347c78e1 Mon Sep 17 00:00:00 2001 From: John Newbery Date: Thu, 22 Mar 2018 14:26:37 -0400 Subject: [PATCH 3/6] [tests] Make json request building more consistent in interface_rest.py --- test/functional/interface_rest.py | 92 +++++++++++++++---------------- 1 file changed, 43 insertions(+), 49 deletions(-) diff --git a/test/functional/interface_rest.py b/test/functional/interface_rest.py index 7e0fe6080a2..79c1fe1a890 100755 --- a/test/functional/interface_rest.py +++ b/test/functional/interface_rest.py @@ -42,8 +42,6 @@ def http_post_call(host, port, path, requestdata='', response_object=0): return conn.getresponse().read() class RESTTest (BitcoinTestFramework): - FORMAT_SEPARATOR = "." - def set_test_params(self): self.setup_clean_chain = True self.num_nodes = 3 @@ -75,7 +73,8 @@ class RESTTest (BitcoinTestFramework): self.log.info("Load the transaction using the /tx URI") - json_string = http_get_call(url.hostname, url.port, '/rest/tx/' + txid + self.FORMAT_SEPARATOR + "json") + json_request = "/rest/tx/{}.json".format(txid) + json_string = http_get_call(url.hostname, url.port, json_request) json_obj = json.loads(json_string) vintx = json_obj['vin'][0]['txid'] # get the vin to later check for utxo (should be spent by then) # get n of 0.1 outpoint @@ -86,8 +85,8 @@ class RESTTest (BitcoinTestFramework): self.log.info("Query an unspent TXO using the /getutxos URI") - json_request = '/' + txid + '-' + str(n) - json_string = http_get_call(url.hostname, url.port, '/rest/getutxos' + json_request + self.FORMAT_SEPARATOR + 'json') + json_request = "/rest/getutxos/{}-{}.json".format(txid, str(n)) + json_string = http_get_call(url.hostname, url.port, json_request) json_obj = json.loads(json_string) # Check chainTip response @@ -99,8 +98,8 @@ class RESTTest (BitcoinTestFramework): self.log.info("Query a spent TXO using the /getutxos URI") - json_request = '/' + vintx + '-0' - json_string = http_get_call(url.hostname, url.port, '/rest/getutxos' + json_request + self.FORMAT_SEPARATOR + 'json') + json_request = "/rest/getutxos/{}-0.json".format(vintx) + json_string = http_get_call(url.hostname, url.port, json_request) json_obj = json.loads(json_string) # Check chainTip response @@ -114,8 +113,8 @@ class RESTTest (BitcoinTestFramework): self.log.info("Query two TXOs using the /getutxos URI") - json_request = '/' + txid + '-' + str(n) + '/' + vintx + '-0' - json_string = http_get_call(url.hostname, url.port, '/rest/getutxos' + json_request + self.FORMAT_SEPARATOR + 'json') + json_request = "/rest/getutxos/{}-{}/{}-0.json".format(txid, str(n), vintx) + json_string = http_get_call(url.hostname, url.port, json_request) json_obj = json.loads(json_string) assert_equal(len(json_obj['utxos']), 1) assert_equal(json_obj['bitmap'], "10") @@ -130,7 +129,7 @@ class RESTTest (BitcoinTestFramework): bin_request += hex_str_to_bytes(vintx) bin_request += pack("i", 0) - bin_response = http_post_call(url.hostname, url.port, '/rest/getutxos' + self.FORMAT_SEPARATOR + 'bin', bin_request) + bin_response = http_post_call(url.hostname, url.port, '/rest/getutxos.bin', bin_request) output = BytesIO() output.write(bin_response) output.seek(0) @@ -147,7 +146,8 @@ class RESTTest (BitcoinTestFramework): # do a tx and don't sync txid = self.nodes[0].sendtoaddress(self.nodes[1].getnewaddress(), 0.1) - json_string = http_get_call(url.hostname, url.port, '/rest/tx/' + txid + self.FORMAT_SEPARATOR + "json") + json_request = "/rest/tx/{}.json".format(txid) + json_string = http_get_call(url.hostname, url.port, json_request) json_obj = json.loads(json_string) # get the spent output to later check for utxo (should be spent by then) spent = '{}-{}'.format(json_obj['vin'][0]['txid'], json_obj['vin'][0]['vout']) @@ -158,64 +158,58 @@ class RESTTest (BitcoinTestFramework): n = vout['n'] spending = '{}-{}'.format(txid, n) - json_request = '/' + spending - json_string = http_get_call(url.hostname, url.port, '/rest/getutxos' + json_request + self.FORMAT_SEPARATOR + 'json') + json_request = '/rest/getutxos/{}.json'.format(spending) + json_string = http_get_call(url.hostname, url.port, json_request) json_obj = json.loads(json_string) assert_equal(len(json_obj['utxos']), 0) - json_request = '/checkmempool/' + spending - json_string = http_get_call(url.hostname, url.port, '/rest/getutxos' + json_request + self.FORMAT_SEPARATOR + 'json') + json_request = '/rest/getutxos/checkmempool/{}.json'.format(spending) + json_string = http_get_call(url.hostname, url.port, json_request) json_obj = json.loads(json_string) assert_equal(len(json_obj['utxos']), 1) - json_request = '/' + spent - json_string = http_get_call(url.hostname, url.port, '/rest/getutxos' + json_request + self.FORMAT_SEPARATOR + 'json') + json_request = '/rest/getutxos/{}.json'.format(spent) + json_string = http_get_call(url.hostname, url.port, json_request) json_obj = json.loads(json_string) assert_equal(len(json_obj['utxos']), 1) - json_request = '/checkmempool/' + spent - json_string = http_get_call(url.hostname, url.port, '/rest/getutxos' + json_request + self.FORMAT_SEPARATOR + 'json') + json_request = '/rest/getutxos/checkmempool/{}.json'.format(spent) + json_string = http_get_call(url.hostname, url.port, json_request) json_obj = json.loads(json_string) assert_equal(len(json_obj['utxos']), 0) self.nodes[0].generate(1) self.sync_all() - json_request = '/' + spending - json_string = http_get_call(url.hostname, url.port, '/rest/getutxos' + json_request + self.FORMAT_SEPARATOR + 'json') + json_request = '/rest/getutxos/{}.json'.format(spending) + json_string = http_get_call(url.hostname, url.port, json_request) json_obj = json.loads(json_string) assert_equal(len(json_obj['utxos']), 1) - json_request = '/checkmempool/' + spending - json_string = http_get_call(url.hostname, url.port, '/rest/getutxos' + json_request + self.FORMAT_SEPARATOR + 'json') + json_request = '/rest/getutxos/checkmempool/{}.json'.format(spending) + json_string = http_get_call(url.hostname, url.port, json_request) json_obj = json.loads(json_string) assert_equal(len(json_obj['utxos']), 1) # Do some invalid requests json_request = '{"checkmempool' - response = http_post_call(url.hostname, url.port, '/rest/getutxos' + self.FORMAT_SEPARATOR + 'json', json_request, True) + response = http_post_call(url.hostname, url.port, '/rest/getutxos.json', json_request, True) assert_equal(response.status, 400) # must be a 400 because we send an invalid json request json_request = '{"checkmempool' - response = http_post_call(url.hostname, url.port, '/rest/getutxos' + self.FORMAT_SEPARATOR + 'bin', json_request, True) + response = http_post_call(url.hostname, url.port, '/rest/getutxos.bin', json_request, True) assert_equal(response.status, 400) # must be a 400 because we send an invalid bin request - response = http_post_call(url.hostname, url.port, '/rest/getutxos/checkmempool' + self.FORMAT_SEPARATOR + 'bin', '', True) + response = http_post_call(url.hostname, url.port, '/rest/getutxos/checkmempool.json', '', True) assert_equal(response.status, 400) # must be a 400 because we send an invalid bin request # Test limits - json_request = '/checkmempool/' - for x in range(0, 20): - json_request += txid + '-' + str(n) + '/' - json_request = json_request.rstrip("/") - response = http_post_call(url.hostname, url.port, '/rest/getutxos' + json_request + self.FORMAT_SEPARATOR + 'json', '', True) + json_request = '/rest/getutxos/checkmempool/' + '/'.join(["{}-{}".format(txid, n) for n in range(20)]) + '.json' + response = http_post_call(url.hostname, url.port, json_request, '', True) assert_equal(response.status, 400) # must be a 400 because we exceeding the limits - json_request = '/checkmempool/' - for x in range(0, 15): - json_request += txid + '-' + str(n) + '/' - json_request = json_request.rstrip("/") - response = http_post_call(url.hostname, url.port, '/rest/getutxos' + json_request + self.FORMAT_SEPARATOR + 'json', '', True) + json_request = '/rest/getutxos/checkmempool/' + '/'.join(['{}-{}'.format(txid, n) for n in range(15)]) + '.json' + response = http_post_call(url.hostname, url.port, json_request, '', True) assert_equal(response.status, 200) # must be a 200 because we are within the limits self.nodes[0].generate(1) # generate block to not affect upcoming tests @@ -224,27 +218,27 @@ class RESTTest (BitcoinTestFramework): self.log.info("Test the /block and /headers URIs") # Check binary format - response = http_get_call(url.hostname, url.port, '/rest/block/' + bb_hash + self.FORMAT_SEPARATOR + "bin", True) + response = http_get_call(url.hostname, url.port, '/rest/block/{}.bin'.format(bb_hash), True) assert_equal(response.status, 200) assert_greater_than(int(response.getheader('content-length')), 80) response_str = response.read() # Compare with block header - response_header = http_get_call(url.hostname, url.port, '/rest/headers/1/' + bb_hash + self.FORMAT_SEPARATOR + "bin", True) + response_header = http_get_call(url.hostname, url.port, '/rest/headers/1/{}.bin'.format(bb_hash), True) assert_equal(response_header.status, 200) assert_equal(int(response_header.getheader('content-length')), 80) response_header_str = response_header.read() assert_equal(response_str[0:80], response_header_str) # Check block hex format - response_hex = http_get_call(url.hostname, url.port, '/rest/block/' + bb_hash + self.FORMAT_SEPARATOR + "hex", True) + response_hex = http_get_call(url.hostname, url.port, '/rest/block/{}.hex'.format(bb_hash), True) assert_equal(response_hex.status, 200) assert_greater_than(int(response_hex.getheader('content-length')), 160) response_hex_str = response_hex.read() assert_equal(encode(response_str, "hex_codec")[0:160], response_hex_str[0:160]) # Compare with hex block header - response_header_hex = http_get_call(url.hostname, url.port, '/rest/headers/1/' + bb_hash + self.FORMAT_SEPARATOR + "hex", True) + response_header_hex = http_get_call(url.hostname, url.port, '/rest/headers/1/{}.hex'.format(bb_hash), True) assert_equal(response_header_hex.status, 200) assert_greater_than(int(response_header_hex.getheader('content-length')), 160) response_header_hex_str = response_header_hex.read() @@ -252,12 +246,12 @@ class RESTTest (BitcoinTestFramework): assert_equal(encode(response_header_str, "hex_codec")[0:160], response_header_hex_str[0:160]) # Check json format - block_json_string = http_get_call(url.hostname, url.port, '/rest/block/' + bb_hash + self.FORMAT_SEPARATOR + 'json') + block_json_string = http_get_call(url.hostname, url.port, '/rest/block/{}.json'.format(bb_hash)) block_json_obj = json.loads(block_json_string) assert_equal(block_json_obj['hash'], bb_hash) # Compare with json block header - response_header_json = http_get_call(url.hostname, url.port, '/rest/headers/1/' + bb_hash + self.FORMAT_SEPARATOR + "json", True) + response_header_json = http_get_call(url.hostname, url.port, '/rest/headers/1/{}.json'.format(bb_hash), True) assert_equal(response_header_json.status, 200) response_header_json_str = response_header_json.read().decode('utf-8') json_obj = json.loads(response_header_json_str, parse_float=Decimal) @@ -272,7 +266,7 @@ class RESTTest (BitcoinTestFramework): # See if we can get 5 headers in one response self.nodes[1].generate(5) self.sync_all() - response_header_json = http_get_call(url.hostname, url.port, '/rest/headers/5/' + bb_hash + self.FORMAT_SEPARATOR + "json", True) + response_header_json = http_get_call(url.hostname, url.port, '/rest/headers/5/{}.json'.format(bb_hash), True) assert_equal(response_header_json.status, 200) response_header_json_str = response_header_json.read().decode('utf-8') json_obj = json.loads(response_header_json_str) @@ -281,12 +275,12 @@ class RESTTest (BitcoinTestFramework): self.log.info("Test the /tx URI") tx_hash = block_json_obj['tx'][0]['txid'] - json_string = http_get_call(url.hostname, url.port, '/rest/tx/' + tx_hash + self.FORMAT_SEPARATOR + "json") + json_string = http_get_call(url.hostname, url.port, '/rest/tx/{}.json'.format(tx_hash)) json_obj = json.loads(json_string) assert_equal(json_obj['txid'], tx_hash) # Check hex format response - hex_string = http_get_call(url.hostname, url.port, '/rest/tx/' + tx_hash + self.FORMAT_SEPARATOR + "hex", True) + hex_string = http_get_call(url.hostname, url.port, '/rest/tx/{}.hex'.format(tx_hash), True) assert_equal(hex_string.status, 200) assert_greater_than(int(response.getheader('content-length')), 10) @@ -300,14 +294,14 @@ class RESTTest (BitcoinTestFramework): self.sync_all() # Check that there are exactly 3 transactions in the TX memory pool before generating the block - json_string = http_get_call(url.hostname, url.port, '/rest/mempool/info' + self.FORMAT_SEPARATOR + 'json') + json_string = http_get_call(url.hostname, url.port, '/rest/mempool/info.json') json_obj = json.loads(json_string) assert_equal(json_obj['size'], 3) # the size of the memory pool should be greater than 3x ~100 bytes assert_greater_than(json_obj['bytes'], 300) # Check that there are our submitted transactions in the TX memory pool - json_string = http_get_call(url.hostname, url.port, '/rest/mempool/contents' + self.FORMAT_SEPARATOR + 'json') + json_string = http_get_call(url.hostname, url.port, '/rest/mempool/contents.json') json_obj = json.loads(json_string) for i, tx in enumerate(txs): assert_equal(tx in json_obj, True) @@ -319,14 +313,14 @@ class RESTTest (BitcoinTestFramework): self.sync_all() # Check if the 3 tx show up in the new block - json_string = http_get_call(url.hostname, url.port, '/rest/block/' + newblockhash[0] + self.FORMAT_SEPARATOR + 'json') + json_string = http_get_call(url.hostname, url.port, '/rest/block/{}.json'.format(newblockhash[0])) json_obj = json.loads(json_string) for tx in json_obj['tx']: if 'coinbase' not in tx['vin'][0]: # exclude coinbase assert_equal(tx['txid'] in txs, True) # Check the same but without tx details - json_string = http_get_call(url.hostname, url.port, '/rest/block/notxdetails/' + newblockhash[0] + self.FORMAT_SEPARATOR + 'json') + json_string = http_get_call(url.hostname, url.port, '/rest/block/notxdetails/{}.json'.format(newblockhash[0])) json_obj = json.loads(json_string) for tx in txs: assert_equal(tx in json_obj['tx'], True) From ad00fbed3cfcfe7e921500179183589ac4aad419 Mon Sep 17 00:00:00 2001 From: John Newbery Date: Thu, 22 Mar 2018 16:37:09 -0400 Subject: [PATCH 4/6] [tests] refactor interface_rest.py to avoid code repetition Also refactor txout index parsing and formatting. --- test/functional/interface_rest.py | 215 +++++++++++++----------------- 1 file changed, 91 insertions(+), 124 deletions(-) diff --git a/test/functional/interface_rest.py b/test/functional/interface_rest.py index 79c1fe1a890..1af986a05e2 100755 --- a/test/functional/interface_rest.py +++ b/test/functional/interface_rest.py @@ -4,6 +4,7 @@ # file COPYING or http://www.opensource.org/licenses/mit-license.php. """Test the REST API.""" from decimal import Decimal +from enum import Enum from io import BytesIO import json from codecs import encode @@ -21,25 +22,20 @@ from test_framework.util import ( hex_str_to_bytes, ) -def http_get_call(host, port, path, response_object=0): - """Make a simple HTTP GET request.""" - conn = http.client.HTTPConnection(host, port) - conn.request('GET', path) +class ReqType(Enum): + JSON = 1 + BIN = 2 + HEX = 3 - if response_object: - return conn.getresponse() +class RetType(Enum): + OBJ = 1 + BYTES = 2 + JSON = 3 - return conn.getresponse().read().decode('utf-8') - -def http_post_call(host, port, path, requestdata='', response_object=0): - """Make a simple HTTP POST request with a request body.""" - conn = http.client.HTTPConnection(host, port) - conn.request('POST', path, requestdata) - - if response_object: - return conn.getresponse() - - return conn.getresponse().read() +def filter_output_indices_by_value(vouts, value): + for vout in vouts: + if vout['value'] == value: + yield vout['n'] class RESTTest (BitcoinTestFramework): def set_test_params(self): @@ -50,10 +46,35 @@ class RESTTest (BitcoinTestFramework): def setup_network(self, split=False): super().setup_network() connect_nodes_bi(self.nodes, 0, 2) + self.url = urllib.parse.urlparse(self.nodes[0].url) + + def test_rest_request(self, uri, http_method='GET', req_type=ReqType.JSON, body='', status=200, ret_type=RetType.JSON): + rest_uri = '/rest' + uri + if req_type == ReqType.JSON: + rest_uri += '.json' + elif req_type == ReqType.BIN: + rest_uri += '.bin' + elif req_type == ReqType.HEX: + rest_uri += '.hex' + + conn = http.client.HTTPConnection(self.url.hostname, self.url.port) + self.log.debug('%s %s %s', http_method, rest_uri, body) + if http_method == 'GET': + conn.request('GET', rest_uri) + elif http_method == 'POST': + conn.request('POST', rest_uri, body) + resp = conn.getresponse() + + assert_equal(resp.status, status) + + if ret_type == RetType.OBJ: + return resp + elif ret_type == RetType.BYTES: + return resp.read() + elif ret_type == RetType.JSON: + return json.loads(resp.read().decode('utf-8'), parse_float=Decimal) def run_test(self): - url = urllib.parse.urlparse(self.nodes[0].url) - self.log.info("Mine blocks and send Bitcoin to node 1") self.nodes[0].generate(1) @@ -73,39 +94,31 @@ class RESTTest (BitcoinTestFramework): self.log.info("Load the transaction using the /tx URI") - json_request = "/rest/tx/{}.json".format(txid) - json_string = http_get_call(url.hostname, url.port, json_request) - json_obj = json.loads(json_string) - vintx = json_obj['vin'][0]['txid'] # get the vin to later check for utxo (should be spent by then) + json_obj = self.test_rest_request("/tx/{}".format(txid)) + spent = (json_obj['vin'][0]['txid'], json_obj['vin'][0]['vout']) # get the vin to later check for utxo (should be spent by then) # get n of 0.1 outpoint - n = 0 - for vout in json_obj['vout']: - if vout['value'] == 0.1: - n = vout['n'] + n, = filter_output_indices_by_value(json_obj['vout'], Decimal('0.1')) + spending = (txid, n) self.log.info("Query an unspent TXO using the /getutxos URI") - json_request = "/rest/getutxos/{}-{}.json".format(txid, str(n)) - json_string = http_get_call(url.hostname, url.port, json_request) - json_obj = json.loads(json_string) + json_obj = self.test_rest_request("/getutxos/{}-{}".format(*spending)) # Check chainTip response assert_equal(json_obj['chaintipHash'], bb_hash) # Make sure there is one utxo assert_equal(len(json_obj['utxos']), 1) - assert_equal(json_obj['utxos'][0]['value'], 0.1) + assert_equal(json_obj['utxos'][0]['value'], Decimal('0.1')) self.log.info("Query a spent TXO using the /getutxos URI") - json_request = "/rest/getutxos/{}-0.json".format(vintx) - json_string = http_get_call(url.hostname, url.port, json_request) - json_obj = json.loads(json_string) + json_obj = self.test_rest_request("/getutxos/{}-{}".format(*spent)) # Check chainTip response assert_equal(json_obj['chaintipHash'], bb_hash) - # Make sure there is no utxo in the response because this oupoint has been spent + # Make sure there is no utxo in the response because this outpoint has been spent assert_equal(len(json_obj['utxos']), 0) # Check bitmap @@ -113,9 +126,8 @@ class RESTTest (BitcoinTestFramework): self.log.info("Query two TXOs using the /getutxos URI") - json_request = "/rest/getutxos/{}-{}/{}-0.json".format(txid, str(n), vintx) - json_string = http_get_call(url.hostname, url.port, json_request) - json_obj = json.loads(json_string) + json_obj = self.test_rest_request("/getutxos/{}-{}/{}-{}".format(*(spending + spent))) + assert_equal(len(json_obj['utxos']), 1) assert_equal(json_obj['bitmap'], "10") @@ -124,12 +136,11 @@ class RESTTest (BitcoinTestFramework): bb_hash = self.nodes[0].getbestblockhash() bin_request = b'\x01\x02' - bin_request += hex_str_to_bytes(txid) - bin_request += pack("i", n) - bin_request += hex_str_to_bytes(vintx) - bin_request += pack("i", 0) + for txid, n in [spending, spent]: + bin_request += hex_str_to_bytes(txid) + bin_request += pack("i", n) - bin_response = http_post_call(url.hostname, url.port, '/rest/getutxos.bin', bin_request) + bin_response = self.test_rest_request("/getutxos", http_method='POST', req_type=ReqType.BIN, body=bin_request, ret_type=RetType.BYTES) output = BytesIO() output.write(bin_response) output.seek(0) @@ -146,71 +157,45 @@ class RESTTest (BitcoinTestFramework): # do a tx and don't sync txid = self.nodes[0].sendtoaddress(self.nodes[1].getnewaddress(), 0.1) - json_request = "/rest/tx/{}.json".format(txid) - json_string = http_get_call(url.hostname, url.port, json_request) - json_obj = json.loads(json_string) + json_obj = self.test_rest_request("/tx/{}".format(txid)) # get the spent output to later check for utxo (should be spent by then) - spent = '{}-{}'.format(json_obj['vin'][0]['txid'], json_obj['vin'][0]['vout']) + spent = (json_obj['vin'][0]['txid'], json_obj['vin'][0]['vout']) # get n of 0.1 outpoint - n = 0 - for vout in json_obj['vout']: - if vout['value'] == 0.1: - n = vout['n'] - spending = '{}-{}'.format(txid, n) + n, = filter_output_indices_by_value(json_obj['vout'], Decimal('0.1')) + spending = (txid, n) - json_request = '/rest/getutxos/{}.json'.format(spending) - json_string = http_get_call(url.hostname, url.port, json_request) - json_obj = json.loads(json_string) + json_obj = self.test_rest_request("/getutxos/{}-{}".format(*spending)) assert_equal(len(json_obj['utxos']), 0) - json_request = '/rest/getutxos/checkmempool/{}.json'.format(spending) - json_string = http_get_call(url.hostname, url.port, json_request) - json_obj = json.loads(json_string) + json_obj = self.test_rest_request("/getutxos/checkmempool/{}-{}".format(*spending)) assert_equal(len(json_obj['utxos']), 1) - json_request = '/rest/getutxos/{}.json'.format(spent) - json_string = http_get_call(url.hostname, url.port, json_request) - json_obj = json.loads(json_string) + json_obj = self.test_rest_request("/getutxos/{}-{}".format(*spent)) assert_equal(len(json_obj['utxos']), 1) - json_request = '/rest/getutxos/checkmempool/{}.json'.format(spent) - json_string = http_get_call(url.hostname, url.port, json_request) - json_obj = json.loads(json_string) + json_obj = self.test_rest_request("/getutxos/checkmempool/{}-{}".format(*spent)) assert_equal(len(json_obj['utxos']), 0) self.nodes[0].generate(1) self.sync_all() - json_request = '/rest/getutxos/{}.json'.format(spending) - json_string = http_get_call(url.hostname, url.port, json_request) - json_obj = json.loads(json_string) + json_obj = self.test_rest_request("/getutxos/{}-{}".format(*spending)) assert_equal(len(json_obj['utxos']), 1) - json_request = '/rest/getutxos/checkmempool/{}.json'.format(spending) - json_string = http_get_call(url.hostname, url.port, json_request) - json_obj = json.loads(json_string) + json_obj = self.test_rest_request("/getutxos/checkmempool/{}-{}".format(*spending)) assert_equal(len(json_obj['utxos']), 1) # Do some invalid requests - json_request = '{"checkmempool' - response = http_post_call(url.hostname, url.port, '/rest/getutxos.json', json_request, True) - assert_equal(response.status, 400) # must be a 400 because we send an invalid json request - - json_request = '{"checkmempool' - response = http_post_call(url.hostname, url.port, '/rest/getutxos.bin', json_request, True) - assert_equal(response.status, 400) # must be a 400 because we send an invalid bin request - - response = http_post_call(url.hostname, url.port, '/rest/getutxos/checkmempool.json', '', True) - assert_equal(response.status, 400) # must be a 400 because we send an invalid bin request + self.test_rest_request("/getutxos", http_method='POST', req_type=ReqType.JSON, body='{"checkmempool', status=400, ret_type=RetType.OBJ) + self.test_rest_request("/getutxos", http_method='POST', req_type=ReqType.BIN, body='{"checkmempool', status=400, ret_type=RetType.OBJ) + self.test_rest_request("/getutxos/checkmempool", http_method='POST', req_type=ReqType.JSON, status=400, ret_type=RetType.OBJ) # Test limits - json_request = '/rest/getutxos/checkmempool/' + '/'.join(["{}-{}".format(txid, n) for n in range(20)]) + '.json' - response = http_post_call(url.hostname, url.port, json_request, '', True) - assert_equal(response.status, 400) # must be a 400 because we exceeding the limits + long_uri = '/'.join(["{}-{}".format(txid, n) for n in range(20)]) + self.test_rest_request("/getutxos/checkmempool/{}".format(long_uri), http_method='POST', status=400, ret_type=RetType.OBJ) - json_request = '/rest/getutxos/checkmempool/' + '/'.join(['{}-{}'.format(txid, n) for n in range(15)]) + '.json' - response = http_post_call(url.hostname, url.port, json_request, '', True) - assert_equal(response.status, 200) # must be a 200 because we are within the limits + long_uri = '/'.join(['{}-{}'.format(txid, n) for n in range(15)]) + self.test_rest_request("/getutxos/checkmempool/{}".format(long_uri), http_method='POST', status=200) self.nodes[0].generate(1) # generate block to not affect upcoming tests self.sync_all() @@ -218,43 +203,35 @@ class RESTTest (BitcoinTestFramework): self.log.info("Test the /block and /headers URIs") # Check binary format - response = http_get_call(url.hostname, url.port, '/rest/block/{}.bin'.format(bb_hash), True) - assert_equal(response.status, 200) + response = self.test_rest_request("/block/{}".format(bb_hash), req_type=ReqType.BIN, ret_type=RetType.OBJ) assert_greater_than(int(response.getheader('content-length')), 80) response_str = response.read() # Compare with block header - response_header = http_get_call(url.hostname, url.port, '/rest/headers/1/{}.bin'.format(bb_hash), True) - assert_equal(response_header.status, 200) + response_header = self.test_rest_request("/headers/1/{}".format(bb_hash), req_type=ReqType.BIN, ret_type=RetType.OBJ) assert_equal(int(response_header.getheader('content-length')), 80) response_header_str = response_header.read() assert_equal(response_str[0:80], response_header_str) # Check block hex format - response_hex = http_get_call(url.hostname, url.port, '/rest/block/{}.hex'.format(bb_hash), True) - assert_equal(response_hex.status, 200) + response_hex = self.test_rest_request("/block/{}".format(bb_hash), req_type=ReqType.HEX, ret_type=RetType.OBJ) assert_greater_than(int(response_hex.getheader('content-length')), 160) response_hex_str = response_hex.read() assert_equal(encode(response_str, "hex_codec")[0:160], response_hex_str[0:160]) # Compare with hex block header - response_header_hex = http_get_call(url.hostname, url.port, '/rest/headers/1/{}.hex'.format(bb_hash), True) - assert_equal(response_header_hex.status, 200) + response_header_hex = self.test_rest_request("/headers/1/{}".format(bb_hash), req_type=ReqType.HEX, ret_type=RetType.OBJ) assert_greater_than(int(response_header_hex.getheader('content-length')), 160) response_header_hex_str = response_header_hex.read() assert_equal(response_hex_str[0:160], response_header_hex_str[0:160]) assert_equal(encode(response_header_str, "hex_codec")[0:160], response_header_hex_str[0:160]) # Check json format - block_json_string = http_get_call(url.hostname, url.port, '/rest/block/{}.json'.format(bb_hash)) - block_json_obj = json.loads(block_json_string) + block_json_obj = self.test_rest_request("/block/{}".format(bb_hash)) assert_equal(block_json_obj['hash'], bb_hash) # Compare with json block header - response_header_json = http_get_call(url.hostname, url.port, '/rest/headers/1/{}.json'.format(bb_hash), True) - assert_equal(response_header_json.status, 200) - response_header_json_str = response_header_json.read().decode('utf-8') - json_obj = json.loads(response_header_json_str, parse_float=Decimal) + json_obj = self.test_rest_request("/headers/1/{}".format(bb_hash)) assert_equal(len(json_obj), 1) # ensure that there is one header in the json response assert_equal(json_obj[0]['hash'], bb_hash) # request/response hash should be the same @@ -266,23 +243,18 @@ class RESTTest (BitcoinTestFramework): # See if we can get 5 headers in one response self.nodes[1].generate(5) self.sync_all() - response_header_json = http_get_call(url.hostname, url.port, '/rest/headers/5/{}.json'.format(bb_hash), True) - assert_equal(response_header_json.status, 200) - response_header_json_str = response_header_json.read().decode('utf-8') - json_obj = json.loads(response_header_json_str) + json_obj = self.test_rest_request("/headers/5/{}".format(bb_hash)) assert_equal(len(json_obj), 5) # now we should have 5 header objects self.log.info("Test the /tx URI") tx_hash = block_json_obj['tx'][0]['txid'] - json_string = http_get_call(url.hostname, url.port, '/rest/tx/{}.json'.format(tx_hash)) - json_obj = json.loads(json_string) + json_obj = self.test_rest_request("/tx/{}".format(tx_hash)) assert_equal(json_obj['txid'], tx_hash) # Check hex format response - hex_string = http_get_call(url.hostname, url.port, '/rest/tx/{}.hex'.format(tx_hash), True) - assert_equal(hex_string.status, 200) - assert_greater_than(int(response.getheader('content-length')), 10) + hex_response = self.test_rest_request("/tx/{}".format(tx_hash), req_type=ReqType.HEX, ret_type=RetType.OBJ) + assert_greater_than(int(hex_response.getheader('content-length')), 10) self.log.info("Test tx inclusion in the /mempool and /block URIs") @@ -294,17 +266,15 @@ class RESTTest (BitcoinTestFramework): self.sync_all() # Check that there are exactly 3 transactions in the TX memory pool before generating the block - json_string = http_get_call(url.hostname, url.port, '/rest/mempool/info.json') - json_obj = json.loads(json_string) + json_obj = self.test_rest_request("/mempool/info") assert_equal(json_obj['size'], 3) # the size of the memory pool should be greater than 3x ~100 bytes assert_greater_than(json_obj['bytes'], 300) # Check that there are our submitted transactions in the TX memory pool - json_string = http_get_call(url.hostname, url.port, '/rest/mempool/contents.json') - json_obj = json.loads(json_string) + json_obj = self.test_rest_request("/mempool/contents") for i, tx in enumerate(txs): - assert_equal(tx in json_obj, True) + assert tx in json_obj assert_equal(json_obj[tx]['spentby'], txs[i + 1:i + 2]) assert_equal(json_obj[tx]['depends'], txs[i - 1:i]) @@ -313,24 +283,21 @@ class RESTTest (BitcoinTestFramework): self.sync_all() # Check if the 3 tx show up in the new block - json_string = http_get_call(url.hostname, url.port, '/rest/block/{}.json'.format(newblockhash[0])) - json_obj = json.loads(json_string) - for tx in json_obj['tx']: - if 'coinbase' not in tx['vin'][0]: # exclude coinbase - assert_equal(tx['txid'] in txs, True) + json_obj = self.test_rest_request("/block/{}".format(newblockhash[0])) + non_coinbase_txs = {tx['txid'] for tx in json_obj['tx'] + if 'coinbase' not in tx['vin'][0]} + assert_equal(non_coinbase_txs, set(txs)) # Check the same but without tx details - json_string = http_get_call(url.hostname, url.port, '/rest/block/notxdetails/{}.json'.format(newblockhash[0])) - json_obj = json.loads(json_string) + json_obj = self.test_rest_request("/block/notxdetails/{}".format(newblockhash[0])) for tx in txs: - assert_equal(tx in json_obj['tx'], True) + assert tx in json_obj['tx'] self.log.info("Test the /chaininfo URI") bb_hash = self.nodes[0].getbestblockhash() - json_string = http_get_call(url.hostname, url.port, '/rest/chaininfo.json') - json_obj = json.loads(json_string) + json_obj = self.test_rest_request("/chaininfo") assert_equal(json_obj['bestblockhash'], bb_hash) if __name__ == '__main__': From ade5964e3f8c5039878e1296311bb0337c152d2f Mon Sep 17 00:00:00 2001 From: John Newbery Date: Thu, 22 Mar 2018 16:41:22 -0400 Subject: [PATCH 5/6] [tests] only use 2 nodes in interface_rest.py --- test/functional/interface_rest.py | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/test/functional/interface_rest.py b/test/functional/interface_rest.py index 1af986a05e2..0a924e3149b 100755 --- a/test/functional/interface_rest.py +++ b/test/functional/interface_rest.py @@ -18,7 +18,6 @@ from test_framework.test_framework import BitcoinTestFramework from test_framework.util import ( assert_equal, assert_greater_than, - connect_nodes_bi, hex_str_to_bytes, ) @@ -40,13 +39,8 @@ def filter_output_indices_by_value(vouts, value): class RESTTest (BitcoinTestFramework): def set_test_params(self): self.setup_clean_chain = True - self.num_nodes = 3 - self.extra_args = [["-rest"]] * self.num_nodes - - def setup_network(self, split=False): - super().setup_network() - connect_nodes_bi(self.nodes, 0, 2) - self.url = urllib.parse.urlparse(self.nodes[0].url) + self.num_nodes = 2 + self.extra_args = [["-rest"], []] def test_rest_request(self, uri, http_method='GET', req_type=ReqType.JSON, body='', status=200, ret_type=RetType.JSON): rest_uri = '/rest' + uri @@ -75,18 +69,22 @@ class RESTTest (BitcoinTestFramework): return json.loads(resp.read().decode('utf-8'), parse_float=Decimal) def run_test(self): + self.url = urllib.parse.urlparse(self.nodes[0].url) self.log.info("Mine blocks and send Bitcoin to node 1") + # Random address so node1's balance doesn't increase + not_related_address = "2MxqoHEdNQTyYeX1mHcbrrpzgojbosTpCvJ" + self.nodes[0].generate(1) self.sync_all() - self.nodes[2].generate(100) + self.nodes[1].generatetoaddress(100, not_related_address) self.sync_all() assert_equal(self.nodes[0].getbalance(), 50) txid = self.nodes[0].sendtoaddress(self.nodes[1].getnewaddress(), 0.1) self.sync_all() - self.nodes[2].generate(1) + self.nodes[1].generatetoaddress(1, not_related_address) self.sync_all() bb_hash = self.nodes[0].getbestblockhash() @@ -260,9 +258,9 @@ class RESTTest (BitcoinTestFramework): # Make 3 tx and mine them on node 1 txs = [] - txs.append(self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(), 11)) - txs.append(self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(), 11)) - txs.append(self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(), 11)) + txs.append(self.nodes[0].sendtoaddress(not_related_address, 11)) + txs.append(self.nodes[0].sendtoaddress(not_related_address, 11)) + txs.append(self.nodes[0].sendtoaddress(not_related_address, 11)) self.sync_all() # Check that there are exactly 3 transactions in the TX memory pool before generating the block From 55efc1f62cef7e19efcb58ba68fbc2053bfa19a0 Mon Sep 17 00:00:00 2001 From: Roman Zeyde Date: Fri, 23 Mar 2018 12:39:54 +0300 Subject: [PATCH 6/6] [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. --- test/functional/interface_rest.py | 34 +++++++++++++++---------------- 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/test/functional/interface_rest.py b/test/functional/interface_rest.py index 0a924e3149b..2ee33aa8690 100755 --- a/test/functional/interface_rest.py +++ b/test/functional/interface_rest.py @@ -3,21 +3,22 @@ # Distributed under the MIT software license, see the accompanying # file COPYING or http://www.opensource.org/licenses/mit-license.php. """Test the REST API.""" + +import binascii from decimal import Decimal from enum import Enum from io import BytesIO import json -from codecs import encode from struct import pack, unpack import http.client import urllib.parse -from test_framework.messages import deser_uint256 from test_framework.test_framework import BitcoinTestFramework from test_framework.util import ( assert_equal, assert_greater_than, + assert_greater_than_or_equal, hex_str_to_bytes, ) @@ -131,19 +132,15 @@ class RESTTest (BitcoinTestFramework): self.log.info("Query the TXOs using the /getutxos URI with a binary response") - bb_hash = self.nodes[0].getbestblockhash() - bin_request = b'\x01\x02' for txid, n in [spending, spent]: bin_request += hex_str_to_bytes(txid) bin_request += pack("i", n) bin_response = self.test_rest_request("/getutxos", http_method='POST', req_type=ReqType.BIN, body=bin_request, ret_type=RetType.BYTES) - output = BytesIO() - output.write(bin_response) - output.seek(0) - chain_height = unpack("i", output.read(4))[0] - response_hash = hex(deser_uint256(output))[2:].zfill(64) + output = BytesIO(bin_response) + chain_height, = unpack("i", output.read(4)) + response_hash = binascii.hexlify(output.read(32)[::-1]).decode('ascii') assert_equal(bb_hash, response_hash) # check if getutxo's chaintip during calculation was fine assert_equal(chain_height, 102) # chain height must be 102 @@ -199,30 +196,30 @@ class RESTTest (BitcoinTestFramework): self.sync_all() self.log.info("Test the /block and /headers URIs") + bb_hash = self.nodes[0].getbestblockhash() # Check binary format response = self.test_rest_request("/block/{}".format(bb_hash), req_type=ReqType.BIN, ret_type=RetType.OBJ) assert_greater_than(int(response.getheader('content-length')), 80) - response_str = response.read() + response_bytes = response.read() # Compare with block header response_header = self.test_rest_request("/headers/1/{}".format(bb_hash), req_type=ReqType.BIN, ret_type=RetType.OBJ) assert_equal(int(response_header.getheader('content-length')), 80) - response_header_str = response_header.read() - assert_equal(response_str[0:80], response_header_str) + response_header_bytes = response_header.read() + assert_equal(response_bytes[:80], response_header_bytes) # Check block hex format response_hex = self.test_rest_request("/block/{}".format(bb_hash), req_type=ReqType.HEX, ret_type=RetType.OBJ) assert_greater_than(int(response_hex.getheader('content-length')), 160) - response_hex_str = response_hex.read() - assert_equal(encode(response_str, "hex_codec")[0:160], response_hex_str[0:160]) + response_hex_bytes = response_hex.read().strip(b'\n') + assert_equal(binascii.hexlify(response_bytes), response_hex_bytes) # Compare with hex block header response_header_hex = self.test_rest_request("/headers/1/{}".format(bb_hash), req_type=ReqType.HEX, ret_type=RetType.OBJ) assert_greater_than(int(response_header_hex.getheader('content-length')), 160) - response_header_hex_str = response_header_hex.read() - assert_equal(response_hex_str[0:160], response_header_hex_str[0:160]) - assert_equal(encode(response_header_str, "hex_codec")[0:160], response_header_hex_str[0:160]) + response_header_hex_bytes = response_header_hex.read(160) + assert_equal(binascii.hexlify(response_bytes[:80]), response_header_hex_bytes) # Check json format block_json_obj = self.test_rest_request("/block/{}".format(bb_hash)) @@ -252,7 +249,8 @@ class RESTTest (BitcoinTestFramework): # Check hex format response hex_response = self.test_rest_request("/tx/{}".format(tx_hash), req_type=ReqType.HEX, ret_type=RetType.OBJ) - assert_greater_than(int(hex_response.getheader('content-length')), 10) + assert_greater_than_or_equal(int(hex_response.getheader('content-length')), + json_obj['size']*2) self.log.info("Test tx inclusion in the /mempool and /block URIs")