Skip to content

Commit b5c3440

Browse files
committed
Mining: return early when block is almost full
Github-Pull: #9959 Rebased-From: eed816a
1 parent b768202 commit b5c3440

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

src/miner.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,13 @@ void BlockAssembler::addPackageTxs()
416416

417417
CTxMemPool::indexed_transaction_set::index<ancestor_score>::type::iterator mi = mempool.mapTx.get<ancestor_score>().begin();
418418
CTxMemPool::txiter iter;
419+
420+
// Limit the number of attempts to add transactions to the block when it is
421+
// close to full; this is just a simple heuristic to finish quickly if the
422+
// mempool has a lot of entries.
423+
const int64_t MAX_CONSECUTIVE_FAILURES = 1000;
424+
int64_t nConsecutiveFailed = 0;
425+
419426
while (mi != mempool.mapTx.get<ancestor_score>().end() || !mapModifiedTx.empty())
420427
{
421428
// First try to find a new transaction in mapTx to evaluate.
@@ -477,6 +484,14 @@ void BlockAssembler::addPackageTxs()
477484
mapModifiedTx.get<ancestor_score>().erase(modit);
478485
failedTx.insert(iter);
479486
}
487+
488+
++nConsecutiveFailed;
489+
490+
if (nConsecutiveFailed > MAX_CONSECUTIVE_FAILURES && nBlockWeight >
491+
nBlockMaxWeight - 4000) {
492+
// Give up if we're close to full and haven't succeeded in a while
493+
break;
494+
}
480495
continue;
481496
}
482497

@@ -497,6 +512,9 @@ void BlockAssembler::addPackageTxs()
497512
continue;
498513
}
499514

515+
// This transaction will make it in; reset the failed counter.
516+
nConsecutiveFailed = 0;
517+
500518
// Package can be added. Sort the entries in a valid order.
501519
std::vector<CTxMemPool::txiter> sortedEntries;
502520
SortForBlock(ancestors, iter, sortedEntries);

0 commit comments

Comments
 (0)