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.
10 using namespace boost;
12 void ThreadFlushWalletDB(void* parg);
15 unsigned int nWalletDBUpdated;
16 uint64 nAccountingEntryNumber = 0;
24 static CCriticalSection cs_db;
25 static bool fDbEnvInit = false;
27 static map<string, int> mapFileUseCount;
28 static map<string, Db*> mapDb;
48 CDB::CDB(const char* pszFile, const char* pszMode) : pdb(NULL)
54 fReadOnly = (!strchr(pszMode, '+') && !strchr(pszMode, 'w'));
55 bool fCreate = strchr(pszMode, 'c');
56 unsigned int nFlags = DB_THREAD;
66 string strDataDir = GetDataDir();
67 string strLogDir = strDataDir + "/database";
68 filesystem::create_directory(strLogDir.c_str());
69 string strErrorFile = strDataDir + "/db.log";
70 printf("dbenv.open strLogDir=%s strErrorFile=%s\n", strLogDir.c_str(), strErrorFile.c_str());
72 dbenv.set_lg_dir(strLogDir.c_str());
73 dbenv.set_lg_max(10000000);
74 dbenv.set_lk_max_locks(10000);
75 dbenv.set_lk_max_objects(10000);
76 dbenv.set_errfile(fopen(strErrorFile.c_str(), "a")); /// debug
77 dbenv.set_flags(DB_AUTO_COMMIT, 1);
78 ret = dbenv.open(strDataDir.c_str(),
88 throw runtime_error(strprintf("CDB() : error %d opening database environment", ret));
93 ++mapFileUseCount[strFile];
97 pdb = new Db(&dbenv, 0);
99 ret = pdb->open(NULL, // Txn pointer
101 "main", // Logical db name
102 DB_BTREE, // Database type
110 CRITICAL_BLOCK(cs_db)
111 --mapFileUseCount[strFile];
113 throw runtime_error(strprintf("CDB() : can't open database file %s, error %d", pszFile, ret));
116 if (fCreate && !Exists(string("version")))
118 bool fTmp = fReadOnly;
120 WriteVersion(VERSION);
124 mapDb[strFile] = pdb;
134 vTxn.front()->abort();
138 // Flush database activity from memory pool to disk log
139 unsigned int nMinutes = 0;
142 if (strFile == "addr.dat")
144 if (strFile == "blkindex.dat" && IsInitialBlockDownload() && nBestHeight % 500 != 0)
146 dbenv.txn_checkpoint(0, nMinutes, 0);
148 CRITICAL_BLOCK(cs_db)
149 --mapFileUseCount[strFile];
152 void CloseDb(const string& strFile)
154 CRITICAL_BLOCK(cs_db)
156 if (mapDb[strFile] != NULL)
158 // Close the database handle
159 Db* pdb = mapDb[strFile];
162 mapDb[strFile] = NULL;
167 void DBFlush(bool fShutdown)
169 // Flush log data to the actual data file
170 // on all files that are not in use
171 printf("DBFlush(%s)%s\n", fShutdown ? "true" : "false", fDbEnvInit ? "" : " db not started");
174 CRITICAL_BLOCK(cs_db)
176 map<string, int>::iterator mi = mapFileUseCount.begin();
177 while (mi != mapFileUseCount.end())
179 string strFile = (*mi).first;
180 int nRefCount = (*mi).second;
181 printf("%s refcount=%d\n", strFile.c_str(), nRefCount);
184 // Move log data to the dat file
186 dbenv.txn_checkpoint(0, 0, 0);
187 printf("%s flush\n", strFile.c_str());
188 dbenv.lsn_reset(strFile.c_str(), 0);
189 mapFileUseCount.erase(mi++);
197 if (mapFileUseCount.empty())
198 dbenv.log_archive(&listp, DB_ARCH_REMOVE);
214 bool CTxDB::ReadTxIndex(uint256 hash, CTxIndex& txindex)
218 return Read(make_pair(string("tx"), hash), txindex);
221 bool CTxDB::UpdateTxIndex(uint256 hash, const CTxIndex& txindex)
224 return Write(make_pair(string("tx"), hash), txindex);
227 bool CTxDB::AddTxIndex(const CTransaction& tx, const CDiskTxPos& pos, int nHeight)
232 uint256 hash = tx.GetHash();
233 CTxIndex txindex(pos, tx.vout.size());
234 return Write(make_pair(string("tx"), hash), txindex);
237 bool CTxDB::EraseTxIndex(const CTransaction& tx)
240 uint256 hash = tx.GetHash();
242 return Erase(make_pair(string("tx"), hash));
245 bool CTxDB::ContainsTx(uint256 hash)
248 return Exists(make_pair(string("tx"), hash));
251 bool CTxDB::ReadOwnerTxes(uint160 hash160, int nMinHeight, vector<CTransaction>& vtx)
257 Dbc* pcursor = GetCursor();
261 unsigned int fFlags = DB_SET_RANGE;
266 if (fFlags == DB_SET_RANGE)
267 ssKey << string("owner") << hash160 << CDiskTxPos(0, 0, 0);
269 int ret = ReadAtCursor(pcursor, ssKey, ssValue, fFlags);
271 if (ret == DB_NOTFOUND)
283 ssKey >> strType >> hashItem >> pos;
285 ssValue >> nItemHeight;
288 if (strType != "owner" || hashItem != hash160)
290 if (nItemHeight >= nMinHeight)
292 vtx.resize(vtx.size()+1);
293 if (!vtx.back().ReadFromDisk(pos))
305 bool CTxDB::ReadDiskTx(uint256 hash, CTransaction& tx, CTxIndex& txindex)
309 if (!ReadTxIndex(hash, txindex))
311 return (tx.ReadFromDisk(txindex.pos));
314 bool CTxDB::ReadDiskTx(uint256 hash, CTransaction& tx)
317 return ReadDiskTx(hash, tx, txindex);
320 bool CTxDB::ReadDiskTx(COutPoint outpoint, CTransaction& tx, CTxIndex& txindex)
322 return ReadDiskTx(outpoint.hash, tx, txindex);
325 bool CTxDB::ReadDiskTx(COutPoint outpoint, CTransaction& tx)
328 return ReadDiskTx(outpoint.hash, tx, txindex);
331 bool CTxDB::WriteBlockIndex(const CDiskBlockIndex& blockindex)
333 return Write(make_pair(string("blockindex"), blockindex.GetBlockHash()), blockindex);
336 bool CTxDB::EraseBlockIndex(uint256 hash)
338 return Erase(make_pair(string("blockindex"), hash));
341 bool CTxDB::ReadHashBestChain(uint256& hashBestChain)
343 return Read(string("hashBestChain"), hashBestChain);
346 bool CTxDB::WriteHashBestChain(uint256 hashBestChain)
348 return Write(string("hashBestChain"), hashBestChain);
351 bool CTxDB::ReadBestInvalidWork(CBigNum& bnBestInvalidWork)
353 return Read(string("bnBestInvalidWork"), bnBestInvalidWork);
356 bool CTxDB::WriteBestInvalidWork(CBigNum bnBestInvalidWork)
358 return Write(string("bnBestInvalidWork"), bnBestInvalidWork);
361 CBlockIndex* InsertBlockIndex(uint256 hash)
367 map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.find(hash);
368 if (mi != mapBlockIndex.end())
372 CBlockIndex* pindexNew = new CBlockIndex();
374 throw runtime_error("LoadBlockIndex() : new CBlockIndex failed");
375 mi = mapBlockIndex.insert(make_pair(hash, pindexNew)).first;
376 pindexNew->phashBlock = &((*mi).first);
381 bool CTxDB::LoadBlockIndex()
383 // Get database cursor
384 Dbc* pcursor = GetCursor();
388 // Load mapBlockIndex
389 unsigned int fFlags = DB_SET_RANGE;
394 if (fFlags == DB_SET_RANGE)
395 ssKey << make_pair(string("blockindex"), uint256(0));
397 int ret = ReadAtCursor(pcursor, ssKey, ssValue, fFlags);
399 if (ret == DB_NOTFOUND)
407 if (strType == "blockindex")
409 CDiskBlockIndex diskindex;
410 ssValue >> diskindex;
412 // Construct block index object
413 CBlockIndex* pindexNew = InsertBlockIndex(diskindex.GetBlockHash());
414 pindexNew->pprev = InsertBlockIndex(diskindex.hashPrev);
415 pindexNew->pnext = InsertBlockIndex(diskindex.hashNext);
416 pindexNew->nFile = diskindex.nFile;
417 pindexNew->nBlockPos = diskindex.nBlockPos;
418 pindexNew->nHeight = diskindex.nHeight;
419 pindexNew->nVersion = diskindex.nVersion;
420 pindexNew->hashMerkleRoot = diskindex.hashMerkleRoot;
421 pindexNew->nTime = diskindex.nTime;
422 pindexNew->nBits = diskindex.nBits;
423 pindexNew->nNonce = diskindex.nNonce;
425 // Watch for genesis block
426 if (pindexGenesisBlock == NULL && diskindex.GetBlockHash() == hashGenesisBlock)
427 pindexGenesisBlock = pindexNew;
429 if (!pindexNew->CheckIndex())
430 return error("LoadBlockIndex() : CheckIndex failed at %d", pindexNew->nHeight);
439 // Calculate bnChainWork
440 vector<pair<int, CBlockIndex*> > vSortedByHeight;
441 vSortedByHeight.reserve(mapBlockIndex.size());
442 BOOST_FOREACH(const PAIRTYPE(uint256, CBlockIndex*)& item, mapBlockIndex)
444 CBlockIndex* pindex = item.second;
445 vSortedByHeight.push_back(make_pair(pindex->nHeight, pindex));
447 sort(vSortedByHeight.begin(), vSortedByHeight.end());
448 BOOST_FOREACH(const PAIRTYPE(int, CBlockIndex*)& item, vSortedByHeight)
450 CBlockIndex* pindex = item.second;
451 pindex->bnChainWork = (pindex->pprev ? pindex->pprev->bnChainWork : 0) + pindex->GetBlockWork();
454 // Load hashBestChain pointer to end of best chain
455 if (!ReadHashBestChain(hashBestChain))
457 if (pindexGenesisBlock == NULL)
459 return error("CTxDB::LoadBlockIndex() : hashBestChain not loaded");
461 if (!mapBlockIndex.count(hashBestChain))
462 return error("CTxDB::LoadBlockIndex() : hashBestChain not found in the block index");
463 pindexBest = mapBlockIndex[hashBestChain];
464 nBestHeight = pindexBest->nHeight;
465 bnBestChainWork = pindexBest->bnChainWork;
466 printf("LoadBlockIndex(): hashBestChain=%s height=%d\n", hashBestChain.ToString().substr(0,20).c_str(), nBestHeight);
468 // Load bnBestInvalidWork, OK if it doesn't exist
469 ReadBestInvalidWork(bnBestInvalidWork);
471 // Verify blocks in the best chain
472 CBlockIndex* pindexFork = NULL;
473 for (CBlockIndex* pindex = pindexBest; pindex && pindex->pprev; pindex = pindex->pprev)
475 if (pindex->nHeight < nBestHeight-2500 && !mapArgs.count("-checkblocks"))
478 if (!block.ReadFromDisk(pindex))
479 return error("LoadBlockIndex() : block.ReadFromDisk failed");
480 if (!block.CheckBlock())
482 printf("LoadBlockIndex() : *** found bad block at %d, hash=%s\n", pindex->nHeight, pindex->GetBlockHash().ToString().c_str());
483 pindexFork = pindex->pprev;
488 // Reorg back to the fork
489 printf("LoadBlockIndex() : *** moving best chain pointer back to block %d\n", pindexFork->nHeight);
491 if (!block.ReadFromDisk(pindexFork))
492 return error("LoadBlockIndex() : block.ReadFromDisk failed");
494 block.SetBestChain(txdb, pindexFork);
508 bool CAddrDB::WriteAddress(const CAddress& addr)
510 return Write(make_pair(string("addr"), addr.GetKey()), addr);
513 bool CAddrDB::EraseAddress(const CAddress& addr)
515 return Erase(make_pair(string("addr"), addr.GetKey()));
518 bool CAddrDB::LoadAddresses()
520 CRITICAL_BLOCK(cs_mapAddresses)
522 // Load user provided addresses
523 CAutoFile filein = fopen((GetDataDir() + "/addr.txt").c_str(), "rt");
529 while (fgets(psz, sizeof(psz), filein))
531 CAddress addr(psz, NODE_NETWORK);
532 addr.nTime = 0; // so it won't relay unless successfully connected
541 Dbc* pcursor = GetCursor();
550 int ret = ReadAtCursor(pcursor, ssKey, ssValue);
551 if (ret == DB_NOTFOUND)
559 if (strType == "addr")
563 mapAddresses.insert(make_pair(addr.GetKey(), addr));
568 printf("Loaded %d addresses\n", mapAddresses.size());
576 return CAddrDB("cr+").LoadAddresses();
586 static set<int64> setKeyPool;
587 static CCriticalSection cs_setKeyPool;
589 bool CWalletDB::ReadAccount(const string& strAccount, CAccount& account)
592 return Read(make_pair(string("acc"), strAccount), account);
595 bool CWalletDB::WriteAccount(const string& strAccount, const CAccount& account)
597 return Write(make_pair(string("acc"), strAccount), account);
600 bool CWalletDB::WriteAccountingEntry(const CAccountingEntry& acentry)
602 return Write(make_tuple(string("acentry"), acentry.strAccount, ++nAccountingEntryNumber), acentry);
605 int64 CWalletDB::GetAccountCreditDebit(const string& strAccount)
607 list<CAccountingEntry> entries;
608 ListAccountCreditDebit(strAccount, entries);
610 int64 nCreditDebit = 0;
611 BOOST_FOREACH (const CAccountingEntry& entry, entries)
612 nCreditDebit += entry.nCreditDebit;
617 void CWalletDB::ListAccountCreditDebit(const string& strAccount, list<CAccountingEntry>& entries)
619 int64 nCreditDebit = 0;
621 bool fAllAccounts = (strAccount == "*");
623 Dbc* pcursor = GetCursor();
625 throw runtime_error("CWalletDB::ListAccountCreditDebit() : cannot create DB cursor");
626 unsigned int fFlags = DB_SET_RANGE;
631 if (fFlags == DB_SET_RANGE)
632 ssKey << make_tuple(string("acentry"), (fAllAccounts? string("") : strAccount), uint64(0));
634 int ret = ReadAtCursor(pcursor, ssKey, ssValue, fFlags);
636 if (ret == DB_NOTFOUND)
641 throw runtime_error("CWalletDB::ListAccountCreditDebit() : error scanning DB");
647 if (strType != "acentry")
649 CAccountingEntry acentry;
650 ssKey >> acentry.strAccount;
651 if (!fAllAccounts && acentry.strAccount != strAccount)
655 entries.push_back(acentry);
662 bool CWalletDB::LoadWallet()
664 vchDefaultKey.clear();
665 int nFileVersion = 0;
666 vector<uint256> vWalletUpgrade;
670 // Tray icon sometimes disappears on 9.10 karmic koala 64-bit, leaving no way to access the program
671 fMinimizeToTray = false;
672 fMinimizeOnClose = false;
675 //// todo: shouldn't we catch exceptions and try to recover and continue?
676 CRITICAL_BLOCK(cs_mapWallet)
677 CRITICAL_BLOCK(cs_mapKeys)
680 Dbc* pcursor = GetCursor();
689 int ret = ReadAtCursor(pcursor, ssKey, ssValue);
690 if (ret == DB_NOTFOUND)
696 // Taking advantage of the fact that pair serialization
697 // is just the two items serialized one after the other
700 if (strType == "name")
704 ssValue >> mapAddressBook[strAddress];
706 else if (strType == "tx")
710 CWalletTx& wtx = mapWallet[hash];
713 if (wtx.GetHash() != hash)
714 printf("Error in wallet.dat, hash mismatch\n");
716 // Undo serialize changes in 31600
717 if (31404 <= wtx.fTimeReceivedIsTxTime && wtx.fTimeReceivedIsTxTime <= 31703)
719 if (!ssValue.empty())
723 ssValue >> fTmp >> fUnused >> wtx.strFromAccount;
724 printf("LoadWallet() upgrading tx ver=%d %d '%s' %s\n", wtx.fTimeReceivedIsTxTime, fTmp, wtx.strFromAccount.c_str(), hash.ToString().c_str());
725 wtx.fTimeReceivedIsTxTime = fTmp;
729 printf("LoadWallet() repairing tx ver=%d %s\n", wtx.fTimeReceivedIsTxTime, hash.ToString().c_str());
730 wtx.fTimeReceivedIsTxTime = 0;
732 vWalletUpgrade.push_back(hash);
736 //printf("LoadWallet %s\n", wtx.GetHash().ToString().c_str());
737 //printf(" %12I64d %s %s %s\n",
738 // wtx.vout[0].nValue,
739 // DateTimeStrFormat("%x %H:%M:%S", wtx.GetBlockTime()).c_str(),
740 // wtx.hashBlock.ToString().substr(0,20).c_str(),
741 // wtx.mapValue["message"].c_str());
743 else if (strType == "acentry")
749 if (nNumber > nAccountingEntryNumber)
750 nAccountingEntryNumber = nNumber;
752 else if (strType == "key" || strType == "wkey")
754 vector<unsigned char> vchPubKey;
757 if (strType == "key")
758 ssValue >> wkey.vchPrivKey;
762 mapKeys[vchPubKey] = wkey.vchPrivKey;
763 mapPubKeys[Hash160(vchPubKey)] = vchPubKey;
765 else if (strType == "defaultkey")
767 ssValue >> vchDefaultKey;
769 else if (strType == "pool")
773 setKeyPool.insert(nIndex);
775 else if (strType == "version")
777 ssValue >> nFileVersion;
778 if (nFileVersion == 10300)
781 else if (strType == "setting")
788 if (strKey == "fGenerateBitcoins") ssValue >> fGenerateBitcoins;
790 if (strKey == "nTransactionFee") ssValue >> nTransactionFee;
791 if (strKey == "addrIncoming") ssValue >> addrIncoming;
792 if (strKey == "fLimitProcessors") ssValue >> fLimitProcessors;
793 if (strKey == "nLimitProcessors") ssValue >> nLimitProcessors;
794 if (strKey == "fMinimizeToTray") ssValue >> fMinimizeToTray;
795 if (strKey == "fMinimizeOnClose") ssValue >> fMinimizeOnClose;
796 if (strKey == "fUseProxy") ssValue >> fUseProxy;
797 if (strKey == "addrProxy") ssValue >> addrProxy;
798 if (fHaveUPnP && strKey == "fUseUPnP") ssValue >> fUseUPnP;
804 BOOST_FOREACH(uint256 hash, vWalletUpgrade)
805 WriteTx(hash, mapWallet[hash]);
807 printf("nFileVersion = %d\n", nFileVersion);
808 printf("fGenerateBitcoins = %d\n", fGenerateBitcoins);
809 printf("nTransactionFee = %"PRI64d"\n", nTransactionFee);
810 printf("addrIncoming = %s\n", addrIncoming.ToString().c_str());
811 printf("fMinimizeToTray = %d\n", fMinimizeToTray);
812 printf("fMinimizeOnClose = %d\n", fMinimizeOnClose);
813 printf("fUseProxy = %d\n", fUseProxy);
814 printf("addrProxy = %s\n", addrProxy.ToString().c_str());
816 printf("fUseUPnP = %d\n", fUseUPnP);
820 if (nFileVersion < VERSION)
822 // Get rid of old debug.log file in current directory
823 if (nFileVersion <= 105 && !pszSetDataDir[0])
826 WriteVersion(VERSION);
833 bool LoadWallet(bool& fFirstRunRet)
835 fFirstRunRet = false;
836 if (!CWalletDB("cr+").LoadWallet())
838 fFirstRunRet = vchDefaultKey.empty();
840 if (mapKeys.count(vchDefaultKey))
843 keyUser.SetPubKey(vchDefaultKey);
844 keyUser.SetPrivKey(mapKeys[vchDefaultKey]);
848 // Create new keyUser and set as default key
849 RandAddSeedPerfmon();
850 keyUser.MakeNewKey();
851 if (!AddKey(keyUser))
853 if (!SetAddressBookName(PubKeyToAddress(keyUser.GetPubKey()), ""))
855 CWalletDB().WriteDefaultKey(keyUser.GetPubKey());
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;