Changeset View
Changeset View
Standalone View
Standalone View
test/functional/interface_rest.py
Show All 12 Lines | |||||
from struct import pack, unpack | from struct import pack, unpack | ||||
import urllib.parse | import urllib.parse | ||||
from test_framework.messages import deser_uint256 | from test_framework.messages import deser_uint256 | ||||
from test_framework.test_framework import BitcoinTestFramework | from test_framework.test_framework import BitcoinTestFramework | ||||
from test_framework.util import ( | from test_framework.util import ( | ||||
assert_equal, | assert_equal, | ||||
assert_greater_than, | assert_greater_than, | ||||
connect_nodes_bi, | |||||
hex_str_to_bytes, | hex_str_to_bytes, | ||||
) | ) | ||||
class ReqType(Enum): | class ReqType(Enum): | ||||
JSON = 1 | JSON = 1 | ||||
BIN = 2 | BIN = 2 | ||||
HEX = 3 | HEX = 3 | ||||
Show All 9 Lines | def filter_output_indices_by_value(vouts, value): | ||||
for vout in vouts: | 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 = 3 | self.num_nodes = 2 | ||||
self.extra_args = [["-rest"]] * self.num_nodes | self.extra_args = [["-rest"], []] | ||||
def setup_network(self, split=False): | |||||
super().setup_network() | |||||
connect_nodes_bi(self.nodes[0], self.nodes[2]) | |||||
self.url = urllib.parse.urlparse(self.nodes[0].url) | |||||
def test_rest_request(self, uri, http_method='GET', req_type=ReqType.JSON, body='', status=200, ret_type=RetType.JSON): | def test_rest_request(self, uri, http_method='GET', req_type=ReqType.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: | ||||
rest_uri += '.json' | rest_uri += '.json' | ||||
elif req_type == ReqType.BIN: | elif req_type == ReqType.BIN: | ||||
rest_uri += '.bin' | rest_uri += '.bin' | ||||
elif req_type == ReqType.HEX: | elif req_type == ReqType.HEX: | ||||
Show All 12 Lines | def test_rest_request(self, uri, http_method='GET', req_type=ReqType.JSON, body='', status=200, ret_type=RetType.JSON): | ||||
if ret_type == RetType.OBJ: | if ret_type == RetType.OBJ: | ||||
return resp | return resp | ||||
elif ret_type == RetType.BYTES: | elif ret_type == RetType.BYTES: | ||||
return resp.read() | return resp.read() | ||||
elif ret_type == RetType.JSON: | elif ret_type == RetType.JSON: | ||||
return json.loads(resp.read().decode('utf-8'), parse_float=Decimal) | return json.loads(resp.read().decode('utf-8'), parse_float=Decimal) | ||||
def run_test(self): | def run_test(self): | ||||
self.url = urllib.parse.urlparse(self.nodes[0].url) | |||||
self.log.info("Mine blocks and send Bitcoin Cash to node 1") | self.log.info("Mine blocks and send Bitcoin Cash to node 1") | ||||
# Random address so node1's balance doesn't increase | |||||
not_related_address = "2MxqoHEdNQTyYeX1mHcbrrpzgojbosTpCvJ" | |||||
self.nodes[0].generate(1) | self.nodes[0].generate(1) | ||||
self.sync_all() | self.sync_all() | ||||
self.nodes[2].generate(100) | 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[2].generate(1) | self.nodes[1].generatetoaddress(1, not_related_address) | ||||
self.sync_all() | self.sync_all() | ||||
bb_hash = self.nodes[0].getbestblockhash() | bb_hash = self.nodes[0].getbestblockhash() | ||||
assert_equal(self.nodes[1].getbalance(), Decimal("0.1")) | assert_equal(self.nodes[1].getbalance(), Decimal("0.1")) | ||||
self.log.info("Load the transaction using 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)) | ||||
▲ Show 20 Lines • Show All 185 Lines • ▼ Show 20 Lines | def run_test(self): | ||||
hex_response = self.test_rest_request( | hex_response = self.test_rest_request( | ||||
"/tx/{}".format(tx_hash), req_type=ReqType.HEX, ret_type=RetType.OBJ) | "/tx/{}".format(tx_hash), req_type=ReqType.HEX, ret_type=RetType.OBJ) | ||||
assert_greater_than(int(hex_response.getheader('content-length')), 10) | assert_greater_than(int(hex_response.getheader('content-length')), 10) | ||||
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( | txs.append(self.nodes[0].sendtoaddress(not_related_address, 11)) | ||||
self.nodes[2].getnewaddress(), 11)) | txs.append(self.nodes[0].sendtoaddress(not_related_address, 11)) | ||||
txs.append(self.nodes[0].sendtoaddress( | txs.append(self.nodes[0].sendtoaddress(not_related_address, 11)) | ||||
self.nodes[2].getnewaddress(), 11)) | |||||
txs.append(self.nodes[0].sendtoaddress( | |||||
self.nodes[2].getnewaddress(), 11)) | |||||
self.sync_all() | self.sync_all() | ||||
# Check that there are exactly 3 transactions in the TX memory pool | # Check that there are exactly 3 transactions in the TX memory pool | ||||
# before generating the block | # before generating the block | ||||
json_obj = self.test_rest_request("/mempool/info") | json_obj = self.test_rest_request("/mempool/info") | ||||
assert_equal(json_obj['size'], 3) | assert_equal(json_obj['size'], 3) | ||||
# The size of the memory pool should be greater than 3x ~100 bytes | # The size of the memory pool should be greater than 3x ~100 bytes | ||||
assert_greater_than(json_obj['bytes'], 300) | assert_greater_than(json_obj['bytes'], 300) | ||||
Show All 34 Lines |