Inside Bitcoin
Opening the selected file…
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// Copyright (c) 2021-present The Bitcoin Core developers2// Distributed under the MIT software license, see the accompanying3// file COPYING or http://www.opensource.org/licenses/mit-license.php.5#include <net_types.h>7#include <netaddress.h>8#include <netbase.h>9#include <univalue.h>10#include <util/log.h>12static const char* BANMAN_JSON_VERSION_KEY{"version"};14CBanEntry::CBanEntry(const UniValue& json)15: nVersion(json[BANMAN_JSON_VERSION_KEY].getInt<int>()),16nCreateTime(json["ban_created"].getInt<int64_t>()),17nBanUntil(json["banned_until"].getInt<int64_t>())18{19}21UniValue CBanEntry::ToJson() const22{23UniValue json(UniValue::VOBJ);24json.pushKV(BANMAN_JSON_VERSION_KEY, nVersion);25json.pushKV("ban_created", nCreateTime);26json.pushKV("banned_until", nBanUntil);27return json;28}30static const char* BANMAN_JSON_ADDR_KEY = "address";32/**33* Convert a `banmap_t` object to a JSON array.34* @param[in] bans Bans list to convert.35* @return a JSON array, similar to the one returned by the `listbanned` RPC. Suitable for36* passing to `BanMapFromJson()`.37*/38UniValue BanMapToJson(const banmap_t& bans)39{40UniValue bans_json(UniValue::VARR);41for (const auto& it : bans) {42const auto& address = it.first;43const auto& ban_entry = it.second;44UniValue j = ban_entry.ToJson();45j.pushKV(BANMAN_JSON_ADDR_KEY, address.ToString());46bans_json.push_back(std::move(j));47}48return bans_json;49}51/**52* Convert a JSON array to a `banmap_t` object.53* @param[in] bans_json JSON to convert, must be as returned by `BanMapToJson()`.54* @param[out] bans Bans list to create from the JSON.55* @throws std::runtime_error if the JSON does not have the expected fields or they contain56* unparsable values.57*/58void BanMapFromJson(const UniValue& bans_json, banmap_t& bans)59{60for (const auto& ban_entry_json : bans_json.getValues()) {61const int version{ban_entry_json[BANMAN_JSON_VERSION_KEY].getInt<int>()};62if (version != CBanEntry::CURRENT_VERSION) {63LogInfo("Dropping entry with unknown version (%s) from ban list\n", version);64continue;65}66const auto& subnet_str = ban_entry_json[BANMAN_JSON_ADDR_KEY].get_str();67const CSubNet subnet{LookupSubNet(subnet_str)};68if (!subnet.IsValid()) {69LogInfo("Dropping entry with unparseable address or subnet (%s) from ban list\n", subnet_str);70continue;71}72bans.insert_or_assign(subnet, CBanEntry{ban_entry_json});73}74}