24 class DbNotFoundError :
public std::exception {
25 using std::exception::exception;
28 template <
typename Stream,
typename Data>
29 bool SerializeDB(
const CChainParams &chainParams, Stream &stream,
33 CHashWriter hasher(stream.GetType(), stream.GetVersion());
34 stream << chainParams.
DiskMagic() << data;
35 hasher << chainParams.
DiskMagic() << data;
36 stream << hasher.GetHash();
37 }
catch (
const std::exception &e) {
38 return error(
"%s: Serialize or I/O error - %s", __func__, e.what());
44 template <
typename Data>
46 const fs::path &path,
const Data &data,
int version) {
48 const uint16_t randv{GetRand<uint16_t>()};
55 if (fileout.IsNull()) {
58 return error(
"%s: Failed to open file %s", __func__,
63 if (!SerializeDB(chainParams, fileout, data)) {
71 return error(
"%s: Failed to flush file %s", __func__,
79 return error(
"%s: Rename-into-place failed", __func__);
85 template <
typename Stream,
typename Data>
86 void DeserializeDB(
const CChainParams &chainParams, Stream &stream, Data &data,
87 bool fCheckSum =
true) {
91 verifier >> pchMsgTmp;
93 if (memcmp(pchMsgTmp, std::begin(chainParams.
DiskMagic()),
95 throw std::runtime_error{
"Invalid network magic number"};
105 if (hashTmp != verifier.GetHash()) {
106 throw std::runtime_error{
"Checksum mismatch, data corrupted"};
111 template <
typename Data>
113 Data &data,
int version) {
117 if (filein.IsNull()) {
118 throw DbNotFoundError{};
121 DeserializeDB(chainParams, filein, data);
127 : m_ban_list_path(
std::move(ban_list_path)), chainParams(_chainParams) {}
141 }
catch (
const std::exception &) {
142 LogPrintf(
"Missing or invalid file %s\n",
153 return SerializeFileDB(chainParams,
"peers", pathAddr, addr,
159 DeserializeDB(chainParams, ssPeers, addr,
false);
165 auto check_addrman = std::clamp<int32_t>(
168 auto addrman{std::make_unique<AddrMan>(
169 asmap, check_addrman)};
174 DeserializeFileDB(chainparams, path_addr, *addrman,
CLIENT_VERSION);
175 LogPrintf(
"Loaded %i addresses from peers.dat %dms\n", addrman->size(),
177 }
catch (
const DbNotFoundError &) {
179 addrman = std::make_unique<AddrMan>(
180 asmap, check_addrman);
181 LogPrintf(
"Creating peers.dat because the file was not found (%s)\n",
187 strprintf(
_(
"Failed to rename invalid peers.dat file. "
188 "Please move or delete it and try again."))};
191 addrman = std::make_unique<AddrMan>(
192 asmap, check_addrman);
193 LogPrintf(
"Creating new peers.dat because the file version was not "
194 "compatible (%s). Original backed up to peers.dat.bak\n",
197 }
catch (
const std::exception &e) {
199 _(
"Invalid or corrupt peers.dat (%s). If you believe this is a "
200 "bug, please report it to %s. As a workaround, you can move the "
201 "file (%s) out of the way (rename, move, or delete) to have a "
202 "new one created on the next start."),
203 e.what(), PACKAGE_BUGREPORT,
210 return {std::move(addrman)};
215 const std::vector<CAddress> &anchors) {
217 "Flush %d outbound block-relay-only peer addresses to anchors.dat",
219 SerializeFileDB(chainParams,
"anchors", anchors_db_path, anchors,
225 std::vector<CAddress> anchors;
227 DeserializeFileDB(chainParams, anchors_db_path, anchors,
229 LogPrintf(
"Loaded %i addresses from %s\n", anchors.size(),
231 }
catch (
const std::exception &) {
235 fs::remove(anchors_db_path);
void ReadFromStream(const CChainParams &chainParams, AddrMan &addr, CDataStream &ssPeers)
Only used by tests.
bool DumpPeerAddresses(const CChainParams &chainParams, const ArgsManager &args, const AddrMan &addr)
std::vector< CAddress > ReadAnchors(const CChainParams &chainParams, const fs::path &anchors_db_path)
Read the anchor IP address database (anchors.dat)
void DumpAnchors(const CChainParams &chainParams, const fs::path &anchors_db_path, const std::vector< CAddress > &anchors)
Dump the anchor IP address database (anchors.dat)
util::Result< std::unique_ptr< AddrMan > > LoadAddrman(const CChainParams &chainparams, const std::vector< bool > &asmap, const ArgsManager &args)
Returns an error string on failure.
static constexpr int32_t DEFAULT_ADDRMAN_CONSISTENCY_CHECKS
Default for -checkaddrman.
Stochastic address manager.
const fs::path & GetDataDirNet() const
Get data directory path with appended network identifier.
int64_t GetIntArg(const std::string &strArg, int64_t nDefault) const
Return integer argument or default value.
bool Write(const banmap_t &banSet)
const fs::path m_ban_list_path
CBanDB(fs::path ban_list_path, const CChainParams &_chainParams)
bool Read(banmap_t &banSet)
const CChainParams & chainParams
CChainParams defines various tweakable parameters of a given instance of the Bitcoin system.
const CMessageHeader::MessageMagic & DiskMagic() const
Double ended buffer combining vector and stream-like interfaces.
Reads data from an underlying stream, while hashing the read data.
A writer stream (for serialization) that computes a 256-bit hash.
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
static constexpr int CLIENT_VERSION
bitcoind-res.rc includes this file, but it cannot cope with real c++ code.
static auto quoted(const std::string &s)
static std::string PathToString(const path &path)
Convert path object to byte string.
FILE * fopen(const fs::path &p, const char *mode)
Implement std::hash so RCUPtr can be used as a key for maps or sets.
std::map< CSubNet, CBanEntry > banmap_t
static constexpr int ADDRV2_FORMAT
A flag that is ORed into the protocol version to designate that addresses should be serialized in (un...
bool RenameOver(fs::path src, fs::path dest)
bool FileCommit(FILE *file)
bool error(const char *fmt, const Args &...args)
int64_t GetTimeMillis()
Returns the system time (not mockable)
#define LOG_TIME_SECONDS(end_msg)
bilingual_str _(const char *psz)
Translation function.