diff options
author | Gavin Andresen <gavinandresen@gmail.com> | 2014-02-08 10:49:49 -0500 |
---|---|---|
committer | Gavin Andresen <gavinandresen@gmail.com> | 2014-02-08 10:49:49 -0500 |
commit | 95e66247ebaac88dadd081f850ebf86c71831e61 (patch) | |
tree | 7fdbefb3bfbc5c967e33c6a982db67fb656570ce /src/main.cpp | |
parent | f8bb2e9dd265332b956c09768796e6f4caeb4585 (diff) | |
parent | bbde1e99c893924dbef135f42c14f4df9828c6e5 (diff) |
Merge pull request #3609 from sipa/limitorphanblocks
Limit the number of orphan blocks in memory
Diffstat (limited to 'src/main.cpp')
-rw-r--r-- | src/main.cpp | 28 |
1 files changed, 27 insertions, 1 deletions
diff --git a/src/main.cpp b/src/main.cpp index 4532b776c6..d33ad2fa95 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1054,6 +1054,31 @@ uint256 static GetOrphanRoot(const uint256& hash) } while(true); } +// Remove a random orphan block (which does not have any dependent orphans). +void static PruneOrphanBlocks() +{ + if (mapOrphanBlocksByPrev.size() <= MAX_ORPHAN_BLOCKS) + return; + + // Pick a random orphan block. + int pos = insecure_rand() % mapOrphanBlocksByPrev.size(); + std::multimap<uint256, COrphanBlock*>::iterator it = mapOrphanBlocksByPrev.begin(); + while (pos--) it++; + + // As long as this block has other orphans depending on it, move to one of those successors. + do { + std::multimap<uint256, COrphanBlock*>::iterator it2 = mapOrphanBlocksByPrev.find(it->second->hashBlock); + if (it2 == mapOrphanBlocksByPrev.end()) + break; + it = it2; + } while(1); + + uint256 hash = it->second->hashBlock; + delete it->second; + mapOrphanBlocksByPrev.erase(it); + mapOrphanBlocks.erase(hash); +} + int64_t GetBlockValue(int nHeight, int64_t nFees) { int64_t nSubsidy = 50 * COIN; @@ -2373,10 +2398,11 @@ bool ProcessBlock(CValidationState &state, CNode* pfrom, CBlock* pblock, CDiskBl // If we don't already have its previous block, shunt it off to holding area until we get it if (pblock->hashPrevBlock != 0 && !mapBlockIndex.count(pblock->hashPrevBlock)) { - LogPrintf("ProcessBlock: ORPHAN BLOCK, prev=%s\n", pblock->hashPrevBlock.ToString()); + LogPrintf("ProcessBlock: ORPHAN BLOCK %lu, prev=%s\n", (unsigned long)mapOrphanBlocks.size(), pblock->hashPrevBlock.ToString()); // Accept orphans as long as there is a node to request its parents from if (pfrom) { + PruneOrphanBlocks(); COrphanBlock* pblock2 = new COrphanBlock(); { CDataStream ss(SER_DISK, CLIENT_VERSION); |