Permalink
Browse files

A little refactor to txdb

Move some code and design into the txdb.* files from main.cpp
Also remove the checking of the block header for each file
on disk, we've done that already on save and will do again
on load.
  • Loading branch information...
zander committed Feb 12, 2017
1 parent 28ce38a commit a608378bbdb413d18ad26da922fe324ebcaf4364
Showing with 48 additions and 45 deletions.
  1. +1 −0 src/checkpoints.cpp
  2. +0 −20 src/main.cpp
  3. +0 −9 src/main.h
  4. +1 −0 src/qt/transactionrecord.cpp
  5. +1 −0 src/rest.cpp
  6. +1 −0 src/rpcmining.cpp
  7. +1 −0 src/rpcrawtransaction.cpp
  8. +1 −0 src/thinblock.cpp
  9. +31 −16 src/txdb.cpp
  10. +9 −0 src/txdb.h
  11. +1 −0 src/wallet/rpcwallet.cpp
  12. +1 −0 src/wallet/wallet.cpp
View
@@ -8,6 +8,7 @@
#include "chainparams.h"
#include "main.h"
#include "uint256.h"
+#include "txdb.h"
#include <stdint.h>
View
@@ -3456,26 +3456,6 @@ boost::filesystem::path GetBlockPosFilename(const CDiskBlockPos &pos, const char
return GetDataDir() / "blocks" / strprintf("%s%05u.dat", prefix, pos.nFile);
}
-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();
- if (!pindexNew)
- throw runtime_error("LoadBlockIndex(): new CBlockIndex failed");
- mi = mapBlockIndex.insert(make_pair(hash, pindexNew)).first;
- pindexNew->phashBlock = &((*mi).first);
-
- return pindexNew;
-}
-
bool static LoadBlockIndexDB()
{
const CChainParams& chainparams = Params();
View
@@ -117,15 +117,8 @@ static const bool DEFAULT_TESTSAFEMODE = false;
/** Maximum number of headers to announce when relaying blocks with headers message.*/
static const unsigned int MAX_BLOCKS_TO_ANNOUNCE = 8;
-struct BlockHasher
-{
- size_t operator()(const uint256& hash) const { return hash.GetCheapHash(); }
-};
-
extern CCriticalSection cs_main;
extern CTxMemPool mempool;
-typedef boost::unordered_map<uint256, CBlockIndex*, BlockHasher> BlockMap;
-extern BlockMap mapBlockIndex;
extern uint64_t nLastBlockTx;
extern uint64_t nLastBlockSize;
extern const std::string strMessageMagic;
@@ -270,8 +263,6 @@ void FindFilesToPrune(std::set<int>& setFilesToPrune, uint64_t nPruneAfterHeight
*/
void UnlinkPrunedFiles(std::set<int>& setFilesToPrune);
-/** Create a new block index entry for a given block hash */
-CBlockIndex * InsertBlockIndex(uint256 hash);
/** Get statistics from node state */
bool GetNodeStateStats(NodeId nodeid, CNodeStateStats &stats);
/** Increase a node's misbehavior score. */
@@ -8,6 +8,7 @@
#include "consensus/consensus.h"
#include "main.h"
#include "timedata.h"
+#include <txdb.h>
#include "wallet/wallet.h"
#include <stdint.h>
View
@@ -12,6 +12,7 @@
#include "rpcserver.h"
#include "streams.h"
#include "sync.h"
+#include "txdb.h"
#include "txmempool.h"
#include "utilstrencodings.h"
#include "version.h"
View
@@ -14,6 +14,7 @@
#include "main.h"
#include "miner.h"
#include "net.h"
+#include "txdb.h"
#include "pow.h"
#include "rpcserver.h"
#include "txmempool.h"
@@ -22,6 +22,7 @@
#include "script/sign.h"
#include "script/standard.h"
#include "txmempool.h"
+#include "txdb.h"
#include "uint256.h"
#include "utilstrencodings.h"
#ifdef ENABLE_WALLET
View
@@ -10,6 +10,7 @@
#include "main.h"
#include "chainparams.h"
#include "txmempool.h"
+#include "txdb.h"
#include "utilstrencodings.h"
#include "txorphancache.h"
#include "consensus/merkle.h"
View
@@ -16,8 +16,6 @@
#include <boost/thread.hpp>
-using namespace std;
-
static const char DB_COINS = 'c';
static const char DB_BLOCK_FILES = 'f';
static const char DB_TXINDEX = 't';
@@ -29,16 +27,37 @@ static const char DB_REINDEX_FLAG = 'R';
static const char DB_LAST_BLOCK = 'l';
static const char DB_SIZE_REMOVAL_FORK_ACTIVATION = 's';
+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;
+}
+}
+
+
CCoinsViewDB::CCoinsViewDB(size_t nCacheSize, bool fMemory, bool fWipe) : db(GetDataDir() / "chainstate", nCacheSize, fMemory, fWipe, false)
{
}
bool CCoinsViewDB::GetCoins(const uint256 &txid, CCoins &coins) const {
- return db.Read(make_pair(DB_COINS, txid), coins);
+ return db.Read(std::make_pair(DB_COINS, txid), coins);
}
bool CCoinsViewDB::HaveCoins(const uint256 &txid) const {
- return db.Exists(make_pair(DB_COINS, txid));
+ return db.Exists(std::make_pair(DB_COINS, txid));
}
uint256 CCoinsViewDB::GetBestBlock() const {
@@ -55,9 +74,9 @@ bool CCoinsViewDB::BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock) {
for (CCoinsMap::iterator it = mapCoins.begin(); it != mapCoins.end();) {
if (it->second.flags & CCoinsCacheEntry::DIRTY) {
if (it->second.coins.IsPruned())
- batch.Erase(make_pair(DB_COINS, it->first));
+ batch.Erase(std::make_pair(DB_COINS, it->first));
else
- batch.Write(make_pair(DB_COINS, it->first), it->second.coins);
+ batch.Write(std::make_pair(DB_COINS, it->first), it->second.coins);
changed++;
}
count++;
@@ -122,7 +141,7 @@ CBlockTreeDB::CBlockTreeDB(size_t nCacheSize, bool fMemory, bool fWipe) : CDBWra
}
bool CBlockTreeDB::ReadBlockFileInfo(int nFile, CBlockFileInfo &info) {
- return Read(make_pair(DB_BLOCK_FILES, nFile), info);
+ return Read(std::make_pair(DB_BLOCK_FILES, nFile), info);
}
bool CBlockTreeDB::WriteReindexing(bool fReindexing) {
@@ -144,23 +163,23 @@ bool CBlockTreeDB::ReadLastBlockFile(int &nFile) {
bool CBlockTreeDB::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(make_pair(DB_BLOCK_FILES, it->first), *it->second);
+ 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(make_pair(DB_BLOCK_INDEX, (*it)->GetBlockHash()), CDiskBlockIndex(*it));
+ batch.Write(std::make_pair(DB_BLOCK_INDEX, (*it)->GetBlockHash()), CDiskBlockIndex(*it));
}
return WriteBatch(batch, true);
}
bool CBlockTreeDB::ReadTxIndex(const uint256 &txid, CDiskTxPos &pos) {
- return Read(make_pair(DB_TXINDEX, txid), pos);
+ return Read(std::make_pair(DB_TXINDEX, txid), pos);
}
bool CBlockTreeDB::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(make_pair(DB_TXINDEX, it->first), it->second);
+ batch.Write(std::make_pair(DB_TXINDEX, it->first), it->second);
return WriteBatch(batch);
}
@@ -180,9 +199,8 @@ bool CBlockTreeDB::LoadBlockIndexGuts()
{
boost::scoped_ptr<CDBIterator> pcursor(NewIterator());
- pcursor->Seek(make_pair(DB_BLOCK_INDEX, uint256()));
+ pcursor->Seek(std::make_pair(DB_BLOCK_INDEX, uint256()));
- // Load mapBlockIndex
while (pcursor->Valid()) {
boost::this_thread::interruption_point();
std::pair<char, uint256> key;
@@ -204,9 +222,6 @@ bool CBlockTreeDB::LoadBlockIndexGuts()
pindexNew->nStatus = diskindex.nStatus;
pindexNew->nTx = diskindex.nTx;
- if (!CheckProofOfWork(pindexNew->GetBlockHash(), pindexNew->nBits, Params().GetConsensus()))
- return error("LoadBlockIndex(): CheckProofOfWork failed: %s", pindexNew->ToString());
-
pcursor->Next();
} else {
return error("LoadBlockIndex() : failed to read value");
View
@@ -62,4 +62,13 @@ class CBlockTreeDB : public CDBWrapper
bool LoadBlockIndexGuts();
};
+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
@@ -13,6 +13,7 @@
#include "netbase.h"
#include "rpcserver.h"
#include "timedata.h"
+#include "txdb.h"
#include "util.h"
#include "utilmoneystr.h"
#include "wallet.h"
View
@@ -22,6 +22,7 @@
#include "script/sign.h"
#include "timedata.h"
#include "txmempool.h"
+#include "txdb.h"
#include "util.h"
#include "utilmoneystr.h"

0 comments on commit a608378

Please sign in to comment.