Permalink
Browse files

Move some methods out of main.cpp to BlocksDB.cpp

  • Loading branch information...
zander committed Feb 14, 2017
1 parent 4ca5459 commit 7c86687253e75ef7e9f0d5e16db1a60fdda0f863
Showing with 160 additions and 150 deletions.
  1. +142 −0 src/BlocksDB.cpp
  2. +14 −1 src/BlocksDB.h
  3. +1 −1 src/init.cpp
  4. +2 −141 src/main.cpp
  5. +1 −7 src/main.h
View
@@ -4,11 +4,14 @@
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include "BlocksDB.h"
+#include "chainparams.h"
+#include "consensus/validation.h"
#include "chain.h"
#include "main.h"
#include "uint256.h"
#include <boost/thread.hpp>
+#include <boost/filesystem.hpp>
static const char DB_BLOCK_FILES = 'f';
static const char DB_TXINDEX = 't';
@@ -154,3 +157,142 @@ bool BlocksDB::CacheAllBlockInfos()
return true;
}
+
+FILE* OpenDiskFile(const CDiskBlockPos &pos, const char *prefix, bool fReadOnly)
+{
+ if (pos.IsNull())
+ return NULL;
+ boost::filesystem::path path = GetBlockPosFilename(pos.nFile, prefix);
+ boost::filesystem::create_directories(path.parent_path());
+ FILE* file = fopen(path.string().c_str(), "rb+");
+ if (!file && !fReadOnly)
+ file = fopen(path.string().c_str(), "wb+");
+ if (!file) {
+ LogPrintf("Unable to open file %s\n", path.string());
+ return NULL;
+ }
+ if (pos.nPos) {
+ if (fseek(file, pos.nPos, SEEK_SET)) {
+ LogPrintf("Unable to seek to position %u of %s\n", pos.nPos, path.string());
+ fclose(file);
+ return NULL;
+ }
+ }
+ return file;
+}
+
+FILE* OpenBlockFile(const CDiskBlockPos &pos, bool fReadOnly) {
+ return OpenDiskFile(pos, "blk", fReadOnly);
+}
+
+FILE* OpenUndoFile(const CDiskBlockPos &pos, bool fReadOnly) {
+ return OpenDiskFile(pos, "rev", fReadOnly);
+}
+
+boost::filesystem::path GetBlockPosFilename(int fileIndex, const char *prefix)
+{
+ return GetDataDir() / "blocks" / strprintf("%s%05u.dat", prefix, fileIndex);
+}
+
+bool LoadExternalBlockFile(const CChainParams& chainparams, FILE* fileIn, CDiskBlockPos *dbp)
+{
+ // Map of disk positions for blocks with unknown parent (only used for reindex)
+ static std::multimap<uint256, CDiskBlockPos> mapBlocksUnknownParent;
+ int64_t nStart = GetTimeMillis();
+
+ int nLoaded = 0;
+ try {
+ // This takes over fileIn and calls fclose() on it in the CBufferedFile destructor
+ CBufferedFile blkdat(fileIn, 2E6, 1E6+8, SER_DISK, CLIENT_VERSION);
+ uint64_t nRewind = blkdat.GetPos();
+ while (!blkdat.eof()) {
+ boost::this_thread::interruption_point();
+
+ blkdat.SetPos(nRewind);
+ nRewind++; // start one byte further next time, in case of failure
+ blkdat.SetLimit(); // remove former limit
+ unsigned int nSize = 0;
+ try {
+ // locate a header
+ unsigned char buf[MESSAGE_START_SIZE];
+ blkdat.FindByte(chainparams.MessageStart()[0]);
+ nRewind = blkdat.GetPos()+1;
+ blkdat >> FLATDATA(buf);
+ if (memcmp(buf, chainparams.MessageStart(), MESSAGE_START_SIZE))
+ continue;
+ // read size
+ blkdat >> nSize;
+ if (nSize < 80)
+ continue;
+ } catch (const std::exception&) {
+ // no valid block header found; don't complain
+ break;
+ }
+ try {
+ // read block
+ uint64_t nBlockPos = blkdat.GetPos();
+ if (dbp)
+ dbp->nPos = nBlockPos;
+ blkdat.SetLimit(nBlockPos + nSize);
+ blkdat.SetPos(nBlockPos);
+ CBlock block;
+ blkdat >> block;
+ nRewind = blkdat.GetPos();
+
+ // detect out of order blocks, and store them for later
+ uint256 hash = block.GetHash();
+ if (hash != chainparams.GetConsensus().hashGenesisBlock && mapBlockIndex.find(block.hashPrevBlock) == mapBlockIndex.end()) {
+ LogPrint("reindex", "%s: Out of order block %s, parent %s not known\n", __func__, hash.ToString(),
+ block.hashPrevBlock.ToString());
+ if (dbp)
+ mapBlocksUnknownParent.insert(std::make_pair(block.hashPrevBlock, *dbp));
+ continue;
+ }
+
+ // process in case the block isn't known yet
+ if (mapBlockIndex.count(hash) == 0 || (mapBlockIndex[hash]->nStatus & BLOCK_HAVE_DATA) == 0) {
+ CValidationState state;
+ if (ProcessNewBlock(state, chainparams, NULL, &block, true, dbp))
+ nLoaded++;
+ if (state.IsError())
+ break;
+ } else if (hash != chainparams.GetConsensus().hashGenesisBlock && mapBlockIndex[hash]->nHeight % 1000 == 0) {
+ LogPrintf("Block Import: already had block %s at height %d\n", hash.ToString(), mapBlockIndex[hash]->nHeight);
+ }
+
+ // Recursively process earlier encountered successors of this block
+ std::deque<uint256> queue;
+ queue.push_back(hash);
+ while (!queue.empty()) {
+ uint256 head = queue.front();
+ queue.pop_front();
+ std::pair<std::multimap<uint256, CDiskBlockPos>::iterator, std::multimap<uint256, CDiskBlockPos>::iterator> range = mapBlocksUnknownParent.equal_range(head);
+ while (range.first != range.second) {
+ std::multimap<uint256, CDiskBlockPos>::iterator it = range.first;
+ if (ReadBlockFromDisk(block, it->second, chainparams.GetConsensus()))
+ {
+ LogPrintf("%s: Processing out of order child %s of %s\n", __func__, block.GetHash().ToString(),
+ head.ToString());
+ CValidationState dummy;
+ if (ProcessNewBlock(dummy, chainparams, NULL, &block, true, &it->second))
+ {
+ nLoaded++;
+ queue.push_back(block.GetHash());
+ }
+ }
+ range.first++;
+ mapBlocksUnknownParent.erase(it);
+ }
+ }
+ } catch (const std::exception& e) {
+ LogPrintf("%s: Deserialize or I/O error - %s\n", __func__, e.what());
+ }
+ }
+ } catch (const std::runtime_error& e) {
+ LogPrintf("%s: Deserialize or I/O error - %s\n", __func__, e.what());
+ return false;
+ }
+ if (nLoaded > 0)
+ LogPrintf("Loaded %i blocks from external file in %dms\n", nLoaded, GetTimeMillis() - nStart);
+ return nLoaded > 0;
+}
View
@@ -16,6 +16,8 @@
class CBlockFileInfo;
class CBlockIndex;
struct CDiskTxPos;
+class CDiskBlockPos;
+class CChainParams;
class uint256;
//! -dbcache default (MiB)
@@ -71,7 +73,18 @@ struct BlockHashShortener {
}
};
typedef boost::unordered_map<uint256, CBlockIndex*, BlockHashShortener> BlockMap;
-// TODO move this into CBlockTreeDB and protect it with a mutex
+// TODO move this into BlocksDB and protect it with a mutex
extern BlockMap mapBlockIndex;
+
+/** Open a block file (blk?????.dat) */
+FILE* OpenBlockFile(const CDiskBlockPos &pos, bool fReadOnly = false);
+/** Open an undo file (rev?????.dat) */
+FILE* OpenUndoFile(const CDiskBlockPos &pos, bool fReadOnly = false);
+/** Translation to a filesystem path */
+boost::filesystem::path GetBlockPosFilename(int fileIndex, const char *prefix);
+/** Import blocks from an external file */
+bool LoadExternalBlockFile(const CChainParams& chainparams, FILE* fileIn, CDiskBlockPos *dbp = 0);
+
+
#endif // BITCOIN_TXDB_H
View
@@ -610,7 +610,7 @@ void ThreadImport(std::vector<boost::filesystem::path> vImportFiles)
int nFile = 0;
while (true) {
CDiskBlockPos pos(nFile, 0);
- if (!boost::filesystem::exists(GetBlockPosFilename(pos, "blk")))
+ if (!boost::filesystem::exists(GetBlockPosFilename(pos.nFile, "blk")))
break; // No block files left to reindex
FILE *file = OpenBlockFile(pos, true);
if (!file)
View
@@ -3354,9 +3354,8 @@ void PruneOneBlockFile(const int fileNumber)
void UnlinkPrunedFiles(std::set<int>& setFilesToPrune)
{
for (set<int>::iterator it = setFilesToPrune.begin(); it != setFilesToPrune.end(); ++it) {
- CDiskBlockPos pos(*it, 0);
- boost::filesystem::remove(GetBlockPosFilename(pos, "blk"));
- boost::filesystem::remove(GetBlockPosFilename(pos, "rev"));
+ boost::filesystem::remove(GetBlockPosFilename(*it, "blk"));
+ boost::filesystem::remove(GetBlockPosFilename(*it, "rev"));
LogPrintf("Prune: %s deleted blk/rev (%05u)\n", __func__, *it);
}
}
@@ -3420,42 +3419,6 @@ bool CheckDiskSpace(uint64_t nAdditionalBytes)
return true;
}
-FILE* OpenDiskFile(const CDiskBlockPos &pos, const char *prefix, bool fReadOnly)
-{
- if (pos.IsNull())
- return NULL;
- boost::filesystem::path path = GetBlockPosFilename(pos, prefix);
- boost::filesystem::create_directories(path.parent_path());
- FILE* file = fopen(path.string().c_str(), "rb+");
- if (!file && !fReadOnly)
- file = fopen(path.string().c_str(), "wb+");
- if (!file) {
- LogPrintf("Unable to open file %s\n", path.string());
- return NULL;
- }
- if (pos.nPos) {
- if (fseek(file, pos.nPos, SEEK_SET)) {
- LogPrintf("Unable to seek to position %u of %s\n", pos.nPos, path.string());
- fclose(file);
- return NULL;
- }
- }
- return file;
-}
-
-FILE* OpenBlockFile(const CDiskBlockPos &pos, bool fReadOnly) {
- return OpenDiskFile(pos, "blk", fReadOnly);
-}
-
-FILE* OpenUndoFile(const CDiskBlockPos &pos, bool fReadOnly) {
- return OpenDiskFile(pos, "rev", fReadOnly);
-}
-
-boost::filesystem::path GetBlockPosFilename(const CDiskBlockPos &pos, const char *prefix)
-{
- return GetDataDir() / "blocks" / strprintf("%s%05u.dat", prefix, pos.nFile);
-}
-
bool static LoadBlockIndexDB()
{
const CChainParams& chainparams = Params();
@@ -3738,108 +3701,6 @@ bool InitBlockIndex(const CChainParams& chainparams)
return true;
}
-bool LoadExternalBlockFile(const CChainParams& chainparams, FILE* fileIn, CDiskBlockPos *dbp)
-{
- // Map of disk positions for blocks with unknown parent (only used for reindex)
- static std::multimap<uint256, CDiskBlockPos> mapBlocksUnknownParent;
- int64_t nStart = GetTimeMillis();
-
- int nLoaded = 0;
- try {
- // This takes over fileIn and calls fclose() on it in the CBufferedFile destructor
- CBufferedFile blkdat(fileIn, 2E6, 1E6+8, SER_DISK, CLIENT_VERSION);
- uint64_t nRewind = blkdat.GetPos();
- while (!blkdat.eof()) {
- boost::this_thread::interruption_point();
-
- blkdat.SetPos(nRewind);
- nRewind++; // start one byte further next time, in case of failure
- blkdat.SetLimit(); // remove former limit
- unsigned int nSize = 0;
- try {
- // locate a header
- unsigned char buf[MESSAGE_START_SIZE];
- blkdat.FindByte(chainparams.MessageStart()[0]);
- nRewind = blkdat.GetPos()+1;
- blkdat >> FLATDATA(buf);
- if (memcmp(buf, chainparams.MessageStart(), MESSAGE_START_SIZE))
- continue;
- // read size
- blkdat >> nSize;
- if (nSize < 80)
- continue;
- } catch (const std::exception&) {
- // no valid block header found; don't complain
- break;
- }
- try {
- // read block
- uint64_t nBlockPos = blkdat.GetPos();
- if (dbp)
- dbp->nPos = nBlockPos;
- blkdat.SetLimit(nBlockPos + nSize);
- blkdat.SetPos(nBlockPos);
- CBlock block;
- blkdat >> block;
- nRewind = blkdat.GetPos();
-
- // detect out of order blocks, and store them for later
- uint256 hash = block.GetHash();
- if (hash != chainparams.GetConsensus().hashGenesisBlock && mapBlockIndex.find(block.hashPrevBlock) == mapBlockIndex.end()) {
- LogPrint("reindex", "%s: Out of order block %s, parent %s not known\n", __func__, hash.ToString(),
- block.hashPrevBlock.ToString());
- if (dbp)
- mapBlocksUnknownParent.insert(std::make_pair(block.hashPrevBlock, *dbp));
- continue;
- }
-
- // process in case the block isn't known yet
- if (mapBlockIndex.count(hash) == 0 || (mapBlockIndex[hash]->nStatus & BLOCK_HAVE_DATA) == 0) {
- CValidationState state;
- if (ProcessNewBlock(state, chainparams, NULL, &block, true, dbp))
- nLoaded++;
- if (state.IsError())
- break;
- } else if (hash != chainparams.GetConsensus().hashGenesisBlock && mapBlockIndex[hash]->nHeight % 1000 == 0) {
- LogPrintf("Block Import: already had block %s at height %d\n", hash.ToString(), mapBlockIndex[hash]->nHeight);
- }
-
- // Recursively process earlier encountered successors of this block
- deque<uint256> queue;
- queue.push_back(hash);
- while (!queue.empty()) {
- uint256 head = queue.front();
- queue.pop_front();
- std::pair<std::multimap<uint256, CDiskBlockPos>::iterator, std::multimap<uint256, CDiskBlockPos>::iterator> range = mapBlocksUnknownParent.equal_range(head);
- while (range.first != range.second) {
- std::multimap<uint256, CDiskBlockPos>::iterator it = range.first;
- if (ReadBlockFromDisk(block, it->second, chainparams.GetConsensus()))
- {
- LogPrintf("%s: Processing out of order child %s of %s\n", __func__, block.GetHash().ToString(),
- head.ToString());
- CValidationState dummy;
- if (ProcessNewBlock(dummy, chainparams, NULL, &block, true, &it->second))
- {
- nLoaded++;
- queue.push_back(block.GetHash());
- }
- }
- range.first++;
- mapBlocksUnknownParent.erase(it);
- }
- }
- } catch (const std::exception& e) {
- LogPrintf("%s: Deserialize or I/O error - %s\n", __func__, e.what());
- }
- }
- } catch (const std::runtime_error& e) {
- AbortNode(std::string("System error: ") + e.what());
- }
- if (nLoaded > 0)
- LogPrintf("Loaded %i blocks from external file in %dms\n", nLoaded, GetTimeMillis() - nStart);
- return nLoaded > 0;
-}
-
void static CheckBlockIndex(const Consensus::Params& consensusParams)
{
if (!fCheckBlockIndex) {
View
@@ -198,14 +198,8 @@ void UnregisterNodeSignals(CNodeSignals& nodeSignals);
bool ProcessNewBlock(CValidationState& state, const CChainParams& chainparams, const CNode* pfrom, const CBlock* pblock, bool fForceProcessing, CDiskBlockPos* dbp);
/** Check whether enough disk space is available for an incoming block */
bool CheckDiskSpace(uint64_t nAdditionalBytes = 0);
-/** Open a block file (blk?????.dat) */
-FILE* OpenBlockFile(const CDiskBlockPos &pos, bool fReadOnly = false);
-/** Open an undo file (rev?????.dat) */
-FILE* OpenUndoFile(const CDiskBlockPos &pos, bool fReadOnly = false);
/** Translation to a filesystem path */
-boost::filesystem::path GetBlockPosFilename(const CDiskBlockPos &pos, const char *prefix);
-/** Import blocks from an external file */
-bool LoadExternalBlockFile(const CChainParams& chainparams, FILE* fileIn, CDiskBlockPos *dbp = NULL);
+boost::filesystem::path GetBlockPosFilename(int fileIndex, const char *prefix);
/** Initialize a new block tree database + block data on disk */
bool InitBlockIndex(const CChainParams& chainparams);
/** Load the block tree and coins database from disk */

0 comments on commit 7c86687

Please sign in to comment.