29 class DbNotFoundError :
public std::exception
31 using std::exception::exception;
34 template <
typename Stream,
typename Data>
35 bool SerializeDB(Stream& stream,
const Data& data)
41 stream << hashwriter.GetHash();
42 }
catch (
const std::exception& e) {
43 return error(
"%s: Serialize or I/O error - %s", __func__, e.what());
49 template <
typename Data>
50 bool SerializeFileDB(
const std::string&
prefix,
const fs::path& path,
const Data& data)
53 const uint16_t randv{GetRand<uint16_t>()};
60 if (fileout.IsNull()) {
67 if (!SerializeDB(fileout, data)) {
82 return error(
"%s: Rename-into-place failed", __func__);
88 template <
typename Stream,
typename Data>
89 void DeserializeDB(Stream& stream, Data&& data,
bool fCheckSum =
true)
94 verifier >> pchMsgTmp;
96 if (pchMsgTmp !=
Params().MessageStart()) {
97 throw std::runtime_error{
"Invalid network magic number"};
107 if (hashTmp != verifier.GetHash()) {
108 throw std::runtime_error{
"Checksum mismatch, data corrupted"};
113 template <
typename Data>
114 void DeserializeFileDB(
const fs::path& path, Data&& data)
118 if (filein.IsNull()) {
119 throw DbNotFoundError{};
121 DeserializeDB(filein, data);
126 : m_banlist_dat(ban_list_path +
".dat"),
127 m_banlist_json(ban_list_path +
".json")
133 std::vector<std::string> errors;
138 for (
const auto& err : errors) {
154 std::map<std::string, common::SettingsValue> settings;
155 std::vector<std::string> errors;
158 for (
const auto& err : errors) {
166 }
catch (
const std::runtime_error& e) {
177 return SerializeFileDB(
"peers", pathAddr, addr);
182 DeserializeDB(ssPeers, addr,
false);
188 auto addrman{std::make_unique<AddrMan>(netgroupman,
false, check_addrman)};
190 const auto start{SteadyClock::now()};
193 DeserializeFileDB(path_addr, *addrman);
194 LogPrintf(
"Loaded %i addresses from peers.dat %dms\n", addrman->Size(), Ticks<std::chrono::milliseconds>(SteadyClock::now() - start));
195 }
catch (
const DbNotFoundError&) {
197 addrman = std::make_unique<AddrMan>(netgroupman,
false, check_addrman);
202 return util::Error{
strprintf(
_(
"Failed to rename invalid peers.dat file. Please move or delete it and try again."))};
205 addrman = std::make_unique<AddrMan>(netgroupman,
false, check_addrman);
208 }
catch (
const std::exception& e) {
209 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."),
212 return {std::move(addrman)};
224 std::vector<CAddress> anchors;
228 }
catch (
const std::exception&) {
232 fs::remove(anchors_db_path);
UniValue BanMapToJson(const banmap_t &bans)
Convert a banmap_t object to a JSON array.
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.
#define LOG_TIME_SECONDS(end_msg)
bool DumpPeerAddresses(const ArgsManager &args, const AddrMan &addr)
void BanMapFromJson(const UniValue &bans_json, banmap_t &bans)
Convert a JSON array to a banmap_t object.
Non-refcounted RAII wrapper for FILE*.
void DumpAnchors(const fs::path &anchors_db_path, const std::vector< CAddress > &anchors)
Dump the anchor IP address database (anchors.dat)
Stochastic address manager.
bool RenameOver(fs::path src, fs::path dest)
Rename src to dest.
util::Result< std::unique_ptr< AddrMan > > LoadAddrman(const NetGroupManager &netgroupman, const ArgsManager &args)
Returns an error string on failure.
std::array< uint8_t, 4 > MessageStartChars
static constexpr SerParams V2_DISK
CBanDB(fs::path ban_list_path)
static std::string PathToString(const path &path)
Convert path object to a byte string.
std::vector< CAddress > ReadAnchors(const fs::path &anchors_db_path)
Read the anchor IP address database (anchors.dat)
Double ended buffer combining vector and stream-like interfaces.
bilingual_str _(const char *psz)
Translation function.
static constexpr const char * JSON_KEY
JSON key under which the data is stored in the json database.
bool Write(const banmap_t &banSet)
bool Read(banmap_t &banSet)
Read the banlist from disk.
void ReadFromStream(AddrMan &addr, DataStream &ssPeers)
Only used by tests.
const fs::path m_banlist_dat
bool error(const char *fmt, const Args &... args)
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.
const MessageStartChars & MessageStart() const
static constexpr int32_t DEFAULT_ADDRMAN_CONSISTENCY_CHECKS
Default for -checkaddrman.
const fs::path m_banlist_json
Writes data to an underlying source stream, while hashing the written data.
bool WriteSettings(const fs::path &path, const std::map< std::string, SettingsValue > &values, std::vector< std::string > &errors)
Write settings file.
Reads data from an underlying stream, while hashing the read data.
std::map< CSubNet, CBanEntry > banmap_t
static auto quoted(const std::string &s)
static bool exists(const path &p)
static path u8path(const std::string &utf8_str)
bool FileCommit(FILE *file)
Ensure file contents are fully committed to disk, using a platform-specific feature analogous to fsyn...
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
#define PACKAGE_BUGREPORT
const fs::path & GetDataDirNet() const
Get data directory path with appended network identifier.