You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

db.h 9.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. // Copyright (c) 2009-2010 Satoshi Nakamoto
  2. // Copyright (c) 2009-2012 The Bitcoin developers
  3. // Distributed under the MIT/X11 software license, see the accompanying
  4. // file COPYING or http://www.opensource.org/licenses/mit-license.php.
  5. #ifndef BITCOIN_DB_H
  6. #define BITCOIN_DB_H
  7. #include "main.h"
  8. #include <map>
  9. #include <string>
  10. #include <vector>
  11. #include <db_cxx.h>
  12. class CAddress;
  13. class CAddrMan;
  14. class CBlockLocator;
  15. class CDiskBlockIndex;
  16. class CDiskTxPos;
  17. class CMasterKey;
  18. class COutPoint;
  19. class CTxIndex;
  20. class CWallet;
  21. class CWalletTx;
  22. extern unsigned int nWalletDBUpdated;
  23. void ThreadFlushWalletDB(void* parg);
  24. bool BackupWallet(const CWallet& wallet, const std::string& strDest);
  25. class CDBEnv
  26. {
  27. private:
  28. bool fDetachDB;
  29. bool fDbEnvInit;
  30. bool fMockDb;
  31. boost::filesystem::path pathEnv;
  32. void EnvShutdown();
  33. public:
  34. mutable CCriticalSection cs_db;
  35. DbEnv dbenv;
  36. std::map<std::string, int> mapFileUseCount;
  37. std::map<std::string, Db*> mapDb;
  38. CDBEnv();
  39. ~CDBEnv();
  40. void MakeMock();
  41. bool IsMock() { return fMockDb; };
  42. /*
  43. * Verify that database file strFile is OK. If it is not,
  44. * call the callback to try to recover.
  45. * This must be called BEFORE strFile is opened.
  46. * Returns true if strFile is OK.
  47. */
  48. enum VerifyResult { VERIFY_OK, RECOVER_OK, RECOVER_FAIL };
  49. VerifyResult Verify(std::string strFile, bool (*recoverFunc)(CDBEnv& dbenv, std::string strFile));
  50. /*
  51. * Salvage data from a file that Verify says is bad.
  52. * fAggressive sets the DB_AGGRESSIVE flag (see berkeley DB->verify() method documentation).
  53. * Appends binary key/value pairs to vResult, returns true if successful.
  54. * NOTE: reads the entire database into memory, so cannot be used
  55. * for huge databases.
  56. */
  57. typedef std::pair<std::vector<unsigned char>, std::vector<unsigned char> > KeyValPair;
  58. bool Salvage(std::string strFile, bool fAggressive, std::vector<KeyValPair>& vResult);
  59. bool Open(boost::filesystem::path pathEnv_);
  60. void Close();
  61. void Flush(bool fShutdown);
  62. void CheckpointLSN(std::string strFile);
  63. void SetDetach(bool fDetachDB_) { fDetachDB = fDetachDB_; }
  64. bool GetDetach() { return fDetachDB; }
  65. void CloseDb(const std::string& strFile);
  66. bool RemoveDb(const std::string& strFile);
  67. DbTxn *TxnBegin(int flags=DB_TXN_WRITE_NOSYNC)
  68. {
  69. DbTxn* ptxn = NULL;
  70. int ret = dbenv.txn_begin(NULL, &ptxn, flags);
  71. if (!ptxn || ret != 0)
  72. return NULL;
  73. return ptxn;
  74. }
  75. };
  76. extern CDBEnv bitdb;
  77. /** RAII class that provides access to a Berkeley database */
  78. class CDB
  79. {
  80. protected:
  81. Db* pdb;
  82. std::string strFile;
  83. DbTxn *activeTxn;
  84. bool fReadOnly;
  85. explicit CDB(const char* pszFile, const char* pszMode="r+");
  86. ~CDB() { Close(); }
  87. public:
  88. void Close();
  89. private:
  90. CDB(const CDB&);
  91. void operator=(const CDB&);
  92. protected:
  93. template<typename K, typename T>
  94. bool Read(const K& key, T& value)
  95. {
  96. if (!pdb)
  97. return false;
  98. // Key
  99. CDataStream ssKey(SER_DISK, CLIENT_VERSION);
  100. ssKey.reserve(1000);
  101. ssKey << key;
  102. Dbt datKey(&ssKey[0], ssKey.size());
  103. // Read
  104. Dbt datValue;
  105. datValue.set_flags(DB_DBT_MALLOC);
  106. int ret = pdb->get(activeTxn, &datKey, &datValue, 0);
  107. memset(datKey.get_data(), 0, datKey.get_size());
  108. if (datValue.get_data() == NULL)
  109. return false;
  110. // Unserialize value
  111. try {
  112. CDataStream ssValue((char*)datValue.get_data(), (char*)datValue.get_data() + datValue.get_size(), SER_DISK, CLIENT_VERSION);
  113. ssValue >> value;
  114. }
  115. catch (std::exception &e) {
  116. return false;
  117. }
  118. // Clear and free memory
  119. memset(datValue.get_data(), 0, datValue.get_size());
  120. free(datValue.get_data());
  121. return (ret == 0);
  122. }
  123. template<typename K, typename T>
  124. bool Write(const K& key, const T& value, bool fOverwrite=true)
  125. {
  126. if (!pdb)
  127. return false;
  128. if (fReadOnly)
  129. assert(!"Write called on database in read-only mode");
  130. // Key
  131. CDataStream ssKey(SER_DISK, CLIENT_VERSION);
  132. ssKey.reserve(1000);
  133. ssKey << key;
  134. Dbt datKey(&ssKey[0], ssKey.size());
  135. // Value
  136. CDataStream ssValue(SER_DISK, CLIENT_VERSION);
  137. ssValue.reserve(10000);
  138. ssValue << value;
  139. Dbt datValue(&ssValue[0], ssValue.size());
  140. // Write
  141. int ret = pdb->put(activeTxn, &datKey, &datValue, (fOverwrite ? 0 : DB_NOOVERWRITE));
  142. // Clear memory in case it was a private key
  143. memset(datKey.get_data(), 0, datKey.get_size());
  144. memset(datValue.get_data(), 0, datValue.get_size());
  145. return (ret == 0);
  146. }
  147. template<typename K>
  148. bool Erase(const K& key)
  149. {
  150. if (!pdb)
  151. return false;
  152. if (fReadOnly)
  153. assert(!"Erase called on database in read-only mode");
  154. // Key
  155. CDataStream ssKey(SER_DISK, CLIENT_VERSION);
  156. ssKey.reserve(1000);
  157. ssKey << key;
  158. Dbt datKey(&ssKey[0], ssKey.size());
  159. // Erase
  160. int ret = pdb->del(activeTxn, &datKey, 0);
  161. // Clear memory
  162. memset(datKey.get_data(), 0, datKey.get_size());
  163. return (ret == 0 || ret == DB_NOTFOUND);
  164. }
  165. template<typename K>
  166. bool Exists(const K& key)
  167. {
  168. if (!pdb)
  169. return false;
  170. // Key
  171. CDataStream ssKey(SER_DISK, CLIENT_VERSION);
  172. ssKey.reserve(1000);
  173. ssKey << key;
  174. Dbt datKey(&ssKey[0], ssKey.size());
  175. // Exists
  176. int ret = pdb->exists(activeTxn, &datKey, 0);
  177. // Clear memory
  178. memset(datKey.get_data(), 0, datKey.get_size());
  179. return (ret == 0);
  180. }
  181. Dbc* GetCursor()
  182. {
  183. if (!pdb)
  184. return NULL;
  185. Dbc* pcursor = NULL;
  186. int ret = pdb->cursor(NULL, &pcursor, 0);
  187. if (ret != 0)
  188. return NULL;
  189. return pcursor;
  190. }
  191. int ReadAtCursor(Dbc* pcursor, CDataStream& ssKey, CDataStream& ssValue, unsigned int fFlags=DB_NEXT)
  192. {
  193. // Read at cursor
  194. Dbt datKey;
  195. if (fFlags == DB_SET || fFlags == DB_SET_RANGE || fFlags == DB_GET_BOTH || fFlags == DB_GET_BOTH_RANGE)
  196. {
  197. datKey.set_data(&ssKey[0]);
  198. datKey.set_size(ssKey.size());
  199. }
  200. Dbt datValue;
  201. if (fFlags == DB_GET_BOTH || fFlags == DB_GET_BOTH_RANGE)
  202. {
  203. datValue.set_data(&ssValue[0]);
  204. datValue.set_size(ssValue.size());
  205. }
  206. datKey.set_flags(DB_DBT_MALLOC);
  207. datValue.set_flags(DB_DBT_MALLOC);
  208. int ret = pcursor->get(&datKey, &datValue, fFlags);
  209. if (ret != 0)
  210. return ret;
  211. else if (datKey.get_data() == NULL || datValue.get_data() == NULL)
  212. return 99999;
  213. // Convert to streams
  214. ssKey.SetType(SER_DISK);
  215. ssKey.clear();
  216. ssKey.write((char*)datKey.get_data(), datKey.get_size());
  217. ssValue.SetType(SER_DISK);
  218. ssValue.clear();
  219. ssValue.write((char*)datValue.get_data(), datValue.get_size());
  220. // Clear and free memory
  221. memset(datKey.get_data(), 0, datKey.get_size());
  222. memset(datValue.get_data(), 0, datValue.get_size());
  223. free(datKey.get_data());
  224. free(datValue.get_data());
  225. return 0;
  226. }
  227. public:
  228. bool TxnBegin()
  229. {
  230. if (!pdb || activeTxn)
  231. return false;
  232. DbTxn* ptxn = bitdb.TxnBegin();
  233. if (!ptxn)
  234. return false;
  235. activeTxn = ptxn;
  236. return true;
  237. }
  238. bool TxnCommit()
  239. {
  240. if (!pdb || !activeTxn)
  241. return false;
  242. int ret = activeTxn->commit(0);
  243. activeTxn = NULL;
  244. return (ret == 0);
  245. }
  246. bool TxnAbort()
  247. {
  248. if (!pdb || !activeTxn)
  249. return false;
  250. int ret = activeTxn->abort();
  251. activeTxn = NULL;
  252. return (ret == 0);
  253. }
  254. bool ReadVersion(int& nVersion)
  255. {
  256. nVersion = 0;
  257. return Read(std::string("version"), nVersion);
  258. }
  259. bool WriteVersion(int nVersion)
  260. {
  261. return Write(std::string("version"), nVersion);
  262. }
  263. bool static Rewrite(const std::string& strFile, const char* pszSkip = NULL);
  264. };
  265. /** Access to the transaction database (blkindex.dat) */
  266. class CTxDB : public CDB
  267. {
  268. public:
  269. CTxDB(const char* pszMode="r+") : CDB("blkindex.dat", pszMode) { }
  270. private:
  271. CTxDB(const CTxDB&);
  272. void operator=(const CTxDB&);
  273. public:
  274. bool ReadTxIndex(uint256 hash, CTxIndex& txindex);
  275. bool UpdateTxIndex(uint256 hash, const CTxIndex& txindex);
  276. bool AddTxIndex(const CTransaction& tx, const CDiskTxPos& pos, int nHeight);
  277. bool EraseTxIndex(const CTransaction& tx);
  278. bool ContainsTx(uint256 hash);
  279. bool ReadDiskTx(uint256 hash, CTransaction& tx, CTxIndex& txindex);
  280. bool ReadDiskTx(uint256 hash, CTransaction& tx);
  281. bool ReadDiskTx(COutPoint outpoint, CTransaction& tx, CTxIndex& txindex);
  282. bool ReadDiskTx(COutPoint outpoint, CTransaction& tx);
  283. bool WriteBlockIndex(const CDiskBlockIndex& blockindex);
  284. bool ReadHashBestChain(uint256& hashBestChain);
  285. bool WriteHashBestChain(uint256 hashBestChain);
  286. bool ReadBestInvalidWork(CBigNum& bnBestInvalidWork);
  287. bool WriteBestInvalidWork(CBigNum bnBestInvalidWork);
  288. bool ReadBlockFileInfo(int nFile, CBlockFileInfo &fileinfo);
  289. bool WriteBlockFileInfo(int nFile, const CBlockFileInfo &fileinfo);
  290. bool ReadLastBlockFile(int &nFile);
  291. bool WriteLastBlockFile(int nFile);
  292. bool LoadBlockIndex();
  293. private:
  294. bool LoadBlockIndexGuts();
  295. };
  296. /** Access to the (IP) address database (peers.dat) */
  297. class CAddrDB
  298. {
  299. private:
  300. boost::filesystem::path pathAddr;
  301. public:
  302. CAddrDB();
  303. bool Write(const CAddrMan& addr);
  304. bool Read(CAddrMan& addr);
  305. };
  306. #endif // BITCOIN_DB_H