19 template <
typename Stream,
typename Data>
20 bool SerializeDB(
const CChainParams &chainParams, Stream &stream,
25 stream << chainParams.
DiskMagic() << data;
26 hasher << chainParams.
DiskMagic() << data;
27 stream << hasher.GetHash();
28 }
catch (
const std::exception &e) {
29 return error(
"%s: Serialize or I/O error - %s", __func__, e.what());
35 template <
typename Data>
37 const fs::path &path,
const Data &data) {
39 unsigned short randv = 0;
41 std::string tmpfn =
strprintf(
"%s.%04x", prefix, randv);
47 if (fileout.IsNull()) {
50 return error(
"%s: Failed to open file %s", __func__, pathTmp.string());
54 if (!SerializeDB(chainParams, fileout, data)) {
62 return error(
"%s: Failed to flush file %s", __func__, pathTmp.string());
69 return error(
"%s: Rename-into-place failed", __func__);
75 template <
typename Stream,
typename Data>
76 bool DeserializeDB(
const CChainParams &chainParams, Stream &stream, Data &data,
77 bool fCheckSum =
true) {
82 verifier >> pchMsgTmp;
84 if (memcmp(pchMsgTmp, std::begin(chainParams.
DiskMagic()),
86 return error(
"%s: Invalid network magic number", __func__);
96 if (hashTmp != verifier.GetHash()) {
97 return error(
"%s: Checksum mismatch, data corrupted", __func__);
100 }
catch (
const std::exception &e) {
101 return error(
"%s: Deserialize or I/O error - %s", __func__, e.what());
107 template <
typename Data>
108 bool DeserializeFileDB(
const CChainParams &chainParams,
const fs::path &path,
113 if (filein.IsNull()) {
114 return error(
"%s: Failed to open file %s", __func__, path.string());
117 return DeserializeDB(chainParams, filein, data);
123 : m_ban_list_path(
std::move(ban_list_path)), chainParams(_chainParams) {}
126 return SerializeFileDB(chainParams,
"banlist",
m_ban_list_path, banSet);
134 : chainParams(chainParamsIn) {
139 return SerializeFileDB(chainParams,
"peers",
pathAddr, addr);
143 return DeserializeFileDB(chainParams,
pathAddr, addr);
147 bool ret = DeserializeDB(chainParams, ssPeers, addr,
false);
bool FileCommit(FILE *file)
FILE * fopen(const fs::path &p, const char *mode)
CBanDB(fs::path ban_list_path, const CChainParams &_chainParams)
const CMessageHeader::MessageMagic & DiskMagic() const
CChainParams defines various tweakable parameters of a given instance of the Bitcoin system...
Double ended buffer combining vector and stream-like interfaces.
const fs::path m_ban_list_path
Reads data from an underlying stream, while hashing the read data.
void GetRandBytes(uint8_t *buf, int num) noexcept
Overall design of the RNG and entropy sources.
Stochastical (IP) address manager.
bool Write(const CAddrMan &addr)
const fs::path & GetDataDir(bool fNetSpecific)
bool Write(const banmap_t &banSet)
CAddrDB(const CChainParams &chainParams)
bool RenameOver(fs::path src, fs::path dest)
bool Read(banmap_t &banSet)
static constexpr int CLIENT_VERSION
bitcoind-res.rc includes this file, but it cannot cope with real c++ code.
A writer stream (for serialization) that computes a 256-bit hash.
std::map< CSubNet, CBanEntry > banmap_t
bool error(const char *fmt, const Args &... args)
bool Read(CAddrMan &addr)
Non-refcounted RAII wrapper for FILE*.