Dash Core Source Documentation (0.16.0.1)

Find detailed information regarding the Dash Core source code.

addrdb.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2015 The Bitcoin Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
6 #include <addrdb.h>
7 
8 #include <addrman.h>
9 #include <chainparams.h>
10 #include <clientversion.h>
11 #include <hash.h>
12 #include <random.h>
13 #include <streams.h>
14 #include <tinyformat.h>
15 #include <util.h>
16 
17 namespace {
18 
19 template <typename Stream, typename Data>
20 bool SerializeDB(Stream& stream, const Data& data)
21 {
22  // Write and commit header, data
23  try {
25  stream << FLATDATA(Params().MessageStart()) << data;
26  hasher << FLATDATA(Params().MessageStart()) << data;
27  stream << hasher.GetHash();
28  } catch (const std::exception& e) {
29  return error("%s: Serialize or I/O error - %s", __func__, e.what());
30  }
31 
32  return true;
33 }
34 
35 template <typename Data>
36 bool SerializeFileDB(const std::string& prefix, const fs::path& path, const Data& data)
37 {
38  // Generate random temporary filename
39  unsigned short randv = 0;
40  GetRandBytes((unsigned char*)&randv, sizeof(randv));
41  std::string tmpfn = strprintf("%s.%04x", prefix, randv);
42 
43  // open temp output file, and associate with CAutoFile
44  fs::path pathTmp = GetDataDir() / tmpfn;
45  FILE *file = fsbridge::fopen(pathTmp, "wb");
47  if (fileout.IsNull())
48  return error("%s: Failed to open file %s", __func__, pathTmp.string());
49 
50  // Serialize
51  if (!SerializeDB(fileout, data)) return false;
52  FileCommit(fileout.Get());
53  fileout.fclose();
54 
55  // replace existing file, if any, with new file
56  if (!RenameOver(pathTmp, path))
57  return error("%s: Rename-into-place failed", __func__);
58 
59  return true;
60 }
61 
62 template <typename Stream, typename Data>
63 bool DeserializeDB(Stream& stream, Data& data, bool fCheckSum = true)
64 {
65  try {
66  CHashVerifier<Stream> verifier(&stream);
67  // de-serialize file header (network specific magic number) and ..
68  unsigned char pchMsgTmp[4];
69  verifier >> FLATDATA(pchMsgTmp);
70  // ... verify the network matches ours
71  if (memcmp(pchMsgTmp, Params().MessageStart(), sizeof(pchMsgTmp)))
72  return error("%s: Invalid network magic number", __func__);
73 
74  // de-serialize data
75  verifier >> data;
76 
77  // verify checksum
78  if (fCheckSum) {
79  uint256 hashTmp;
80  stream >> hashTmp;
81  if (hashTmp != verifier.GetHash()) {
82  return error("%s: Checksum mismatch, data corrupted", __func__);
83  }
84  }
85  }
86  catch (const std::exception& e) {
87  return error("%s: Deserialize or I/O error - %s", __func__, e.what());
88  }
89 
90  return true;
91 }
92 
93 template <typename Data>
94 bool DeserializeFileDB(const fs::path& path, Data& data)
95 {
96  // open input file, and associate with CAutoFile
97  FILE *file = fsbridge::fopen(path, "rb");
98  CAutoFile filein(file, SER_DISK, CLIENT_VERSION);
99  if (filein.IsNull())
100  return error("%s: Failed to open file %s", __func__, path.string());
101 
102  return DeserializeDB(filein, data);
103 }
104 
105 }
106 
108 {
109  pathBanlist = GetDataDir() / "banlist.dat";
110 }
111 
112 bool CBanDB::Write(const banmap_t& banSet)
113 {
114  return SerializeFileDB("banlist", pathBanlist, banSet);
115 }
116 
117 bool CBanDB::Read(banmap_t& banSet)
118 {
119  return DeserializeFileDB(pathBanlist, banSet);
120 }
121 
123 {
124  pathAddr = GetDataDir() / "peers.dat";
125 }
126 
127 bool CAddrDB::Write(const CAddrMan& addr)
128 {
129  return SerializeFileDB("peers", pathAddr, addr);
130 }
131 
133 {
134  return DeserializeFileDB(pathAddr, addr);
135 }
136 
137 bool CAddrDB::Read(CAddrMan& addr, CDataStream& ssPeers)
138 {
139  bool ret = DeserializeDB(ssPeers, addr, false);
140  if (!ret) {
141  // Ensure addrman is left in a clean state
142  addr.Clear();
143  }
144  return ret;
145 }
FILE * fopen(const fs::path &p, const char *mode)
Definition: fs.cpp:5
void FileCommit(FILE *file)
Definition: util.cpp:1099
CBanDB()
Definition: addrdb.cpp:107
#define strprintf
Definition: tinyformat.h:1066
CAddrDB()
Definition: addrdb.cpp:122
static FILE * fileout
We use boost::call_once() to make sure mutexDebugLog and vMsgsBeforeOpenLog are initialized in a thre...
Definition: util.cpp:192
const char * prefix
Definition: rest.cpp:574
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:103
#define FLATDATA(obj)
Definition: serialize.h:370
Reads data from an underlying stream, while hashing the read data.
Definition: hash.h:219
Stochastical (IP) address manager.
Definition: addrman.h:182
std::map< CSubNet, CBanEntry > banmap_t
Definition: addrdb.h:77
bool Write(const CAddrMan &addr)
Definition: addrdb.cpp:127
bool RenameOver(fs::path src, fs::path dest)
Definition: util.cpp:1069
bool Write(const banmap_t &banSet)
Definition: addrdb.cpp:112
fs::path pathBanlist
Definition: addrdb.h:95
bool Read(banmap_t &banSet)
Definition: addrdb.cpp:117
256-bit opaque blob.
Definition: uint256.h:123
void Clear()
Definition: addrman.h:462
const CChainParams & Params()
Return the currently selected parameters.
fs::path pathAddr
Definition: addrdb.h:83
void GetRandBytes(unsigned char *buf, int num)
Functions to gather random data via the OpenSSL PRNG.
Definition: random.cpp:273
bool error(const char *fmt, const Args &... args)
Definition: util.h:222
A writer stream (for serialization) that computes a 256-bit hash.
Definition: hash.h:184
const fs::path & GetDataDir(bool fNetSpecific)
Definition: util.cpp:928
static const int CLIENT_VERSION
dashd-res.rc includes this file, but it cannot cope with real c++ code.
Definition: clientversion.h:38
bool Read(CAddrMan &addr)
Definition: addrdb.cpp:132
Non-refcounted RAII wrapper for FILE*.
Definition: streams.h:410
Released under the MIT license