2016-05-06 11:23:48 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# Copyright (c) 2014-2016 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.
|
2019-01-07 10:55:35 +01:00
|
|
|
"""Test the REST API."""
|
2015-05-02 12:53:35 +02:00
|
|
|
|
2021-04-08 22:35:33 +02:00
|
|
|
import binascii
|
|
|
|
from decimal import Decimal
|
|
|
|
from enum import Enum
|
2016-03-19 21:36:32 +01:00
|
|
|
from io import BytesIO
|
2021-04-08 22:35:33 +02:00
|
|
|
import json
|
|
|
|
from struct import pack, unpack
|
2014-11-26 16:26:02 +01:00
|
|
|
|
2016-05-06 11:23:48 +02:00
|
|
|
import http.client
|
|
|
|
import urllib.parse
|
2014-11-26 16:26:02 +01:00
|
|
|
|
2021-04-08 22:35:33 +02:00
|
|
|
from test_framework.test_framework import BitcoinTestFramework
|
|
|
|
from test_framework.util import (
|
|
|
|
assert_equal,
|
|
|
|
assert_greater_than,
|
|
|
|
assert_greater_than_or_equal,
|
|
|
|
hex_str_to_bytes,
|
|
|
|
)
|
|
|
|
|
|
|
|
class ReqType(Enum):
|
|
|
|
JSON = 1
|
|
|
|
BIN = 2
|
|
|
|
HEX = 3
|
|
|
|
|
|
|
|
class RetType(Enum):
|
|
|
|
OBJ = 1
|
|
|
|
BYTES = 2
|
|
|
|
JSON = 3
|
|
|
|
|
|
|
|
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-09-01 18:47:13 +02:00
|
|
|
def set_test_params(self):
|
2016-05-20 15:16:51 +02:00
|
|
|
self.setup_clean_chain = True
|
2021-04-08 22:35:33 +02:00
|
|
|
self.num_nodes = 2
|
|
|
|
self.extra_args = [["-rest"], []]
|
|
|
|
|
|
|
|
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)
|
|
|
|
self.log.debug('%s %s %s', http_method, rest_uri, body)
|
|
|
|
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):
|
2021-04-08 22:35:33 +02:00
|
|
|
self.url = urllib.parse.urlparse(self.nodes[0].url)
|
|
|
|
self.log.info("Mine blocks and send Dash to node 1")
|
|
|
|
|
|
|
|
# Random address so node1's balance doesn't increase
|
|
|
|
not_related_address = "yj949n1UH6fDhw6HtVE5VMj2iSTaSWBMcW"
|
2015-05-27 15:56:16 +02:00
|
|
|
|
2015-04-21 20:32:37 +02:00
|
|
|
self.nodes[0].generate(1)
|
2014-12-01 12:38:42 +01:00
|
|
|
self.sync_all()
|
2021-04-08 22:35:33 +02:00
|
|
|
self.nodes[1].generatetoaddress(100, not_related_address)
|
2014-12-01 12:38:42 +01:00
|
|
|
self.sync_all()
|
2015-05-27 15:56:16 +02:00
|
|
|
|
2016-03-06 16:14:39 +01:00
|
|
|
assert_equal(self.nodes[0].getbalance(), 500)
|
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()
|
2021-04-08 22:35:33 +02:00
|
|
|
self.nodes[1].generatetoaddress(1, not_related_address)
|
2014-12-01 12:38:42 +01:00
|
|
|
self.sync_all()
|
2014-11-26 16:26:02 +01:00
|
|
|
bb_hash = self.nodes[0].getbestblockhash()
|
2015-05-27 15:56:16 +02:00
|
|
|
|
2021-04-08 22:35:33 +02:00
|
|
|
assert_equal(self.nodes[1].getbalance(), Decimal("0.1"))
|
2015-05-27 15:56:16 +02:00
|
|
|
|
2021-04-08 22:35:33 +02:00
|
|
|
self.log.info("Load the transaction using the /tx URI")
|
|
|
|
|
|
|
|
json_obj = self.test_rest_request("/tx/{}".format(txid))
|
|
|
|
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
|
2021-04-08 22:35:33 +02:00
|
|
|
n, = filter_output_indices_by_value(json_obj['vout'], Decimal('0.1'))
|
|
|
|
spending = (txid, n)
|
2015-05-27 15:56:16 +02:00
|
|
|
|
2021-04-08 22:35:33 +02:00
|
|
|
self.log.info("Query an unspent TXO using the /getutxos URI")
|
2015-05-27 15:56:16 +02:00
|
|
|
|
2021-04-08 22:35:33 +02:00
|
|
|
json_obj = self.test_rest_request("/getutxos/{}-{}".format(*spending))
|
2015-05-27 15:56:16 +02:00
|
|
|
|
2021-04-08 22:35:33 +02:00
|
|
|
# Check chainTip response
|
2014-12-01 12:38:42 +01:00
|
|
|
assert_equal(json_obj['chaintipHash'], bb_hash)
|
2015-05-27 15:56:16 +02:00
|
|
|
|
2021-04-08 22:35:33 +02:00
|
|
|
# Make sure there is one utxo
|
2014-12-01 12:38:42 +01:00
|
|
|
assert_equal(len(json_obj['utxos']), 1)
|
2021-04-08 22:35:33 +02:00
|
|
|
assert_equal(json_obj['utxos'][0]['value'], Decimal('0.1'))
|
2015-05-27 15:56:16 +02:00
|
|
|
|
2021-04-08 22:35:33 +02:00
|
|
|
self.log.info("Query a spent TXO using the /getutxos URI")
|
2015-05-27 15:56:16 +02:00
|
|
|
|
2021-04-08 22:35:33 +02:00
|
|
|
json_obj = self.test_rest_request("/getutxos/{}-{}".format(*spent))
|
2015-05-27 15:56:16 +02:00
|
|
|
|
2021-04-08 22:35:33 +02: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
|
|
|
|
2021-04-08 22:35:33 +02: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
|
|
|
|
2021-04-08 22:35:33 +02: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
|
|
|
|
2021-04-08 22:35:33 +02:00
|
|
|
self.log.info("Query two TXOs using the /getutxos URI")
|
|
|
|
|
|
|
|
json_obj = self.test_rest_request("/getutxos/{}-{}/{}-{}".format(*(spending + spent)))
|
2015-05-27 15:56:16 +02: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
|
|
|
|
2021-04-08 22:35:33 +02:00
|
|
|
self.log.info("Query the TXOs using the /getutxos URI with a binary response")
|
2015-05-27 15:56:16 +02:00
|
|
|
|
2021-04-08 22:35:33 +02:00
|
|
|
bin_request = b'\x01\x02'
|
|
|
|
for txid, n in [spending, spent]:
|
|
|
|
bin_request += hex_str_to_bytes(txid)
|
|
|
|
bin_request += pack("i", n)
|
2015-05-27 15:56:16 +02:00
|
|
|
|
2021-04-08 22:35:33 +02:00
|
|
|
bin_response = self.test_rest_request("/getutxos", http_method='POST', req_type=ReqType.BIN, body=bin_request, ret_type=RetType.BYTES)
|
|
|
|
output = BytesIO(bin_response)
|
|
|
|
chain_height, = unpack("i", output.read(4))
|
|
|
|
response_hash = binascii.hexlify(output.read(32)[::-1]).decode('ascii')
|
2015-05-27 15:56:16 +02:00
|
|
|
|
2021-04-08 22:35:33 +02: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
|
|
|
|
2021-04-08 22:35:33 +02: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-04-08 22:35:33 +02:00
|
|
|
json_obj = self.test_rest_request("/tx/{}".format(txid))
|
Merge #12717: [REST] Handle UTXO retrieval when ignoring the mempool
9cb9af8 [REST] Handle UTXO retrieval when ignoring the mempool (Roman Zeyde)
1fdc7c4 Make CTxMemPool::isSpent() const (Roman Zeyde)
Pull request description:
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": []
}
```
Tree-SHA512: 994a350cb34a3c8f5a7afbc169c6b177c5be6cf223b2071c62d63644819d416d3e10d1c58b244d9d351bae7233d2974aa5e9ebadd1b5d6218f5245558675be0d
2018-03-27 21:31:27 +02:00
|
|
|
# get the spent output to later check for utxo (should be spent by then)
|
2021-04-08 22:35:33 +02: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
|
2021-04-08 22:35:33 +02:00
|
|
|
n, = filter_output_indices_by_value(json_obj['vout'], Decimal('0.1'))
|
|
|
|
spending = (txid, n)
|
|
|
|
|
|
|
|
json_obj = self.test_rest_request("/getutxos/{}-{}".format(*spending))
|
|
|
|
assert_equal(len(json_obj['utxos']), 0)
|
|
|
|
|
|
|
|
json_obj = self.test_rest_request("/getutxos/checkmempool/{}-{}".format(*spending))
|
|
|
|
assert_equal(len(json_obj['utxos']), 1)
|
|
|
|
|
|
|
|
json_obj = self.test_rest_request("/getutxos/{}-{}".format(*spent))
|
|
|
|
assert_equal(len(json_obj['utxos']), 1)
|
|
|
|
|
|
|
|
json_obj = self.test_rest_request("/getutxos/checkmempool/{}-{}".format(*spent))
|
|
|
|
assert_equal(len(json_obj['utxos']), 0)
|
Merge #12717: [REST] Handle UTXO retrieval when ignoring the mempool
9cb9af8 [REST] Handle UTXO retrieval when ignoring the mempool (Roman Zeyde)
1fdc7c4 Make CTxMemPool::isSpent() const (Roman Zeyde)
Pull request description:
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": []
}
```
Tree-SHA512: 994a350cb34a3c8f5a7afbc169c6b177c5be6cf223b2071c62d63644819d416d3e10d1c58b244d9d351bae7233d2974aa5e9ebadd1b5d6218f5245558675be0d
2018-03-27 21:31:27 +02:00
|
|
|
|
|
|
|
self.nodes[0].generate(1)
|
|
|
|
self.sync_all()
|
|
|
|
|
2021-04-08 22:35:33 +02:00
|
|
|
json_obj = self.test_rest_request("/getutxos/{}-{}".format(*spending))
|
|
|
|
assert_equal(len(json_obj['utxos']), 1)
|
|
|
|
|
|
|
|
json_obj = self.test_rest_request("/getutxos/checkmempool/{}-{}".format(*spending))
|
|
|
|
assert_equal(len(json_obj['utxos']), 1)
|
|
|
|
|
|
|
|
# Do some invalid requests
|
|
|
|
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)
|
|
|
|
|
|
|
|
# Test limits
|
|
|
|
long_uri = '/'.join(["{}-{}".format(txid, n) for n in range(20)])
|
|
|
|
self.test_rest_request("/getutxos/checkmempool/{}".format(long_uri), http_method='POST', status=400, ret_type=RetType.OBJ)
|
|
|
|
|
|
|
|
long_uri = '/'.join(['{}-{}'.format(txid, n) for n in range(15)])
|
|
|
|
self.test_rest_request("/getutxos/checkmempool/{}".format(long_uri), http_method='POST', status=200)
|
|
|
|
|
|
|
|
self.nodes[0].generate(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
|
|
|
|
2019-01-23 06:58:43 +01:00
|
|
|
self.log.info("Test the /block, /blockhashbyheight and /headers URIs")
|
2021-04-08 22:35:33 +02:00
|
|
|
bb_hash = self.nodes[0].getbestblockhash()
|
2015-05-27 15:56:16 +02:00
|
|
|
|
2019-01-21 17:37:18 +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)
|
|
|
|
assert_equal(self.test_rest_request('/headers/1/{}'.format(bb_hash)), [])
|
|
|
|
self.test_rest_request('/block/{}'.format(bb_hash))
|
|
|
|
self.nodes[0].reconsiderblock(bb_hash)
|
|
|
|
|
2021-04-08 22:35:33 +02:00
|
|
|
# Check binary format
|
|
|
|
response = self.test_rest_request("/block/{}".format(bb_hash), req_type=ReqType.BIN, ret_type=RetType.OBJ)
|
2014-12-08 13:44:49 +01:00
|
|
|
assert_greater_than(int(response.getheader('content-length')), 80)
|
2021-04-08 22:35:33 +02:00
|
|
|
response_bytes = response.read()
|
2014-12-08 13:44:49 +01:00
|
|
|
|
2021-04-08 22:35:33 +02:00
|
|
|
# Compare with block header
|
|
|
|
response_header = self.test_rest_request("/headers/1/{}".format(bb_hash), req_type=ReqType.BIN, ret_type=RetType.OBJ)
|
2014-12-08 13:44:49 +01:00
|
|
|
assert_equal(int(response_header.getheader('content-length')), 80)
|
2021-04-08 22:35:33 +02:00
|
|
|
response_header_bytes = response_header.read()
|
|
|
|
assert_equal(response_bytes[:80], response_header_bytes)
|
2014-12-08 13:44:49 +01:00
|
|
|
|
2021-04-08 22:35:33 +02:00
|
|
|
# Check block hex format
|
|
|
|
response_hex = self.test_rest_request("/block/{}".format(bb_hash), req_type=ReqType.HEX, ret_type=RetType.OBJ)
|
2014-12-08 13:44:49 +01:00
|
|
|
assert_greater_than(int(response_hex.getheader('content-length')), 160)
|
2021-04-08 22:35:33 +02:00
|
|
|
response_hex_bytes = response_hex.read().strip(b'\n')
|
|
|
|
assert_equal(binascii.hexlify(response_bytes), response_hex_bytes)
|
2014-12-08 13:44:49 +01:00
|
|
|
|
2021-04-08 22:35:33 +02:00
|
|
|
# Compare with hex block header
|
|
|
|
response_header_hex = self.test_rest_request("/headers/1/{}".format(bb_hash), req_type=ReqType.HEX, ret_type=RetType.OBJ)
|
2014-12-08 13:44:49 +01:00
|
|
|
assert_greater_than(int(response_header_hex.getheader('content-length')), 160)
|
2021-04-08 22:35:33 +02:00
|
|
|
response_header_hex_bytes = response_header_hex.read(160)
|
|
|
|
assert_equal(binascii.hexlify(response_bytes[:80]), response_header_hex_bytes)
|
2014-12-08 13:44:49 +01:00
|
|
|
|
2021-04-08 22:35:33 +02:00
|
|
|
# Check json format
|
|
|
|
block_json_obj = self.test_rest_request("/block/{}".format(bb_hash))
|
2014-12-16 10:50:44 +01:00
|
|
|
assert_equal(block_json_obj['hash'], bb_hash)
|
2019-01-23 06:58:43 +01:00
|
|
|
assert_equal(self.test_rest_request("/blockhashbyheight/{}".format(block_json_obj['height']))['blockhash'], bb_hash)
|
|
|
|
|
|
|
|
# Check hex/bin format
|
|
|
|
resp_hex = self.test_rest_request("/blockhashbyheight/{}".format(block_json_obj['height']), req_type=ReqType.HEX, ret_type=RetType.OBJ)
|
|
|
|
assert_equal(resp_hex.read().decode('utf-8').rstrip(), bb_hash)
|
|
|
|
resp_bytes = self.test_rest_request("/blockhashbyheight/{}".format(block_json_obj['height']), req_type=ReqType.BIN, ret_type=RetType.BYTES)
|
|
|
|
blockhash = binascii.hexlify(resp_bytes[::-1]).decode('utf-8')
|
|
|
|
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
|
|
|
|
2021-04-08 22:35:33 +02:00
|
|
|
# Compare with json block header
|
|
|
|
json_obj = self.test_rest_request("/headers/1/{}".format(bb_hash))
|
|
|
|
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
|
|
|
|
2021-04-08 22:35:33 +02:00
|
|
|
# Compare with normal RPC block response
|
2014-12-16 10:50:44 +01:00
|
|
|
rpc_block_json = self.nodes[0].getblock(bb_hash)
|
2021-04-08 22:35:33 +02: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
|
2014-12-16 10:50:44 +01:00
|
|
|
self.nodes[1].generate(5)
|
|
|
|
self.sync_all()
|
2021-04-08 22:35:33 +02:00
|
|
|
json_obj = self.test_rest_request("/headers/5/{}".format(bb_hash))
|
|
|
|
assert_equal(len(json_obj), 5) # now we should have 5 header objects
|
|
|
|
|
|
|
|
self.log.info("Test the /tx URI")
|
2014-12-08 13:44:49 +01:00
|
|
|
|
2015-12-02 18:12:23 +01:00
|
|
|
tx_hash = block_json_obj['tx'][0]['txid']
|
2021-04-08 22:35:33 +02:00
|
|
|
json_obj = self.test_rest_request("/tx/{}".format(tx_hash))
|
2014-11-26 16:26:02 +01:00
|
|
|
assert_equal(json_obj['txid'], tx_hash)
|
2014-12-08 13:44:49 +01:00
|
|
|
|
2021-04-08 22:35:33 +02:00
|
|
|
# Check hex format response
|
|
|
|
hex_response = self.test_rest_request("/tx/{}".format(tx_hash), req_type=ReqType.HEX, ret_type=RetType.OBJ)
|
|
|
|
assert_greater_than_or_equal(int(hex_response.getheader('content-length')),
|
|
|
|
json_obj['size']*2)
|
2015-05-27 15:56:16 +02:00
|
|
|
|
2021-04-08 22:35:33 +02:00
|
|
|
self.log.info("Test tx inclusion in the /mempool and /block URIs")
|
2014-12-01 12:38:42 +01:00
|
|
|
|
2021-04-08 22:35:33 +02:00
|
|
|
# Make 3 tx and mine them on node 1
|
2014-11-28 20:32:52 +01:00
|
|
|
txs = []
|
2021-04-08 22:35:33 +02: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
|
|
|
|
2021-04-08 22:35:33 +02:00
|
|
|
# Check that there are exactly 3 transactions in the TX memory pool before generating the block
|
|
|
|
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)
|
|
|
|
|
2021-04-08 22:35:33 +02:00
|
|
|
# Check that there are our submitted transactions in the TX memory pool
|
|
|
|
json_obj = self.test_rest_request("/mempool/contents")
|
2018-03-06 22:14:44 +01:00
|
|
|
for i, tx in enumerate(txs):
|
2021-04-08 22:35:33 +02:00
|
|
|
assert tx in json_obj
|
|
|
|
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
|
|
|
|
2021-04-08 22:35:33 +02:00
|
|
|
# Now mine the transactions
|
2015-04-01 05:28:28 +02:00
|
|
|
newblockhash = self.nodes[1].generate(1)
|
2014-11-28 20:32:52 +01:00
|
|
|
self.sync_all()
|
2014-12-08 13:44:49 +01:00
|
|
|
|
2021-04-08 22:35:33 +02:00
|
|
|
# Check if the 3 tx show up in the new block
|
|
|
|
json_obj = self.test_rest_request("/block/{}".format(newblockhash[0]))
|
|
|
|
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-04-08 22:35:33 +02:00
|
|
|
# Check the same but without tx details
|
|
|
|
json_obj = self.test_rest_request("/block/notxdetails/{}".format(newblockhash[0]))
|
2014-11-28 20:32:52 +01:00
|
|
|
for tx in txs:
|
2021-04-08 22:35:33 +02:00
|
|
|
assert tx in json_obj['tx']
|
|
|
|
|
|
|
|
self.log.info("Test the /chaininfo URI")
|
2014-11-26 16:26:02 +01:00
|
|
|
|
2014-12-27 13:18:36 +01:00
|
|
|
bb_hash = self.nodes[0].getbestblockhash()
|
2015-05-27 15:56:16 +02:00
|
|
|
|
2021-04-08 22:35:33 +02: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__':
|
2021-04-08 22:35:33 +02:00
|
|
|
RESTTest().main()
|