mirror of
https://github.com/dashpay/dash.git
synced 2024-12-27 21:12:48 +01:00
65226da849
68faa87881f5334b2528db4adc72ec19d94316a3 test: use f-strings in mining_*.py tests (fanquake) c2a5d560df2824df5731100c2584e8ad7a3d7bc2 test: use f-strings in interface_*.py tests (fanquake) 86d958262dff43002820d58ccb8958e2dbfb9d5b test: use f-strings in feature_proxy.py (fanquake) 31bdb33dcb8345df1bb94b28e811252a918d7dcb test: use f-strings in feature_segwit.py (fanquake) b166d54c3cbb0c028210cee977b3dcde5ac5474f test: use f-strings in feature_versionbits_warning.py (fanquake) cf6d66bf941d946600047d712c7cd15d7605322e test: use f-strings in feature_settings.py (fanquake) 6651d77f22862716f5bd7d0b31cfbd3937ab7b1d test: use f-strings in feature_pruning.py (fanquake) 961f5813ba65b6a601081912c4ece96c2679794d test: use f-strings in feature_notifications.py (fanquake) 1a546e6f6ca95772f0d7dbc2792477becbb8ea63 test: use f-strings in feature_minchainwork.py (fanquake) 6679eceacc915a8ea7cd7063f103ffc5eb9da884 test: use f-strings in feature_logging.py (fanquake) fb633933ab570e945d2a366f37eeff39f516c613 test: use f-strings in feature_loadblock.py (fanquake) e9ca8b254d4b9567831c0e113ce1c0a2b4795a95 test: use f-strings in feature_help.py (fanquake) ff7e3309995a8960ac371741b2b00c6da40f7490 test: use f-strings in feature_filelock.py (fanquake) d5a6adc5e478fa5c6e562377eea873dc38e66578 test: use f-strings in feature_fee_estimation.py (fanquake) a2de33cbdc79202bccddb4beadfde88266ac979f test: use f-strings in feature_dersig.py (fanquake) a2502cc63fd308be8af840962da9c53339433fa6 test: use f-strings in feature_dbcrash.py (fanquake) 3e2f84e7a96cb4b97b609ac853f78edd0ed43f82 test: use f-strings in feature_csv_activation.py (fanquake) e2f1fd8ee92fa421b6d293169044d6ddd5a9b8df test: use f-strings in feature_config_args.py (fanquake) 36d33d32b1b498b61f56d552f6e2c1d064f978c3 test: use f-strings in feature_cltv.py (fanquake) dca173cc044270b30782b1e3355e9dcb8c534295 test: use f-strings in feature_blocksdir.py (fanquake) 5453e8706278918ac51a725e81599cfa18c8cdbc test: use f-strings in feature_backwards_compatibility.py (fanquake) 6f3d5ad67ac8e7b50abae1a2949898d858e38106 test: use f-strings in feature_asmap.py (fanquake) Pull request description: Rather than using 3 different ways to build/format strings (sometimes all in the same test, i.e [`feature_config_args.py`](https://github.com/bitcoin/bitcoin/blob/master/test/functional/feature_config_args.py)), consolidate to using [f-strings (3.6+)](https://docs.python.org/3/reference/lexical_analysis.html#f-strings), which are generally more concise / readable, as well as more performant than existing methods. This deals with the `feature_*.py`, `interface_*.py` and `mining_*.py` tests. See also: [PEP 498](https://www.python.org/dev/peps/pep-0498/) ACKs for top commit: mjdietzx: reACK 68faa87881f5334b2528db4adc72ec19d94316a3 Zero-1729: crACK 68faa87881f5334b2528db4adc72ec19d94316a3 Tree-SHA512: d4e1a42e07d96d2c552387a46da1534223c4ce408703d7568ad2ef580797dd68d9695b8d19666b567af37f44de6e430e8be5db5d5404ba8fcecf9f5b026a6efb
352 lines
16 KiB
Python
Executable File
352 lines
16 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
# Copyright (c) 2014-2016 The Bitcoin Core developers
|
|
# Distributed under the MIT software license, see the accompanying
|
|
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
"""Test the REST API."""
|
|
|
|
from decimal import Decimal
|
|
from enum import Enum
|
|
from io import BytesIO
|
|
import json
|
|
from struct import pack, unpack
|
|
|
|
import http.client
|
|
import urllib.parse
|
|
|
|
from test_framework.test_framework import BitcoinTestFramework
|
|
from test_framework.util import (
|
|
assert_equal,
|
|
assert_greater_than,
|
|
assert_greater_than_or_equal,
|
|
)
|
|
|
|
from test_framework.messages import BLOCK_HEADER_SIZE
|
|
|
|
INVALID_PARAM = "abc"
|
|
UNKNOWN_PARAM = "0000000000000000000000000000000000000000000000000000000000000000"
|
|
|
|
|
|
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']
|
|
|
|
class RESTTest (BitcoinTestFramework):
|
|
def set_test_params(self):
|
|
self.setup_clean_chain = True
|
|
self.num_nodes = 2
|
|
self.extra_args = [["-rest", "-blockfilterindex=1"], []]
|
|
self.supports_cli = False
|
|
|
|
def skip_test_if_missing_module(self):
|
|
self.skip_if_no_wallet()
|
|
|
|
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(f'{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)
|
|
|
|
def run_test(self):
|
|
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"
|
|
|
|
self.generate(self.nodes[0], 1)
|
|
self.generatetoaddress(self.nodes[1], 100, not_related_address)
|
|
|
|
assert_equal(self.nodes[0].getbalance(), 500)
|
|
|
|
txid = self.nodes[0].sendtoaddress(self.nodes[1].getnewaddress(), 0.1)
|
|
self.sync_all()
|
|
|
|
self.log.info("Test the /tx URI")
|
|
|
|
json_obj = self.test_rest_request(f"/tx/{txid}")
|
|
assert_equal(json_obj['txid'], txid)
|
|
|
|
# Check hex format response
|
|
hex_response = self.test_rest_request(f"/tx/{txid}", req_type=ReqType.HEX, ret_type=RetType.OBJ)
|
|
assert_greater_than_or_equal(int(hex_response.getheader('content-length')),
|
|
json_obj['size']*2)
|
|
|
|
spent = (json_obj['vin'][0]['txid'], json_obj['vin'][0]['vout']) # get the vin to later check for utxo (should be spent by then)
|
|
# get n of 0.1 outpoint
|
|
n, = filter_output_indices_by_value(json_obj['vout'], Decimal('0.1'))
|
|
spending = (txid, n)
|
|
|
|
# Test /tx with an invalid and an unknown txid
|
|
resp = self.test_rest_request(uri=f"/tx/{INVALID_PARAM}", ret_type=RetType.OBJ, status=400)
|
|
assert_equal(resp.read().decode('utf-8').rstrip(), f"Invalid hash: {INVALID_PARAM}")
|
|
resp = self.test_rest_request(uri=f"/tx/{UNKNOWN_PARAM}", ret_type=RetType.OBJ, status=404)
|
|
assert_equal(resp.read().decode('utf-8').rstrip(), f"{UNKNOWN_PARAM} not found")
|
|
|
|
self.log.info("Query an unspent TXO using the /getutxos URI")
|
|
|
|
self.generatetoaddress(self.nodes[1], 1, not_related_address)
|
|
bb_hash = self.nodes[0].getbestblockhash()
|
|
|
|
assert_equal(self.nodes[1].getbalance(), Decimal("0.1"))
|
|
|
|
# Check chainTip response
|
|
json_obj = self.test_rest_request(f"/getutxos/{spending[0]}-{spending[1]}")
|
|
assert_equal(json_obj['chaintipHash'], bb_hash)
|
|
|
|
# Make sure there is one utxo
|
|
assert_equal(len(json_obj['utxos']), 1)
|
|
assert_equal(json_obj['utxos'][0]['value'], Decimal('0.1'))
|
|
|
|
self.log.info("Query a spent TXO using the /getutxos URI")
|
|
|
|
json_obj = self.test_rest_request(f"/getutxos/{spent[0]}-{spent[1]}")
|
|
|
|
# Check chainTip response
|
|
assert_equal(json_obj['chaintipHash'], bb_hash)
|
|
|
|
# Make sure there is no utxo in the response because this outpoint has been spent
|
|
assert_equal(len(json_obj['utxos']), 0)
|
|
|
|
# Check bitmap
|
|
assert_equal(json_obj['bitmap'], "0")
|
|
|
|
self.log.info("Query two TXOs using the /getutxos URI")
|
|
|
|
json_obj = self.test_rest_request(f"/getutxos/{spending[0]}-{spending[1]}/{spent[0]}-{spent[1]}")
|
|
|
|
assert_equal(len(json_obj['utxos']), 1)
|
|
assert_equal(json_obj['bitmap'], "10")
|
|
|
|
self.log.info("Query the TXOs using the /getutxos URI with a binary response")
|
|
|
|
bin_request = b'\x01\x02'
|
|
for txid, n in [spending, spent]:
|
|
bin_request += bytes.fromhex(txid)
|
|
bin_request += pack("i", n)
|
|
|
|
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 = output.read(32)[::-1].hex()
|
|
|
|
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
|
|
|
|
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.
|
|
|
|
# do a tx and don't sync
|
|
txid = self.nodes[0].sendtoaddress(self.nodes[1].getnewaddress(), 0.1)
|
|
json_obj = self.test_rest_request(f"/tx/{txid}")
|
|
# get the spent output to later check for utxo (should be spent by then)
|
|
spent = (json_obj['vin'][0]['txid'], json_obj['vin'][0]['vout'])
|
|
# get n of 0.1 outpoint
|
|
n, = filter_output_indices_by_value(json_obj['vout'], Decimal('0.1'))
|
|
spending = (txid, n)
|
|
|
|
json_obj = self.test_rest_request(f"/getutxos/{spending[0]}-{spending[1]}")
|
|
assert_equal(len(json_obj['utxos']), 0)
|
|
|
|
json_obj = self.test_rest_request(f"/getutxos/checkmempool/{spending[0]}-{spending[1]}")
|
|
assert_equal(len(json_obj['utxos']), 1)
|
|
|
|
json_obj = self.test_rest_request(f"/getutxos/{spent[0]}-{spent[1]}")
|
|
assert_equal(len(json_obj['utxos']), 1)
|
|
|
|
json_obj = self.test_rest_request(f"/getutxos/checkmempool/{spent[0]}-{spent[1]}")
|
|
assert_equal(len(json_obj['utxos']), 0)
|
|
|
|
self.generate(self.nodes[0], 1)
|
|
|
|
json_obj = self.test_rest_request(f"/getutxos/{spending[0]}-{spending[1]}")
|
|
assert_equal(len(json_obj['utxos']), 1)
|
|
|
|
json_obj = self.test_rest_request(f"/getutxos/checkmempool/{spending[0]}-{spending[1]}")
|
|
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([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)
|
|
|
|
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)
|
|
|
|
self.generate(self.nodes[0], 1) # generate block to not affect upcoming tests
|
|
|
|
self.log.info("Test the /block, /blockhashbyheight and /headers URIs")
|
|
bb_hash = self.nodes[0].getbestblockhash()
|
|
|
|
# Check result if block does not exists
|
|
assert_equal(self.test_rest_request(f"/headers/1/{UNKNOWN_PARAM}"), [])
|
|
self.test_rest_request(f"/block/{UNKNOWN_PARAM}", 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(f'/headers/1/{bb_hash}'), [])
|
|
self.test_rest_request(f'/block/{bb_hash}')
|
|
self.nodes[0].reconsiderblock(bb_hash)
|
|
|
|
# Check binary format
|
|
response = self.test_rest_request(f"/block/{bb_hash}", req_type=ReqType.BIN, ret_type=RetType.OBJ)
|
|
assert_greater_than(int(response.getheader('content-length')), BLOCK_HEADER_SIZE)
|
|
response_bytes = response.read()
|
|
|
|
# Compare with block header
|
|
response_header = self.test_rest_request(f"/headers/1/{bb_hash}", req_type=ReqType.BIN, ret_type=RetType.OBJ)
|
|
assert_equal(int(response_header.getheader('content-length')), BLOCK_HEADER_SIZE)
|
|
response_header_bytes = response_header.read()
|
|
assert_equal(response_bytes[:BLOCK_HEADER_SIZE], response_header_bytes)
|
|
|
|
# Check block hex format
|
|
response_hex = self.test_rest_request(f"/block/{bb_hash}", req_type=ReqType.HEX, ret_type=RetType.OBJ)
|
|
assert_greater_than(int(response_hex.getheader('content-length')), BLOCK_HEADER_SIZE*2)
|
|
response_hex_bytes = response_hex.read().strip(b'\n')
|
|
assert_equal(response_bytes.hex().encode(), response_hex_bytes)
|
|
|
|
# Compare with hex block header
|
|
response_header_hex = self.test_rest_request(f"/headers/1/{bb_hash}", req_type=ReqType.HEX, ret_type=RetType.OBJ)
|
|
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)
|
|
assert_equal(response_bytes[:BLOCK_HEADER_SIZE].hex().encode(), response_header_hex_bytes)
|
|
|
|
# Check json format
|
|
block_json_obj = self.test_rest_request(f"/block/{bb_hash}")
|
|
assert_equal(block_json_obj['hash'], bb_hash)
|
|
assert_equal(self.test_rest_request(f"/blockhashbyheight/{block_json_obj['height']}")['blockhash'], bb_hash)
|
|
|
|
# Check hex/bin format
|
|
resp_hex = self.test_rest_request(f"/blockhashbyheight/{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(f"/blockhashbyheight/{block_json_obj['height']}", req_type=ReqType.BIN, ret_type=RetType.BYTES)
|
|
blockhash = resp_bytes[::-1].hex()
|
|
assert_equal(blockhash, bb_hash)
|
|
|
|
# Check invalid blockhashbyheight requests
|
|
resp = self.test_rest_request(f"/blockhashbyheight/{INVALID_PARAM}", ret_type=RetType.OBJ, status=400)
|
|
assert_equal(resp.read().decode('utf-8').rstrip(), f"Invalid height: {INVALID_PARAM}")
|
|
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)
|
|
|
|
# Compare with json block header
|
|
json_obj = self.test_rest_request(f"/headers/1/{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
|
|
|
|
# Compare with normal RPC block response
|
|
rpc_block_json = self.nodes[0].getblock(bb_hash)
|
|
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
|
|
self.generate(self.nodes[1], 5)
|
|
json_obj = self.test_rest_request(f"/headers/5/{bb_hash}")
|
|
assert_equal(len(json_obj), 5) # now we should have 5 header objects
|
|
json_obj = self.test_rest_request(f"/blockfilterheaders/basic/5/{bb_hash}")
|
|
assert_equal(len(json_obj), 5) # now we should have 5 filter header objects
|
|
self.test_rest_request(f"/blockfilter/basic/{bb_hash}", req_type=ReqType.BIN, ret_type=RetType.OBJ)
|
|
|
|
# Test number parsing
|
|
for num in ['5a', '-5', '0', '2001', '99999999999999999999999999999999999']:
|
|
assert_equal(
|
|
bytes(f'Header count out of acceptable range (1-2000): {num}\r\n', 'ascii'),
|
|
self.test_rest_request(f"/headers/{num}/{bb_hash}", ret_type=RetType.BYTES, status=400),
|
|
)
|
|
|
|
self.log.info("Test tx inclusion in the /mempool and /block URIs")
|
|
|
|
# Make 3 tx and mine them on node 1
|
|
txs = []
|
|
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))
|
|
self.sync_all()
|
|
|
|
# Check that there are exactly 3 transactions in the TX memory pool before generating the block
|
|
json_obj = self.test_rest_request("/mempool/info")
|
|
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)
|
|
|
|
mempool_info = self.nodes[0].getmempoolinfo()
|
|
assert_equal(json_obj, mempool_info)
|
|
|
|
# Check that there are our submitted transactions in the TX memory pool
|
|
json_obj = self.test_rest_request("/mempool/contents")
|
|
raw_mempool_verbose = self.nodes[0].getrawmempool(verbose=True)
|
|
|
|
assert_equal(json_obj, raw_mempool_verbose)
|
|
|
|
for i, tx in enumerate(txs):
|
|
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])
|
|
|
|
# Now mine the transactions
|
|
newblockhash = self.generate(self.nodes[1], 1)
|
|
|
|
# Check if the 3 tx show up in the new block
|
|
json_obj = self.test_rest_request(f"/block/{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))
|
|
|
|
# Check the same but without tx details
|
|
json_obj = self.test_rest_request(f"/block/notxdetails/{newblockhash[0]}")
|
|
for tx in txs:
|
|
assert tx in json_obj['tx']
|
|
|
|
self.log.info("Test the /chaininfo URI")
|
|
|
|
bb_hash = self.nodes[0].getbestblockhash()
|
|
|
|
json_obj = self.test_rest_request("/chaininfo")
|
|
assert_equal(json_obj['bestblockhash'], bb_hash)
|
|
|
|
# Compare with normal RPC getblockchaininfo response
|
|
blockchain_info = self.nodes[0].getblockchaininfo()
|
|
assert_equal(blockchain_info, json_obj)
|
|
|
|
if __name__ == '__main__':
|
|
RESTTest().main()
|