Skip to content

Commit eed816a

Browse files
committed
Mining: return early when block is almost full
1 parent 8bfa13b commit eed816a

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
@@ -358,6 +358,13 @@ void BlockAssembler::addPackageTxs()
358358

359359
CTxMemPool::indexed_transaction_set::index<ancestor_score>::type::iterator mi = mempool.mapTx.get<ancestor_score>().begin();
360360
CTxMemPool::txiter iter;
361+
362+
// Limit the number of attempts to add transactions to the block when it is
363+
// close to full; this is just a simple heuristic to finish quickly if the
364+
// mempool has a lot of entries.
365+
const int64_t MAX_CONSECUTIVE_FAILURES = 1000;
366+
int64_t nConsecutiveFailed = 0;
367+
361368
while (mi != mempool.mapTx.get<ancestor_score>().end() || !mapModifiedTx.empty())
362369
{
363370
// First try to find a new transaction in mapTx to evaluate.
@@ -419,6 +426,14 @@ void BlockAssembler::addPackageTxs()
419426
mapModifiedTx.get<ancestor_score>().erase(modit);
420427
failedTx.insert(iter);
421428
}
429+
430+
++nConsecutiveFailed;
431+
432+
if (nConsecutiveFailed > MAX_CONSECUTIVE_FAILURES && nBlockWeight >
433+
nBlockMaxWeight - 4000) {
434+
// Give up if we're close to full and haven't succeeded in a while
435+
break;
436+
}
422437
continue;
423438
}
424439

@@ -439,6 +454,9 @@ void BlockAssembler::addPackageTxs()
439454
continue;
440455
}
441456

457+
// This transaction will make it in; reset the failed counter.
458+
nConsecutiveFailed = 0;
459+
442460
// Package can be added. Sort the entries in a valid order.
443461
std::vector<CTxMemPool::txiter> sortedEntries;
444462
SortForBlock(ancestors, iter, sortedEntries);

0 commit comments

Comments
 (0)