Prevent crashes due to missing or corrupted database records
[novacoin.git] / src / db.h
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 COPYING or http://www.opensource.org/licenses/mit-license.php.
5 #ifndef BITCOIN_DB_H
6 #define BITCOIN_DB_H
7
8 #include "key.h"
9
10 #include <map>
11 #include <string>
12 #include <vector>
13
14 #include <db_cxx.h>
15
16 class CTxIndex;
17 class CDiskBlockIndex;
18 class CDiskTxPos;
19 class COutPoint;
20 class CAddress;
21 class CWalletTx;
22 class CWallet;
23 class CAccount;
24 class CAccountingEntry;
25 class CBlockLocator;
26
27
28 extern unsigned int nWalletDBUpdated;
29 extern DbEnv dbenv;
30
31 extern void DBFlush(bool fShutdown);
32 void ThreadFlushWalletDB(void* parg);
33 bool BackupWallet(const CWallet& wallet, const std::string& strDest);
34
35
36
37 class CDB
38 {
39 protected:
40     Db* pdb;
41     std::string strFile;
42     std::vector<DbTxn*> vTxn;
43     bool fReadOnly;
44
45     explicit CDB(const char* pszFile, const char* pszMode="r+");
46     ~CDB() { Close(); }
47 public:
48     void Close();
49 private:
50     CDB(const CDB&);
51     void operator=(const CDB&);
52
53 protected:
54     template<typename K, typename T>
55     bool Read(const K& key, T& value)
56     {
57         if (!pdb)
58             return false;
59
60         // Key
61         CDataStream ssKey(SER_DISK);
62         ssKey.reserve(1000);
63         ssKey << key;
64         Dbt datKey(&ssKey[0], ssKey.size());
65
66         // Read
67         Dbt datValue;
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)
72             return false;
73
74         // Unserialize value
75         try {
76             CDataStream ssValue((char*)datValue.get_data(), (char*)datValue.get_data() + datValue.get_size(), SER_DISK);
77             ssValue >> value;
78         }
79         catch (std::exception &e) {
80             return false;
81         }
82
83         // Clear and free memory
84         memset(datValue.get_data(), 0, datValue.get_size());
85         free(datValue.get_data());
86         return (ret == 0);
87     }
88
89     template<typename K, typename T>
90     bool Write(const K& key, const T& value, bool fOverwrite=true)
91     {
92         if (!pdb)
93             return false;
94         if (fReadOnly)
95             assert(!"Write called on database in read-only mode");
96
97         // Key
98         CDataStream ssKey(SER_DISK);
99         ssKey.reserve(1000);
100         ssKey << key;
101         Dbt datKey(&ssKey[0], ssKey.size());
102
103         // Value
104         CDataStream ssValue(SER_DISK);
105         ssValue.reserve(10000);
106         ssValue << value;
107         Dbt datValue(&ssValue[0], ssValue.size());
108
109         // Write
110         int ret = pdb->put(GetTxn(), &datKey, &datValue, (fOverwrite ? 0 : DB_NOOVERWRITE));
111
112         // Clear memory in case it was a private key
113         memset(datKey.get_data(), 0, datKey.get_size());
114         memset(datValue.get_data(), 0, datValue.get_size());
115         return (ret == 0);
116     }
117
118     template<typename K>
119     bool Erase(const K& key)
120     {
121         if (!pdb)
122             return false;
123         if (fReadOnly)
124             assert(!"Erase called on database in read-only mode");
125
126         // Key
127         CDataStream ssKey(SER_DISK);
128         ssKey.reserve(1000);
129         ssKey << key;
130         Dbt datKey(&ssKey[0], ssKey.size());
131
132         // Erase
133         int ret = pdb->del(GetTxn(), &datKey, 0);
134
135         // Clear memory
136         memset(datKey.get_data(), 0, datKey.get_size());
137         return (ret == 0 || ret == DB_NOTFOUND);
138     }
139
140     template<typename K>
141     bool Exists(const K& key)
142     {
143         if (!pdb)
144             return false;
145
146         // Key
147         CDataStream ssKey(SER_DISK);
148         ssKey.reserve(1000);
149         ssKey << key;
150         Dbt datKey(&ssKey[0], ssKey.size());
151
152         // Exists
153         int ret = pdb->exists(GetTxn(), &datKey, 0);
154
155         // Clear memory
156         memset(datKey.get_data(), 0, datKey.get_size());
157         return (ret == 0);
158     }
159
160     Dbc* GetCursor()
161     {
162         if (!pdb)
163             return NULL;
164         Dbc* pcursor = NULL;
165         int ret = pdb->cursor(NULL, &pcursor, 0);
166         if (ret != 0)
167             return NULL;
168         return pcursor;
169     }
170
171     int ReadAtCursor(Dbc* pcursor, CDataStream& ssKey, CDataStream& ssValue, unsigned int fFlags=DB_NEXT)
172     {
173         // Read at cursor
174         Dbt datKey;
175         if (fFlags == DB_SET || fFlags == DB_SET_RANGE || fFlags == DB_GET_BOTH || fFlags == DB_GET_BOTH_RANGE)
176         {
177             datKey.set_data(&ssKey[0]);
178             datKey.set_size(ssKey.size());
179         }
180         Dbt datValue;
181         if (fFlags == DB_GET_BOTH || fFlags == DB_GET_BOTH_RANGE)
182         {
183             datValue.set_data(&ssValue[0]);
184             datValue.set_size(ssValue.size());
185         }
186         datKey.set_flags(DB_DBT_MALLOC);
187         datValue.set_flags(DB_DBT_MALLOC);
188         int ret = pcursor->get(&datKey, &datValue, fFlags);
189         if (ret != 0)
190             return ret;
191         else if (datKey.get_data() == NULL || datValue.get_data() == NULL)
192             return 99999;
193
194         // Convert to streams
195         ssKey.SetType(SER_DISK);
196         ssKey.clear();
197         ssKey.write((char*)datKey.get_data(), datKey.get_size());
198         ssValue.SetType(SER_DISK);
199         ssValue.clear();
200         ssValue.write((char*)datValue.get_data(), datValue.get_size());
201
202         // Clear and free memory
203         memset(datKey.get_data(), 0, datKey.get_size());
204         memset(datValue.get_data(), 0, datValue.get_size());
205         free(datKey.get_data());
206         free(datValue.get_data());
207         return 0;
208     }
209
210     DbTxn* GetTxn()
211     {
212         if (!vTxn.empty())
213             return vTxn.back();
214         else
215             return NULL;
216     }
217
218 public:
219     bool TxnBegin()
220     {
221         if (!pdb)
222             return false;
223         DbTxn* ptxn = NULL;
224         int ret = dbenv.txn_begin(GetTxn(), &ptxn, DB_TXN_NOSYNC);
225         if (!ptxn || ret != 0)
226             return false;
227         vTxn.push_back(ptxn);
228         return true;
229     }
230
231     bool TxnCommit()
232     {
233         if (!pdb)
234             return false;
235         if (vTxn.empty())
236             return false;
237         int ret = vTxn.back()->commit(0);
238         vTxn.pop_back();
239         return (ret == 0);
240     }
241
242     bool TxnAbort()
243     {
244         if (!pdb)
245             return false;
246         if (vTxn.empty())
247             return false;
248         int ret = vTxn.back()->abort();
249         vTxn.pop_back();
250         return (ret == 0);
251     }
252
253     bool ReadVersion(int& nVersion)
254     {
255         nVersion = 0;
256         return Read(std::string("version"), nVersion);
257     }
258
259     bool WriteVersion(int nVersion)
260     {
261         return Write(std::string("version"), nVersion);
262     }
263
264     bool static Rewrite(const std::string& strFile, const char* pszSkip = NULL);
265 };
266
267
268
269
270
271
272
273
274 class CTxDB : public CDB
275 {
276 public:
277     CTxDB(const char* pszMode="r+") : CDB("blkindex.dat", pszMode) { }
278 private:
279     CTxDB(const CTxDB&);
280     void operator=(const CTxDB&);
281 public:
282     bool ReadTxIndex(uint256 hash, CTxIndex& txindex);
283     bool UpdateTxIndex(uint256 hash, const CTxIndex& txindex);
284     bool AddTxIndex(const CTransaction& tx, const CDiskTxPos& pos, int nHeight);
285     bool EraseTxIndex(const CTransaction& tx);
286     bool ContainsTx(uint256 hash);
287     bool ReadOwnerTxes(uint160 hash160, int nHeight, std::vector<CTransaction>& vtx);
288     bool ReadDiskTx(uint256 hash, CTransaction& tx, CTxIndex& txindex);
289     bool ReadDiskTx(uint256 hash, CTransaction& tx);
290     bool ReadDiskTx(COutPoint outpoint, CTransaction& tx, CTxIndex& txindex);
291     bool ReadDiskTx(COutPoint outpoint, CTransaction& tx);
292     bool WriteBlockIndex(const CDiskBlockIndex& blockindex);
293     bool EraseBlockIndex(uint256 hash);
294     bool ReadHashBestChain(uint256& hashBestChain);
295     bool WriteHashBestChain(uint256 hashBestChain);
296     bool ReadBestInvalidWork(CBigNum& bnBestInvalidWork);
297     bool WriteBestInvalidWork(CBigNum bnBestInvalidWork);
298     bool LoadBlockIndex();
299 };
300
301
302
303
304
305 class CAddrDB : public CDB
306 {
307 public:
308     CAddrDB(const char* pszMode="r+") : CDB("addr.dat", pszMode) { }
309 private:
310     CAddrDB(const CAddrDB&);
311     void operator=(const CAddrDB&);
312 public:
313     bool WriteAddress(const CAddress& addr);
314     bool EraseAddress(const CAddress& addr);
315     bool LoadAddresses();
316 };
317
318 bool LoadAddresses();
319
320
321
322 class CKeyPool
323 {
324 public:
325     int64 nTime;
326     std::vector<unsigned char> vchPubKey;
327
328     CKeyPool()
329     {
330         nTime = GetTime();
331     }
332
333     CKeyPool(const std::vector<unsigned char>& vchPubKeyIn)
334     {
335         nTime = GetTime();
336         vchPubKey = vchPubKeyIn;
337     }
338
339     IMPLEMENT_SERIALIZE
340     (
341         if (!(nType & SER_GETHASH))
342             READWRITE(nVersion);
343         READWRITE(nTime);
344         READWRITE(vchPubKey);
345     )
346 };
347
348
349
350
351 enum DBErrors
352 {
353     DB_LOAD_OK,
354     DB_CORRUPT,
355     DB_TOO_NEW,
356     DB_LOAD_FAIL,
357     DB_NEED_REWRITE
358 };
359
360 class CWalletDB : public CDB
361 {
362 public:
363     CWalletDB(std::string strFilename, const char* pszMode="r+") : CDB(strFilename.c_str(), pszMode)
364     {
365     }
366 private:
367     CWalletDB(const CWalletDB&);
368     void operator=(const CWalletDB&);
369 public:
370     bool ReadName(const std::string& strAddress, std::string& strName)
371     {
372         strName = "";
373         return Read(std::make_pair(std::string("name"), strAddress), strName);
374     }
375
376     bool WriteName(const std::string& strAddress, const std::string& strName);
377
378     bool EraseName(const std::string& strAddress);
379
380     bool ReadTx(uint256 hash, CWalletTx& wtx)
381     {
382         return Read(std::make_pair(std::string("tx"), hash), wtx);
383     }
384
385     bool WriteTx(uint256 hash, const CWalletTx& wtx)
386     {
387         nWalletDBUpdated++;
388         return Write(std::make_pair(std::string("tx"), hash), wtx);
389     }
390
391     bool EraseTx(uint256 hash)
392     {
393         nWalletDBUpdated++;
394         return Erase(std::make_pair(std::string("tx"), hash));
395     }
396
397     bool ReadKey(const std::vector<unsigned char>& vchPubKey, CPrivKey& vchPrivKey)
398     {
399         vchPrivKey.clear();
400         return Read(std::make_pair(std::string("key"), vchPubKey), vchPrivKey);
401     }
402
403     bool WriteKey(const std::vector<unsigned char>& vchPubKey, const CPrivKey& vchPrivKey)
404     {
405         nWalletDBUpdated++;
406         return Write(std::make_pair(std::string("key"), vchPubKey), vchPrivKey, false);
407     }
408
409     bool WriteCryptedKey(const std::vector<unsigned char>& vchPubKey, const std::vector<unsigned char>& vchCryptedSecret, bool fEraseUnencryptedKey = true)
410     {
411         nWalletDBUpdated++;
412         if (!Write(std::make_pair(std::string("ckey"), vchPubKey), vchCryptedSecret, false))
413             return false;
414         if (fEraseUnencryptedKey)
415         {
416             Erase(std::make_pair(std::string("key"), vchPubKey));
417             Erase(std::make_pair(std::string("wkey"), vchPubKey));
418         }
419         return true;
420     }
421
422     bool WriteMasterKey(unsigned int nID, const CMasterKey& kMasterKey)
423     {
424         nWalletDBUpdated++;
425         return Write(std::make_pair(std::string("mkey"), nID), kMasterKey, true);
426     }
427
428     bool WriteBestBlock(const CBlockLocator& locator)
429     {
430         nWalletDBUpdated++;
431         return Write(std::string("bestblock"), locator);
432     }
433
434     bool ReadBestBlock(CBlockLocator& locator)
435     {
436         return Read(std::string("bestblock"), locator);
437     }
438
439     bool ReadDefaultKey(std::vector<unsigned char>& vchPubKey)
440     {
441         vchPubKey.clear();
442         return Read(std::string("defaultkey"), vchPubKey);
443     }
444
445     bool WriteDefaultKey(const std::vector<unsigned char>& vchPubKey)
446     {
447         nWalletDBUpdated++;
448         return Write(std::string("defaultkey"), vchPubKey);
449     }
450
451     bool ReadPool(int64 nPool, CKeyPool& keypool)
452     {
453         return Read(std::make_pair(std::string("pool"), nPool), keypool);
454     }
455
456     bool WritePool(int64 nPool, const CKeyPool& keypool)
457     {
458         nWalletDBUpdated++;
459         return Write(std::make_pair(std::string("pool"), nPool), keypool);
460     }
461
462     bool ErasePool(int64 nPool)
463     {
464         nWalletDBUpdated++;
465         return Erase(std::make_pair(std::string("pool"), nPool));
466     }
467
468     template<typename T>
469     bool ReadSetting(const std::string& strKey, T& value)
470     {
471         return Read(std::make_pair(std::string("setting"), strKey), value);
472     }
473
474     template<typename T>
475     bool WriteSetting(const std::string& strKey, const T& value)
476     {
477         nWalletDBUpdated++;
478         return Write(std::make_pair(std::string("setting"), strKey), value);
479     }
480
481     bool ReadAccount(const std::string& strAccount, CAccount& account);
482     bool WriteAccount(const std::string& strAccount, const CAccount& account);
483     bool WriteAccountingEntry(const CAccountingEntry& acentry);
484     int64 GetAccountCreditDebit(const std::string& strAccount);
485     void ListAccountCreditDebit(const std::string& strAccount, std::list<CAccountingEntry>& acentries);
486
487     int LoadWallet(CWallet* pwallet);
488 };
489
490 #endif