@@ -94,18 +94,14 @@ BlockAssembler::BlockAssembler(const CChainParams& _chainparams)
94
94
nBlockMaxCost = nBlockMaxSize * WITNESS_SCALE_FACTOR;
95
95
}
96
96
}
97
+
97
98
// Limit cost to between 4K and MAX_BLOCK_COST-4K for sanity:
98
99
nBlockMaxCost = std::max ((unsigned int )4000 , std::min ((unsigned int )(MAX_BLOCK_COST-4000 ), nBlockMaxCost));
99
100
// Limit size to between 1K and MAX_BLOCK_SERIALIZED_SIZE-1K for sanity:
100
101
nBlockMaxSize = std::max ((unsigned int )1000 , std::min ((unsigned int )(MAX_BLOCK_SERIALIZED_SIZE-1000 ), nBlockMaxSize));
101
102
102
- // Minimum block size you want to create; block will be filled with free transactions
103
- // until there are no more or the block reaches this size:
104
- nBlockMinSize = GetArg (" -blockminsize" , DEFAULT_BLOCK_MIN_SIZE);
105
- nBlockMinSize = std::min (nBlockMaxSize, nBlockMinSize);
106
-
107
103
// Whether we need to account for byte usage (in addition to cost usage)
108
- fNeedSizeAccounting = (nBlockMaxSize < MAX_BLOCK_SERIALIZED_SIZE-1000 ) || (nBlockMinSize > 0 ) ;
104
+ fNeedSizeAccounting = (nBlockMaxSize < MAX_BLOCK_SERIALIZED_SIZE-1000 );
109
105
}
110
106
111
107
void BlockAssembler::resetBlock ()
@@ -167,13 +163,7 @@ CBlockTemplate* BlockAssembler::CreateNewBlock(const CScript& scriptPubKeyIn)
167
163
fIncludeWitness = IsWitnessEnabled (pindexPrev, chainparams.GetConsensus ());
168
164
169
165
addPriorityTxs ();
170
- if (fNeedSizeAccounting ) {
171
- // addPackageTxs (the CPFP-based algorithm) cannot deal with size based
172
- // accounting, so fall back to the old algorithm.
173
- addScoreTxs ();
174
- } else {
175
- addPackageTxs ();
176
- }
166
+ addPackageTxs ();
177
167
178
168
nLastBlockTx = nBlockTx;
179
169
nLastBlockSize = nBlockSize;
@@ -241,13 +231,26 @@ bool BlockAssembler::TestPackage(uint64_t packageSize, int64_t packageSigOpsCost
241
231
return true ;
242
232
}
243
233
244
- // Block size and sigops have already been tested. Check that all transactions
245
- // are final.
246
- bool BlockAssembler::TestPackageFinality (const CTxMemPool::setEntries& package)
234
+ // Perform transaction-level checks before adding to block:
235
+ // - transaction finality (locktime)
236
+ // - premature witness (in case segwit transactions are added to mempool before
237
+ // segwit activation)
238
+ // - serialized size (in case -blockmaxsize is in use)
239
+ bool BlockAssembler::TestPackageTransactions (const CTxMemPool::setEntries& package)
247
240
{
241
+ uint64_t nPotentialBlockSize = nBlockSize; // only used with fNeedSizeAccounting
248
242
BOOST_FOREACH (const CTxMemPool::txiter it, package) {
249
243
if (!IsFinalTx (it->GetTx (), nHeight, nLockTimeCutoff))
250
244
return false ;
245
+ if (!fIncludeWitness && !it->GetTx ().wit .IsNull ())
246
+ return false ;
247
+ if (fNeedSizeAccounting ) {
248
+ uint64_t nTxSize = ::GetSerializeSize (it->GetTx (), SER_NETWORK, PROTOCOL_VERSION);
249
+ if (nPotentialBlockSize + nTxSize >= nBlockMaxSize) {
250
+ return false ;
251
+ }
252
+ nPotentialBlockSize += nTxSize;
253
+ }
251
254
}
252
255
return true ;
253
256
}
@@ -330,66 +333,6 @@ void BlockAssembler::AddToBlock(CTxMemPool::txiter iter)
330
333
}
331
334
}
332
335
333
- void BlockAssembler::addScoreTxs ()
334
- {
335
- std::priority_queue<CTxMemPool::txiter, std::vector<CTxMemPool::txiter>, ScoreCompare> clearedTxs;
336
- CTxMemPool::setEntries waitSet;
337
- CTxMemPool::indexed_transaction_set::index<mining_score>::type::iterator mi = mempool.mapTx .get <mining_score>().begin ();
338
- CTxMemPool::txiter iter;
339
- while (!blockFinished && (mi != mempool.mapTx .get <mining_score>().end () || !clearedTxs.empty ()))
340
- {
341
- // If no txs that were previously postponed are available to try
342
- // again, then try the next highest score tx
343
- if (clearedTxs.empty ()) {
344
- iter = mempool.mapTx .project <0 >(mi);
345
- mi++;
346
- }
347
- // If a previously postponed tx is available to try again, then it
348
- // has higher score than all untried so far txs
349
- else {
350
- iter = clearedTxs.top ();
351
- clearedTxs.pop ();
352
- }
353
-
354
- // If tx already in block, skip (added by addPriorityTxs)
355
- if (inBlock.count (iter)) {
356
- continue ;
357
- }
358
-
359
- // cannot accept witness transactions into a non-witness block
360
- if (!fIncludeWitness && !iter->GetTx ().wit .IsNull ())
361
- continue ;
362
-
363
- // If tx is dependent on other mempool txs which haven't yet been included
364
- // then put it in the waitSet
365
- if (isStillDependent (iter)) {
366
- waitSet.insert (iter);
367
- continue ;
368
- }
369
-
370
- // If the fee rate is below the min fee rate for mining, then we're done
371
- // adding txs based on score (fee rate)
372
- if (iter->GetModifiedFee () < ::minRelayTxFee.GetFee (iter->GetTxSize ()) && nBlockSize >= nBlockMinSize) {
373
- return ;
374
- }
375
-
376
- // If this tx fits in the block add it, otherwise keep looping
377
- if (TestForBlock (iter)) {
378
- AddToBlock (iter);
379
-
380
- // This tx was successfully added, so
381
- // add transactions that depend on this one to the priority queue to try again
382
- BOOST_FOREACH (CTxMemPool::txiter child, mempool.GetMemPoolChildren (iter))
383
- {
384
- if (waitSet.count (child)) {
385
- clearedTxs.push (child);
386
- waitSet.erase (child);
387
- }
388
- }
389
- }
390
- }
391
- }
392
-
393
336
void BlockAssembler::UpdatePackagesForAdded (const CTxMemPool::setEntries& alreadyAdded,
394
337
indexed_modified_transaction_set &mapModifiedTx)
395
338
{
@@ -539,7 +482,7 @@ void BlockAssembler::addPackageTxs()
539
482
ancestors.insert (iter);
540
483
541
484
// Test if all tx's are Final
542
- if (!TestPackageFinality (ancestors)) {
485
+ if (!TestPackageTransactions (ancestors)) {
543
486
if (fUsingModified ) {
544
487
mapModifiedTx.get <ancestor_score>().erase (modit);
545
488
failedTx.insert (iter);
@@ -573,6 +516,7 @@ void BlockAssembler::addPriorityTxs()
573
516
return ;
574
517
}
575
518
519
+ bool fSizeAccounting = fNeedSizeAccounting ;
576
520
fNeedSizeAccounting = true ;
577
521
578
522
// This vector will be sorted into a priority queue:
@@ -624,7 +568,7 @@ void BlockAssembler::addPriorityTxs()
624
568
// If now that this txs is added we've surpassed our desired priority size
625
569
// or have dropped below the AllowFreeThreshold, then we're done adding priority txs
626
570
if (nBlockSize >= nBlockPrioritySize || !AllowFree (actualPriority)) {
627
- return ;
571
+ break ;
628
572
}
629
573
630
574
// This tx was successfully added, so
@@ -640,6 +584,7 @@ void BlockAssembler::addPriorityTxs()
640
584
}
641
585
}
642
586
}
587
+ fNeedSizeAccounting = fSizeAccounting ;
643
588
}
644
589
645
590
void IncrementExtraNonce (CBlock* pblock, const CBlockIndex* pindexPrev, unsigned int & nExtraNonce)
0 commit comments