You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

txdb.cpp 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. // Copyright (c) 2009-2010 Satoshi Nakamoto
  2. // Copyright (c) 2009-2016 The Bitcoin Core developers
  3. // Distributed under the MIT software license, see the accompanying
  4. // file COPYING or http://www.opensource.org/licenses/mit-license.php.
  5. #include "txdb.h"
  6. #include "chainparams.h"
  7. #include "hash.h"
  8. #include "pow.h"
  9. #include "uint256.h"
  10. #include <stdint.h>
  11. #include <boost/thread.hpp>
  12. static const char DB_COINS = 'c';
  13. static const char DB_BLOCK_FILES = 'f';
  14. static const char DB_TXINDEX = 't';
  15. static const char DB_BLOCK_INDEX = 'b';
  16. static const char DB_BEST_BLOCK = 'B';
  17. static const char DB_FLAG = 'F';
  18. static const char DB_REINDEX_FLAG = 'R';
  19. static const char DB_LAST_BLOCK = 'l';
  20. CCoinsViewDB::CCoinsViewDB(size_t nCacheSize, bool fMemory, bool fWipe) : db(GetDataDir() / "chainstate", nCacheSize, fMemory, fWipe, true)
  21. {
  22. }
  23. bool CCoinsViewDB::GetCoins(const uint256 &txid, CCoins &coins) const {
  24. return db.Read(std::make_pair(DB_COINS, txid), coins);
  25. }
  26. bool CCoinsViewDB::HaveCoins(const uint256 &txid) const {
  27. return db.Exists(std::make_pair(DB_COINS, txid));
  28. }
  29. uint256 CCoinsViewDB::GetBestBlock() const {
  30. uint256 hashBestChain;
  31. if (!db.Read(DB_BEST_BLOCK, hashBestChain))
  32. return uint256();
  33. return hashBestChain;
  34. }
  35. bool CCoinsViewDB::BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock) {
  36. CDBBatch batch(db);
  37. size_t count = 0;
  38. size_t changed = 0;
  39. for (CCoinsMap::iterator it = mapCoins.begin(); it != mapCoins.end();) {
  40. if (it->second.flags & CCoinsCacheEntry::DIRTY) {
  41. if (it->second.coins.IsPruned())
  42. batch.Erase(std::make_pair(DB_COINS, it->first));
  43. else
  44. batch.Write(std::make_pair(DB_COINS, it->first), it->second.coins);
  45. changed++;
  46. }
  47. count++;
  48. CCoinsMap::iterator itOld = it++;
  49. mapCoins.erase(itOld);
  50. }
  51. if (!hashBlock.IsNull())
  52. batch.Write(DB_BEST_BLOCK, hashBlock);
  53. LogPrint(BCLog::COINDB, "Committing %u changed transactions (out of %u) to coin database...\n", (unsigned int)changed, (unsigned int)count);
  54. return db.WriteBatch(batch);
  55. }
  56. CBlockTreeDB::CBlockTreeDB(size_t nCacheSize, bool fMemory, bool fWipe) : CDBWrapper(GetDataDir() / "blocks" / "index", nCacheSize, fMemory, fWipe) {
  57. }
  58. bool CBlockTreeDB::ReadBlockFileInfo(int nFile, CBlockFileInfo &info) {
  59. return Read(std::make_pair(DB_BLOCK_FILES, nFile), info);
  60. }
  61. bool CBlockTreeDB::WriteReindexing(bool fReindexing) {
  62. if (fReindexing)
  63. return Write(DB_REINDEX_FLAG, '1');
  64. else
  65. return Erase(DB_REINDEX_FLAG);
  66. }
  67. bool CBlockTreeDB::ReadReindexing(bool &fReindexing) {
  68. fReindexing = Exists(DB_REINDEX_FLAG);
  69. return true;
  70. }
  71. bool CBlockTreeDB::ReadLastBlockFile(int &nFile) {
  72. return Read(DB_LAST_BLOCK, nFile);
  73. }
  74. CCoinsViewCursor *CCoinsViewDB::Cursor() const
  75. {
  76. CCoinsViewDBCursor *i = new CCoinsViewDBCursor(const_cast<CDBWrapper*>(&db)->NewIterator(), GetBestBlock());
  77. /* It seems that there are no "const iterators" for LevelDB. Since we
  78. only need read operations on it, use a const-cast to get around
  79. that restriction. */
  80. i->pcursor->Seek(DB_COINS);
  81. // Cache key of first record
  82. i->pcursor->GetKey(i->keyTmp);
  83. return i;
  84. }
  85. bool CCoinsViewDBCursor::GetKey(uint256 &key) const
  86. {
  87. // Return cached key
  88. if (keyTmp.first == DB_COINS) {
  89. key = keyTmp.second;
  90. return true;
  91. }
  92. return false;
  93. }
  94. bool CCoinsViewDBCursor::GetValue(CCoins &coins) const
  95. {
  96. return pcursor->GetValue(coins);
  97. }
  98. unsigned int CCoinsViewDBCursor::GetValueSize() const
  99. {
  100. return pcursor->GetValueSize();
  101. }
  102. bool CCoinsViewDBCursor::Valid() const
  103. {
  104. return keyTmp.first == DB_COINS;
  105. }
  106. void CCoinsViewDBCursor::Next()
  107. {
  108. pcursor->Next();
  109. if (!pcursor->Valid() || !pcursor->GetKey(keyTmp))
  110. keyTmp.first = 0; // Invalidate cached key after last record so that Valid() and GetKey() return false
  111. }
  112. bool CBlockTreeDB::WriteBatchSync(const std::vector<std::pair<int, const CBlockFileInfo*> >& fileInfo, int nLastFile, const std::vector<const CBlockIndex*>& blockinfo) {
  113. CDBBatch batch(*this);
  114. for (std::vector<std::pair<int, const CBlockFileInfo*> >::const_iterator it=fileInfo.begin(); it != fileInfo.end(); it++) {
  115. batch.Write(std::make_pair(DB_BLOCK_FILES, it->first), *it->second);
  116. }
  117. batch.Write(DB_LAST_BLOCK, nLastFile);
  118. for (std::vector<const CBlockIndex*>::const_iterator it=blockinfo.begin(); it != blockinfo.end(); it++) {
  119. batch.Write(std::make_pair(DB_BLOCK_INDEX, (*it)->GetBlockHash()), CDiskBlockIndex(*it));
  120. }
  121. return WriteBatch(batch, true);
  122. }
  123. bool CBlockTreeDB::ReadTxIndex(const uint256 &txid, CDiskTxPos &pos) {
  124. return Read(std::make_pair(DB_TXINDEX, txid), pos);
  125. }
  126. bool CBlockTreeDB::WriteTxIndex(const std::vector<std::pair<uint256, CDiskTxPos> >&vect) {
  127. CDBBatch batch(*this);
  128. for (std::vector<std::pair<uint256,CDiskTxPos> >::const_iterator it=vect.begin(); it!=vect.end(); it++)
  129. batch.Write(std::make_pair(DB_TXINDEX, it->first), it->second);
  130. return WriteBatch(batch);
  131. }
  132. bool CBlockTreeDB::WriteFlag(const std::string &name, bool fValue) {
  133. return Write(std::make_pair(DB_FLAG, name), fValue ? '1' : '0');
  134. }
  135. bool CBlockTreeDB::ReadFlag(const std::string &name, bool &fValue) {
  136. char ch;
  137. if (!Read(std::make_pair(DB_FLAG, name), ch))
  138. return false;
  139. fValue = ch == '1';
  140. return true;
  141. }
  142. bool CBlockTreeDB::LoadBlockIndexGuts(std::function<CBlockIndex*(const uint256&)> insertBlockIndex)
  143. {
  144. std::unique_ptr<CDBIterator> pcursor(NewIterator());
  145. pcursor->Seek(std::make_pair(DB_BLOCK_INDEX, uint256()));
  146. // Load mapBlockIndex
  147. while (pcursor->Valid()) {
  148. boost::this_thread::interruption_point();
  149. std::pair<char, uint256> key;
  150. if (pcursor->GetKey(key) && key.first == DB_BLOCK_INDEX) {
  151. CDiskBlockIndex diskindex;
  152. if (pcursor->GetValue(diskindex)) {
  153. // Construct block index object
  154. CBlockIndex* pindexNew = insertBlockIndex(diskindex.GetBlockHash());
  155. pindexNew->pprev = insertBlockIndex(diskindex.hashPrev);
  156. pindexNew->nHeight = diskindex.nHeight;
  157. pindexNew->nFile = diskindex.nFile;
  158. pindexNew->nDataPos = diskindex.nDataPos;
  159. pindexNew->nUndoPos = diskindex.nUndoPos;
  160. pindexNew->nVersion = diskindex.nVersion;
  161. pindexNew->hashMerkleRoot = diskindex.hashMerkleRoot;
  162. pindexNew->nTime = diskindex.nTime;
  163. pindexNew->nBits = diskindex.nBits;
  164. pindexNew->nNonce = diskindex.nNonce;
  165. pindexNew->nStatus = diskindex.nStatus;
  166. pindexNew->nTx = diskindex.nTx;
  167. if (!CheckProofOfWork(pindexNew->GetBlockHash(), pindexNew->nBits, Params().GetConsensus()))
  168. return error("LoadBlockIndex(): CheckProofOfWork failed: %s", pindexNew->ToString());
  169. pcursor->Next();
  170. } else {
  171. return error("LoadBlockIndex() : failed to read value");
  172. }
  173. } else {
  174. break;
  175. }
  176. }
  177. return true;
  178. }