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.
8 #include <boost/filesystem/fstream.hpp>
11 using namespace boost;
13 void ThreadFlushWalletDB(void* parg);
16 unsigned int nWalletDBUpdated;
17 uint64 nAccountingEntryNumber = 0;
25 static CCriticalSection cs_db;
26 static bool fDbEnvInit = false;
28 static map<string, int> mapFileUseCount;
29 static map<string, Db*> mapDb;
49 CDB::CDB(const char* pszFile, const char* pszMode) : pdb(NULL)
55 fReadOnly = (!strchr(pszMode, '+') && !strchr(pszMode, 'w'));
56 bool fCreate = strchr(pszMode, 'c');
57 unsigned int nFlags = DB_THREAD;
67 string strDataDir = GetDataDir();
68 string strLogDir = strDataDir + "/database";
69 filesystem::create_directory(strLogDir.c_str());
70 string strErrorFile = strDataDir + "/db.log";
71 printf("dbenv.open strLogDir=%s strErrorFile=%s\n", strLogDir.c_str(), strErrorFile.c_str());
73 dbenv.set_lg_dir(strLogDir.c_str());
74 dbenv.set_lg_max(10000000);
75 dbenv.set_lk_max_locks(10000);
76 dbenv.set_lk_max_objects(10000);
77 dbenv.set_errfile(fopen(strErrorFile.c_str(), "a")); /// debug
78 dbenv.set_flags(DB_AUTO_COMMIT, 1);
79 ret = dbenv.open(strDataDir.c_str(),
89 throw runtime_error(strprintf("CDB() : error %d opening database environment", ret));
94 ++mapFileUseCount[strFile];
98 pdb = new Db(&dbenv, 0);
100 ret = pdb->open(NULL, // Txn pointer
102 "main", // Logical db name
103 DB_BTREE, // Database type
111 CRITICAL_BLOCK(cs_db)
112 --mapFileUseCount[strFile];
114 throw runtime_error(strprintf("CDB() : can't open database file %s, error %d", pszFile, ret));
117 if (fCreate && !Exists(string("version")))
119 bool fTmp = fReadOnly;
121 WriteVersion(VERSION);
125 mapDb[strFile] = pdb;
135 vTxn.front()->abort();
139 // Flush database activity from memory pool to disk log
140 unsigned int nMinutes = 0;
143 if (strFile == "addr.dat")
145 if (strFile == "blkindex.dat" && IsInitialBlockDownload() && nBestHeight % 500 != 0)
147 dbenv.txn_checkpoint(0, nMinutes, 0);
149 CRITICAL_BLOCK(cs_db)
150 --mapFileUseCount[strFile];
153 void CloseDb(const string& strFile)
155 CRITICAL_BLOCK(cs_db)
157 if (mapDb[strFile] != NULL)
159 // Close the database handle
160 Db* pdb = mapDb[strFile];
163 mapDb[strFile] = NULL;
168 void DBFlush(bool fShutdown)
170 // Flush log data to the actual data file
171 // on all files that are not in use
172 printf("DBFlush(%s)%s\n", fShutdown ? "true" : "false", fDbEnvInit ? "" : " db not started");
175 CRITICAL_BLOCK(cs_db)
177 map<string, int>::iterator mi = mapFileUseCount.begin();
178 while (mi != mapFileUseCount.end())
180 string strFile = (*mi).first;
181 int nRefCount = (*mi).second;
182 printf("%s refcount=%d\n", strFile.c_str(), nRefCount);
185 // Move log data to the dat file
187 dbenv.txn_checkpoint(0, 0, 0);
188 printf("%s flush\n", strFile.c_str());
189 dbenv.lsn_reset(strFile.c_str(), 0);
190 mapFileUseCount.erase(mi++);
198 if (mapFileUseCount.empty())
199 dbenv.log_archive(&listp, DB_ARCH_REMOVE);
215 bool CTxDB::ReadTxIndex(uint256 hash, CTxIndex& txindex)
219 return Read(make_pair(string("tx"), hash), txindex);
222 bool CTxDB::UpdateTxIndex(uint256 hash, const CTxIndex& txindex)
225 return Write(make_pair(string("tx"), hash), txindex);
228 bool CTxDB::AddTxIndex(const CTransaction& tx, const CDiskTxPos& pos, int nHeight)
233 uint256 hash = tx.GetHash();
234 CTxIndex txindex(pos, tx.vout.size());
235 return Write(make_pair(string("tx"), hash), txindex);
238 bool CTxDB::EraseTxIndex(const CTransaction& tx)
241 uint256 hash = tx.GetHash();
243 return Erase(make_pair(string("tx"), hash));
246 bool CTxDB::ContainsTx(uint256 hash)
249 return Exists(make_pair(string("tx"), hash));
252 bool CTxDB::ReadOwnerTxes(uint160 hash160, int nMinHeight, vector<CTransaction>& vtx)
258 Dbc* pcursor = GetCursor();
262 unsigned int fFlags = DB_SET_RANGE;
267 if (fFlags == DB_SET_RANGE)
268 ssKey << string("owner") << hash160 << CDiskTxPos(0, 0, 0);
270 int ret = ReadAtCursor(pcursor, ssKey, ssValue, fFlags);
272 if (ret == DB_NOTFOUND)
284 ssKey >> strType >> hashItem >> pos;
286 ssValue >> nItemHeight;
289 if (strType != "owner" || hashItem != hash160)
291 if (nItemHeight >= nMinHeight)
293 vtx.resize(vtx.size()+1);
294 if (!vtx.back().ReadFromDisk(pos))
306 bool CTxDB::ReadDiskTx(uint256 hash, CTransaction& tx, CTxIndex& txindex)
310 if (!ReadTxIndex(hash, txindex))
312 return (tx.ReadFromDisk(txindex.pos));
315 bool CTxDB::ReadDiskTx(uint256 hash, CTransaction& tx)
318 return ReadDiskTx(hash, tx, txindex);
321 bool CTxDB::ReadDiskTx(COutPoint outpoint, CTransaction& tx, CTxIndex& txindex)
323 return ReadDiskTx(outpoint.hash, tx, txindex);
326 bool CTxDB::ReadDiskTx(COutPoint outpoint, CTransaction& tx)
329 return ReadDiskTx(outpoint.hash, tx, txindex);
332 bool CTxDB::WriteBlockIndex(const CDiskBlockIndex& blockindex)
334 return Write(make_pair(string("blockindex"), blockindex.GetBlockHash()), blockindex);
337 bool CTxDB::EraseBlockIndex(uint256 hash)
339 return Erase(make_pair(string("blockindex"), hash));
342 bool CTxDB::ReadHashBestChain(uint256& hashBestChain)
344 return Read(string("hashBestChain"), hashBestChain);
347 bool CTxDB::WriteHashBestChain(uint256 hashBestChain)
349 return Write(string("hashBestChain"), hashBestChain);
352 bool CTxDB::ReadBestInvalidWork(CBigNum& bnBestInvalidWork)
354 return Read(string("bnBestInvalidWork"), bnBestInvalidWork);
357 bool CTxDB::WriteBestInvalidWork(CBigNum bnBestInvalidWork)
359 return Write(string("bnBestInvalidWork"), bnBestInvalidWork);
362 CBlockIndex* InsertBlockIndex(uint256 hash)
368 map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.find(hash);
369 if (mi != mapBlockIndex.end())
373 CBlockIndex* pindexNew = new CBlockIndex();
375 throw runtime_error("LoadBlockIndex() : new CBlockIndex failed");
376 mi = mapBlockIndex.insert(make_pair(hash, pindexNew)).first;
377 pindexNew->phashBlock = &((*mi).first);
382 bool CTxDB::LoadBlockIndex()
384 // Get database cursor
385 Dbc* pcursor = GetCursor();
389 // Load mapBlockIndex
390 unsigned int fFlags = DB_SET_RANGE;
395 if (fFlags == DB_SET_RANGE)
396 ssKey << make_pair(string("blockindex"), uint256(0));
398 int ret = ReadAtCursor(pcursor, ssKey, ssValue, fFlags);
400 if (ret == DB_NOTFOUND)
408 if (strType == "blockindex")
410 CDiskBlockIndex diskindex;
411 ssValue >> diskindex;
413 // Construct block index object
414 CBlockIndex* pindexNew = InsertBlockIndex(diskindex.GetBlockHash());
415 pindexNew->pprev = InsertBlockIndex(diskindex.hashPrev);
416 pindexNew->pnext = InsertBlockIndex(diskindex.hashNext);
417 pindexNew->nFile = diskindex.nFile;
418 pindexNew->nBlockPos = diskindex.nBlockPos;
419 pindexNew->nHeight = diskindex.nHeight;
420 pindexNew->nVersion = diskindex.nVersion;
421 pindexNew->hashMerkleRoot = diskindex.hashMerkleRoot;
422 pindexNew->nTime = diskindex.nTime;
423 pindexNew->nBits = diskindex.nBits;
424 pindexNew->nNonce = diskindex.nNonce;
426 // Watch for genesis block
427 if (pindexGenesisBlock == NULL && diskindex.GetBlockHash() == hashGenesisBlock)
428 pindexGenesisBlock = pindexNew;
430 if (!pindexNew->CheckIndex())
431 return error("LoadBlockIndex() : CheckIndex failed at %d", pindexNew->nHeight);
440 // Calculate bnChainWork
441 vector<pair<int, CBlockIndex*> > vSortedByHeight;
442 vSortedByHeight.reserve(mapBlockIndex.size());
443 BOOST_FOREACH(const PAIRTYPE(uint256, CBlockIndex*)& item, mapBlockIndex)
445 CBlockIndex* pindex = item.second;
446 vSortedByHeight.push_back(make_pair(pindex->nHeight, pindex));
448 sort(vSortedByHeight.begin(), vSortedByHeight.end());
449 BOOST_FOREACH(const PAIRTYPE(int, CBlockIndex*)& item, vSortedByHeight)
451 CBlockIndex* pindex = item.second;
452 pindex->bnChainWork = (pindex->pprev ? pindex->pprev->bnChainWork : 0) + pindex->GetBlockWork();
455 // Load hashBestChain pointer to end of best chain
456 if (!ReadHashBestChain(hashBestChain))
458 if (pindexGenesisBlock == NULL)
460 return error("CTxDB::LoadBlockIndex() : hashBestChain not loaded");
462 if (!mapBlockIndex.count(hashBestChain))
463 return error("CTxDB::LoadBlockIndex() : hashBestChain not found in the block index");
464 pindexBest = mapBlockIndex[hashBestChain];
465 nBestHeight = pindexBest->nHeight;
466 bnBestChainWork = pindexBest->bnChainWork;
467 printf("LoadBlockIndex(): hashBestChain=%s height=%d\n", hashBestChain.ToString().substr(0,20).c_str(), nBestHeight);
469 // Load bnBestInvalidWork, OK if it doesn't exist
470 ReadBestInvalidWork(bnBestInvalidWork);
472 // Verify blocks in the best chain
473 CBlockIndex* pindexFork = NULL;
474 for (CBlockIndex* pindex = pindexBest; pindex && pindex->pprev; pindex = pindex->pprev)
476 if (pindex->nHeight < nBestHeight-2500 && !mapArgs.count("-checkblocks"))
479 if (!block.ReadFromDisk(pindex))
480 return error("LoadBlockIndex() : block.ReadFromDisk failed");
481 if (!block.CheckBlock())
483 printf("LoadBlockIndex() : *** found bad block at %d, hash=%s\n", pindex->nHeight, pindex->GetBlockHash().ToString().c_str());
484 pindexFork = pindex->pprev;
489 // Reorg back to the fork
490 printf("LoadBlockIndex() : *** moving best chain pointer back to block %d\n", pindexFork->nHeight);
492 if (!block.ReadFromDisk(pindexFork))
493 return error("LoadBlockIndex() : block.ReadFromDisk failed");
495 block.SetBestChain(txdb, pindexFork);
509 bool CAddrDB::WriteAddress(const CAddress& addr)
511 return Write(make_pair(string("addr"), addr.GetKey()), addr);
514 bool CAddrDB::EraseAddress(const CAddress& addr)
516 return Erase(make_pair(string("addr"), addr.GetKey()));
519 bool CAddrDB::LoadAddresses()
521 CRITICAL_BLOCK(cs_mapAddresses)
523 // Load user provided addresses
524 CAutoFile filein = fopen((GetDataDir() + "/addr.txt").c_str(), "rt");
530 while (fgets(psz, sizeof(psz), filein))
532 CAddress addr(psz, NODE_NETWORK);
533 addr.nTime = 0; // so it won't relay unless successfully connected
542 Dbc* pcursor = GetCursor();
551 int ret = ReadAtCursor(pcursor, ssKey, ssValue);
552 if (ret == DB_NOTFOUND)
560 if (strType == "addr")
564 mapAddresses.insert(make_pair(addr.GetKey(), addr));
569 printf("Loaded %d addresses\n", mapAddresses.size());
577 return CAddrDB("cr+").LoadAddresses();
587 static set<int64> setKeyPool;
588 static CCriticalSection cs_setKeyPool;
590 bool CWalletDB::ReadAccount(const string& strAccount, CAccount& account)
593 return Read(make_pair(string("acc"), strAccount), account);
596 bool CWalletDB::WriteAccount(const string& strAccount, const CAccount& account)
598 return Write(make_pair(string("acc"), strAccount), account);
601 bool CWalletDB::WriteAccountingEntry(const CAccountingEntry& acentry)
603 return Write(make_tuple(string("acentry"), acentry.strAccount, ++nAccountingEntryNumber), acentry);
606 int64 CWalletDB::GetAccountCreditDebit(const string& strAccount)
608 list<CAccountingEntry> entries;
609 ListAccountCreditDebit(strAccount, entries);
611 int64 nCreditDebit = 0;
612 BOOST_FOREACH (const CAccountingEntry& entry, entries)
613 nCreditDebit += entry.nCreditDebit;
618 void CWalletDB::ListAccountCreditDebit(const string& strAccount, list<CAccountingEntry>& entries)
620 int64 nCreditDebit = 0;
622 bool fAllAccounts = (strAccount == "*");
624 Dbc* pcursor = GetCursor();
626 throw runtime_error("CWalletDB::ListAccountCreditDebit() : cannot create DB cursor");
627 unsigned int fFlags = DB_SET_RANGE;
632 if (fFlags == DB_SET_RANGE)
633 ssKey << make_tuple(string("acentry"), (fAllAccounts? string("") : strAccount), uint64(0));
635 int ret = ReadAtCursor(pcursor, ssKey, ssValue, fFlags);
637 if (ret == DB_NOTFOUND)
642 throw runtime_error("CWalletDB::ListAccountCreditDebit() : error scanning DB");
648 if (strType != "acentry")
650 CAccountingEntry acentry;
651 ssKey >> acentry.strAccount;
652 if (!fAllAccounts && acentry.strAccount != strAccount)
656 entries.push_back(acentry);
663 bool CWalletDB::LoadWallet()
665 vchDefaultKey.clear();
666 int nFileVersion = 0;
667 vector<uint256> vWalletUpgrade;
671 // Tray icon sometimes disappears on 9.10 karmic koala 64-bit, leaving no way to access the program
672 fMinimizeToTray = false;
673 fMinimizeOnClose = false;
676 //// todo: shouldn't we catch exceptions and try to recover and continue?
677 CRITICAL_BLOCK(cs_mapWallet)
678 CRITICAL_BLOCK(cs_mapKeys)
681 Dbc* pcursor = GetCursor();
690 int ret = ReadAtCursor(pcursor, ssKey, ssValue);
691 if (ret == DB_NOTFOUND)
697 // Taking advantage of the fact that pair serialization
698 // is just the two items serialized one after the other
701 if (strType == "name")
705 ssValue >> mapAddressBook[strAddress];
707 else if (strType == "tx")
711 CWalletTx& wtx = mapWallet[hash];
714 if (wtx.GetHash() != hash)
715 printf("Error in wallet.dat, hash mismatch\n");
717 // Undo serialize changes in 31600
718 if (31404 <= wtx.fTimeReceivedIsTxTime && wtx.fTimeReceivedIsTxTime <= 31703)
720 if (!ssValue.empty())
724 ssValue >> fTmp >> fUnused >> wtx.strFromAccount;
725 printf("LoadWallet() upgrading tx ver=%d %d '%s' %s\n", wtx.fTimeReceivedIsTxTime, fTmp, wtx.strFromAccount.c_str(), hash.ToString().c_str());
726 wtx.fTimeReceivedIsTxTime = fTmp;
730 printf("LoadWallet() repairing tx ver=%d %s\n", wtx.fTimeReceivedIsTxTime, hash.ToString().c_str());
731 wtx.fTimeReceivedIsTxTime = 0;
733 vWalletUpgrade.push_back(hash);
737 //printf("LoadWallet %s\n", wtx.GetHash().ToString().c_str());
738 //printf(" %12I64d %s %s %s\n",
739 // wtx.vout[0].nValue,
740 // DateTimeStrFormat("%x %H:%M:%S", wtx.GetBlockTime()).c_str(),
741 // wtx.hashBlock.ToString().substr(0,20).c_str(),
742 // wtx.mapValue["message"].c_str());
744 else if (strType == "acentry")
750 if (nNumber > nAccountingEntryNumber)
751 nAccountingEntryNumber = nNumber;
753 else if (strType == "key" || strType == "wkey")
755 vector<unsigned char> vchPubKey;
758 if (strType == "key")
759 ssValue >> wkey.vchPrivKey;
763 mapKeys[vchPubKey] = wkey.vchPrivKey;
764 mapPubKeys[Hash160(vchPubKey)] = vchPubKey;
766 else if (strType == "defaultkey")
768 ssValue >> vchDefaultKey;
770 else if (strType == "pool")
774 setKeyPool.insert(nIndex);
776 else if (strType == "version")
778 ssValue >> nFileVersion;
779 if (nFileVersion == 10300)
782 else if (strType == "setting")
789 if (strKey == "fGenerateBitcoins") ssValue >> fGenerateBitcoins;
791 if (strKey == "nTransactionFee") ssValue >> nTransactionFee;
792 if (strKey == "addrIncoming") ssValue >> addrIncoming;
793 if (strKey == "fLimitProcessors") ssValue >> fLimitProcessors;
794 if (strKey == "nLimitProcessors") ssValue >> nLimitProcessors;
795 if (strKey == "fMinimizeToTray") ssValue >> fMinimizeToTray;
796 if (strKey == "fMinimizeOnClose") ssValue >> fMinimizeOnClose;
797 if (strKey == "fUseProxy") ssValue >> fUseProxy;
798 if (strKey == "addrProxy") ssValue >> addrProxy;
799 if (fHaveUPnP && strKey == "fUseUPnP") ssValue >> fUseUPnP;
805 BOOST_FOREACH(uint256 hash, vWalletUpgrade)
806 WriteTx(hash, mapWallet[hash]);
808 printf("nFileVersion = %d\n", nFileVersion);
809 printf("fGenerateBitcoins = %d\n", fGenerateBitcoins);
810 printf("nTransactionFee = %"PRI64d"\n", nTransactionFee);
811 printf("addrIncoming = %s\n", addrIncoming.ToString().c_str());
812 printf("fMinimizeToTray = %d\n", fMinimizeToTray);
813 printf("fMinimizeOnClose = %d\n", fMinimizeOnClose);
814 printf("fUseProxy = %d\n", fUseProxy);
815 printf("addrProxy = %s\n", addrProxy.ToString().c_str());
817 printf("fUseUPnP = %d\n", fUseUPnP);
821 if (nFileVersion < VERSION)
823 // Get rid of old debug.log file in current directory
824 if (nFileVersion <= 105 && !pszSetDataDir[0])
827 WriteVersion(VERSION);
834 bool LoadWallet(bool& fFirstRunRet)
836 fFirstRunRet = false;
837 if (!CWalletDB("cr+").LoadWallet())
839 fFirstRunRet = vchDefaultKey.empty();
841 if (mapKeys.count(vchDefaultKey))
844 keyUser.SetPubKey(vchDefaultKey);
845 keyUser.SetPrivKey(mapKeys[vchDefaultKey]);
849 // Create new keyUser and set as default key
850 RandAddSeedPerfmon();
853 vchDefaultKey = GetKeyFromKeyPool();
854 walletdb.WriteDefaultKey(vchDefaultKey);
855 walletdb.WriteName(PubKeyToAddress(vchDefaultKey), "");
858 CreateThread(ThreadFlushWalletDB, NULL);
862 void ThreadFlushWalletDB(void* parg)
864 static bool fOneThread;
868 if (mapArgs.count("-noflushwallet"))
871 unsigned int nLastSeen = nWalletDBUpdated;
872 unsigned int nLastFlushed = nWalletDBUpdated;
873 int64 nLastWalletUpdate = GetTime();
878 if (nLastSeen != nWalletDBUpdated)
880 nLastSeen = nWalletDBUpdated;
881 nLastWalletUpdate = GetTime();
884 if (nLastFlushed != nWalletDBUpdated && GetTime() - nLastWalletUpdate >= 2)
886 TRY_CRITICAL_BLOCK(cs_db)
888 // Don't do this if any databases are in use
890 map<string, int>::iterator mi = mapFileUseCount.begin();
891 while (mi != mapFileUseCount.end())
893 nRefCount += (*mi).second;
897 if (nRefCount == 0 && !fShutdown)
899 string strFile = "wallet.dat";
900 map<string, int>::iterator mi = mapFileUseCount.find(strFile);
901 if (mi != mapFileUseCount.end())
903 printf("%s ", DateTimeStrFormat("%x %H:%M:%S", GetTime()).c_str());
904 printf("Flushing wallet.dat\n");
905 nLastFlushed = nWalletDBUpdated;
906 int64 nStart = GetTimeMillis();
908 // Flush wallet.dat so it's self contained
910 dbenv.txn_checkpoint(0, 0, 0);
911 dbenv.lsn_reset(strFile.c_str(), 0);
913 mapFileUseCount.erase(mi++);
914 printf("Flushed wallet.dat %"PRI64d"ms\n", GetTimeMillis() - nStart);
922 void BackupWallet(const string& strDest)
926 CRITICAL_BLOCK(cs_db)
928 const string strFile = "wallet.dat";
929 if (!mapFileUseCount.count(strFile) || mapFileUseCount[strFile] == 0)
931 // Flush log data to the dat file
933 dbenv.txn_checkpoint(0, 0, 0);
934 dbenv.lsn_reset(strFile.c_str(), 0);
935 mapFileUseCount.erase(strFile);
938 filesystem::path pathSrc(GetDataDir() + "/" + strFile);
939 filesystem::path pathDest(strDest);
940 if (filesystem::is_directory(pathDest))
941 pathDest = pathDest / strFile;
942 #if BOOST_VERSION >= 104000
943 filesystem::copy_file(pathSrc, pathDest, filesystem::copy_option::overwrite_if_exists);
945 filesystem::copy_file(pathSrc, pathDest);
947 printf("copied wallet.dat to %s\n", pathDest.string().c_str());
957 void CWalletDB::ReserveKeyFromKeyPool(int64& nIndex, CKeyPool& keypool)
960 keypool.vchPubKey.clear();
961 CRITICAL_BLOCK(cs_main)
962 CRITICAL_BLOCK(cs_mapWallet)
963 CRITICAL_BLOCK(cs_setKeyPool)
966 int64 nTargetSize = max(GetArg("-keypool", 100), (int64)0);
967 while (setKeyPool.size() < nTargetSize+1)
970 if (!setKeyPool.empty())
971 nEnd = *(--setKeyPool.end()) + 1;
972 if (!Write(make_pair(string("pool"), nEnd), CKeyPool(GenerateNewKey())))
973 throw runtime_error("ReserveKeyFromKeyPool() : writing generated key failed");
974 setKeyPool.insert(nEnd);
975 printf("keypool added key %"PRI64d", size=%d\n", nEnd, setKeyPool.size());
978 // Get the oldest key
979 assert(!setKeyPool.empty());
980 nIndex = *(setKeyPool.begin());
981 setKeyPool.erase(setKeyPool.begin());
982 if (!Read(make_pair(string("pool"), nIndex), keypool))
983 throw runtime_error("ReserveKeyFromKeyPool() : read failed");
984 if (!mapKeys.count(keypool.vchPubKey))
985 throw runtime_error("ReserveKeyFromKeyPool() : unknown key in key pool");
986 assert(!keypool.vchPubKey.empty());
987 printf("keypool reserve %"PRI64d"\n", nIndex);
991 void CWalletDB::KeepKey(int64 nIndex)
993 // Remove from key pool
994 CRITICAL_BLOCK(cs_main)
995 CRITICAL_BLOCK(cs_mapWallet)
997 Erase(make_pair(string("pool"), nIndex));
999 printf("keypool keep %"PRI64d"\n", nIndex);
1002 void CWalletDB::ReturnKey(int64 nIndex)
1004 // Return to key pool
1005 CRITICAL_BLOCK(cs_setKeyPool)
1006 setKeyPool.insert(nIndex);
1007 printf("keypool return %"PRI64d"\n", nIndex);
1010 vector<unsigned char> GetKeyFromKeyPool()
1015 walletdb.ReserveKeyFromKeyPool(nIndex, keypool);
1016 walletdb.KeepKey(nIndex);
1017 return keypool.vchPubKey;
1020 int64 GetOldestKeyPoolTime()
1025 walletdb.ReserveKeyFromKeyPool(nIndex, keypool);
1026 walletdb.ReturnKey(nIndex);
1027 return keypool.nTime;