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 license.txt or http://www.opensource.org/licenses/mit-license.php.
19 class CDiskBlockIndex;
27 extern unsigned int nWalletDBUpdated;
30 extern void DBFlush(bool fShutdown);
31 void ThreadFlushWalletDB(void* parg);
32 bool BackupWallet(const CWallet& wallet, const std::string& strDest);
35 /** RAII class that provides access to a Berkeley database */
41 std::vector<DbTxn*> vTxn;
44 explicit CDB(const char* pszFile, const char* pszMode="r+");
50 void operator=(const CDB&);
53 template<typename K, typename T>
54 bool Read(const K& key, T& value)
60 CDataStream ssKey(SER_DISK, CLIENT_VERSION);
63 Dbt datKey(&ssKey[0], ssKey.size());
67 datValue.set_flags(DB_DBT_MALLOC);
68 int ret = pdb->get(GetTxn(), &datKey, &datValue, 0);
69 memset(datKey.get_data(), 0, datKey.get_size());
70 if (datValue.get_data() == NULL)
74 CDataStream ssValue((char*)datValue.get_data(), (char*)datValue.get_data() + datValue.get_size(), SER_DISK, CLIENT_VERSION);
77 // Clear and free memory
78 memset(datValue.get_data(), 0, datValue.get_size());
79 free(datValue.get_data());
83 template<typename K, typename T>
84 bool Write(const K& key, const T& value, bool fOverwrite=true)
89 assert(!"Write called on database in read-only mode");
92 CDataStream ssKey(SER_DISK, CLIENT_VERSION);
95 Dbt datKey(&ssKey[0], ssKey.size());
98 CDataStream ssValue(SER_DISK, CLIENT_VERSION);
99 ssValue.reserve(10000);
101 Dbt datValue(&ssValue[0], ssValue.size());
104 int ret = pdb->put(GetTxn(), &datKey, &datValue, (fOverwrite ? 0 : DB_NOOVERWRITE));
106 // Clear memory in case it was a private key
107 memset(datKey.get_data(), 0, datKey.get_size());
108 memset(datValue.get_data(), 0, datValue.get_size());
113 bool Erase(const K& key)
118 assert(!"Erase called on database in read-only mode");
121 CDataStream ssKey(SER_DISK, CLIENT_VERSION);
124 Dbt datKey(&ssKey[0], ssKey.size());
127 int ret = pdb->del(GetTxn(), &datKey, 0);
130 memset(datKey.get_data(), 0, datKey.get_size());
131 return (ret == 0 || ret == DB_NOTFOUND);
135 bool Exists(const K& key)
141 CDataStream ssKey(SER_DISK, CLIENT_VERSION);
144 Dbt datKey(&ssKey[0], ssKey.size());
147 int ret = pdb->exists(GetTxn(), &datKey, 0);
150 memset(datKey.get_data(), 0, datKey.get_size());
159 int ret = pdb->cursor(NULL, &pcursor, 0);
165 int ReadAtCursor(Dbc* pcursor, CDataStream& ssKey, CDataStream& ssValue, unsigned int fFlags=DB_NEXT)
169 if (fFlags == DB_SET || fFlags == DB_SET_RANGE || fFlags == DB_GET_BOTH || fFlags == DB_GET_BOTH_RANGE)
171 datKey.set_data(&ssKey[0]);
172 datKey.set_size(ssKey.size());
175 if (fFlags == DB_GET_BOTH || fFlags == DB_GET_BOTH_RANGE)
177 datValue.set_data(&ssValue[0]);
178 datValue.set_size(ssValue.size());
180 datKey.set_flags(DB_DBT_MALLOC);
181 datValue.set_flags(DB_DBT_MALLOC);
182 int ret = pcursor->get(&datKey, &datValue, fFlags);
185 else if (datKey.get_data() == NULL || datValue.get_data() == NULL)
188 // Convert to streams
189 ssKey.SetType(SER_DISK);
191 ssKey.write((char*)datKey.get_data(), datKey.get_size());
192 ssValue.SetType(SER_DISK);
194 ssValue.write((char*)datValue.get_data(), datValue.get_size());
196 // Clear and free memory
197 memset(datKey.get_data(), 0, datKey.get_size());
198 memset(datValue.get_data(), 0, datValue.get_size());
199 free(datKey.get_data());
200 free(datValue.get_data());
218 int ret = dbenv.txn_begin(GetTxn(), &ptxn, DB_TXN_NOSYNC);
219 if (!ptxn || ret != 0)
221 vTxn.push_back(ptxn);
231 int ret = vTxn.back()->commit(0);
242 int ret = vTxn.back()->abort();
247 bool ReadVersion(int& nVersion)
250 return Read(std::string("version"), nVersion);
253 bool WriteVersion(int nVersion)
255 return Write(std::string("version"), nVersion);
258 bool static Rewrite(const std::string& strFile, const char* pszSkip = NULL);
267 /** Access to the transaction database (blkindex.dat) */
268 class CTxDB : public CDB
271 CTxDB(const char* pszMode="r+") : CDB("blkindex.dat", pszMode) { }
274 void operator=(const CTxDB&);
276 bool ReadTxIndex(uint256 hash, CTxIndex& txindex);
277 bool UpdateTxIndex(uint256 hash, const CTxIndex& txindex);
278 bool AddTxIndex(const CTransaction& tx, const CDiskTxPos& pos, int nHeight);
279 bool EraseTxIndex(const CTransaction& tx);
280 bool ContainsTx(uint256 hash);
281 bool ReadOwnerTxes(uint160 hash160, int nHeight, std::vector<CTransaction>& vtx);
282 bool ReadDiskTx(uint256 hash, CTransaction& tx, CTxIndex& txindex);
283 bool ReadDiskTx(uint256 hash, CTransaction& tx);
284 bool ReadDiskTx(COutPoint outpoint, CTransaction& tx, CTxIndex& txindex);
285 bool ReadDiskTx(COutPoint outpoint, CTransaction& tx);
286 bool WriteBlockIndex(const CDiskBlockIndex& blockindex);
287 bool EraseBlockIndex(uint256 hash);
288 bool ReadHashBestChain(uint256& hashBestChain);
289 bool WriteHashBestChain(uint256 hashBestChain);
290 bool ReadBestInvalidWork(CBigNum& bnBestInvalidWork);
291 bool WriteBestInvalidWork(CBigNum bnBestInvalidWork);
292 bool LoadBlockIndex();
298 /** Access to the (IP) address database (addr.dat) */
299 class CAddrDB : public CDB
302 CAddrDB(const char* pszMode="r+") : CDB("addr.dat", pszMode) { }
304 CAddrDB(const CAddrDB&);
305 void operator=(const CAddrDB&);
307 bool WriteAddrman(const CAddrMan& addr);
308 bool LoadAddresses();
311 bool LoadAddresses();
314 #endif // BITCOIN_DB_H