2016-03-19 12:58:06 -07:00
|
|
|
#!/usr/bin/env python3
|
2018-01-02 09:12:05 -08:00
|
|
|
# Copyright (c) 2014-2017 The Bitcoin Core developers
|
2014-11-26 07:26:02 -08:00
|
|
|
# Distributed under the MIT software license, see the accompanying
|
|
|
|
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
2017-01-17 15:34:40 -08:00
|
|
|
"""Test the REST API."""
|
2015-05-02 03:53:35 -07:00
|
|
|
|
|
|
|
from test_framework.test_framework import BitcoinTestFramework
|
|
|
|
from test_framework.util import *
|
2014-12-01 03:38:42 -08:00
|
|
|
from struct import *
|
2016-03-19 13:36:32 -07:00
|
|
|
from io import BytesIO
|
|
|
|
from codecs import encode
|
2014-11-26 07:26:02 -08:00
|
|
|
|
2016-03-19 12:58:06 -07:00
|
|
|
import http.client
|
|
|
|
import urllib.parse
|
2014-11-26 07:26:02 -08:00
|
|
|
|
2014-12-01 03:38:42 -08:00
|
|
|
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
|
|
|
|
|
2015-08-27 11:27:12 -07:00
|
|
|
#allows simple http get calls
|
|
|
|
def http_get_call(host, port, path, response_object = 0):
|
2016-03-19 12:58:06 -07:00
|
|
|
conn = http.client.HTTPConnection(host, port)
|
2015-08-27 11:27:12 -07:00
|
|
|
conn.request('GET', path)
|
|
|
|
|
|
|
|
if response_object:
|
|
|
|
return conn.getresponse()
|
|
|
|
|
2016-04-10 07:54:28 -07:00
|
|
|
return conn.getresponse().read().decode('utf-8')
|
2015-08-27 11:27:12 -07:00
|
|
|
|
|
|
|
#allows simple http post calls with a request body
|
|
|
|
def http_post_call(host, port, path, requestdata = '', response_object = 0):
|
2016-03-19 12:58:06 -07:00
|
|
|
conn = http.client.HTTPConnection(host, port)
|
2015-08-27 11:27:12 -07:00
|
|
|
conn.request('POST', path, requestdata)
|
2014-12-08 04:44:49 -08:00
|
|
|
|
2014-11-26 07:26:02 -08:00
|
|
|
if response_object:
|
|
|
|
return conn.getresponse()
|
2014-12-08 04:44:49 -08:00
|
|
|
|
2014-11-26 07:26:02 -08:00
|
|
|
return conn.getresponse().read()
|
|
|
|
|
|
|
|
class RESTTest (BitcoinTestFramework):
|
2014-12-02 06:57:20 -08:00
|
|
|
FORMAT_SEPARATOR = "."
|
2014-12-08 04:44:49 -08:00
|
|
|
|
2017-06-09 15:21:21 -07:00
|
|
|
def set_test_params(self):
|
2016-05-14 04:01:31 -07:00
|
|
|
self.setup_clean_chain = True
|
|
|
|
self.num_nodes = 3
|
2018-02-15 11:01:43 -08:00
|
|
|
self.extra_args = [["-rest"]] * self.num_nodes
|
2014-12-01 03:38:42 -08:00
|
|
|
|
|
|
|
def setup_network(self, split=False):
|
2017-04-03 06:34:04 -07:00
|
|
|
super().setup_network()
|
|
|
|
connect_nodes_bi(self.nodes, 0, 2)
|
2015-05-27 06:56:16 -07:00
|
|
|
|
2014-11-26 07:26:02 -08:00
|
|
|
def run_test(self):
|
2016-03-19 12:58:06 -07:00
|
|
|
url = urllib.parse.urlparse(self.nodes[0].url)
|
2017-03-07 15:46:17 -08:00
|
|
|
self.log.info("Mining blocks...")
|
2015-05-27 06:56:16 -07:00
|
|
|
|
2015-04-21 11:32:37 -07:00
|
|
|
self.nodes[0].generate(1)
|
2014-12-01 03:38:42 -08:00
|
|
|
self.sync_all()
|
2015-04-21 11:32:37 -07:00
|
|
|
self.nodes[2].generate(100)
|
2014-12-01 03:38:42 -08:00
|
|
|
self.sync_all()
|
2015-05-27 06:56:16 -07:00
|
|
|
|
2014-12-01 03:38:42 -08:00
|
|
|
assert_equal(self.nodes[0].getbalance(), 50)
|
2015-05-27 06:56:16 -07:00
|
|
|
|
2014-12-01 03:38:42 -08:00
|
|
|
txid = self.nodes[0].sendtoaddress(self.nodes[1].getnewaddress(), 0.1)
|
|
|
|
self.sync_all()
|
2015-04-21 11:32:37 -07:00
|
|
|
self.nodes[2].generate(1)
|
2014-12-01 03:38:42 -08:00
|
|
|
self.sync_all()
|
2014-11-26 07:26:02 -08:00
|
|
|
bb_hash = self.nodes[0].getbestblockhash()
|
2015-05-27 06:56:16 -07:00
|
|
|
|
2014-12-01 03:38:42 -08:00
|
|
|
assert_equal(self.nodes[1].getbalance(), Decimal("0.1")) #balance now should be 0.1 on node 1
|
2015-05-27 06:56:16 -07:00
|
|
|
|
2014-12-01 03:38:42 -08:00
|
|
|
# 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)
|
2015-05-27 06:56:16 -07:00
|
|
|
# get n of 0.1 outpoint
|
2014-12-01 03:38:42 -08:00
|
|
|
n = 0
|
|
|
|
for vout in json_obj['vout']:
|
|
|
|
if vout['value'] == 0.1:
|
|
|
|
n = vout['n']
|
2015-05-27 06:56:16 -07:00
|
|
|
|
|
|
|
|
2017-06-19 15:57:31 -07:00
|
|
|
#######################################
|
|
|
|
# GETUTXOS: query an unspent outpoint #
|
|
|
|
#######################################
|
[REST] Handle UTXO retrieval when ignoring the mempool
Current REST API always returns empty UTXO when invoked without `/checkmempool/` URL part.
After the fix:
```
$ curl -s http://localhost:8332/rest/getutxos/0e3e2357e806b6cdb1f70b54c3a3a17b6714ee1f0e68bebb44a74b1efd512098-0.json | jq
{
"chainHeight": 514109,
"chaintipHash": "0000000000000000001fe76d1445e8a6432fd2de04261dc9c5915311dc7ad6de",
"bitmap": "1",
"utxos": [
{
"height": 1,
"value": 50,
"scriptPubKey": {
"asm": "0496b538e853519c726a2c91e61ec11600ae1390813a627c66fb8be7947be63c52da7589379515d4e0a604f8141781e62294721166bf621e73a82cbf2342c858ee OP_CHECKSIG",
"hex": "410496b538e853519c726a2c91e61ec11600ae1390813a627c66fb8be7947be63c52da7589379515d4e0a604f8141781e62294721166bf621e73a82cbf2342c858eeac",
"reqSigs": 1,
"type": "pubkey",
"addresses": [
"12c6DSiU4Rq3P4ZxziKxzrL5LmMBrzjrJX"
]
}
}
]
}
```
Before the fix:
```
$ curl -s http://localhost:8332/rest/getutxos/0e3e2357e806b6cdb1f70b54c3a3a17b6714ee1f0e68bebb44a74b1efd512098-0.json | jq
{
"chainHeight": 514109,
"chaintipHash": "0000000000000000001fe76d1445e8a6432fd2de04261dc9c5915311dc7ad6de",
"bitmap": "0",
"utxos": []
}
```
2018-03-18 07:25:30 -07:00
|
|
|
json_request = '/'+txid+'-'+str(n)
|
2015-05-27 06:56:16 -07:00
|
|
|
json_string = http_get_call(url.hostname, url.port, '/rest/getutxos'+json_request+self.FORMAT_SEPARATOR+'json')
|
2014-12-01 03:38:42 -08:00
|
|
|
json_obj = json.loads(json_string)
|
2015-05-27 06:56:16 -07:00
|
|
|
|
2014-12-01 03:38:42 -08:00
|
|
|
#check chainTip response
|
|
|
|
assert_equal(json_obj['chaintipHash'], bb_hash)
|
2015-05-27 06:56:16 -07:00
|
|
|
|
2014-12-01 03:38:42 -08:00
|
|
|
#make sure there is one utxo
|
|
|
|
assert_equal(len(json_obj['utxos']), 1)
|
|
|
|
assert_equal(json_obj['utxos'][0]['value'], 0.1)
|
2015-05-27 06:56:16 -07:00
|
|
|
|
|
|
|
|
2017-06-19 15:57:31 -07:00
|
|
|
#################################################
|
|
|
|
# GETUTXOS: now query an already spent outpoint #
|
|
|
|
#################################################
|
[REST] Handle UTXO retrieval when ignoring the mempool
Current REST API always returns empty UTXO when invoked without `/checkmempool/` URL part.
After the fix:
```
$ curl -s http://localhost:8332/rest/getutxos/0e3e2357e806b6cdb1f70b54c3a3a17b6714ee1f0e68bebb44a74b1efd512098-0.json | jq
{
"chainHeight": 514109,
"chaintipHash": "0000000000000000001fe76d1445e8a6432fd2de04261dc9c5915311dc7ad6de",
"bitmap": "1",
"utxos": [
{
"height": 1,
"value": 50,
"scriptPubKey": {
"asm": "0496b538e853519c726a2c91e61ec11600ae1390813a627c66fb8be7947be63c52da7589379515d4e0a604f8141781e62294721166bf621e73a82cbf2342c858ee OP_CHECKSIG",
"hex": "410496b538e853519c726a2c91e61ec11600ae1390813a627c66fb8be7947be63c52da7589379515d4e0a604f8141781e62294721166bf621e73a82cbf2342c858eeac",
"reqSigs": 1,
"type": "pubkey",
"addresses": [
"12c6DSiU4Rq3P4ZxziKxzrL5LmMBrzjrJX"
]
}
}
]
}
```
Before the fix:
```
$ curl -s http://localhost:8332/rest/getutxos/0e3e2357e806b6cdb1f70b54c3a3a17b6714ee1f0e68bebb44a74b1efd512098-0.json | jq
{
"chainHeight": 514109,
"chaintipHash": "0000000000000000001fe76d1445e8a6432fd2de04261dc9c5915311dc7ad6de",
"bitmap": "0",
"utxos": []
}
```
2018-03-18 07:25:30 -07:00
|
|
|
json_request = '/'+vintx+'-0'
|
2015-05-27 06:56:16 -07:00
|
|
|
json_string = http_get_call(url.hostname, url.port, '/rest/getutxos'+json_request+self.FORMAT_SEPARATOR+'json')
|
2014-12-01 03:38:42 -08:00
|
|
|
json_obj = json.loads(json_string)
|
2015-05-27 06:56:16 -07:00
|
|
|
|
2014-12-01 03:38:42 -08:00
|
|
|
#check chainTip response
|
|
|
|
assert_equal(json_obj['chaintipHash'], bb_hash)
|
2014-12-08 04:44:49 -08:00
|
|
|
|
2018-03-18 07:26:45 -07:00
|
|
|
#make sure there is no utxo in the response because this oupoint has been spent
|
2014-12-01 03:38:42 -08:00
|
|
|
assert_equal(len(json_obj['utxos']), 0)
|
2015-05-27 06:56:16 -07:00
|
|
|
|
2014-12-01 03:38:42 -08:00
|
|
|
#check bitmap
|
|
|
|
assert_equal(json_obj['bitmap'], "0")
|
2015-05-27 06:56:16 -07:00
|
|
|
|
|
|
|
|
2014-12-01 03:38:42 -08:00
|
|
|
##################################################
|
|
|
|
# GETUTXOS: now check both with the same request #
|
|
|
|
##################################################
|
[REST] Handle UTXO retrieval when ignoring the mempool
Current REST API always returns empty UTXO when invoked without `/checkmempool/` URL part.
After the fix:
```
$ curl -s http://localhost:8332/rest/getutxos/0e3e2357e806b6cdb1f70b54c3a3a17b6714ee1f0e68bebb44a74b1efd512098-0.json | jq
{
"chainHeight": 514109,
"chaintipHash": "0000000000000000001fe76d1445e8a6432fd2de04261dc9c5915311dc7ad6de",
"bitmap": "1",
"utxos": [
{
"height": 1,
"value": 50,
"scriptPubKey": {
"asm": "0496b538e853519c726a2c91e61ec11600ae1390813a627c66fb8be7947be63c52da7589379515d4e0a604f8141781e62294721166bf621e73a82cbf2342c858ee OP_CHECKSIG",
"hex": "410496b538e853519c726a2c91e61ec11600ae1390813a627c66fb8be7947be63c52da7589379515d4e0a604f8141781e62294721166bf621e73a82cbf2342c858eeac",
"reqSigs": 1,
"type": "pubkey",
"addresses": [
"12c6DSiU4Rq3P4ZxziKxzrL5LmMBrzjrJX"
]
}
}
]
}
```
Before the fix:
```
$ curl -s http://localhost:8332/rest/getutxos/0e3e2357e806b6cdb1f70b54c3a3a17b6714ee1f0e68bebb44a74b1efd512098-0.json | jq
{
"chainHeight": 514109,
"chaintipHash": "0000000000000000001fe76d1445e8a6432fd2de04261dc9c5915311dc7ad6de",
"bitmap": "0",
"utxos": []
}
```
2018-03-18 07:25:30 -07:00
|
|
|
json_request = '/'+txid+'-'+str(n)+'/'+vintx+'-0'
|
2015-05-27 06:56:16 -07:00
|
|
|
json_string = http_get_call(url.hostname, url.port, '/rest/getutxos'+json_request+self.FORMAT_SEPARATOR+'json')
|
2014-12-01 03:38:42 -08:00
|
|
|
json_obj = json.loads(json_string)
|
|
|
|
assert_equal(len(json_obj['utxos']), 1)
|
|
|
|
assert_equal(json_obj['bitmap'], "10")
|
2015-05-27 06:56:16 -07:00
|
|
|
|
2014-12-01 03:38:42 -08:00
|
|
|
#test binary response
|
|
|
|
bb_hash = self.nodes[0].getbestblockhash()
|
|
|
|
|
|
|
|
binaryRequest = b'\x01\x02'
|
2016-04-10 07:54:28 -07:00
|
|
|
binaryRequest += hex_str_to_bytes(txid)
|
2015-12-02 09:12:23 -08:00
|
|
|
binaryRequest += pack("i", n)
|
2016-04-10 07:54:28 -07:00
|
|
|
binaryRequest += hex_str_to_bytes(vintx)
|
2015-12-02 09:12:23 -08:00
|
|
|
binaryRequest += pack("i", 0)
|
2015-05-27 06:56:16 -07:00
|
|
|
|
2015-08-27 11:27:12 -07:00
|
|
|
bin_response = http_post_call(url.hostname, url.port, '/rest/getutxos'+self.FORMAT_SEPARATOR+'bin', binaryRequest)
|
2016-03-19 13:36:32 -07:00
|
|
|
output = BytesIO()
|
2014-12-01 03:38:42 -08:00
|
|
|
output.write(bin_response)
|
|
|
|
output.seek(0)
|
|
|
|
chainHeight = unpack("i", output.read(4))[0]
|
2016-03-19 12:58:06 -07:00
|
|
|
hashFromBinResponse = hex(deser_uint256(output))[2:].zfill(64)
|
2015-05-27 06:56:16 -07:00
|
|
|
|
2014-12-01 03:38:42 -08:00
|
|
|
assert_equal(bb_hash, hashFromBinResponse) #check if getutxo's chaintip during calculation was fine
|
|
|
|
assert_equal(chainHeight, 102) #chain height must be 102
|
2015-05-27 06:56:16 -07:00
|
|
|
|
|
|
|
|
2014-12-01 03:38:42 -08:00
|
|
|
############################
|
|
|
|
# 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)
|
[REST] Handle UTXO retrieval when ignoring the mempool
Current REST API always returns empty UTXO when invoked without `/checkmempool/` URL part.
After the fix:
```
$ curl -s http://localhost:8332/rest/getutxos/0e3e2357e806b6cdb1f70b54c3a3a17b6714ee1f0e68bebb44a74b1efd512098-0.json | jq
{
"chainHeight": 514109,
"chaintipHash": "0000000000000000001fe76d1445e8a6432fd2de04261dc9c5915311dc7ad6de",
"bitmap": "1",
"utxos": [
{
"height": 1,
"value": 50,
"scriptPubKey": {
"asm": "0496b538e853519c726a2c91e61ec11600ae1390813a627c66fb8be7947be63c52da7589379515d4e0a604f8141781e62294721166bf621e73a82cbf2342c858ee OP_CHECKSIG",
"hex": "410496b538e853519c726a2c91e61ec11600ae1390813a627c66fb8be7947be63c52da7589379515d4e0a604f8141781e62294721166bf621e73a82cbf2342c858eeac",
"reqSigs": 1,
"type": "pubkey",
"addresses": [
"12c6DSiU4Rq3P4ZxziKxzrL5LmMBrzjrJX"
]
}
}
]
}
```
Before the fix:
```
$ curl -s http://localhost:8332/rest/getutxos/0e3e2357e806b6cdb1f70b54c3a3a17b6714ee1f0e68bebb44a74b1efd512098-0.json | jq
{
"chainHeight": 514109,
"chaintipHash": "0000000000000000001fe76d1445e8a6432fd2de04261dc9c5915311dc7ad6de",
"bitmap": "0",
"utxos": []
}
```
2018-03-18 07:25:30 -07:00
|
|
|
# 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'])
|
2015-05-27 06:56:16 -07:00
|
|
|
# get n of 0.1 outpoint
|
2014-12-01 03:38:42 -08:00
|
|
|
n = 0
|
|
|
|
for vout in json_obj['vout']:
|
|
|
|
if vout['value'] == 0.1:
|
|
|
|
n = vout['n']
|
[REST] Handle UTXO retrieval when ignoring the mempool
Current REST API always returns empty UTXO when invoked without `/checkmempool/` URL part.
After the fix:
```
$ curl -s http://localhost:8332/rest/getutxos/0e3e2357e806b6cdb1f70b54c3a3a17b6714ee1f0e68bebb44a74b1efd512098-0.json | jq
{
"chainHeight": 514109,
"chaintipHash": "0000000000000000001fe76d1445e8a6432fd2de04261dc9c5915311dc7ad6de",
"bitmap": "1",
"utxos": [
{
"height": 1,
"value": 50,
"scriptPubKey": {
"asm": "0496b538e853519c726a2c91e61ec11600ae1390813a627c66fb8be7947be63c52da7589379515d4e0a604f8141781e62294721166bf621e73a82cbf2342c858ee OP_CHECKSIG",
"hex": "410496b538e853519c726a2c91e61ec11600ae1390813a627c66fb8be7947be63c52da7589379515d4e0a604f8141781e62294721166bf621e73a82cbf2342c858eeac",
"reqSigs": 1,
"type": "pubkey",
"addresses": [
"12c6DSiU4Rq3P4ZxziKxzrL5LmMBrzjrJX"
]
}
}
]
}
```
Before the fix:
```
$ curl -s http://localhost:8332/rest/getutxos/0e3e2357e806b6cdb1f70b54c3a3a17b6714ee1f0e68bebb44a74b1efd512098-0.json | jq
{
"chainHeight": 514109,
"chaintipHash": "0000000000000000001fe76d1445e8a6432fd2de04261dc9c5915311dc7ad6de",
"bitmap": "0",
"utxos": []
}
```
2018-03-18 07:25:30 -07:00
|
|
|
spending = '{}-{}'.format(txid, n)
|
2015-05-27 06:56:16 -07:00
|
|
|
|
[REST] Handle UTXO retrieval when ignoring the mempool
Current REST API always returns empty UTXO when invoked without `/checkmempool/` URL part.
After the fix:
```
$ curl -s http://localhost:8332/rest/getutxos/0e3e2357e806b6cdb1f70b54c3a3a17b6714ee1f0e68bebb44a74b1efd512098-0.json | jq
{
"chainHeight": 514109,
"chaintipHash": "0000000000000000001fe76d1445e8a6432fd2de04261dc9c5915311dc7ad6de",
"bitmap": "1",
"utxos": [
{
"height": 1,
"value": 50,
"scriptPubKey": {
"asm": "0496b538e853519c726a2c91e61ec11600ae1390813a627c66fb8be7947be63c52da7589379515d4e0a604f8141781e62294721166bf621e73a82cbf2342c858ee OP_CHECKSIG",
"hex": "410496b538e853519c726a2c91e61ec11600ae1390813a627c66fb8be7947be63c52da7589379515d4e0a604f8141781e62294721166bf621e73a82cbf2342c858eeac",
"reqSigs": 1,
"type": "pubkey",
"addresses": [
"12c6DSiU4Rq3P4ZxziKxzrL5LmMBrzjrJX"
]
}
}
]
}
```
Before the fix:
```
$ curl -s http://localhost:8332/rest/getutxos/0e3e2357e806b6cdb1f70b54c3a3a17b6714ee1f0e68bebb44a74b1efd512098-0.json | jq
{
"chainHeight": 514109,
"chaintipHash": "0000000000000000001fe76d1445e8a6432fd2de04261dc9c5915311dc7ad6de",
"bitmap": "0",
"utxos": []
}
```
2018-03-18 07:25:30 -07:00
|
|
|
json_request = '/'+spending
|
2015-05-27 06:56:16 -07:00
|
|
|
json_string = http_get_call(url.hostname, url.port, '/rest/getutxos'+json_request+self.FORMAT_SEPARATOR+'json')
|
2014-12-01 03:38:42 -08:00
|
|
|
json_obj = json.loads(json_string)
|
[REST] Handle UTXO retrieval when ignoring the mempool
Current REST API always returns empty UTXO when invoked without `/checkmempool/` URL part.
After the fix:
```
$ curl -s http://localhost:8332/rest/getutxos/0e3e2357e806b6cdb1f70b54c3a3a17b6714ee1f0e68bebb44a74b1efd512098-0.json | jq
{
"chainHeight": 514109,
"chaintipHash": "0000000000000000001fe76d1445e8a6432fd2de04261dc9c5915311dc7ad6de",
"bitmap": "1",
"utxos": [
{
"height": 1,
"value": 50,
"scriptPubKey": {
"asm": "0496b538e853519c726a2c91e61ec11600ae1390813a627c66fb8be7947be63c52da7589379515d4e0a604f8141781e62294721166bf621e73a82cbf2342c858ee OP_CHECKSIG",
"hex": "410496b538e853519c726a2c91e61ec11600ae1390813a627c66fb8be7947be63c52da7589379515d4e0a604f8141781e62294721166bf621e73a82cbf2342c858eeac",
"reqSigs": 1,
"type": "pubkey",
"addresses": [
"12c6DSiU4Rq3P4ZxziKxzrL5LmMBrzjrJX"
]
}
}
]
}
```
Before the fix:
```
$ curl -s http://localhost:8332/rest/getutxos/0e3e2357e806b6cdb1f70b54c3a3a17b6714ee1f0e68bebb44a74b1efd512098-0.json | jq
{
"chainHeight": 514109,
"chaintipHash": "0000000000000000001fe76d1445e8a6432fd2de04261dc9c5915311dc7ad6de",
"bitmap": "0",
"utxos": []
}
```
2018-03-18 07:25:30 -07:00
|
|
|
assert_equal(len(json_obj['utxos']), 0) #there should be no outpoint because it has just added to the mempool
|
2015-05-27 06:56:16 -07:00
|
|
|
|
[REST] Handle UTXO retrieval when ignoring the mempool
Current REST API always returns empty UTXO when invoked without `/checkmempool/` URL part.
After the fix:
```
$ curl -s http://localhost:8332/rest/getutxos/0e3e2357e806b6cdb1f70b54c3a3a17b6714ee1f0e68bebb44a74b1efd512098-0.json | jq
{
"chainHeight": 514109,
"chaintipHash": "0000000000000000001fe76d1445e8a6432fd2de04261dc9c5915311dc7ad6de",
"bitmap": "1",
"utxos": [
{
"height": 1,
"value": 50,
"scriptPubKey": {
"asm": "0496b538e853519c726a2c91e61ec11600ae1390813a627c66fb8be7947be63c52da7589379515d4e0a604f8141781e62294721166bf621e73a82cbf2342c858ee OP_CHECKSIG",
"hex": "410496b538e853519c726a2c91e61ec11600ae1390813a627c66fb8be7947be63c52da7589379515d4e0a604f8141781e62294721166bf621e73a82cbf2342c858eeac",
"reqSigs": 1,
"type": "pubkey",
"addresses": [
"12c6DSiU4Rq3P4ZxziKxzrL5LmMBrzjrJX"
]
}
}
]
}
```
Before the fix:
```
$ curl -s http://localhost:8332/rest/getutxos/0e3e2357e806b6cdb1f70b54c3a3a17b6714ee1f0e68bebb44a74b1efd512098-0.json | jq
{
"chainHeight": 514109,
"chaintipHash": "0000000000000000001fe76d1445e8a6432fd2de04261dc9c5915311dc7ad6de",
"bitmap": "0",
"utxos": []
}
```
2018-03-18 07:25:30 -07:00
|
|
|
json_request = '/checkmempool/'+spending
|
2015-05-27 06:56:16 -07:00
|
|
|
json_string = http_get_call(url.hostname, url.port, '/rest/getutxos'+json_request+self.FORMAT_SEPARATOR+'json')
|
2014-12-01 03:38:42 -08:00
|
|
|
json_obj = json.loads(json_string)
|
2017-06-19 15:57:31 -07:00
|
|
|
assert_equal(len(json_obj['utxos']), 1) #there should be an outpoint because it has just added to the mempool
|
2015-05-27 06:56:16 -07:00
|
|
|
|
[REST] Handle UTXO retrieval when ignoring the mempool
Current REST API always returns empty UTXO when invoked without `/checkmempool/` URL part.
After the fix:
```
$ curl -s http://localhost:8332/rest/getutxos/0e3e2357e806b6cdb1f70b54c3a3a17b6714ee1f0e68bebb44a74b1efd512098-0.json | jq
{
"chainHeight": 514109,
"chaintipHash": "0000000000000000001fe76d1445e8a6432fd2de04261dc9c5915311dc7ad6de",
"bitmap": "1",
"utxos": [
{
"height": 1,
"value": 50,
"scriptPubKey": {
"asm": "0496b538e853519c726a2c91e61ec11600ae1390813a627c66fb8be7947be63c52da7589379515d4e0a604f8141781e62294721166bf621e73a82cbf2342c858ee OP_CHECKSIG",
"hex": "410496b538e853519c726a2c91e61ec11600ae1390813a627c66fb8be7947be63c52da7589379515d4e0a604f8141781e62294721166bf621e73a82cbf2342c858eeac",
"reqSigs": 1,
"type": "pubkey",
"addresses": [
"12c6DSiU4Rq3P4ZxziKxzrL5LmMBrzjrJX"
]
}
}
]
}
```
Before the fix:
```
$ curl -s http://localhost:8332/rest/getutxos/0e3e2357e806b6cdb1f70b54c3a3a17b6714ee1f0e68bebb44a74b1efd512098-0.json | jq
{
"chainHeight": 514109,
"chaintipHash": "0000000000000000001fe76d1445e8a6432fd2de04261dc9c5915311dc7ad6de",
"bitmap": "0",
"utxos": []
}
```
2018-03-18 07:25:30 -07:00
|
|
|
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
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
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_obj = json.loads(json_string)
|
|
|
|
assert_equal(len(json_obj['utxos']), 1) #there should be an outpoint because it was mined
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2014-12-01 03:38:42 -08:00
|
|
|
#do some invalid requests
|
|
|
|
json_request = '{"checkmempool'
|
2015-08-27 11:27:12 -07:00
|
|
|
response = http_post_call(url.hostname, url.port, '/rest/getutxos'+self.FORMAT_SEPARATOR+'json', json_request, True)
|
2017-06-19 15:57:31 -07:00
|
|
|
assert_equal(response.status, 400) #must be a 400 because we send an invalid json request
|
2015-05-27 06:56:16 -07:00
|
|
|
|
2014-12-01 03:38:42 -08:00
|
|
|
json_request = '{"checkmempool'
|
2015-08-27 11:27:12 -07:00
|
|
|
response = http_post_call(url.hostname, url.port, '/rest/getutxos'+self.FORMAT_SEPARATOR+'bin', json_request, True)
|
2017-06-19 15:57:31 -07:00
|
|
|
assert_equal(response.status, 400) #must be a 400 because we send an invalid bin request
|
2015-05-27 06:56:16 -07:00
|
|
|
|
2015-08-27 11:27:12 -07:00
|
|
|
response = http_post_call(url.hostname, url.port, '/rest/getutxos/checkmempool'+self.FORMAT_SEPARATOR+'bin', '', True)
|
2017-06-19 15:57:31 -07:00
|
|
|
assert_equal(response.status, 400) #must be a 400 because we send an invalid bin request
|
2015-05-27 06:56:16 -07:00
|
|
|
|
2014-12-01 03:38:42 -08:00
|
|
|
#test limits
|
2015-05-27 06:56:16 -07:00
|
|
|
json_request = '/checkmempool/'
|
|
|
|
for x in range(0, 20):
|
|
|
|
json_request += txid+'-'+str(n)+'/'
|
|
|
|
json_request = json_request.rstrip("/")
|
2015-08-27 11:27:12 -07:00
|
|
|
response = http_post_call(url.hostname, url.port, '/rest/getutxos'+json_request+self.FORMAT_SEPARATOR+'json', '', True)
|
2016-08-31 16:52:47 -07:00
|
|
|
assert_equal(response.status, 400) #must be a 400 because we exceeding the limits
|
2015-05-27 06:56:16 -07:00
|
|
|
|
|
|
|
json_request = '/checkmempool/'
|
|
|
|
for x in range(0, 15):
|
|
|
|
json_request += txid+'-'+str(n)+'/'
|
2015-12-02 09:12:23 -08:00
|
|
|
json_request = json_request.rstrip("/")
|
2015-08-27 11:27:12 -07:00
|
|
|
response = http_post_call(url.hostname, url.port, '/rest/getutxos'+json_request+self.FORMAT_SEPARATOR+'json', '', True)
|
2016-08-31 16:52:47 -07:00
|
|
|
assert_equal(response.status, 200) #must be a 200 because we are within the limits
|
2014-12-01 03:38:42 -08:00
|
|
|
|
2015-08-08 16:17:27 -07:00
|
|
|
self.nodes[0].generate(1) #generate block to not affect upcoming tests
|
2014-12-01 03:38:42 -08:00
|
|
|
self.sync_all()
|
2015-05-27 06:56:16 -07:00
|
|
|
|
2014-12-01 03:38:42 -08:00
|
|
|
################
|
|
|
|
# /rest/block/ #
|
|
|
|
################
|
2015-05-27 06:56:16 -07:00
|
|
|
|
2014-11-26 07:26:02 -08:00
|
|
|
# check binary format
|
2015-08-27 11:27:12 -07:00
|
|
|
response = http_get_call(url.hostname, url.port, '/rest/block/'+bb_hash+self.FORMAT_SEPARATOR+"bin", True)
|
2014-11-26 07:26:02 -08:00
|
|
|
assert_equal(response.status, 200)
|
2014-12-08 04:44:49 -08:00
|
|
|
assert_greater_than(int(response.getheader('content-length')), 80)
|
|
|
|
response_str = response.read()
|
|
|
|
|
|
|
|
# compare with block header
|
2015-08-27 11:27:12 -07:00
|
|
|
response_header = http_get_call(url.hostname, url.port, '/rest/headers/1/'+bb_hash+self.FORMAT_SEPARATOR+"bin", True)
|
2014-12-08 04:44:49 -08:00
|
|
|
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
|
2015-08-27 11:27:12 -07:00
|
|
|
response_hex = http_get_call(url.hostname, url.port, '/rest/block/'+bb_hash+self.FORMAT_SEPARATOR+"hex", True)
|
2014-12-08 04:44:49 -08:00
|
|
|
assert_equal(response_hex.status, 200)
|
|
|
|
assert_greater_than(int(response_hex.getheader('content-length')), 160)
|
|
|
|
response_hex_str = response_hex.read()
|
2016-04-10 07:54:28 -07:00
|
|
|
assert_equal(encode(response_str, "hex_codec")[0:160], response_hex_str[0:160])
|
2014-12-08 04:44:49 -08:00
|
|
|
|
|
|
|
# compare with hex block header
|
2015-08-27 11:27:12 -07:00
|
|
|
response_header_hex = http_get_call(url.hostname, url.port, '/rest/headers/1/'+bb_hash+self.FORMAT_SEPARATOR+"hex", True)
|
2014-12-08 04:44:49 -08:00
|
|
|
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])
|
2016-04-10 07:54:28 -07:00
|
|
|
assert_equal(encode(response_header_str, "hex_codec")[0:160], response_header_hex_str[0:160])
|
2014-12-08 04:44:49 -08:00
|
|
|
|
2014-11-26 07:26:02 -08:00
|
|
|
# check json format
|
2014-12-16 01:50:44 -08:00
|
|
|
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
|
2015-08-27 11:27:12 -07:00
|
|
|
response_header_json = http_get_call(url.hostname, url.port, '/rest/headers/1/'+bb_hash+self.FORMAT_SEPARATOR+"json", True)
|
2014-12-16 01:50:44 -08:00
|
|
|
assert_equal(response_header_json.status, 200)
|
2016-04-10 07:54:28 -07:00
|
|
|
response_header_json_str = response_header_json.read().decode('utf-8')
|
2015-12-02 09:12:23 -08:00
|
|
|
json_obj = json.loads(response_header_json_str, parse_float=Decimal)
|
2014-12-16 01:50:44 -08:00
|
|
|
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()
|
2015-08-27 11:27:12 -07:00
|
|
|
response_header_json = http_get_call(url.hostname, url.port, '/rest/headers/5/'+bb_hash+self.FORMAT_SEPARATOR+"json", True)
|
2014-12-16 01:50:44 -08:00
|
|
|
assert_equal(response_header_json.status, 200)
|
2016-04-10 07:54:28 -07:00
|
|
|
response_header_json_str = response_header_json.read().decode('utf-8')
|
2014-12-16 01:50:44 -08:00
|
|
|
json_obj = json.loads(response_header_json_str)
|
|
|
|
assert_equal(len(json_obj), 5) #now we should have 5 header objects
|
2014-12-08 04:44:49 -08:00
|
|
|
|
2014-11-26 07:26:02 -08:00
|
|
|
# do tx test
|
2015-12-02 09:12:23 -08:00
|
|
|
tx_hash = block_json_obj['tx'][0]['txid']
|
2014-11-26 07:26:02 -08:00
|
|
|
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)
|
2014-12-08 04:44:49 -08:00
|
|
|
|
2014-11-26 07:26:02 -08:00
|
|
|
# check hex format response
|
2015-08-27 11:27:12 -07:00
|
|
|
hex_string = http_get_call(url.hostname, url.port, '/rest/tx/'+tx_hash+self.FORMAT_SEPARATOR+"hex", True)
|
2014-12-27 04:18:36 -08:00
|
|
|
assert_equal(hex_string.status, 200)
|
2014-11-26 07:26:02 -08:00
|
|
|
assert_greater_than(int(response.getheader('content-length')), 10)
|
2015-05-27 06:56:16 -07:00
|
|
|
|
2014-12-01 03:38:42 -08:00
|
|
|
|
2014-11-28 11:32:52 -08:00
|
|
|
# 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()
|
2014-12-08 04:44:49 -08:00
|
|
|
|
2015-08-06 10:38:19 -07:00
|
|
|
# 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)
|
2018-02-18 07:54:20 -08:00
|
|
|
for i, tx in enumerate(txs):
|
2015-08-06 10:38:19 -07:00
|
|
|
assert_equal(tx in json_obj, True)
|
2018-02-18 07:54:20 -08:00
|
|
|
assert_equal(json_obj[tx]['spentby'], txs[i+1:i+2])
|
|
|
|
assert_equal(json_obj[tx]['depends'], txs[i-1:i])
|
2015-08-06 10:38:19 -07:00
|
|
|
|
2014-11-28 11:32:52 -08:00
|
|
|
# now mine the transactions
|
2015-03-31 20:28:28 -07:00
|
|
|
newblockhash = self.nodes[1].generate(1)
|
2014-11-28 11:32:52 -08:00
|
|
|
self.sync_all()
|
2014-12-08 04:44:49 -08:00
|
|
|
|
2014-11-28 11:32:52 -08:00
|
|
|
#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)
|
2014-12-08 04:44:49 -08:00
|
|
|
|
2014-11-28 11:32:52 -08:00
|
|
|
#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)
|
2014-11-26 07:26:02 -08:00
|
|
|
|
2014-12-27 04:18:36 -08:00
|
|
|
#test rest bestblock
|
|
|
|
bb_hash = self.nodes[0].getbestblockhash()
|
2015-05-27 06:56:16 -07:00
|
|
|
|
2014-12-27 04:18:36 -08:00
|
|
|
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)
|
|
|
|
|
2014-11-26 07:26:02 -08:00
|
|
|
if __name__ == '__main__':
|
2014-12-02 06:57:20 -08:00
|
|
|
RESTTest ().main ()
|