diff --git a/src/chain.h b/src/chain.h index 240dd6058..068090523 100644 --- a/src/chain.h +++ b/src/chain.h @@ -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; @@ -215,6 +219,8 @@ class CBlockIndex nTime = 0; nBits = 0; nNonce = uint256(); + + cache_init = false; } CBlockIndex() @@ -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 { @@ -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; } diff --git a/src/main.cpp b/src/main.cpp index 14b84065f..43c748393 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -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. @@ -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 + 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); diff --git a/src/primitives/block.h b/src/primitives/block.h index a083dcf4b..a39081b1e 100644 --- a/src/primitives/block.h +++ b/src/primitives/block.h @@ -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; @@ -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; };