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);
33 void ThreadFlushWalletDB(void* parg);
34 bool BackupWallet(const CWallet& wallet, const std::string& strDest);
44 std::vector<DbTxn*> vTxn;
47 explicit CDB(const char* pszFile, const char* pszMode="r+");
53 void operator=(const CDB&);
56 template<typename K, typename T>
57 bool Read(const K& key, T& value)
63 CDataStream ssKey(SER_DISK);
66 Dbt datKey(&ssKey[0], ssKey.size());
70 datValue.set_flags(DB_DBT_MALLOC);
71 int ret = pdb->get(GetTxn(), &datKey, &datValue, 0);
72 memset(datKey.get_data(), 0, datKey.get_size());
73 if (datValue.get_data() == NULL)
77 CDataStream ssValue((char*)datValue.get_data(), (char*)datValue.get_data() + datValue.get_size(), SER_DISK);
80 // Clear and free memory
81 memset(datValue.get_data(), 0, datValue.get_size());
82 free(datValue.get_data());
86 template<typename K, typename T>
87 bool Write(const K& key, const T& value, bool fOverwrite=true)
92 assert(!"Write called on database in read-only mode");
95 CDataStream ssKey(SER_DISK);
98 Dbt datKey(&ssKey[0], ssKey.size());
101 CDataStream ssValue(SER_DISK);
102 ssValue.reserve(10000);
104 Dbt datValue(&ssValue[0], ssValue.size());
107 int ret = pdb->put(GetTxn(), &datKey, &datValue, (fOverwrite ? 0 : DB_NOOVERWRITE));
109 // Clear memory in case it was a private key
110 memset(datKey.get_data(), 0, datKey.get_size());
111 memset(datValue.get_data(), 0, datValue.get_size());
116 bool Erase(const K& key)
121 assert(!"Erase called on database in read-only mode");
124 CDataStream ssKey(SER_DISK);
127 Dbt datKey(&ssKey[0], ssKey.size());
130 int ret = pdb->del(GetTxn(), &datKey, 0);
133 memset(datKey.get_data(), 0, datKey.get_size());
134 return (ret == 0 || ret == DB_NOTFOUND);
138 bool Exists(const K& key)
144 CDataStream ssKey(SER_DISK);
147 Dbt datKey(&ssKey[0], ssKey.size());
150 int ret = pdb->exists(GetTxn(), &datKey, 0);
153 memset(datKey.get_data(), 0, datKey.get_size());
162 int ret = pdb->cursor(NULL, &pcursor, 0);
168 int ReadAtCursor(Dbc* pcursor, CDataStream& ssKey, CDataStream& ssValue, unsigned int fFlags=DB_NEXT)
172 if (fFlags == DB_SET || fFlags == DB_SET_RANGE || fFlags == DB_GET_BOTH || fFlags == DB_GET_BOTH_RANGE)
174 datKey.set_data(&ssKey[0]);
175 datKey.set_size(ssKey.size());
178 if (fFlags == DB_GET_BOTH || fFlags == DB_GET_BOTH_RANGE)
180 datValue.set_data(&ssValue[0]);
181 datValue.set_size(ssValue.size());
183 datKey.set_flags(DB_DBT_MALLOC);
184 datValue.set_flags(DB_DBT_MALLOC);
185 int ret = pcursor->get(&datKey, &datValue, fFlags);
188 else if (datKey.get_data() == NULL || datValue.get_data() == NULL)
191 // Convert to streams
192 ssKey.SetType(SER_DISK);
194 ssKey.write((char*)datKey.get_data(), datKey.get_size());
195 ssValue.SetType(SER_DISK);
197 ssValue.write((char*)datValue.get_data(), datValue.get_size());
199 // Clear and free memory
200 memset(datKey.get_data(), 0, datKey.get_size());
201 memset(datValue.get_data(), 0, datValue.get_size());
202 free(datKey.get_data());
203 free(datValue.get_data());
221 int ret = dbenv.txn_begin(GetTxn(), &ptxn, DB_TXN_NOSYNC);
222 if (!ptxn || ret != 0)
224 vTxn.push_back(ptxn);
234 int ret = vTxn.back()->commit(0);
245 int ret = vTxn.back()->abort();
250 bool ReadVersion(int& nVersion)
253 return Read(std::string("version"), nVersion);
256 bool WriteVersion(int nVersion)
258 return Write(std::string("version"), nVersion);
269 class CTxDB : public CDB
272 CTxDB(const char* pszMode="r+") : CDB("blkindex.dat", pszMode) { }
275 void operator=(const CTxDB&);
277 bool ReadTxIndex(uint256 hash, CTxIndex& txindex);
278 bool UpdateTxIndex(uint256 hash, const CTxIndex& txindex);
279 bool AddTxIndex(const CTransaction& tx, const CDiskTxPos& pos, int nHeight);
280 bool EraseTxIndex(const CTransaction& tx);
281 bool ContainsTx(uint256 hash);
282 bool ReadOwnerTxes(uint160 hash160, int nHeight, std::vector<CTransaction>& vtx);
283 bool ReadDiskTx(uint256 hash, CTransaction& tx, CTxIndex& txindex);
284 bool ReadDiskTx(uint256 hash, CTransaction& tx);
285 bool ReadDiskTx(COutPoint outpoint, CTransaction& tx, CTxIndex& txindex);
286 bool ReadDiskTx(COutPoint outpoint, CTransaction& tx);
287 bool WriteBlockIndex(const CDiskBlockIndex& blockindex);
288 bool EraseBlockIndex(uint256 hash);
289 bool ReadHashBestChain(uint256& hashBestChain);
290 bool WriteHashBestChain(uint256 hashBestChain);
291 bool ReadBestInvalidWork(CBigNum& bnBestInvalidWork);
292 bool WriteBestInvalidWork(CBigNum bnBestInvalidWork);
293 bool LoadBlockIndex();
300 class CAddrDB : public CDB
303 CAddrDB(const char* pszMode="r+") : CDB("addr.dat", pszMode) { }
305 CAddrDB(const CAddrDB&);
306 void operator=(const CAddrDB&);
308 bool WriteAddress(const CAddress& addr);
309 bool EraseAddress(const CAddress& addr);
310 bool LoadAddresses();
313 bool LoadAddresses();
321 std::vector<unsigned char> vchPubKey;
328 CKeyPool(const std::vector<unsigned char>& vchPubKeyIn)
331 vchPubKey = vchPubKeyIn;
336 if (!(nType & SER_GETHASH))
339 READWRITE(vchPubKey);
354 class CWalletDB : public CDB
357 CWalletDB(std::string strFilename, const char* pszMode="r+") : CDB(strFilename.c_str(), pszMode)
361 CWalletDB(const CWalletDB&);
362 void operator=(const CWalletDB&);
364 bool ReadName(const std::string& strAddress, std::string& strName)
367 return Read(std::make_pair(std::string("name"), strAddress), strName);
370 bool WriteName(const std::string& strAddress, const std::string& strName);
372 bool EraseName(const std::string& strAddress);
374 bool ReadTx(uint256 hash, CWalletTx& wtx)
376 return Read(std::make_pair(std::string("tx"), hash), wtx);
379 bool WriteTx(uint256 hash, const CWalletTx& wtx)
382 return Write(std::make_pair(std::string("tx"), hash), wtx);
385 bool EraseTx(uint256 hash)
388 return Erase(std::make_pair(std::string("tx"), hash));
391 bool ReadKey(const std::vector<unsigned char>& vchPubKey, CPrivKey& vchPrivKey)
394 return Read(std::make_pair(std::string("key"), vchPubKey), vchPrivKey);
397 bool WriteKey(const std::vector<unsigned char>& vchPubKey, const CPrivKey& vchPrivKey)
400 return Write(std::make_pair(std::string("key"), vchPubKey), vchPrivKey, false);
403 bool WriteCryptedKey(const std::vector<unsigned char>& vchPubKey, const std::vector<unsigned char>& vchCryptedSecret, bool fEraseUnencryptedKey = true)
406 if (!Write(std::make_pair(std::string("ckey"), vchPubKey), vchCryptedSecret, false))
408 if (fEraseUnencryptedKey)
410 Erase(std::make_pair(std::string("key"), vchPubKey));
411 Erase(std::make_pair(std::string("wkey"), vchPubKey));
416 bool WriteMasterKey(unsigned int nID, const CMasterKey& kMasterKey)
419 return Write(std::make_pair(std::string("mkey"), nID), kMasterKey, true);
422 bool WriteBestBlock(const CBlockLocator& locator)
425 return Write(std::string("bestblock"), locator);
428 bool ReadBestBlock(CBlockLocator& locator)
430 return Read(std::string("bestblock"), locator);
433 bool ReadDefaultKey(std::vector<unsigned char>& vchPubKey)
436 return Read(std::string("defaultkey"), vchPubKey);
439 bool WriteDefaultKey(const std::vector<unsigned char>& vchPubKey)
442 return Write(std::string("defaultkey"), vchPubKey);
445 bool ReadPool(int64 nPool, CKeyPool& keypool)
447 return Read(std::make_pair(std::string("pool"), nPool), keypool);
450 bool WritePool(int64 nPool, const CKeyPool& keypool)
453 return Write(std::make_pair(std::string("pool"), nPool), keypool);
456 bool ErasePool(int64 nPool)
459 return Erase(std::make_pair(std::string("pool"), nPool));
463 bool ReadSetting(const std::string& strKey, T& value)
465 return Read(std::make_pair(std::string("setting"), strKey), value);
469 bool WriteSetting(const std::string& strKey, const T& value)
472 return Write(std::make_pair(std::string("setting"), strKey), value);
475 bool ReadAccount(const std::string& strAccount, CAccount& account);
476 bool WriteAccount(const std::string& strAccount, const CAccount& account);
477 bool WriteAccountingEntry(const CAccountingEntry& acentry);
478 int64 GetAccountCreditDebit(const std::string& strAccount);
479 void ListAccountCreditDebit(const std::string& strAccount, std::list<CAccountingEntry>& acentries);
481 int LoadWallet(CWallet* pwallet);