|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Copyright (c) 2017 The Bitcoin Core developers |
| 3 | +# Distributed under the MIT software license, see the accompanying |
| 4 | +# file COPYING or http://www.opensource.org/licenses/mit-license.php. |
| 5 | +"""Test RPC calls related to net. |
| 6 | +
|
| 7 | +Tests correspond to code in rpc/net.cpp. |
| 8 | +""" |
| 9 | + |
| 10 | +from decimal import Decimal |
| 11 | +import time |
| 12 | + |
| 13 | +from test_framework.test_framework import BitcoinTestFramework |
| 14 | +from test_framework.authproxy import JSONRPCException |
| 15 | +from test_framework.util import ( |
| 16 | + assert_equal, |
| 17 | + start_nodes, |
| 18 | + connect_nodes_bi, |
| 19 | +) |
| 20 | + |
| 21 | + |
| 22 | +class NetTest(BitcoinTestFramework): |
| 23 | + def __init__(self): |
| 24 | + super().__init__() |
| 25 | + self.setup_clean_chain = True |
| 26 | + self.num_nodes = 2 |
| 27 | + |
| 28 | + def setup_network(self): |
| 29 | + self.nodes = start_nodes(self.num_nodes, self.options.tmpdir) |
| 30 | + connect_nodes_bi(self.nodes, 0, 1) |
| 31 | + self.is_network_split = False |
| 32 | + self.sync_all() |
| 33 | + |
| 34 | + def run_test(self): |
| 35 | + assert_equal(self.nodes[0].getnetworkinfo()['networkactive'], True) |
| 36 | + assert_equal(self.nodes[0].getnetworkinfo()['connections'], 2) # bilateral connection |
| 37 | + |
| 38 | + self.nodes[0].setnetworkactive(False) |
| 39 | + assert_equal(self.nodes[0].getnetworkinfo()['networkactive'], False) |
| 40 | + timeout = 3 |
| 41 | + while self.nodes[0].getnetworkinfo()['connections'] != 0: |
| 42 | + # Wait a bit for all sockets to close |
| 43 | + assert timeout > 0, 'not all connections closed in time' |
| 44 | + timeout -= 0.1 |
| 45 | + time.sleep(0.1) |
| 46 | + |
| 47 | + self.nodes[0].setnetworkactive(True) |
| 48 | + connect_nodes_bi(self.nodes, 0, 1) |
| 49 | + assert_equal(self.nodes[0].getnetworkinfo()['networkactive'], True) |
| 50 | + assert_equal(self.nodes[0].getnetworkinfo()['connections'], 2) |
| 51 | + |
| 52 | + |
| 53 | +if __name__ == '__main__': |
| 54 | + NetTest().main() |
0 commit comments