Branch data Line data Source code
1 : : // Copyright (c) 2019-present 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 : : #ifndef BITCOIN_NET_TYPES_H
6 : : #define BITCOIN_NET_TYPES_H
7 : :
8 : : #include <cstdint>
9 : : #include <map>
10 : :
11 : : class CSubNet;
12 : : class UniValue;
13 : :
14 : : /// Identifier of a peer connection.
15 : : using NodeId = int64_t;
16 : :
17 : : class CBanEntry
18 : : {
19 : : public:
20 : : static constexpr int CURRENT_VERSION{1};
21 : : int nVersion{CBanEntry::CURRENT_VERSION};
22 : : int64_t nCreateTime{0};
23 : : int64_t nBanUntil{0};
24 : :
25 : 7 : CBanEntry() = default;
26 : :
27 : 7 : explicit CBanEntry(int64_t nCreateTimeIn)
28 [ + + ]: 7 : : nCreateTime{nCreateTimeIn} {}
29 : :
30 : : /**
31 : : * Create a ban entry from JSON.
32 : : * @param[in] json A JSON representation of a ban entry, as created by `ToJson()`.
33 : : * @throw std::runtime_error if the JSON does not have the expected fields.
34 : : */
35 : : explicit CBanEntry(const UniValue& json);
36 : :
37 : : /**
38 : : * Generate a JSON representation of this ban entry.
39 : : * @return JSON suitable for passing to the `CBanEntry(const UniValue&)` constructor.
40 : : */
41 : : UniValue ToJson() const;
42 : : };
43 : :
44 : : using banmap_t = std::map<CSubNet, CBanEntry>;
45 : :
46 : : /**
47 : : * Convert a `banmap_t` object to a JSON array.
48 : : * @param[in] bans Bans list to convert.
49 : : * @return a JSON array, similar to the one returned by the `listbanned` RPC. Suitable for
50 : : * passing to `BanMapFromJson()`.
51 : : */
52 : : UniValue BanMapToJson(const banmap_t& bans);
53 : :
54 : : /**
55 : : * Convert a JSON array to a `banmap_t` object.
56 : : * @param[in] bans_json JSON to convert, must be as returned by `BanMapToJson()`.
57 : : * @param[out] bans Bans list to create from the JSON.
58 : : * @throws std::runtime_error if the JSON does not have the expected fields or they contain
59 : : * unparsable values.
60 : : */
61 : : void BanMapFromJson(const UniValue& bans_json, banmap_t& bans);
62 : :
63 : : #endif // BITCOIN_NET_TYPES_H
|