Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1#!/usr/bin/env python32# Copyright (c) 2015-present The Bitcoin Core developers3# Distributed under the MIT software license, see the accompanying4# file COPYING or http://www.opensource.org/licenses/mit-license.php.5"""Test the setban rpc call."""7from contextlib import ExitStack8from test_framework.test_framework import BitcoinTestFramework9from test_framework.util import (10p2p_port,11assert_equal,12)14class SetBanTests(BitcoinTestFramework):15def set_test_params(self):16self.num_nodes = 217self.setup_clean_chain = True18self.extra_args = [[],[]]20def is_banned(self, node, addr):21return any(e['address'] == addr for e in node.listbanned())23def run_test(self):24# Node 0 connects to Node 1, check that the noban permission is not granted25self.connect_nodes(0, 1)26peerinfo = self.nodes[1].getpeerinfo()[0]27assert "noban" not in peerinfo["permissions"]29# Node 0 gets banned by Node 130self.nodes[1].setban("127.0.0.1", "add")31self.wait_until(lambda: not self.nodes[0].is_connected_to(self.nodes[1]))33# Node 0 should not be able to reconnect34self.restart_node(1, [])35context = ExitStack()36context.enter_context(self.nodes[1].assert_debug_log(expected_msgs=["dropped (banned)\n"]))37# When disconnected right after connecting, a v2 node will attempt to reconnect with v1.38# Wait for all disconnects on node0, so that it cannot mess with later tests.39context.enter_context(self.nodes[0].assert_debug_log(40expected_msgs=[41"retrying with v1 transport protocol for peer=2",42"Cleared nodestate for peer=2",43"Cleared nodestate for peer=3",44] if self.options.v2transport else [45"Cleared nodestate for peer=2", # Just one v1 disconnect to wait for46],47timeout=8))48with context:49self.nodes[0].addnode("127.0.0.1:" + str(p2p_port(1)), "onetry")50assert not self.nodes[0].is_connected_to(self.nodes[1])52# However, node 0 should be able to reconnect if it has noban permission53self.restart_node(1, ['-whitelist=127.0.0.1'])54self.connect_nodes(0, 1)55peerinfo = self.nodes[1].getpeerinfo()[0]56assert "noban" in peerinfo["permissions"]58# If we remove the ban, Node 0 should be able to reconnect even without noban permission59self.nodes[1].setban("127.0.0.1", "remove")60self.restart_node(1, [])61self.connect_nodes(0, 1)62peerinfo = self.nodes[1].getpeerinfo()[0]63assert "noban" not in peerinfo["permissions"]65self.log.info("Test that a non-IP address can be banned/unbanned")66node = self.nodes[1]67tor_addr = "pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd.onion"68ip_addr = "1.2.3.4"69assert not self.is_banned(node, tor_addr)70assert not self.is_banned(node, ip_addr)72node.setban(tor_addr, "add")73assert self.is_banned(node, tor_addr)74assert not self.is_banned(node, ip_addr)76node.setban(tor_addr, "remove")77assert not self.is_banned(self.nodes[1], tor_addr)78assert not self.is_banned(node, ip_addr)80self.log.info("Test -bantime")81self.restart_node(1, ["-bantime=1234"])82self.nodes[1].setban("127.0.0.1", "add")83banned = self.nodes[1].listbanned()[0]84assert_equal(banned['ban_duration'], 1234)86if __name__ == '__main__':87SetBanTests(__file__).main()
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.