TeachMeBitcoin

The Defensive Fortress: Banman and Discouraging Bad Peers

From TeachMeBitcoin, the free encyclopedia Reading time: 2 min

The Defensive Fortress: Banman and Discouraging Bad Peers

The internet is full of "Jerks." Some people will try to send you fake data, some will try to crash your node, and some will just waste your bandwidth. To handle this, the node has a "Bouncer" called the Banman. He keeps a "Blacklist" of IP addresses that have behaved badly. If a peer tries to lie to you too many times, the Banman kicks them out and says: "Don't come back for 24 hours!" It is the "Self-Defense of the Node." It is the "Immunity against the Malicious." It is the "Fortress of the Faith." It is the "Shield of the Sovereign." It is the "Wall of the World." It is the "Sentinel of the Space."

The Ban Logic

In src/banman.cpp, the node records the "Crimes" of its peers. Every time a peer does something wrong (like sending an invalid block), the node gives them "Penalty Points." Once they reach a certain limit (usually 100 points), they are banned. It is the "Three Strikes" law of the internet. It is the "Justice of the Joint." It is the "Verdict of the Verifier." It is the "Law of the Land."

/**
 * Bans a peer for a specific amount of time.
 * This protects the node's resources from being wasted by bad actors.
 */
void BanMan::Ban(const CNetAddr& addr, int64_t ban_time_offset)
{
 // Adding the "Troublemaker" to the blacklist.
 // "Address 1.2.3.4 is banned until tomorrow."
 m_banned[addr] = GetTime() + ban_time_offset;

 // Disconnecting the phone line immediately.
 Disconnect(addr);
}

Explaining the Code: The Digital Bouncer

  1. ban_time_offset: How long should the ban last? For minor offenses, it might be an hour. For major "Attacks," it can be indefinitely. This allows the node to be "Proportional" in its response. It is the "Justice of the Gateway." It is the "Moderation of the Machine." It is the "Balance of the Body." It is the "Harmony of the Heart."

  2. Disconnect: This is the physical act of "Hanging Up." The node stops wasting any more CPU or RAM on the troublemaker. This "Efficiency of Disdain" is what allows a single Bitcoin node to survive in the "Wild West" of the open internet. It is the "Survival of the Skeptical." It is the "Victory of the Vigilant." It is the "Triumph of the Truth." It is the "Silence of the Scorned."


☕ Help support TeachMeBitcoin

TeachMeBitcoin is an ad-free, open-source educational repository curated by a passionate team of Bitcoin researchers and educators for public benefit. If you found our articles helpful, please consider supporting our hosting and ongoing content updates with a clean donation:

Ethereum: 0x578417C51783663D8A6A811B3544E1f779D39A85
Bitcoin: bc1q77k9e95rn669kpzyjr8ke9w95zhk7pa5s63qzz
Solana: 4ycT2ayqeMucixj3wS8Ay8Tq9NRDYRPKYbj3UGESyQ4J
Address copied to clipboard!