25class DbNotFoundError :
public std::exception
27 using std::exception::exception;
30template <
typename Stream,
typename Data>
31bool SerializeDB(Stream& stream,
const Data& data)
35 CHashWriter hasher(stream.GetType(), stream.GetVersion());
38 stream << hasher.GetHash();
39 }
catch (
const std::exception& e) {
40 return error(
"%s: Serialize or I/O error - %s", __func__, e.what());
46template <
typename Data>
47bool SerializeFileDB(
const std::string&
prefix,
const fs::path& path,
const Data& data,
int version)
58 if (fileout.IsNull()) {
65 if (!SerializeDB(fileout, data)) {
80 return error(
"%s: Rename-into-place failed", __func__);
86template <
typename Stream,
typename Data>
87void DeserializeDB(Stream& stream, Data& data,
bool fCheckSum =
true)
91 unsigned char pchMsgTmp[4];
92 verifier >> pchMsgTmp;
94 if (memcmp(pchMsgTmp,
Params().MessageStart(),
sizeof(pchMsgTmp))) {
95 throw std::runtime_error{
"Invalid network magic number"};
105 if (hashTmp != verifier.GetHash()) {
106 throw std::runtime_error{
"Checksum mismatch, data corrupted"};
111template <
typename Data>
112void DeserializeFileDB(
const fs::path& path, Data& data,
int version)
117 if (filein.IsNull()) {
118 throw DbNotFoundError{};
120 DeserializeDB(filein, data);
125 : m_banlist_dat(ban_list_path +
".dat"),
126 m_banlist_json(ban_list_path +
".json")
132 std::vector<std::string> errors;
137 for (
const auto& err : errors) {
153 std::map<std::string, util::SettingsValue> settings;
154 std::vector<std::string> errors;
157 for (
const auto& err : errors) {
165 }
catch (
const std::runtime_error& e) {
181 DeserializeDB(ssPeers, addr,
false);
184std::optional<bilingual_str>
LoadAddrman(
const std::vector<bool>& asmap,
const ArgsManager& args, std::unique_ptr<AddrMan>& addrman)
187 addrman = std::make_unique<AddrMan>(asmap,
false, check_addrman);
194 }
catch (
const DbNotFoundError&) {
196 addrman = std::make_unique<AddrMan>(asmap,
false, check_addrman);
199 }
catch (
const std::exception& e) {
201 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."),
215 std::vector<CAddress> anchors;
219 }
catch (
const std::exception&) {
223 fs::remove(anchors_db_path);
bool DumpPeerAddresses(const ArgsManager &args, const AddrMan &addr)
std::vector< CAddress > ReadAnchors(const fs::path &anchors_db_path)
Read the anchor IP address database (anchors.dat)
void DumpAnchors(const fs::path &anchors_db_path, const std::vector< CAddress > &anchors)
Dump the anchor IP address database (anchors.dat)
void ReadFromStream(AddrMan &addr, CDataStream &ssPeers)
Only used by tests.
std::optional< bilingual_str > LoadAddrman(const std::vector< bool > &asmap, const ArgsManager &args, std::unique_ptr< AddrMan > &addrman)
Returns an error string on failure.
static constexpr int32_t DEFAULT_ADDRMAN_CONSISTENCY_CHECKS
Default for -checkaddrman.
#define PACKAGE_BUGREPORT
const CChainParams & Params()
Return the currently selected parameters.
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.
Non-refcounted RAII wrapper for FILE*.
bool Write(const banmap_t &banSet)
const fs::path m_banlist_dat
bool Read(banmap_t &banSet)
Read the banlist from disk.
static constexpr const char * JSON_KEY
JSON key under which the data is stored in the json database.
const fs::path m_banlist_json
CBanDB(fs::path ban_list_path)
const CMessageHeader::MessageStartChars & MessageStart() 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 prepare application code for transition from boost::filesystem library to std::...
static const 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 bool exists(const path &p)
static std::string PathToString(const path &path)
Convert path object to byte string.
FILE * fopen(const fs::path &p, const char *mode)
bool ReadSettings(const fs::path &path, std::map< std::string, SettingsValue > &values, std::vector< std::string > &errors)
Read settings file.
bool WriteSettings(const fs::path &path, const std::map< std::string, SettingsValue > &values, std::vector< std::string > &errors)
Write settings file.
void BanMapFromJson(const UniValue &bans_json, banmap_t &bans)
Convert a JSON array to a banmap_t object.
UniValue BanMapToJson(const banmap_t &bans)
Convert a banmap_t object to a JSON array.
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...
void GetRandBytes(unsigned char *buf, int num) noexcept
Overall design of the RNG and entropy sources.
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.
bool RenameOver(fs::path src, fs::path dest)
bool FileCommit(FILE *file)
Ensure file contents are fully committed to disk, using a platform-specific feature analogous to fsyn...