1 // Copyright (c) 2009-2010 Satoshi Nakamoto
\r
2 // Distributed under the MIT/X11 software license, see the accompanying
\r
3 // file license.txt or http://www.opensource.org/licenses/mit-license.php.
\r
7 class CDiskBlockIndex;
\r
15 extern map<string, string> mapAddressBook;
\r
16 extern CCriticalSection cs_mapAddressBook;
\r
17 extern vector<unsigned char> vchDefaultKey;
\r
18 extern bool fClient;
\r
19 extern int nBestHeight;
\r
22 extern unsigned int nWalletDBUpdated;
\r
26 extern void DBFlush(bool fShutdown);
\r
36 vector<DbTxn*> vTxn;
\r
39 explicit CDB(const char* pszFile, const char* pszMode="r+");
\r
45 void operator=(const CDB&);
\r
48 template<typename K, typename T>
\r
49 bool Read(const K& key, T& value)
\r
55 CDataStream ssKey(SER_DISK);
\r
56 ssKey.reserve(1000);
\r
58 Dbt datKey(&ssKey[0], ssKey.size());
\r
62 datValue.set_flags(DB_DBT_MALLOC);
\r
63 int ret = pdb->get(GetTxn(), &datKey, &datValue, 0);
\r
64 memset(datKey.get_data(), 0, datKey.get_size());
\r
65 if (datValue.get_data() == NULL)
\r
68 // Unserialize value
\r
69 CDataStream ssValue((char*)datValue.get_data(), (char*)datValue.get_data() + datValue.get_size(), SER_DISK);
\r
72 // Clear and free memory
\r
73 memset(datValue.get_data(), 0, datValue.get_size());
\r
74 free(datValue.get_data());
\r
78 template<typename K, typename T>
\r
79 bool Write(const K& key, const T& value, bool fOverwrite=true)
\r
84 assert(("Write called on database in read-only mode", false));
\r
87 CDataStream ssKey(SER_DISK);
\r
88 ssKey.reserve(1000);
\r
90 Dbt datKey(&ssKey[0], ssKey.size());
\r
93 CDataStream ssValue(SER_DISK);
\r
94 ssValue.reserve(10000);
\r
96 Dbt datValue(&ssValue[0], ssValue.size());
\r
99 int ret = pdb->put(GetTxn(), &datKey, &datValue, (fOverwrite ? 0 : DB_NOOVERWRITE));
\r
101 // Clear memory in case it was a private key
\r
102 memset(datKey.get_data(), 0, datKey.get_size());
\r
103 memset(datValue.get_data(), 0, datValue.get_size());
\r
107 template<typename K>
\r
108 bool Erase(const K& key)
\r
113 assert(("Erase called on database in read-only mode", false));
\r
116 CDataStream ssKey(SER_DISK);
\r
117 ssKey.reserve(1000);
\r
119 Dbt datKey(&ssKey[0], ssKey.size());
\r
122 int ret = pdb->del(GetTxn(), &datKey, 0);
\r
125 memset(datKey.get_data(), 0, datKey.get_size());
\r
126 return (ret == 0 || ret == DB_NOTFOUND);
\r
129 template<typename K>
\r
130 bool Exists(const K& key)
\r
136 CDataStream ssKey(SER_DISK);
\r
137 ssKey.reserve(1000);
\r
139 Dbt datKey(&ssKey[0], ssKey.size());
\r
142 int ret = pdb->exists(GetTxn(), &datKey, 0);
\r
145 memset(datKey.get_data(), 0, datKey.get_size());
\r
153 Dbc* pcursor = NULL;
\r
154 int ret = pdb->cursor(NULL, &pcursor, 0);
\r
160 int ReadAtCursor(Dbc* pcursor, CDataStream& ssKey, CDataStream& ssValue, unsigned int fFlags=DB_NEXT)
\r
164 if (fFlags == DB_SET || fFlags == DB_SET_RANGE || fFlags == DB_GET_BOTH || fFlags == DB_GET_BOTH_RANGE)
\r
166 datKey.set_data(&ssKey[0]);
\r
167 datKey.set_size(ssKey.size());
\r
170 if (fFlags == DB_GET_BOTH || fFlags == DB_GET_BOTH_RANGE)
\r
172 datValue.set_data(&ssValue[0]);
\r
173 datValue.set_size(ssValue.size());
\r
175 datKey.set_flags(DB_DBT_MALLOC);
\r
176 datValue.set_flags(DB_DBT_MALLOC);
\r
177 int ret = pcursor->get(&datKey, &datValue, fFlags);
\r
180 else if (datKey.get_data() == NULL || datValue.get_data() == NULL)
\r
183 // Convert to streams
\r
184 ssKey.SetType(SER_DISK);
\r
186 ssKey.write((char*)datKey.get_data(), datKey.get_size());
\r
187 ssValue.SetType(SER_DISK);
\r
189 ssValue.write((char*)datValue.get_data(), datValue.get_size());
\r
191 // Clear and free memory
\r
192 memset(datKey.get_data(), 0, datKey.get_size());
\r
193 memset(datValue.get_data(), 0, datValue.get_size());
\r
194 free(datKey.get_data());
\r
195 free(datValue.get_data());
\r
202 return vTxn.back();
\r
212 DbTxn* ptxn = NULL;
\r
213 int ret = dbenv.txn_begin(GetTxn(), &ptxn, DB_TXN_NOSYNC);
\r
214 if (!ptxn || ret != 0)
\r
216 vTxn.push_back(ptxn);
\r
226 int ret = vTxn.back()->commit(0);
\r
237 int ret = vTxn.back()->abort();
\r
242 bool ReadVersion(int& nVersion)
\r
245 return Read(string("version"), nVersion);
\r
248 bool WriteVersion(int nVersion)
\r
250 return Write(string("version"), nVersion);
\r
261 class CTxDB : public CDB
\r
264 CTxDB(const char* pszMode="r+") : CDB(!fClient ? "blkindex.dat" : NULL, pszMode) { }
\r
266 CTxDB(const CTxDB&);
\r
267 void operator=(const CTxDB&);
\r
269 bool ReadTxIndex(uint256 hash, CTxIndex& txindex);
\r
270 bool UpdateTxIndex(uint256 hash, const CTxIndex& txindex);
\r
271 bool AddTxIndex(const CTransaction& tx, const CDiskTxPos& pos, int nHeight);
\r
272 bool EraseTxIndex(const CTransaction& tx);
\r
273 bool ContainsTx(uint256 hash);
\r
274 bool ReadOwnerTxes(uint160 hash160, int nHeight, vector<CTransaction>& vtx);
\r
275 bool ReadDiskTx(uint256 hash, CTransaction& tx, CTxIndex& txindex);
\r
276 bool ReadDiskTx(uint256 hash, CTransaction& tx);
\r
277 bool ReadDiskTx(COutPoint outpoint, CTransaction& tx, CTxIndex& txindex);
\r
278 bool ReadDiskTx(COutPoint outpoint, CTransaction& tx);
\r
279 bool WriteBlockIndex(const CDiskBlockIndex& blockindex);
\r
280 bool EraseBlockIndex(uint256 hash);
\r
281 bool ReadHashBestChain(uint256& hashBestChain);
\r
282 bool WriteHashBestChain(uint256 hashBestChain);
\r
283 bool ReadBestInvalidWork(CBigNum& bnBestInvalidWork);
\r
284 bool WriteBestInvalidWork(CBigNum bnBestInvalidWork);
\r
285 bool LoadBlockIndex();
\r
292 class CAddrDB : public CDB
\r
295 CAddrDB(const char* pszMode="r+") : CDB("addr.dat", pszMode) { }
\r
297 CAddrDB(const CAddrDB&);
\r
298 void operator=(const CAddrDB&);
\r
300 bool WriteAddress(const CAddress& addr);
\r
301 bool LoadAddresses();
\r
304 bool LoadAddresses();
\r
311 class CWalletDB : public CDB
\r
314 CWalletDB(const char* pszMode="r+") : CDB("wallet.dat", pszMode) { }
\r
316 CWalletDB(const CWalletDB&);
\r
317 void operator=(const CWalletDB&);
\r
319 bool ReadName(const string& strAddress, string& strName)
\r
322 return Read(make_pair(string("name"), strAddress), strName);
\r
325 bool WriteName(const string& strAddress, const string& strName)
\r
327 CRITICAL_BLOCK(cs_mapAddressBook)
\r
328 mapAddressBook[strAddress] = strName;
\r
329 nWalletDBUpdated++;
\r
330 return Write(make_pair(string("name"), strAddress), strName);
\r
333 bool EraseName(const string& strAddress)
\r
335 // This should only be used for sending addresses, never for receiving addresses,
\r
336 // receiving addresses must always have an address book entry if they're not change return.
\r
337 CRITICAL_BLOCK(cs_mapAddressBook)
\r
338 mapAddressBook.erase(strAddress);
\r
339 nWalletDBUpdated++;
\r
340 return Erase(make_pair(string("name"), strAddress));
\r
343 bool ReadTx(uint256 hash, CWalletTx& wtx)
\r
345 return Read(make_pair(string("tx"), hash), wtx);
\r
348 bool WriteTx(uint256 hash, const CWalletTx& wtx)
\r
350 nWalletDBUpdated++;
\r
351 return Write(make_pair(string("tx"), hash), wtx);
\r
354 bool EraseTx(uint256 hash)
\r
356 nWalletDBUpdated++;
\r
357 return Erase(make_pair(string("tx"), hash));
\r
360 bool ReadKey(const vector<unsigned char>& vchPubKey, CPrivKey& vchPrivKey)
\r
362 vchPrivKey.clear();
\r
363 return Read(make_pair(string("key"), vchPubKey), vchPrivKey);
\r
366 bool WriteKey(const vector<unsigned char>& vchPubKey, const CPrivKey& vchPrivKey)
\r
368 nWalletDBUpdated++;
\r
369 return Write(make_pair(string("key"), vchPubKey), vchPrivKey, false);
\r
372 bool ReadDefaultKey(vector<unsigned char>& vchPubKey)
\r
375 return Read(string("defaultkey"), vchPubKey);
\r
378 bool WriteDefaultKey(const vector<unsigned char>& vchPubKey)
\r
380 vchDefaultKey = vchPubKey;
\r
381 nWalletDBUpdated++;
\r
382 return Write(string("defaultkey"), vchPubKey);
\r
385 template<typename T>
\r
386 bool ReadSetting(const string& strKey, T& value)
\r
388 return Read(make_pair(string("setting"), strKey), value);
\r
391 template<typename T>
\r
392 bool WriteSetting(const string& strKey, const T& value)
\r
394 nWalletDBUpdated++;
\r
395 return Write(make_pair(string("setting"), strKey), value);
\r
401 bool LoadWallet(bool& fFirstRunRet);
\r
403 inline bool SetAddressBookName(const string& strAddress, const string& strName)
\r
405 return CWalletDB().WriteName(strAddress, strName);
\r