Skip to content

Commit

Permalink
[tests] Make json request building more consistent in interface_rest.py
Browse files Browse the repository at this point in the history
  • Loading branch information
John Newbery authored and romanz committed Apr 3, 2018
1 parent 3fd4490 commit 7a3181a
Showing 1 changed file with 43 additions and 49 deletions.
92 changes: 43 additions & 49 deletions test/functional/interface_rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -75,7 +73,8 @@ def run_test(self):

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
Expand All @@ -86,8 +85,8 @@ def run_test(self):

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
Expand All @@ -99,8 +98,8 @@ def run_test(self):

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
Expand All @@ -114,8 +113,8 @@ def run_test(self):

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")
Expand All @@ -130,7 +129,7 @@ def run_test(self):
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)
Expand All @@ -147,7 +146,8 @@ def run_test(self):

# 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'])
Expand All @@ -158,64 +158,58 @@ def run_test(self):
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
Expand All @@ -224,40 +218,40 @@ def run_test(self):
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()
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/' + 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)
Expand All @@ -272,7 +266,7 @@ def run_test(self):
# 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)
Expand All @@ -281,12 +275,12 @@ def run_test(self):
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)

Expand All @@ -300,14 +294,14 @@ def run_test(self):
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)
Expand All @@ -319,14 +313,14 @@ def run_test(self):
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)
Expand Down

0 comments on commit 7a3181a

Please sign in to comment.