Permalink
Browse files

Move the BlocksDB to its own file.

  • Loading branch information...
zander committed Feb 14, 2017
1 parent 789f7d1 commit 4ca54594afeaf67e7e97be7e8bc9734b74ca68c0
View
@@ -0,0 +1,156 @@
+// Copyright (c) 2009-2010 Satoshi Nakamoto
+// Copyright (C) 2017 Tom Zander <tomz@freedommail.ch>
+// Distributed under the MIT software license, see the accompanying
+// file COPYING or http://www.opensource.org/licenses/mit-license.php.
+
+#include "BlocksDB.h"
+
+#include "chain.h"
+#include "main.h"
+#include "uint256.h"
+#include <boost/thread.hpp>
+
+static const char DB_BLOCK_FILES = 'f';
+static const char DB_TXINDEX = 't';
+static const char DB_BLOCK_INDEX = 'b';
+
+static const char DB_FLAG = 'F';
+static const char DB_REINDEX_FLAG = 'R';
+static const char DB_LAST_BLOCK = 'l';
+
+namespace {
+CBlockIndex * InsertBlockIndex(uint256 hash)
+{
+ if (hash.IsNull())
+ return NULL;
+
+ // Return existing
+ BlockMap::iterator mi = mapBlockIndex.find(hash);
+ if (mi != mapBlockIndex.end())
+ return (*mi).second;
+
+ // Create new
+ CBlockIndex* pindexNew = new CBlockIndex();
+ mi = mapBlockIndex.insert(std::make_pair(hash, pindexNew)).first;
+ pindexNew->phashBlock = &((*mi).first);
+
+ return pindexNew;
+}
+}
+
+BlocksDB* BlocksDB::s_instance = nullptr;
+
+BlocksDB &BlocksDB::instance()
+{
+ assert(BlocksDB::s_instance);
+ return *BlocksDB::s_instance;
+}
+
+void BlocksDB::createInstance(size_t nCacheSize, bool fWipe)
+{
+ delete BlocksDB::s_instance;
+ BlocksDB::s_instance = new BlocksDB(nCacheSize, false, fWipe);
+}
+
+void BlocksDB::createTestInstance(size_t nCacheSize)
+{
+ delete BlocksDB::s_instance;
+ BlocksDB::s_instance = new BlocksDB(nCacheSize, true);
+}
+
+BlocksDB::BlocksDB(size_t nCacheSize, bool fMemory, bool fWipe) : CDBWrapper(GetDataDir() / "blocks" / "index", nCacheSize, fMemory, fWipe) {
+}
+
+bool BlocksDB::ReadBlockFileInfo(int nFile, CBlockFileInfo &info) {
+ return Read(std::make_pair(DB_BLOCK_FILES, nFile), info);
+}
+
+bool BlocksDB::WriteReindexing(bool fReindexing) {
+ if (fReindexing)
+ return Write(DB_REINDEX_FLAG, '1');
+ else
+ return Erase(DB_REINDEX_FLAG);
+}
+
+bool BlocksDB::ReadReindexing(bool &fReindexing) {
+ fReindexing = Exists(DB_REINDEX_FLAG);
+ return true;
+}
+
+bool BlocksDB::ReadLastBlockFile(int &nFile) {
+ return Read(DB_LAST_BLOCK, nFile);
+}
+
+bool BlocksDB::WriteBatchSync(const std::vector<std::pair<int, const CBlockFileInfo*> >& fileInfo, int nLastFile, const std::vector<const CBlockIndex*>& blockinfo) {
+ CDBBatch batch(&GetObfuscateKey());
+ for (std::vector<std::pair<int, const CBlockFileInfo*> >::const_iterator it=fileInfo.begin(); it != fileInfo.end(); it++) {
+ batch.Write(std::make_pair(DB_BLOCK_FILES, it->first), *it->second);
+ }
+ batch.Write(DB_LAST_BLOCK, nLastFile);
+ for (std::vector<const CBlockIndex*>::const_iterator it=blockinfo.begin(); it != blockinfo.end(); it++) {
+ batch.Write(std::make_pair(DB_BLOCK_INDEX, (*it)->GetBlockHash()), CDiskBlockIndex(*it));
+ }
+ return WriteBatch(batch, true);
+}
+
+bool BlocksDB::ReadTxIndex(const uint256 &txid, CDiskTxPos &pos) {
+ return Read(std::make_pair(DB_TXINDEX, txid), pos);
+}
+
+bool BlocksDB::WriteTxIndex(const std::vector<std::pair<uint256, CDiskTxPos> >&vect) {
+ CDBBatch batch(&GetObfuscateKey());
+ for (std::vector<std::pair<uint256,CDiskTxPos> >::const_iterator it=vect.begin(); it!=vect.end(); it++)
+ batch.Write(std::make_pair(DB_TXINDEX, it->first), it->second);
+ return WriteBatch(batch);
+}
+
+bool BlocksDB::WriteFlag(const std::string &name, bool fValue) {
+ return Write(std::make_pair(DB_FLAG, name), fValue ? '1' : '0');
+}
+
+bool BlocksDB::ReadFlag(const std::string &name, bool &fValue) {
+ char ch;
+ if (!Read(std::make_pair(DB_FLAG, name), ch))
+ return false;
+ fValue = ch == '1';
+ return true;
+}
+
+bool BlocksDB::CacheAllBlockInfos()
+{
+ boost::scoped_ptr<CDBIterator> pcursor(NewIterator());
+
+ pcursor->Seek(std::make_pair(DB_BLOCK_INDEX, uint256()));
+
+ while (pcursor->Valid()) {
+ boost::this_thread::interruption_point();
+ std::pair<char, uint256> key;
+ if (pcursor->GetKey(key) && key.first == DB_BLOCK_INDEX) {
+ CDiskBlockIndex diskindex;
+ if (pcursor->GetValue(diskindex)) {
+ // Construct block index object
+ CBlockIndex* pindexNew = InsertBlockIndex(diskindex.GetBlockHash());
+ pindexNew->pprev = InsertBlockIndex(diskindex.hashPrev);
+ pindexNew->nHeight = diskindex.nHeight;
+ pindexNew->nFile = diskindex.nFile;
+ pindexNew->nDataPos = diskindex.nDataPos;
+ pindexNew->nUndoPos = diskindex.nUndoPos;
+ pindexNew->nVersion = diskindex.nVersion;
+ pindexNew->hashMerkleRoot = diskindex.hashMerkleRoot;
+ pindexNew->nTime = diskindex.nTime;
+ pindexNew->nBits = diskindex.nBits;
+ pindexNew->nNonce = diskindex.nNonce;
+ pindexNew->nStatus = diskindex.nStatus;
+ pindexNew->nTx = diskindex.nTx;
+
+ pcursor->Next();
+ } else {
+ return error("LoadBlockIndex() : failed to read value");
+ }
+ } else {
+ break;
+ }
+ }
+
+ return true;
+}
View
@@ -0,0 +1,77 @@
+// Copyright (c) 2009-2010 Satoshi Nakamoto
+// Copyright (c) 2009-2015 The Bitcoin Core developers
+// Copyright (c) 2017 Tom Zander <tomz@freedommail.ch>
+// Distributed under the MIT software license, see the accompanying
+// file COPYING or http://www.opensource.org/licenses/mit-license.php.
+
+#ifndef BITCOIN_BLOCKSDB_H
+#define BITCOIN_BLOCKSDB_H
+
+#include "dbwrapper.h"
+
+#include <boost/unordered_map.hpp>
+#include <string>
+#include <vector>
+
+class CBlockFileInfo;
+class CBlockIndex;
+struct CDiskTxPos;
+class uint256;
+
+//! -dbcache default (MiB)
+static const int64_t nDefaultDbCache = 300;
+//! max. -dbcache in (MiB)
+static const int64_t nMaxDbCache = sizeof(void*) > 4 ? 16384 : 1024;
+//! min. -dbcache in (MiB)
+static const int64_t nMinDbCache = 4;
+
+/** Access to the block database (blocks/index/) */
+class BlocksDB : public CDBWrapper
+{
+public:
+ /**
+ * returns the singleton instance of the BlocksDB. Please be aware that
+ * you need to call createInstance once in the app-init before it is allowed to call instance()
+ */
+ static BlocksDB &instance();
+ /**
+ * Deletes an old and creates a new instance of the BlocksDB singleton.
+ * @param[in] nCacheSize Configures various leveldb cache settings.
+ * @param[in] fWipe If true, remove all existing data.
+ * @see instance()
+ */
+ static void createInstance(size_t nCacheSize, bool fWipe);
+ /// Deletes old singleton and creates a new one for unit testing.
+ static void createTestInstance(size_t nCacheSize);
+
+private:
+ BlocksDB(size_t nCacheSize, bool fMemory = false, bool fWipe = false);
+ BlocksDB(const BlocksDB&);
+ void operator=(const BlocksDB&);
+public:
+ bool WriteBatchSync(const std::vector<std::pair<int, const CBlockFileInfo*> >& fileInfo, int nLastFile, const std::vector<const CBlockIndex*>& blockinfo);
+ bool ReadBlockFileInfo(int nFile, CBlockFileInfo &fileinfo);
+ bool ReadLastBlockFile(int &nFile);
+ bool WriteReindexing(bool fReindex);
+ bool ReadReindexing(bool &fReindex);
+ bool ReadTxIndex(const uint256 &txid, CDiskTxPos &pos);
+ bool WriteTxIndex(const std::vector<std::pair<uint256, CDiskTxPos> > &list);
+ bool WriteFlag(const std::string &name, bool fValue);
+ bool ReadFlag(const std::string &name, bool &fValue);
+ /// Reads and caches all info about blocks.
+ bool CacheAllBlockInfos();
+
+private:
+ static BlocksDB *s_instance;
+};
+
+struct BlockHashShortener {
+ inline size_t operator()(const uint256& hash) const {
+ return hash.GetCheapHash();
+ }
+};
+typedef boost::unordered_map<uint256, CBlockIndex*, BlockHashShortener> BlockMap;
+// TODO move this into CBlockTreeDB and protect it with a mutex
+extern BlockMap mapBlockIndex;
+
+#endif // BITCOIN_TXDB_H
View
@@ -160,6 +160,7 @@ BITCOIN_CORE_H = \
tinyformat.h \
torcontrol.h \
txdb.h \
+ BlocksDB.h \
txmempool.h \
txorphancache.h \
ui_interface.h \
@@ -227,6 +228,7 @@ libbitcoin_server_a_SOURCES = \
timedata.cpp \
torcontrol.cpp \
txdb.cpp \
+ BlocksDB.cpp \
txmempool.cpp \
validationinterface.cpp \
versionbits.cpp \
View
@@ -8,7 +8,7 @@
#include "chainparams.h"
#include "main.h"
#include "uint256.h"
-#include "txdb.h"
+#include "BlocksDB.h"
#include <stdint.h>
View
@@ -29,6 +29,7 @@
#include "script/sigcache.h"
#include "scheduler.h"
#include "txdb.h"
+#include "BlocksDB.h"
#include "txmempool.h"
#include "torcontrol.h"
#include "ui_interface.h"
View
@@ -26,6 +26,7 @@
#include "script/standard.h"
#include "thinblock.h"
#include "tinyformat.h"
+#include "BlocksDB.h"
#include "txdb.h"
#include "txmempool.h"
#include "txorphancache.h"
View
@@ -15,7 +15,7 @@
#include "main.h" // for DEFAULT_SCRIPTCHECK_THREADS and MAX_SCRIPTCHECK_THREADS
#include "netbase.h"
-#include "txdb.h" // for -dbcache defaults
+#include <BlocksDB.h> // for -dbcache defaults
#ifdef ENABLE_WALLET
#include "wallet/wallet.h" // for CWallet::GetRequiredFee()
View
@@ -15,7 +15,7 @@
#include "init.h"
#include "main.h" // For DEFAULT_SCRIPTCHECK_THREADS
#include "net.h"
-#include "txdb.h" // for -dbcache defaults
+#include <BlocksDB.h> // for -dbcache defaults
#ifdef ENABLE_WALLET
#include "wallet/wallet.h"
@@ -8,7 +8,7 @@
#include "consensus/consensus.h"
#include "main.h"
#include "timedata.h"
-#include <txdb.h>
+#include <BlocksDB.h>
#include "wallet/wallet.h"
#include <stdint.h>
View
@@ -12,7 +12,7 @@
#include "rpcserver.h"
#include "streams.h"
#include "sync.h"
-#include "txdb.h"
+#include "BlocksDB.h"
#include "txmempool.h"
#include "utilstrencodings.h"
#include "version.h"
View
@@ -16,7 +16,7 @@
#include "streams.h"
#include "sync.h"
#include "txmempool.h"
-#include "txdb.h"
+#include "BlocksDB.h"
#include "timedata.h"
#include "util.h"
#include "utilstrencodings.h"
View
@@ -14,7 +14,7 @@
#include "main.h"
#include "miner.h"
#include "net.h"
-#include "txdb.h"
+#include "BlocksDB.h"
#include "pow.h"
#include "rpcserver.h"
#include "txmempool.h"
@@ -22,7 +22,7 @@
#include "script/sign.h"
#include "script/standard.h"
#include "txmempool.h"
-#include "txdb.h"
+#include "BlocksDB.h"
#include "uint256.h"
#include "utilstrencodings.h"
#ifdef ENABLE_WALLET
@@ -14,7 +14,7 @@
#include "miner.h"
#include "pubkey.h"
#include "random.h"
-#include "txdb.h"
+#include <BlocksDB.h>
#include "txmempool.h"
#include "ui_interface.h"
#include "util.h"
View
@@ -10,7 +10,7 @@
#include "main.h"
#include "chainparams.h"
#include "txmempool.h"
-#include "txdb.h"
+#include "BlocksDB.h"
#include "utilstrencodings.h"
#include "txorphancache.h"
#include "consensus/merkle.h"
Oops, something went wrong.

0 comments on commit 4ca5459

Please sign in to comment.