Bitcoin Core  22.99.0
P2P Digital Currency
addrdb.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2021 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 #include <addrdb.h>
7 
8 #include <addrman.h>
9 #include <chainparams.h>
10 #include <clientversion.h>
11 #include <cstdint>
12 #include <fs.h>
13 #include <hash.h>
14 #include <logging/timer.h>
15 #include <netbase.h>
16 #include <random.h>
17 #include <streams.h>
18 #include <tinyformat.h>
19 #include <univalue.h>
20 #include <util/settings.h>
21 #include <util/system.h>
22 #include <util/translation.h>
23 
24 namespace {
25 
26 class DbNotFoundError : public std::exception
27 {
28  using std::exception::exception;
29 };
30 
31 template <typename Stream, typename Data>
32 bool SerializeDB(Stream& stream, const Data& data)
33 {
34  // Write and commit header, data
35  try {
36  CHashWriter hasher(stream.GetType(), stream.GetVersion());
37  stream << Params().MessageStart() << data;
38  hasher << Params().MessageStart() << data;
39  stream << hasher.GetHash();
40  } catch (const std::exception& e) {
41  return error("%s: Serialize or I/O error - %s", __func__, e.what());
42  }
43 
44  return true;
45 }
46 
47 template <typename Data>
48 bool SerializeFileDB(const std::string& prefix, const fs::path& path, const Data& data, int version)
49 {
50  // Generate random temporary filename
51  uint16_t randv = 0;
52  GetRandBytes((unsigned char*)&randv, sizeof(randv));
53  std::string tmpfn = strprintf("%s.%04x", prefix, randv);
54 
55  // open temp output file, and associate with CAutoFile
56  fs::path pathTmp = gArgs.GetDataDirNet() / tmpfn;
57  FILE *file = fsbridge::fopen(pathTmp, "wb");
58  CAutoFile fileout(file, SER_DISK, version);
59  if (fileout.IsNull()) {
60  fileout.fclose();
61  remove(pathTmp);
62  return error("%s: Failed to open file %s", __func__, fs::PathToString(pathTmp));
63  }
64 
65  // Serialize
66  if (!SerializeDB(fileout, data)) {
67  fileout.fclose();
68  remove(pathTmp);
69  return false;
70  }
71  if (!FileCommit(fileout.Get())) {
72  fileout.fclose();
73  remove(pathTmp);
74  return error("%s: Failed to flush file %s", __func__, fs::PathToString(pathTmp));
75  }
76  fileout.fclose();
77 
78  // replace existing file, if any, with new file
79  if (!RenameOver(pathTmp, path)) {
80  remove(pathTmp);
81  return error("%s: Rename-into-place failed", __func__);
82  }
83 
84  return true;
85 }
86 
87 template <typename Stream, typename Data>
88 void DeserializeDB(Stream& stream, Data& data, bool fCheckSum = true)
89 {
90  CHashVerifier<Stream> verifier(&stream);
91  // de-serialize file header (network specific magic number) and ..
92  unsigned char pchMsgTmp[4];
93  verifier >> pchMsgTmp;
94  // ... verify the network matches ours
95  if (memcmp(pchMsgTmp, Params().MessageStart(), sizeof(pchMsgTmp))) {
96  throw std::runtime_error{"Invalid network magic number"};
97  }
98 
99  // de-serialize data
100  verifier >> data;
101 
102  // verify checksum
103  if (fCheckSum) {
104  uint256 hashTmp;
105  stream >> hashTmp;
106  if (hashTmp != verifier.GetHash()) {
107  throw std::runtime_error{"Checksum mismatch, data corrupted"};
108  }
109  }
110 }
111 
112 template <typename Data>
113 void DeserializeFileDB(const fs::path& path, Data& data, int version)
114 {
115  // open input file, and associate with CAutoFile
116  FILE* file = fsbridge::fopen(path, "rb");
117  CAutoFile filein(file, SER_DISK, version);
118  if (filein.IsNull()) {
119  throw DbNotFoundError{};
120  }
121  DeserializeDB(filein, data);
122 }
123 } // namespace
124 
125 CBanDB::CBanDB(fs::path ban_list_path)
126  : m_banlist_dat(ban_list_path + ".dat"),
127  m_banlist_json(ban_list_path + ".json")
128 {
129 }
130 
131 bool CBanDB::Write(const banmap_t& banSet)
132 {
133  std::vector<std::string> errors;
134  if (util::WriteSettings(m_banlist_json, {{JSON_KEY, BanMapToJson(banSet)}}, errors)) {
135  return true;
136  }
137 
138  for (const auto& err : errors) {
139  error("%s", err);
140  }
141  return false;
142 }
143 
144 bool CBanDB::Read(banmap_t& banSet)
145 {
146  if (fs::exists(m_banlist_dat)) {
147  LogPrintf("banlist.dat ignored because it can only be read by " PACKAGE_NAME " version 22.x. Remove %s to silence this warning.\n", fs::quoted(fs::PathToString(m_banlist_dat)));
148  }
149  // If the JSON banlist does not exist, then recreate it
150  if (!fs::exists(m_banlist_json)) {
151  return false;
152  }
153 
154  std::map<std::string, util::SettingsValue> settings;
155  std::vector<std::string> errors;
156 
157  if (!util::ReadSettings(m_banlist_json, settings, errors)) {
158  for (const auto& err : errors) {
159  LogPrintf("Cannot load banlist %s: %s\n", fs::PathToString(m_banlist_json), err);
160  }
161  return false;
162  }
163 
164  try {
165  BanMapFromJson(settings[JSON_KEY], banSet);
166  } catch (const std::runtime_error& e) {
167  LogPrintf("Cannot parse banlist %s: %s\n", fs::PathToString(m_banlist_json), e.what());
168  return false;
169  }
170 
171  return true;
172 }
173 
174 bool DumpPeerAddresses(const ArgsManager& args, const AddrMan& addr)
175 {
176  const auto pathAddr = args.GetDataDirNet() / "peers.dat";
177  return SerializeFileDB("peers", pathAddr, addr, CLIENT_VERSION);
178 }
179 
180 void ReadFromStream(AddrMan& addr, CDataStream& ssPeers)
181 {
182  DeserializeDB(ssPeers, addr, false);
183 }
184 
185 std::optional<bilingual_str> LoadAddrman(const std::vector<bool>& asmap, const ArgsManager& args, std::unique_ptr<AddrMan>& addrman)
186 {
187  auto check_addrman = std::clamp<int32_t>(args.GetIntArg("-checkaddrman", DEFAULT_ADDRMAN_CONSISTENCY_CHECKS), 0, 1000000);
188  addrman = std::make_unique<AddrMan>(asmap, /* deterministic */ false, /* consistency_check_ratio */ check_addrman);
189 
190  int64_t nStart = GetTimeMillis();
191  const auto path_addr{args.GetDataDirNet() / "peers.dat"};
192  try {
193  DeserializeFileDB(path_addr, *addrman, CLIENT_VERSION);
194  LogPrintf("Loaded %i addresses from peers.dat %dms\n", addrman->size(), GetTimeMillis() - nStart);
195  } catch (const DbNotFoundError&) {
196  // Addrman can be in an inconsistent state after failure, reset it
197  addrman = std::make_unique<AddrMan>(asmap, /* deterministic */ false, /* consistency_check_ratio */ check_addrman);
198  LogPrintf("Creating peers.dat because the file was not found (%s)\n", fs::quoted(fs::PathToString(path_addr)));
199  DumpPeerAddresses(args, *addrman);
200  } catch (const std::exception& e) {
201  addrman = nullptr;
202  return strprintf(_("Invalid or corrupt peers.dat (%s). If you believe this is a bug, please report it to %s. As a workaround, you can move the file (%s) out of the way (rename, move, or delete) to have a new one created on the next start."),
203  e.what(), PACKAGE_BUGREPORT, fs::quoted(fs::PathToString(path_addr)));
204  }
205  return std::nullopt;
206 }
207 
208 void DumpAnchors(const fs::path& anchors_db_path, const std::vector<CAddress>& anchors)
209 {
210  LOG_TIME_SECONDS(strprintf("Flush %d outbound block-relay-only peer addresses to anchors.dat", anchors.size()));
211  SerializeFileDB("anchors", anchors_db_path, anchors, CLIENT_VERSION | ADDRV2_FORMAT);
212 }
213 
214 std::vector<CAddress> ReadAnchors(const fs::path& anchors_db_path)
215 {
216  std::vector<CAddress> anchors;
217  try {
218  DeserializeFileDB(anchors_db_path, anchors, CLIENT_VERSION | ADDRV2_FORMAT);
219  LogPrintf("Loaded %i addresses from %s\n", anchors.size(), fs::quoted(fs::PathToString(anchors_db_path.filename())));
220  } catch (const std::exception&) {
221  anchors.clear();
222  }
223 
224  fs::remove(anchors_db_path);
225  return anchors;
226 }
fs::path::filename
path filename() const
Definition: fs.h:57
ADDRV2_FORMAT
static constexpr int ADDRV2_FORMAT
A flag that is ORed into the protocol version to designate that addresses should be serialized in (un...
Definition: netaddress.h:34
util::WriteSettings
bool WriteSettings(const fs::path &path, const std::map< std::string, SettingsValue > &values, std::vector< std::string > &errors)
Write settings file.
Definition: settings.cpp:107
CBanDB::Read
bool Read(banmap_t &banSet)
Read the banlist from disk.
Definition: addrdb.cpp:144
fs::exists
static bool exists(const path &p)
Definition: fs.h:69
_
bilingual_str _(const char *psz)
Translation function.
Definition: translation.h:63
SER_DISK
@ SER_DISK
Definition: serialize.h:132
LOG_TIME_SECONDS
#define LOG_TIME_SECONDS(end_msg)
Definition: timer.h:105
fs.h
CHashVerifier
Reads data from an underlying stream, while hashing the read data.
Definition: hash.h:158
PACKAGE_BUGREPORT
#define PACKAGE_BUGREPORT
Definition: bitcoin-config.h:351
BanMapToJson
UniValue BanMapToJson(const banmap_t &bans)
Convert a banmap_t object to a JSON array.
Definition: net_types.cpp:38
CBanDB::JSON_KEY
static constexpr const char * JSON_KEY
JSON key under which the data is stored in the json database.
Definition: addrdb.h:33
fsbridge::fopen
FILE * fopen(const fs::path &p, const char *mode)
Definition: fs.cpp:27
ArgsManager::GetDataDirNet
const fs::path & GetDataDirNet() const
Get data directory path with appended network identifier.
Definition: system.h:304
streams.h
CBanDB::m_banlist_json
const fs::path m_banlist_json
Definition: addrdb.h:36
DumpAnchors
void DumpAnchors(const fs::path &anchors_db_path, const std::vector< CAddress > &anchors)
Dump the anchor IP address database (anchors.dat)
Definition: addrdb.cpp:208
CBanDB::Write
bool Write(const banmap_t &banSet)
Definition: addrdb.cpp:131
FileCommit
bool FileCommit(FILE *file)
Ensure file contents are fully committed to disk, using a platform-specific feature analogous to fsyn...
Definition: system.cpp:1100
BanMapFromJson
void BanMapFromJson(const UniValue &bans_json, banmap_t &bans)
Convert a JSON array to a banmap_t object.
Definition: net_types.cpp:58
AddrMan
Stochastic address manager.
Definition: addrman.h:79
fs::quoted
static auto quoted(const std::string &s)
Definition: fs.h:75
clientversion.h
fs::PathToString
static std::string PathToString(const path &path)
Convert path object to a byte string.
Definition: fs.h:112
chainparams.h
tinyformat.h
prefix
const char * prefix
Definition: rest.cpp:926
CBanDB::m_banlist_dat
const fs::path m_banlist_dat
Definition: addrdb.h:35
CAutoFile
Non-refcounted RAII wrapper for FILE*.
Definition: streams.h:478
util::ReadSettings
bool ReadSettings(const fs::path &path, std::map< std::string, SettingsValue > &values, std::vector< std::string > &errors)
Read settings file.
Definition: settings.cpp:64
addrdb.h
RenameOver
bool RenameOver(fs::path src, fs::path dest)
Rename src to dest.
Definition: system.cpp:1063
settings.h
random.h
fs::path
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
Definition: fs.h:29
PACKAGE_NAME
#define PACKAGE_NAME
Definition: bitcoin-config.h:354
univalue.h
timer.h
GetRandBytes
void GetRandBytes(unsigned char *buf, int num) noexcept
Overall design of the RNG and entropy sources.
Definition: random.cpp:581
LogPrintf
#define LogPrintf(...)
Definition: logging.h:188
uint256
256-bit opaque blob.
Definition: uint256.h:126
LoadAddrman
std::optional< bilingual_str > LoadAddrman(const std::vector< bool > &asmap, const ArgsManager &args, std::unique_ptr< AddrMan > &addrman)
Returns an error string on failure.
Definition: addrdb.cpp:185
gArgs
ArgsManager gArgs
Definition: system.cpp:87
CChainParams::MessageStart
const CMessageHeader::MessageStartChars & MessageStart() const
Definition: chainparams.h:83
system.h
strprintf
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1164
ArgsManager
Definition: system.h:164
translation.h
CLIENT_VERSION
static const int CLIENT_VERSION
bitcoind-res.rc includes this file, but it cannot cope with real c++ code.
Definition: clientversion.h:33
CHashWriter
A writer stream (for serialization) that computes a 256-bit hash.
Definition: hash.h:100
Params
const CChainParams & Params()
Return the currently selected parameters.
Definition: chainparams.cpp:561
CBanDB::CBanDB
CBanDB(fs::path ban_list_path)
Definition: addrdb.cpp:125
hash.h
CDataStream
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:184
ReadAnchors
std::vector< CAddress > ReadAnchors(const fs::path &anchors_db_path)
Read the anchor IP address database (anchors.dat)
Definition: addrdb.cpp:214
addrman.h
ReadFromStream
void ReadFromStream(AddrMan &addr, CDataStream &ssPeers)
Only used by tests.
Definition: addrdb.cpp:180
DEFAULT_ADDRMAN_CONSISTENCY_CHECKS
static constexpr int32_t DEFAULT_ADDRMAN_CONSISTENCY_CHECKS
Default for -checkaddrman.
Definition: addrman.h:23
banmap_t
std::map< CSubNet, CBanEntry > banmap_t
Definition: net_types.h:41
netbase.h
error
bool error(const char *fmt, const Args &... args)
Definition: system.h:49
ArgsManager::GetIntArg
int64_t GetIntArg(const std::string &strArg, int64_t nDefault) const
Return integer argument or default value.
Definition: system.cpp:594
GetTimeMillis
int64_t GetTimeMillis()
Returns the system time (not mockable)
Definition: time.cpp:117
DumpPeerAddresses
bool DumpPeerAddresses(const ArgsManager &args, const AddrMan &addr)
Definition: addrdb.cpp:174
args
ArgsManager args
Definition: notifications.cpp:36