2016-03-19 20:58:06 +01:00
|
|
|
#!/usr/bin/env python3
|
2019-02-21 02:03:13 +01:00
|
|
|
# Copyright (c) 2014-2019 The Bitcoin Core developers
|
2014-11-26 16:26:02 +01:00
|
|
|
# Distributed under the MIT software license, see the accompanying
|
|
|
|
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
2017-01-18 00:34:40 +01:00
|
|
|
"""Test the REST API."""
|
2018-03-23 10:39:54 +01:00
|
|
|
|
2018-03-22 17:36:37 +01:00
|
|
|
from decimal import Decimal
|
2018-03-22 21:37:09 +01:00
|
|
|
from enum import Enum
|
2016-03-19 21:36:32 +01:00
|
|
|
from io import BytesIO
|
2018-03-22 17:36:37 +01:00
|
|
|
import json
|
|
|
|
from struct import pack, unpack
|
2014-11-26 16:26:02 +01:00
|
|
|
|
2016-03-19 20:58:06 +01:00
|
|
|
import http.client
|
|
|
|
import urllib.parse
|
2014-11-26 16:26:02 +01:00
|
|
|
|
2018-03-22 17:36:37 +01:00
|
|
|
from test_framework.test_framework import BitcoinTestFramework
|
|
|
|
from test_framework.util import (
|
|
|
|
assert_equal,
|
|
|
|
assert_greater_than,
|
2018-03-23 10:39:54 +01:00
|
|
|
assert_greater_than_or_equal,
|
2018-03-22 17:36:37 +01:00
|
|
|
)
|
|
|
|
|
2019-01-23 16:44:13 +01:00
|
|
|
from test_framework.messages import BLOCK_HEADER_SIZE
|
|
|
|
|
2018-03-22 21:37:09 +01:00
|
|
|
class ReqType(Enum):
|
|
|
|
JSON = 1
|
|
|
|
BIN = 2
|
|
|
|
HEX = 3
|
2015-08-27 20:27:12 +02:00
|
|
|
|
2018-03-22 21:37:09 +01:00
|
|
|
class RetType(Enum):
|
|
|
|
OBJ = 1
|
|
|
|
BYTES = 2
|
|
|
|
JSON = 3
|
2015-08-27 20:27:12 +02:00
|
|
|
|
2018-03-22 21:37:09 +01:00
|
|
|
def filter_output_indices_by_value(vouts, value):
|
|
|
|
for vout in vouts:
|
|
|
|
if vout['value'] == value:
|
|
|
|
yield vout['n']
|
2014-11-26 16:26:02 +01:00
|
|
|
|
|
|
|
class RESTTest (BitcoinTestFramework):
|
2017-06-10 00:21:21 +02:00
|
|
|
def set_test_params(self):
|
2016-05-14 13:01:31 +02:00
|
|
|
self.setup_clean_chain = True
|
2018-03-22 21:41:22 +01:00
|
|
|
self.num_nodes = 2
|
2019-02-14 04:22:40 +01:00
|
|
|
self.extra_args = [["-rest"], []]
|
2019-12-06 15:37:49 +01:00
|
|
|
self.supports_cli = False
|
2018-03-22 21:37:09 +01:00
|
|
|
|
2018-09-09 19:32:37 +02:00
|
|
|
def skip_test_if_missing_module(self):
|
|
|
|
self.skip_if_no_wallet()
|
|
|
|
|
2018-03-22 21:37:09 +01:00
|
|
|
def test_rest_request(self, uri, http_method='GET', req_type=ReqType.JSON, body='', status=200, ret_type=RetType.JSON):
|
|
|
|
rest_uri = '/rest' + uri
|
|
|
|
if req_type == ReqType.JSON:
|
|
|
|
rest_uri += '.json'
|
|
|
|
elif req_type == ReqType.BIN:
|
|
|
|
rest_uri += '.bin'
|
|
|
|
elif req_type == ReqType.HEX:
|
|
|
|
rest_uri += '.hex'
|
|
|
|
|
|
|
|
conn = http.client.HTTPConnection(self.url.hostname, self.url.port)
|
2021-06-12 08:26:31 +02:00
|
|
|
self.log.debug(f'{http_method} {rest_uri} {body}')
|
2018-03-22 21:37:09 +01:00
|
|
|
if http_method == 'GET':
|
|
|
|
conn.request('GET', rest_uri)
|
|
|
|
elif http_method == 'POST':
|
|
|
|
conn.request('POST', rest_uri, body)
|
|
|
|
resp = conn.getresponse()
|
|
|
|
|
|
|
|
assert_equal(resp.status, status)
|
|
|
|
|
|
|
|
if ret_type == RetType.OBJ:
|
|
|
|
return resp
|
|
|
|
elif ret_type == RetType.BYTES:
|
|
|
|
return resp.read()
|
|
|
|
elif ret_type == RetType.JSON:
|
|
|
|
return json.loads(resp.read().decode('utf-8'), parse_float=Decimal)
|
2015-05-27 15:56:16 +02:00
|
|
|
|
2014-11-26 16:26:02 +01:00
|
|
|
def run_test(self):
|
2018-03-22 21:41:22 +01:00
|
|
|
self.url = urllib.parse.urlparse(self.nodes[0].url)
|
2018-03-22 18:24:37 +01:00
|
|
|
self.log.info("Mine blocks and send Bitcoin to node 1")
|
2015-05-27 15:56:16 +02:00
|
|
|
|
2018-03-22 21:41:22 +01:00
|
|
|
# Random address so node1's balance doesn't increase
|
|
|
|
not_related_address = "2MxqoHEdNQTyYeX1mHcbrrpzgojbosTpCvJ"
|
|
|
|
|
2021-08-19 17:10:24 +02:00
|
|
|
self.generate(self.nodes[0], 1)
|
2014-12-01 12:38:42 +01:00
|
|
|
self.sync_all()
|
2021-08-19 17:10:24 +02:00
|
|
|
self.generatetoaddress(self.nodes[1], 100, not_related_address)
|
2014-12-01 12:38:42 +01:00
|
|
|
self.sync_all()
|
2015-05-27 15:56:16 +02:00
|
|
|
|
2014-12-01 12:38:42 +01:00
|
|
|
assert_equal(self.nodes[0].getbalance(), 50)
|
2015-05-27 15:56:16 +02:00
|
|
|
|
2014-12-01 12:38:42 +01:00
|
|
|
txid = self.nodes[0].sendtoaddress(self.nodes[1].getnewaddress(), 0.1)
|
|
|
|
self.sync_all()
|
2015-05-27 15:56:16 +02:00
|
|
|
|
2019-02-14 04:22:40 +01:00
|
|
|
self.log.info("Test the /tx URI")
|
2015-05-27 15:56:16 +02:00
|
|
|
|
2021-06-12 08:26:31 +02:00
|
|
|
json_obj = self.test_rest_request(f"/tx/{txid}")
|
2019-02-14 04:22:40 +01:00
|
|
|
assert_equal(json_obj['txid'], txid)
|
|
|
|
|
|
|
|
# Check hex format response
|
2021-06-12 08:26:31 +02:00
|
|
|
hex_response = self.test_rest_request(f"/tx/{txid}", req_type=ReqType.HEX, ret_type=RetType.OBJ)
|
2019-02-14 04:22:40 +01:00
|
|
|
assert_greater_than_or_equal(int(hex_response.getheader('content-length')),
|
|
|
|
json_obj['size']*2)
|
|
|
|
|
2018-03-22 21:37:09 +01:00
|
|
|
spent = (json_obj['vin'][0]['txid'], json_obj['vin'][0]['vout']) # get the vin to later check for utxo (should be spent by then)
|
2015-05-27 15:56:16 +02:00
|
|
|
# get n of 0.1 outpoint
|
2018-03-22 21:37:09 +01:00
|
|
|
n, = filter_output_indices_by_value(json_obj['vout'], Decimal('0.1'))
|
|
|
|
spending = (txid, n)
|
2015-05-27 15:56:16 +02:00
|
|
|
|
2018-03-22 18:24:37 +01:00
|
|
|
self.log.info("Query an unspent TXO using the /getutxos URI")
|
|
|
|
|
2021-08-19 17:10:24 +02:00
|
|
|
self.generatetoaddress(self.nodes[1], 1, not_related_address)
|
2019-02-14 04:22:40 +01:00
|
|
|
self.sync_all()
|
|
|
|
bb_hash = self.nodes[0].getbestblockhash()
|
|
|
|
|
|
|
|
assert_equal(self.nodes[1].getbalance(), Decimal("0.1"))
|
2015-05-27 15:56:16 +02:00
|
|
|
|
2018-03-22 17:36:37 +01:00
|
|
|
# Check chainTip response
|
2021-06-12 08:26:31 +02:00
|
|
|
json_obj = self.test_rest_request(f"/getutxos/{spending[0]}-{spending[1]}")
|
2014-12-01 12:38:42 +01:00
|
|
|
assert_equal(json_obj['chaintipHash'], bb_hash)
|
2015-05-27 15:56:16 +02:00
|
|
|
|
2018-03-22 17:36:37 +01:00
|
|
|
# Make sure there is one utxo
|
2014-12-01 12:38:42 +01:00
|
|
|
assert_equal(len(json_obj['utxos']), 1)
|
2018-03-22 21:37:09 +01:00
|
|
|
assert_equal(json_obj['utxos'][0]['value'], Decimal('0.1'))
|
2015-05-27 15:56:16 +02:00
|
|
|
|
2018-03-22 18:24:37 +01:00
|
|
|
self.log.info("Query a spent TXO using the /getutxos URI")
|
|
|
|
|
2021-06-12 08:26:31 +02:00
|
|
|
json_obj = self.test_rest_request(f"/getutxos/{spent[0]}-{spent[1]}")
|
2015-05-27 15:56:16 +02:00
|
|
|
|
2018-03-22 17:36:37 +01:00
|
|
|
# Check chainTip response
|
2014-12-01 12:38:42 +01:00
|
|
|
assert_equal(json_obj['chaintipHash'], bb_hash)
|
2014-12-08 13:44:49 +01:00
|
|
|
|
2018-03-22 21:37:09 +01:00
|
|
|
# Make sure there is no utxo in the response because this outpoint has been spent
|
2014-12-01 12:38:42 +01:00
|
|
|
assert_equal(len(json_obj['utxos']), 0)
|
2015-05-27 15:56:16 +02:00
|
|
|
|
2018-03-22 17:36:37 +01:00
|
|
|
# Check bitmap
|
2014-12-01 12:38:42 +01:00
|
|
|
assert_equal(json_obj['bitmap'], "0")
|
2015-05-27 15:56:16 +02:00
|
|
|
|
2018-03-22 18:24:37 +01:00
|
|
|
self.log.info("Query two TXOs using the /getutxos URI")
|
|
|
|
|
2021-06-12 08:26:31 +02:00
|
|
|
json_obj = self.test_rest_request(f"/getutxos/{spending[0]}-{spending[1]}/{spent[0]}-{spent[1]}")
|
2018-03-22 21:37:09 +01:00
|
|
|
|
2014-12-01 12:38:42 +01:00
|
|
|
assert_equal(len(json_obj['utxos']), 1)
|
|
|
|
assert_equal(json_obj['bitmap'], "10")
|
2015-05-27 15:56:16 +02:00
|
|
|
|
2018-03-22 18:24:37 +01:00
|
|
|
self.log.info("Query the TXOs using the /getutxos URI with a binary response")
|
|
|
|
|
2018-03-22 17:36:37 +01:00
|
|
|
bin_request = b'\x01\x02'
|
2018-03-22 21:37:09 +01:00
|
|
|
for txid, n in [spending, spent]:
|
2021-07-31 21:23:16 +02:00
|
|
|
bin_request += bytes.fromhex(txid)
|
2018-03-22 21:37:09 +01:00
|
|
|
bin_request += pack("i", n)
|
2015-05-27 15:56:16 +02:00
|
|
|
|
2018-03-22 21:37:09 +01:00
|
|
|
bin_response = self.test_rest_request("/getutxos", http_method='POST', req_type=ReqType.BIN, body=bin_request, ret_type=RetType.BYTES)
|
2018-03-23 10:39:54 +01:00
|
|
|
output = BytesIO(bin_response)
|
2019-11-25 21:30:01 +01:00
|
|
|
chain_height, = unpack("<i", output.read(4))
|
2019-02-18 16:35:48 +01:00
|
|
|
response_hash = output.read(32)[::-1].hex()
|
2015-05-27 15:56:16 +02:00
|
|
|
|
2018-03-22 17:36:37 +01:00
|
|
|
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
|
2015-05-27 15:56:16 +02:00
|
|
|
|
2018-03-22 18:24:37 +01:00
|
|
|
self.log.info("Test the /getutxos URI with and without /checkmempool")
|
|
|
|
# Create a transaction, check that it's found with /checkmempool, but
|
|
|
|
# not found without. Then confirm the transaction and check that it's
|
|
|
|
# found with or without /checkmempool.
|
2014-12-01 12:38:42 +01:00
|
|
|
|
|
|
|
# do a tx and don't sync
|
|
|
|
txid = self.nodes[0].sendtoaddress(self.nodes[1].getnewaddress(), 0.1)
|
2021-06-12 08:26:31 +02:00
|
|
|
json_obj = self.test_rest_request(f"/tx/{txid}")
|
[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 15:25:30 +01:00
|
|
|
# get the spent output to later check for utxo (should be spent by then)
|
2018-03-22 21:37:09 +01:00
|
|
|
spent = (json_obj['vin'][0]['txid'], json_obj['vin'][0]['vout'])
|
2015-05-27 15:56:16 +02:00
|
|
|
# get n of 0.1 outpoint
|
2018-03-22 21:37:09 +01:00
|
|
|
n, = filter_output_indices_by_value(json_obj['vout'], Decimal('0.1'))
|
|
|
|
spending = (txid, n)
|
|
|
|
|
2021-06-12 08:26:31 +02:00
|
|
|
json_obj = self.test_rest_request(f"/getutxos/{spending[0]}-{spending[1]}")
|
2018-03-22 18:24:37 +01:00
|
|
|
assert_equal(len(json_obj['utxos']), 0)
|
2015-05-27 15:56:16 +02:00
|
|
|
|
2021-06-12 08:26:31 +02:00
|
|
|
json_obj = self.test_rest_request(f"/getutxos/checkmempool/{spending[0]}-{spending[1]}")
|
2018-03-22 18:24:37 +01:00
|
|
|
assert_equal(len(json_obj['utxos']), 1)
|
2015-05-27 15:56:16 +02:00
|
|
|
|
2021-06-12 08:26:31 +02:00
|
|
|
json_obj = self.test_rest_request(f"/getutxos/{spent[0]}-{spent[1]}")
|
2018-03-22 18:24:37 +01:00
|
|
|
assert_equal(len(json_obj['utxos']), 1)
|
[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 15:25:30 +01:00
|
|
|
|
2021-06-12 08:26:31 +02:00
|
|
|
json_obj = self.test_rest_request(f"/getutxos/checkmempool/{spent[0]}-{spent[1]}")
|
2018-03-22 18:24:37 +01:00
|
|
|
assert_equal(len(json_obj['utxos']), 0)
|
[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 15:25:30 +01:00
|
|
|
|
2021-08-19 17:10:24 +02:00
|
|
|
self.generate(self.nodes[0], 1)
|
[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 15:25:30 +01:00
|
|
|
self.sync_all()
|
|
|
|
|
2021-06-12 08:26:31 +02:00
|
|
|
json_obj = self.test_rest_request(f"/getutxos/{spending[0]}-{spending[1]}")
|
2018-03-22 18:24:37 +01:00
|
|
|
assert_equal(len(json_obj['utxos']), 1)
|
[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 15:25:30 +01:00
|
|
|
|
2021-06-12 08:26:31 +02:00
|
|
|
json_obj = self.test_rest_request(f"/getutxos/checkmempool/{spending[0]}-{spending[1]}")
|
2018-03-22 18:24:37 +01:00
|
|
|
assert_equal(len(json_obj['utxos']), 1)
|
[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 15:25:30 +01:00
|
|
|
|
2018-03-22 17:36:37 +01:00
|
|
|
# Do some invalid requests
|
2018-03-22 21:37:09 +01:00
|
|
|
self.test_rest_request("/getutxos", http_method='POST', req_type=ReqType.JSON, body='{"checkmempool', status=400, ret_type=RetType.OBJ)
|
|
|
|
self.test_rest_request("/getutxos", http_method='POST', req_type=ReqType.BIN, body='{"checkmempool', status=400, ret_type=RetType.OBJ)
|
|
|
|
self.test_rest_request("/getutxos/checkmempool", http_method='POST', req_type=ReqType.JSON, status=400, ret_type=RetType.OBJ)
|
2015-05-27 15:56:16 +02:00
|
|
|
|
2018-03-22 17:36:37 +01:00
|
|
|
# Test limits
|
2021-06-12 08:26:31 +02:00
|
|
|
long_uri = '/'.join([f"{txid}-{n_}" for n_ in range(20)])
|
|
|
|
self.test_rest_request(f"/getutxos/checkmempool/{long_uri}", http_method='POST', status=400, ret_type=RetType.OBJ)
|
2015-05-27 15:56:16 +02:00
|
|
|
|
2021-06-12 08:26:31 +02:00
|
|
|
long_uri = '/'.join([f'{txid}-{n_}' for n_ in range(15)])
|
|
|
|
self.test_rest_request(f"/getutxos/checkmempool/{long_uri}", http_method='POST', status=200)
|
2014-12-01 12:38:42 +01:00
|
|
|
|
2021-08-19 17:10:24 +02:00
|
|
|
self.generate(self.nodes[0], 1) # generate block to not affect upcoming tests
|
2014-12-01 12:38:42 +01:00
|
|
|
self.sync_all()
|
2015-05-27 15:56:16 +02:00
|
|
|
|
2018-09-29 21:44:40 +02:00
|
|
|
self.log.info("Test the /block, /blockhashbyheight and /headers URIs")
|
2018-03-23 10:39:54 +01:00
|
|
|
bb_hash = self.nodes[0].getbestblockhash()
|
2015-05-27 15:56:16 +02:00
|
|
|
|
2019-01-16 15:46:09 +01:00
|
|
|
# Check result if block does not exists
|
|
|
|
assert_equal(self.test_rest_request('/headers/1/0000000000000000000000000000000000000000000000000000000000000000'), [])
|
|
|
|
self.test_rest_request('/block/0000000000000000000000000000000000000000000000000000000000000000', status=404, ret_type=RetType.OBJ)
|
|
|
|
|
|
|
|
# Check result if block is not in the active chain
|
|
|
|
self.nodes[0].invalidateblock(bb_hash)
|
2021-06-12 08:26:31 +02:00
|
|
|
assert_equal(self.test_rest_request(f'/headers/1/{bb_hash}'), [])
|
|
|
|
self.test_rest_request(f'/block/{bb_hash}')
|
2019-01-16 15:46:09 +01:00
|
|
|
self.nodes[0].reconsiderblock(bb_hash)
|
|
|
|
|
2018-03-22 17:36:37 +01:00
|
|
|
# Check binary format
|
2021-06-12 08:26:31 +02:00
|
|
|
response = self.test_rest_request(f"/block/{bb_hash}", req_type=ReqType.BIN, ret_type=RetType.OBJ)
|
2019-01-23 16:44:13 +01:00
|
|
|
assert_greater_than(int(response.getheader('content-length')), BLOCK_HEADER_SIZE)
|
2018-03-23 10:39:54 +01:00
|
|
|
response_bytes = response.read()
|
2014-12-08 13:44:49 +01:00
|
|
|
|
2018-03-22 17:36:37 +01:00
|
|
|
# Compare with block header
|
2021-06-12 08:26:31 +02:00
|
|
|
response_header = self.test_rest_request(f"/headers/1/{bb_hash}", req_type=ReqType.BIN, ret_type=RetType.OBJ)
|
2019-01-23 16:44:13 +01:00
|
|
|
assert_equal(int(response_header.getheader('content-length')), BLOCK_HEADER_SIZE)
|
2018-03-23 10:39:54 +01:00
|
|
|
response_header_bytes = response_header.read()
|
2019-01-23 16:44:13 +01:00
|
|
|
assert_equal(response_bytes[:BLOCK_HEADER_SIZE], response_header_bytes)
|
2014-12-08 13:44:49 +01:00
|
|
|
|
2018-03-22 17:36:37 +01:00
|
|
|
# Check block hex format
|
2021-06-12 08:26:31 +02:00
|
|
|
response_hex = self.test_rest_request(f"/block/{bb_hash}", req_type=ReqType.HEX, ret_type=RetType.OBJ)
|
2019-01-23 16:44:13 +01:00
|
|
|
assert_greater_than(int(response_hex.getheader('content-length')), BLOCK_HEADER_SIZE*2)
|
2018-03-23 10:39:54 +01:00
|
|
|
response_hex_bytes = response_hex.read().strip(b'\n')
|
2021-08-04 20:59:06 +02:00
|
|
|
assert_equal(response_bytes.hex().encode(), response_hex_bytes)
|
2014-12-08 13:44:49 +01:00
|
|
|
|
2018-03-22 17:36:37 +01:00
|
|
|
# Compare with hex block header
|
2021-06-12 08:26:31 +02:00
|
|
|
response_header_hex = self.test_rest_request(f"/headers/1/{bb_hash}", req_type=ReqType.HEX, ret_type=RetType.OBJ)
|
2019-01-23 16:44:13 +01:00
|
|
|
assert_greater_than(int(response_header_hex.getheader('content-length')), BLOCK_HEADER_SIZE*2)
|
|
|
|
response_header_hex_bytes = response_header_hex.read(BLOCK_HEADER_SIZE*2)
|
2021-08-04 20:59:06 +02:00
|
|
|
assert_equal(response_bytes[:BLOCK_HEADER_SIZE].hex().encode(), response_header_hex_bytes)
|
2014-12-08 13:44:49 +01:00
|
|
|
|
2018-03-22 17:36:37 +01:00
|
|
|
# Check json format
|
2021-06-12 08:26:31 +02:00
|
|
|
block_json_obj = self.test_rest_request(f"/block/{bb_hash}")
|
2014-12-16 10:50:44 +01:00
|
|
|
assert_equal(block_json_obj['hash'], bb_hash)
|
2021-06-12 08:26:31 +02:00
|
|
|
assert_equal(self.test_rest_request(f"/blockhashbyheight/{block_json_obj['height']}")['blockhash'], bb_hash)
|
2018-09-29 21:44:40 +02:00
|
|
|
|
|
|
|
# Check hex/bin format
|
2021-06-12 08:26:31 +02:00
|
|
|
resp_hex = self.test_rest_request(f"/blockhashbyheight/{block_json_obj['height']}", req_type=ReqType.HEX, ret_type=RetType.OBJ)
|
2018-09-29 21:44:40 +02:00
|
|
|
assert_equal(resp_hex.read().decode('utf-8').rstrip(), bb_hash)
|
2021-06-12 08:26:31 +02:00
|
|
|
resp_bytes = self.test_rest_request(f"/blockhashbyheight/{block_json_obj['height']}", req_type=ReqType.BIN, ret_type=RetType.BYTES)
|
2019-02-18 16:35:48 +01:00
|
|
|
blockhash = resp_bytes[::-1].hex()
|
2018-09-29 21:44:40 +02:00
|
|
|
assert_equal(blockhash, bb_hash)
|
|
|
|
|
|
|
|
# Check invalid blockhashbyheight requests
|
|
|
|
resp = self.test_rest_request("/blockhashbyheight/abc", ret_type=RetType.OBJ, status=400)
|
|
|
|
assert_equal(resp.read().decode('utf-8').rstrip(), "Invalid height: abc")
|
|
|
|
resp = self.test_rest_request("/blockhashbyheight/1000000", ret_type=RetType.OBJ, status=404)
|
|
|
|
assert_equal(resp.read().decode('utf-8').rstrip(), "Block height out of range")
|
|
|
|
resp = self.test_rest_request("/blockhashbyheight/-1", ret_type=RetType.OBJ, status=400)
|
|
|
|
assert_equal(resp.read().decode('utf-8').rstrip(), "Invalid height: -1")
|
|
|
|
self.test_rest_request("/blockhashbyheight/", ret_type=RetType.OBJ, status=400)
|
2014-12-16 10:50:44 +01:00
|
|
|
|
2018-03-22 17:36:37 +01:00
|
|
|
# Compare with json block header
|
2021-06-12 08:26:31 +02:00
|
|
|
json_obj = self.test_rest_request(f"/headers/1/{bb_hash}")
|
2018-03-22 17:36:37 +01: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
|
2014-12-16 10:50:44 +01:00
|
|
|
|
2018-03-22 17:36:37 +01:00
|
|
|
# Compare with normal RPC block response
|
2014-12-16 10:50:44 +01:00
|
|
|
rpc_block_json = self.nodes[0].getblock(bb_hash)
|
2018-03-22 17:36:37 +01:00
|
|
|
for key in ['hash', 'confirmations', 'height', 'version', 'merkleroot', 'time', 'nonce', 'bits', 'difficulty', 'chainwork', 'previousblockhash']:
|
|
|
|
assert_equal(json_obj[0][key], rpc_block_json[key])
|
|
|
|
|
|
|
|
# See if we can get 5 headers in one response
|
2021-08-19 17:10:24 +02:00
|
|
|
self.generate(self.nodes[1], 5)
|
2014-12-16 10:50:44 +01:00
|
|
|
self.sync_all()
|
2021-06-12 08:26:31 +02:00
|
|
|
json_obj = self.test_rest_request(f"/headers/5/{bb_hash}")
|
2018-03-22 17:36:37 +01:00
|
|
|
assert_equal(len(json_obj), 5) # now we should have 5 header objects
|
2014-12-08 13:44:49 +01:00
|
|
|
|
2018-03-22 18:24:37 +01:00
|
|
|
self.log.info("Test tx inclusion in the /mempool and /block URIs")
|
|
|
|
|
|
|
|
# Make 3 tx and mine them on node 1
|
2014-11-28 20:32:52 +01:00
|
|
|
txs = []
|
2018-03-22 21:41:22 +01:00
|
|
|
txs.append(self.nodes[0].sendtoaddress(not_related_address, 11))
|
|
|
|
txs.append(self.nodes[0].sendtoaddress(not_related_address, 11))
|
|
|
|
txs.append(self.nodes[0].sendtoaddress(not_related_address, 11))
|
2014-11-28 20:32:52 +01:00
|
|
|
self.sync_all()
|
2014-12-08 13:44:49 +01:00
|
|
|
|
2018-03-22 17:36:37 +01:00
|
|
|
# Check that there are exactly 3 transactions in the TX memory pool before generating the block
|
2018-03-22 21:37:09 +01:00
|
|
|
json_obj = self.test_rest_request("/mempool/info")
|
2015-08-06 19:38:19 +02:00
|
|
|
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)
|
|
|
|
|
2018-03-22 17:36:37 +01:00
|
|
|
# Check that there are our submitted transactions in the TX memory pool
|
2018-03-22 21:37:09 +01:00
|
|
|
json_obj = self.test_rest_request("/mempool/contents")
|
2018-02-18 16:54:20 +01:00
|
|
|
for i, tx in enumerate(txs):
|
2018-03-22 21:37:09 +01:00
|
|
|
assert tx in json_obj
|
2018-03-22 17:36:37 +01: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 19:38:19 +02:00
|
|
|
|
2018-03-22 17:36:37 +01:00
|
|
|
# Now mine the transactions
|
2021-08-19 17:10:24 +02:00
|
|
|
newblockhash = self.generate(self.nodes[1], 1)
|
2014-11-28 20:32:52 +01:00
|
|
|
self.sync_all()
|
2014-12-08 13:44:49 +01:00
|
|
|
|
2018-03-22 17:36:37 +01:00
|
|
|
# Check if the 3 tx show up in the new block
|
2021-06-12 08:26:31 +02:00
|
|
|
json_obj = self.test_rest_request(f"/block/{newblockhash[0]}")
|
2018-03-22 21:37:09 +01:00
|
|
|
non_coinbase_txs = {tx['txid'] for tx in json_obj['tx']
|
|
|
|
if 'coinbase' not in tx['vin'][0]}
|
|
|
|
assert_equal(non_coinbase_txs, set(txs))
|
2014-12-08 13:44:49 +01:00
|
|
|
|
2021-02-27 19:18:41 +01:00
|
|
|
# Verify that the non-coinbase tx has "prevout" key set
|
|
|
|
for tx_obj in json_obj["tx"]:
|
|
|
|
for vin in tx_obj["vin"]:
|
|
|
|
if "coinbase" not in vin:
|
|
|
|
assert "prevout" in vin
|
|
|
|
assert_equal(vin["prevout"]["generated"], False)
|
|
|
|
else:
|
|
|
|
assert "prevout" not in vin
|
|
|
|
|
2018-03-22 17:36:37 +01:00
|
|
|
# Check the same but without tx details
|
2021-06-12 08:26:31 +02:00
|
|
|
json_obj = self.test_rest_request(f"/block/notxdetails/{newblockhash[0]}")
|
2014-11-28 20:32:52 +01:00
|
|
|
for tx in txs:
|
2018-03-22 21:37:09 +01:00
|
|
|
assert tx in json_obj['tx']
|
2014-11-26 16:26:02 +01:00
|
|
|
|
2018-03-22 18:24:37 +01:00
|
|
|
self.log.info("Test the /chaininfo URI")
|
|
|
|
|
2014-12-27 13:18:36 +01:00
|
|
|
bb_hash = self.nodes[0].getbestblockhash()
|
2015-05-27 15:56:16 +02:00
|
|
|
|
2018-03-22 21:37:09 +01:00
|
|
|
json_obj = self.test_rest_request("/chaininfo")
|
2014-12-27 13:18:36 +01:00
|
|
|
assert_equal(json_obj['bestblockhash'], bb_hash)
|
|
|
|
|
2014-11-26 16:26:02 +01:00
|
|
|
if __name__ == '__main__':
|
2018-03-22 17:36:37 +01:00
|
|
|
RESTTest().main()
|