Back to home page

Bitcoin sources

 
 

    


File indexing completed on 2020-06-25 15:50:39

0001 // Copyright (c) 2009-2010 Satoshi Nakamoto
0002 // Copyright (c) 2009-2012 The Bitcoin developers
0003 // Distributed under the MIT/X11 software license, see the accompanying
0004 // file license.txt or http://www.opensource.org/licenses/mit-license.php.
0005 
0006 #include "headers.h"
0007 #include "db.h"
0008 #include "net.h"
0009 #include <boost/filesystem.hpp>
0010 #include <boost/filesystem/fstream.hpp>
0011 
0012 // v0.5.4 RELEASE
0013 
0014 using namespace std;
0015 using namespace boost;
0016 
0017 
0018 unsigned int nWalletDBUpdated;
0019 uint64 nAccountingEntryNumber = 0;
0020 
0021 
0022 
0023 //
0024 // CDB
0025 //
0026 
0027 static CCriticalSection cs_db;
0028 static bool fDbEnvInit = false;
0029 DbEnv dbenv(0);
0030 static map<string, int> mapFileUseCount;
0031 static map<string, Db*> mapDb;
0032 
0033 static void EnvShutdown()
0034 {
0035     if (!fDbEnvInit)
0036         return;
0037 
0038     fDbEnvInit = false;
0039     try
0040     {
0041         dbenv.close(0);
0042     }
0043     catch (const DbException& e)
0044     {
0045         printf("EnvShutdown exception: %s (%d)\n", e.what(), e.get_errno());
0046     }
0047     DbEnv(0).remove(GetDataDir().c_str(), 0);
0048 }
0049 
0050 class CDBInit
0051 {
0052 public:
0053     CDBInit()
0054     {
0055     }
0056     ~CDBInit()
0057     {
0058         EnvShutdown();
0059     }
0060 }
0061 instance_of_cdbinit;
0062 
0063 
0064 CDB::CDB(const char* pszFile, const char* pszMode) : pdb(NULL)
0065 {
0066     int ret;
0067     if (pszFile == NULL)
0068         return;
0069 
0070     fReadOnly = (!strchr(pszMode, '+') && !strchr(pszMode, 'w'));
0071     bool fCreate = strchr(pszMode, 'c');
0072     unsigned int nFlags = DB_THREAD;
0073     if (fCreate)
0074         nFlags |= DB_CREATE;
0075 
0076     CRITICAL_BLOCK(cs_db)
0077     {
0078         if (!fDbEnvInit)
0079         {
0080             if (fShutdown)
0081                 return;
0082             string strDataDir = GetDataDir();
0083             string strLogDir = strDataDir + "/database";
0084             filesystem::create_directory(strLogDir.c_str());
0085             string strErrorFile = strDataDir + "/db.log";
0086             printf("dbenv.open strLogDir=%s strErrorFile=%s\n", strLogDir.c_str(), strErrorFile.c_str());
0087 
0088             dbenv.set_lg_dir(strLogDir.c_str());
0089             dbenv.set_lg_max(1000000);
0090             dbenv.set_lk_max_locks(2737000);
0091             dbenv.set_lk_max_objects(1119200);
0092             dbenv.set_lk_max_lockers(1119200);
0093             dbenv.set_errfile(fopen(strErrorFile.c_str(), "a")); /// debug
0094             dbenv.set_flags(DB_AUTO_COMMIT, 1);
0095             dbenv.set_flags(DB_TXN_WRITE_NOSYNC, 1);
0096             dbenv.log_set_config(DB_LOG_AUTO_REMOVE, 1);
0097             ret = dbenv.open(strDataDir.c_str(),
0098                              DB_CREATE     |
0099                              DB_INIT_LOCK  |
0100                              DB_INIT_LOG   |
0101                              DB_INIT_MPOOL |
0102                              DB_INIT_TXN   |
0103                              DB_THREAD     |
0104                              DB_RECOVER,
0105                              S_IRUSR | S_IWUSR);
0106             if (ret > 0)
0107                 throw runtime_error(strprintf("CDB() : error %d opening database environment", ret));
0108             fDbEnvInit = true;
0109         }
0110 
0111         strFile = pszFile;
0112         ++mapFileUseCount[strFile];
0113         pdb = mapDb[strFile];
0114         if (pdb == NULL)
0115         {
0116             pdb = new Db(&dbenv, 0);
0117 
0118             ret = pdb->open(NULL,      // Txn pointer
0119                             pszFile,   // Filename
0120                             "main",    // Logical db name
0121                             DB_BTREE,  // Database type
0122                             nFlags,    // Flags
0123                             0);
0124 
0125             if (ret > 0)
0126             {
0127                 delete pdb;
0128                 pdb = NULL;
0129                 CRITICAL_BLOCK(cs_db)
0130                     --mapFileUseCount[strFile];
0131                 strFile = "";
0132                 throw runtime_error(strprintf("CDB() : can't open database file %s, error %d", pszFile, ret));
0133             }
0134 
0135             if (fCreate && !Exists(string("version")))
0136             {
0137                 bool fTmp = fReadOnly;
0138                 fReadOnly = false;
0139                 WriteVersion(VERSION);
0140                 fReadOnly = fTmp;
0141             }
0142 
0143             mapDb[strFile] = pdb;
0144         }
0145     }
0146 }
0147 
0148 void CDB::Close()
0149 {
0150     if (!pdb)
0151         return;
0152     if (!vTxn.empty())
0153         vTxn.front()->abort();
0154     vTxn.clear();
0155     pdb = NULL;
0156 
0157     // Flush database activity from memory pool to disk log
0158     unsigned int nMinutes = 0;
0159     if (fReadOnly)
0160         nMinutes = 1;
0161     if (strFile == "addr.dat")
0162         nMinutes = 2;
0163     if (strFile == "blkindex.dat" && IsInitialBlockDownload() && nBestHeight % 500 != 0)
0164         nMinutes = 1;
0165     dbenv.txn_checkpoint(0, nMinutes, 0);
0166 
0167     CRITICAL_BLOCK(cs_db)
0168         --mapFileUseCount[strFile];
0169 }
0170 
0171 void static CloseDb(const string& strFile)
0172 {
0173     CRITICAL_BLOCK(cs_db)
0174     {
0175         if (mapDb[strFile] != NULL)
0176         {
0177             // Close the database handle
0178             Db* pdb = mapDb[strFile];
0179             pdb->close(0);
0180             delete pdb;
0181             mapDb[strFile] = NULL;
0182         }
0183     }
0184 }
0185 
0186 bool CDB::Rewrite(const string& strFile, const char* pszSkip)
0187 {
0188     while (!fShutdown)
0189     {
0190         CRITICAL_BLOCK(cs_db)
0191         {
0192             if (!mapFileUseCount.count(strFile) || mapFileUseCount[strFile] == 0)
0193             {
0194                 // Flush log data to the dat file
0195                 CloseDb(strFile);
0196                 dbenv.txn_checkpoint(0, 0, 0);
0197                 dbenv.lsn_reset(strFile.c_str(), 0);
0198                 mapFileUseCount.erase(strFile);
0199 
0200                 bool fSuccess = true;
0201                 printf("Rewriting %s...\n", strFile.c_str());
0202                 string strFileRes = strFile + ".rewrite";
0203                 { // surround usage of db with extra {}
0204                     CDB db(strFile.c_str(), "r");
0205                     Db* pdbCopy = new Db(&dbenv, 0);
0206     
0207                     int ret = pdbCopy->open(NULL,                 // Txn pointer
0208                                             strFileRes.c_str(),   // Filename
0209                                             "main",    // Logical db name
0210                                             DB_BTREE,  // Database type
0211                                             DB_CREATE,    // Flags
0212                                             0);
0213                     if (ret > 0)
0214                     {
0215                         printf("Cannot create database file %s\n", strFileRes.c_str());
0216                         fSuccess = false;
0217                     }
0218     
0219                     Dbc* pcursor = db.GetCursor();
0220                     if (pcursor)
0221                         while (fSuccess)
0222                         {
0223                             CDataStream ssKey;
0224                             CDataStream ssValue;
0225                             int ret = db.ReadAtCursor(pcursor, ssKey, ssValue, DB_NEXT);
0226                             if (ret == DB_NOTFOUND)
0227                             {
0228                                 pcursor->close();
0229                                 break;
0230                             }
0231                             else if (ret != 0)
0232                             {
0233                                 pcursor->close();
0234                                 fSuccess = false;
0235                                 break;
0236                             }
0237                             if (pszSkip &&
0238                                 strncmp(&ssKey[0], pszSkip, std::min(ssKey.size(), strlen(pszSkip))) == 0)
0239                                 continue;
0240                             if (strncmp(&ssKey[0], "\x07version", 8) == 0)
0241                             {
0242                                 // Update version:
0243                                 ssValue.clear();
0244                                 ssValue << VERSION;
0245                             }
0246                             Dbt datKey(&ssKey[0], ssKey.size());
0247                             Dbt datValue(&ssValue[0], ssValue.size());
0248                             int ret2 = pdbCopy->put(NULL, &datKey, &datValue, DB_NOOVERWRITE);
0249                             if (ret2 > 0)
0250                                 fSuccess = false;
0251                         }
0252                     if (fSuccess)
0253                     {
0254                         db.Close();
0255                         CloseDb(strFile);
0256                         if (pdbCopy->close(0))
0257                             fSuccess = false;
0258                         delete pdbCopy;
0259                     }
0260                 }
0261                 if (fSuccess)
0262                 {
0263                     Db dbA(&dbenv, 0);
0264                     if (dbA.remove(strFile.c_str(), NULL, 0))
0265                         fSuccess = false;
0266                     Db dbB(&dbenv, 0);
0267                     if (dbB.rename(strFileRes.c_str(), NULL, strFile.c_str(), 0))
0268                         fSuccess = false;
0269                 }
0270                 if (!fSuccess)
0271                     printf("Rewriting of %s FAILED!\n", strFileRes.c_str());
0272                 return fSuccess;
0273             }
0274         }
0275         Sleep(100);
0276     }
0277     return false;
0278 }
0279 
0280 
0281 void DBFlush(bool fShutdown)
0282 {
0283     // Flush log data to the actual data file
0284     //  on all files that are not in use
0285     printf("DBFlush(%s)%s\n", fShutdown ? "true" : "false", fDbEnvInit ? "" : " db not started");
0286     if (!fDbEnvInit)
0287         return;
0288     CRITICAL_BLOCK(cs_db)
0289     {
0290         map<string, int>::iterator mi = mapFileUseCount.begin();
0291         while (mi != mapFileUseCount.end())
0292         {
0293             string strFile = (*mi).first;
0294             int nRefCount = (*mi).second;
0295             printf("%s refcount=%d\n", strFile.c_str(), nRefCount);
0296             if (nRefCount == 0)
0297             {
0298                 // Move log data to the dat file
0299                 CloseDb(strFile);
0300                 dbenv.txn_checkpoint(0, 0, 0);
0301                 printf("%s flush\n", strFile.c_str());
0302                 dbenv.lsn_reset(strFile.c_str(), 0);
0303                 mapFileUseCount.erase(mi++);
0304             }
0305             else
0306                 mi++;
0307         }
0308         if (fShutdown)
0309         {
0310             char** listp;
0311             if (mapFileUseCount.empty())
0312             {
0313                 dbenv.log_archive(&listp, DB_ARCH_REMOVE);
0314                 EnvShutdown();
0315             }
0316         }
0317     }
0318 }
0319 
0320 
0321 
0322 
0323 
0324 
0325 //
0326 // CTxDB
0327 //
0328 
0329 bool CTxDB::ReadTxIndex(uint256 hash, CTxIndex& txindex)
0330 {
0331     assert(!fClient);
0332     txindex.SetNull();
0333     return Read(make_pair(string("tx"), hash), txindex);
0334 }
0335 
0336 bool CTxDB::UpdateTxIndex(uint256 hash, const CTxIndex& txindex)
0337 {
0338     assert(!fClient);
0339     return Write(make_pair(string("tx"), hash), txindex);
0340 }
0341 
0342 bool CTxDB::AddTxIndex(const CTransaction& tx, const CDiskTxPos& pos, int nHeight)
0343 {
0344     assert(!fClient);
0345 
0346     // Add to tx index
0347     uint256 hash = tx.GetHash();
0348     CTxIndex txindex(pos, tx.vout.size());
0349     return Write(make_pair(string("tx"), hash), txindex);
0350 }
0351 
0352 bool CTxDB::EraseTxIndex(const CTransaction& tx)
0353 {
0354     assert(!fClient);
0355     uint256 hash = tx.GetHash();
0356 
0357     return Erase(make_pair(string("tx"), hash));
0358 }
0359 
0360 bool CTxDB::ContainsTx(uint256 hash)
0361 {
0362     assert(!fClient);
0363     return Exists(make_pair(string("tx"), hash));
0364 }
0365 
0366 bool CTxDB::ReadOwnerTxes(uint160 hash160, int nMinHeight, vector<CTransaction>& vtx)
0367 {
0368     assert(!fClient);
0369     vtx.clear();
0370 
0371     // Get cursor
0372     Dbc* pcursor = GetCursor();
0373     if (!pcursor)
0374         return false;
0375 
0376     unsigned int fFlags = DB_SET_RANGE;
0377     loop
0378     {
0379         // Read next record
0380         CDataStream ssKey;
0381         if (fFlags == DB_SET_RANGE)
0382             ssKey << string("owner") << hash160 << CDiskTxPos(0, 0, 0);
0383         CDataStream ssValue;
0384         int ret = ReadAtCursor(pcursor, ssKey, ssValue, fFlags);
0385         fFlags = DB_NEXT;
0386         if (ret == DB_NOTFOUND)
0387             break;
0388         else if (ret != 0)
0389         {
0390             pcursor->close();
0391             return false;
0392         }
0393 
0394         // Unserialize
0395         string strType;
0396         uint160 hashItem;
0397         CDiskTxPos pos;
0398         ssKey >> strType >> hashItem >> pos;
0399         int nItemHeight;
0400         ssValue >> nItemHeight;
0401 
0402         // Read transaction
0403         if (strType != "owner" || hashItem != hash160)
0404             break;
0405         if (nItemHeight >= nMinHeight)
0406         {
0407             vtx.resize(vtx.size()+1);
0408             if (!vtx.back().ReadFromDisk(pos))
0409             {
0410                 pcursor->close();
0411                 return false;
0412             }
0413         }
0414     }
0415 
0416     pcursor->close();
0417     return true;
0418 }
0419 
0420 bool CTxDB::ReadDiskTx(uint256 hash, CTransaction& tx, CTxIndex& txindex)
0421 {
0422     assert(!fClient);
0423     tx.SetNull();
0424     if (!ReadTxIndex(hash, txindex))
0425         return false;
0426     return (tx.ReadFromDisk(txindex.pos));
0427 }
0428 
0429 bool CTxDB::ReadDiskTx(uint256 hash, CTransaction& tx)
0430 {
0431     CTxIndex txindex;
0432     return ReadDiskTx(hash, tx, txindex);
0433 }
0434 
0435 bool CTxDB::ReadDiskTx(COutPoint outpoint, CTransaction& tx, CTxIndex& txindex)
0436 {
0437     return ReadDiskTx(outpoint.hash, tx, txindex);
0438 }
0439 
0440 bool CTxDB::ReadDiskTx(COutPoint outpoint, CTransaction& tx)
0441 {
0442     CTxIndex txindex;
0443     return ReadDiskTx(outpoint.hash, tx, txindex);
0444 }
0445 
0446 bool CTxDB::WriteBlockIndex(const CDiskBlockIndex& blockindex)
0447 {
0448     return Write(make_pair(string("blockindex"), blockindex.GetBlockHash()), blockindex);
0449 }
0450 
0451 bool CTxDB::EraseBlockIndex(uint256 hash)
0452 {
0453     return Erase(make_pair(string("blockindex"), hash));
0454 }
0455 
0456 bool CTxDB::ReadHashBestChain(uint256& hashBestChain)
0457 {
0458     return Read(string("hashBestChain"), hashBestChain);
0459 }
0460 
0461 bool CTxDB::WriteHashBestChain(uint256 hashBestChain)
0462 {
0463     return Write(string("hashBestChain"), hashBestChain);
0464 }
0465 
0466 bool CTxDB::ReadBestInvalidWork(CBigNum& bnBestInvalidWork)
0467 {
0468     return Read(string("bnBestInvalidWork"), bnBestInvalidWork);
0469 }
0470 
0471 bool CTxDB::WriteBestInvalidWork(CBigNum bnBestInvalidWork)
0472 {
0473     return Write(string("bnBestInvalidWork"), bnBestInvalidWork);
0474 }
0475 
0476 CBlockIndex static * InsertBlockIndex(uint256 hash)
0477 {
0478     if (hash == 0)
0479         return NULL;
0480 
0481     // Return existing
0482     map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.find(hash);
0483     if (mi != mapBlockIndex.end())
0484         return (*mi).second;
0485 
0486     // Create new
0487     CBlockIndex* pindexNew = new CBlockIndex();
0488     if (!pindexNew)
0489         throw runtime_error("LoadBlockIndex() : new CBlockIndex failed");
0490     mi = mapBlockIndex.insert(make_pair(hash, pindexNew)).first;
0491     pindexNew->phashBlock = &((*mi).first);
0492 
0493     return pindexNew;
0494 }
0495 
0496 bool CTxDB::LoadBlockIndex()
0497 {
0498     // Get database cursor
0499     Dbc* pcursor = GetCursor();
0500     if (!pcursor)
0501         return false;
0502 
0503     // Load mapBlockIndex
0504     unsigned int fFlags = DB_SET_RANGE;
0505     loop
0506     {
0507         // Read next record
0508         CDataStream ssKey;
0509         if (fFlags == DB_SET_RANGE)
0510             ssKey << make_pair(string("blockindex"), uint256(0));
0511         CDataStream ssValue;
0512         int ret = ReadAtCursor(pcursor, ssKey, ssValue, fFlags);
0513         fFlags = DB_NEXT;
0514         if (ret == DB_NOTFOUND)
0515             break;
0516         else if (ret != 0)
0517             return false;
0518 
0519         // Unserialize
0520         string strType;
0521         ssKey >> strType;
0522         if (strType == "blockindex")
0523         {
0524             CDiskBlockIndex diskindex;
0525             ssValue >> diskindex;
0526 
0527             // Construct block index object
0528             CBlockIndex* pindexNew = InsertBlockIndex(diskindex.GetBlockHash());
0529             pindexNew->pprev          = InsertBlockIndex(diskindex.hashPrev);
0530             pindexNew->pnext          = InsertBlockIndex(diskindex.hashNext);
0531             pindexNew->nFile          = diskindex.nFile;
0532             pindexNew->nBlockPos      = diskindex.nBlockPos;
0533             pindexNew->nHeight        = diskindex.nHeight;
0534             pindexNew->nVersion       = diskindex.nVersion;
0535             pindexNew->hashMerkleRoot = diskindex.hashMerkleRoot;
0536             pindexNew->nTime          = diskindex.nTime;
0537             pindexNew->nBits          = diskindex.nBits;
0538             pindexNew->nNonce         = diskindex.nNonce;
0539 
0540             // Watch for genesis block
0541             if (pindexGenesisBlock == NULL && diskindex.GetBlockHash() == hashGenesisBlock)
0542                 pindexGenesisBlock = pindexNew;
0543 
0544             if (!pindexNew->CheckIndex())
0545                 return error("LoadBlockIndex() : CheckIndex failed at %d", pindexNew->nHeight);
0546         }
0547         else
0548         {
0549             break;
0550         }
0551     }
0552     pcursor->close();
0553 
0554     // Calculate bnChainWork
0555     vector<pair<int, CBlockIndex*> > vSortedByHeight;
0556     vSortedByHeight.reserve(mapBlockIndex.size());
0557     BOOST_FOREACH(const PAIRTYPE(uint256, CBlockIndex*)& item, mapBlockIndex)
0558     {
0559         CBlockIndex* pindex = item.second;
0560         vSortedByHeight.push_back(make_pair(pindex->nHeight, pindex));
0561     }
0562     sort(vSortedByHeight.begin(), vSortedByHeight.end());
0563     BOOST_FOREACH(const PAIRTYPE(int, CBlockIndex*)& item, vSortedByHeight)
0564     {
0565         CBlockIndex* pindex = item.second;
0566         pindex->bnChainWork = (pindex->pprev ? pindex->pprev->bnChainWork : 0) + pindex->GetBlockWork();
0567     }
0568 
0569     // Load hashBestChain pointer to end of best chain
0570     if (!ReadHashBestChain(hashBestChain))
0571     {
0572         if (pindexGenesisBlock == NULL)
0573             return true;
0574         return error("CTxDB::LoadBlockIndex() : hashBestChain not loaded");
0575     }
0576     if (!mapBlockIndex.count(hashBestChain))
0577         return error("CTxDB::LoadBlockIndex() : hashBestChain not found in the block index");
0578     pindexBest = mapBlockIndex[hashBestChain];
0579     nBestHeight = pindexBest->nHeight;
0580     bnBestChainWork = pindexBest->bnChainWork;
0581     printf("LoadBlockIndex(): hashBestChain=%s  height=%d\n", hashBestChain.ToString().substr(0,20).c_str(), nBestHeight);
0582 
0583     // Load bnBestInvalidWork, OK if it doesn't exist
0584     ReadBestInvalidWork(bnBestInvalidWork);
0585 
0586     // Verify blocks in the best chain
0587     CBlockIndex* pindexFork = NULL;
0588     for (CBlockIndex* pindex = pindexBest; pindex && pindex->pprev; pindex = pindex->pprev)
0589     {
0590         if (pindex->nHeight < nBestHeight-2500 && !mapArgs.count("-checkblocks"))
0591             break;
0592         CBlock block;
0593         if (!block.ReadFromDisk(pindex))
0594             return error("LoadBlockIndex() : block.ReadFromDisk failed");
0595         if (!block.CheckBlock())
0596         {
0597             printf("LoadBlockIndex() : *** found bad block at %d, hash=%s\n", pindex->nHeight, pindex->GetBlockHash().ToString().c_str());
0598             pindexFork = pindex->pprev;
0599         }
0600     }
0601     if (pindexFork)
0602     {
0603         // Reorg back to the fork
0604         printf("LoadBlockIndex() : *** moving best chain pointer back to block %d\n", pindexFork->nHeight);
0605         CBlock block;
0606         if (!block.ReadFromDisk(pindexFork))
0607             return error("LoadBlockIndex() : block.ReadFromDisk failed");
0608         CTxDB txdb;
0609         block.SetBestChain(txdb, pindexFork);
0610     }
0611 
0612     return true;
0613 }
0614 
0615 
0616 
0617 
0618 
0619 //
0620 // CAddrDB
0621 //
0622 
0623 bool CAddrDB::WriteAddress(const CAddress& addr)
0624 {
0625     return Write(make_pair(string("addr"), addr.GetKey()), addr);
0626 }
0627 
0628 bool CAddrDB::EraseAddress(const CAddress& addr)
0629 {
0630     return Erase(make_pair(string("addr"), addr.GetKey()));
0631 }
0632 
0633 bool CAddrDB::LoadAddresses()
0634 {
0635     CRITICAL_BLOCK(cs_mapAddresses)
0636     {
0637         // Get cursor
0638         Dbc* pcursor = GetCursor();
0639         if (!pcursor)
0640             return false;
0641 
0642         loop
0643         {
0644             // Read next record
0645             CDataStream ssKey;
0646             CDataStream ssValue;
0647             int ret = ReadAtCursor(pcursor, ssKey, ssValue);
0648             if (ret == DB_NOTFOUND)
0649                 break;
0650             else if (ret != 0)
0651                 return false;
0652 
0653             // Unserialize
0654             string strType;
0655             ssKey >> strType;
0656             if (strType == "addr")
0657             {
0658                 CAddress addr;
0659                 ssValue >> addr;
0660                 mapAddresses.insert(make_pair(addr.GetKey(), addr));
0661             }
0662         }
0663         pcursor->close();
0664 
0665         printf("Loaded %d addresses\n", mapAddresses.size());
0666     }
0667 
0668     return true;
0669 }
0670 
0671 bool LoadAddresses()
0672 {
0673     return CAddrDB("cr+").LoadAddresses();
0674 }
0675 
0676 
0677 
0678 
0679 //
0680 // CWalletDB
0681 //
0682 
0683 bool CWalletDB::WriteName(const string& strAddress, const string& strName)
0684 {
0685     nWalletDBUpdated++;
0686     return Write(make_pair(string("name"), strAddress), strName);
0687 }
0688 
0689 bool CWalletDB::EraseName(const string& strAddress)
0690 {
0691     // This should only be used for sending addresses, never for receiving addresses,
0692     // receiving addresses must always have an address book entry if they're not change return.
0693     nWalletDBUpdated++;
0694     return Erase(make_pair(string("name"), strAddress));
0695 }
0696 
0697 bool CWalletDB::ReadAccount(const string& strAccount, CAccount& account)
0698 {
0699     account.SetNull();
0700     return Read(make_pair(string("acc"), strAccount), account);
0701 }
0702 
0703 bool CWalletDB::WriteAccount(const string& strAccount, const CAccount& account)
0704 {
0705     return Write(make_pair(string("acc"), strAccount), account);
0706 }
0707 
0708 bool CWalletDB::WriteAccountingEntry(const CAccountingEntry& acentry)
0709 {
0710     return Write(boost::make_tuple(string("acentry"), acentry.strAccount, ++nAccountingEntryNumber), acentry);
0711 }
0712 
0713 int64 CWalletDB::GetAccountCreditDebit(const string& strAccount)
0714 {
0715     list<CAccountingEntry> entries;
0716     ListAccountCreditDebit(strAccount, entries);
0717 
0718     int64 nCreditDebit = 0;
0719     BOOST_FOREACH (const CAccountingEntry& entry, entries)
0720         nCreditDebit += entry.nCreditDebit;
0721 
0722     return nCreditDebit;
0723 }
0724 
0725 void CWalletDB::ListAccountCreditDebit(const string& strAccount, list<CAccountingEntry>& entries)
0726 {
0727     bool fAllAccounts = (strAccount == "*");
0728 
0729     Dbc* pcursor = GetCursor();
0730     if (!pcursor)
0731         throw runtime_error("CWalletDB::ListAccountCreditDebit() : cannot create DB cursor");
0732     unsigned int fFlags = DB_SET_RANGE;
0733     loop
0734     {
0735         // Read next record
0736         CDataStream ssKey;
0737         if (fFlags == DB_SET_RANGE)
0738             ssKey << boost::make_tuple(string("acentry"), (fAllAccounts? string("") : strAccount), uint64(0));
0739         CDataStream ssValue;
0740         int ret = ReadAtCursor(pcursor, ssKey, ssValue, fFlags);
0741         fFlags = DB_NEXT;
0742         if (ret == DB_NOTFOUND)
0743             break;
0744         else if (ret != 0)
0745         {
0746             pcursor->close();
0747             throw runtime_error("CWalletDB::ListAccountCreditDebit() : error scanning DB");
0748         }
0749 
0750         // Unserialize
0751         string strType;
0752         ssKey >> strType;
0753         if (strType != "acentry")
0754             break;
0755         CAccountingEntry acentry;
0756         ssKey >> acentry.strAccount;
0757         if (!fAllAccounts && acentry.strAccount != strAccount)
0758             break;
0759 
0760         ssValue >> acentry;
0761         entries.push_back(acentry);
0762     }
0763 
0764     pcursor->close();
0765 }
0766 
0767 
0768 int CWalletDB::LoadWallet(CWallet* pwallet)
0769 {
0770     pwallet->vchDefaultKey.clear();
0771     int nFileVersion = 0;
0772     vector<uint256> vWalletUpgrade;
0773     bool fIsEncrypted = false;
0774 
0775     // Modify defaults
0776     // Tray icon sometimes disappears on 9.10 karmic koala 64-bit, leaving no way to access the program
0777     fMinimizeToTray = false;
0778     fMinimizeOnClose = false;
0779 
0780     //// todo: shouldn't we catch exceptions and try to recover and continue?
0781     CRITICAL_BLOCK(pwallet->cs_wallet)
0782     {
0783         // Get cursor
0784         Dbc* pcursor = GetCursor();
0785         if (!pcursor)
0786             return DB_CORRUPT;
0787 
0788         loop
0789         {
0790             // Read next record
0791             CDataStream ssKey;
0792             CDataStream ssValue;
0793             int ret = ReadAtCursor(pcursor, ssKey, ssValue);
0794             if (ret == DB_NOTFOUND)
0795                 break;
0796             else if (ret != 0)
0797                 return DB_CORRUPT;
0798 
0799             // Unserialize
0800             // Taking advantage of the fact that pair serialization
0801             // is just the two items serialized one after the other
0802             string strType;
0803             ssKey >> strType;
0804             if (strType == "name")
0805             {
0806                 string strAddress;
0807                 ssKey >> strAddress;
0808                 ssValue >> pwallet->mapAddressBook[strAddress];
0809             }
0810             else if (strType == "tx")
0811             {
0812                 uint256 hash;
0813                 ssKey >> hash;
0814                 CWalletTx& wtx = pwallet->mapWallet[hash];
0815                 ssValue >> wtx;
0816                 wtx.pwallet = pwallet;
0817 
0818                 if (wtx.GetHash() != hash)
0819                     printf("Error in wallet.dat, hash mismatch\n");
0820 
0821                 // Undo serialize changes in 31600
0822                 if (31404 <= wtx.fTimeReceivedIsTxTime && wtx.fTimeReceivedIsTxTime <= 31703)
0823                 {
0824                     if (!ssValue.empty())
0825                     {
0826                         char fTmp;
0827                         char fUnused;
0828                         ssValue >> fTmp >> fUnused >> wtx.strFromAccount;
0829                         printf("LoadWallet() upgrading tx ver=%d %d '%s' %s\n", wtx.fTimeReceivedIsTxTime, fTmp, wtx.strFromAccount.c_str(), hash.ToString().c_str());
0830                         wtx.fTimeReceivedIsTxTime = fTmp;
0831                     }
0832                     else
0833                     {
0834                         printf("LoadWallet() repairing tx ver=%d %s\n", wtx.fTimeReceivedIsTxTime, hash.ToString().c_str());
0835                         wtx.fTimeReceivedIsTxTime = 0;
0836                     }
0837                     vWalletUpgrade.push_back(hash);
0838                 }
0839 
0840                 //// debug print
0841                 //printf("LoadWallet  %s\n", wtx.GetHash().ToString().c_str());
0842                 //printf(" %12I64d  %s  %s  %s\n",
0843                 //    wtx.vout[0].nValue,
0844                 //    DateTimeStrFormat("%x %H:%M:%S", wtx.GetBlockTime()).c_str(),
0845                 //    wtx.hashBlock.ToString().substr(0,20).c_str(),
0846                 //    wtx.mapValue["message"].c_str());
0847             }
0848             else if (strType == "acentry")
0849             {
0850                 string strAccount;
0851                 ssKey >> strAccount;
0852                 uint64 nNumber;
0853                 ssKey >> nNumber;
0854                 if (nNumber > nAccountingEntryNumber)
0855                     nAccountingEntryNumber = nNumber;
0856             }
0857             else if (strType == "key" || strType == "wkey")
0858             {
0859                 vector<unsigned char> vchPubKey;
0860                 ssKey >> vchPubKey;
0861                 CKey key;
0862                 if (strType == "key")
0863                 {
0864                     CPrivKey pkey;
0865                     ssValue >> pkey;
0866                     key.SetPrivKey(pkey);
0867                     if (key.GetPubKey() != vchPubKey || !key.IsValid())
0868                         return DB_CORRUPT;
0869                 }
0870                 else
0871                 {
0872                     CWalletKey wkey;
0873                     ssValue >> wkey;
0874                     key.SetPrivKey(wkey.vchPrivKey);
0875                     if (key.GetPubKey() != vchPubKey || !key.IsValid())
0876                         return DB_CORRUPT;
0877                 }
0878                 if (!pwallet->LoadKey(key))
0879                     return DB_CORRUPT;
0880             }
0881             else if (strType == "mkey")
0882             {
0883                 unsigned int nID;
0884                 ssKey >> nID;
0885                 CMasterKey kMasterKey;
0886                 ssValue >> kMasterKey;
0887                 if(pwallet->mapMasterKeys.count(nID) != 0)
0888                     return DB_CORRUPT;
0889                 pwallet->mapMasterKeys[nID] = kMasterKey;
0890                 if (pwallet->nMasterKeyMaxID < nID)
0891                     pwallet->nMasterKeyMaxID = nID;
0892             }
0893             else if (strType == "ckey")
0894             {
0895                 vector<unsigned char> vchPubKey;
0896                 ssKey >> vchPubKey;
0897                 vector<unsigned char> vchPrivKey;
0898                 ssValue >> vchPrivKey;
0899                 if (!pwallet->LoadCryptedKey(vchPubKey, vchPrivKey))
0900                     return DB_CORRUPT;
0901                 fIsEncrypted = true;
0902             }
0903             else if (strType == "defaultkey")
0904             {
0905                 ssValue >> pwallet->vchDefaultKey;
0906             }
0907             else if (strType == "pool")
0908             {
0909                 int64 nIndex;
0910                 ssKey >> nIndex;
0911                 pwallet->setKeyPool.insert(nIndex);
0912             }
0913             else if (strType == "version")
0914             {
0915                 ssValue >> nFileVersion;
0916                 if (nFileVersion == 10300)
0917                     nFileVersion = 300;
0918             }
0919             else if (strType == "setting")
0920             {
0921                 string strKey;
0922                 ssKey >> strKey;
0923 
0924                 // Options
0925                 if (strKey == "fGenerateBitcoins")  ssValue >> fGenerateBitcoins;
0926                 if (strKey == "nTransactionFee")    ssValue >> nTransactionFee;
0927                 if (strKey == "fLimitProcessors")   ssValue >> fLimitProcessors;
0928                 if (strKey == "nLimitProcessors")   ssValue >> nLimitProcessors;
0929                 if (strKey == "fMinimizeToTray")    ssValue >> fMinimizeToTray;
0930                 if (strKey == "fMinimizeOnClose")   ssValue >> fMinimizeOnClose;
0931                 if (strKey == "fUseProxy")          ssValue >> fUseProxy;
0932                 if (strKey == "addrProxy")          ssValue >> addrProxy;
0933             }
0934             else if (strType == "minversion")
0935             {
0936                 int nMinVersion = 0;
0937                 ssValue >> nMinVersion;
0938                 if (nMinVersion > VERSION)
0939                     return DB_TOO_NEW;
0940             }
0941         }
0942         pcursor->close();
0943     }
0944 
0945     BOOST_FOREACH(uint256 hash, vWalletUpgrade)
0946         WriteTx(hash, pwallet->mapWallet[hash]);
0947 
0948     printf("nFileVersion = %d\n", nFileVersion);
0949     printf("fGenerateBitcoins = %d\n", fGenerateBitcoins);
0950     printf("nTransactionFee = %"PRI64d"\n", nTransactionFee);
0951     printf("fMinimizeToTray = %d\n", fMinimizeToTray);
0952     printf("fMinimizeOnClose = %d\n", fMinimizeOnClose);
0953     printf("fUseProxy = %d\n", fUseProxy);
0954     printf("addrProxy = %s\n", addrProxy.ToString().c_str());
0955 
0956     // Rewrite encrypted wallets of versions 0.4.0 and 0.5.0rc:
0957     if (fIsEncrypted && (nFileVersion == 40000 || nFileVersion == 50000))
0958         return DB_NEED_REWRITE;
0959 
0960     if (nFileVersion < VERSION) // Update
0961     {
0962         // Get rid of old debug.log file in current directory
0963         if (nFileVersion <= 105 && !pszSetDataDir[0])
0964             unlink("debug.log");
0965 
0966         WriteVersion(VERSION);
0967     }
0968 
0969     return DB_LOAD_OK;
0970 }
0971 
0972 void ThreadFlushWalletDB(void* parg)
0973 {
0974     const string& strFile = ((const string*)parg)[0];
0975     static bool fOneThread;
0976     if (fOneThread)
0977         return;
0978     fOneThread = true;
0979     if (mapArgs.count("-noflushwallet"))
0980         return;
0981 
0982     unsigned int nLastSeen = nWalletDBUpdated;
0983     unsigned int nLastFlushed = nWalletDBUpdated;
0984     int64 nLastWalletUpdate = GetTime();
0985     while (!fShutdown)
0986     {
0987         Sleep(500);
0988 
0989         if (nLastSeen != nWalletDBUpdated)
0990         {
0991             nLastSeen = nWalletDBUpdated;
0992             nLastWalletUpdate = GetTime();
0993         }
0994 
0995         if (nLastFlushed != nWalletDBUpdated && GetTime() - nLastWalletUpdate >= 2)
0996         {
0997             TRY_CRITICAL_BLOCK(cs_db)
0998             {
0999                 // Don't do this if any databases are in use
1000                 int nRefCount = 0;
1001                 map<string, int>::iterator mi = mapFileUseCount.begin();
1002                 while (mi != mapFileUseCount.end())
1003                 {
1004                     nRefCount += (*mi).second;
1005                     mi++;
1006                 }
1007 
1008                 if (nRefCount == 0 && !fShutdown)
1009                 {
1010                     map<string, int>::iterator mi = mapFileUseCount.find(strFile);
1011                     if (mi != mapFileUseCount.end())
1012                     {
1013                         printf("%s ", DateTimeStrFormat("%x %H:%M:%S", GetTime()).c_str());
1014                         printf("Flushing wallet.dat\n");
1015                         nLastFlushed = nWalletDBUpdated;
1016                         int64 nStart = GetTimeMillis();
1017 
1018                         // Flush wallet.dat so it's self contained
1019                         CloseDb(strFile);
1020                         dbenv.txn_checkpoint(0, 0, 0);
1021                         dbenv.lsn_reset(strFile.c_str(), 0);
1022 
1023                         mapFileUseCount.erase(mi++);
1024                         printf("Flushed wallet.dat %"PRI64d"ms\n", GetTimeMillis() - nStart);
1025                     }
1026                 }
1027             }
1028         }
1029     }
1030 }
1031 
1032 bool BackupWallet(const CWallet& wallet, const string& strDest)
1033 {
1034     if (!wallet.fFileBacked)
1035         return false;
1036     while (!fShutdown)
1037     {
1038         CRITICAL_BLOCK(cs_db)
1039         {
1040             if (!mapFileUseCount.count(wallet.strWalletFile) || mapFileUseCount[wallet.strWalletFile] == 0)
1041             {
1042                 // Flush log data to the dat file
1043                 CloseDb(wallet.strWalletFile);
1044                 dbenv.txn_checkpoint(0, 0, 0);
1045                 dbenv.lsn_reset(wallet.strWalletFile.c_str(), 0);
1046                 mapFileUseCount.erase(wallet.strWalletFile);
1047 
1048                 // Copy wallet.dat
1049                 filesystem::path pathSrc(GetDataDir() + "/" + wallet.strWalletFile);
1050                 filesystem::path pathDest(strDest);
1051                 if (filesystem::is_directory(pathDest))
1052                     pathDest = pathDest / wallet.strWalletFile;
1053 #if BOOST_VERSION >= 104000
1054                 filesystem::copy_file(pathSrc, pathDest, filesystem::copy_option::overwrite_if_exists);
1055 #else
1056                 filesystem::copy_file(pathSrc, pathDest);
1057 #endif
1058                 printf("copied wallet.dat to %s\n", pathDest.string().c_str());
1059 
1060                 return true;
1061             }
1062         }
1063         Sleep(100);
1064     }
1065     return false;
1066 }