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/operations.hpp>
9 #include <boost/filesystem/fstream.hpp>
12 using namespace boost;
14 void ThreadFlushWalletDB(void* parg);
17 unsigned int nWalletDBUpdated;
18 uint64 nAccountingEntryNumber = 0;
26 static CCriticalSection cs_db;
27 static bool fDbEnvInit = false;
29 static map<string, int> mapFileUseCount;
30 static map<string, Db*> mapDb;
50 CDB::CDB(const char* pszFile, const char* pszMode) : pdb(NULL)
56 fReadOnly = (!strchr(pszMode, '+') && !strchr(pszMode, 'w'));
57 bool fCreate = strchr(pszMode, 'c');
58 unsigned int nFlags = DB_THREAD;
68 string strDataDir = GetDataDir();
69 string strLogDir = strDataDir + "/database";
70 filesystem::create_directory(strLogDir.c_str());
71 string strErrorFile = strDataDir + "/db.log";
72 printf("dbenv.open strLogDir=%s strErrorFile=%s\n", strLogDir.c_str(), strErrorFile.c_str());
74 dbenv.set_lg_dir(strLogDir.c_str());
75 dbenv.set_lg_max(10000000);
76 dbenv.set_lk_max_locks(10000);
77 dbenv.set_lk_max_objects(10000);
78 dbenv.set_errfile(fopen(strErrorFile.c_str(), "a")); /// debug
79 dbenv.set_flags(DB_AUTO_COMMIT, 1);
80 ret = dbenv.open(strDataDir.c_str(),
90 throw runtime_error(strprintf("CDB() : error %d opening database environment", ret));
95 ++mapFileUseCount[strFile];
99 pdb = new Db(&dbenv, 0);
101 ret = pdb->open(NULL, // Txn pointer
103 "main", // Logical db name
104 DB_BTREE, // Database type
112 CRITICAL_BLOCK(cs_db)
113 --mapFileUseCount[strFile];
115 throw runtime_error(strprintf("CDB() : can't open database file %s, error %d", pszFile, ret));
118 if (fCreate && !Exists(string("version")))
120 bool fTmp = fReadOnly;
122 WriteVersion(VERSION);
126 mapDb[strFile] = pdb;
136 vTxn.front()->abort();
140 // Flush database activity from memory pool to disk log
141 unsigned int nMinutes = 0;
144 if (strFile == "addr.dat")
146 if (strFile == "blkindex.dat" && IsInitialBlockDownload() && nBestHeight % 500 != 0)
148 dbenv.txn_checkpoint(0, nMinutes, 0);
150 CRITICAL_BLOCK(cs_db)
151 --mapFileUseCount[strFile];
154 void CloseDb(const string& strFile)
156 CRITICAL_BLOCK(cs_db)
158 if (mapDb[strFile] != NULL)
160 // Close the database handle
161 Db* pdb = mapDb[strFile];
164 mapDb[strFile] = NULL;
169 void DBFlush(bool fShutdown)
171 // Flush log data to the actual data file
172 // on all files that are not in use
173 printf("DBFlush(%s)%s\n", fShutdown ? "true" : "false", fDbEnvInit ? "" : " db not started");
176 CRITICAL_BLOCK(cs_db)
178 map<string, int>::iterator mi = mapFileUseCount.begin();
179 while (mi != mapFileUseCount.end())
181 string strFile = (*mi).first;
182 int nRefCount = (*mi).second;
183 printf("%s refcount=%d\n", strFile.c_str(), nRefCount);
186 // Move log data to the dat file
188 dbenv.txn_checkpoint(0, 0, 0);
189 printf("%s flush\n", strFile.c_str());
190 dbenv.lsn_reset(strFile.c_str(), 0);
191 mapFileUseCount.erase(mi++);
199 if (mapFileUseCount.empty())
200 dbenv.log_archive(&listp, DB_ARCH_REMOVE);
216 bool CTxDB::ReadTxIndex(uint256 hash, CTxIndex& txindex)
220 return Read(make_pair(string("tx"), hash), txindex);
223 bool CTxDB::UpdateTxIndex(uint256 hash, const CTxIndex& txindex)
226 return Write(make_pair(string("tx"), hash), txindex);
229 bool CTxDB::AddTxIndex(const CTransaction& tx, const CDiskTxPos& pos, int nHeight)
234 uint256 hash = tx.GetHash();
235 CTxIndex txindex(pos, tx.vout.size());
236 return Write(make_pair(string("tx"), hash), txindex);
239 bool CTxDB::EraseTxIndex(const CTransaction& tx)
242 uint256 hash = tx.GetHash();
244 return Erase(make_pair(string("tx"), hash));
247 bool CTxDB::ContainsTx(uint256 hash)
250 return Exists(make_pair(string("tx"), hash));
253 bool CTxDB::ReadOwnerTxes(uint160 hash160, int nMinHeight, vector<CTransaction>& vtx)
259 Dbc* pcursor = GetCursor();
263 unsigned int fFlags = DB_SET_RANGE;
268 if (fFlags == DB_SET_RANGE)
269 ssKey << string("owner") << hash160 << CDiskTxPos(0, 0, 0);
271 int ret = ReadAtCursor(pcursor, ssKey, ssValue, fFlags);
273 if (ret == DB_NOTFOUND)
285 ssKey >> strType >> hashItem >> pos;
287 ssValue >> nItemHeight;
290 if (strType != "owner" || hashItem != hash160)
292 if (nItemHeight >= nMinHeight)
294 vtx.resize(vtx.size()+1);
295 if (!vtx.back().ReadFromDisk(pos))
307 bool CTxDB::ReadDiskTx(uint256 hash, CTransaction& tx, CTxIndex& txindex)
311 if (!ReadTxIndex(hash, txindex))
313 return (tx.ReadFromDisk(txindex.pos));
316 bool CTxDB::ReadDiskTx(uint256 hash, CTransaction& tx)
319 return ReadDiskTx(hash, tx, txindex);
322 bool CTxDB::ReadDiskTx(COutPoint outpoint, CTransaction& tx, CTxIndex& txindex)
324 return ReadDiskTx(outpoint.hash, tx, txindex);
327 bool CTxDB::ReadDiskTx(COutPoint outpoint, CTransaction& tx)
330 return ReadDiskTx(outpoint.hash, tx, txindex);
333 bool CTxDB::WriteBlockIndex(const CDiskBlockIndex& blockindex)
335 return Write(make_pair(string("blockindex"), blockindex.GetBlockHash()), blockindex);
338 bool CTxDB::EraseBlockIndex(uint256 hash)
340 return Erase(make_pair(string("blockindex"), hash));
343 bool CTxDB::ReadHashBestChain(uint256& hashBestChain)
345 return Read(string("hashBestChain"), hashBestChain);
348 bool CTxDB::WriteHashBestChain(uint256 hashBestChain)
350 return Write(string("hashBestChain"), hashBestChain);
353 bool CTxDB::ReadBestInvalidWork(CBigNum& bnBestInvalidWork)
355 return Read(string("bnBestInvalidWork"), bnBestInvalidWork);
358 bool CTxDB::WriteBestInvalidWork(CBigNum bnBestInvalidWork)
360 return Write(string("bnBestInvalidWork"), bnBestInvalidWork);
363 CBlockIndex* InsertBlockIndex(uint256 hash)
369 map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.find(hash);
370 if (mi != mapBlockIndex.end())
374 CBlockIndex* pindexNew = new CBlockIndex();
376 throw runtime_error("LoadBlockIndex() : new CBlockIndex failed");
377 mi = mapBlockIndex.insert(make_pair(hash, pindexNew)).first;
378 pindexNew->phashBlock = &((*mi).first);
383 bool CTxDB::LoadBlockIndex()
385 // Get database cursor
386 Dbc* pcursor = GetCursor();
390 // Load mapBlockIndex
391 unsigned int fFlags = DB_SET_RANGE;
396 if (fFlags == DB_SET_RANGE)
397 ssKey << make_pair(string("blockindex"), uint256(0));
399 int ret = ReadAtCursor(pcursor, ssKey, ssValue, fFlags);
401 if (ret == DB_NOTFOUND)
409 if (strType == "blockindex")
411 CDiskBlockIndex diskindex;
412 ssValue >> diskindex;
414 // Construct block index object
415 CBlockIndex* pindexNew = InsertBlockIndex(diskindex.GetBlockHash());
416 pindexNew->pprev = InsertBlockIndex(diskindex.hashPrev);
417 pindexNew->pnext = InsertBlockIndex(diskindex.hashNext);
418 pindexNew->nFile = diskindex.nFile;
419 pindexNew->nBlockPos = diskindex.nBlockPos;
420 pindexNew->nHeight = diskindex.nHeight;
421 pindexNew->nVersion = diskindex.nVersion;
422 pindexNew->hashMerkleRoot = diskindex.hashMerkleRoot;
423 pindexNew->nTime = diskindex.nTime;
424 pindexNew->nBits = diskindex.nBits;
425 pindexNew->nNonce = diskindex.nNonce;
427 // Watch for genesis block
428 if (pindexGenesisBlock == NULL && diskindex.GetBlockHash() == hashGenesisBlock)
429 pindexGenesisBlock = pindexNew;
431 if (!pindexNew->CheckIndex())
432 return error("LoadBlockIndex() : CheckIndex failed at %d", pindexNew->nHeight);
441 // Calculate bnChainWork
442 vector<pair<int, CBlockIndex*> > vSortedByHeight;
443 vSortedByHeight.reserve(mapBlockIndex.size());
444 BOOST_FOREACH(const PAIRTYPE(uint256, CBlockIndex*)& item, mapBlockIndex)
446 CBlockIndex* pindex = item.second;
447 vSortedByHeight.push_back(make_pair(pindex->nHeight, pindex));
449 sort(vSortedByHeight.begin(), vSortedByHeight.end());
450 BOOST_FOREACH(const PAIRTYPE(int, CBlockIndex*)& item, vSortedByHeight)
452 CBlockIndex* pindex = item.second;
453 pindex->bnChainWork = (pindex->pprev ? pindex->pprev->bnChainWork : 0) + pindex->GetBlockWork();
456 // Load hashBestChain pointer to end of best chain
457 if (!ReadHashBestChain(hashBestChain))
459 if (pindexGenesisBlock == NULL)
461 return error("CTxDB::LoadBlockIndex() : hashBestChain not loaded");
463 if (!mapBlockIndex.count(hashBestChain))
464 return error("CTxDB::LoadBlockIndex() : hashBestChain not found in the block index");
465 pindexBest = mapBlockIndex[hashBestChain];
466 nBestHeight = pindexBest->nHeight;
467 bnBestChainWork = pindexBest->bnChainWork;
468 printf("LoadBlockIndex(): hashBestChain=%s height=%d\n", hashBestChain.ToString().substr(0,20).c_str(), nBestHeight);
470 // Load bnBestInvalidWork, OK if it doesn't exist
471 ReadBestInvalidWork(bnBestInvalidWork);
473 // Verify blocks in the best chain
474 CBlockIndex* pindexFork = NULL;
475 for (CBlockIndex* pindex = pindexBest; pindex && pindex->pprev; pindex = pindex->pprev)
477 if (pindex->nHeight < nBestHeight-2500 && !mapArgs.count("-checkblocks"))
480 if (!block.ReadFromDisk(pindex))
481 return error("LoadBlockIndex() : block.ReadFromDisk failed");
482 if (!block.CheckBlock())
484 printf("LoadBlockIndex() : *** found bad block at %d, hash=%s\n", pindex->nHeight, pindex->GetBlockHash().ToString().c_str());
485 pindexFork = pindex->pprev;
490 // Reorg back to the fork
491 printf("LoadBlockIndex() : *** moving best chain pointer back to block %d\n", pindexFork->nHeight);
493 if (!block.ReadFromDisk(pindexFork))
494 return error("LoadBlockIndex() : block.ReadFromDisk failed");
496 block.SetBestChain(txdb, pindexFork);
510 bool CAddrDB::WriteAddress(const CAddress& addr)
512 return Write(make_pair(string("addr"), addr.GetKey()), addr);
515 bool CAddrDB::EraseAddress(const CAddress& addr)
517 return Erase(make_pair(string("addr"), addr.GetKey()));
520 bool CAddrDB::LoadAddresses()
522 CRITICAL_BLOCK(cs_mapAddresses)
524 // Load user provided addresses
525 CAutoFile filein = fopen((GetDataDir() + "/addr.txt").c_str(), "rt");
531 while (fgets(psz, sizeof(psz), filein))
533 CAddress addr(psz, NODE_NETWORK);
534 addr.nTime = 0; // so it won't relay unless successfully connected
543 Dbc* pcursor = GetCursor();
552 int ret = ReadAtCursor(pcursor, ssKey, ssValue);
553 if (ret == DB_NOTFOUND)
561 if (strType == "addr")
565 mapAddresses.insert(make_pair(addr.GetKey(), addr));
570 printf("Loaded %d addresses\n", mapAddresses.size());
578 return CAddrDB("cr+").LoadAddresses();
588 static set<int64> setKeyPool;
589 static CCriticalSection cs_setKeyPool;
591 bool CWalletDB::ReadAccount(const string& strAccount, CAccount& account)
594 return Read(make_pair(string("acc"), strAccount), account);
597 bool CWalletDB::WriteAccount(const string& strAccount, const CAccount& account)
599 return Write(make_pair(string("acc"), strAccount), account);
602 bool CWalletDB::WriteAccountingEntry(const CAccountingEntry& acentry)
604 return Write(make_tuple(string("acentry"), acentry.strAccount, ++nAccountingEntryNumber), acentry);
607 int64 CWalletDB::GetAccountCreditDebit(const string& strAccount)
609 list<CAccountingEntry> entries;
610 ListAccountCreditDebit(strAccount, entries);
612 int64 nCreditDebit = 0;
613 BOOST_FOREACH (const CAccountingEntry& entry, entries)
614 nCreditDebit += entry.nCreditDebit;
619 void CWalletDB::ListAccountCreditDebit(const string& strAccount, list<CAccountingEntry>& entries)
621 int64 nCreditDebit = 0;
623 bool fAllAccounts = (strAccount == "*");
625 Dbc* pcursor = GetCursor();
627 throw runtime_error("CWalletDB::ListAccountCreditDebit() : cannot create DB cursor");
628 unsigned int fFlags = DB_SET_RANGE;
633 if (fFlags == DB_SET_RANGE)
634 ssKey << make_tuple(string("acentry"), (fAllAccounts? string("") : strAccount), uint64(0));
636 int ret = ReadAtCursor(pcursor, ssKey, ssValue, fFlags);
638 if (ret == DB_NOTFOUND)
643 throw runtime_error("CWalletDB::ListAccountCreditDebit() : error scanning DB");
649 if (strType != "acentry")
651 CAccountingEntry acentry;
652 ssKey >> acentry.strAccount;
653 if (!fAllAccounts && acentry.strAccount != strAccount)
657 entries.push_back(acentry);
664 bool CWalletDB::LoadWallet()
666 vchDefaultKey.clear();
667 int nFileVersion = 0;
668 vector<uint256> vWalletUpgrade;
672 // Tray icon sometimes disappears on 9.10 karmic koala 64-bit, leaving no way to access the program
673 fMinimizeToTray = false;
674 fMinimizeOnClose = false;
677 //// todo: shouldn't we catch exceptions and try to recover and continue?
678 CRITICAL_BLOCK(cs_mapWallet)
679 CRITICAL_BLOCK(cs_mapKeys)
682 Dbc* pcursor = GetCursor();
691 int ret = ReadAtCursor(pcursor, ssKey, ssValue);
692 if (ret == DB_NOTFOUND)
698 // Taking advantage of the fact that pair serialization
699 // is just the two items serialized one after the other
702 if (strType == "name")
706 ssValue >> mapAddressBook[strAddress];
708 else if (strType == "tx")
712 CWalletTx& wtx = mapWallet[hash];
715 if (wtx.GetHash() != hash)
716 printf("Error in wallet.dat, hash mismatch\n");
718 // Undo serialize changes in 31600
719 if (31404 <= wtx.fTimeReceivedIsTxTime && wtx.fTimeReceivedIsTxTime <= 31703)
721 if (!ssValue.empty())
725 ssValue >> fTmp >> fUnused >> wtx.strFromAccount;
726 printf("LoadWallet() upgrading tx ver=%d %d '%s' %s\n", wtx.fTimeReceivedIsTxTime, fTmp, wtx.strFromAccount.c_str(), hash.ToString().c_str());
727 wtx.fTimeReceivedIsTxTime = fTmp;
731 printf("LoadWallet() repairing tx ver=%d %s\n", wtx.fTimeReceivedIsTxTime, hash.ToString().c_str());
732 wtx.fTimeReceivedIsTxTime = 0;
734 vWalletUpgrade.push_back(hash);
738 //printf("LoadWallet %s\n", wtx.GetHash().ToString().c_str());
739 //printf(" %12I64d %s %s %s\n",
740 // wtx.vout[0].nValue,
741 // DateTimeStrFormat("%x %H:%M:%S", wtx.GetBlockTime()).c_str(),
742 // wtx.hashBlock.ToString().substr(0,20).c_str(),
743 // wtx.mapValue["message"].c_str());
745 else if (strType == "acentry")
751 if (nNumber > nAccountingEntryNumber)
752 nAccountingEntryNumber = nNumber;
754 else if (strType == "key" || strType == "wkey")
756 vector<unsigned char> vchPubKey;
759 if (strType == "key")
760 ssValue >> wkey.vchPrivKey;
764 mapKeys[vchPubKey] = wkey.vchPrivKey;
765 mapPubKeys[Hash160(vchPubKey)] = vchPubKey;
767 else if (strType == "defaultkey")
769 ssValue >> vchDefaultKey;
771 else if (strType == "pool")
775 setKeyPool.insert(nIndex);
777 else if (strType == "version")
779 ssValue >> nFileVersion;
780 if (nFileVersion == 10300)
783 else if (strType == "setting")
790 if (strKey == "fGenerateBitcoins") ssValue >> fGenerateBitcoins;
792 if (strKey == "nTransactionFee") ssValue >> nTransactionFee;
793 if (strKey == "addrIncoming") ssValue >> addrIncoming;
794 if (strKey == "fLimitProcessors") ssValue >> fLimitProcessors;
795 if (strKey == "nLimitProcessors") ssValue >> nLimitProcessors;
796 if (strKey == "fMinimizeToTray") ssValue >> fMinimizeToTray;
797 if (strKey == "fMinimizeOnClose") ssValue >> fMinimizeOnClose;
798 if (strKey == "fUseProxy") ssValue >> fUseProxy;
799 if (strKey == "addrProxy") ssValue >> addrProxy;
800 if (fHaveUPnP && strKey == "fUseUPnP") ssValue >> fUseUPnP;
806 BOOST_FOREACH(uint256 hash, vWalletUpgrade)
807 WriteTx(hash, mapWallet[hash]);
809 printf("nFileVersion = %d\n", nFileVersion);
810 printf("fGenerateBitcoins = %d\n", fGenerateBitcoins);
811 printf("nTransactionFee = %"PRI64d"\n", nTransactionFee);
812 printf("addrIncoming = %s\n", addrIncoming.ToString().c_str());
813 printf("fMinimizeToTray = %d\n", fMinimizeToTray);
814 printf("fMinimizeOnClose = %d\n", fMinimizeOnClose);
815 printf("fUseProxy = %d\n", fUseProxy);
816 printf("addrProxy = %s\n", addrProxy.ToString().c_str());
818 printf("fUseUPnP = %d\n", fUseUPnP);
822 if (nFileVersion < VERSION)
824 // Get rid of old debug.log file in current directory
825 if (nFileVersion <= 105 && !pszSetDataDir[0])
828 WriteVersion(VERSION);
835 bool LoadWallet(bool& fFirstRunRet)
837 fFirstRunRet = false;
838 if (!CWalletDB("cr+").LoadWallet())
840 fFirstRunRet = vchDefaultKey.empty();
842 if (mapKeys.count(vchDefaultKey))
845 keyUser.SetPubKey(vchDefaultKey);
846 keyUser.SetPrivKey(mapKeys[vchDefaultKey]);
850 // Create new keyUser and set as default key
851 RandAddSeedPerfmon();
854 vchDefaultKey = GetKeyFromKeyPool();
855 walletdb.WriteDefaultKey(vchDefaultKey);
856 walletdb.WriteName(PubKeyToAddress(vchDefaultKey), "");
859 CreateThread(ThreadFlushWalletDB, NULL);
863 void ThreadFlushWalletDB(void* parg)
865 static bool fOneThread;
869 if (mapArgs.count("-noflushwallet"))
872 unsigned int nLastSeen = nWalletDBUpdated;
873 unsigned int nLastFlushed = nWalletDBUpdated;
874 int64 nLastWalletUpdate = GetTime();
879 if (nLastSeen != nWalletDBUpdated)
881 nLastSeen = nWalletDBUpdated;
882 nLastWalletUpdate = GetTime();
885 if (nLastFlushed != nWalletDBUpdated && GetTime() - nLastWalletUpdate >= 2)
887 TRY_CRITICAL_BLOCK(cs_db)
889 // Don't do this if any databases are in use
891 map<string, int>::iterator mi = mapFileUseCount.begin();
892 while (mi != mapFileUseCount.end())
894 nRefCount += (*mi).second;
898 if (nRefCount == 0 && !fShutdown)
900 string strFile = "wallet.dat";
901 map<string, int>::iterator mi = mapFileUseCount.find(strFile);
902 if (mi != mapFileUseCount.end())
904 printf("%s ", DateTimeStrFormat("%x %H:%M:%S", GetTime()).c_str());
905 printf("Flushing wallet.dat\n");
906 nLastFlushed = nWalletDBUpdated;
907 int64 nStart = GetTimeMillis();
909 // Flush wallet.dat so it's self contained
911 dbenv.txn_checkpoint(0, 0, 0);
912 dbenv.lsn_reset(strFile.c_str(), 0);
914 mapFileUseCount.erase(mi++);
915 printf("Flushed wallet.dat %"PRI64d"ms\n", GetTimeMillis() - nStart);
923 void BackupWallet(const string& strDest)
927 CRITICAL_BLOCK(cs_db)
929 const string strFile = "wallet.dat";
930 if (!mapFileUseCount.count(strFile) || mapFileUseCount[strFile] == 0)
932 // Flush log data to the dat file
934 dbenv.txn_checkpoint(0, 0, 0);
935 dbenv.lsn_reset(strFile.c_str(), 0);
936 mapFileUseCount.erase(strFile);
939 filesystem::path pathSrc(GetDataDir() + "/" + strFile);
940 filesystem::path pathDest(strDest);
941 if (filesystem::is_directory(pathDest))
942 pathDest = pathDest / strFile;
943 #if BOOST_VERSION >= 104000
944 filesystem::copy_file(pathSrc, pathDest, filesystem::copy_option::overwrite_if_exists);
946 filesystem::copy_file(pathSrc, pathDest);
948 printf("copied wallet.dat to %s\n", pathDest.string().c_str());
958 void CWalletDB::ReserveKeyFromKeyPool(int64& nIndex, CKeyPool& keypool)
961 keypool.vchPubKey.clear();
962 CRITICAL_BLOCK(cs_main)
963 CRITICAL_BLOCK(cs_mapWallet)
964 CRITICAL_BLOCK(cs_setKeyPool)
967 int64 nTargetSize = max(GetArg("-keypool", 100), (int64)0);
968 while (setKeyPool.size() < nTargetSize+1)
971 if (!setKeyPool.empty())
972 nEnd = *(--setKeyPool.end()) + 1;
973 if (!Write(make_pair(string("pool"), nEnd), CKeyPool(GenerateNewKey())))
974 throw runtime_error("ReserveKeyFromKeyPool() : writing generated key failed");
975 setKeyPool.insert(nEnd);
976 printf("keypool added key %"PRI64d", size=%d\n", nEnd, setKeyPool.size());
979 // Get the oldest key
980 assert(!setKeyPool.empty());
981 nIndex = *(setKeyPool.begin());
982 setKeyPool.erase(setKeyPool.begin());
983 if (!Read(make_pair(string("pool"), nIndex), keypool))
984 throw runtime_error("ReserveKeyFromKeyPool() : read failed");
985 if (!mapKeys.count(keypool.vchPubKey))
986 throw runtime_error("ReserveKeyFromKeyPool() : unknown key in key pool");
987 assert(!keypool.vchPubKey.empty());
988 printf("keypool reserve %"PRI64d"\n", nIndex);
992 void CWalletDB::KeepKey(int64 nIndex)
994 // Remove from key pool
995 CRITICAL_BLOCK(cs_main)
996 CRITICAL_BLOCK(cs_mapWallet)
998 Erase(make_pair(string("pool"), nIndex));
1000 printf("keypool keep %"PRI64d"\n", nIndex);
1003 void CWalletDB::ReturnKey(int64 nIndex)
1005 // Return to key pool
1006 CRITICAL_BLOCK(cs_setKeyPool)
1007 setKeyPool.insert(nIndex);
1008 printf("keypool return %"PRI64d"\n", nIndex);
1011 vector<unsigned char> GetKeyFromKeyPool()
1016 walletdb.ReserveKeyFromKeyPool(nIndex, keypool);
1017 walletdb.KeepKey(nIndex);
1018 return keypool.vchPubKey;
1021 int64 GetOldestKeyPoolTime()
1026 walletdb.ReserveKeyFromKeyPool(nIndex, keypool);
1027 walletdb.ReturnKey(nIndex);
1028 return keypool.nTime;