6 #if defined(HAVE_CONFIG_H)
33 class DbNotFoundError :
public std::exception
35 using std::exception::exception;
38 template <
typename Stream,
typename Data>
39 bool SerializeDB(Stream& stream,
const Data& data)
45 stream << hashwriter.GetHash();
46 }
catch (
const std::exception& e) {
47 LogError(
"%s: Serialize or I/O error - %s\n", __func__, e.what());
54 template <
typename Data>
55 bool SerializeFileDB(
const std::string&
prefix,
const fs::path& path,
const Data& data)
58 const uint16_t randv{GetRand<uint16_t>()};
65 if (fileout.IsNull()) {
73 if (!SerializeDB(fileout, data)) {
89 LogError(
"%s: Rename-into-place failed\n", __func__);
96 template <
typename Stream,
typename Data>
97 void DeserializeDB(Stream& stream, Data&& data,
bool fCheckSum =
true)
102 verifier >> pchMsgTmp;
104 if (pchMsgTmp !=
Params().MessageStart()) {
105 throw std::runtime_error{
"Invalid network magic number"};
115 if (hashTmp != verifier.GetHash()) {
116 throw std::runtime_error{
"Checksum mismatch, data corrupted"};
121 template <
typename Data>
122 void DeserializeFileDB(
const fs::path& path, Data&& data)
126 if (filein.IsNull()) {
127 throw DbNotFoundError{};
129 DeserializeDB(filein, data);
134 : m_banlist_dat(ban_list_path +
".dat"),
135 m_banlist_json(ban_list_path +
".json")
141 std::vector<std::string> errors;
146 for (
const auto& err : errors) {
162 std::map<std::string, common::SettingsValue> settings;
163 std::vector<std::string> errors;
166 for (
const auto& err : errors) {
174 }
catch (
const std::runtime_error& e) {
185 return SerializeFileDB(
"peers", pathAddr, addr);
190 DeserializeDB(ssPeers, addr,
false);
198 auto addrman{std::make_unique<AddrMan>(netgroupman, deterministic, check_addrman)};
200 const auto start{SteadyClock::now()};
203 DeserializeFileDB(path_addr, *addrman);
204 LogPrintf(
"Loaded %i addresses from peers.dat %dms\n", addrman->Size(), Ticks<std::chrono::milliseconds>(SteadyClock::now() - start));
205 }
catch (
const DbNotFoundError&) {
207 addrman = std::make_unique<AddrMan>(netgroupman, deterministic, check_addrman);
212 return util::Error{
strprintf(
_(
"Failed to rename invalid peers.dat file. Please move or delete it and try again."))};
215 addrman = std::make_unique<AddrMan>(netgroupman, deterministic, check_addrman);
218 }
catch (
const std::exception& e) {
219 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."),
233 std::vector<CAddress> anchors;
237 }
catch (
const std::exception&) {
241 fs::remove(anchors_db_path);
std::vector< CAddress > ReadAnchors(const fs::path &anchors_db_path)
Read the anchor IP address database (anchors.dat)
bool DumpPeerAddresses(const ArgsManager &args, const AddrMan &addr)
util::Result< std::unique_ptr< AddrMan > > LoadAddrman(const NetGroupManager &netgroupman, const ArgsManager &args)
Returns an error string on failure.
void ReadFromStream(AddrMan &addr, DataStream &ssPeers)
Only used by tests.
void DumpAnchors(const fs::path &anchors_db_path, const std::vector< CAddress > &anchors)
Dump the anchor IP address database (anchors.dat)
static constexpr int32_t DEFAULT_ADDRMAN_CONSISTENCY_CHECKS
Default for -checkaddrman.
bool HasTestOption(const ArgsManager &args, const std::string &test_option)
Checks if a particular test option is present in -test command-line arg options.
#define PACKAGE_BUGREPORT
const CChainParams & Params()
Return the currently selected parameters.
Stochastic address manager.
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*.
static constexpr SerParams V2_DISK
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 MessageStartChars & MessageStart() const
Double ended buffer combining vector and stream-like interfaces.
Reads data from an underlying stream, while hashing the read data.
Writes data to an underlying source stream, while hashing the written data.
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
bool RenameOver(fs::path src, fs::path dest)
Rename src to dest.
bool FileCommit(FILE *file)
Ensure file contents are fully committed to disk, using a platform-specific feature analogous to fsyn...
std::array< uint8_t, 4 > MessageStartChars
bool WriteSettings(const fs::path &path, const std::map< std::string, SettingsValue > &values, std::vector< std::string > &errors)
Write settings file.
bool ReadSettings(const fs::path &path, std::map< std::string, SettingsValue > &values, std::vector< std::string > &errors)
Read settings file.
static path u8path(const std::string &utf8_str)
static auto quoted(const std::string &s)
static bool exists(const path &p)
static std::string PathToString(const path &path)
Convert path object to a byte string.
FILE * fopen(const fs::path &p, const char *mode)
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
#define LOG_TIME_SECONDS(end_msg)
bilingual_str _(const char *psz)
Translation function.