Skip to content

Commit

Permalink
Cache the block hashes on disk.
Browse files Browse the repository at this point in the history
  • Loading branch information
caraphinneth committed Oct 20, 2019
1 parent 956438e commit bbabd0f
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 2 deletions.
20 changes: 19 additions & 1 deletion src/chain.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,15 @@
#include "arith_uint256.h"
#include "primitives/block.h"
#include "pow.h"
#include "timedata.h"
#include "tinyformat.h"
#include "uint256.h"

#include <vector>

/** Whether to recalculate hashes on startup */
extern bool fUseFastIndex;

class CBlockFileInfo
{
public:
Expand Down Expand Up @@ -333,11 +337,15 @@ class CBlockIndex
/** Used to marshal pointers into hashes for db storage. */
class CDiskBlockIndex : public CBlockIndex
{
private:
uint256 blockHash;

public:
uint256 hashPrev;

CDiskBlockIndex() {
hashPrev = uint256();
blockHash = uint256();
}

explicit CDiskBlockIndex(const CBlockIndex* pindex) : CBlockIndex(*pindex) {
Expand All @@ -361,6 +369,10 @@ class CDiskBlockIndex : public CBlockIndex
if (nStatus & BLOCK_HAVE_UNDO)
READWRITE(VARINT(nUndoPos));

if (!ser_action.ForRead() && blockHash.IsNull())
GetBlockHash();
READWRITE(blockHash);

// block header
READWRITE(this->nVersion);
READWRITE(hashPrev);
Expand All @@ -372,14 +384,20 @@ class CDiskBlockIndex : public CBlockIndex

uint256 GetBlockHash() const
{
if (fUseFastIndex && (nTime < GetAdjustedTime() - 24 * 60 * 60) && !blockHash.IsNull()) // TODO: clock drift settings.
return blockHash;

CBlockHeader block;
block.nVersion = nVersion;
block.hashPrevBlock = hashPrev;
block.hashMerkleRoot = hashMerkleRoot;
block.nTime = nTime;
block.nBits = nBits;
block.nNonce = nNonce;
return block.GetHash();

const_cast<CDiskBlockIndex*>(this)->blockHash = block.GetHash();

return blockHash;
}


Expand Down
4 changes: 4 additions & 0 deletions src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
using namespace std;

bool fFeeEstimatesInitialized = false;
bool fUseFastIndex;
static const bool DEFAULT_PROXYRANDOMIZE = true;
static const bool DEFAULT_REST_ENABLE = false;
static const bool DEFAULT_DISABLE_SAFEMODE = false;
Expand Down Expand Up @@ -843,6 +844,9 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler)

// also see: InitParameterInteraction()

// whether to recalculate block hashes on startup or not
fUseFastIndex = GetBoolArg("-fastindex", true);

// if using block pruning, then disable txindex
if (GetArg("-prune", 0)||!GetBoolArg("-autorequestblocks", DEFAULT_AUTOMATIC_BLOCK_REQUESTS)) {
if (GetBoolArg("-txindex", DEFAULT_TXINDEX))
Expand Down
1 change: 1 addition & 0 deletions src/test/test_bitcoin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <boost/test/unit_test.hpp>
#include <boost/thread.hpp>

bool fUseFastIndex;
std::unique_ptr<CConnman> g_connman;
FastRandomContext insecure_rand_ctx(true);

Expand Down
3 changes: 2 additions & 1 deletion src/txdb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,9 @@ bool CBlockTreeDB::LoadBlockIndexGuts(std::function<CBlockIndex*(const uint256&)
if (pcursor->GetKey(key) && key.first == 'b') {
CDiskBlockIndex diskindex;
if (pcursor->GetValue(diskindex)) {
uint256 blockHash = diskindex.GetBlockHash();
// Construct block index object
CBlockIndex* pindexNew = insertBlockIndex(diskindex.GetBlockHash());
CBlockIndex* pindexNew = insertBlockIndex(blockHash);
pindexNew->pprev = insertBlockIndex(diskindex.hashPrev);
pindexNew->nHeight = diskindex.nHeight;
pindexNew->nFile = diskindex.nFile;
Expand Down

0 comments on commit bbabd0f

Please sign in to comment.