@@ -126,6 +126,8 @@ void BlockAssembler::resetBlock()
126
126
127
127
std::unique_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock (const CScript& scriptPubKeyIn, bool fMineWitnessTx )
128
128
{
129
+ int64_t nTimeStart = GetTimeMicros ();
130
+
129
131
resetBlock ();
130
132
131
133
pblocktemplate.reset (new CBlockTemplate ());
@@ -164,7 +166,11 @@ std::unique_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock(const CScript& sc
164
166
// transaction (which in most cases can be a no-op).
165
167
fIncludeWitness = IsWitnessEnabled (pindexPrev, chainparams.GetConsensus ()) && fMineWitnessTx ;
166
168
167
- addPackageTxs ();
169
+ int nPackagesSelected = 0 ;
170
+ int nDescendantsUpdated = 0 ;
171
+ addPackageTxs (nPackagesSelected, nDescendantsUpdated);
172
+
173
+ int64_t nTime1 = GetTimeMicros ();
168
174
169
175
nLastBlockTx = nBlockTx;
170
176
nLastBlockSize = nBlockSize;
@@ -196,6 +202,9 @@ std::unique_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock(const CScript& sc
196
202
if (!TestBlockValidity (state, chainparams, *pblock, pindexPrev, false , false )) {
197
203
throw std::runtime_error (strprintf (" %s: TestBlockValidity failed: %s" , __func__, FormatStateMessage (state)));
198
204
}
205
+ int64_t nTime2 = GetTimeMicros ();
206
+
207
+ LogPrint (" bench" , " CreateNewBlock() packages: %.2fms (%d packages, %d updated descendants), validity: %.2fms (total %.2fms)\n " , 0.001 * (nTime1 - nTimeStart), nPackagesSelected, nDescendantsUpdated, 0.001 * (nTime2 - nTime1), 0.001 * (nTime2 - nTimeStart));
199
208
200
209
return std::move (pblocktemplate);
201
210
}
@@ -269,16 +278,18 @@ void BlockAssembler::AddToBlock(CTxMemPool::txiter iter)
269
278
}
270
279
}
271
280
272
- void BlockAssembler::UpdatePackagesForAdded (const CTxMemPool::setEntries& alreadyAdded,
281
+ int BlockAssembler::UpdatePackagesForAdded (const CTxMemPool::setEntries& alreadyAdded,
273
282
indexed_modified_transaction_set &mapModifiedTx)
274
283
{
284
+ int nDescendantsUpdated = 0 ;
275
285
BOOST_FOREACH (const CTxMemPool::txiter it, alreadyAdded) {
276
286
CTxMemPool::setEntries descendants;
277
287
mempool.CalculateDescendants (it, descendants);
278
288
// Insert all descendants (not yet in block) into the modified set
279
289
BOOST_FOREACH (CTxMemPool::txiter desc, descendants) {
280
290
if (alreadyAdded.count (desc))
281
291
continue ;
292
+ ++nDescendantsUpdated;
282
293
modtxiter mit = mapModifiedTx.find (desc);
283
294
if (mit == mapModifiedTx.end ()) {
284
295
CTxMemPoolModifiedEntry modEntry (desc);
@@ -291,6 +302,7 @@ void BlockAssembler::UpdatePackagesForAdded(const CTxMemPool::setEntries& alread
291
302
}
292
303
}
293
304
}
305
+ return nDescendantsUpdated;
294
306
}
295
307
296
308
// Skip entries in mapTx that are already in a block or are present
@@ -331,7 +343,7 @@ void BlockAssembler::SortForBlock(const CTxMemPool::setEntries& package, CTxMemP
331
343
// Each time through the loop, we compare the best transaction in
332
344
// mapModifiedTxs with the next transaction in the mempool to decide what
333
345
// transaction package to work on next.
334
- void BlockAssembler::addPackageTxs ()
346
+ void BlockAssembler::addPackageTxs (int &nPackagesSelected, int &nDescendantsUpdated )
335
347
{
336
348
// mapModifiedTx will store sorted packages after they are modified
337
349
// because some of their txs are already in the block
@@ -345,6 +357,13 @@ void BlockAssembler::addPackageTxs()
345
357
346
358
CTxMemPool::indexed_transaction_set::index<ancestor_score>::type::iterator mi = mempool.mapTx .get <ancestor_score>().begin ();
347
359
CTxMemPool::txiter iter;
360
+
361
+ // Limit the number of attempts to add transactions to the block when it is
362
+ // close to full; this is just a simple heuristic to finish quickly if the
363
+ // mempool has a lot of entries.
364
+ const int64_t MAX_CONSECUTIVE_FAILURES = 1000 ;
365
+ int64_t nConsecutiveFailed = 0 ;
366
+
348
367
while (mi != mempool.mapTx .get <ancestor_score>().end () || !mapModifiedTx.empty ())
349
368
{
350
369
// First try to find a new transaction in mapTx to evaluate.
@@ -406,6 +425,14 @@ void BlockAssembler::addPackageTxs()
406
425
mapModifiedTx.get <ancestor_score>().erase (modit);
407
426
failedTx.insert (iter);
408
427
}
428
+
429
+ ++nConsecutiveFailed;
430
+
431
+ if (nConsecutiveFailed > MAX_CONSECUTIVE_FAILURES && nBlockWeight >
432
+ nBlockMaxWeight - 4000 ) {
433
+ // Give up if we're close to full and haven't succeeded in a while
434
+ break ;
435
+ }
409
436
continue ;
410
437
}
411
438
@@ -426,6 +453,9 @@ void BlockAssembler::addPackageTxs()
426
453
continue ;
427
454
}
428
455
456
+ // This transaction will make it in; reset the failed counter.
457
+ nConsecutiveFailed = 0 ;
458
+
429
459
// Package can be added. Sort the entries in a valid order.
430
460
std::vector<CTxMemPool::txiter> sortedEntries;
431
461
SortForBlock (ancestors, iter, sortedEntries);
@@ -436,8 +466,10 @@ void BlockAssembler::addPackageTxs()
436
466
mapModifiedTx.erase (sortedEntries[i]);
437
467
}
438
468
469
+ ++nPackagesSelected;
470
+
439
471
// Update transactions that depend on each of these
440
- UpdatePackagesForAdded (ancestors, mapModifiedTx);
472
+ nDescendantsUpdated += UpdatePackagesForAdded (ancestors, mapModifiedTx);
441
473
}
442
474
}
443
475
0 commit comments