aboutsummaryrefslogtreecommitdiff
path: root/src/net_types.h
blob: b9e019d8fdeeda5dc2b1767fca4d5331fd884271 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Copyright (c) 2019-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.

#ifndef BITCOIN_NET_TYPES_H
#define BITCOIN_NET_TYPES_H

#include <cstdint>
#include <map>

class CSubNet;
class UniValue;

class CBanEntry
{
public:
    static constexpr int CURRENT_VERSION{1};
    int nVersion{CBanEntry::CURRENT_VERSION};
    int64_t nCreateTime{0};
    int64_t nBanUntil{0};

    CBanEntry() {}

    explicit CBanEntry(int64_t nCreateTimeIn)
        : nCreateTime{nCreateTimeIn} {}

    /**
     * Create a ban entry from JSON.
     * @param[in] json A JSON representation of a ban entry, as created by `ToJson()`.
     * @throw std::runtime_error if the JSON does not have the expected fields.
     */
    explicit CBanEntry(const UniValue& json);

    /**
     * Generate a JSON representation of this ban entry.
     * @return JSON suitable for passing to the `CBanEntry(const UniValue&)` constructor.
     */
    UniValue ToJson() const;
};

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

/**
 * 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);

/**
 * 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);

#endif // BITCOIN_NET_TYPES_H