diff --git a/src/miner.h b/src/miner.h --- a/src/miner.h +++ b/src/miner.h @@ -151,7 +151,6 @@ // Variables used for addPriorityTxs int lastFewTxs; - bool blockFinished; public: BlockAssembler(const Config &_config); @@ -178,7 +177,7 @@ // helper function for addPriorityTxs /** Test if tx will still "fit" in the block */ - bool TestForBlock(CTxMemPool::txiter iter); + struct TestForBlockResult TestForBlock(CTxMemPool::txiter iter); /** Test if tx still has unconfirmed parents not yet in block */ bool isStillDependent(CTxMemPool::txiter iter); diff --git a/src/miner.cpp b/src/miner.cpp --- a/src/miner.cpp +++ b/src/miner.cpp @@ -117,7 +117,6 @@ nFees = Amount(0); lastFewTxs = 0; - blockFinished = false; } static const std::vector @@ -297,21 +296,25 @@ return true; } -bool BlockAssembler::TestForBlock(CTxMemPool::txiter it) { +struct TestForBlockResult { + bool transactionFits; + bool blockFinished; +}; + +TestForBlockResult BlockAssembler::TestForBlock(CTxMemPool::txiter it) { auto blockSizeWithTx = nBlockSize + ::GetSerializeSize(it->GetTx(), SER_NETWORK, PROTOCOL_VERSION); if (blockSizeWithTx >= nMaxGeneratedBlockSize) { if (nBlockSize > nMaxGeneratedBlockSize - 100 || lastFewTxs > 50) { - blockFinished = true; - return false; + return {false, true}; } if (nBlockSize > nMaxGeneratedBlockSize - 1000) { lastFewTxs++; } - return false; + return {false, false}; } auto maxBlockSigOps = GetMaxBlockSigOpsCount(blockSizeWithTx); @@ -321,13 +324,12 @@ // TODO: We should consider adding another transaction that isn't very // dense in sigops instead of bailing out so easily. if (nBlockSigOps > maxBlockSigOps - 2) { - blockFinished = true; - return false; + return {false, true}; } // Otherwise attempt to find another tx with fewer sigops to put in the // block. - return false; + return {false, false}; } // Must check that lock times are still valid. This can be removed once MTP @@ -335,10 +337,10 @@ CValidationState state; if (!ContextualCheckTransaction(*config, it->GetTx(), state, nHeight, nLockTimeCutoff)) { - return false; + return {false, false}; } - return true; + return {true, false}; } void BlockAssembler::AddToBlock(CTxMemPool::txiter iter) { @@ -608,7 +610,7 @@ // Add a tx from priority queue to fill the part of block reserved to // priority transactions. - while (!vecPriority.empty() && !blockFinished) { + while (!vecPriority.empty()) { iter = vecPriority.front().second; actualPriority = vecPriority.front().first; std::pop_heap(vecPriority.begin(), vecPriority.end(), pricomparer); @@ -628,11 +630,18 @@ continue; } - // If this tx fits in the block add it, otherwise keep looping. - if (!TestForBlock(iter)) { + TestForBlockResult testResult = TestForBlock(iter); + + // If this tx does not fit in the block, skip to next transaction. + if (!testResult.transactionFits) { continue; } + // Or break if the block is completed + if (!testResult.blockFinished) { + break; + } + AddToBlock(iter); // If now that this txs is added we've surpassed our desired priority