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.
11 void ThreadFlushWalletDB(void* parg);
14 unsigned int nWalletDBUpdated;
15 uint64 nAccountingEntryNumber = 0;
23 static CCriticalSection cs_db;
24 static bool fDbEnvInit = false;
26 static map<string, int> mapFileUseCount;
27 static map<string, Db*> mapDb;
47 CDB::CDB(const char* pszFile, const char* pszMode) : pdb(NULL)
53 fReadOnly = (!strchr(pszMode, '+') && !strchr(pszMode, 'w'));
54 bool fCreate = strchr(pszMode, 'c');
55 unsigned int nFlags = DB_THREAD;
65 string strDataDir = GetDataDir();
66 string strLogDir = strDataDir + "/database";
67 filesystem::create_directory(strLogDir.c_str());
68 string strErrorFile = strDataDir + "/db.log";
69 printf("dbenv.open strLogDir=%s strErrorFile=%s\n", strLogDir.c_str(), strErrorFile.c_str());
71 dbenv.set_lg_dir(strLogDir.c_str());
72 dbenv.set_lg_max(10000000);
73 dbenv.set_lk_max_locks(10000);
74 dbenv.set_lk_max_objects(10000);
75 dbenv.set_errfile(fopen(strErrorFile.c_str(), "a")); /// debug
76 dbenv.set_flags(DB_AUTO_COMMIT, 1);
77 ret = dbenv.open(strDataDir.c_str(),
87 throw runtime_error(strprintf("CDB() : error %d opening database environment", ret));
92 ++mapFileUseCount[strFile];
96 pdb = new Db(&dbenv, 0);
98 ret = pdb->open(NULL, // Txn pointer
100 "main", // Logical db name
101 DB_BTREE, // Database type
109 CRITICAL_BLOCK(cs_db)
110 --mapFileUseCount[strFile];
112 throw runtime_error(strprintf("CDB() : can't open database file %s, error %d", pszFile, ret));
115 if (fCreate && !Exists(string("version")))
117 bool fTmp = fReadOnly;
119 WriteVersion(VERSION);
123 mapDb[strFile] = pdb;
133 vTxn.front()->abort();
137 // Flush database activity from memory pool to disk log
138 unsigned int nMinutes = 0;
141 if (strFile == "addr.dat")
143 if (strFile == "blkindex.dat" && IsInitialBlockDownload() && nBestHeight % 500 != 0)
145 dbenv.txn_checkpoint(0, nMinutes, 0);
147 CRITICAL_BLOCK(cs_db)
148 --mapFileUseCount[strFile];
151 void CloseDb(const string& strFile)
153 CRITICAL_BLOCK(cs_db)
155 if (mapDb[strFile] != NULL)
157 // Close the database handle
158 Db* pdb = mapDb[strFile];
161 mapDb[strFile] = NULL;
166 void DBFlush(bool fShutdown)
168 // Flush log data to the actual data file
169 // on all files that are not in use
170 printf("DBFlush(%s)%s\n", fShutdown ? "true" : "false", fDbEnvInit ? "" : " db not started");
173 CRITICAL_BLOCK(cs_db)
175 map<string, int>::iterator mi = mapFileUseCount.begin();
176 while (mi != mapFileUseCount.end())
178 string strFile = (*mi).first;
179 int nRefCount = (*mi).second;
180 printf("%s refcount=%d\n", strFile.c_str(), nRefCount);
183 // Move log data to the dat file
185 dbenv.txn_checkpoint(0, 0, 0);
186 printf("%s flush\n", strFile.c_str());
187 dbenv.lsn_reset(strFile.c_str(), 0);
188 mapFileUseCount.erase(mi++);
196 if (mapFileUseCount.empty())
197 dbenv.log_archive(&listp, DB_ARCH_REMOVE);
213 bool CTxDB::ReadTxIndex(uint256 hash, CTxIndex& txindex)
217 return Read(make_pair(string("tx"), hash), txindex);
220 bool CTxDB::UpdateTxIndex(uint256 hash, const CTxIndex& txindex)
223 return Write(make_pair(string("tx"), hash), txindex);
226 bool CTxDB::AddTxIndex(const CTransaction& tx, const CDiskTxPos& pos, int nHeight)
231 uint256 hash = tx.GetHash();
232 CTxIndex txindex(pos, tx.vout.size());
233 return Write(make_pair(string("tx"), hash), txindex);
236 bool CTxDB::EraseTxIndex(const CTransaction& tx)
239 uint256 hash = tx.GetHash();
241 return Erase(make_pair(string("tx"), hash));
244 bool CTxDB::ContainsTx(uint256 hash)
247 return Exists(make_pair(string("tx"), hash));
250 bool CTxDB::ReadOwnerTxes(uint160 hash160, int nMinHeight, vector<CTransaction>& vtx)
256 Dbc* pcursor = GetCursor();
260 unsigned int fFlags = DB_SET_RANGE;
265 if (fFlags == DB_SET_RANGE)
266 ssKey << string("owner") << hash160 << CDiskTxPos(0, 0, 0);
268 int ret = ReadAtCursor(pcursor, ssKey, ssValue, fFlags);
270 if (ret == DB_NOTFOUND)
282 ssKey >> strType >> hashItem >> pos;
284 ssValue >> nItemHeight;
287 if (strType != "owner" || hashItem != hash160)
289 if (nItemHeight >= nMinHeight)
291 vtx.resize(vtx.size()+1);
292 if (!vtx.back().ReadFromDisk(pos))
304 bool CTxDB::ReadDiskTx(uint256 hash, CTransaction& tx, CTxIndex& txindex)
308 if (!ReadTxIndex(hash, txindex))
310 return (tx.ReadFromDisk(txindex.pos));
313 bool CTxDB::ReadDiskTx(uint256 hash, CTransaction& tx)
316 return ReadDiskTx(hash, tx, txindex);
319 bool CTxDB::ReadDiskTx(COutPoint outpoint, CTransaction& tx, CTxIndex& txindex)
321 return ReadDiskTx(outpoint.hash, tx, txindex);
324 bool CTxDB::ReadDiskTx(COutPoint outpoint, CTransaction& tx)
327 return ReadDiskTx(outpoint.hash, tx, txindex);
330 bool CTxDB::WriteBlockIndex(const CDiskBlockIndex& blockindex)
332 return Write(make_pair(string("blockindex"), blockindex.GetBlockHash()), blockindex);
335 bool CTxDB::EraseBlockIndex(uint256 hash)
337 return Erase(make_pair(string("blockindex"), hash));
340 bool CTxDB::ReadHashBestChain(uint256& hashBestChain)
342 return Read(string("hashBestChain"), hashBestChain);
345 bool CTxDB::WriteHashBestChain(uint256 hashBestChain)
347 return Write(string("hashBestChain"), hashBestChain);
350 bool CTxDB::ReadBestInvalidWork(CBigNum& bnBestInvalidWork)
352 return Read(string("bnBestInvalidWork"), bnBestInvalidWork);
355 bool CTxDB::WriteBestInvalidWork(CBigNum bnBestInvalidWork)
357 return Write(string("bnBestInvalidWork"), bnBestInvalidWork);
360 CBlockIndex* InsertBlockIndex(uint256 hash)
366 map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.find(hash);
367 if (mi != mapBlockIndex.end())
371 CBlockIndex* pindexNew = new CBlockIndex();
373 throw runtime_error("LoadBlockIndex() : new CBlockIndex failed");
374 mi = mapBlockIndex.insert(make_pair(hash, pindexNew)).first;
375 pindexNew->phashBlock = &((*mi).first);
380 bool CTxDB::LoadBlockIndex()
382 // Get database cursor
383 Dbc* pcursor = GetCursor();
387 // Load mapBlockIndex
388 unsigned int fFlags = DB_SET_RANGE;
393 if (fFlags == DB_SET_RANGE)
394 ssKey << make_pair(string("blockindex"), uint256(0));
396 int ret = ReadAtCursor(pcursor, ssKey, ssValue, fFlags);
398 if (ret == DB_NOTFOUND)
406 if (strType == "blockindex")
408 CDiskBlockIndex diskindex;
409 ssValue >> diskindex;
411 // Construct block index object
412 CBlockIndex* pindexNew = InsertBlockIndex(diskindex.GetBlockHash());
413 pindexNew->pprev = InsertBlockIndex(diskindex.hashPrev);
414 pindexNew->pnext = InsertBlockIndex(diskindex.hashNext);
415 pindexNew->nFile = diskindex.nFile;
416 pindexNew->nBlockPos = diskindex.nBlockPos;
417 pindexNew->nHeight = diskindex.nHeight;
418 pindexNew->nVersion = diskindex.nVersion;
419 pindexNew->hashMerkleRoot = diskindex.hashMerkleRoot;
420 pindexNew->nTime = diskindex.nTime;
421 pindexNew->nBits = diskindex.nBits;
422 pindexNew->nNonce = diskindex.nNonce;
424 // Watch for genesis block
425 if (pindexGenesisBlock == NULL && diskindex.GetBlockHash() == hashGenesisBlock)
426 pindexGenesisBlock = pindexNew;
428 if (!pindexNew->CheckIndex())
429 return error("LoadBlockIndex() : CheckIndex failed at %d", pindexNew->nHeight);
438 // Calculate bnChainWork
439 vector<pair<int, CBlockIndex*> > vSortedByHeight;
440 vSortedByHeight.reserve(mapBlockIndex.size());
441 BOOST_FOREACH(const PAIRTYPE(uint256, CBlockIndex*)& item, mapBlockIndex)
443 CBlockIndex* pindex = item.second;
444 vSortedByHeight.push_back(make_pair(pindex->nHeight, pindex));
446 sort(vSortedByHeight.begin(), vSortedByHeight.end());
447 BOOST_FOREACH(const PAIRTYPE(int, CBlockIndex*)& item, vSortedByHeight)
449 CBlockIndex* pindex = item.second;
450 pindex->bnChainWork = (pindex->pprev ? pindex->pprev->bnChainWork : 0) + pindex->GetBlockWork();
453 // Load hashBestChain pointer to end of best chain
454 if (!ReadHashBestChain(hashBestChain))
456 if (pindexGenesisBlock == NULL)
458 return error("CTxDB::LoadBlockIndex() : hashBestChain not loaded");
460 if (!mapBlockIndex.count(hashBestChain))
461 return error("CTxDB::LoadBlockIndex() : hashBestChain not found in the block index");
462 pindexBest = mapBlockIndex[hashBestChain];
463 nBestHeight = pindexBest->nHeight;
464 bnBestChainWork = pindexBest->bnChainWork;
465 printf("LoadBlockIndex(): hashBestChain=%s height=%d\n", hashBestChain.ToString().substr(0,20).c_str(), nBestHeight);
467 // Load bnBestInvalidWork, OK if it doesn't exist
468 ReadBestInvalidWork(bnBestInvalidWork);
470 // Verify blocks in the best chain
471 CBlockIndex* pindexFork = NULL;
472 for (CBlockIndex* pindex = pindexBest; pindex && pindex->pprev; pindex = pindex->pprev)
474 if (pindex->nHeight < nBestHeight-2500 && !mapArgs.count("-checkblocks"))
477 if (!block.ReadFromDisk(pindex))
478 return error("LoadBlockIndex() : block.ReadFromDisk failed");
479 if (!block.CheckBlock())
481 printf("LoadBlockIndex() : *** found bad block at %d, hash=%s\n", pindex->nHeight, pindex->GetBlockHash().ToString().c_str());
482 pindexFork = pindex->pprev;
487 // Reorg back to the fork
488 printf("LoadBlockIndex() : *** moving best chain pointer back to block %d\n", pindexFork->nHeight);
490 if (!block.ReadFromDisk(pindexFork))
491 return error("LoadBlockIndex() : block.ReadFromDisk failed");
493 block.SetBestChain(txdb, pindexFork);
507 bool CAddrDB::WriteAddress(const CAddress& addr)
509 return Write(make_pair(string("addr"), addr.GetKey()), addr);
512 bool CAddrDB::EraseAddress(const CAddress& addr)
514 return Erase(make_pair(string("addr"), addr.GetKey()));
517 bool CAddrDB::LoadAddresses()
519 CRITICAL_BLOCK(cs_mapAddresses)
521 // Load user provided addresses
522 CAutoFile filein = fopen((GetDataDir() + "/addr.txt").c_str(), "rt");
528 while (fgets(psz, sizeof(psz), filein))
530 CAddress addr(psz, NODE_NETWORK);
531 addr.nTime = 0; // so it won't relay unless successfully connected
540 Dbc* pcursor = GetCursor();
549 int ret = ReadAtCursor(pcursor, ssKey, ssValue);
550 if (ret == DB_NOTFOUND)
558 if (strType == "addr")
562 mapAddresses.insert(make_pair(addr.GetKey(), addr));
567 printf("Loaded %d addresses\n", mapAddresses.size());
575 return CAddrDB("cr+").LoadAddresses();
585 static set<int64> setKeyPool;
586 static CCriticalSection cs_setKeyPool;
588 bool CWalletDB::ReadAccount(const string& strAccount, CAccount& account)
591 return Read(make_pair(string("acc"), strAccount), account);
594 bool CWalletDB::WriteAccount(const string& strAccount, const CAccount& account)
596 return Write(make_pair(string("acc"), strAccount), account);
599 bool CWalletDB::WriteAccountingEntry(const CAccountingEntry& acentry)
601 return Write(make_tuple(string("acentry"), acentry.strAccount, ++nAccountingEntryNumber), acentry);
604 int64 CWalletDB::GetAccountCreditDebit(const string& strAccount)
606 list<CAccountingEntry> entries;
607 ListAccountCreditDebit(strAccount, entries);
609 int64 nCreditDebit = 0;
610 BOOST_FOREACH (const CAccountingEntry& entry, entries)
611 nCreditDebit += entry.nCreditDebit;
616 void CWalletDB::ListAccountCreditDebit(const string& strAccount, list<CAccountingEntry>& entries)
618 int64 nCreditDebit = 0;
620 bool fAllAccounts = (strAccount == "*");
622 Dbc* pcursor = GetCursor();
624 throw runtime_error("CWalletDB::ListAccountCreditDebit() : cannot create DB cursor");
625 unsigned int fFlags = DB_SET_RANGE;
630 if (fFlags == DB_SET_RANGE)
631 ssKey << make_tuple(string("acentry"), (fAllAccounts? string("") : strAccount), uint64(0));
633 int ret = ReadAtCursor(pcursor, ssKey, ssValue, fFlags);
635 if (ret == DB_NOTFOUND)
640 throw runtime_error("CWalletDB::ListAccountCreditDebit() : error scanning DB");
646 if (strType != "acentry")
648 CAccountingEntry acentry;
649 ssKey >> acentry.strAccount;
650 if (!fAllAccounts && acentry.strAccount != strAccount)
654 entries.push_back(acentry);
661 bool CWalletDB::LoadWallet()
663 vchDefaultKey.clear();
664 int nFileVersion = 0;
665 vector<uint256> vWalletUpgrade;
669 // Tray icon sometimes disappears on 9.10 karmic koala 64-bit, leaving no way to access the program
670 fMinimizeToTray = false;
671 fMinimizeOnClose = false;
674 //// todo: shouldn't we catch exceptions and try to recover and continue?
675 CRITICAL_BLOCK(cs_mapWallet)
676 CRITICAL_BLOCK(cs_mapKeys)
679 Dbc* pcursor = GetCursor();
688 int ret = ReadAtCursor(pcursor, ssKey, ssValue);
689 if (ret == DB_NOTFOUND)
695 // Taking advantage of the fact that pair serialization
696 // is just the two items serialized one after the other
699 if (strType == "name")
703 ssValue >> mapAddressBook[strAddress];
705 else if (strType == "tx")
709 CWalletTx& wtx = mapWallet[hash];
712 if (wtx.GetHash() != hash)
713 printf("Error in wallet.dat, hash mismatch\n");
715 // Undo serialize changes in 31600
716 if (31404 <= wtx.fTimeReceivedIsTxTime && wtx.fTimeReceivedIsTxTime <= 31703)
718 if (!ssValue.empty())
722 ssValue >> fTmp >> fUnused >> wtx.strFromAccount;
723 printf("LoadWallet() upgrading tx ver=%d %d '%s' %s\n", wtx.fTimeReceivedIsTxTime, fTmp, wtx.strFromAccount.c_str(), hash.ToString().c_str());
724 wtx.fTimeReceivedIsTxTime = fTmp;
728 printf("LoadWallet() repairing tx ver=%d %s\n", wtx.fTimeReceivedIsTxTime, hash.ToString().c_str());
729 wtx.fTimeReceivedIsTxTime = 0;
731 vWalletUpgrade.push_back(hash);
735 //printf("LoadWallet %s\n", wtx.GetHash().ToString().c_str());
736 //printf(" %12I64d %s %s %s\n",
737 // wtx.vout[0].nValue,
738 // DateTimeStrFormat("%x %H:%M:%S", wtx.GetBlockTime()).c_str(),
739 // wtx.hashBlock.ToString().substr(0,20).c_str(),
740 // wtx.mapValue["message"].c_str());
742 else if (strType == "acentry")
748 if (nNumber > nAccountingEntryNumber)
749 nAccountingEntryNumber = nNumber;
751 else if (strType == "key" || strType == "wkey")
753 vector<unsigned char> vchPubKey;
756 if (strType == "key")
757 ssValue >> wkey.vchPrivKey;
761 mapKeys[vchPubKey] = wkey.vchPrivKey;
762 mapPubKeys[Hash160(vchPubKey)] = vchPubKey;
764 else if (strType == "defaultkey")
766 ssValue >> vchDefaultKey;
768 else if (strType == "pool")
772 setKeyPool.insert(nIndex);
774 else if (strType == "version")
776 ssValue >> nFileVersion;
777 if (nFileVersion == 10300)
780 else if (strType == "setting")
787 if (strKey == "fGenerateBitcoins") ssValue >> fGenerateBitcoins;
789 if (strKey == "nTransactionFee") ssValue >> nTransactionFee;
790 if (strKey == "addrIncoming") ssValue >> addrIncoming;
791 if (strKey == "fLimitProcessors") ssValue >> fLimitProcessors;
792 if (strKey == "nLimitProcessors") ssValue >> nLimitProcessors;
793 if (strKey == "fMinimizeToTray") ssValue >> fMinimizeToTray;
794 if (strKey == "fMinimizeOnClose") ssValue >> fMinimizeOnClose;
795 if (strKey == "fUseProxy") ssValue >> fUseProxy;
796 if (strKey == "addrProxy") ssValue >> addrProxy;
797 if (fHaveUPnP && strKey == "fUseUPnP") ssValue >> fUseUPnP;
803 BOOST_FOREACH(uint256 hash, vWalletUpgrade)
804 WriteTx(hash, mapWallet[hash]);
806 printf("nFileVersion = %d\n", nFileVersion);
807 printf("fGenerateBitcoins = %d\n", fGenerateBitcoins);
808 printf("nTransactionFee = %"PRI64d"\n", nTransactionFee);
809 printf("addrIncoming = %s\n", addrIncoming.ToString().c_str());
810 printf("fMinimizeToTray = %d\n", fMinimizeToTray);
811 printf("fMinimizeOnClose = %d\n", fMinimizeOnClose);
812 printf("fUseProxy = %d\n", fUseProxy);
813 printf("addrProxy = %s\n", addrProxy.ToString().c_str());
815 printf("fUseUPnP = %d\n", fUseUPnP);
819 if (nFileVersion < VERSION)
821 // Get rid of old debug.log file in current directory
822 if (nFileVersion <= 105 && !pszSetDataDir[0])
825 WriteVersion(VERSION);
832 bool LoadWallet(bool& fFirstRunRet)
834 fFirstRunRet = false;
835 if (!CWalletDB("cr+").LoadWallet())
837 fFirstRunRet = vchDefaultKey.empty();
839 if (mapKeys.count(vchDefaultKey))
842 keyUser.SetPubKey(vchDefaultKey);
843 keyUser.SetPrivKey(mapKeys[vchDefaultKey]);
847 // Create new keyUser and set as default key
848 RandAddSeedPerfmon();
849 keyUser.MakeNewKey();
850 if (!AddKey(keyUser))
852 if (!SetAddressBookName(PubKeyToAddress(keyUser.GetPubKey()), ""))
854 CWalletDB().WriteDefaultKey(keyUser.GetPubKey());
857 CreateThread(ThreadFlushWalletDB, NULL);
861 void ThreadFlushWalletDB(void* parg)
863 static bool fOneThread;
867 if (mapArgs.count("-noflushwallet"))
870 unsigned int nLastSeen = nWalletDBUpdated;
871 unsigned int nLastFlushed = nWalletDBUpdated;
872 int64 nLastWalletUpdate = GetTime();
877 if (nLastSeen != nWalletDBUpdated)
879 nLastSeen = nWalletDBUpdated;
880 nLastWalletUpdate = GetTime();
883 if (nLastFlushed != nWalletDBUpdated && GetTime() - nLastWalletUpdate >= 2)
885 TRY_CRITICAL_BLOCK(cs_db)
887 // Don't do this if any databases are in use
889 map<string, int>::iterator mi = mapFileUseCount.begin();
890 while (mi != mapFileUseCount.end())
892 nRefCount += (*mi).second;
896 if (nRefCount == 0 && !fShutdown)
898 string strFile = "wallet.dat";
899 map<string, int>::iterator mi = mapFileUseCount.find(strFile);
900 if (mi != mapFileUseCount.end())
902 printf("%s ", DateTimeStrFormat("%x %H:%M:%S", GetTime()).c_str());
903 printf("Flushing wallet.dat\n");
904 nLastFlushed = nWalletDBUpdated;
905 int64 nStart = GetTimeMillis();
907 // Flush wallet.dat so it's self contained
909 dbenv.txn_checkpoint(0, 0, 0);
910 dbenv.lsn_reset(strFile.c_str(), 0);
912 mapFileUseCount.erase(mi++);
913 printf("Flushed wallet.dat %"PRI64d"ms\n", GetTimeMillis() - nStart);
921 void BackupWallet(const string& strDest)
925 CRITICAL_BLOCK(cs_db)
927 const string strFile = "wallet.dat";
928 if (!mapFileUseCount.count(strFile) || mapFileUseCount[strFile] == 0)
930 // Flush log data to the dat file
932 dbenv.txn_checkpoint(0, 0, 0);
933 dbenv.lsn_reset(strFile.c_str(), 0);
934 mapFileUseCount.erase(strFile);
937 filesystem::path pathSrc(GetDataDir() + "/" + strFile);
938 filesystem::path pathDest(strDest);
939 if (filesystem::is_directory(pathDest))
940 pathDest = pathDest / strFile;
941 #if BOOST_VERSION >= 104000
942 filesystem::copy_file(pathSrc, pathDest, filesystem::copy_option::overwrite_if_exists);
944 filesystem::copy_file(pathSrc, pathDest);
946 printf("copied wallet.dat to %s\n", pathDest.string().c_str());
956 void CWalletDB::ReserveKeyFromKeyPool(int64& nIndex, CKeyPool& keypool)
959 keypool.vchPubKey.clear();
960 CRITICAL_BLOCK(cs_main)
961 CRITICAL_BLOCK(cs_mapWallet)
962 CRITICAL_BLOCK(cs_setKeyPool)
965 int64 nTargetSize = max(GetArg("-keypool", 100), (int64)0);
966 while (setKeyPool.size() < nTargetSize+1)
969 if (!setKeyPool.empty())
970 nEnd = *(--setKeyPool.end()) + 1;
971 if (!Write(make_pair(string("pool"), nEnd), CKeyPool(GenerateNewKey())))
972 throw runtime_error("ReserveKeyFromKeyPool() : writing generated key failed");
973 setKeyPool.insert(nEnd);
974 printf("keypool added key %"PRI64d", size=%d\n", nEnd, setKeyPool.size());
977 // Get the oldest key
978 assert(!setKeyPool.empty());
979 nIndex = *(setKeyPool.begin());
980 setKeyPool.erase(setKeyPool.begin());
981 if (!Read(make_pair(string("pool"), nIndex), keypool))
982 throw runtime_error("ReserveKeyFromKeyPool() : read failed");
983 if (!mapKeys.count(keypool.vchPubKey))
984 throw runtime_error("ReserveKeyFromKeyPool() : unknown key in key pool");
985 assert(!keypool.vchPubKey.empty());
986 printf("keypool reserve %"PRI64d"\n", nIndex);
990 void CWalletDB::KeepKey(int64 nIndex)
992 // Remove from key pool
993 CRITICAL_BLOCK(cs_main)
994 CRITICAL_BLOCK(cs_mapWallet)
996 Erase(make_pair(string("pool"), nIndex));
998 printf("keypool keep %"PRI64d"\n", nIndex);
1001 void CWalletDB::ReturnKey(int64 nIndex)
1003 // Return to key pool
1004 CRITICAL_BLOCK(cs_setKeyPool)
1005 setKeyPool.insert(nIndex);
1006 printf("keypool return %"PRI64d"\n", nIndex);
1009 vector<unsigned char> GetKeyFromKeyPool()
1014 walletdb.ReserveKeyFromKeyPool(nIndex, keypool);
1015 walletdb.KeepKey(nIndex);
1016 return keypool.vchPubKey;
1019 int64 GetOldestKeyPoolTime()
1024 walletdb.ReserveKeyFromKeyPool(nIndex, keypool);
1025 walletdb.ReturnKey(nIndex);
1026 return keypool.nTime;