1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2011 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.
17 class CDiskBlockIndex;
24 class CAccountingEntry;
28 extern unsigned int nWalletDBUpdated;
32 extern void DBFlush(bool fShutdown, bool fRemoveLogFiles);
33 void ThreadFlushWalletDB(void* parg);
34 bool BackupWallet(const CWallet& wallet, const std::string& strDest);
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);
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)
76 CDataStream ssValue((char*)datValue.get_data(), (char*)datValue.get_data() + datValue.get_size(), SER_DISK);
79 // Clear and free memory
80 memset(datValue.get_data(), 0, datValue.get_size());
81 free(datValue.get_data());
85 template<typename K, typename T>
86 bool Write(const K& key, const T& value, bool fOverwrite=true)
91 assert(!"Write called on database in read-only mode");
94 CDataStream ssKey(SER_DISK);
97 Dbt datKey(&ssKey[0], ssKey.size());
100 CDataStream ssValue(SER_DISK);
101 ssValue.reserve(10000);
103 Dbt datValue(&ssValue[0], ssValue.size());
106 int ret = pdb->put(GetTxn(), &datKey, &datValue, (fOverwrite ? 0 : DB_NOOVERWRITE));
108 // Clear memory in case it was a private key
109 memset(datKey.get_data(), 0, datKey.get_size());
110 memset(datValue.get_data(), 0, datValue.get_size());
115 bool Erase(const K& key)
120 assert(!"Erase called on database in read-only mode");
123 CDataStream ssKey(SER_DISK);
126 Dbt datKey(&ssKey[0], ssKey.size());
129 int ret = pdb->del(GetTxn(), &datKey, 0);
132 memset(datKey.get_data(), 0, datKey.get_size());
133 return (ret == 0 || ret == DB_NOTFOUND);
137 bool Exists(const K& key)
143 CDataStream ssKey(SER_DISK);
146 Dbt datKey(&ssKey[0], ssKey.size());
149 int ret = pdb->exists(GetTxn(), &datKey, 0);
152 memset(datKey.get_data(), 0, datKey.get_size());
161 int ret = pdb->cursor(NULL, &pcursor, 0);
167 int ReadAtCursor(Dbc* pcursor, CDataStream& ssKey, CDataStream& ssValue, unsigned int fFlags=DB_NEXT)
171 if (fFlags == DB_SET || fFlags == DB_SET_RANGE || fFlags == DB_GET_BOTH || fFlags == DB_GET_BOTH_RANGE)
173 datKey.set_data(&ssKey[0]);
174 datKey.set_size(ssKey.size());
177 if (fFlags == DB_GET_BOTH || fFlags == DB_GET_BOTH_RANGE)
179 datValue.set_data(&ssValue[0]);
180 datValue.set_size(ssValue.size());
182 datKey.set_flags(DB_DBT_MALLOC);
183 datValue.set_flags(DB_DBT_MALLOC);
184 int ret = pcursor->get(&datKey, &datValue, fFlags);
187 else if (datKey.get_data() == NULL || datValue.get_data() == NULL)
190 // Convert to streams
191 ssKey.SetType(SER_DISK);
193 ssKey.write((char*)datKey.get_data(), datKey.get_size());
194 ssValue.SetType(SER_DISK);
196 ssValue.write((char*)datValue.get_data(), datValue.get_size());
198 // Clear and free memory
199 memset(datKey.get_data(), 0, datKey.get_size());
200 memset(datValue.get_data(), 0, datValue.get_size());
201 free(datKey.get_data());
202 free(datValue.get_data());
220 int ret = dbenv.txn_begin(GetTxn(), &ptxn, DB_TXN_NOSYNC);
221 if (!ptxn || ret != 0)
223 vTxn.push_back(ptxn);
233 int ret = vTxn.back()->commit(0);
244 int ret = vTxn.back()->abort();
249 bool ReadVersion(int& nVersion)
252 return Read(std::string("version"), nVersion);
255 bool WriteVersion(int nVersion)
257 return Write(std::string("version"), nVersion);
260 bool static Rewrite(const std::string& strFile, const char* pszSkip = NULL);
270 class CTxDB : public CDB
273 CTxDB(const char* pszMode="r+") : CDB("blkindex.dat", pszMode) { }
276 void operator=(const CTxDB&);
278 bool ReadTxIndex(uint256 hash, CTxIndex& txindex);
279 bool UpdateTxIndex(uint256 hash, const CTxIndex& txindex);
280 bool AddTxIndex(const CTransaction& tx, const CDiskTxPos& pos, int nHeight);
281 bool EraseTxIndex(const CTransaction& tx);
282 bool ContainsTx(uint256 hash);
283 bool ReadOwnerTxes(uint160 hash160, int nHeight, std::vector<CTransaction>& vtx);
284 bool ReadDiskTx(uint256 hash, CTransaction& tx, CTxIndex& txindex);
285 bool ReadDiskTx(uint256 hash, CTransaction& tx);
286 bool ReadDiskTx(COutPoint outpoint, CTransaction& tx, CTxIndex& txindex);
287 bool ReadDiskTx(COutPoint outpoint, CTransaction& tx);
288 bool WriteBlockIndex(const CDiskBlockIndex& blockindex);
289 bool EraseBlockIndex(uint256 hash);
290 bool ReadHashBestChain(uint256& hashBestChain);
291 bool WriteHashBestChain(uint256 hashBestChain);
292 bool ReadBestInvalidWork(CBigNum& bnBestInvalidWork);
293 bool WriteBestInvalidWork(CBigNum bnBestInvalidWork);
294 bool LoadBlockIndex();
301 class CAddrDB : public CDB
304 CAddrDB(const char* pszMode="r+") : CDB("addr.dat", pszMode) { }
306 CAddrDB(const CAddrDB&);
307 void operator=(const CAddrDB&);
309 bool WriteAddress(const CAddress& addr);
310 bool EraseAddress(const CAddress& addr);
311 bool LoadAddresses();
314 bool LoadAddresses();
322 std::vector<unsigned char> vchPubKey;
329 CKeyPool(const std::vector<unsigned char>& vchPubKeyIn)
332 vchPubKey = vchPubKeyIn;
337 if (!(nType & SER_GETHASH))
340 READWRITE(vchPubKey);
356 class CWalletDB : public CDB
359 CWalletDB(std::string strFilename, const char* pszMode="r+") : CDB(strFilename.c_str(), pszMode)
363 CWalletDB(const CWalletDB&);
364 void operator=(const CWalletDB&);
366 bool ReadName(const std::string& strAddress, std::string& strName)
369 return Read(std::make_pair(std::string("name"), strAddress), strName);
372 bool WriteName(const std::string& strAddress, const std::string& strName);
374 bool EraseName(const std::string& strAddress);
376 bool ReadTx(uint256 hash, CWalletTx& wtx)
378 return Read(std::make_pair(std::string("tx"), hash), wtx);
381 bool WriteTx(uint256 hash, const CWalletTx& wtx)
384 return Write(std::make_pair(std::string("tx"), hash), wtx);
387 bool EraseTx(uint256 hash)
390 return Erase(std::make_pair(std::string("tx"), hash));
393 bool ReadKey(const std::vector<unsigned char>& vchPubKey, CPrivKey& vchPrivKey)
396 return Read(std::make_pair(std::string("key"), vchPubKey), vchPrivKey);
399 bool WriteKey(const std::vector<unsigned char>& vchPubKey, const CPrivKey& vchPrivKey)
402 return Write(std::make_pair(std::string("key"), vchPubKey), vchPrivKey, false);
405 bool WriteCryptedKey(const std::vector<unsigned char>& vchPubKey, const std::vector<unsigned char>& vchCryptedSecret, bool fEraseUnencryptedKey = true)
408 if (!Write(std::make_pair(std::string("ckey"), vchPubKey), vchCryptedSecret, false))
410 if (fEraseUnencryptedKey)
412 Erase(std::make_pair(std::string("key"), vchPubKey));
413 Erase(std::make_pair(std::string("wkey"), vchPubKey));
418 bool WriteMasterKey(unsigned int nID, const CMasterKey& kMasterKey)
421 return Write(std::make_pair(std::string("mkey"), nID), kMasterKey, true);
424 bool WriteBestBlock(const CBlockLocator& locator)
427 return Write(std::string("bestblock"), locator);
430 bool ReadBestBlock(CBlockLocator& locator)
432 return Read(std::string("bestblock"), locator);
435 bool ReadDefaultKey(std::vector<unsigned char>& vchPubKey)
438 return Read(std::string("defaultkey"), vchPubKey);
441 bool WriteDefaultKey(const std::vector<unsigned char>& vchPubKey)
444 return Write(std::string("defaultkey"), vchPubKey);
447 bool ReadPool(int64 nPool, CKeyPool& keypool)
449 return Read(std::make_pair(std::string("pool"), nPool), keypool);
452 bool WritePool(int64 nPool, const CKeyPool& keypool)
455 return Write(std::make_pair(std::string("pool"), nPool), keypool);
458 bool ErasePool(int64 nPool)
461 return Erase(std::make_pair(std::string("pool"), nPool));
465 bool ReadSetting(const std::string& strKey, T& value)
467 return Read(std::make_pair(std::string("setting"), strKey), value);
471 bool WriteSetting(const std::string& strKey, const T& value)
474 return Write(std::make_pair(std::string("setting"), strKey), value);
477 bool ReadAccount(const std::string& strAccount, CAccount& account);
478 bool WriteAccount(const std::string& strAccount, const CAccount& account);
479 bool WriteAccountingEntry(const CAccountingEntry& acentry);
480 int64 GetAccountCreditDebit(const std::string& strAccount);
481 void ListAccountCreditDebit(const std::string& strAccount, std::list<CAccountingEntry>& acentries);
483 int LoadWallet(CWallet* pwallet);