Bitcoin Core  22.0.0
P2P Digital Currency
addrdb.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2020 The Bitcoin Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
6 #ifndef BITCOIN_ADDRDB_H
7 #define BITCOIN_ADDRDB_H
8 
9 #include <fs.h>
10 #include <net_types.h> // For banmap_t
11 #include <serialize.h>
12 #include <univalue.h>
13 
14 #include <string>
15 #include <vector>
16 
17 class CAddress;
18 class CAddrMan;
19 class CDataStream;
20 
21 class CBanEntry
22 {
23 public:
24  static const int CURRENT_VERSION=1;
25  int nVersion;
26  int64_t nCreateTime;
27  int64_t nBanUntil;
28 
30  {
31  SetNull();
32  }
33 
34  explicit CBanEntry(int64_t nCreateTimeIn)
35  {
36  SetNull();
37  nCreateTime = nCreateTimeIn;
38  }
39 
45  explicit CBanEntry(const UniValue& json);
46 
48  {
49  uint8_t ban_reason = 2;
50  READWRITE(obj.nVersion, obj.nCreateTime, obj.nBanUntil, ban_reason);
51  }
52 
53  void SetNull()
54  {
56  nCreateTime = 0;
57  nBanUntil = 0;
58  }
59 
64  UniValue ToJson() const;
65 };
66 
68 class CAddrDB
69 {
70 private:
71  fs::path pathAddr;
72 public:
73  CAddrDB();
74  bool Write(const CAddrMan& addr);
75  bool Read(CAddrMan& addr);
76  static bool Read(CAddrMan& addr, CDataStream& ssPeers);
77 };
78 
80 class CBanDB
81 {
82 private:
86  static constexpr const char* JSON_KEY = "banned_nets";
87 
88  const fs::path m_banlist_dat;
89  const fs::path m_banlist_json;
90 public:
91  explicit CBanDB(fs::path ban_list_path);
92  bool Write(const banmap_t& banSet);
93 
102  bool Read(banmap_t& banSet, bool& dirty);
103 };
104 
111 void DumpAnchors(const fs::path& anchors_db_path, const std::vector<CAddress>& anchors);
112 
119 std::vector<CAddress> ReadAnchors(const fs::path& anchors_db_path);
120 
121 #endif // BITCOIN_ADDRDB_H
char const * json() noexcept
Template to generate JSON data.
void SetNull()
Definition: addrdb.h:53
Access to the (IP) address database (peers.dat)
Definition: addrdb.h:68
CBanEntry(int64_t nCreateTimeIn)
Definition: addrdb.h:34
CAddrDB()
Definition: addrdb.cpp:231
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:204
bool Read(banmap_t &banSet, bool &dirty)
Read the banlist from disk.
Definition: addrdb.cpp:200
int nVersion
Definition: addrdb.h:25
int64_t nCreateTime
Definition: addrdb.h:26
CBanEntry()
Definition: addrdb.h:29
Stochastical (IP) address manager.
Definition: addrman.h:173
Access to the banlist databases (banlist.json and banlist.dat)
Definition: addrdb.h:80
bool Write(const CAddrMan &addr)
Definition: addrdb.cpp:236
CBanDB(fs::path ban_list_path)
Definition: addrdb.cpp:181
A CService with information about it as peer.
Definition: protocol.h:358
static constexpr const char * JSON_KEY
JSON key under which the data is stored in the json database.
Definition: addrdb.h:86
bool Write(const banmap_t &banSet)
Definition: addrdb.cpp:187
int64_t nBanUntil
Definition: addrdb.h:27
const fs::path m_banlist_dat
Definition: addrdb.h:88
std::vector< CAddress > ReadAnchors(const fs::path &anchors_db_path)
Read the anchor IP address database (anchors.dat)
Definition: addrdb.cpp:262
void DumpAnchors(const fs::path &anchors_db_path, const std::vector< CAddress > &anchors)
Dump the anchor IP address database (anchors.dat)
Definition: addrdb.cpp:256
fs::path pathAddr
Definition: addrdb.h:71
const fs::path m_banlist_json
Definition: addrdb.h:89
SERIALIZE_METHODS(CBanEntry, obj)
Definition: addrdb.h:47
std::map< CSubNet, CBanEntry > banmap_t
Definition: net_types.h:13
static const int CURRENT_VERSION
Definition: addrdb.h:24
Definition: addrdb.h:21
#define READWRITE(...)
Definition: serialize.h:147
UniValue ToJson() const
Generate a JSON representation of this ban entry.
Definition: addrdb.cpp:28
bool Read(CAddrMan &addr)
Definition: addrdb.cpp:241