new getwork
[novacoin.git] / db.cpp
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Distributed under the MIT/X11 software license, see the accompanying
3 // file license.txt or http://www.opensource.org/licenses/mit-license.php.
4
5 #include "headers.h"
6
7 void ThreadFlushWalletDB(void* parg);
8
9
10 unsigned int nWalletDBUpdated;
11 uint64 nAccountingEntryNumber = 0;
12
13
14
15 //
16 // CDB
17 //
18
19 static CCriticalSection cs_db;
20 static bool fDbEnvInit = false;
21 DbEnv dbenv(0);
22 static map<string, int> mapFileUseCount;
23 static map<string, Db*> mapDb;
24
25 class CDBInit
26 {
27 public:
28     CDBInit()
29     {
30     }
31     ~CDBInit()
32     {
33         if (fDbEnvInit)
34         {
35             dbenv.close(0);
36             fDbEnvInit = false;
37         }
38     }
39 }
40 instance_of_cdbinit;
41
42
43 CDB::CDB(const char* pszFile, const char* pszMode) : pdb(NULL)
44 {
45     int ret;
46     if (pszFile == NULL)
47         return;
48
49     fReadOnly = (!strchr(pszMode, '+') && !strchr(pszMode, 'w'));
50     bool fCreate = strchr(pszMode, 'c');
51     unsigned int nFlags = DB_THREAD;
52     if (fCreate)
53         nFlags |= DB_CREATE;
54
55     CRITICAL_BLOCK(cs_db)
56     {
57         if (!fDbEnvInit)
58         {
59             if (fShutdown)
60                 return;
61             string strDataDir = GetDataDir();
62             string strLogDir = strDataDir + "/database";
63             filesystem::create_directory(strLogDir.c_str());
64             string strErrorFile = strDataDir + "/db.log";
65             printf("dbenv.open strLogDir=%s strErrorFile=%s\n", strLogDir.c_str(), strErrorFile.c_str());
66
67             dbenv.set_lg_dir(strLogDir.c_str());
68             dbenv.set_lg_max(10000000);
69             dbenv.set_lk_max_locks(10000);
70             dbenv.set_lk_max_objects(10000);
71             dbenv.set_errfile(fopen(strErrorFile.c_str(), "a")); /// debug
72             dbenv.set_flags(DB_AUTO_COMMIT, 1);
73             ret = dbenv.open(strDataDir.c_str(),
74                              DB_CREATE     |
75                              DB_INIT_LOCK  |
76                              DB_INIT_LOG   |
77                              DB_INIT_MPOOL |
78                              DB_INIT_TXN   |
79                              DB_THREAD     |
80                              DB_RECOVER,
81                              S_IRUSR | S_IWUSR);
82             if (ret > 0)
83                 throw runtime_error(strprintf("CDB() : error %d opening database environment", ret));
84             fDbEnvInit = true;
85         }
86
87         strFile = pszFile;
88         ++mapFileUseCount[strFile];
89         pdb = mapDb[strFile];
90         if (pdb == NULL)
91         {
92             pdb = new Db(&dbenv, 0);
93
94             ret = pdb->open(NULL,      // Txn pointer
95                             pszFile,   // Filename
96                             "main",    // Logical db name
97                             DB_BTREE,  // Database type
98                             nFlags,    // Flags
99                             0);
100
101             if (ret > 0)
102             {
103                 delete pdb;
104                 pdb = NULL;
105                 CRITICAL_BLOCK(cs_db)
106                     --mapFileUseCount[strFile];
107                 strFile = "";
108                 throw runtime_error(strprintf("CDB() : can't open database file %s, error %d", pszFile, ret));
109             }
110
111             if (fCreate && !Exists(string("version")))
112             {
113                 bool fTmp = fReadOnly;
114                 fReadOnly = false;
115                 WriteVersion(VERSION);
116                 fReadOnly = fTmp;
117             }
118
119             mapDb[strFile] = pdb;
120         }
121     }
122 }
123
124 void CDB::Close()
125 {
126     if (!pdb)
127         return;
128     if (!vTxn.empty())
129         vTxn.front()->abort();
130     vTxn.clear();
131     pdb = NULL;
132
133     // Flush database activity from memory pool to disk log
134     unsigned int nMinutes = 0;
135     if (strFile == "addr.dat")
136         nMinutes = 2;
137     if (strFile == "blkindex.dat" && IsInitialBlockDownload() && nBestHeight % 500 != 0)
138         nMinutes = 1;
139     dbenv.txn_checkpoint(0, nMinutes, 0);
140
141     CRITICAL_BLOCK(cs_db)
142         --mapFileUseCount[strFile];
143 }
144
145 void CloseDb(const string& strFile)
146 {
147     CRITICAL_BLOCK(cs_db)
148     {
149         if (mapDb[strFile] != NULL)
150         {
151             // Close the database handle
152             Db* pdb = mapDb[strFile];
153             pdb->close(0);
154             delete pdb;
155             mapDb[strFile] = NULL;
156         }
157     }
158 }
159
160 void DBFlush(bool fShutdown)
161 {
162     // Flush log data to the actual data file
163     //  on all files that are not in use
164     printf("DBFlush(%s)%s\n", fShutdown ? "true" : "false", fDbEnvInit ? "" : " db not started");
165     if (!fDbEnvInit)
166         return;
167     CRITICAL_BLOCK(cs_db)
168     {
169         map<string, int>::iterator mi = mapFileUseCount.begin();
170         while (mi != mapFileUseCount.end())
171         {
172             string strFile = (*mi).first;
173             int nRefCount = (*mi).second;
174             printf("%s refcount=%d\n", strFile.c_str(), nRefCount);
175             if (nRefCount == 0)
176             {
177                 // Move log data to the dat file
178                 CloseDb(strFile);
179                 dbenv.txn_checkpoint(0, 0, 0);
180                 printf("%s flush\n", strFile.c_str());
181                 dbenv.lsn_reset(strFile.c_str(), 0);
182                 mapFileUseCount.erase(mi++);
183             }
184             else
185                 mi++;
186         }
187         if (fShutdown)
188         {
189             char** listp;
190             if (mapFileUseCount.empty())
191                 dbenv.log_archive(&listp, DB_ARCH_REMOVE);
192             dbenv.close(0);
193             fDbEnvInit = false;
194         }
195     }
196 }
197
198
199
200
201
202
203 //
204 // CTxDB
205 //
206
207 bool CTxDB::ReadTxIndex(uint256 hash, CTxIndex& txindex)
208 {
209     assert(!fClient);
210     txindex.SetNull();
211     return Read(make_pair(string("tx"), hash), txindex);
212 }
213
214 bool CTxDB::UpdateTxIndex(uint256 hash, const CTxIndex& txindex)
215 {
216     assert(!fClient);
217     return Write(make_pair(string("tx"), hash), txindex);
218 }
219
220 bool CTxDB::AddTxIndex(const CTransaction& tx, const CDiskTxPos& pos, int nHeight)
221 {
222     assert(!fClient);
223
224     // Add to tx index
225     uint256 hash = tx.GetHash();
226     CTxIndex txindex(pos, tx.vout.size());
227     return Write(make_pair(string("tx"), hash), txindex);
228 }
229
230 bool CTxDB::EraseTxIndex(const CTransaction& tx)
231 {
232     assert(!fClient);
233     uint256 hash = tx.GetHash();
234
235     return Erase(make_pair(string("tx"), hash));
236 }
237
238 bool CTxDB::ContainsTx(uint256 hash)
239 {
240     assert(!fClient);
241     return Exists(make_pair(string("tx"), hash));
242 }
243
244 bool CTxDB::ReadOwnerTxes(uint160 hash160, int nMinHeight, vector<CTransaction>& vtx)
245 {
246     assert(!fClient);
247     vtx.clear();
248
249     // Get cursor
250     Dbc* pcursor = GetCursor();
251     if (!pcursor)
252         return false;
253
254     unsigned int fFlags = DB_SET_RANGE;
255     loop
256     {
257         // Read next record
258         CDataStream ssKey;
259         if (fFlags == DB_SET_RANGE)
260             ssKey << string("owner") << hash160 << CDiskTxPos(0, 0, 0);
261         CDataStream ssValue;
262         int ret = ReadAtCursor(pcursor, ssKey, ssValue, fFlags);
263         fFlags = DB_NEXT;
264         if (ret == DB_NOTFOUND)
265             break;
266         else if (ret != 0)
267         {
268             pcursor->close();
269             return false;
270         }
271
272         // Unserialize
273         string strType;
274         uint160 hashItem;
275         CDiskTxPos pos;
276         ssKey >> strType >> hashItem >> pos;
277         int nItemHeight;
278         ssValue >> nItemHeight;
279
280         // Read transaction
281         if (strType != "owner" || hashItem != hash160)
282             break;
283         if (nItemHeight >= nMinHeight)
284         {
285             vtx.resize(vtx.size()+1);
286             if (!vtx.back().ReadFromDisk(pos))
287             {
288                 pcursor->close();
289                 return false;
290             }
291         }
292     }
293
294     pcursor->close();
295     return true;
296 }
297
298 bool CTxDB::ReadDiskTx(uint256 hash, CTransaction& tx, CTxIndex& txindex)
299 {
300     assert(!fClient);
301     tx.SetNull();
302     if (!ReadTxIndex(hash, txindex))
303         return false;
304     return (tx.ReadFromDisk(txindex.pos));
305 }
306
307 bool CTxDB::ReadDiskTx(uint256 hash, CTransaction& tx)
308 {
309     CTxIndex txindex;
310     return ReadDiskTx(hash, tx, txindex);
311 }
312
313 bool CTxDB::ReadDiskTx(COutPoint outpoint, CTransaction& tx, CTxIndex& txindex)
314 {
315     return ReadDiskTx(outpoint.hash, tx, txindex);
316 }
317
318 bool CTxDB::ReadDiskTx(COutPoint outpoint, CTransaction& tx)
319 {
320     CTxIndex txindex;
321     return ReadDiskTx(outpoint.hash, tx, txindex);
322 }
323
324 bool CTxDB::WriteBlockIndex(const CDiskBlockIndex& blockindex)
325 {
326     return Write(make_pair(string("blockindex"), blockindex.GetBlockHash()), blockindex);
327 }
328
329 bool CTxDB::EraseBlockIndex(uint256 hash)
330 {
331     return Erase(make_pair(string("blockindex"), hash));
332 }
333
334 bool CTxDB::ReadHashBestChain(uint256& hashBestChain)
335 {
336     return Read(string("hashBestChain"), hashBestChain);
337 }
338
339 bool CTxDB::WriteHashBestChain(uint256 hashBestChain)
340 {
341     return Write(string("hashBestChain"), hashBestChain);
342 }
343
344 bool CTxDB::ReadBestInvalidWork(CBigNum& bnBestInvalidWork)
345 {
346     return Read(string("bnBestInvalidWork"), bnBestInvalidWork);
347 }
348
349 bool CTxDB::WriteBestInvalidWork(CBigNum bnBestInvalidWork)
350 {
351     return Write(string("bnBestInvalidWork"), bnBestInvalidWork);
352 }
353
354 CBlockIndex* InsertBlockIndex(uint256 hash)
355 {
356     if (hash == 0)
357         return NULL;
358
359     // Return existing
360     map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.find(hash);
361     if (mi != mapBlockIndex.end())
362         return (*mi).second;
363
364     // Create new
365     CBlockIndex* pindexNew = new CBlockIndex();
366     if (!pindexNew)
367         throw runtime_error("LoadBlockIndex() : new CBlockIndex failed");
368     mi = mapBlockIndex.insert(make_pair(hash, pindexNew)).first;
369     pindexNew->phashBlock = &((*mi).first);
370
371     return pindexNew;
372 }
373
374 bool CTxDB::LoadBlockIndex()
375 {
376     // Get database cursor
377     Dbc* pcursor = GetCursor();
378     if (!pcursor)
379         return false;
380
381     // Load mapBlockIndex
382     unsigned int fFlags = DB_SET_RANGE;
383     loop
384     {
385         // Read next record
386         CDataStream ssKey;
387         if (fFlags == DB_SET_RANGE)
388             ssKey << make_pair(string("blockindex"), uint256(0));
389         CDataStream ssValue;
390         int ret = ReadAtCursor(pcursor, ssKey, ssValue, fFlags);
391         fFlags = DB_NEXT;
392         if (ret == DB_NOTFOUND)
393             break;
394         else if (ret != 0)
395             return false;
396
397         // Unserialize
398         string strType;
399         ssKey >> strType;
400         if (strType == "blockindex")
401         {
402             CDiskBlockIndex diskindex;
403             ssValue >> diskindex;
404
405             // Construct block index object
406             CBlockIndex* pindexNew = InsertBlockIndex(diskindex.GetBlockHash());
407             pindexNew->pprev          = InsertBlockIndex(diskindex.hashPrev);
408             pindexNew->pnext          = InsertBlockIndex(diskindex.hashNext);
409             pindexNew->nFile          = diskindex.nFile;
410             pindexNew->nBlockPos      = diskindex.nBlockPos;
411             pindexNew->nHeight        = diskindex.nHeight;
412             pindexNew->nVersion       = diskindex.nVersion;
413             pindexNew->hashMerkleRoot = diskindex.hashMerkleRoot;
414             pindexNew->nTime          = diskindex.nTime;
415             pindexNew->nBits          = diskindex.nBits;
416             pindexNew->nNonce         = diskindex.nNonce;
417
418             // Watch for genesis block
419             if (pindexGenesisBlock == NULL && diskindex.GetBlockHash() == hashGenesisBlock)
420                 pindexGenesisBlock = pindexNew;
421
422             if (!pindexNew->CheckIndex())
423                 return error("LoadBlockIndex() : CheckIndex failed at %d", pindexNew->nHeight);
424         }
425         else
426         {
427             break;
428         }
429     }
430     pcursor->close();
431
432     // Calculate bnChainWork
433     vector<pair<int, CBlockIndex*> > vSortedByHeight;
434     vSortedByHeight.reserve(mapBlockIndex.size());
435     foreach(const PAIRTYPE(uint256, CBlockIndex*)& item, mapBlockIndex)
436     {
437         CBlockIndex* pindex = item.second;
438         vSortedByHeight.push_back(make_pair(pindex->nHeight, pindex));
439     }
440     sort(vSortedByHeight.begin(), vSortedByHeight.end());
441     foreach(const PAIRTYPE(int, CBlockIndex*)& item, vSortedByHeight)
442     {
443         CBlockIndex* pindex = item.second;
444         pindex->bnChainWork = (pindex->pprev ? pindex->pprev->bnChainWork : 0) + pindex->GetBlockWork();
445     }
446
447     // Load hashBestChain pointer to end of best chain
448     if (!ReadHashBestChain(hashBestChain))
449     {
450         if (pindexGenesisBlock == NULL)
451             return true;
452         return error("CTxDB::LoadBlockIndex() : hashBestChain not loaded");
453     }
454     if (!mapBlockIndex.count(hashBestChain))
455         return error("CTxDB::LoadBlockIndex() : hashBestChain not found in the block index");
456     pindexBest = mapBlockIndex[hashBestChain];
457     nBestHeight = pindexBest->nHeight;
458     bnBestChainWork = pindexBest->bnChainWork;
459     printf("LoadBlockIndex(): hashBestChain=%s  height=%d\n", hashBestChain.ToString().substr(0,20).c_str(), nBestHeight);
460
461     // Load bnBestInvalidWork, OK if it doesn't exist
462     ReadBestInvalidWork(bnBestInvalidWork);
463
464     // Verify blocks in the best chain
465     CBlockIndex* pindexFork = NULL;
466     for (CBlockIndex* pindex = pindexBest; pindex && pindex->pprev; pindex = pindex->pprev)
467     {
468         if (pindex->nHeight < nBestHeight-2500 && !mapArgs.count("-checkblocks"))
469             break;
470         CBlock block;
471         if (!block.ReadFromDisk(pindex))
472             return error("LoadBlockIndex() : block.ReadFromDisk failed");
473         if (!block.CheckBlock())
474         {
475             printf("LoadBlockIndex() : *** found bad block at %d, hash=%s\n", pindex->nHeight, pindex->GetBlockHash().ToString().c_str());
476             pindexFork = pindex->pprev;
477         }
478     }
479     if (pindexFork)
480     {
481         // Reorg back to the fork
482         printf("LoadBlockIndex() : *** moving best chain pointer back to block %d\n", pindexFork->nHeight);
483         CBlock block;
484         if (!block.ReadFromDisk(pindexFork))
485             return error("LoadBlockIndex() : block.ReadFromDisk failed");
486         CTxDB txdb;
487         block.SetBestChain(txdb, pindexFork);
488     }
489
490     return true;
491 }
492
493
494
495
496
497 //
498 // CAddrDB
499 //
500
501 bool CAddrDB::WriteAddress(const CAddress& addr)
502 {
503     return Write(make_pair(string("addr"), addr.GetKey()), addr);
504 }
505
506 bool CAddrDB::EraseAddress(const CAddress& addr)
507 {
508     return Erase(make_pair(string("addr"), addr.GetKey()));
509 }
510
511 bool CAddrDB::LoadAddresses()
512 {
513     CRITICAL_BLOCK(cs_mapAddresses)
514     {
515         // Load user provided addresses
516         CAutoFile filein = fopen((GetDataDir() + "/addr.txt").c_str(), "rt");
517         if (filein)
518         {
519             try
520             {
521                 char psz[1000];
522                 while (fgets(psz, sizeof(psz), filein))
523                 {
524                     CAddress addr(psz, NODE_NETWORK);
525                     addr.nTime = 0; // so it won't relay unless successfully connected
526                     if (addr.IsValid())
527                         AddAddress(addr);
528                 }
529             }
530             catch (...) { }
531         }
532
533         // Get cursor
534         Dbc* pcursor = GetCursor();
535         if (!pcursor)
536             return false;
537
538         loop
539         {
540             // Read next record
541             CDataStream ssKey;
542             CDataStream ssValue;
543             int ret = ReadAtCursor(pcursor, ssKey, ssValue);
544             if (ret == DB_NOTFOUND)
545                 break;
546             else if (ret != 0)
547                 return false;
548
549             // Unserialize
550             string strType;
551             ssKey >> strType;
552             if (strType == "addr")
553             {
554                 CAddress addr;
555                 ssValue >> addr;
556                 mapAddresses.insert(make_pair(addr.GetKey(), addr));
557             }
558         }
559         pcursor->close();
560
561         printf("Loaded %d addresses\n", mapAddresses.size());
562     }
563
564     return true;
565 }
566
567 bool LoadAddresses()
568 {
569     return CAddrDB("cr+").LoadAddresses();
570 }
571
572
573
574
575 //
576 // CWalletDB
577 //
578
579 static set<int64> setKeyPool;
580 static CCriticalSection cs_setKeyPool;
581
582 bool CWalletDB::ReadAccount(const string& strAccount, CAccount& account)
583 {
584     account.SetNull();
585     return Read(make_pair(string("acc"), strAccount), account);
586 }
587
588 bool CWalletDB::WriteAccount(const string& strAccount, const CAccount& account)
589 {
590     return Write(make_pair(string("acc"), strAccount), account);
591 }
592
593 bool CWalletDB::WriteAccountingEntry(const string& strAccount, const CAccountingEntry& acentry)
594 {
595     return Write(make_pair(string("acentry"), make_pair(strAccount, ++nAccountingEntryNumber)), acentry);
596 }
597
598 int64 CWalletDB::GetAccountCreditDebit(const string& strAccount)
599 {
600     int64 nCreditDebit = 0;
601
602     Dbc* pcursor = GetCursor();
603     if (!pcursor)
604         throw runtime_error("CWalletDB::GetAccountCreditDebit() : cannot create DB cursor");
605     unsigned int fFlags = DB_SET_RANGE;
606     loop
607     {
608         // Read next record
609         CDataStream ssKey;
610         if (fFlags == DB_SET_RANGE)
611             ssKey << make_pair(string("acentry"), make_pair(strAccount, uint64(0)));
612         CDataStream ssValue;
613         int ret = ReadAtCursor(pcursor, ssKey, ssValue, fFlags);
614         fFlags = DB_NEXT;
615         if (ret == DB_NOTFOUND)
616             break;
617         else if (ret != 0)
618         {
619             pcursor->close();
620             throw runtime_error("CWalletDB::GetAccountCreditDebit() : error scanning DB");
621         }
622
623         // Unserialize
624         string strType;
625         ssKey >> strType;
626         if (strType != "acentry")
627             break;
628         string strAccountName;
629         ssKey >> strAccountName;
630         if (strAccountName != strAccount)
631             break;
632
633         CAccountingEntry acentry;
634         ssValue >> acentry;
635         nCreditDebit += acentry.nCreditDebit;
636     }
637
638     pcursor->close();
639     return nCreditDebit;
640 }
641
642 bool CWalletDB::LoadWallet()
643 {
644     vchDefaultKey.clear();
645     int nFileVersion = 0;
646
647     // Modify defaults
648 #ifndef __WXMSW__
649     // Tray icon sometimes disappears on 9.10 karmic koala 64-bit, leaving no way to access the program
650     fMinimizeToTray = false;
651     fMinimizeOnClose = false;
652 #endif
653
654     //// todo: shouldn't we catch exceptions and try to recover and continue?
655     CRITICAL_BLOCK(cs_mapKeys)
656     CRITICAL_BLOCK(cs_mapWallet)
657     {
658         // Get cursor
659         Dbc* pcursor = GetCursor();
660         if (!pcursor)
661             return false;
662
663         loop
664         {
665             // Read next record
666             CDataStream ssKey;
667             CDataStream ssValue;
668             int ret = ReadAtCursor(pcursor, ssKey, ssValue);
669             if (ret == DB_NOTFOUND)
670                 break;
671             else if (ret != 0)
672                 return false;
673
674             // Unserialize
675             // Taking advantage of the fact that pair serialization
676             // is just the two items serialized one after the other
677             string strType;
678             ssKey >> strType;
679             if (strType == "name")
680             {
681                 string strAddress;
682                 ssKey >> strAddress;
683                 ssValue >> mapAddressBook[strAddress];
684             }
685             else if (strType == "tx")
686             {
687                 uint256 hash;
688                 ssKey >> hash;
689                 CWalletTx& wtx = mapWallet[hash];
690                 ssValue >> wtx;
691
692                 if (wtx.GetHash() != hash)
693                     printf("Error in wallet.dat, hash mismatch\n");
694
695                 //// debug print
696                 //printf("LoadWallet  %s\n", wtx.GetHash().ToString().c_str());
697                 //printf(" %12I64d  %s  %s  %s\n",
698                 //    wtx.vout[0].nValue,
699                 //    DateTimeStrFormat("%x %H:%M:%S", wtx.GetBlockTime()).c_str(),
700                 //    wtx.hashBlock.ToString().substr(0,20).c_str(),
701                 //    wtx.mapValue["message"].c_str());
702             }
703             else if (strType == "acentry")
704             {
705                 string strAccount;
706                 ssKey >> strAccount;
707                 uint64 nNumber;
708                 ssKey >> nNumber;
709                 if (nNumber > nAccountingEntryNumber)
710                     nAccountingEntryNumber = nNumber;
711             }
712             else if (strType == "key" || strType == "wkey")
713             {
714                 vector<unsigned char> vchPubKey;
715                 ssKey >> vchPubKey;
716                 CWalletKey wkey;
717                 if (strType == "key")
718                     ssValue >> wkey.vchPrivKey;
719                 else
720                     ssValue >> wkey;
721
722                 mapKeys[vchPubKey] = wkey.vchPrivKey;
723                 mapPubKeys[Hash160(vchPubKey)] = vchPubKey;
724             }
725             else if (strType == "defaultkey")
726             {
727                 ssValue >> vchDefaultKey;
728             }
729             else if (strType == "pool")
730             {
731                 int64 nIndex;
732                 ssKey >> nIndex;
733                 setKeyPool.insert(nIndex);
734             }
735             else if (strType == "version")
736             {
737                 ssValue >> nFileVersion;
738                 if (nFileVersion == 10300)
739                     nFileVersion = 300;
740             }
741             else if (strType == "setting")
742             {
743                 string strKey;
744                 ssKey >> strKey;
745
746                 // Menu state
747                 if (strKey == "fGenerateBitcoins")  ssValue >> fGenerateBitcoins;
748
749                 // Options
750                 if (strKey == "nTransactionFee")    ssValue >> nTransactionFee;
751                 if (strKey == "addrIncoming")       ssValue >> addrIncoming;
752                 if (strKey == "fLimitProcessors")   ssValue >> fLimitProcessors;
753                 if (strKey == "nLimitProcessors")   ssValue >> nLimitProcessors;
754                 if (strKey == "fMinimizeToTray")    ssValue >> fMinimizeToTray;
755                 if (strKey == "fMinimizeOnClose")   ssValue >> fMinimizeOnClose;
756                 if (strKey == "fUseProxy")          ssValue >> fUseProxy;
757                 if (strKey == "addrProxy")          ssValue >> addrProxy;
758
759             }
760         }
761         pcursor->close();
762     }
763
764     printf("nFileVersion = %d\n", nFileVersion);
765     printf("fGenerateBitcoins = %d\n", fGenerateBitcoins);
766     printf("nTransactionFee = %"PRI64d"\n", nTransactionFee);
767     printf("addrIncoming = %s\n", addrIncoming.ToString().c_str());
768     printf("fMinimizeToTray = %d\n", fMinimizeToTray);
769     printf("fMinimizeOnClose = %d\n", fMinimizeOnClose);
770     printf("fUseProxy = %d\n", fUseProxy);
771     printf("addrProxy = %s\n", addrProxy.ToString().c_str());
772
773
774     // Upgrade
775     if (nFileVersion < VERSION)
776     {
777         // Get rid of old debug.log file in current directory
778         if (nFileVersion <= 105 && !pszSetDataDir[0])
779             unlink("debug.log");
780
781         WriteVersion(VERSION);
782     }
783
784     return true;
785 }
786
787 bool LoadWallet(bool& fFirstRunRet)
788 {
789     fFirstRunRet = false;
790     if (!CWalletDB("cr+").LoadWallet())
791         return false;
792     fFirstRunRet = vchDefaultKey.empty();
793
794     if (mapKeys.count(vchDefaultKey))
795     {
796         // Set keyUser
797         keyUser.SetPubKey(vchDefaultKey);
798         keyUser.SetPrivKey(mapKeys[vchDefaultKey]);
799     }
800     else
801     {
802         // Create new keyUser and set as default key
803         RandAddSeedPerfmon();
804         keyUser.MakeNewKey();
805         if (!AddKey(keyUser))
806             return false;
807         if (!SetAddressBookName(PubKeyToAddress(keyUser.GetPubKey()), "Your Address"))
808             return false;
809         CWalletDB().WriteDefaultKey(keyUser.GetPubKey());
810     }
811
812     CreateThread(ThreadFlushWalletDB, NULL);
813     return true;
814 }
815
816 void ThreadFlushWalletDB(void* parg)
817 {
818     static bool fOneThread;
819     if (fOneThread)
820         return;
821     fOneThread = true;
822     if (mapArgs.count("-noflushwallet"))
823         return;
824
825     unsigned int nLastSeen = nWalletDBUpdated;
826     unsigned int nLastFlushed = nWalletDBUpdated;
827     int64 nLastWalletUpdate = GetTime();
828     while (!fShutdown)
829     {
830         Sleep(500);
831
832         if (nLastSeen != nWalletDBUpdated)
833         {
834             nLastSeen = nWalletDBUpdated;
835             nLastWalletUpdate = GetTime();
836         }
837
838         if (nLastFlushed != nWalletDBUpdated && GetTime() - nLastWalletUpdate >= 2)
839         {
840             TRY_CRITICAL_BLOCK(cs_db)
841             {
842                 // Don't do this if any databases are in use
843                 int nRefCount = 0;
844                 map<string, int>::iterator mi = mapFileUseCount.begin();
845                 while (mi != mapFileUseCount.end())
846                 {
847                     nRefCount += (*mi).second;
848                     mi++;
849                 }
850
851                 if (nRefCount == 0 && !fShutdown)
852                 {
853                     string strFile = "wallet.dat";
854                     map<string, int>::iterator mi = mapFileUseCount.find(strFile);
855                     if (mi != mapFileUseCount.end())
856                     {
857                         printf("%s ", DateTimeStrFormat("%x %H:%M:%S", GetTime()).c_str());
858                         printf("Flushing wallet.dat\n");
859                         nLastFlushed = nWalletDBUpdated;
860                         int64 nStart = GetTimeMillis();
861
862                         // Flush wallet.dat so it's self contained
863                         CloseDb(strFile);
864                         dbenv.txn_checkpoint(0, 0, 0);
865                         dbenv.lsn_reset(strFile.c_str(), 0);
866
867                         mapFileUseCount.erase(mi++);
868                         printf("Flushed wallet.dat %"PRI64d"ms\n", GetTimeMillis() - nStart);
869                     }
870                 }
871             }
872         }
873     }
874 }
875
876 void BackupWallet(const string& strDest)
877 {
878     while (!fShutdown)
879     {
880         CRITICAL_BLOCK(cs_db)
881         {
882             const string strFile = "wallet.dat";
883             if (!mapFileUseCount.count(strFile) || mapFileUseCount[strFile] == 0)
884             {
885                 // Flush log data to the dat file
886                 CloseDb(strFile);
887                 dbenv.txn_checkpoint(0, 0, 0);
888                 dbenv.lsn_reset(strFile.c_str(), 0);
889                 mapFileUseCount.erase(strFile);
890
891                 // Copy wallet.dat
892                 filesystem::path pathSrc(GetDataDir() + "/" + strFile);
893                 filesystem::path pathDest(strDest);
894                 if (filesystem::is_directory(pathDest))
895                     pathDest = pathDest / strFile;
896 #if BOOST_VERSION >= 104000
897                 filesystem::copy_file(pathSrc, pathDest, filesystem::copy_option::overwrite_if_exists);
898 #else
899                 filesystem::copy_file(pathSrc, pathDest);
900 #endif
901                 printf("copied wallet.dat to %s\n", pathDest.string().c_str());
902
903                 return;
904             }
905         }
906         Sleep(100);
907     }
908 }
909
910
911 void CWalletDB::ReserveKeyFromKeyPool(int64& nIndex, CKeyPool& keypool)
912 {
913     nIndex = -1;
914     keypool.vchPubKey.clear();
915     CRITICAL_BLOCK(cs_main)
916     CRITICAL_BLOCK(cs_mapWallet)
917     CRITICAL_BLOCK(cs_setKeyPool)
918     {
919         // Top up key pool
920         int64 nTargetSize = max(GetArg("-keypool", 100), (int64)0);
921         while (setKeyPool.size() < nTargetSize+1)
922         {
923             int64 nEnd = 1;
924             if (!setKeyPool.empty())
925                 nEnd = *(--setKeyPool.end()) + 1;
926             if (!Write(make_pair(string("pool"), nEnd), CKeyPool(GenerateNewKey())))
927                 throw runtime_error("ReserveKeyFromKeyPool() : writing generated key failed");
928             setKeyPool.insert(nEnd);
929             printf("keypool added key %"PRI64d", size=%d\n", nEnd, setKeyPool.size());
930         }
931
932         // Get the oldest key
933         assert(!setKeyPool.empty());
934         nIndex = *(setKeyPool.begin());
935         setKeyPool.erase(setKeyPool.begin());
936         if (!Read(make_pair(string("pool"), nIndex), keypool))
937             throw runtime_error("ReserveKeyFromKeyPool() : read failed");
938         if (!mapKeys.count(keypool.vchPubKey))
939             throw runtime_error("ReserveKeyFromKeyPool() : unknown key in key pool");
940         assert(!keypool.vchPubKey.empty());
941         printf("keypool reserve %"PRI64d"\n", nIndex);
942     }
943 }
944
945 void CWalletDB::KeepKey(int64 nIndex)
946 {
947     // Remove from key pool
948     CRITICAL_BLOCK(cs_main)
949     CRITICAL_BLOCK(cs_mapWallet)
950     {
951         Erase(make_pair(string("pool"), nIndex));
952     }
953     printf("keypool keep %"PRI64d"\n", nIndex);
954 }
955
956 void CWalletDB::ReturnKey(int64 nIndex)
957 {
958     // Return to key pool
959     CRITICAL_BLOCK(cs_setKeyPool)
960         setKeyPool.insert(nIndex);
961     printf("keypool return %"PRI64d"\n", nIndex);
962 }
963
964 vector<unsigned char> GetKeyFromKeyPool()
965 {
966     CWalletDB walletdb;
967     int64 nIndex = 0;
968     CKeyPool keypool;
969     walletdb.ReserveKeyFromKeyPool(nIndex, keypool);
970     walletdb.KeepKey(nIndex);
971     return keypool.vchPubKey;
972 }
973
974 int64 GetOldestKeyPoolTime()
975 {
976     CWalletDB walletdb;
977     int64 nIndex = 0;
978     CKeyPool keypool;
979     walletdb.ReserveKeyFromKeyPool(nIndex, keypool);
980     walletdb.ReturnKey(nIndex);
981     return keypool.nTime;
982 }