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 : 11 : class CSubNet; 12 : class UniValue; 13 : 14 : class CBanEntry 15 : { 16 : public: 17 : static constexpr int CURRENT_VERSION{1}; 18 0 : int nVersion{CBanEntry::CURRENT_VERSION}; 19 0 : int64_t nCreateTime{0}; 20 0 : int64_t nBanUntil{0}; 21 : 22 0 : CBanEntry() {} 23 : 24 0 : explicit CBanEntry(int64_t nCreateTimeIn) 25 0 : : 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 : }; 40 : 41 : using banmap_t = std::map<CSubNet, CBanEntry>; 42 : 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 : 60 : #endif // BITCOIN_NET_TYPES_H