diff options
Diffstat (limited to 'test/functional/rpc_net.py')
-rwxr-xr-x | test/functional/rpc_net.py | 131 |
1 files changed, 70 insertions, 61 deletions
diff --git a/test/functional/rpc_net.py b/test/functional/rpc_net.py index 506c77c567..2d41963beb 100755 --- a/test/functional/rpc_net.py +++ b/test/functional/rpc_net.py @@ -1,5 +1,5 @@ #!/usr/bin/env python3 -# Copyright (c) 2017-2019 The Bitcoin Core developers +# Copyright (c) 2017-2020 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 RPC calls related to net. @@ -21,12 +21,11 @@ from test_framework.test_framework import BitcoinTestFramework from test_framework.util import ( assert_approx, assert_equal, - assert_greater_than_or_equal, assert_greater_than, assert_raises_rpc_error, - connect_nodes, p2p_port, ) +from test_framework.wallet import MiniWallet def assert_net_servicesnames(servicesflag, servicenames): @@ -50,11 +49,18 @@ class NetTest(BitcoinTestFramework): self.supports_cli = False def run_test(self): + # We need miniwallet to make a transaction + self.wallet = MiniWallet(self.nodes[0]) + self.wallet.generate(1) # Get out of IBD for the minfeefilter and getpeerinfo tests. self.nodes[0].generate(101) - # Connect nodes both ways. - connect_nodes(self.nodes[0], 1) - connect_nodes(self.nodes[1], 0) + + # By default, the test framework sets up an addnode connection from + # node 1 --> node0. By connecting node0 --> node 1, we're left with + # the two nodes being connected both ways. + # Topology will look like: node0 <--> node1 + self.connect_nodes(0, 1) + self.sync_all() self.test_connection_count() self.test_getpeerinfo() @@ -69,41 +75,62 @@ class NetTest(BitcoinTestFramework): # After using `connect_nodes` to connect nodes 0 and 1 to each other. assert_equal(self.nodes[0].getconnectioncount(), 2) + def test_getpeerinfo(self): + self.log.info("Test getpeerinfo") + # Create a few getpeerinfo last_block/last_transaction values. + self.wallet.send_self_transfer(from_node=self.nodes[0]) # Make a transaction so we can see it in the getpeerinfo results + self.nodes[1].generate(1) + self.sync_all() + time_now = int(time.time()) + peer_info = [x.getpeerinfo() for x in self.nodes] + # Verify last_block and last_transaction keys/values. + for node, peer, field in product(range(self.num_nodes), range(2), ['last_block', 'last_transaction']): + assert field in peer_info[node][peer].keys() + if peer_info[node][peer][field] != 0: + assert_approx(peer_info[node][peer][field], time_now, vspan=60) + # check both sides of bidirectional connection between nodes + # the address bound to on one side will be the source address for the other node + 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[0][0]['minfeefilter'], Decimal("0.00000500")) + 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"], 0x10), info[0]["servicesnames"]) + + assert_equal(peer_info[0][0]['connection_type'], 'inbound') + assert_equal(peer_info[0][1]['connection_type'], 'manual') + + assert_equal(peer_info[1][0]['connection_type'], 'manual') + assert_equal(peer_info[1][1]['connection_type'], 'inbound') + + # Check dynamically generated networks list in getpeerinfo help output. + assert "(ipv4, ipv6, onion, not_publicly_routable)" in self.nodes[0].help("getpeerinfo") + def test_getnettotals(self): self.log.info("Test getnettotals") - # getnettotals totalbytesrecv and totalbytessent should be - # consistent with getpeerinfo. Since the RPC calls are not atomic, - # and messages might have been recvd or sent between RPC calls, call - # getnettotals before and after and verify that the returned values - # from getpeerinfo are bounded by those values. + # Test getnettotals and getpeerinfo by doing a ping. The bytes + # sent/received should increase by at least the size of one ping (32 + # bytes) and one pong (32 bytes). net_totals_before = self.nodes[0].getnettotals() - peer_info = self.nodes[0].getpeerinfo() - net_totals_after = self.nodes[0].getnettotals() - assert_equal(len(peer_info), 2) - peers_recv = sum([peer['bytesrecv'] for peer in peer_info]) - peers_sent = sum([peer['bytessent'] for peer in peer_info]) - - assert_greater_than_or_equal(peers_recv, net_totals_before['totalbytesrecv']) - assert_greater_than_or_equal(net_totals_after['totalbytesrecv'], peers_recv) - assert_greater_than_or_equal(peers_sent, net_totals_before['totalbytessent']) - assert_greater_than_or_equal(net_totals_after['totalbytessent'], peers_sent) - - # test getnettotals and getpeerinfo by doing a ping - # the bytes sent/received should change - # note ping and pong are 32 bytes each + peer_info_before = self.nodes[0].getpeerinfo() + self.nodes[0].ping() - self.wait_until(lambda: (self.nodes[0].getnettotals()['totalbytessent'] >= net_totals_after['totalbytessent'] + 32 * 2), timeout=1) - self.wait_until(lambda: (self.nodes[0].getnettotals()['totalbytesrecv'] >= net_totals_after['totalbytesrecv'] + 32 * 2), timeout=1) + self.wait_until(lambda: (self.nodes[0].getnettotals()['totalbytessent'] >= net_totals_before['totalbytessent'] + 32 * 2), timeout=1) + self.wait_until(lambda: (self.nodes[0].getnettotals()['totalbytesrecv'] >= net_totals_before['totalbytesrecv'] + 32 * 2), timeout=1) - peer_info_after_ping = self.nodes[0].getpeerinfo() - for before, after in zip(peer_info, peer_info_after_ping): - assert_greater_than_or_equal(after['bytesrecv_per_msg'].get('pong', 0), before['bytesrecv_per_msg'].get('pong', 0) + 32) - assert_greater_than_or_equal(after['bytessent_per_msg'].get('ping', 0), before['bytessent_per_msg'].get('ping', 0) + 32) + for peer_before in peer_info_before: + peer_after = lambda: next(p for p in self.nodes[0].getpeerinfo() if p['id'] == peer_before['id']) + self.wait_until(lambda: peer_after()['bytesrecv_per_msg'].get('pong', 0) >= peer_before['bytesrecv_per_msg'].get('pong', 0) + 32, timeout=1) + self.wait_until(lambda: peer_after()['bytessent_per_msg'].get('ping', 0) >= peer_before['bytessent_per_msg'].get('ping', 0) + 32, timeout=1) def test_getnetworkinfo(self): self.log.info("Test getnetworkinfo") - assert_equal(self.nodes[0].getnetworkinfo()['networkactive'], True) - assert_equal(self.nodes[0].getnetworkinfo()['connections'], 2) + info = self.nodes[0].getnetworkinfo() + assert_equal(info['networkactive'], True) + assert_equal(info['connections'], 2) + assert_equal(info['connections_in'], 1) + assert_equal(info['connections_out'], 1) with self.nodes[0].assert_debug_log(expected_msgs=['SetNetworkActive: false\n']): self.nodes[0].setnetworkactive(state=False) @@ -114,17 +141,23 @@ class NetTest(BitcoinTestFramework): with self.nodes[0].assert_debug_log(expected_msgs=['SetNetworkActive: true\n']): self.nodes[0].setnetworkactive(state=True) # Connect nodes both ways. - connect_nodes(self.nodes[0], 1) - connect_nodes(self.nodes[1], 0) + self.connect_nodes(0, 1) + self.connect_nodes(1, 0) - assert_equal(self.nodes[0].getnetworkinfo()['networkactive'], True) - assert_equal(self.nodes[0].getnetworkinfo()['connections'], 2) + info = self.nodes[0].getnetworkinfo() + assert_equal(info['networkactive'], True) + assert_equal(info['connections'], 2) + assert_equal(info['connections_in'], 1) + assert_equal(info['connections_out'], 1) # check the `servicesnames` field network_info = [node.getnetworkinfo() for node in self.nodes] for info in network_info: assert_net_servicesnames(int(info["localservices"], 0x10), info["localservicesnames"]) + # Check dynamically generated networks list in getnetworkinfo help output. + assert "(ipv4, ipv6, onion)" in self.nodes[0].help("getnetworkinfo") + def test_getaddednodeinfo(self): self.log.info("Test getaddednodeinfo") assert_equal(self.nodes[0].getaddednodeinfo(), []) @@ -145,30 +178,6 @@ class NetTest(BitcoinTestFramework): # check that a non-existent node returns an error assert_raises_rpc_error(-24, "Node has not been added", self.nodes[0].getaddednodeinfo, '1.1.1.1') - def test_getpeerinfo(self): - self.log.info("Test getpeerinfo") - # Create a few getpeerinfo last_block/last_transaction values. - if self.is_wallet_compiled(): - self.nodes[0].sendtoaddress(self.nodes[1].getnewaddress(), 1) - self.nodes[1].generate(1) - self.sync_all() - time_now = int(time.time()) - peer_info = [x.getpeerinfo() for x in self.nodes] - # Verify last_block and last_transaction keys/values. - for node, peer, field in product(range(self.num_nodes), range(2), ['last_block', 'last_transaction']): - assert field in peer_info[node][peer].keys() - if peer_info[node][peer][field] != 0: - assert_approx(peer_info[node][peer][field], time_now, vspan=60) - # check both sides of bidirectional connection between nodes - # the address bound to on one side will be the source address for the other node - 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[0][0]['minfeefilter'], Decimal("0.00000500")) - 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"], 0x10), info[0]["servicesnames"]) - def test_service_flags(self): self.log.info("Test service flags") self.nodes[0].add_p2p_connection(P2PInterface(), services=(1 << 4) | (1 << 63)) |