Changeset View
Changeset View
Standalone View
Standalone View
test/functional/interface_rest.py
Show All 39 Lines | def http_post_call(host, port, path, requestdata='', response_object=0): | ||||
if response_object: | if response_object: | ||||
return conn.getresponse() | return conn.getresponse() | ||||
return conn.getresponse().read() | return conn.getresponse().read() | ||||
class RESTTest (BitcoinTestFramework): | class RESTTest (BitcoinTestFramework): | ||||
FORMAT_SEPARATOR = "." | |||||
def set_test_params(self): | def set_test_params(self): | ||||
self.setup_clean_chain = True | self.setup_clean_chain = True | ||||
self.num_nodes = 3 | self.num_nodes = 3 | ||||
self.extra_args = [["-rest"]] * self.num_nodes | self.extra_args = [["-rest"]] * self.num_nodes | ||||
def setup_network(self, split=False): | def setup_network(self, split=False): | ||||
super().setup_network() | super().setup_network() | ||||
connect_nodes_bi(self.nodes[0], self.nodes[2]) | connect_nodes_bi(self.nodes[0], self.nodes[2]) | ||||
Show All 15 Lines | def run_test(self): | ||||
self.nodes[2].generate(1) | self.nodes[2].generate(1) | ||||
self.sync_all() | self.sync_all() | ||||
bb_hash = self.nodes[0].getbestblockhash() | bb_hash = self.nodes[0].getbestblockhash() | ||||
assert_equal(self.nodes[1].getbalance(), Decimal("0.1")) | assert_equal(self.nodes[1].getbalance(), Decimal("0.1")) | ||||
self.log.info("Load the transaction using the /tx URI") | self.log.info("Load the transaction using the /tx URI") | ||||
json_string = http_get_call( | json_request = "/rest/tx/{}.json".format(txid) | ||||
url.hostname, url.port, '/rest/tx/' + txid + self.FORMAT_SEPARATOR + "json") | json_string = http_get_call(url.hostname, url.port, json_request) | ||||
json_obj = json.loads(json_string) | json_obj = json.loads(json_string) | ||||
# Get the vin to later check for utxo (should be spent by then) | # Get the vin to later check for utxo (should be spent by then) | ||||
vintx = json_obj['vin'][0]['txid'] | vintx = json_obj['vin'][0]['txid'] | ||||
# Get n of 0.1 outpoint | # Get n of 0.1 outpoint | ||||
n = 0 | n = 0 | ||||
for vout in json_obj['vout']: | for vout in json_obj['vout']: | ||||
if vout['value'] == 0.1: | if vout['value'] == 0.1: | ||||
n = vout['n'] | n = vout['n'] | ||||
self.log.info("Query an unspent TXO using the /getutxos URI") | self.log.info("Query an unspent TXO using the /getutxos URI") | ||||
json_request = '/' + txid + '-' + str(n) | json_request = "/rest/getutxos/{}-{}.json".format(txid, str(n)) | ||||
json_string = http_get_call( | json_string = http_get_call(url.hostname, url.port, json_request) | ||||
url.hostname, url.port, '/rest/getutxos' + json_request + self.FORMAT_SEPARATOR + 'json') | |||||
json_obj = json.loads(json_string) | json_obj = json.loads(json_string) | ||||
# Check chainTip response | # Check chainTip response | ||||
assert_equal(json_obj['chaintipHash'], bb_hash) | assert_equal(json_obj['chaintipHash'], bb_hash) | ||||
# Make sure there is one utxo | # Make sure there is one utxo | ||||
assert_equal(len(json_obj['utxos']), 1) | assert_equal(len(json_obj['utxos']), 1) | ||||
assert_equal(json_obj['utxos'][0]['value'], 0.1) | assert_equal(json_obj['utxos'][0]['value'], 0.1) | ||||
self.log.info("Query a spent TXO using the /getutxos URI") | self.log.info("Query a spent TXO using the /getutxos URI") | ||||
json_request = '/' + vintx + '-0' | json_request = "/rest/getutxos/{}-0.json".format(vintx) | ||||
json_string = http_get_call( | json_string = http_get_call(url.hostname, url.port, json_request) | ||||
url.hostname, url.port, '/rest/getutxos' + json_request + self.FORMAT_SEPARATOR + 'json') | |||||
json_obj = json.loads(json_string) | json_obj = json.loads(json_string) | ||||
# Check chainTip response | # Check chainTip response | ||||
assert_equal(json_obj['chaintipHash'], bb_hash) | assert_equal(json_obj['chaintipHash'], bb_hash) | ||||
# Make sure there is no utox in the response because this oupoint has | # Make sure there is no utox in the response because this oupoint has | ||||
# been spent | # been spent | ||||
assert_equal(len(json_obj['utxos']), 0) | assert_equal(len(json_obj['utxos']), 0) | ||||
# Check bitmap | # Check bitmap | ||||
assert_equal(json_obj['bitmap'], "0") | assert_equal(json_obj['bitmap'], "0") | ||||
self.log.info("Query two TXOs using the /getutxos URI") | self.log.info("Query two TXOs using the /getutxos URI") | ||||
json_request = '/' + txid + '-' + str(n) + '/' + vintx + '-0' | json_request = "/rest/getutxos/{}-{}/{}-0.json".format( | ||||
json_string = http_get_call( | txid, str(n), vintx) | ||||
url.hostname, url.port, '/rest/getutxos' + json_request + self.FORMAT_SEPARATOR + 'json') | json_string = http_get_call(url.hostname, url.port, json_request) | ||||
json_obj = json.loads(json_string) | json_obj = json.loads(json_string) | ||||
assert_equal(len(json_obj['utxos']), 1) | assert_equal(len(json_obj['utxos']), 1) | ||||
assert_equal(json_obj['bitmap'], "10") | assert_equal(json_obj['bitmap'], "10") | ||||
self.log.info( | self.log.info( | ||||
"Query the TXOs using the /getutxos URI with a binary response") | "Query the TXOs using the /getutxos URI with a binary response") | ||||
bb_hash = self.nodes[0].getbestblockhash() | bb_hash = self.nodes[0].getbestblockhash() | ||||
bin_request = b'\x01\x02' | bin_request = b'\x01\x02' | ||||
bin_request += hex_str_to_bytes(txid) | bin_request += hex_str_to_bytes(txid) | ||||
bin_request += pack("i", n) | bin_request += pack("i", n) | ||||
bin_request += hex_str_to_bytes(vintx) | bin_request += hex_str_to_bytes(vintx) | ||||
bin_request += pack("i", 0) | bin_request += pack("i", 0) | ||||
bin_response = http_post_call( | bin_response = http_post_call( | ||||
url.hostname, url.port, '/rest/getutxos' + self.FORMAT_SEPARATOR + 'bin', bin_request) | url.hostname, url.port, '/rest/getutxos.bin', bin_request) | ||||
output = BytesIO() | output = BytesIO() | ||||
output.write(bin_response) | output.write(bin_response) | ||||
output.seek(0) | output.seek(0) | ||||
chain_height = unpack("i", output.read(4))[0] | chain_height = unpack("i", output.read(4))[0] | ||||
response_hash = hex(deser_uint256(output))[2:].zfill(64) | response_hash = hex(deser_uint256(output))[2:].zfill(64) | ||||
# Check if getutxo's chaintip during calculation was fine | # Check if getutxo's chaintip during calculation was fine | ||||
assert_equal(bb_hash, response_hash) | assert_equal(bb_hash, response_hash) | ||||
# Chain height must be 102 | # Chain height must be 102 | ||||
assert_equal(chain_height, 102) | assert_equal(chain_height, 102) | ||||
self.log.info("Test the /getutxos URI with and without /checkmempool") | self.log.info("Test the /getutxos URI with and without /checkmempool") | ||||
# Create a transaction, check that it's found with /checkmempool, but | # Create a transaction, check that it's found with /checkmempool, but | ||||
# not found without. Then confirm the transaction and check that it's | # not found without. Then confirm the transaction and check that it's | ||||
# found with or without /checkmempool. | # found with or without /checkmempool. | ||||
# Do a tx and don't sync | # Do a tx and don't sync | ||||
txid = self.nodes[0].sendtoaddress(self.nodes[1].getnewaddress(), 0.1) | txid = self.nodes[0].sendtoaddress(self.nodes[1].getnewaddress(), 0.1) | ||||
json_string = http_get_call( | json_request = "/rest/tx/{}.json".format(txid) | ||||
url.hostname, url.port, '/rest/tx/' + txid + self.FORMAT_SEPARATOR + "json") | json_string = http_get_call(url.hostname, url.port, json_request) | ||||
json_obj = json.loads(json_string) | json_obj = json.loads(json_string) | ||||
# Get the spent output to later check for utxo (should be spent by then) | # Get the spent output to later check for utxo (should be spent by then) | ||||
spent = '{}-{}'.format(json_obj['vin'][0] | spent = '{}-{}'.format(json_obj['vin'][0] | ||||
['txid'], json_obj['vin'][0]['vout']) | ['txid'], json_obj['vin'][0]['vout']) | ||||
# Get n of 0.1 outpoint | # Get n of 0.1 outpoint | ||||
n = 0 | n = 0 | ||||
for vout in json_obj['vout']: | for vout in json_obj['vout']: | ||||
if vout['value'] == 0.1: | if vout['value'] == 0.1: | ||||
n = vout['n'] | n = vout['n'] | ||||
spending = '{}-{}'.format(txid, n) | spending = '{}-{}'.format(txid, n) | ||||
json_request = '/' + spending | json_request = '/rest/getutxos/{}.json'.format(spending) | ||||
json_string = http_get_call( | json_string = http_get_call(url.hostname, url.port, json_request) | ||||
url.hostname, url.port, '/rest/getutxos' + json_request + self.FORMAT_SEPARATOR + 'json') | |||||
json_obj = json.loads(json_string) | json_obj = json.loads(json_string) | ||||
assert_equal(len(json_obj['utxos']), 0) | assert_equal(len(json_obj['utxos']), 0) | ||||
json_request = '/checkmempool/' + spending | json_request = '/rest/getutxos/checkmempool/{}.json'.format(spending) | ||||
json_string = http_get_call( | json_string = http_get_call(url.hostname, url.port, json_request) | ||||
url.hostname, url.port, '/rest/getutxos' + json_request + self.FORMAT_SEPARATOR + 'json') | |||||
json_obj = json.loads(json_string) | json_obj = json.loads(json_string) | ||||
assert_equal(len(json_obj['utxos']), 1) | assert_equal(len(json_obj['utxos']), 1) | ||||
json_request = '/' + spent | json_request = '/rest/getutxos/{}.json'.format(spent) | ||||
json_string = http_get_call( | json_string = http_get_call(url.hostname, url.port, json_request) | ||||
url.hostname, url.port, '/rest/getutxos' + json_request + self.FORMAT_SEPARATOR + 'json') | |||||
json_obj = json.loads(json_string) | json_obj = json.loads(json_string) | ||||
assert_equal(len(json_obj['utxos']), 1) | assert_equal(len(json_obj['utxos']), 1) | ||||
json_request = '/checkmempool/' + spent | json_request = '/rest/getutxos/checkmempool/{}.json'.format(spent) | ||||
json_string = http_get_call( | json_string = http_get_call(url.hostname, url.port, json_request) | ||||
url.hostname, url.port, '/rest/getutxos' + json_request + self.FORMAT_SEPARATOR + 'json') | |||||
json_obj = json.loads(json_string) | json_obj = json.loads(json_string) | ||||
assert_equal(len(json_obj['utxos']), 0) | assert_equal(len(json_obj['utxos']), 0) | ||||
self.nodes[0].generate(1) | self.nodes[0].generate(1) | ||||
self.sync_all() | self.sync_all() | ||||
json_request = '/' + spending | json_request = '/rest/getutxos/{}.json'.format(spending) | ||||
json_string = http_get_call( | json_string = http_get_call(url.hostname, url.port, json_request) | ||||
url.hostname, url.port, '/rest/getutxos' + json_request + self.FORMAT_SEPARATOR + 'json') | |||||
json_obj = json.loads(json_string) | json_obj = json.loads(json_string) | ||||
assert_equal(len(json_obj['utxos']), 1) | assert_equal(len(json_obj['utxos']), 1) | ||||
json_request = '/checkmempool/' + spending | json_request = '/rest/getutxos/checkmempool/{}.json'.format(spending) | ||||
json_string = http_get_call( | json_string = http_get_call(url.hostname, url.port, json_request) | ||||
url.hostname, url.port, '/rest/getutxos' + json_request + self.FORMAT_SEPARATOR + 'json') | |||||
json_obj = json.loads(json_string) | json_obj = json.loads(json_string) | ||||
assert_equal(len(json_obj['utxos']), 1) | assert_equal(len(json_obj['utxos']), 1) | ||||
# Do some invalid requests | # Do some invalid requests | ||||
json_request = '{"checkmempool' | json_request = '{"checkmempool' | ||||
response = http_post_call( | response = http_post_call( | ||||
url.hostname, url.port, '/rest/getutxos' + self.FORMAT_SEPARATOR + 'json', json_request, True) | url.hostname, url.port, '/rest/getutxos.json', json_request, True) | ||||
# Must be a 400 because we send an invalid json request | # Must be a 400 because we send an invalid json request | ||||
assert_equal(response.status, 400) | assert_equal(response.status, 400) | ||||
json_request = '{"checkmempool' | json_request = '{"checkmempool' | ||||
response = http_post_call( | response = http_post_call( | ||||
url.hostname, url.port, '/rest/getutxos' + self.FORMAT_SEPARATOR + 'bin', json_request, True) | url.hostname, url.port, '/rest/getutxos.bin', json_request, True) | ||||
# Must be a 400 because we send an invalid bin request | # Must be a 400 because we send an invalid bin request | ||||
assert_equal(response.status, 400) | assert_equal(response.status, 400) | ||||
response = http_post_call( | response = http_post_call( | ||||
url.hostname, url.port, '/rest/getutxos/checkmempool' + self.FORMAT_SEPARATOR + 'bin', '', True) | url.hostname, url.port, '/rest/getutxos/checkmempool.json', '', True) | ||||
# Must be a 400 because we send an invalid bin request | # Must be a 400 because we send an invalid bin request | ||||
assert_equal(response.status, 400) | assert_equal(response.status, 400) | ||||
# Test limits | # Test limits | ||||
json_request = '/checkmempool/' | json_request = '/rest/getutxos/checkmempool/' + \ | ||||
for x in range(0, 20): | '/'.join(["{}-{}".format(txid, n) for n in range(20)]) + '.json' | ||||
json_request += txid + '-' + str(n) + '/' | |||||
json_request = json_request.rstrip("/") | |||||
response = http_post_call( | response = http_post_call( | ||||
url.hostname, url.port, '/rest/getutxos' + json_request + self.FORMAT_SEPARATOR + 'json', '', True) | url.hostname, url.port, json_request, '', True) | ||||
# Must be a 400 because we exceeding the limits | # Must be a 400 because we exceeding the limits | ||||
assert_equal(response.status, 400) | assert_equal(response.status, 400) | ||||
json_request = '/checkmempool/' | json_request = '/rest/getutxos/checkmempool/' + \ | ||||
for x in range(0, 15): | '/'.join(['{}-{}'.format(txid, n) for n in range(15)]) + '.json' | ||||
json_request += txid + '-' + str(n) + '/' | |||||
json_request = json_request.rstrip("/") | |||||
response = http_post_call( | response = http_post_call( | ||||
url.hostname, url.port, '/rest/getutxos' + json_request + self.FORMAT_SEPARATOR + 'json', '', True) | url.hostname, url.port, json_request, '', True) | ||||
# Must be a 200 because we are within the limits | # Must be a 200 because we are within the limits | ||||
assert_equal(response.status, 200) | assert_equal(response.status, 200) | ||||
# Generate block to not affect upcoming tests | # Generate block to not affect upcoming tests | ||||
self.nodes[0].generate( | self.nodes[0].generate( | ||||
1) | 1) | ||||
self.sync_all() | self.sync_all() | ||||
self.log.info("Test the /block and /headers URIs") | self.log.info("Test the /block and /headers URIs") | ||||
# Check binary format | # Check binary format | ||||
response = http_get_call( | response = http_get_call( | ||||
url.hostname, url.port, '/rest/block/' + bb_hash + self.FORMAT_SEPARATOR + "bin", True) | url.hostname, url.port, '/rest/block/{}.bin'.format(bb_hash), True) | ||||
assert_equal(response.status, 200) | assert_equal(response.status, 200) | ||||
assert_greater_than(int(response.getheader('content-length')), 80) | assert_greater_than(int(response.getheader('content-length')), 80) | ||||
response_str = response.read() | response_str = response.read() | ||||
# Compare with block header | # Compare with block header | ||||
response_header = http_get_call( | response_header = http_get_call( | ||||
url.hostname, url.port, '/rest/headers/1/' + bb_hash + self.FORMAT_SEPARATOR + "bin", True) | url.hostname, url.port, '/rest/headers/1/{}.bin'.format(bb_hash), True) | ||||
assert_equal(response_header.status, 200) | assert_equal(response_header.status, 200) | ||||
assert_equal(int(response_header.getheader('content-length')), 80) | assert_equal(int(response_header.getheader('content-length')), 80) | ||||
response_header_str = response_header.read() | response_header_str = response_header.read() | ||||
assert_equal(response_str[0:80], response_header_str) | assert_equal(response_str[0:80], response_header_str) | ||||
# Check block hex format | # Check block hex format | ||||
response_hex = http_get_call( | response_hex = http_get_call( | ||||
url.hostname, url.port, '/rest/block/' + bb_hash + self.FORMAT_SEPARATOR + "hex", True) | url.hostname, url.port, '/rest/block/{}.hex'.format(bb_hash), True) | ||||
assert_equal(response_hex.status, 200) | assert_equal(response_hex.status, 200) | ||||
assert_greater_than(int(response_hex.getheader('content-length')), 160) | assert_greater_than(int(response_hex.getheader('content-length')), 160) | ||||
response_hex_str = response_hex.read() | response_hex_str = response_hex.read() | ||||
assert_equal( | assert_equal( | ||||
encode(response_str, "hex_codec")[0:160], response_hex_str[0:160]) | encode(response_str, "hex_codec")[0:160], response_hex_str[0:160]) | ||||
# Compare with hex block header | # Compare with hex block header | ||||
response_header_hex = http_get_call( | response_header_hex = http_get_call( | ||||
url.hostname, url.port, '/rest/headers/1/' + bb_hash + self.FORMAT_SEPARATOR + "hex", True) | url.hostname, url.port, '/rest/headers/1/{}.hex'.format(bb_hash), True) | ||||
assert_equal(response_header_hex.status, 200) | assert_equal(response_header_hex.status, 200) | ||||
assert_greater_than( | assert_greater_than( | ||||
int(response_header_hex.getheader('content-length')), 160) | int(response_header_hex.getheader('content-length')), 160) | ||||
response_header_hex_str = response_header_hex.read() | response_header_hex_str = response_header_hex.read() | ||||
assert_equal(response_hex_str[0:160], response_header_hex_str[0:160]) | assert_equal(response_hex_str[0:160], response_header_hex_str[0:160]) | ||||
assert_equal(encode(response_header_str, "hex_codec")[ | assert_equal(encode(response_header_str, "hex_codec")[ | ||||
0:160], response_header_hex_str[0:160]) | 0:160], response_header_hex_str[0:160]) | ||||
# Check json format | # Check json format | ||||
block_json_string = http_get_call( | block_json_string = http_get_call( | ||||
url.hostname, url.port, '/rest/block/' + bb_hash + self.FORMAT_SEPARATOR + 'json') | url.hostname, url.port, '/rest/block/{}.json'.format(bb_hash)) | ||||
block_json_obj = json.loads(block_json_string) | block_json_obj = json.loads(block_json_string) | ||||
assert_equal(block_json_obj['hash'], bb_hash) | assert_equal(block_json_obj['hash'], bb_hash) | ||||
# Compare with json block header | # Compare with json block header | ||||
response_header_json = http_get_call( | response_header_json = http_get_call( | ||||
url.hostname, url.port, '/rest/headers/1/' + bb_hash + self.FORMAT_SEPARATOR + "json", True) | url.hostname, url.port, '/rest/headers/1/{}.json'.format(bb_hash), True) | ||||
assert_equal(response_header_json.status, 200) | assert_equal(response_header_json.status, 200) | ||||
response_header_json_str = response_header_json.read().decode('utf-8') | response_header_json_str = response_header_json.read().decode('utf-8') | ||||
json_obj = json.loads(response_header_json_str, parse_float=Decimal) | json_obj = json.loads(response_header_json_str, parse_float=Decimal) | ||||
# Ensure that there is one header in the json response | # Ensure that there is one header in the json response | ||||
assert_equal(len(json_obj), 1) | assert_equal(len(json_obj), 1) | ||||
# Request/response hash should be the same | # Request/response hash should be the same | ||||
assert_equal(json_obj[0]['hash'], bb_hash) | assert_equal(json_obj[0]['hash'], bb_hash) | ||||
# Compare with normal RPC block response | # Compare with normal RPC block response | ||||
rpc_block_json = self.nodes[0].getblock(bb_hash) | rpc_block_json = self.nodes[0].getblock(bb_hash) | ||||
for key in ['hash', 'confirmations', 'height', 'version', 'merkleroot', 'time', 'nonce', 'bits', 'difficulty', 'chainwork', 'previousblockhash']: | for key in ['hash', 'confirmations', 'height', 'version', 'merkleroot', 'time', 'nonce', 'bits', 'difficulty', 'chainwork', 'previousblockhash']: | ||||
assert_equal(json_obj[0][key], rpc_block_json[key]) | assert_equal(json_obj[0][key], rpc_block_json[key]) | ||||
# See if we can get 5 headers in one response | # See if we can get 5 headers in one response | ||||
self.nodes[1].generate(5) | self.nodes[1].generate(5) | ||||
self.sync_all() | self.sync_all() | ||||
response_header_json = http_get_call( | response_header_json = http_get_call( | ||||
url.hostname, url.port, '/rest/headers/5/' + bb_hash + self.FORMAT_SEPARATOR + "json", True) | url.hostname, url.port, '/rest/headers/5/{}.json'.format(bb_hash), True) | ||||
assert_equal(response_header_json.status, 200) | assert_equal(response_header_json.status, 200) | ||||
response_header_json_str = response_header_json.read().decode('utf-8') | response_header_json_str = response_header_json.read().decode('utf-8') | ||||
json_obj = json.loads(response_header_json_str) | json_obj = json.loads(response_header_json_str) | ||||
# Now we should have 5 header objects | # Now we should have 5 header objects | ||||
assert_equal(len(json_obj), 5) | assert_equal(len(json_obj), 5) | ||||
self.log.info("Test the /tx URI") | self.log.info("Test the /tx URI") | ||||
tx_hash = block_json_obj['tx'][0]['txid'] | tx_hash = block_json_obj['tx'][0]['txid'] | ||||
json_string = http_get_call( | json_string = http_get_call( | ||||
url.hostname, url.port, '/rest/tx/' + tx_hash + self.FORMAT_SEPARATOR + "json") | url.hostname, url.port, '/rest/tx/{}.json'.format(tx_hash)) | ||||
json_obj = json.loads(json_string) | json_obj = json.loads(json_string) | ||||
assert_equal(json_obj['txid'], tx_hash) | assert_equal(json_obj['txid'], tx_hash) | ||||
# Check hex format response | # Check hex format response | ||||
hex_string = http_get_call( | hex_string = http_get_call( | ||||
url.hostname, url.port, '/rest/tx/' + tx_hash + self.FORMAT_SEPARATOR + "hex", True) | url.hostname, url.port, '/rest/tx/{}.hex'.format(tx_hash), True) | ||||
assert_equal(hex_string.status, 200) | assert_equal(hex_string.status, 200) | ||||
assert_greater_than(int(response.getheader('content-length')), 10) | assert_greater_than(int(response.getheader('content-length')), 10) | ||||
self.log.info("Test tx inclusion in the /mempool and /block URIs") | self.log.info("Test tx inclusion in the /mempool and /block URIs") | ||||
# Make 3 tx and mine them on node 1 | # Make 3 tx and mine them on node 1 | ||||
txs = [] | txs = [] | ||||
txs.append(self.nodes[0].sendtoaddress( | txs.append(self.nodes[0].sendtoaddress( | ||||
self.nodes[2].getnewaddress(), 11)) | self.nodes[2].getnewaddress(), 11)) | ||||
txs.append(self.nodes[0].sendtoaddress( | txs.append(self.nodes[0].sendtoaddress( | ||||
self.nodes[2].getnewaddress(), 11)) | self.nodes[2].getnewaddress(), 11)) | ||||
txs.append(self.nodes[0].sendtoaddress( | txs.append(self.nodes[0].sendtoaddress( | ||||
self.nodes[2].getnewaddress(), 11)) | self.nodes[2].getnewaddress(), 11)) | ||||
self.sync_all() | self.sync_all() | ||||
# Check that there are exactly 3 transactions in the TX memory pool | # Check that there are exactly 3 transactions in the TX memory pool | ||||
# before generating the block | # before generating the block | ||||
json_string = http_get_call( | json_string = http_get_call( | ||||
url.hostname, url.port, '/rest/mempool/info' + self.FORMAT_SEPARATOR + 'json') | url.hostname, url.port, '/rest/mempool/info.json') | ||||
json_obj = json.loads(json_string) | json_obj = json.loads(json_string) | ||||
assert_equal(json_obj['size'], 3) | assert_equal(json_obj['size'], 3) | ||||
# The size of the memory pool should be greater than 3x ~100 bytes | # The size of the memory pool should be greater than 3x ~100 bytes | ||||
assert_greater_than(json_obj['bytes'], 300) | assert_greater_than(json_obj['bytes'], 300) | ||||
# Check that there are our submitted transactions in the TX memory pool | # Check that there are our submitted transactions in the TX memory pool | ||||
json_string = http_get_call( | json_string = http_get_call( | ||||
url.hostname, url.port, '/rest/mempool/contents' + self.FORMAT_SEPARATOR + 'json') | url.hostname, url.port, '/rest/mempool/contents.json') | ||||
json_obj = json.loads(json_string) | json_obj = json.loads(json_string) | ||||
for i, tx in enumerate(txs): | for i, tx in enumerate(txs): | ||||
assert_equal(tx in json_obj, True) | assert_equal(tx in json_obj, True) | ||||
assert_equal(json_obj[tx]['spentby'], txs[i + 1:i + 2]) | assert_equal(json_obj[tx]['spentby'], txs[i + 1:i + 2]) | ||||
assert_equal(json_obj[tx]['depends'], txs[i - 1:i]) | assert_equal(json_obj[tx]['depends'], txs[i - 1:i]) | ||||
# Now mine the transactions | # Now mine the transactions | ||||
newblockhash = self.nodes[1].generate(1) | newblockhash = self.nodes[1].generate(1) | ||||
self.sync_all() | self.sync_all() | ||||
# Check if the 3 tx show up in the new block | # Check if the 3 tx show up in the new block | ||||
json_string = http_get_call( | json_string = http_get_call( | ||||
url.hostname, url.port, '/rest/block/' + newblockhash[0] + self.FORMAT_SEPARATOR + 'json') | url.hostname, url.port, '/rest/block/{}.json'.format(newblockhash[0])) | ||||
json_obj = json.loads(json_string) | json_obj = json.loads(json_string) | ||||
for tx in json_obj['tx']: | for tx in json_obj['tx']: | ||||
if not 'coinbase' in tx['vin'][0]: # exclude coinbase | if not 'coinbase' in tx['vin'][0]: # exclude coinbase | ||||
assert_equal(tx['txid'] in txs, True) | assert_equal(tx['txid'] in txs, True) | ||||
# Check the same but without tx details | # Check the same but without tx details | ||||
json_string = http_get_call( | json_string = http_get_call( | ||||
url.hostname, url.port, '/rest/block/notxdetails/' + newblockhash[0] + self.FORMAT_SEPARATOR + 'json') | url.hostname, url.port, '/rest/block/notxdetails/{}.json'.format(newblockhash[0])) | ||||
json_obj = json.loads(json_string) | json_obj = json.loads(json_string) | ||||
for tx in txs: | for tx in txs: | ||||
assert_equal(tx in json_obj['tx'], True) | assert_equal(tx in json_obj['tx'], True) | ||||
self.log.info("Test the /chaininfo URI") | self.log.info("Test the /chaininfo URI") | ||||
bb_hash = self.nodes[0].getbestblockhash() | bb_hash = self.nodes[0].getbestblockhash() | ||||
json_string = http_get_call( | json_string = http_get_call( | ||||
url.hostname, url.port, '/rest/chaininfo.json') | url.hostname, url.port, '/rest/chaininfo.json') | ||||
json_obj = json.loads(json_string) | json_obj = json.loads(json_string) | ||||
assert_equal(json_obj['bestblockhash'], bb_hash) | assert_equal(json_obj['bestblockhash'], bb_hash) | ||||
if __name__ == '__main__': | if __name__ == '__main__': | ||||
RESTTest().main() | RESTTest().main() |