dash/src/net_types.cpp
W. J. van der Laan bd67f80634
Merge bitcoin/bitcoin#22362: Drop only invalid entries when reading banlist.json
faa6c3d44c861c0486c1369e1d098b7645ab07cd net: Drop only invalid entries when reading banlist.json (MarcoFalke)

Pull request description:

  All entries will be dropped when there is at least one invalid one in `banlist.json`. Fix this by only dropping invalid ones.

  Also suggested in https://github.com/bitcoin/bitcoin/pull/20966#issuecomment-861150204

ACKs for top commit:
  laanwj:
    Re-ACK faa6c3d44c861c0486c1369e1d098b7645ab07cd

Tree-SHA512: 5a58e7f1dcabf78d0c65d8c6d5d997063af1efeaa50ca7730fc00056fda7e0061b6f7a38907ea045fe667c9f61d392e01e556b425a95e6b126e3c41cd33deb83
2024-09-25 10:18:12 +05:30

75 lines
2.4 KiB
C++

// Copyright (c) 2021 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include <net_types.h>
#include <logging.h>
#include <netaddress.h>
#include <netbase.h>
#include <univalue.h>
static const char* BANMAN_JSON_VERSION_KEY{"version"};
CBanEntry::CBanEntry(const UniValue& json)
: nVersion(json[BANMAN_JSON_VERSION_KEY].get_int()),
nCreateTime(json["ban_created"].get_int64()),
nBanUntil(json["banned_until"].get_int64())
{
}
UniValue CBanEntry::ToJson() const
{
UniValue json(UniValue::VOBJ);
json.pushKV(BANMAN_JSON_VERSION_KEY, nVersion);
json.pushKV("ban_created", nCreateTime);
json.pushKV("banned_until", nBanUntil);
return json;
}
static const char* BANMAN_JSON_ADDR_KEY = "address";
/**
* Convert a `banmap_t` object to a JSON array.
* @param[in] bans Bans list to convert.
* @return a JSON array, similar to the one returned by the `listbanned` RPC. Suitable for
* passing to `BanMapFromJson()`.
*/
UniValue BanMapToJson(const banmap_t& bans)
{
UniValue bans_json(UniValue::VARR);
for (const auto& it : bans) {
const auto& address = it.first;
const auto& ban_entry = it.second;
UniValue j = ban_entry.ToJson();
j.pushKV(BANMAN_JSON_ADDR_KEY, address.ToString());
bans_json.push_back(j);
}
return bans_json;
}
/**
* Convert a JSON array to a `banmap_t` object.
* @param[in] bans_json JSON to convert, must be as returned by `BanMapToJson()`.
* @param[out] bans Bans list to create from the JSON.
* @throws std::runtime_error if the JSON does not have the expected fields or they contain
* unparsable values.
*/
void BanMapFromJson(const UniValue& bans_json, banmap_t& bans)
{
for (const auto& ban_entry_json : bans_json.getValues()) {
const int version{ban_entry_json[BANMAN_JSON_VERSION_KEY].get_int()};
if (version != CBanEntry::CURRENT_VERSION) {
LogPrintf("Dropping entry with unknown version (%s) from ban list\n", version);
continue;
}
CSubNet subnet;
const auto& subnet_str = ban_entry_json[BANMAN_JSON_ADDR_KEY].get_str();
if (!LookupSubNet(subnet_str, subnet)) {
LogPrintf("Dropping entry with unparseable address or subnet (%s) from ban list\n", subnet_str);
continue;
}
bans.insert_or_assign(subnet, CBanEntry{ban_entry_json});
}
}