Bitcoin Core  27.1.0
P2P Digital Currency
addrdb.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2022 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 #if defined(HAVE_CONFIG_H)
8 #endif
9 
10 #include <addrdb.h>
11 
12 #include <addrman.h>
13 #include <chainparams.h>
14 #include <clientversion.h>
15 #include <common/args.h>
16 #include <common/settings.h>
17 #include <cstdint>
18 #include <hash.h>
19 #include <logging.h>
20 #include <logging/timer.h>
21 #include <netbase.h>
22 #include <netgroup.h>
23 #include <random.h>
24 #include <streams.h>
25 #include <tinyformat.h>
26 #include <univalue.h>
27 #include <util/fs.h>
28 #include <util/fs_helpers.h>
29 #include <util/translation.h>
30 
31 namespace {
32 
33 class DbNotFoundError : public std::exception
34 {
35  using std::exception::exception;
36 };
37 
38 template <typename Stream, typename Data>
39 bool SerializeDB(Stream& stream, const Data& data)
40 {
41  // Write and commit header, data
42  try {
43  HashedSourceWriter hashwriter{stream};
44  hashwriter << Params().MessageStart() << data;
45  stream << hashwriter.GetHash();
46  } catch (const std::exception& e) {
47  return error("%s: Serialize or I/O error - %s", __func__, e.what());
48  }
49 
50  return true;
51 }
52 
53 template <typename Data>
54 bool SerializeFileDB(const std::string& prefix, const fs::path& path, const Data& data)
55 {
56  // Generate random temporary filename
57  const uint16_t randv{GetRand<uint16_t>()};
58  std::string tmpfn = strprintf("%s.%04x", prefix, randv);
59 
60  // open temp output file
61  fs::path pathTmp = gArgs.GetDataDirNet() / fs::u8path(tmpfn);
62  FILE *file = fsbridge::fopen(pathTmp, "wb");
63  AutoFile fileout{file};
64  if (fileout.IsNull()) {
65  fileout.fclose();
66  remove(pathTmp);
67  return error("%s: Failed to open file %s", __func__, fs::PathToString(pathTmp));
68  }
69 
70  // Serialize
71  if (!SerializeDB(fileout, data)) {
72  fileout.fclose();
73  remove(pathTmp);
74  return false;
75  }
76  if (!FileCommit(fileout.Get())) {
77  fileout.fclose();
78  remove(pathTmp);
79  return error("%s: Failed to flush file %s", __func__, fs::PathToString(pathTmp));
80  }
81  fileout.fclose();
82 
83  // replace existing file, if any, with new file
84  if (!RenameOver(pathTmp, path)) {
85  remove(pathTmp);
86  return error("%s: Rename-into-place failed", __func__);
87  }
88 
89  return true;
90 }
91 
92 template <typename Stream, typename Data>
93 void DeserializeDB(Stream& stream, Data&& data, bool fCheckSum = true)
94 {
95  HashVerifier verifier{stream};
96  // de-serialize file header (network specific magic number) and ..
97  MessageStartChars pchMsgTmp;
98  verifier >> pchMsgTmp;
99  // ... verify the network matches ours
100  if (pchMsgTmp != Params().MessageStart()) {
101  throw std::runtime_error{"Invalid network magic number"};
102  }
103 
104  // de-serialize data
105  verifier >> data;
106 
107  // verify checksum
108  if (fCheckSum) {
109  uint256 hashTmp;
110  stream >> hashTmp;
111  if (hashTmp != verifier.GetHash()) {
112  throw std::runtime_error{"Checksum mismatch, data corrupted"};
113  }
114  }
115 }
116 
117 template <typename Data>
118 void DeserializeFileDB(const fs::path& path, Data&& data)
119 {
120  FILE* file = fsbridge::fopen(path, "rb");
121  AutoFile filein{file};
122  if (filein.IsNull()) {
123  throw DbNotFoundError{};
124  }
125  DeserializeDB(filein, data);
126 }
127 } // namespace
128 
129 CBanDB::CBanDB(fs::path ban_list_path)
130  : m_banlist_dat(ban_list_path + ".dat"),
131  m_banlist_json(ban_list_path + ".json")
132 {
133 }
134 
135 bool CBanDB::Write(const banmap_t& banSet)
136 {
137  std::vector<std::string> errors;
138  if (common::WriteSettings(m_banlist_json, {{JSON_KEY, BanMapToJson(banSet)}}, errors)) {
139  return true;
140  }
141 
142  for (const auto& err : errors) {
143  error("%s", err);
144  }
145  return false;
146 }
147 
148 bool CBanDB::Read(banmap_t& banSet)
149 {
150  if (fs::exists(m_banlist_dat)) {
151  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)));
152  }
153  // If the JSON banlist does not exist, then recreate it
154  if (!fs::exists(m_banlist_json)) {
155  return false;
156  }
157 
158  std::map<std::string, common::SettingsValue> settings;
159  std::vector<std::string> errors;
160 
161  if (!common::ReadSettings(m_banlist_json, settings, errors)) {
162  for (const auto& err : errors) {
163  LogPrintf("Cannot load banlist %s: %s\n", fs::PathToString(m_banlist_json), err);
164  }
165  return false;
166  }
167 
168  try {
169  BanMapFromJson(settings[JSON_KEY], banSet);
170  } catch (const std::runtime_error& e) {
171  LogPrintf("Cannot parse banlist %s: %s\n", fs::PathToString(m_banlist_json), e.what());
172  return false;
173  }
174 
175  return true;
176 }
177 
178 bool DumpPeerAddresses(const ArgsManager& args, const AddrMan& addr)
179 {
180  const auto pathAddr = args.GetDataDirNet() / "peers.dat";
181  return SerializeFileDB("peers", pathAddr, addr);
182 }
183 
184 void ReadFromStream(AddrMan& addr, DataStream& ssPeers)
185 {
186  DeserializeDB(ssPeers, addr, false);
187 }
188 
190 {
191  auto check_addrman = std::clamp<int32_t>(args.GetIntArg("-checkaddrman", DEFAULT_ADDRMAN_CONSISTENCY_CHECKS), 0, 1000000);
192  auto addrman{std::make_unique<AddrMan>(netgroupman, /*deterministic=*/false, /*consistency_check_ratio=*/check_addrman)};
193 
194  const auto start{SteadyClock::now()};
195  const auto path_addr{args.GetDataDirNet() / "peers.dat"};
196  try {
197  DeserializeFileDB(path_addr, *addrman);
198  LogPrintf("Loaded %i addresses from peers.dat %dms\n", addrman->Size(), Ticks<std::chrono::milliseconds>(SteadyClock::now() - start));
199  } catch (const DbNotFoundError&) {
200  // Addrman can be in an inconsistent state after failure, reset it
201  addrman = std::make_unique<AddrMan>(netgroupman, /*deterministic=*/false, /*consistency_check_ratio=*/check_addrman);
202  LogPrintf("Creating peers.dat because the file was not found (%s)\n", fs::quoted(fs::PathToString(path_addr)));
203  DumpPeerAddresses(args, *addrman);
204  } catch (const InvalidAddrManVersionError&) {
205  if (!RenameOver(path_addr, (fs::path)path_addr + ".bak")) {
206  return util::Error{strprintf(_("Failed to rename invalid peers.dat file. Please move or delete it and try again."))};
207  }
208  // Addrman can be in an inconsistent state after failure, reset it
209  addrman = std::make_unique<AddrMan>(netgroupman, /*deterministic=*/false, /*consistency_check_ratio=*/check_addrman);
210  LogPrintf("Creating new peers.dat because the file version was not compatible (%s). Original backed up to peers.dat.bak\n", fs::quoted(fs::PathToString(path_addr)));
211  DumpPeerAddresses(args, *addrman);
212  } catch (const std::exception& e) {
213  return util::Error{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."),
214  e.what(), PACKAGE_BUGREPORT, fs::quoted(fs::PathToString(path_addr)))};
215  }
216  return addrman;
217 }
218 
219 void DumpAnchors(const fs::path& anchors_db_path, const std::vector<CAddress>& anchors)
220 {
221  LOG_TIME_SECONDS(strprintf("Flush %d outbound block-relay-only peer addresses to anchors.dat", anchors.size()));
222  SerializeFileDB("anchors", anchors_db_path, CAddress::V2_DISK(anchors));
223 }
224 
225 std::vector<CAddress> ReadAnchors(const fs::path& anchors_db_path)
226 {
227  std::vector<CAddress> anchors;
228  try {
229  DeserializeFileDB(anchors_db_path, CAddress::V2_DISK(anchors));
230  LogPrintf("Loaded %i addresses from %s\n", anchors.size(), fs::quoted(fs::PathToString(anchors_db_path.filename())));
231  } catch (const std::exception&) {
232  anchors.clear();
233  }
234 
235  fs::remove(anchors_db_path);
236  return anchors;
237 }
UniValue BanMapToJson(const banmap_t &bans)
Convert a banmap_t object to a JSON array.
Definition: net_types.cpp:38
FILE * fopen(const fs::path &p, const char *mode)
Definition: fs.cpp:26
bool ReadSettings(const fs::path &path, std::map< std::string, SettingsValue > &values, std::vector< std::string > &errors)
Read settings file.
Definition: settings.cpp:74
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1162
const char * prefix
Definition: rest.cpp:1005
#define LOG_TIME_SECONDS(end_msg)
Definition: timer.h:107
bool DumpPeerAddresses(const ArgsManager &args, const AddrMan &addr)
Definition: addrdb.cpp:178
#define PACKAGE_NAME
void BanMapFromJson(const UniValue &bans_json, banmap_t &bans)
Convert a JSON array to a banmap_t object.
Definition: net_types.cpp:58
Netgroup manager.
Definition: netgroup.h:16
Non-refcounted RAII wrapper for FILE*.
Definition: streams.h:388
void DumpAnchors(const fs::path &anchors_db_path, const std::vector< CAddress > &anchors)
Dump the anchor IP address database (anchors.dat)
Definition: addrdb.cpp:219
Stochastic address manager.
Definition: addrman.h:87
bool RenameOver(fs::path src, fs::path dest)
Rename src to dest.
Definition: fs_helpers.cpp:250
util::Result< std::unique_ptr< AddrMan > > LoadAddrman(const NetGroupManager &netgroupman, const ArgsManager &args)
Returns an error string on failure.
Definition: addrdb.cpp:189
std::array< uint8_t, 4 > MessageStartChars
static constexpr SerParams V2_DISK
Definition: protocol.h:376
fs::path GetDataDirNet() const
Get data directory path with appended network identifier.
Definition: args.h:232
CBanDB(fs::path ban_list_path)
Definition: addrdb.cpp:129
static std::string PathToString(const path &path)
Convert path object to a byte string.
Definition: fs.h:151
ArgsManager & args
Definition: bitcoind.cpp:268
std::vector< CAddress > ReadAnchors(const fs::path &anchors_db_path)
Read the anchor IP address database (anchors.dat)
Definition: addrdb.cpp:225
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:146
bilingual_str _(const char *psz)
Translation function.
Definition: translation.h:74
static constexpr const char * JSON_KEY
JSON key under which the data is stored in the json database.
Definition: addrdb.h:34
bool Write(const banmap_t &banSet)
Definition: addrdb.cpp:135
ArgsManager gArgs
Definition: args.cpp:41
bool Read(banmap_t &banSet)
Read the banlist from disk.
Definition: addrdb.cpp:148
void ReadFromStream(AddrMan &addr, DataStream &ssPeers)
Only used by tests.
Definition: addrdb.cpp:184
256-bit opaque blob.
Definition: uint256.h:106
const fs::path m_banlist_dat
Definition: addrdb.h:36
bool error(const char *fmt, const Args &... args)
Definition: logging.h:267
const CChainParams & Params()
Return the currently selected parameters.
int64_t GetIntArg(const std::string &strArg, int64_t nDefault) const
Return integer argument or default value.
Definition: args.cpp:480
const MessageStartChars & MessageStart() const
Definition: chainparams.h:94
static constexpr int32_t DEFAULT_ADDRMAN_CONSISTENCY_CHECKS
Default for -checkaddrman.
Definition: addrman.h:31
const fs::path m_banlist_json
Definition: addrdb.h:37
int fclose()
Definition: streams.h:405
Writes data to an underlying source stream, while hashing the written data.
Definition: hash.h:184
bool WriteSettings(const fs::path &path, const std::map< std::string, SettingsValue > &values, std::vector< std::string > &errors)
Write settings file.
Definition: settings.cpp:125
Reads data from an underlying stream, while hashing the read data.
Definition: hash.h:150
std::map< CSubNet, CBanEntry > banmap_t
Definition: net_types.h:41
static auto quoted(const std::string &s)
Definition: fs.h:95
path filename() const
Definition: fs.h:72
static bool exists(const path &p)
Definition: fs.h:89
static path u8path(const std::string &utf8_str)
Definition: fs.h:75
#define LogPrintf(...)
Definition: logging.h:245
bool FileCommit(FILE *file)
Ensure file contents are fully committed to disk, using a platform-specific feature analogous to fsyn...
Definition: fs_helpers.cpp:109
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
Definition: fs.h:32
#define PACKAGE_BUGREPORT