db.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. // ECOin - Copyright (c) - 2014/2022 - GPLv3 - epsylon@riseup.net (https://03c8.net)
  2. #ifndef ECOIN_DB_H
  3. #define ECOIN_DB_H
  4. #include "main.h"
  5. #include <cstdlib>
  6. #include <cstring>
  7. #include <map>
  8. #include <string>
  9. #include <vector>
  10. #include <db_cxx.h>
  11. class CAddress;
  12. class CAddrMan;
  13. class CBlockLocator;
  14. class CDiskBlockIndex;
  15. class CDiskTxPos;
  16. class CMasterKey;
  17. class COutPoint;
  18. class CTxIndex;
  19. class CWallet;
  20. class CWalletTx;
  21. extern unsigned int nWalletDBUpdated;
  22. void ThreadFlushWalletDB(void* parg);
  23. bool BackupWallet(const CWallet& wallet, const std::string& strDest);
  24. class CDBEnv
  25. {
  26. private:
  27. bool fDetachDB;
  28. bool fDbEnvInit;
  29. bool fMockDb;
  30. boost::filesystem::path pathEnv;
  31. std::string strPath;
  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. std::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. std::memset(datValue.get_data(), 0, datValue.get_size());
  120. std::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. std::memset(datKey.get_data(), 0, datKey.get_size());
  144. std::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. std::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. std::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. std::memset(datKey.get_data(), 0, datKey.get_size());
  222. std::memset(datValue.get_data(), 0, datValue.get_size());
  223. std::free(datKey.get_data());
  224. std::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. /**
  267. class CTxDB : public CDB
  268. {
  269. public:
  270. CTxDB(const char* pszMode="r+") : CDB("blkindex.dat", pszMode) { }
  271. private:
  272. CTxDB(const CTxDB&);
  273. void operator=(const CTxDB&);
  274. public:
  275. bool ReadTxIndex(uint256 hash, CTxIndex& txindex);
  276. bool UpdateTxIndex(uint256 hash, const CTxIndex& txindex);
  277. bool AddTxIndex(const CTransaction& tx, const CDiskTxPos& pos, int nHeight);
  278. bool EraseTxIndex(const CTransaction& tx);
  279. bool ContainsTx(uint256 hash);
  280. bool ReadDiskTx(uint256 hash, CTransaction& tx, CTxIndex& txindex);
  281. bool ReadDiskTx(uint256 hash, CTransaction& tx);
  282. bool ReadDiskTx(COutPoint outpoint, CTransaction& tx, CTxIndex& txindex);
  283. bool ReadDiskTx(COutPoint outpoint, CTransaction& tx);
  284. bool WriteBlockIndex(const CDiskBlockIndex& blockindex);
  285. bool ReadHashBestChain(uint256& hashBestChain);
  286. bool WriteHashBestChain(uint256 hashBestChain);
  287. bool ReadBestInvalidTrust(CBigNum& bnBestInvalidTrust);
  288. bool WriteBestInvalidTrust(CBigNum bnBestInvalidTrust);
  289. bool ReadSyncCheckpoint(uint256& hashCheckpoint);
  290. bool WriteSyncCheckpoint(uint256 hashCheckpoint);
  291. bool ReadCheckpointPubKey(std::string& strPubKey);
  292. bool WriteCheckpointPubKey(const std::string& strPubKey);
  293. bool LoadBlockIndex();
  294. private:
  295. bool LoadBlockIndexGuts();
  296. };
  297. */
  298. /** Access to the (IP) address database (peers.dat) */
  299. class CAddrDB
  300. {
  301. private:
  302. boost::filesystem::path pathAddr;
  303. public:
  304. CAddrDB();
  305. bool Write(const CAddrMan& addr);
  306. bool Read(CAddrMan& addr);
  307. };
  308. #endif // ECOIN_DB_H