Changeset View
Changeset View
Standalone View
Standalone View
test/functional/disconnect_ban.py
#!/usr/bin/env python3 | #!/usr/bin/env python3 | ||||
# Copyright (c) 2014-2016 The Bitcoin Core developers | # Copyright (c) 2014-2016 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 node disconnect and ban behavior""" | """Test node disconnect and ban behavior""" | ||||
from test_framework.mininode import wait_until | |||||
from test_framework.test_framework import BitcoinTestFramework | from test_framework.test_framework import BitcoinTestFramework | ||||
from test_framework.util import (assert_equal, | from test_framework.util import ( | ||||
assert_equal, | |||||
assert_raises_jsonrpc, | assert_raises_jsonrpc, | ||||
connect_nodes_bi) | connect_nodes_bi, | ||||
wait_until, | |||||
) | |||||
class DisconnectBanTest(BitcoinTestFramework): | class DisconnectBanTest(BitcoinTestFramework): | ||||
def __init__(self): | def __init__(self): | ||||
super().__init__() | super().__init__() | ||||
self.num_nodes = 2 | self.num_nodes = 2 | ||||
self.setup_clean_chain = False | self.setup_clean_chain = False | ||||
def run_test(self): | def run_test(self): | ||||
self.log.info("Test setban and listbanned RPCs") | self.log.info("Test setban and listbanned RPCs") | ||||
self.log.info("setban: successfully ban single IP address") | self.log.info("setban: successfully ban single IP address") | ||||
# node1 should have 2 connections to node0 at this point | # node1 should have 2 connections to node0 at this point | ||||
assert_equal(len(self.nodes[1].getpeerinfo()), 2) | assert_equal(len(self.nodes[1].getpeerinfo()), 2) | ||||
self.nodes[1].setban("127.0.0.1", "add") | self.nodes[1].setban("127.0.0.1", "add") | ||||
wait_until(lambda: len(self.nodes[1].getpeerinfo()) == 0) | wait_until(lambda: len(self.nodes[1].getpeerinfo()) == 0, timeout=10) | ||||
# all nodes must be disconnected at this point | # all nodes must be disconnected at this point | ||||
assert_equal(len(self.nodes[1].getpeerinfo()), 0) | assert_equal(len(self.nodes[1].getpeerinfo()), 0) | ||||
assert_equal(len(self.nodes[1].listbanned()), 1) | assert_equal(len(self.nodes[1].listbanned()), 1) | ||||
self.log.info("clearbanned: successfully clear ban list") | self.log.info("clearbanned: successfully clear ban list") | ||||
self.nodes[1].clearbanned() | self.nodes[1].clearbanned() | ||||
assert_equal(len(self.nodes[1].listbanned()), 0) | assert_equal(len(self.nodes[1].listbanned()), 0) | ||||
self.nodes[1].setban("127.0.0.0/24", "add") | self.nodes[1].setban("127.0.0.0/24", "add") | ||||
▲ Show 20 Lines • Show All 59 Lines • ▼ Show 20 Lines | def run_test(self): | ||||
"disconnectnode: fail to disconnect when calling with junk address") | "disconnectnode: fail to disconnect when calling with junk address") | ||||
assert_raises_jsonrpc(-29, "Node not found in connected nodes", | assert_raises_jsonrpc(-29, "Node not found in connected nodes", | ||||
self.nodes[0].disconnectnode, address="221B Baker Street") | self.nodes[0].disconnectnode, address="221B Baker Street") | ||||
self.log.info( | self.log.info( | ||||
"disconnectnode: successfully disconnect node by address") | "disconnectnode: successfully disconnect node by address") | ||||
address1 = self.nodes[0].getpeerinfo()[0]['addr'] | address1 = self.nodes[0].getpeerinfo()[0]['addr'] | ||||
self.nodes[0].disconnectnode(address=address1) | self.nodes[0].disconnectnode(address=address1) | ||||
wait_until(lambda: len(self.nodes[0].getpeerinfo()) == 1) | wait_until(lambda: len(self.nodes[0].getpeerinfo()) == 1, timeout=10) | ||||
assert not [node for node in self.nodes[0] | assert not [node for node in self.nodes[0].getpeerinfo() | ||||
.getpeerinfo() if node['addr'] == address1] | if node['addr'] == address1] | ||||
self.log.info("disconnectnode: successfully reconnect node") | self.log.info("disconnectnode: successfully reconnect node") | ||||
# reconnect the node | # reconnect the node | ||||
connect_nodes_bi(self.nodes, 0, 1) | connect_nodes_bi(self.nodes, 0, 1) | ||||
assert_equal(len(self.nodes[0].getpeerinfo()), 2) | assert_equal(len(self.nodes[0].getpeerinfo()), 2) | ||||
assert [node for node in self.nodes[0] | assert [node for node in self.nodes[0] | ||||
.getpeerinfo() if node['addr'] == address1] | .getpeerinfo() if node['addr'] == address1] | ||||
self.log.info( | self.log.info( | ||||
"disconnectnode: successfully disconnect node by node id") | "disconnectnode: successfully disconnect node by node id") | ||||
id1 = self.nodes[0].getpeerinfo()[0]['id'] | id1 = self.nodes[0].getpeerinfo()[0]['id'] | ||||
self.nodes[0].disconnectnode(nodeid=id1) | self.nodes[0].disconnectnode(nodeid=id1) | ||||
wait_until(lambda: len(self.nodes[0].getpeerinfo()) == 1) | wait_until(lambda: len(self.nodes[0].getpeerinfo()) == 1, timeout=10) | ||||
assert not [node for node in self.nodes[ | assert not [node for node in self.nodes[0].getpeerinfo() | ||||
0].getpeerinfo() if node['id'] == id1] | if node['id'] == id1] | ||||
if __name__ == '__main__': | if __name__ == '__main__': | ||||
DisconnectBanTest().main() | DisconnectBanTest().main() |