Bitcoin r202 (last Satoshi commit) · 2010-12-15
db.h
1// Copyright (c) 2009-2010 Satoshi Nakamoto2// Distributed under the MIT/X11 software license, see the accompanying3// file license.txt or http://www.opensource.org/licenses/mit-license.php.45class CTransaction;6class CTxIndex;7class CDiskBlockIndex;8class CDiskTxPos;9class COutPoint;10class CUser;11class CReview;12class CAddress;13class CWalletTx;14class CAccount;15class CAccountingEntry;1617extern map<string, string> mapAddressBook;18extern CCriticalSection cs_mapAddressBook;19extern vector<unsigned char> vchDefaultKey;20extern bool fClient;21extern int nBestHeight;222324extern unsigned int nWalletDBUpdated;25extern DbEnv dbenv;262728extern void DBFlush(bool fShutdown);29extern vector<unsigned char> GetKeyFromKeyPool();30extern int64 GetOldestKeyPoolTime();3132333435class CDB36{37protected:38 Db* pdb;39 string strFile;40 vector<DbTxn*> vTxn;41 bool fReadOnly;4243 explicit CDB(const char* pszFile, const char* pszMode="r+");44 ~CDB() { Close(); }45public:46 void Close();47private:48 CDB(const CDB&);49 void operator=(const CDB&);5051protected:52 template<typename K, typename T>53 bool Read(const K& key, T& value)54 {55 if (!pdb)56 return false;5758 // Key59 CDataStream ssKey(SER_DISK);60 ssKey.reserve(1000);61 ssKey << key;62 Dbt datKey(&ssKey[0], ssKey.size());6364 // Read65 Dbt datValue;66 datValue.set_flags(DB_DBT_MALLOC);67 int ret = pdb->get(GetTxn(), &datKey, &datValue, 0);68 memset(datKey.get_data(), 0, datKey.get_size());69 if (datValue.get_data() == NULL)70 return false;7172 // Unserialize value73 CDataStream ssValue((char*)datValue.get_data(), (char*)datValue.get_data() + datValue.get_size(), SER_DISK);74 ssValue >> value;7576 // Clear and free memory77 memset(datValue.get_data(), 0, datValue.get_size());78 free(datValue.get_data());79 return (ret == 0);80 }8182 template<typename K, typename T>83 bool Write(const K& key, const T& value, bool fOverwrite=true)84 {85 if (!pdb)86 return false;87 if (fReadOnly)88 assert(("Write called on database in read-only mode", false));8990 // Key91 CDataStream ssKey(SER_DISK);92 ssKey.reserve(1000);93 ssKey << key;94 Dbt datKey(&ssKey[0], ssKey.size());9596 // Value97 CDataStream ssValue(SER_DISK);98 ssValue.reserve(10000);99 ssValue << value;100 Dbt datValue(&ssValue[0], ssValue.size());101102 // Write103 int ret = pdb->put(GetTxn(), &datKey, &datValue, (fOverwrite ? 0 : DB_NOOVERWRITE));104105 // Clear memory in case it was a private key106 memset(datKey.get_data(), 0, datKey.get_size());107 memset(datValue.get_data(), 0, datValue.get_size());108 return (ret == 0);109 }110111 template<typename K>112 bool Erase(const K& key)113 {114 if (!pdb)115 return false;116 if (fReadOnly)117 assert(("Erase called on database in read-only mode", false));118119 // Key120 CDataStream ssKey(SER_DISK);121 ssKey.reserve(1000);122 ssKey << key;123 Dbt datKey(&ssKey[0], ssKey.size());124125 // Erase126 int ret = pdb->del(GetTxn(), &datKey, 0);127128 // Clear memory129 memset(datKey.get_data(), 0, datKey.get_size());130 return (ret == 0 || ret == DB_NOTFOUND);131 }132133 template<typename K>134 bool Exists(const K& key)135 {136 if (!pdb)137 return false;138139 // Key140 CDataStream ssKey(SER_DISK);141 ssKey.reserve(1000);142 ssKey << key;143 Dbt datKey(&ssKey[0], ssKey.size());144145 // Exists146 int ret = pdb->exists(GetTxn(), &datKey, 0);147148 // Clear memory149 memset(datKey.get_data(), 0, datKey.get_size());150 return (ret == 0);151 }152153 Dbc* GetCursor()154 {155 if (!pdb)156 return NULL;157 Dbc* pcursor = NULL;158 int ret = pdb->cursor(NULL, &pcursor, 0);159 if (ret != 0)160 return NULL;161 return pcursor;162 }163164 int ReadAtCursor(Dbc* pcursor, CDataStream& ssKey, CDataStream& ssValue, unsigned int fFlags=DB_NEXT)165 {166 // Read at cursor167 Dbt datKey;168 if (fFlags == DB_SET || fFlags == DB_SET_RANGE || fFlags == DB_GET_BOTH || fFlags == DB_GET_BOTH_RANGE)169 {170 datKey.set_data(&ssKey[0]);171 datKey.set_size(ssKey.size());172 }173 Dbt datValue;174 if (fFlags == DB_GET_BOTH || fFlags == DB_GET_BOTH_RANGE)175 {176 datValue.set_data(&ssValue[0]);177 datValue.set_size(ssValue.size());178 }179 datKey.set_flags(DB_DBT_MALLOC);180 datValue.set_flags(DB_DBT_MALLOC);181 int ret = pcursor->get(&datKey, &datValue, fFlags);182 if (ret != 0)183 return ret;184 else if (datKey.get_data() == NULL || datValue.get_data() == NULL)185 return 99999;186187 // Convert to streams188 ssKey.SetType(SER_DISK);189 ssKey.clear();190 ssKey.write((char*)datKey.get_data(), datKey.get_size());191 ssValue.SetType(SER_DISK);192 ssValue.clear();193 ssValue.write((char*)datValue.get_data(), datValue.get_size());194195 // Clear and free memory196 memset(datKey.get_data(), 0, datKey.get_size());197 memset(datValue.get_data(), 0, datValue.get_size());198 free(datKey.get_data());199 free(datValue.get_data());200 return 0;201 }202203 DbTxn* GetTxn()204 {205 if (!vTxn.empty())206 return vTxn.back();207 else208 return NULL;209 }210211public:212 bool TxnBegin()213 {214 if (!pdb)215 return false;216 DbTxn* ptxn = NULL;217 int ret = dbenv.txn_begin(GetTxn(), &ptxn, DB_TXN_NOSYNC);218 if (!ptxn || ret != 0)219 return false;220 vTxn.push_back(ptxn);221 return true;222 }223224 bool TxnCommit()225 {226 if (!pdb)227 return false;228 if (vTxn.empty())229 return false;230 int ret = vTxn.back()->commit(0);231 vTxn.pop_back();232 return (ret == 0);233 }234235 bool TxnAbort()236 {237 if (!pdb)238 return false;239 if (vTxn.empty())240 return false;241 int ret = vTxn.back()->abort();242 vTxn.pop_back();243 return (ret == 0);244 }245246 bool ReadVersion(int& nVersion)247 {248 nVersion = 0;249 return Read(string("version"), nVersion);250 }251252 bool WriteVersion(int nVersion)253 {254 return Write(string("version"), nVersion);255 }256};257258259260261262263264265class CTxDB : public CDB266{267public:268 CTxDB(const char* pszMode="r+") : CDB("blkindex.dat", pszMode) { }269private:270 CTxDB(const CTxDB&);271 void operator=(const CTxDB&);272public:273 bool ReadTxIndex(uint256 hash, CTxIndex& txindex);274 bool UpdateTxIndex(uint256 hash, const CTxIndex& txindex);275 bool AddTxIndex(const CTransaction& tx, const CDiskTxPos& pos, int nHeight);276 bool EraseTxIndex(const CTransaction& tx);277 bool ContainsTx(uint256 hash);278 bool ReadOwnerTxes(uint160 hash160, int nHeight, vector<CTransaction>& vtx);279 bool ReadDiskTx(uint256 hash, CTransaction& tx, CTxIndex& txindex);280 bool ReadDiskTx(uint256 hash, CTransaction& tx);281 bool ReadDiskTx(COutPoint outpoint, CTransaction& tx, CTxIndex& txindex);282 bool ReadDiskTx(COutPoint outpoint, CTransaction& tx);283 bool WriteBlockIndex(const CDiskBlockIndex& blockindex);284 bool EraseBlockIndex(uint256 hash);285 bool ReadHashBestChain(uint256& hashBestChain);286 bool WriteHashBestChain(uint256 hashBestChain);287 bool ReadBestInvalidWork(CBigNum& bnBestInvalidWork);288 bool WriteBestInvalidWork(CBigNum bnBestInvalidWork);289 bool LoadBlockIndex();290};291292293294295296class CAddrDB : public CDB297{298public:299 CAddrDB(const char* pszMode="r+") : CDB("addr.dat", pszMode) { }300private:301 CAddrDB(const CAddrDB&);302 void operator=(const CAddrDB&);303public:304 bool WriteAddress(const CAddress& addr);305 bool EraseAddress(const CAddress& addr);306 bool LoadAddresses();307};308309bool LoadAddresses();310311312313314315316class CKeyPool317{318public:319 int64 nTime;320 vector<unsigned char> vchPubKey;321322 CKeyPool()323 {324 nTime = GetTime();325 }326327 CKeyPool(const vector<unsigned char>& vchPubKeyIn)328 {329 nTime = GetTime();330 vchPubKey = vchPubKeyIn;331 }332333 IMPLEMENT_SERIALIZE334 (335 if (!(nType & SER_GETHASH))336 READWRITE(nVersion);337 READWRITE(nTime);338 READWRITE(vchPubKey);339 )340};341342343344345class CWalletDB : public CDB346{347public:348 CWalletDB(const char* pszMode="r+") : CDB("wallet.dat", pszMode)349 {350 }351private:352 CWalletDB(const CWalletDB&);353 void operator=(const CWalletDB&);354public:355 bool ReadName(const string& strAddress, string& strName)356 {357 strName = "";358 return Read(make_pair(string("name"), strAddress), strName);359 }360361 bool WriteName(const string& strAddress, const string& strName)362 {363 CRITICAL_BLOCK(cs_mapAddressBook)364 mapAddressBook[strAddress] = strName;365 nWalletDBUpdated++;366 return Write(make_pair(string("name"), strAddress), strName);367 }368369 bool EraseName(const string& strAddress)370 {371 // This should only be used for sending addresses, never for receiving addresses,372 // receiving addresses must always have an address book entry if they're not change return.373 CRITICAL_BLOCK(cs_mapAddressBook)374 mapAddressBook.erase(strAddress);375 nWalletDBUpdated++;376 return Erase(make_pair(string("name"), strAddress));377 }378379 bool ReadTx(uint256 hash, CWalletTx& wtx)380 {381 return Read(make_pair(string("tx"), hash), wtx);382 }383384 bool WriteTx(uint256 hash, const CWalletTx& wtx)385 {386 nWalletDBUpdated++;387 return Write(make_pair(string("tx"), hash), wtx);388 }389390 bool EraseTx(uint256 hash)391 {392 nWalletDBUpdated++;393 return Erase(make_pair(string("tx"), hash));394 }395396 bool ReadKey(const vector<unsigned char>& vchPubKey, CPrivKey& vchPrivKey)397 {398 vchPrivKey.clear();399 return Read(make_pair(string("key"), vchPubKey), vchPrivKey);400 }401402 bool WriteKey(const vector<unsigned char>& vchPubKey, const CPrivKey& vchPrivKey)403 {404 nWalletDBUpdated++;405 return Write(make_pair(string("key"), vchPubKey), vchPrivKey, false);406 }407408 bool ReadDefaultKey(vector<unsigned char>& vchPubKey)409 {410 vchPubKey.clear();411 return Read(string("defaultkey"), vchPubKey);412 }413414 bool WriteDefaultKey(const vector<unsigned char>& vchPubKey)415 {416 vchDefaultKey = vchPubKey;417 nWalletDBUpdated++;418 return Write(string("defaultkey"), vchPubKey);419 }420421 template<typename T>422 bool ReadSetting(const string& strKey, T& value)423 {424 return Read(make_pair(string("setting"), strKey), value);425 }426427 template<typename T>428 bool WriteSetting(const string& strKey, const T& value)429 {430 nWalletDBUpdated++;431 return Write(make_pair(string("setting"), strKey), value);432 }433434 bool ReadAccount(const string& strAccount, CAccount& account);435 bool WriteAccount(const string& strAccount, const CAccount& account);436 bool WriteAccountingEntry(const string& strAccount, const CAccountingEntry& acentry);437 int64 GetAccountCreditDebit(const string& strAccount);438 void ListAccountCreditDebit(const string& strAccount, list<CAccountingEntry>& acentries);439440 bool LoadWallet();441protected:442 void ReserveKeyFromKeyPool(int64& nIndex, CKeyPool& keypool);443 void KeepKey(int64 nIndex);444 static void ReturnKey(int64 nIndex);445 friend class CReserveKey;446 friend vector<unsigned char> GetKeyFromKeyPool();447 friend int64 GetOldestKeyPoolTime();448};449450bool LoadWallet(bool& fFirstRunRet);451void BackupWallet(const string& strDest);452453inline bool SetAddressBookName(const string& strAddress, const string& strName)454{455 return CWalletDB().WriteName(strAddress, strName);456}457458class CReserveKey459{460protected:461 int64 nIndex;462 vector<unsigned char> vchPubKey;463public:464 CReserveKey()465 {466 nIndex = -1;467 }468469 ~CReserveKey()470 {471 if (!fShutdown)472 ReturnKey();473 }474475 vector<unsigned char> GetReservedKey()476 {477 if (nIndex == -1)478 {479 CKeyPool keypool;480 CWalletDB().ReserveKeyFromKeyPool(nIndex, keypool);481 vchPubKey = keypool.vchPubKey;482 }483 assert(!vchPubKey.empty());484 return vchPubKey;485 }486487 void KeepKey()488 {489 if (nIndex != -1)490 CWalletDB().KeepKey(nIndex);491 nIndex = -1;492 vchPubKey.clear();493 }494495 void ReturnKey()496 {497 if (nIndex != -1)498 CWalletDB::ReturnKey(nIndex);499 nIndex = -1;500 vchPubKey.clear();501 }502};
Click a line number to link to it; use #L10-L20 for a range. "=" next to a version means the file is identical there.
