Skip to content

Commit f15c2cd

Browse files
committed
CreateNewBlock: add support for size-accounting to addPackageTxs
Includes a change to not continue to use size-accounting in addScoreTxs or addPackageTxs just because addPriorityTxs() is used.
1 parent 1922e5a commit f15c2cd

File tree

2 files changed

+16
-11
lines changed

2 files changed

+16
-11
lines changed

src/miner.cpp

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ BlockAssembler::BlockAssembler(const CChainParams& _chainparams)
9494
nBlockMaxCost = nBlockMaxSize * WITNESS_SCALE_FACTOR;
9595
}
9696
}
97+
9798
// Limit cost to between 4K and MAX_BLOCK_COST-4K for sanity:
9899
nBlockMaxCost = std::max((unsigned int)4000, std::min((unsigned int)(MAX_BLOCK_COST-4000), nBlockMaxCost));
99100
// Limit size to between 1K and MAX_BLOCK_SERIALIZED_SIZE-1K for sanity:
@@ -167,13 +168,7 @@ CBlockTemplate* BlockAssembler::CreateNewBlock(const CScript& scriptPubKeyIn)
167168
fIncludeWitness = IsWitnessEnabled(pindexPrev, chainparams.GetConsensus());
168169

169170
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-
}
171+
addPackageTxs();
177172

178173
nLastBlockTx = nBlockTx;
179174
nLastBlockSize = nBlockSize;
@@ -243,11 +238,19 @@ bool BlockAssembler::TestPackage(uint64_t packageSize, int64_t packageSigOpsCost
243238

244239
// Block size and sigops have already been tested. Check that all transactions
245240
// are final.
246-
bool BlockAssembler::TestPackageFinality(const CTxMemPool::setEntries& package)
241+
bool BlockAssembler::TestPackageFinalityAndSerializedSize(const CTxMemPool::setEntries& package)
247242
{
243+
uint64_t nPotentialBlockSize = nBlockSize; // only used with fNeedSizeAccounting
248244
BOOST_FOREACH (const CTxMemPool::txiter it, package) {
249245
if (!IsFinalTx(it->GetTx(), nHeight, nLockTimeCutoff))
250246
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+
}
251254
}
252255
return true;
253256
}
@@ -539,7 +542,7 @@ void BlockAssembler::addPackageTxs()
539542
ancestors.insert(iter);
540543

541544
// Test if all tx's are Final
542-
if (!TestPackageFinality(ancestors)) {
545+
if (!TestPackageFinalityAndSerializedSize(ancestors)) {
543546
if (fUsingModified) {
544547
mapModifiedTx.get<ancestor_score>().erase(modit);
545548
failedTx.insert(iter);
@@ -573,6 +576,7 @@ void BlockAssembler::addPriorityTxs()
573576
return;
574577
}
575578

579+
bool fSizeAccounting = fNeedSizeAccounting;
576580
fNeedSizeAccounting = true;
577581

578582
// This vector will be sorted into a priority queue:
@@ -624,7 +628,7 @@ void BlockAssembler::addPriorityTxs()
624628
// If now that this txs is added we've surpassed our desired priority size
625629
// or have dropped below the AllowFreeThreshold, then we're done adding priority txs
626630
if (nBlockSize >= nBlockPrioritySize || !AllowFree(actualPriority)) {
627-
return;
631+
break;
628632
}
629633

630634
// This tx was successfully added, so
@@ -640,6 +644,7 @@ void BlockAssembler::addPriorityTxs()
640644
}
641645
}
642646
}
647+
fNeedSizeAccounting = fSizeAccounting;
643648
}
644649

645650
void IncrementExtraNonce(CBlock* pblock, const CBlockIndex* pindexPrev, unsigned int& nExtraNonce)

src/miner.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ class BlockAssembler
193193
/** Test if a new package would "fit" in the block */
194194
bool TestPackage(uint64_t packageSize, int64_t packageSigOpsCost);
195195
/** Test if a set of transactions are all final */
196-
bool TestPackageFinality(const CTxMemPool::setEntries& package);
196+
bool TestPackageFinalityAndSerializedSize(const CTxMemPool::setEntries& package);
197197
/** Return true if given transaction from mapTx has already been evaluated,
198198
* or if the transaction's cached data in mapTx is incorrect. */
199199
bool SkipMapTxEntry(CTxMemPool::txiter it, indexed_modified_transaction_set &mapModifiedTx, CTxMemPool::setEntries &failedTx);

0 commit comments

Comments
 (0)