[tests] fix flake8 warnings in interface_rest.py test
This commit is contained in:
parent
18815b4bfb
commit
abf190e4e7
1 changed files with 114 additions and 127 deletions
|
@ -3,25 +3,26 @@
|
||||||
# Distributed under the MIT software license, see the accompanying
|
# Distributed under the MIT software license, see the accompanying
|
||||||
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||||
"""Test the REST API."""
|
"""Test the REST API."""
|
||||||
|
from decimal import Decimal
|
||||||
from test_framework.test_framework import BitcoinTestFramework
|
|
||||||
from test_framework.util import *
|
|
||||||
from struct import *
|
|
||||||
from io import BytesIO
|
from io import BytesIO
|
||||||
|
import json
|
||||||
from codecs import encode
|
from codecs import encode
|
||||||
|
from struct import pack, unpack
|
||||||
|
|
||||||
import http.client
|
import http.client
|
||||||
import urllib.parse
|
import urllib.parse
|
||||||
|
|
||||||
def deser_uint256(f):
|
from test_framework.messages import deser_uint256
|
||||||
r = 0
|
from test_framework.test_framework import BitcoinTestFramework
|
||||||
for i in range(8):
|
from test_framework.util import (
|
||||||
t = unpack(b"<I", f.read(4))[0]
|
assert_equal,
|
||||||
r += t << (i * 32)
|
assert_greater_than,
|
||||||
return r
|
connect_nodes_bi,
|
||||||
|
hex_str_to_bytes,
|
||||||
|
)
|
||||||
|
|
||||||
#allows simple http get calls
|
def http_get_call(host, port, path, response_object=0):
|
||||||
def http_get_call(host, port, path, response_object = 0):
|
"""Make a simple HTTP GET request."""
|
||||||
conn = http.client.HTTPConnection(host, port)
|
conn = http.client.HTTPConnection(host, port)
|
||||||
conn.request('GET', path)
|
conn.request('GET', path)
|
||||||
|
|
||||||
|
@ -30,8 +31,8 @@ def http_get_call(host, port, path, response_object = 0):
|
||||||
|
|
||||||
return conn.getresponse().read().decode('utf-8')
|
return conn.getresponse().read().decode('utf-8')
|
||||||
|
|
||||||
#allows simple http post calls with a request body
|
def http_post_call(host, port, path, requestdata='', response_object=0):
|
||||||
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 = http.client.HTTPConnection(host, port)
|
||||||
conn.request('POST', path, requestdata)
|
conn.request('POST', path, requestdata)
|
||||||
|
|
||||||
|
@ -69,79 +70,75 @@ class RESTTest (BitcoinTestFramework):
|
||||||
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")) #balance now should be 0.1 on node 1
|
assert_equal(self.nodes[1].getbalance(), Decimal("0.1")) # balance now should be 0.1 on node 1
|
||||||
|
|
||||||
# load the latest 0.1 tx over the REST API
|
# 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_string = http_get_call(url.hostname, url.port, '/rest/tx/' + txid + self.FORMAT_SEPARATOR + "json")
|
||||||
json_obj = json.loads(json_string)
|
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)
|
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
|
# 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']
|
||||||
|
|
||||||
|
|
||||||
#######################################
|
#######################################
|
||||||
# GETUTXOS: query an unspent outpoint #
|
# GETUTXOS: query an unspent outpoint #
|
||||||
#######################################
|
#######################################
|
||||||
json_request = '/'+txid+'-'+str(n)
|
json_request = '/' + txid + '-' + str(n)
|
||||||
json_string = http_get_call(url.hostname, url.port, '/rest/getutxos'+json_request+self.FORMAT_SEPARATOR+'json')
|
json_string = http_get_call(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)
|
||||||
|
|
||||||
|
|
||||||
#################################################
|
#################################################
|
||||||
# GETUTXOS: now query an already spent outpoint #
|
# GETUTXOS: now query an already spent outpoint #
|
||||||
#################################################
|
#################################################
|
||||||
json_request = '/'+vintx+'-0'
|
json_request = '/' + vintx + '-0'
|
||||||
json_string = http_get_call(url.hostname, url.port, '/rest/getutxos'+json_request+self.FORMAT_SEPARATOR+'json')
|
json_string = http_get_call(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 utxo in the response because this oupoint has been spent
|
# Make sure there is no utxo in the response because this oupoint has 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")
|
||||||
|
|
||||||
|
|
||||||
##################################################
|
##################################################
|
||||||
# GETUTXOS: now check both with the same request #
|
# GETUTXOS: now check both with the same request #
|
||||||
##################################################
|
##################################################
|
||||||
json_request = '/'+txid+'-'+str(n)+'/'+vintx+'-0'
|
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_string = http_get_call(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)
|
||||||
assert_equal(json_obj['bitmap'], "10")
|
assert_equal(json_obj['bitmap'], "10")
|
||||||
|
|
||||||
#test binary response
|
# Test binary response
|
||||||
bb_hash = self.nodes[0].getbestblockhash()
|
bb_hash = self.nodes[0].getbestblockhash()
|
||||||
|
|
||||||
binaryRequest = b'\x01\x02'
|
bin_request = b'\x01\x02'
|
||||||
binaryRequest += hex_str_to_bytes(txid)
|
bin_request += hex_str_to_bytes(txid)
|
||||||
binaryRequest += pack("i", n)
|
bin_request += pack("i", n)
|
||||||
binaryRequest += hex_str_to_bytes(vintx)
|
bin_request += hex_str_to_bytes(vintx)
|
||||||
binaryRequest += pack("i", 0)
|
bin_request += pack("i", 0)
|
||||||
|
|
||||||
bin_response = http_post_call(url.hostname, url.port, '/rest/getutxos'+self.FORMAT_SEPARATOR+'bin', binaryRequest)
|
bin_response = http_post_call(url.hostname, url.port, '/rest/getutxos' + self.FORMAT_SEPARATOR + 'bin', bin_request)
|
||||||
output = BytesIO()
|
output = BytesIO()
|
||||||
output.write(bin_response)
|
output.write(bin_response)
|
||||||
output.seek(0)
|
output.seek(0)
|
||||||
chainHeight = unpack("i", output.read(4))[0]
|
chain_height = unpack("i", output.read(4))[0]
|
||||||
hashFromBinResponse = hex(deser_uint256(output))[2:].zfill(64)
|
response_hash = hex(deser_uint256(output))[2:].zfill(64)
|
||||||
|
|
||||||
assert_equal(bb_hash, hashFromBinResponse) #check if getutxo's chaintip during calculation was fine
|
|
||||||
assert_equal(chainHeight, 102) #chain height must be 102
|
|
||||||
|
|
||||||
|
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 #
|
# GETUTXOS: mempool checks #
|
||||||
|
@ -149,7 +146,7 @@ class RESTTest (BitcoinTestFramework):
|
||||||
|
|
||||||
# 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(url.hostname, url.port, '/rest/tx/'+txid+self.FORMAT_SEPARATOR+"json")
|
json_string = http_get_call(url.hostname, url.port, '/rest/tx/' + txid + self.FORMAT_SEPARATOR + "json")
|
||||||
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]['txid'], json_obj['vin'][0]['vout'])
|
spent = '{}-{}'.format(json_obj['vin'][0]['txid'], json_obj['vin'][0]['vout'])
|
||||||
|
@ -160,190 +157,180 @@ class RESTTest (BitcoinTestFramework):
|
||||||
n = vout['n']
|
n = vout['n']
|
||||||
spending = '{}-{}'.format(txid, n)
|
spending = '{}-{}'.format(txid, n)
|
||||||
|
|
||||||
json_request = '/'+spending
|
json_request = '/' + spending
|
||||||
json_string = http_get_call(url.hostname, url.port, '/rest/getutxos'+json_request+self.FORMAT_SEPARATOR+'json')
|
json_string = http_get_call(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) #there should be no outpoint because it has just added to the mempool
|
assert_equal(len(json_obj['utxos']), 0) # there should be no outpoint because it has just added to the mempool
|
||||||
|
|
||||||
json_request = '/checkmempool/'+spending
|
json_request = '/checkmempool/' + spending
|
||||||
json_string = http_get_call(url.hostname, url.port, '/rest/getutxos'+json_request+self.FORMAT_SEPARATOR+'json')
|
json_string = http_get_call(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) #there should be an outpoint because it has just added to the mempool
|
assert_equal(len(json_obj['utxos']), 1) # there should be an outpoint because it has just added to the mempool
|
||||||
|
|
||||||
json_request = '/'+spent
|
json_request = '/' + spent
|
||||||
json_string = http_get_call(url.hostname, url.port, '/rest/getutxos'+json_request+self.FORMAT_SEPARATOR+'json')
|
json_string = http_get_call(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) #there should be an outpoint because its spending tx is not confirmed
|
assert_equal(len(json_obj['utxos']), 1) # there should be an outpoint because its spending tx is not confirmed
|
||||||
|
|
||||||
json_request = '/checkmempool/'+spent
|
json_request = '/checkmempool/' + spent
|
||||||
json_string = http_get_call(url.hostname, url.port, '/rest/getutxos'+json_request+self.FORMAT_SEPARATOR+'json')
|
json_string = http_get_call(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) #there should be no outpoint because it has just spent (by mempool tx)
|
assert_equal(len(json_obj['utxos']), 0) # there should be no outpoint because it has just spent (by mempool tx)
|
||||||
|
|
||||||
self.nodes[0].generate(1)
|
self.nodes[0].generate(1)
|
||||||
self.sync_all()
|
self.sync_all()
|
||||||
|
|
||||||
json_request = '/'+spending
|
json_request = '/' + spending
|
||||||
json_string = http_get_call(url.hostname, url.port, '/rest/getutxos'+json_request+self.FORMAT_SEPARATOR+'json')
|
json_string = http_get_call(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) #there should be an outpoint because it was mined
|
assert_equal(len(json_obj['utxos']), 1) # there should be an outpoint because it was mined
|
||||||
|
|
||||||
json_request = '/checkmempool/'+spending
|
json_request = '/checkmempool/' + spending
|
||||||
json_string = http_get_call(url.hostname, url.port, '/rest/getutxos'+json_request+self.FORMAT_SEPARATOR+'json')
|
json_string = http_get_call(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) #there should be an outpoint because it was mined
|
assert_equal(len(json_obj['utxos']), 1) # there should be an outpoint because it was mined
|
||||||
|
|
||||||
#do some invalid requests
|
# Do some invalid requests
|
||||||
json_request = '{"checkmempool'
|
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' + self.FORMAT_SEPARATOR + 'json', json_request, True)
|
||||||
assert_equal(response.status, 400) #must be a 400 because we send an invalid json request
|
assert_equal(response.status, 400) # must be a 400 because we send an invalid json request
|
||||||
|
|
||||||
json_request = '{"checkmempool'
|
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' + self.FORMAT_SEPARATOR + 'bin', json_request, True)
|
||||||
assert_equal(response.status, 400) #must be a 400 because we send an invalid bin request
|
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' + self.FORMAT_SEPARATOR + 'bin', '', True)
|
||||||
assert_equal(response.status, 400) #must be a 400 because we send an invalid bin request
|
assert_equal(response.status, 400) # must be a 400 because we send an invalid bin request
|
||||||
|
|
||||||
#test limits
|
# Test limits
|
||||||
json_request = '/checkmempool/'
|
json_request = '/checkmempool/'
|
||||||
for x in range(0, 20):
|
for x in range(0, 20):
|
||||||
json_request += txid+'-'+str(n)+'/'
|
json_request += txid + '-' + str(n) + '/'
|
||||||
json_request = json_request.rstrip("/")
|
json_request = json_request.rstrip("/")
|
||||||
response = http_post_call(url.hostname, url.port, '/rest/getutxos'+json_request+self.FORMAT_SEPARATOR+'json', '', True)
|
response = http_post_call(url.hostname, url.port, '/rest/getutxos' + json_request + self.FORMAT_SEPARATOR + 'json', '', True)
|
||||||
assert_equal(response.status, 400) #must be a 400 because we exceeding the limits
|
assert_equal(response.status, 400) # must be a 400 because we exceeding the limits
|
||||||
|
|
||||||
json_request = '/checkmempool/'
|
json_request = '/checkmempool/'
|
||||||
for x in range(0, 15):
|
for x in range(0, 15):
|
||||||
json_request += txid+'-'+str(n)+'/'
|
json_request += txid + '-' + str(n) + '/'
|
||||||
json_request = json_request.rstrip("/")
|
json_request = json_request.rstrip("/")
|
||||||
response = http_post_call(url.hostname, url.port, '/rest/getutxos'+json_request+self.FORMAT_SEPARATOR+'json', '', True)
|
response = http_post_call(url.hostname, url.port, '/rest/getutxos' + json_request + self.FORMAT_SEPARATOR + 'json', '', True)
|
||||||
assert_equal(response.status, 200) #must be a 200 because we are within the limits
|
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
|
self.nodes[0].generate(1) # generate block to not affect upcoming tests
|
||||||
self.sync_all()
|
self.sync_all()
|
||||||
|
|
||||||
################
|
################
|
||||||
# /rest/block/ #
|
# /rest/block/ #
|
||||||
################
|
################
|
||||||
|
|
||||||
# check binary format
|
# 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/' + bb_hash + self.FORMAT_SEPARATOR + "bin", 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(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/' + bb_hash + self.FORMAT_SEPARATOR + "bin", 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(url.hostname, url.port, '/rest/block/'+bb_hash+self.FORMAT_SEPARATOR+"hex", True)
|
response_hex = http_get_call(url.hostname, url.port, '/rest/block/' + bb_hash + self.FORMAT_SEPARATOR + "hex", 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(encode(response_str, "hex_codec")[0:160], response_hex_str[0:160])
|
assert_equal(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(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/' + bb_hash + self.FORMAT_SEPARATOR + "hex", True)
|
||||||
assert_equal(response_header_hex.status, 200)
|
assert_equal(response_header_hex.status, 200)
|
||||||
assert_greater_than(int(response_header_hex.getheader('content-length')), 160)
|
assert_greater_than(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")[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
|
# 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/' + bb_hash + self.FORMAT_SEPARATOR + 'json')
|
||||||
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(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/' + bb_hash + self.FORMAT_SEPARATOR + "json", 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)
|
||||||
assert_equal(len(json_obj), 1) #ensure that there is one header in the json response
|
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
|
assert_equal(json_obj[0]['hash'], bb_hash) # request/response hash should be the same
|
||||||
|
|
||||||
#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)
|
||||||
assert_equal(json_obj[0]['hash'], rpc_block_json['hash'])
|
for key in ['hash', 'confirmations', 'height', 'version', 'merkleroot', 'time', 'nonce', 'bits', 'difficulty', 'chainwork', 'previousblockhash']:
|
||||||
assert_equal(json_obj[0]['confirmations'], rpc_block_json['confirmations'])
|
assert_equal(json_obj[0][key], rpc_block_json[key])
|
||||||
assert_equal(json_obj[0]['height'], rpc_block_json['height'])
|
|
||||||
assert_equal(json_obj[0]['version'], rpc_block_json['version'])
|
|
||||||
assert_equal(json_obj[0]['merkleroot'], rpc_block_json['merkleroot'])
|
|
||||||
assert_equal(json_obj[0]['time'], rpc_block_json['time'])
|
|
||||||
assert_equal(json_obj[0]['nonce'], rpc_block_json['nonce'])
|
|
||||||
assert_equal(json_obj[0]['bits'], rpc_block_json['bits'])
|
|
||||||
assert_equal(json_obj[0]['difficulty'], rpc_block_json['difficulty'])
|
|
||||||
assert_equal(json_obj[0]['chainwork'], rpc_block_json['chainwork'])
|
|
||||||
assert_equal(json_obj[0]['previousblockhash'], rpc_block_json['previousblockhash'])
|
|
||||||
|
|
||||||
#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(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/' + bb_hash + self.FORMAT_SEPARATOR + "json", 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)
|
||||||
assert_equal(len(json_obj), 5) #now we should have 5 header objects
|
assert_equal(len(json_obj), 5) # now we should have 5 header objects
|
||||||
|
|
||||||
# do tx test
|
# Do tx test
|
||||||
tx_hash = block_json_obj['tx'][0]['txid']
|
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/' + tx_hash + self.FORMAT_SEPARATOR + "json")
|
||||||
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(url.hostname, url.port, '/rest/tx/'+tx_hash+self.FORMAT_SEPARATOR+"hex", True)
|
hex_string = http_get_call(url.hostname, url.port, '/rest/tx/' + tx_hash + self.FORMAT_SEPARATOR + "hex", 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)
|
||||||
|
|
||||||
|
# Check block tx details
|
||||||
# check block tx details
|
# Let's make 3 tx and mine them on node 1
|
||||||
# let's make 3 tx and mine them on node 1
|
|
||||||
txs = []
|
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(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))
|
||||||
self.sync_all()
|
self.sync_all()
|
||||||
|
|
||||||
# check that there are exactly 3 transactions in the TX memory pool before generating the block
|
# 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' + self.FORMAT_SEPARATOR + '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(url.hostname, url.port, '/rest/mempool/contents'+self.FORMAT_SEPARATOR+'json')
|
json_string = http_get_call(url.hostname, url.port, '/rest/mempool/contents' + self.FORMAT_SEPARATOR + '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(url.hostname, url.port, '/rest/block/'+newblockhash[0]+self.FORMAT_SEPARATOR+'json')
|
json_string = http_get_call(url.hostname, url.port, '/rest/block/' + newblockhash[0] + self.FORMAT_SEPARATOR + 'json')
|
||||||
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 'coinbase' not 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(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/' + newblockhash[0] + self.FORMAT_SEPARATOR + 'json')
|
||||||
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)
|
||||||
|
|
||||||
#test rest bestblock
|
# Test rest bestblock
|
||||||
bb_hash = self.nodes[0].getbestblockhash()
|
bb_hash = self.nodes[0].getbestblockhash()
|
||||||
|
|
||||||
json_string = http_get_call(url.hostname, url.port, '/rest/chaininfo.json')
|
json_string = http_get_call(url.hostname, url.port, '/rest/chaininfo.json')
|
||||||
|
@ -351,4 +338,4 @@ class RESTTest (BitcoinTestFramework):
|
||||||
assert_equal(json_obj['bestblockhash'], bb_hash)
|
assert_equal(json_obj['bestblockhash'], bb_hash)
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
RESTTest ().main ()
|
RESTTest().main()
|
||||||
|
|
Loading…
Reference in a new issue