Changeset View
Changeset View
Standalone View
Standalone View
test/functional/rpc_net.py
Show All 14 Lines | from test_framework.util import ( | ||||
assert_greater_than_or_equal, | assert_greater_than_or_equal, | ||||
assert_greater_than, | assert_greater_than, | ||||
assert_raises_rpc_error, | assert_raises_rpc_error, | ||||
connect_nodes, | connect_nodes, | ||||
p2p_port, | p2p_port, | ||||
wait_until, | wait_until, | ||||
) | ) | ||||
from test_framework.mininode import P2PInterface | from test_framework.mininode import P2PInterface | ||||
from test_framework.messages import CAddress, msg_addr, NODE_NETWORK | from test_framework.messages import ( | ||||
CAddress, | |||||
msg_addr, | |||||
NODE_NETWORK, | |||||
NODE_GETUTXO, | |||||
NODE_BLOOM, | |||||
NODE_XTHIN, | |||||
NODE_BITCOIN_CASH, | |||||
NODE_NETWORK_LIMITED, | |||||
NODE_AVALANCHE, | |||||
) | |||||
def assert_net_servicesnames(servicesflag, servicenames): | |||||
"""Utility that checks if all flags are correctly decoded in | |||||
`getpeerinfo` and `getnetworkinfo`. | |||||
:param servicesflag: The services as an integer. | |||||
:param servicesnames: The list of decoded services names, as strings. | |||||
""" | |||||
if servicesflag & NODE_NETWORK: | |||||
assert "NETWORK" in servicenames | |||||
if servicesflag & NODE_GETUTXO: | |||||
assert "GETUTXO" in servicenames | |||||
if servicesflag & NODE_BLOOM: | |||||
assert "BLOOM" in servicenames | |||||
if servicesflag & NODE_XTHIN: | |||||
assert "XTHIN" in servicenames | |||||
if servicesflag & NODE_BITCOIN_CASH: | |||||
assert "BITCOIN_CASH" in servicenames | |||||
if servicesflag & NODE_NETWORK_LIMITED: | |||||
assert "NETWORK_LIMITED" in servicenames | |||||
if servicesflag & NODE_AVALANCHE: | |||||
assert "AVALANCHE" in servicenames | |||||
class NetTest(BitcoinTestFramework): | class NetTest(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 | ||||
self.extra_args = [["-minrelaytxfee=0.00001000"], | self.extra_args = [["-minrelaytxfee=0.00001000"], | ||||
["-minrelaytxfee=0.00000500"]] | ["-minrelaytxfee=0.00000500"]] | ||||
def run_test(self): | def run_test(self): | ||||
self.log.info('Connect nodes both way') | self.log.info('Connect nodes both way') | ||||
connect_nodes(self.nodes[0], self.nodes[1]) | connect_nodes(self.nodes[0], self.nodes[1]) | ||||
connect_nodes(self.nodes[1], self.nodes[0]) | connect_nodes(self.nodes[1], self.nodes[0]) | ||||
self._test_connection_count() | self._test_connection_count() | ||||
self._test_getnettotals() | self._test_getnettotals() | ||||
self._test_getnetworkinginfo() | self._test_getnetworkinfo() | ||||
self._test_getaddednodeinfo() | self._test_getaddednodeinfo() | ||||
self._test_getpeerinfo() | self._test_getpeerinfo() | ||||
self._test_getnodeaddresses() | self._test_getnodeaddresses() | ||||
def _test_connection_count(self): | def _test_connection_count(self): | ||||
# connect_nodes connects each node to the other | # connect_nodes connects each node to the other | ||||
assert_equal(self.nodes[0].getconnectioncount(), 2) | assert_equal(self.nodes[0].getconnectioncount(), 2) | ||||
Show All 34 Lines | def _test_getnettotals(self): | ||||
after['bytesrecv_per_msg'].get( | after['bytesrecv_per_msg'].get( | ||||
'pong', 0), before['bytesrecv_per_msg'].get( | 'pong', 0), before['bytesrecv_per_msg'].get( | ||||
'pong', 0) + 32) | 'pong', 0) + 32) | ||||
assert_greater_than_or_equal( | assert_greater_than_or_equal( | ||||
after['bytessent_per_msg'].get( | after['bytessent_per_msg'].get( | ||||
'ping', 0), before['bytessent_per_msg'].get( | 'ping', 0), before['bytessent_per_msg'].get( | ||||
'ping', 0) + 32) | 'ping', 0) + 32) | ||||
def _test_getnetworkinginfo(self): | def _test_getnetworkinfo(self): | ||||
assert_equal(self.nodes[0].getnetworkinfo()['networkactive'], True) | assert_equal(self.nodes[0].getnetworkinfo()['networkactive'], True) | ||||
assert_equal(self.nodes[0].getnetworkinfo()['connections'], 2) | assert_equal(self.nodes[0].getnetworkinfo()['connections'], 2) | ||||
self.nodes[0].setnetworkactive(state=False) | self.nodes[0].setnetworkactive(state=False) | ||||
assert_equal(self.nodes[0].getnetworkinfo()['networkactive'], False) | assert_equal(self.nodes[0].getnetworkinfo()['networkactive'], False) | ||||
# Wait a bit for all sockets to close | # Wait a bit for all sockets to close | ||||
wait_until(lambda: self.nodes[0].getnetworkinfo()[ | wait_until(lambda: self.nodes[0].getnetworkinfo()[ | ||||
'connections'] == 0, timeout=3) | 'connections'] == 0, timeout=3) | ||||
self.nodes[0].setnetworkactive(state=True) | self.nodes[0].setnetworkactive(state=True) | ||||
self.log.info('Connect nodes both way') | self.log.info('Connect nodes both way') | ||||
connect_nodes(self.nodes[0], self.nodes[1]) | connect_nodes(self.nodes[0], self.nodes[1]) | ||||
connect_nodes(self.nodes[1], self.nodes[0]) | connect_nodes(self.nodes[1], self.nodes[0]) | ||||
assert_equal(self.nodes[0].getnetworkinfo()['networkactive'], True) | assert_equal(self.nodes[0].getnetworkinfo()['networkactive'], True) | ||||
assert_equal(self.nodes[0].getnetworkinfo()['connections'], 2) | assert_equal(self.nodes[0].getnetworkinfo()['connections'], 2) | ||||
# check the `servicesnames` field | |||||
network_info = [node.getnetworkinfo() for node in self.nodes] | |||||
for info in network_info: | |||||
assert_net_servicesnames( | |||||
int(info["localservices"]), info["localservicesnames"]) | |||||
def _test_getaddednodeinfo(self): | def _test_getaddednodeinfo(self): | ||||
assert_equal(self.nodes[0].getaddednodeinfo(), []) | assert_equal(self.nodes[0].getaddednodeinfo(), []) | ||||
# add a node (node2) to node0 | # add a node (node2) to node0 | ||||
ip_port = "127.0.0.1:{}".format(p2p_port(2)) | ip_port = "127.0.0.1:{}".format(p2p_port(2)) | ||||
self.nodes[0].addnode(node=ip_port, command='add') | self.nodes[0].addnode(node=ip_port, command='add') | ||||
# check that the node has indeed been added | # check that the node has indeed been added | ||||
added_nodes = self.nodes[0].getaddednodeinfo(ip_port) | added_nodes = self.nodes[0].getaddednodeinfo(ip_port) | ||||
assert_equal(len(added_nodes), 1) | assert_equal(len(added_nodes), 1) | ||||
assert_equal(added_nodes[0]['addednode'], ip_port) | assert_equal(added_nodes[0]['addednode'], ip_port) | ||||
# check that a non-existent node returns an error | # check that a non-existent node returns an error | ||||
assert_raises_rpc_error(-24, "Node has not been added", | assert_raises_rpc_error(-24, "Node has not been added", | ||||
self.nodes[0].getaddednodeinfo, '1.1.1.1') | self.nodes[0].getaddednodeinfo, '1.1.1.1') | ||||
def _test_getpeerinfo(self): | def _test_getpeerinfo(self): | ||||
peer_info = [x.getpeerinfo() for x in self.nodes] | peer_info = [x.getpeerinfo() for x in self.nodes] | ||||
# check both sides of bidirectional connection between nodes | # check both sides of bidirectional connection between nodes | ||||
# the address bound to on one side will be the source address for the | # the address bound to on one side will be the source address for the | ||||
# other node | # other node | ||||
assert_equal(peer_info[0][0]['addrbind'], peer_info[1][0]['addr']) | assert_equal(peer_info[0][0]['addrbind'], peer_info[1][0]['addr']) | ||||
assert_equal(peer_info[1][0]['addrbind'], peer_info[0][0]['addr']) | assert_equal(peer_info[1][0]['addrbind'], peer_info[0][0]['addr']) | ||||
assert_equal(peer_info[0][0]['minfeefilter'], Decimal("0.00000500")) | assert_equal(peer_info[0][0]['minfeefilter'], Decimal("0.00000500")) | ||||
assert_equal(peer_info[1][0]['minfeefilter'], Decimal("0.00001000")) | assert_equal(peer_info[1][0]['minfeefilter'], Decimal("0.00001000")) | ||||
# check the `servicesnames` field | |||||
for info in peer_info: | |||||
assert_net_servicesnames( | |||||
int(info[0]["services"]), info[0]["servicesnames"]) | |||||
def _test_getnodeaddresses(self): | def _test_getnodeaddresses(self): | ||||
self.nodes[0].add_p2p_connection(P2PInterface()) | self.nodes[0].add_p2p_connection(P2PInterface()) | ||||
# send some addresses to the node via the p2p message addr | # send some addresses to the node via the p2p message addr | ||||
msg = msg_addr() | msg = msg_addr() | ||||
imported_addrs = [] | imported_addrs = [] | ||||
for i in range(256): | for i in range(256): | ||||
Show All 33 Lines |