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.
17 class CAccountingEntry;
20 class CDiskBlockIndex;
28 extern unsigned int nWalletDBUpdated;
31 extern void DBFlush(bool fShutdown);
32 void ThreadFlushWalletDB(void* parg);
33 bool BackupWallet(const CWallet& wallet, const std::string& strDest);
42 std::vector<DbTxn*> vTxn;
45 explicit CDB(const char* pszFile, const char* pszMode="r+");
51 void operator=(const CDB&);
54 template<typename K, typename T>
55 bool Read(const K& key, T& value)
61 CDataStream ssKey(SER_DISK);
64 Dbt datKey(&ssKey[0], ssKey.size());
68 datValue.set_flags(DB_DBT_MALLOC);
69 int ret = pdb->get(GetTxn(), &datKey, &datValue, 0);
70 memset(datKey.get_data(), 0, datKey.get_size());
71 if (datValue.get_data() == NULL)
75 CDataStream ssValue((char*)datValue.get_data(), (char*)datValue.get_data() + datValue.get_size(), SER_DISK);
78 // Clear and free memory
79 memset(datValue.get_data(), 0, datValue.get_size());
80 free(datValue.get_data());
84 template<typename K, typename T>
85 bool Write(const K& key, const T& value, bool fOverwrite=true)
90 assert(!"Write called on database in read-only mode");
93 CDataStream ssKey(SER_DISK);
96 Dbt datKey(&ssKey[0], ssKey.size());
99 CDataStream ssValue(SER_DISK);
100 ssValue.reserve(10000);
102 Dbt datValue(&ssValue[0], ssValue.size());
105 int ret = pdb->put(GetTxn(), &datKey, &datValue, (fOverwrite ? 0 : DB_NOOVERWRITE));
107 // Clear memory in case it was a private key
108 memset(datKey.get_data(), 0, datKey.get_size());
109 memset(datValue.get_data(), 0, datValue.get_size());
114 bool Erase(const K& key)
119 assert(!"Erase called on database in read-only mode");
122 CDataStream ssKey(SER_DISK);
125 Dbt datKey(&ssKey[0], ssKey.size());
128 int ret = pdb->del(GetTxn(), &datKey, 0);
131 memset(datKey.get_data(), 0, datKey.get_size());
132 return (ret == 0 || ret == DB_NOTFOUND);
136 bool Exists(const K& key)
142 CDataStream ssKey(SER_DISK);
145 Dbt datKey(&ssKey[0], ssKey.size());
148 int ret = pdb->exists(GetTxn(), &datKey, 0);
151 memset(datKey.get_data(), 0, datKey.get_size());
160 int ret = pdb->cursor(NULL, &pcursor, 0);
166 int ReadAtCursor(Dbc* pcursor, CDataStream& ssKey, CDataStream& ssValue, unsigned int fFlags=DB_NEXT)
170 if (fFlags == DB_SET || fFlags == DB_SET_RANGE || fFlags == DB_GET_BOTH || fFlags == DB_GET_BOTH_RANGE)
172 datKey.set_data(&ssKey[0]);
173 datKey.set_size(ssKey.size());
176 if (fFlags == DB_GET_BOTH || fFlags == DB_GET_BOTH_RANGE)
178 datValue.set_data(&ssValue[0]);
179 datValue.set_size(ssValue.size());
181 datKey.set_flags(DB_DBT_MALLOC);
182 datValue.set_flags(DB_DBT_MALLOC);
183 int ret = pcursor->get(&datKey, &datValue, fFlags);
186 else if (datKey.get_data() == NULL || datValue.get_data() == NULL)
189 // Convert to streams
190 ssKey.SetType(SER_DISK);
192 ssKey.write((char*)datKey.get_data(), datKey.get_size());
193 ssValue.SetType(SER_DISK);
195 ssValue.write((char*)datValue.get_data(), datValue.get_size());
197 // Clear and free memory
198 memset(datKey.get_data(), 0, datKey.get_size());
199 memset(datValue.get_data(), 0, datValue.get_size());
200 free(datKey.get_data());
201 free(datValue.get_data());
219 int ret = dbenv.txn_begin(GetTxn(), &ptxn, DB_TXN_NOSYNC);
220 if (!ptxn || ret != 0)
222 vTxn.push_back(ptxn);
232 int ret = vTxn.back()->commit(0);
243 int ret = vTxn.back()->abort();
248 bool ReadVersion(int& nVersion)
251 return Read(std::string("version"), nVersion);
254 bool WriteVersion(int nVersion)
256 return Write(std::string("version"), nVersion);
259 bool static Rewrite(const std::string& strFile, const char* pszSkip = NULL);
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);
355 class CWalletDB : public CDB
358 CWalletDB(std::string strFilename, const char* pszMode="r+") : CDB(strFilename.c_str(), pszMode)
362 CWalletDB(const CWalletDB&);
363 void operator=(const CWalletDB&);
365 bool ReadName(const std::string& strAddress, std::string& strName)
368 return Read(std::make_pair(std::string("name"), strAddress), strName);
371 bool WriteName(const std::string& strAddress, const std::string& strName);
373 bool EraseName(const std::string& strAddress);
375 bool ReadTx(uint256 hash, CWalletTx& wtx)
377 return Read(std::make_pair(std::string("tx"), hash), wtx);
380 bool WriteTx(uint256 hash, const CWalletTx& wtx)
383 return Write(std::make_pair(std::string("tx"), hash), wtx);
386 bool EraseTx(uint256 hash)
389 return Erase(std::make_pair(std::string("tx"), hash));
392 bool ReadKey(const std::vector<unsigned char>& vchPubKey, CPrivKey& vchPrivKey)
395 return Read(std::make_pair(std::string("key"), vchPubKey), vchPrivKey);
398 bool WriteKey(const std::vector<unsigned char>& vchPubKey, const CPrivKey& vchPrivKey)
401 return Write(std::make_pair(std::string("key"), vchPubKey), vchPrivKey, false);
404 bool WriteCryptedKey(const std::vector<unsigned char>& vchPubKey, const std::vector<unsigned char>& vchCryptedSecret, bool fEraseUnencryptedKey = true)
407 if (!Write(std::make_pair(std::string("ckey"), vchPubKey), vchCryptedSecret, false))
409 if (fEraseUnencryptedKey)
411 Erase(std::make_pair(std::string("key"), vchPubKey));
412 Erase(std::make_pair(std::string("wkey"), vchPubKey));
417 bool WriteMasterKey(unsigned int nID, const CMasterKey& kMasterKey)
420 return Write(std::make_pair(std::string("mkey"), nID), kMasterKey, true);
423 // Support for BIP 0013 : see https://en.bitcoin.it/wiki/BIP_0013
424 bool ReadCScript(const uint160 &hash, CScript& redeemScript)
426 redeemScript.clear();
427 return Read(std::make_pair(std::string("cscript"), hash), redeemScript);
430 bool WriteCScript(const uint160& hash, const CScript& redeemScript)
433 return Write(std::make_pair(std::string("cscript"), hash), redeemScript, false);
436 bool WriteBestBlock(const CBlockLocator& locator)
439 return Write(std::string("bestblock"), locator);
442 bool ReadBestBlock(CBlockLocator& locator)
444 return Read(std::string("bestblock"), locator);
447 bool ReadDefaultKey(std::vector<unsigned char>& vchPubKey)
450 return Read(std::string("defaultkey"), vchPubKey);
453 bool WriteDefaultKey(const std::vector<unsigned char>& vchPubKey)
456 return Write(std::string("defaultkey"), vchPubKey);
459 bool ReadPool(int64 nPool, CKeyPool& keypool)
461 return Read(std::make_pair(std::string("pool"), nPool), keypool);
464 bool WritePool(int64 nPool, const CKeyPool& keypool)
467 return Write(std::make_pair(std::string("pool"), nPool), keypool);
470 bool ErasePool(int64 nPool)
473 return Erase(std::make_pair(std::string("pool"), nPool));
476 // Settings are no longer stored in wallet.dat; these are
477 // used only for backwards compatibility:
479 bool ReadSetting(const std::string& strKey, T& value)
481 return Read(std::make_pair(std::string("setting"), strKey), value);
484 bool WriteSetting(const std::string& strKey, const T& value)
487 return Write(std::make_pair(std::string("setting"), strKey), value);
489 bool EraseSetting(const std::string& strKey)
492 return Erase(std::make_pair(std::string("setting"), strKey));
495 bool WriteMinVersion(int nVersion)
497 return Write(std::string("minversion"), nVersion);
500 bool ReadAccount(const std::string& strAccount, CAccount& account);
501 bool WriteAccount(const std::string& strAccount, const CAccount& account);
502 bool WriteAccountingEntry(const CAccountingEntry& acentry);
503 int64 GetAccountCreditDebit(const std::string& strAccount);
504 void ListAccountCreditDebit(const std::string& strAccount, std::list<CAccountingEntry>& acentries);
506 int LoadWallet(CWallet* pwallet);