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