aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSuhas Daftuar <sdaftuar@gmail.com>2017-03-06 11:16:06 -0500
committerSuhas Daftuar <sdaftuar@gmail.com>2017-03-29 13:57:52 -0400
commiteed816af6c68c0c67f5fc05472a3927db62f8a18 (patch)
treee356cb6d6aa88d986d22eac90d7810a7c41e2f6b
parent8bfa13b15b84cb372950fb7b25a1080173060b6a (diff)
Mining: return early when block is almost full
-rw-r--r--src/miner.cpp18
1 files changed, 18 insertions, 0 deletions
diff --git a/src/miner.cpp b/src/miner.cpp
index 67e7ff155b..570053a53b 100644
--- a/src/miner.cpp
+++ b/src/miner.cpp
@@ -358,6 +358,13 @@ void BlockAssembler::addPackageTxs()
CTxMemPool::indexed_transaction_set::index<ancestor_score>::type::iterator mi = mempool.mapTx.get<ancestor_score>().begin();
CTxMemPool::txiter iter;
+
+ // Limit the number of attempts to add transactions to the block when it is
+ // close to full; this is just a simple heuristic to finish quickly if the
+ // mempool has a lot of entries.
+ const int64_t MAX_CONSECUTIVE_FAILURES = 1000;
+ int64_t nConsecutiveFailed = 0;
+
while (mi != mempool.mapTx.get<ancestor_score>().end() || !mapModifiedTx.empty())
{
// First try to find a new transaction in mapTx to evaluate.
@@ -419,6 +426,14 @@ void BlockAssembler::addPackageTxs()
mapModifiedTx.get<ancestor_score>().erase(modit);
failedTx.insert(iter);
}
+
+ ++nConsecutiveFailed;
+
+ if (nConsecutiveFailed > MAX_CONSECUTIVE_FAILURES && nBlockWeight >
+ nBlockMaxWeight - 4000) {
+ // Give up if we're close to full and haven't succeeded in a while
+ break;
+ }
continue;
}
@@ -439,6 +454,9 @@ void BlockAssembler::addPackageTxs()
continue;
}
+ // This transaction will make it in; reset the failed counter.
+ nConsecutiveFailed = 0;
+
// Package can be added. Sort the entries in a valid order.
std::vector<CTxMemPool::txiter> sortedEntries;
SortForBlock(ancestors, iter, sortedEntries);