Changeset View
Changeset View
Standalone View
Standalone View
test/functional/interface_rest.py
Show All 38 Lines | for vout in vouts: | ||||
if vout['value'] == value: | if vout['value'] == value: | ||||
yield vout['n'] | yield vout['n'] | ||||
class RESTTest (BitcoinTestFramework): | class RESTTest (BitcoinTestFramework): | ||||
def set_test_params(self): | def set_test_params(self): | ||||
self.setup_clean_chain = True | self.setup_clean_chain = True | ||||
self.num_nodes = 2 | self.num_nodes = 2 | ||||
# TODO: remove -txindex. Currently required for getrawtransaction call. | self.extra_args = [["-rest"], []] | ||||
self.extra_args = [["-rest", "-txindex"], []] | |||||
def skip_test_if_missing_module(self): | def skip_test_if_missing_module(self): | ||||
self.skip_if_no_wallet() | self.skip_if_no_wallet() | ||||
def test_rest_request(self, uri, http_method='GET', req_type=ReqType.JSON, | def test_rest_request(self, uri, http_method='GET', req_type=ReqType.JSON, | ||||
body='', status=200, ret_type=RetType.JSON): | body='', status=200, ret_type=RetType.JSON): | ||||
rest_uri = '/rest' + uri | rest_uri = '/rest' + uri | ||||
if req_type == ReqType.JSON: | if req_type == ReqType.JSON: | ||||
Show All 31 Lines | def run_test(self): | ||||
self.sync_all() | self.sync_all() | ||||
self.nodes[1].generatetoaddress(100, not_related_address) | self.nodes[1].generatetoaddress(100, not_related_address) | ||||
self.sync_all() | self.sync_all() | ||||
assert_equal(self.nodes[0].getbalance(), 50) | assert_equal(self.nodes[0].getbalance(), 50) | ||||
txid = self.nodes[0].sendtoaddress(self.nodes[1].getnewaddress(), 0.1) | txid = self.nodes[0].sendtoaddress(self.nodes[1].getnewaddress(), 0.1) | ||||
self.sync_all() | self.sync_all() | ||||
self.nodes[1].generatetoaddress(1, not_related_address) | |||||
self.sync_all() | |||||
bb_hash = self.nodes[0].getbestblockhash() | |||||
assert_equal(self.nodes[1].getbalance(), Decimal("0.1")) | self.log.info("Test the /tx URI") | ||||
self.log.info("Load the transaction using the /tx URI") | |||||
json_obj = self.test_rest_request("/tx/{}".format(txid)) | json_obj = self.test_rest_request("/tx/{}".format(txid)) | ||||
assert_equal(json_obj['txid'], txid) | |||||
# Check hex format response | |||||
hex_response = self.test_rest_request( | |||||
"/tx/{}".format(txid), req_type=ReqType.HEX, ret_type=RetType.OBJ) | |||||
assert_greater_than_or_equal(int(hex_response.getheader('content-length')), | |||||
json_obj['size'] * 2) | |||||
# Get the vin to later check for utxo (should be spent by then) | # Get the vin to later check for utxo (should be spent by then) | ||||
spent = (json_obj['vin'][0]['txid'], json_obj['vin'][0]['vout']) | spent = (json_obj['vin'][0]['txid'], json_obj['vin'][0]['vout']) | ||||
# Get n of 0.1 outpoint | # Get n of 0.1 outpoint | ||||
n, = filter_output_indices_by_value(json_obj['vout'], Decimal('0.1')) | n, = filter_output_indices_by_value(json_obj['vout'], Decimal('0.1')) | ||||
spending = (txid, n) | spending = (txid, n) | ||||
self.log.info("Query an unspent TXO using the /getutxos URI") | self.log.info("Query an unspent TXO using the /getutxos URI") | ||||
json_obj = self.test_rest_request("/getutxos/{}-{}".format(*spending)) | self.nodes[1].generatetoaddress(1, not_related_address) | ||||
self.sync_all() | |||||
bb_hash = self.nodes[0].getbestblockhash() | |||||
assert_equal(self.nodes[1].getbalance(), Decimal("0.1")) | |||||
# Check chainTip response | # Check chainTip response | ||||
json_obj = self.test_rest_request("/getutxos/{}-{}".format(*spending)) | |||||
assert_equal(json_obj['chaintipHash'], bb_hash) | assert_equal(json_obj['chaintipHash'], bb_hash) | ||||
# Make sure there is one utxo | # Make sure there is one utxo | ||||
assert_equal(len(json_obj['utxos']), 1) | assert_equal(len(json_obj['utxos']), 1) | ||||
assert_equal(json_obj['utxos'][0]['value'], Decimal('0.1')) | assert_equal(json_obj['utxos'][0]['value'], Decimal('0.1')) | ||||
self.log.info("Query a spent TXO using the /getutxos URI") | self.log.info("Query a spent TXO using the /getutxos URI") | ||||
▲ Show 20 Lines • Show All 148 Lines • ▼ Show 20 Lines | def run_test(self): | ||||
# See if we can get 5 headers in one response | # See if we can get 5 headers in one response | ||||
self.nodes[1].generate(5) | self.nodes[1].generate(5) | ||||
self.sync_all() | self.sync_all() | ||||
json_obj = self.test_rest_request("/headers/5/{}".format(bb_hash)) | json_obj = self.test_rest_request("/headers/5/{}".format(bb_hash)) | ||||
# Now we should have 5 header objects | # Now we should have 5 header objects | ||||
assert_equal(len(json_obj), 5) | assert_equal(len(json_obj), 5) | ||||
self.log.info("Test the /tx URI") | |||||
tx_hash = block_json_obj['tx'][0]['txid'] | |||||
json_obj = self.test_rest_request("/tx/{}".format(tx_hash)) | |||||
assert_equal(json_obj['txid'], tx_hash) | |||||
# 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) | |||||
self.log.info("Test tx inclusion in the /mempool and /block URIs") | self.log.info("Test tx inclusion in the /mempool and /block URIs") | ||||
# Make 3 tx and mine them on node 1 | # Make 3 tx and mine them on node 1 | ||||
txs = [] | 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)) | 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() | self.sync_all() | ||||
▲ Show 20 Lines • Show All 41 Lines • Show Last 20 Lines |