Changeset View
Changeset View
Standalone View
Standalone View
test/functional/rpc_net.py
#!/usr/bin/env python3 | #!/usr/bin/env python3 | ||||
# Copyright (c) 2017 The Bitcoin Core developers | # Copyright (c) 2017 The Bitcoin Core developers | ||||
# Distributed under the MIT software license, see the accompanying | # Distributed under the MIT software license, see the accompanying | ||||
# file COPYING or http://www.opensource.org/licenses/mit-license.php. | # file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
"""Test RPC calls related to net. | """Test RPC calls related to net. | ||||
Tests correspond to code in rpc/net.cpp. | Tests correspond to code in rpc/net.cpp. | ||||
""" | """ | ||||
from decimal import Decimal | from decimal import Decimal | ||||
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_or_equal, | assert_greater_than_or_equal, | ||||
assert_greater_than, | |||||
assert_raises_rpc_error, | assert_raises_rpc_error, | ||||
connect_nodes_bi, | connect_nodes_bi, | ||||
p2p_port, | p2p_port, | ||||
wait_until, | wait_until, | ||||
) | ) | ||||
from test_framework.mininode import P2PInterface | |||||
from test_framework.messages import CAddress, msg_addr, NODE_NETWORK | |||||
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._test_connection_count() | self._test_connection_count() | ||||
self._test_getnettotals() | self._test_getnettotals() | ||||
self._test_getnetworkinginfo() | self._test_getnetworkinginfo() | ||||
self._test_getaddednodeinfo() | self._test_getaddednodeinfo() | ||||
self._test_getpeerinfo() | self._test_getpeerinfo() | ||||
self._test_getnodeaddresses() | |||||
def _test_connection_count(self): | def _test_connection_count(self): | ||||
# connect_nodes_bi connects each node to the other | # connect_nodes_bi connects each node to the other | ||||
assert_equal(self.nodes[0].getconnectioncount(), 2) | assert_equal(self.nodes[0].getconnectioncount(), 2) | ||||
def _test_getnettotals(self): | def _test_getnettotals(self): | ||||
# getnettotals totalbytesrecv and totalbytessent should be | # getnettotals totalbytesrecv and totalbytessent should be | ||||
# consistent with getpeerinfo. Since the RPC calls are not atomic, | # consistent with getpeerinfo. Since the RPC calls are not atomic, | ||||
▲ Show 20 Lines • Show All 65 Lines • ▼ Show 20 Lines | def _test_getpeerinfo(self): | ||||
# 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")) | ||||
def _test_getnodeaddresses(self): | |||||
self.nodes[0].add_p2p_connection(P2PInterface()) | |||||
# send some addresses to the node via the p2p message addr | |||||
msg = msg_addr() | |||||
imported_addrs = [] | |||||
for i in range(256): | |||||
a = "123.123.123.{}".format(i) | |||||
imported_addrs.append(a) | |||||
addr = CAddress() | |||||
addr.time = 100000000 | |||||
addr.nServices = NODE_NETWORK | |||||
addr.ip = a | |||||
addr.port = 8333 | |||||
msg.addrs.append(addr) | |||||
self.nodes[0].p2p.send_and_ping(msg) | |||||
# obtain addresses via rpc call and check they were ones sent in before | |||||
REQUEST_COUNT = 10 | |||||
node_addresses = self.nodes[0].getnodeaddresses(REQUEST_COUNT) | |||||
assert_equal(len(node_addresses), REQUEST_COUNT) | |||||
for a in node_addresses: | |||||
assert_greater_than(a["time"], 1527811200) # 1st June 2018 | |||||
assert_equal(a["services"], NODE_NETWORK) | |||||
assert a["address"] in imported_addrs | |||||
assert_equal(a["port"], 8333) | |||||
assert_raises_rpc_error(-8, "Address count out of range", | |||||
self.nodes[0].getnodeaddresses, -1) | |||||
# addrman's size cannot be known reliably after insertion, as hash collisions may occur | |||||
# so only test that requesting a large number of addresses returns less | |||||
# than that | |||||
LARGE_REQUEST_COUNT = 10000 | |||||
node_addresses = self.nodes[0].getnodeaddresses(LARGE_REQUEST_COUNT) | |||||
assert_greater_than(LARGE_REQUEST_COUNT, len(node_addresses)) | |||||
if __name__ == '__main__': | if __name__ == '__main__': | ||||
NetTest().main() | NetTest().main() |