Skip to content

Commit 95e6624

Browse files
committed
Merge pull request #3609 from sipa/limitorphanblocks
Limit the number of orphan blocks in memory
2 parents f8bb2e9 + bbde1e9 commit 95e6624

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

src/main.cpp

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1054,6 +1054,31 @@ uint256 static GetOrphanRoot(const uint256& hash)
10541054
} while(true);
10551055
}
10561056

1057+
// Remove a random orphan block (which does not have any dependent orphans).
1058+
void static PruneOrphanBlocks()
1059+
{
1060+
if (mapOrphanBlocksByPrev.size() <= MAX_ORPHAN_BLOCKS)
1061+
return;
1062+
1063+
// Pick a random orphan block.
1064+
int pos = insecure_rand() % mapOrphanBlocksByPrev.size();
1065+
std::multimap<uint256, COrphanBlock*>::iterator it = mapOrphanBlocksByPrev.begin();
1066+
while (pos--) it++;
1067+
1068+
// As long as this block has other orphans depending on it, move to one of those successors.
1069+
do {
1070+
std::multimap<uint256, COrphanBlock*>::iterator it2 = mapOrphanBlocksByPrev.find(it->second->hashBlock);
1071+
if (it2 == mapOrphanBlocksByPrev.end())
1072+
break;
1073+
it = it2;
1074+
} while(1);
1075+
1076+
uint256 hash = it->second->hashBlock;
1077+
delete it->second;
1078+
mapOrphanBlocksByPrev.erase(it);
1079+
mapOrphanBlocks.erase(hash);
1080+
}
1081+
10571082
int64_t GetBlockValue(int nHeight, int64_t nFees)
10581083
{
10591084
int64_t nSubsidy = 50 * COIN;
@@ -2373,10 +2398,11 @@ bool ProcessBlock(CValidationState &state, CNode* pfrom, CBlock* pblock, CDiskBl
23732398
// If we don't already have its previous block, shunt it off to holding area until we get it
23742399
if (pblock->hashPrevBlock != 0 && !mapBlockIndex.count(pblock->hashPrevBlock))
23752400
{
2376-
LogPrintf("ProcessBlock: ORPHAN BLOCK, prev=%s\n", pblock->hashPrevBlock.ToString());
2401+
LogPrintf("ProcessBlock: ORPHAN BLOCK %lu, prev=%s\n", (unsigned long)mapOrphanBlocks.size(), pblock->hashPrevBlock.ToString());
23772402

23782403
// Accept orphans as long as there is a node to request its parents from
23792404
if (pfrom) {
2405+
PruneOrphanBlocks();
23802406
COrphanBlock* pblock2 = new COrphanBlock();
23812407
{
23822408
CDataStream ss(SER_DISK, CLIENT_VERSION);

src/main.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ static const unsigned int MAX_STANDARD_TX_SIZE = 100000;
4545
static const unsigned int MAX_BLOCK_SIGOPS = MAX_BLOCK_SIZE/50;
4646
/** The maximum number of orphan transactions kept in memory */
4747
static const unsigned int MAX_ORPHAN_TRANSACTIONS = MAX_BLOCK_SIZE/100;
48+
/** The maximum number of orphan blocks kept in memory */
49+
static const unsigned int MAX_ORPHAN_BLOCKS = 750;
4850
/** The maximum size of a blk?????.dat file (since 0.8) */
4951
static const unsigned int MAX_BLOCKFILE_SIZE = 0x8000000; // 128 MiB
5052
/** The pre-allocation chunk size for blk?????.dat files (since 0.8) */

0 commit comments

Comments
 (0)