From c0cb27465916ecfdf27a7e32d7eddd17c41846f1 Mon Sep 17 00:00:00 2001 From: Alexander Block Date: Tue, 12 Feb 2019 20:52:06 +0100 Subject: [PATCH] Fix incorrect usage of begin() when genesis block is requested in "protx diff" (#2699) * Fix incorrect usage of begin() when genesis block is requested in "protx diff" .begin() on mapBlockIndex does NOT return the genesis block, but just the block with lowest hash. The fix is to use chainActive[0] to get the genesis block. * Update src/evo/simplifiedmns.cpp Co-Authored-By: codablock --- src/evo/simplifiedmns.cpp | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/src/evo/simplifiedmns.cpp b/src/evo/simplifiedmns.cpp index 2b9d730f44c12..e8428398594cf 100644 --- a/src/evo/simplifiedmns.cpp +++ b/src/evo/simplifiedmns.cpp @@ -119,25 +119,27 @@ bool BuildSimplifiedMNListDiff(const uint256& baseBlockHash, const uint256& bloc AssertLockHeld(cs_main); mnListDiffRet = CSimplifiedMNListDiff(); - BlockMap::iterator baseBlockIt = mapBlockIndex.begin(); + const CBlockIndex* baseBlockIndex = chainActive.Genesis(); if (!baseBlockHash.IsNull()) { - baseBlockIt = mapBlockIndex.find(baseBlockHash); + auto it = mapBlockIndex.find(baseBlockHash); + if (it == mapBlockIndex.end()) { + errorRet = strprintf("block %s not found", baseBlockHash.ToString()); + return false; + } + baseBlockIndex = it->second; } auto blockIt = mapBlockIndex.find(blockHash); - if (baseBlockIt == mapBlockIndex.end()) { - errorRet = strprintf("block %s not found", baseBlockHash.ToString()); - return false; - } if (blockIt == mapBlockIndex.end()) { errorRet = strprintf("block %s not found", blockHash.ToString()); return false; } + const CBlockIndex* blockIndex = blockIt->second; - if (!chainActive.Contains(baseBlockIt->second) || !chainActive.Contains(blockIt->second)) { + if (!chainActive.Contains(baseBlockIndex) || !chainActive.Contains(blockIndex)) { errorRet = strprintf("block %s and %s are not in the same chain", baseBlockHash.ToString(), blockHash.ToString()); return false; } - if (baseBlockIt->second->nHeight > blockIt->second->nHeight) { + if (baseBlockIndex->nHeight > blockIndex->nHeight) { errorRet = strprintf("base block %s is higher then block %s", baseBlockHash.ToString(), blockHash.ToString()); return false; } @@ -150,7 +152,7 @@ bool BuildSimplifiedMNListDiff(const uint256& baseBlockHash, const uint256& bloc // TODO store coinbase TX in CBlockIndex CBlock block; - if (!ReadBlockFromDisk(block, blockIt->second, Params().GetConsensus())) { + if (!ReadBlockFromDisk(block, blockIndex, Params().GetConsensus())) { errorRet = strprintf("failed to read block %s from disk", blockHash.ToString()); return false; }