From e55c1ef81e552b4258ccac838a3eb0e52ca54644 Mon Sep 17 00:00:00 2001 From: Just Wonder Date: Fri, 20 Mar 2020 01:24:08 -0700 Subject: [PATCH] Fixed Initial Block Download issue with RandomKeva. --- src/bitcoin-tx.cpp | 6 ++++++ src/primitives/block.cpp | 18 ++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/src/bitcoin-tx.cpp b/src/bitcoin-tx.cpp index 0e92b134f..d17c25a2e 100644 --- a/src/bitcoin-tx.cpp +++ b/src/bitcoin-tx.cpp @@ -33,6 +33,12 @@ static const int CONTINUE_EXECUTION=-1; // To fix the chainActive not defined error. CChain chainActive; +struct BlockHasher +{ + size_t operator()(const uint256& hash) const { return hash.GetCheapHash(); } +}; +typedef std::unordered_map BlockMap; +BlockMap mapBlockIndex; // // This function returns either one of EXIT_ codes when it's expected to stop the process or diff --git a/src/primitives/block.cpp b/src/primitives/block.cpp index 40f269007..7f5db08c1 100644 --- a/src/primitives/block.cpp +++ b/src/primitives/block.cpp @@ -43,6 +43,24 @@ unsigned get_max_concurrency() static uint256 cn_get_block_hash_by_height(uint64_t seed_height, char cnHash[32]) { CBlockIndex* pblockindex = chainActive[seed_height]; + if (pblockindex == NULL) { + // This will only happens during initial block download. + static std::map mapBlockHeight; + std::map::iterator iter = mapBlockHeight.find(seed_height); + if (iter != mapBlockHeight.end()) { + pblockindex = iter->second; + } else { + for (const std::pair& item : mapBlockIndex) + { + CBlockIndex* pindex = item.second; + if (pindex->nNonce == seed_height) { + pblockindex = pindex; + mapBlockHeight.insert(std::make_pair(seed_height, pindex)); + break; + } + } + } + } uint256 blockHash = pblockindex->GetBlockHash(); const unsigned char* pHash = blockHash.begin(); for (int j = 31; j >= 0; j--) {