Skip to content

Commit 6919c82

Browse files
committed
MOVEONLY: Expose BanMapToJson / BanMapFromJson
CSubNet serialization code that was removed in fa4e6af was needed by multiprocess code to share ban map between gui and node processes. Rather than adding it back, use suggestion from MarcoFalke <[email protected]> bitcoin/bitcoin#10102 (comment) to use JSON serialization. This requires making BanMapToJson / BanMapFromJson functions public.
1 parent 629c4ab commit 6919c82

File tree

5 files changed

+112
-85
lines changed

5 files changed

+112
-85
lines changed

src/Makefile.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,7 @@ libbitcoin_common_a_SOURCES = \
548548
key.cpp \
549549
key_io.cpp \
550550
merkleblock.cpp \
551+
net_types.cpp \
551552
netaddress.cpp \
552553
netbase.cpp \
553554
net_permissions.cpp \

src/addrdb.cpp

Lines changed: 0 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -19,64 +19,7 @@
1919
#include <util/settings.h>
2020
#include <util/system.h>
2121

22-
CBanEntry::CBanEntry(const UniValue& json)
23-
: nVersion(json["version"].get_int()), nCreateTime(json["ban_created"].get_int64()),
24-
nBanUntil(json["banned_until"].get_int64())
25-
{
26-
}
27-
28-
UniValue CBanEntry::ToJson() const
29-
{
30-
UniValue json(UniValue::VOBJ);
31-
json.pushKV("version", nVersion);
32-
json.pushKV("ban_created", nCreateTime);
33-
json.pushKV("banned_until", nBanUntil);
34-
return json;
35-
}
36-
3722
namespace {
38-
39-
static const char* BANMAN_JSON_ADDR_KEY = "address";
40-
41-
/**
42-
* Convert a `banmap_t` object to a JSON array.
43-
* @param[in] bans Bans list to convert.
44-
* @return a JSON array, similar to the one returned by the `listbanned` RPC. Suitable for
45-
* passing to `BanMapFromJson()`.
46-
*/
47-
UniValue BanMapToJson(const banmap_t& bans)
48-
{
49-
UniValue bans_json(UniValue::VARR);
50-
for (const auto& it : bans) {
51-
const auto& address = it.first;
52-
const auto& ban_entry = it.second;
53-
UniValue j = ban_entry.ToJson();
54-
j.pushKV(BANMAN_JSON_ADDR_KEY, address.ToString());
55-
bans_json.push_back(j);
56-
}
57-
return bans_json;
58-
}
59-
60-
/**
61-
* Convert a JSON array to a `banmap_t` object.
62-
* @param[in] bans_json JSON to convert, must be as returned by `BanMapToJson()`.
63-
* @param[out] bans Bans list to create from the JSON.
64-
* @throws std::runtime_error if the JSON does not have the expected fields or they contain
65-
* unparsable values.
66-
*/
67-
void BanMapFromJson(const UniValue& bans_json, banmap_t& bans)
68-
{
69-
for (const auto& ban_entry_json : bans_json.getValues()) {
70-
CSubNet subnet;
71-
const auto& subnet_str = ban_entry_json[BANMAN_JSON_ADDR_KEY].get_str();
72-
if (!LookupSubNet(subnet_str, subnet)) {
73-
throw std::runtime_error(
74-
strprintf("Cannot parse banned address or subnet: %s", subnet_str));
75-
}
76-
bans.insert_or_assign(subnet, CBanEntry{ban_entry_json});
77-
}
78-
}
79-
8023
template <typename Stream, typename Data>
8124
bool SerializeDB(Stream& stream, const Data& data)
8225
{

src/addrdb.h

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -16,33 +16,6 @@ class CAddress;
1616
class CAddrMan;
1717
class CDataStream;
1818

19-
class CBanEntry
20-
{
21-
public:
22-
static constexpr int CURRENT_VERSION{1};
23-
int nVersion{CBanEntry::CURRENT_VERSION};
24-
int64_t nCreateTime{0};
25-
int64_t nBanUntil{0};
26-
27-
CBanEntry() {}
28-
29-
explicit CBanEntry(int64_t nCreateTimeIn)
30-
: nCreateTime{nCreateTimeIn} {}
31-
32-
/**
33-
* Create a ban entry from JSON.
34-
* @param[in] json A JSON representation of a ban entry, as created by `ToJson()`.
35-
* @throw std::runtime_error if the JSON does not have the expected fields.
36-
*/
37-
explicit CBanEntry(const UniValue& json);
38-
39-
/**
40-
* Generate a JSON representation of this ban entry.
41-
* @return JSON suitable for passing to the `CBanEntry(const UniValue&)` constructor.
42-
*/
43-
UniValue ToJson() const;
44-
};
45-
4619
/** Access to the (IP) address database (peers.dat) */
4720
class CAddrDB
4821
{

src/net_types.cpp

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Copyright (c) 2021 The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#include <net_types.h>
6+
7+
#include <netaddress.h>
8+
#include <netbase.h>
9+
#include <univalue.h>
10+
11+
CBanEntry::CBanEntry(const UniValue& json)
12+
: nVersion(json["version"].get_int()), nCreateTime(json["ban_created"].get_int64()),
13+
nBanUntil(json["banned_until"].get_int64())
14+
{
15+
}
16+
17+
UniValue CBanEntry::ToJson() const
18+
{
19+
UniValue json(UniValue::VOBJ);
20+
json.pushKV("version", nVersion);
21+
json.pushKV("ban_created", nCreateTime);
22+
json.pushKV("banned_until", nBanUntil);
23+
return json;
24+
}
25+
26+
static const char* BANMAN_JSON_ADDR_KEY = "address";
27+
28+
/**
29+
* Convert a `banmap_t` object to a JSON array.
30+
* @param[in] bans Bans list to convert.
31+
* @return a JSON array, similar to the one returned by the `listbanned` RPC. Suitable for
32+
* passing to `BanMapFromJson()`.
33+
*/
34+
UniValue BanMapToJson(const banmap_t& bans)
35+
{
36+
UniValue bans_json(UniValue::VARR);
37+
for (const auto& it : bans) {
38+
const auto& address = it.first;
39+
const auto& ban_entry = it.second;
40+
UniValue j = ban_entry.ToJson();
41+
j.pushKV(BANMAN_JSON_ADDR_KEY, address.ToString());
42+
bans_json.push_back(j);
43+
}
44+
return bans_json;
45+
}
46+
47+
/**
48+
* Convert a JSON array to a `banmap_t` object.
49+
* @param[in] bans_json JSON to convert, must be as returned by `BanMapToJson()`.
50+
* @param[out] bans Bans list to create from the JSON.
51+
* @throws std::runtime_error if the JSON does not have the expected fields or they contain
52+
* unparsable values.
53+
*/
54+
void BanMapFromJson(const UniValue& bans_json, banmap_t& bans)
55+
{
56+
for (const auto& ban_entry_json : bans_json.getValues()) {
57+
CSubNet subnet;
58+
const auto& subnet_str = ban_entry_json[BANMAN_JSON_ADDR_KEY].get_str();
59+
if (!LookupSubNet(subnet_str, subnet)) {
60+
throw std::runtime_error(
61+
strprintf("Cannot parse banned address or subnet: %s", subnet_str));
62+
}
63+
bans.insert_or_assign(subnet, CBanEntry{ban_entry_json});
64+
}
65+
}

src/net_types.h

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,56 @@
55
#ifndef BITCOIN_NET_TYPES_H
66
#define BITCOIN_NET_TYPES_H
77

8+
#include <cstdint>
89
#include <map>
910

10-
class CBanEntry;
1111
class CSubNet;
12+
class UniValue;
13+
14+
class CBanEntry
15+
{
16+
public:
17+
static constexpr int CURRENT_VERSION{1};
18+
int nVersion{CBanEntry::CURRENT_VERSION};
19+
int64_t nCreateTime{0};
20+
int64_t nBanUntil{0};
21+
22+
CBanEntry() {}
23+
24+
explicit CBanEntry(int64_t nCreateTimeIn)
25+
: nCreateTime{nCreateTimeIn} {}
26+
27+
/**
28+
* Create a ban entry from JSON.
29+
* @param[in] json A JSON representation of a ban entry, as created by `ToJson()`.
30+
* @throw std::runtime_error if the JSON does not have the expected fields.
31+
*/
32+
explicit CBanEntry(const UniValue& json);
33+
34+
/**
35+
* Generate a JSON representation of this ban entry.
36+
* @return JSON suitable for passing to the `CBanEntry(const UniValue&)` constructor.
37+
*/
38+
UniValue ToJson() const;
39+
};
1240

1341
using banmap_t = std::map<CSubNet, CBanEntry>;
1442

43+
/**
44+
* Convert a `banmap_t` object to a JSON array.
45+
* @param[in] bans Bans list to convert.
46+
* @return a JSON array, similar to the one returned by the `listbanned` RPC. Suitable for
47+
* passing to `BanMapFromJson()`.
48+
*/
49+
UniValue BanMapToJson(const banmap_t& bans);
50+
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 contain
56+
* unparsable values.
57+
*/
58+
void BanMapFromJson(const UniValue& bans_json, banmap_t& bans);
59+
1560
#endif // BITCOIN_NET_TYPES_H

0 commit comments

Comments
 (0)