You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
327 lines
15 KiB
327 lines
15 KiB
9 years ago
|
#!/usr/bin/env python3
|
||
7 years ago
|
# Copyright (c) 2014-2017 The Bitcoin Core developers
|
||
10 years ago
|
# Distributed under the MIT software license, see the accompanying
|
||
|
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||
8 years ago
|
"""Test the REST API."""
|
||
10 years ago
|
|
||
|
from test_framework.test_framework import BitcoinTestFramework
|
||
|
from test_framework.util import *
|
||
10 years ago
|
from struct import *
|
||
9 years ago
|
from io import BytesIO
|
||
|
from codecs import encode
|
||
10 years ago
|
|
||
9 years ago
|
import http.client
|
||
|
import urllib.parse
|
||
10 years ago
|
|
||
10 years ago
|
def deser_uint256(f):
|
||
|
r = 0
|
||
|
for i in range(8):
|
||
|
t = unpack(b"<I", f.read(4))[0]
|
||
|
r += t << (i * 32)
|
||
|
return r
|
||
|
|
||
9 years ago
|
#allows simple http get calls
|
||
|
def http_get_call(host, port, path, response_object = 0):
|
||
9 years ago
|
conn = http.client.HTTPConnection(host, port)
|
||
9 years ago
|
conn.request('GET', path)
|
||
|
|
||
|
if response_object:
|
||
|
return conn.getresponse()
|
||
|
|
||
9 years ago
|
return conn.getresponse().read().decode('utf-8')
|
||
9 years ago
|
|
||
|
#allows simple http post calls with a request body
|
||
|
def http_post_call(host, port, path, requestdata = '', response_object = 0):
|
||
9 years ago
|
conn = http.client.HTTPConnection(host, port)
|
||
9 years ago
|
conn.request('POST', path, requestdata)
|
||
10 years ago
|
|
||
10 years ago
|
if response_object:
|
||
|
return conn.getresponse()
|
||
10 years ago
|
|
||
10 years ago
|
return conn.getresponse().read()
|
||
|
|
||
|
class RESTTest (BitcoinTestFramework):
|
||
10 years ago
|
FORMAT_SEPARATOR = "."
|
||
10 years ago
|
|
||
8 years ago
|
def set_test_params(self):
|
||
9 years ago
|
self.setup_clean_chain = True
|
||
|
self.num_nodes = 3
|
||
10 years ago
|
|
||
|
def setup_network(self, split=False):
|
||
8 years ago
|
super().setup_network()
|
||
|
connect_nodes_bi(self.nodes, 0, 2)
|
||
10 years ago
|
|
||
10 years ago
|
def run_test(self):
|
||
9 years ago
|
url = urllib.parse.urlparse(self.nodes[0].url)
|
||
8 years ago
|
self.log.info("Mining blocks...")
|
||
10 years ago
|
|
||
10 years ago
|
self.nodes[0].generate(1)
|
||
10 years ago
|
self.sync_all()
|
||
10 years ago
|
self.nodes[2].generate(100)
|
||
10 years ago
|
self.sync_all()
|
||
10 years ago
|
|
||
10 years ago
|
assert_equal(self.nodes[0].getbalance(), 50)
|
||
10 years ago
|
|
||
10 years ago
|
txid = self.nodes[0].sendtoaddress(self.nodes[1].getnewaddress(), 0.1)
|
||
|
self.sync_all()
|
||
10 years ago
|
self.nodes[2].generate(1)
|
||
10 years ago
|
self.sync_all()
|
||
10 years ago
|
bb_hash = self.nodes[0].getbestblockhash()
|
||
10 years ago
|
|
||
10 years ago
|
assert_equal(self.nodes[1].getbalance(), Decimal("0.1")) #balance now should be 0.1 on node 1
|
||
10 years ago
|
|
||
10 years ago
|
# 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)
|
||
10 years ago
|
# get n of 0.1 outpoint
|
||
10 years ago
|
n = 0
|
||
|
for vout in json_obj['vout']:
|
||
|
if vout['value'] == 0.1:
|
||
|
n = vout['n']
|
||
10 years ago
|
|
||
|
|
||
7 years ago
|
#######################################
|
||
|
# GETUTXOS: query an unspent outpoint #
|
||
|
#######################################
|
||
10 years ago
|
json_request = '/checkmempool/'+txid+'-'+str(n)
|
||
|
json_string = http_get_call(url.hostname, url.port, '/rest/getutxos'+json_request+self.FORMAT_SEPARATOR+'json')
|
||
10 years ago
|
json_obj = json.loads(json_string)
|
||
10 years ago
|
|
||
10 years ago
|
#check chainTip response
|
||
|
assert_equal(json_obj['chaintipHash'], bb_hash)
|
||
10 years ago
|
|
||
10 years ago
|
#make sure there is one utxo
|
||
|
assert_equal(len(json_obj['utxos']), 1)
|
||
|
assert_equal(json_obj['utxos'][0]['value'], 0.1)
|
||
10 years ago
|
|
||
|
|
||
7 years ago
|
#################################################
|
||
|
# GETUTXOS: now query an already spent outpoint #
|
||
|
#################################################
|
||
10 years ago
|
json_request = '/checkmempool/'+vintx+'-0'
|
||
|
json_string = http_get_call(url.hostname, url.port, '/rest/getutxos'+json_request+self.FORMAT_SEPARATOR+'json')
|
||
10 years ago
|
json_obj = json.loads(json_string)
|
||
10 years ago
|
|
||
10 years ago
|
#check chainTip response
|
||
|
assert_equal(json_obj['chaintipHash'], bb_hash)
|
||
10 years ago
|
|
||
10 years ago
|
#make sure there is no utox in the response because this oupoint has been spent
|
||
|
assert_equal(len(json_obj['utxos']), 0)
|
||
10 years ago
|
|
||
10 years ago
|
#check bitmap
|
||
|
assert_equal(json_obj['bitmap'], "0")
|
||
10 years ago
|
|
||
|
|
||
10 years ago
|
##################################################
|
||
|
# GETUTXOS: now check both with the same request #
|
||
|
##################################################
|
||
10 years ago
|
json_request = '/checkmempool/'+txid+'-'+str(n)+'/'+vintx+'-0'
|
||
|
json_string = http_get_call(url.hostname, url.port, '/rest/getutxos'+json_request+self.FORMAT_SEPARATOR+'json')
|
||
10 years ago
|
json_obj = json.loads(json_string)
|
||
|
assert_equal(len(json_obj['utxos']), 1)
|
||
|
assert_equal(json_obj['bitmap'], "10")
|
||
10 years ago
|
|
||
10 years ago
|
#test binary response
|
||
|
bb_hash = self.nodes[0].getbestblockhash()
|
||
|
|
||
|
binaryRequest = b'\x01\x02'
|
||
9 years ago
|
binaryRequest += hex_str_to_bytes(txid)
|
||
9 years ago
|
binaryRequest += pack("i", n)
|
||
9 years ago
|
binaryRequest += hex_str_to_bytes(vintx)
|
||
9 years ago
|
binaryRequest += pack("i", 0)
|
||
10 years ago
|
|
||
9 years ago
|
bin_response = http_post_call(url.hostname, url.port, '/rest/getutxos'+self.FORMAT_SEPARATOR+'bin', binaryRequest)
|
||
9 years ago
|
output = BytesIO()
|
||
10 years ago
|
output.write(bin_response)
|
||
|
output.seek(0)
|
||
|
chainHeight = unpack("i", output.read(4))[0]
|
||
9 years ago
|
hashFromBinResponse = hex(deser_uint256(output))[2:].zfill(64)
|
||
10 years ago
|
|
||
10 years ago
|
assert_equal(bb_hash, hashFromBinResponse) #check if getutxo's chaintip during calculation was fine
|
||
|
assert_equal(chainHeight, 102) #chain height must be 102
|
||
10 years ago
|
|
||
|
|
||
10 years ago
|
############################
|
||
|
# GETUTXOS: mempool checks #
|
||
|
############################
|
||
|
|
||
|
# 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_obj = json.loads(json_string)
|
||
|
vintx = json_obj['vin'][0]['txid'] # get the vin to later check for utxo (should be spent by then)
|
||
10 years ago
|
# get n of 0.1 outpoint
|
||
10 years ago
|
n = 0
|
||
|
for vout in json_obj['vout']:
|
||
|
if vout['value'] == 0.1:
|
||
|
n = vout['n']
|
||
10 years ago
|
|
||
|
json_request = '/'+txid+'-'+str(n)
|
||
|
json_string = http_get_call(url.hostname, url.port, '/rest/getutxos'+json_request+self.FORMAT_SEPARATOR+'json')
|
||
10 years ago
|
json_obj = json.loads(json_string)
|
||
7 years ago
|
assert_equal(len(json_obj['utxos']), 0) #there should be an outpoint because it has just added to the mempool
|
||
10 years ago
|
|
||
|
json_request = '/checkmempool/'+txid+'-'+str(n)
|
||
|
json_string = http_get_call(url.hostname, url.port, '/rest/getutxos'+json_request+self.FORMAT_SEPARATOR+'json')
|
||
10 years ago
|
json_obj = json.loads(json_string)
|
||
7 years ago
|
assert_equal(len(json_obj['utxos']), 1) #there should be an outpoint because it has just added to the mempool
|
||
10 years ago
|
|
||
10 years ago
|
#do some invalid requests
|
||
|
json_request = '{"checkmempool'
|
||
9 years ago
|
response = http_post_call(url.hostname, url.port, '/rest/getutxos'+self.FORMAT_SEPARATOR+'json', json_request, True)
|
||
7 years ago
|
assert_equal(response.status, 400) #must be a 400 because we send an invalid json request
|
||
10 years ago
|
|
||
10 years ago
|
json_request = '{"checkmempool'
|
||
9 years ago
|
response = http_post_call(url.hostname, url.port, '/rest/getutxos'+self.FORMAT_SEPARATOR+'bin', json_request, True)
|
||
7 years ago
|
assert_equal(response.status, 400) #must be a 400 because we send an invalid bin request
|
||
10 years ago
|
|
||
9 years ago
|
response = http_post_call(url.hostname, url.port, '/rest/getutxos/checkmempool'+self.FORMAT_SEPARATOR+'bin', '', True)
|
||
7 years ago
|
assert_equal(response.status, 400) #must be a 400 because we send an invalid bin request
|
||
10 years ago
|
|
||
10 years ago
|
#test limits
|
||
10 years ago
|
json_request = '/checkmempool/'
|
||
|
for x in range(0, 20):
|
||
|
json_request += txid+'-'+str(n)+'/'
|
||
|
json_request = json_request.rstrip("/")
|
||
9 years ago
|
response = http_post_call(url.hostname, url.port, '/rest/getutxos'+json_request+self.FORMAT_SEPARATOR+'json', '', True)
|
||
8 years ago
|
assert_equal(response.status, 400) #must be a 400 because we exceeding the limits
|
||
10 years ago
|
|
||
|
json_request = '/checkmempool/'
|
||
|
for x in range(0, 15):
|
||
|
json_request += txid+'-'+str(n)+'/'
|
||
9 years ago
|
json_request = json_request.rstrip("/")
|
||
9 years ago
|
response = http_post_call(url.hostname, url.port, '/rest/getutxos'+json_request+self.FORMAT_SEPARATOR+'json', '', True)
|
||
8 years ago
|
assert_equal(response.status, 200) #must be a 200 because we are within the limits
|
||
10 years ago
|
|
||
9 years ago
|
self.nodes[0].generate(1) #generate block to not affect upcoming tests
|
||
10 years ago
|
self.sync_all()
|
||
10 years ago
|
|
||
10 years ago
|
################
|
||
|
# /rest/block/ #
|
||
|
################
|
||
10 years ago
|
|
||
10 years ago
|
# check binary format
|
||
9 years ago
|
response = http_get_call(url.hostname, url.port, '/rest/block/'+bb_hash+self.FORMAT_SEPARATOR+"bin", True)
|
||
10 years ago
|
assert_equal(response.status, 200)
|
||
10 years ago
|
assert_greater_than(int(response.getheader('content-length')), 80)
|
||
|
response_str = response.read()
|
||
|
|
||
|
# compare with block header
|
||
9 years ago
|
response_header = http_get_call(url.hostname, url.port, '/rest/headers/1/'+bb_hash+self.FORMAT_SEPARATOR+"bin", True)
|
||
10 years ago
|
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
|
||
9 years ago
|
response_hex = http_get_call(url.hostname, url.port, '/rest/block/'+bb_hash+self.FORMAT_SEPARATOR+"hex", True)
|
||
10 years ago
|
assert_equal(response_hex.status, 200)
|
||
|
assert_greater_than(int(response_hex.getheader('content-length')), 160)
|
||
|
response_hex_str = response_hex.read()
|
||
9 years ago
|
assert_equal(encode(response_str, "hex_codec")[0:160], response_hex_str[0:160])
|
||
10 years ago
|
|
||
|
# compare with hex block header
|
||
9 years ago
|
response_header_hex = http_get_call(url.hostname, url.port, '/rest/headers/1/'+bb_hash+self.FORMAT_SEPARATOR+"hex", True)
|
||
10 years ago
|
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])
|
||
9 years ago
|
assert_equal(encode(response_header_str, "hex_codec")[0:160], response_header_hex_str[0:160])
|
||
10 years ago
|
|
||
10 years ago
|
# check json format
|
||
10 years ago
|
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)
|
||
|
assert_equal(block_json_obj['hash'], bb_hash)
|
||
|
|
||
|
# compare with json block header
|
||
9 years ago
|
response_header_json = http_get_call(url.hostname, url.port, '/rest/headers/1/'+bb_hash+self.FORMAT_SEPARATOR+"json", True)
|
||
10 years ago
|
assert_equal(response_header_json.status, 200)
|
||
9 years ago
|
response_header_json_str = response_header_json.read().decode('utf-8')
|
||
9 years ago
|
json_obj = json.loads(response_header_json_str, parse_float=Decimal)
|
||
10 years ago
|
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
|
||
|
|
||
|
#compare with normal RPC block response
|
||
|
rpc_block_json = self.nodes[0].getblock(bb_hash)
|
||
|
assert_equal(json_obj[0]['hash'], rpc_block_json['hash'])
|
||
|
assert_equal(json_obj[0]['confirmations'], rpc_block_json['confirmations'])
|
||
|
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
|
||
|
self.nodes[1].generate(5)
|
||
|
self.sync_all()
|
||
9 years ago
|
response_header_json = http_get_call(url.hostname, url.port, '/rest/headers/5/'+bb_hash+self.FORMAT_SEPARATOR+"json", True)
|
||
10 years ago
|
assert_equal(response_header_json.status, 200)
|
||
9 years ago
|
response_header_json_str = response_header_json.read().decode('utf-8')
|
||
10 years ago
|
json_obj = json.loads(response_header_json_str)
|
||
|
assert_equal(len(json_obj), 5) #now we should have 5 header objects
|
||
10 years ago
|
|
||
10 years ago
|
# do tx test
|
||
9 years ago
|
tx_hash = block_json_obj['tx'][0]['txid']
|
||
10 years ago
|
json_string = http_get_call(url.hostname, url.port, '/rest/tx/'+tx_hash+self.FORMAT_SEPARATOR+"json")
|
||
|
json_obj = json.loads(json_string)
|
||
|
assert_equal(json_obj['txid'], tx_hash)
|
||
10 years ago
|
|
||
10 years ago
|
# check hex format response
|
||
9 years ago
|
hex_string = http_get_call(url.hostname, url.port, '/rest/tx/'+tx_hash+self.FORMAT_SEPARATOR+"hex", True)
|
||
10 years ago
|
assert_equal(hex_string.status, 200)
|
||
10 years ago
|
assert_greater_than(int(response.getheader('content-length')), 10)
|
||
10 years ago
|
|
||
10 years ago
|
|
||
10 years ago
|
# check block tx details
|
||
|
# let's 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))
|
||
|
self.sync_all()
|
||
10 years ago
|
|
||
9 years ago
|
# 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_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_obj = json.loads(json_string)
|
||
|
for tx in txs:
|
||
|
assert_equal(tx in json_obj, True)
|
||
|
|
||
10 years ago
|
# now mine the transactions
|
||
10 years ago
|
newblockhash = self.nodes[1].generate(1)
|
||
10 years ago
|
self.sync_all()
|
||
10 years ago
|
|
||
10 years ago
|
#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_obj = json.loads(json_string)
|
||
|
for tx in json_obj['tx']:
|
||
|
if not 'coinbase' in tx['vin'][0]: #exclude coinbase
|
||
|
assert_equal(tx['txid'] in txs, True)
|
||
10 years ago
|
|
||
10 years ago
|
#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_obj = json.loads(json_string)
|
||
|
for tx in txs:
|
||
|
assert_equal(tx in json_obj['tx'], True)
|
||
10 years ago
|
|
||
10 years ago
|
#test rest bestblock
|
||
|
bb_hash = self.nodes[0].getbestblockhash()
|
||
10 years ago
|
|
||
10 years ago
|
json_string = http_get_call(url.hostname, url.port, '/rest/chaininfo.json')
|
||
|
json_obj = json.loads(json_string)
|
||
|
assert_equal(json_obj['bestblockhash'], bb_hash)
|
||
|
|
||
10 years ago
|
if __name__ == '__main__':
|
||
10 years ago
|
RESTTest ().main ()
|