Skip to content

Commit f5660d3

Browse files
committed
Merge #8295: Mining-related fixups for 0.13.0
c1d61fb Add warning if -blockminsize is used. (Suhas Daftuar) 27362dd Remove -blockminsize option (Suhas Daftuar) d2e46e1 Remove addScoreTxs() (Suhas Daftuar) 6dd4bc2 Exclude witness transactions in addPackageTxs() pre-segwit activation (Suhas Daftuar) f15c2cd CreateNewBlock: add support for size-accounting to addPackageTxs (Suhas Daftuar)
2 parents 238300b + c1d61fb commit f5660d3

File tree

5 files changed

+36
-89
lines changed

5 files changed

+36
-89
lines changed

contrib/devtools/check-doc.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
REGEX_ARG = re.compile(r'(?:map(?:Multi)?Args(?:\.count\(|\[)|Get(?:Bool)?Arg\()\"(\-[^\"]+?)\"')
2222
REGEX_DOC = re.compile(r'HelpMessageOpt\(\"(\-[^\"=]+?)(?:=|\")')
2323
# list unsupported, deprecated and duplicate args as they need no documentation
24-
SET_DOC_OPTIONAL = set(['-rpcssl', '-benchmark', '-h', '-help', '-socks', '-tor', '-debugnet', '-whitelistalwaysrelay', '-prematurewitness', '-walletprematurewitness', '-promiscuousmempoolflags'])
24+
SET_DOC_OPTIONAL = set(['-rpcssl', '-benchmark', '-h', '-help', '-socks', '-tor', '-debugnet', '-whitelistalwaysrelay', '-prematurewitness', '-walletprematurewitness', '-promiscuousmempoolflags', '-blockminsize'])
2525

2626
def main():
2727
used = check_output(CMD_GREP_ARGS, shell=True)

src/init.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,6 @@ std::string HelpMessage(HelpMessageMode mode)
453453

454454
strUsage += HelpMessageGroup(_("Block creation options:"));
455455
strUsage += HelpMessageOpt("-blockmaxcost=<n>", strprintf(_("Set maximum block cost (default: %d)"), DEFAULT_BLOCK_MAX_COST));
456-
strUsage += HelpMessageOpt("-blockminsize=<n>", strprintf(_("Set minimum block size in bytes (default: %u)"), DEFAULT_BLOCK_MIN_SIZE));
457456
strUsage += HelpMessageOpt("-blockmaxsize=<n>", strprintf(_("Set maximum block size in bytes (default: %d)"), DEFAULT_BLOCK_MAX_SIZE));
458457
strUsage += HelpMessageOpt("-blockprioritysize=<n>", strprintf(_("Set maximum size of high-priority/low-fee transactions in bytes (default: %d)"), DEFAULT_BLOCK_PRIORITY_SIZE));
459458
if (showDebug)
@@ -877,6 +876,9 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler)
877876
if (GetBoolArg("-whitelistalwaysrelay", false))
878877
InitWarning(_("Unsupported argument -whitelistalwaysrelay ignored, use -whitelistrelay and/or -whitelistforcerelay."));
879878

879+
if (mapArgs.count("-blockminsize"))
880+
InitWarning("Unsupported argument -blockminsize ignored.");
881+
880882
// Checkmempool and checkblockindex default to true in regtest mode
881883
int ratio = std::min<int>(std::max<int>(GetArg("-checkmempool", chainparams.DefaultConsistencyChecks() ? 1 : 0), 0), 1000000);
882884
if (ratio != 0) {

src/miner.cpp

Lines changed: 23 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -94,18 +94,14 @@ 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:
100101
nBlockMaxSize = std::max((unsigned int)1000, std::min((unsigned int)(MAX_BLOCK_SERIALIZED_SIZE-1000), nBlockMaxSize));
101102

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-
107103
// 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);
109105
}
110106

111107
void BlockAssembler::resetBlock()
@@ -167,13 +163,7 @@ CBlockTemplate* BlockAssembler::CreateNewBlock(const CScript& scriptPubKeyIn)
167163
fIncludeWitness = IsWitnessEnabled(pindexPrev, chainparams.GetConsensus());
168164

169165
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();
177167

178168
nLastBlockTx = nBlockTx;
179169
nLastBlockSize = nBlockSize;
@@ -241,13 +231,26 @@ bool BlockAssembler::TestPackage(uint64_t packageSize, int64_t packageSigOpsCost
241231
return true;
242232
}
243233

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)
247240
{
241+
uint64_t nPotentialBlockSize = nBlockSize; // only used with fNeedSizeAccounting
248242
BOOST_FOREACH (const CTxMemPool::txiter it, package) {
249243
if (!IsFinalTx(it->GetTx(), nHeight, nLockTimeCutoff))
250244
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+
}
251254
}
252255
return true;
253256
}
@@ -330,66 +333,6 @@ void BlockAssembler::AddToBlock(CTxMemPool::txiter iter)
330333
}
331334
}
332335

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-
393336
void BlockAssembler::UpdatePackagesForAdded(const CTxMemPool::setEntries& alreadyAdded,
394337
indexed_modified_transaction_set &mapModifiedTx)
395338
{
@@ -539,7 +482,7 @@ void BlockAssembler::addPackageTxs()
539482
ancestors.insert(iter);
540483

541484
// Test if all tx's are Final
542-
if (!TestPackageFinality(ancestors)) {
485+
if (!TestPackageTransactions(ancestors)) {
543486
if (fUsingModified) {
544487
mapModifiedTx.get<ancestor_score>().erase(modit);
545488
failedTx.insert(iter);
@@ -573,6 +516,7 @@ void BlockAssembler::addPriorityTxs()
573516
return;
574517
}
575518

519+
bool fSizeAccounting = fNeedSizeAccounting;
576520
fNeedSizeAccounting = true;
577521

578522
// This vector will be sorted into a priority queue:
@@ -624,7 +568,7 @@ void BlockAssembler::addPriorityTxs()
624568
// If now that this txs is added we've surpassed our desired priority size
625569
// or have dropped below the AllowFreeThreshold, then we're done adding priority txs
626570
if (nBlockSize >= nBlockPrioritySize || !AllowFree(actualPriority)) {
627-
return;
571+
break;
628572
}
629573

630574
// This tx was successfully added, so
@@ -640,6 +584,7 @@ void BlockAssembler::addPriorityTxs()
640584
}
641585
}
642586
}
587+
fNeedSizeAccounting = fSizeAccounting;
643588
}
644589

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

src/miner.h

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ class BlockAssembler
141141

142142
// Configuration parameters for the block size
143143
bool fIncludeWitness;
144-
unsigned int nBlockMaxCost, nBlockMaxSize, nBlockMinSize;
144+
unsigned int nBlockMaxCost, nBlockMaxSize;
145145
bool fNeedSizeAccounting;
146146

147147
// Information on the current status of the block
@@ -157,7 +157,7 @@ class BlockAssembler
157157
int64_t nLockTimeCutoff;
158158
const CChainParams& chainparams;
159159

160-
// Variables used for addScoreTxs and addPriorityTxs
160+
// Variables used for addPriorityTxs
161161
int lastFewTxs;
162162
bool blockFinished;
163163

@@ -174,14 +174,12 @@ class BlockAssembler
174174
void AddToBlock(CTxMemPool::txiter iter);
175175

176176
// Methods for how to add transactions to a block.
177-
/** Add transactions based on modified feerate */
178-
void addScoreTxs();
179177
/** Add transactions based on tx "priority" */
180178
void addPriorityTxs();
181179
/** Add transactions based on feerate including unconfirmed ancestors */
182180
void addPackageTxs();
183181

184-
// helper function for addScoreTxs and addPriorityTxs
182+
// helper function for addPriorityTxs
185183
/** Test if tx will still "fit" in the block */
186184
bool TestForBlock(CTxMemPool::txiter iter);
187185
/** Test if tx still has unconfirmed parents not yet in block */
@@ -192,8 +190,11 @@ class BlockAssembler
192190
void onlyUnconfirmed(CTxMemPool::setEntries& testSet);
193191
/** Test if a new package would "fit" in the block */
194192
bool TestPackage(uint64_t packageSize, int64_t packageSigOpsCost);
195-
/** Test if a set of transactions are all final */
196-
bool TestPackageFinality(const CTxMemPool::setEntries& package);
193+
/** Perform checks on each transaction in a package:
194+
* locktime, premature-witness, serialized size (if necessary)
195+
* These checks should always succeed, and they're here
196+
* only as an extra check in case of suboptimal node configuration */
197+
bool TestPackageTransactions(const CTxMemPool::setEntries& package);
197198
/** Return true if given transaction from mapTx has already been evaluated,
198199
* or if the transaction's cached data in mapTx is incorrect. */
199200
bool SkipMapTxEntry(CTxMemPool::txiter it, indexed_modified_transaction_set &mapModifiedTx, CTxMemPool::setEntries &failedTx);

src/policy/policy.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,8 @@
1414

1515
class CCoinsViewCache;
1616

17-
/** Default for -blockmaxsize and -blockminsize, which control the range of sizes the mining code will create **/
17+
/** Default for -blockmaxsize, which controls the maximum size of block the mining code will create **/
1818
static const unsigned int DEFAULT_BLOCK_MAX_SIZE = 750000;
19-
static const unsigned int DEFAULT_BLOCK_MIN_SIZE = 0;
2019
/** Default for -blockprioritysize, maximum space for zero/low-fee transactions **/
2120
static const unsigned int DEFAULT_BLOCK_PRIORITY_SIZE = 0;
2221
/** Default for -blockmaxcost, which control the range of block costs the mining code will create **/

0 commit comments

Comments
 (0)