Skip to content

Commit

Permalink
Cache PoW hashes in CBlockIndex objects
Browse files Browse the repository at this point in the history
  • Loading branch information
solardiz committed Mar 31, 2019
1 parent a73d694 commit 3c80dac
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 5 deletions.
13 changes: 13 additions & 0 deletions src/chain.h
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,10 @@ class CBlockIndex
unsigned int nBits;
uint256 nNonce;

//! (currently memory only, but don't have to be)
bool cache_init;
uint256 cache_block_hash, cache_PoW_hash;

//! (memory only) Sequential id assigned to distinguish order in which blocks are received.
uint32_t nSequenceId;

Expand Down Expand Up @@ -215,6 +219,8 @@ class CBlockIndex
nTime = 0;
nBits = 0;
nNonce = uint256();

cache_init = false;
}

CBlockIndex()
Expand All @@ -232,6 +238,10 @@ class CBlockIndex
nTime = block.nTime;
nBits = block.nBits;
nNonce = block.nNonce;

cache_init = block.cache_init;
cache_block_hash = block.cache_block_hash;
cache_PoW_hash = block.cache_PoW_hash;
}

CDiskBlockPos GetBlockPos() const {
Expand Down Expand Up @@ -263,6 +273,9 @@ class CBlockIndex
block.nTime = nTime;
block.nBits = nBits;
block.nNonce = nNonce;
block.cache_init = cache_init;
block.cache_block_hash = cache_block_hash;
block.cache_PoW_hash = cache_PoW_hash;
return block;
}

Expand Down
21 changes: 21 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright (c) 2009-2010 Satoshi Nakamoto
// Copyright (c) 2009-2014 The Bitcoin Core developers
// Copyright (c) 2018-2019 The Resistance developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

Expand Down Expand Up @@ -3818,6 +3819,26 @@ static bool IsSuperMajority(int minVersion, const CBlockIndex* pstart, unsigned

bool ProcessNewBlock(CValidationState &state, CNode* pfrom, CBlock* pblock, bool fForceProcessing, CDiskBlockPos *dbp)
{
// Look for this block's header in the index like AcceptBlock() will
uint256 hash = pblock->GetHash();

{
LOCK(cs_main);

BlockMap::iterator miSelf = mapBlockIndex.find(hash);
CBlockIndex *pindex = NULL;
if (miSelf != mapBlockIndex.end()) {
// Block header is already known
pindex = miSelf->second;
if (!pblock->cache_init && pindex->cache_init) {
LOCK(pblock->cache_lock); // Probably unnecessary since no concurrent access to pblock is expected

This comment has been minimized.

Copy link
@solardiz

solardiz Mar 31, 2019

Author Collaborator

Do you mean create a function that would do the LOCK and the 3 assignments below? And accept new values for the cache contents as parameters? If so, I don't feel about this strongly, but I don't feel this would make the code any cleaner because that function would be very specialized and used in just this one place.

pblock->cache_init = true;
pblock->cache_block_hash = pindex->cache_block_hash;
pblock->cache_PoW_hash = pindex->cache_PoW_hash;
}
}
}

// Preliminary checks
auto verifier = libzcash::ProofVerifier::Disabled();
bool checked = CheckBlock(*pblock, state, verifier);
Expand Down
9 changes: 4 additions & 5 deletions src/primitives/block.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ class CBlockHeaderUncached
class CBlockHeader : public CBlockHeaderUncached
{
public:
mutable CCriticalSection cache_lock;
mutable bool cache_init;
mutable uint256 cache_block_hash, cache_PoW_hash;

CBlockHeader()
{
cache_init = false;
Expand All @@ -101,11 +105,6 @@ class CBlockHeader : public CBlockHeaderUncached
}

uint256 GetPoWHash_cached() const;

private:
mutable CCriticalSection cache_lock;
mutable bool cache_init;
mutable uint256 cache_block_hash, cache_PoW_hash;
};


Expand Down

0 comments on commit 3c80dac

Please sign in to comment.