1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2012 The Bitcoin developers
3 // Copyright (c) 2011-2012 The PPCoin developers
4 // Distributed under the MIT/X11 software license, see the accompanying
5 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
20 class CDiskBlockIndex;
28 extern unsigned int nWalletDBUpdated;
29 extern bool fDetachDB;
32 extern void DBFlush(bool fShutdown);
33 void ThreadFlushWalletDB(void* parg);
34 bool BackupWallet(const CWallet& wallet, const std::string& strDest);
37 /** RAII class that provides access to a Berkeley database */
43 std::vector<DbTxn*> vTxn;
46 explicit CDB(const char* pszFile, const char* pszMode="r+");
52 void operator=(const CDB&);
55 template<typename K, typename T>
56 bool Read(const K& key, T& value)
62 CDataStream ssKey(SER_DISK, CLIENT_VERSION);
65 Dbt datKey(&ssKey[0], ssKey.size());
69 datValue.set_flags(DB_DBT_MALLOC);
70 int ret = pdb->get(GetTxn(), &datKey, &datValue, 0);
71 memset(datKey.get_data(), 0, datKey.get_size());
72 if (datValue.get_data() == NULL)
77 CDataStream ssValue((char*)datValue.get_data(), (char*)datValue.get_data() + datValue.get_size(), SER_DISK, CLIENT_VERSION);
80 catch (std::exception &e) {
84 // Clear and free memory
85 memset(datValue.get_data(), 0, datValue.get_size());
86 free(datValue.get_data());
90 template<typename K, typename T>
91 bool Write(const K& key, const T& value, bool fOverwrite=true)
96 assert(!"Write called on database in read-only mode");
99 CDataStream ssKey(SER_DISK, CLIENT_VERSION);
102 Dbt datKey(&ssKey[0], ssKey.size());
105 CDataStream ssValue(SER_DISK, CLIENT_VERSION);
106 ssValue.reserve(10000);
108 Dbt datValue(&ssValue[0], ssValue.size());
111 int ret = pdb->put(GetTxn(), &datKey, &datValue, (fOverwrite ? 0 : DB_NOOVERWRITE));
113 // Clear memory in case it was a private key
114 memset(datKey.get_data(), 0, datKey.get_size());
115 memset(datValue.get_data(), 0, datValue.get_size());
120 bool Erase(const K& key)
125 assert(!"Erase called on database in read-only mode");
128 CDataStream ssKey(SER_DISK, CLIENT_VERSION);
131 Dbt datKey(&ssKey[0], ssKey.size());
134 int ret = pdb->del(GetTxn(), &datKey, 0);
137 memset(datKey.get_data(), 0, datKey.get_size());
138 return (ret == 0 || ret == DB_NOTFOUND);
142 bool Exists(const K& key)
148 CDataStream ssKey(SER_DISK, CLIENT_VERSION);
151 Dbt datKey(&ssKey[0], ssKey.size());
154 int ret = pdb->exists(GetTxn(), &datKey, 0);
157 memset(datKey.get_data(), 0, datKey.get_size());
166 int ret = pdb->cursor(NULL, &pcursor, 0);
172 int ReadAtCursor(Dbc* pcursor, CDataStream& ssKey, CDataStream& ssValue, unsigned int fFlags=DB_NEXT)
176 if (fFlags == DB_SET || fFlags == DB_SET_RANGE || fFlags == DB_GET_BOTH || fFlags == DB_GET_BOTH_RANGE)
178 datKey.set_data(&ssKey[0]);
179 datKey.set_size(ssKey.size());
182 if (fFlags == DB_GET_BOTH || fFlags == DB_GET_BOTH_RANGE)
184 datValue.set_data(&ssValue[0]);
185 datValue.set_size(ssValue.size());
187 datKey.set_flags(DB_DBT_MALLOC);
188 datValue.set_flags(DB_DBT_MALLOC);
189 int ret = pcursor->get(&datKey, &datValue, fFlags);
192 else if (datKey.get_data() == NULL || datValue.get_data() == NULL)
195 // Convert to streams
196 ssKey.SetType(SER_DISK);
198 ssKey.write((char*)datKey.get_data(), datKey.get_size());
199 ssValue.SetType(SER_DISK);
201 ssValue.write((char*)datValue.get_data(), datValue.get_size());
203 // Clear and free memory
204 memset(datKey.get_data(), 0, datKey.get_size());
205 memset(datValue.get_data(), 0, datValue.get_size());
206 free(datKey.get_data());
207 free(datValue.get_data());
225 int ret = dbenv.txn_begin(GetTxn(), &ptxn, DB_TXN_WRITE_NOSYNC);
226 if (!ptxn || ret != 0)
228 vTxn.push_back(ptxn);
238 int ret = vTxn.back()->commit(0);
249 int ret = vTxn.back()->abort();
254 bool ReadVersion(int& nVersion)
257 return Read(std::string("version"), nVersion);
260 bool WriteVersion(int nVersion)
262 return Write(std::string("version"), nVersion);
265 bool static Rewrite(const std::string& strFile, const char* pszSkip = NULL);
274 /** Access to the transaction database (blkindex.dat) */
275 class CTxDB : public CDB
278 CTxDB(const char* pszMode="r+") : CDB("blkindex.dat", pszMode) { }
281 void operator=(const CTxDB&);
283 bool ReadTxIndex(uint256 hash, CTxIndex& txindex);
284 bool UpdateTxIndex(uint256 hash, const CTxIndex& txindex);
285 bool AddTxIndex(const CTransaction& tx, const CDiskTxPos& pos, int nHeight);
286 bool EraseTxIndex(const CTransaction& tx);
287 bool ContainsTx(uint256 hash);
288 bool ReadOwnerTxes(uint160 hash160, int nHeight, std::vector<CTransaction>& vtx);
289 bool ReadDiskTx(uint256 hash, CTransaction& tx, CTxIndex& txindex);
290 bool ReadDiskTx(uint256 hash, CTransaction& tx);
291 bool ReadDiskTx(COutPoint outpoint, CTransaction& tx, CTxIndex& txindex);
292 bool ReadDiskTx(COutPoint outpoint, CTransaction& tx);
293 bool WriteBlockIndex(const CDiskBlockIndex& blockindex);
294 bool EraseBlockIndex(uint256 hash);
295 bool ReadHashBestChain(uint256& hashBestChain);
296 bool WriteHashBestChain(uint256 hashBestChain);
297 bool ReadBestInvalidTrust(CBigNum& bnBestInvalidTrust);
298 bool WriteBestInvalidTrust(CBigNum bnBestInvalidTrust);
299 bool ReadSyncCheckpoint(uint256& hashCheckpoint);
300 bool WriteSyncCheckpoint(uint256 hashCheckpoint);
301 bool ReadCheckpointPubKey(std::string& strPubKey);
302 bool WriteCheckpointPubKey(const std::string& strPubKey);
303 bool LoadBlockIndex();
309 /** Access to the (IP) address database (addr.dat) */
310 class CAddrDB : public CDB
313 CAddrDB(const char* pszMode="r+") : CDB("addr.dat", pszMode) { }
315 CAddrDB(const CAddrDB&);
316 void operator=(const CAddrDB&);
318 bool WriteAddrman(const CAddrMan& addr);
319 bool LoadAddresses();
322 bool LoadAddresses();
325 #endif // BITCOIN_DB_H