Changeset View
Changeset View
Standalone View
Standalone View
src/miner.cpp
Show All 28 Lines | |||||
#include <validationinterface.h> | #include <validationinterface.h> | ||||
#include <algorithm> | #include <algorithm> | ||||
#include <queue> | #include <queue> | ||||
#include <utility> | #include <utility> | ||||
// Unconfirmed transactions in the memory pool often depend on other | // Unconfirmed transactions in the memory pool often depend on other | ||||
// transactions in the memory pool. When we select transactions from the | // transactions in the memory pool. When we select transactions from the | ||||
// pool, we select by highest priority or fee rate, so we might consider | // pool, we select by highest fee rate of a transaction combined with all | ||||
// transactions that depend on transactions that aren't yet in the block. | // its ancestors. | ||||
uint64_t nLastBlockTx = 0; | uint64_t nLastBlockTx = 0; | ||||
uint64_t nLastBlockSize = 0; | uint64_t nLastBlockSize = 0; | ||||
int64_t UpdateTime(CBlockHeader *pblock, const Consensus::Params ¶ms, | int64_t UpdateTime(CBlockHeader *pblock, const Consensus::Params ¶ms, | ||||
const CBlockIndex *pindexPrev) { | const CBlockIndex *pindexPrev) { | ||||
int64_t nOldTime = pblock->nTime; | int64_t nOldTime = pblock->nTime; | ||||
int64_t nNewTime = | int64_t nNewTime = | ||||
▲ Show 20 Lines • Show All 59 Lines • ▼ Show 20 Lines | void BlockAssembler::resetBlock() { | ||||
// Reserve space for coinbase tx. | // Reserve space for coinbase tx. | ||||
nBlockSize = 1000; | nBlockSize = 1000; | ||||
nBlockSigOps = 100; | nBlockSigOps = 100; | ||||
// These counters do not include coinbase tx. | // These counters do not include coinbase tx. | ||||
nBlockTx = 0; | nBlockTx = 0; | ||||
nFees = Amount::zero(); | nFees = Amount::zero(); | ||||
lastFewTxs = 0; | |||||
} | } | ||||
std::unique_ptr<CBlockTemplate> | std::unique_ptr<CBlockTemplate> | ||||
BlockAssembler::CreateNewBlock(const CScript &scriptPubKeyIn) { | BlockAssembler::CreateNewBlock(const CScript &scriptPubKeyIn) { | ||||
int64_t nTimeStart = GetTimeMicros(); | int64_t nTimeStart = GetTimeMicros(); | ||||
resetBlock(); | resetBlock(); | ||||
▲ Show 20 Lines • Show All 108 Lines • ▼ Show 20 Lines | LogPrint(BCLog::BENCH, | ||||
"descendants), validity: %.2fms (total %.2fms)\n", | "descendants), validity: %.2fms (total %.2fms)\n", | ||||
0.001 * (nTime1 - nTimeStart), nPackagesSelected, | 0.001 * (nTime1 - nTimeStart), nPackagesSelected, | ||||
nDescendantsUpdated, 0.001 * (nTime2 - nTime1), | nDescendantsUpdated, 0.001 * (nTime2 - nTime1), | ||||
0.001 * (nTime2 - nTimeStart)); | 0.001 * (nTime2 - nTimeStart)); | ||||
return std::move(pblocktemplate); | return std::move(pblocktemplate); | ||||
} | } | ||||
bool BlockAssembler::isStillDependent(CTxMemPool::txiter iter) { | |||||
for (CTxMemPool::txiter parent : mempool->GetMemPoolParents(iter)) { | |||||
if (!inBlock.count(parent)) { | |||||
return true; | |||||
} | |||||
} | |||||
return false; | |||||
} | |||||
void BlockAssembler::onlyUnconfirmed(CTxMemPool::setEntries &testSet) { | void BlockAssembler::onlyUnconfirmed(CTxMemPool::setEntries &testSet) { | ||||
for (CTxMemPool::setEntries::iterator iit = testSet.begin(); | for (CTxMemPool::setEntries::iterator iit = testSet.begin(); | ||||
iit != testSet.end();) { | iit != testSet.end();) { | ||||
// Only test txs not already in the block. | // Only test txs not already in the block. | ||||
if (inBlock.count(*iit)) { | if (inBlock.count(*iit)) { | ||||
testSet.erase(iit++); | testSet.erase(iit++); | ||||
} else { | } else { | ||||
iit++; | iit++; | ||||
Show All 38 Lines | for (CTxMemPool::txiter it : package) { | ||||
} | } | ||||
nPotentialBlockSize += nTxSize; | nPotentialBlockSize += nTxSize; | ||||
} | } | ||||
return true; | return true; | ||||
} | } | ||||
BlockAssembler::TestForBlockResult | |||||
BlockAssembler::TestForBlock(CTxMemPool::txiter it) { | |||||
auto blockSizeWithTx = | |||||
nBlockSize + ::GetSerializeSize(it->GetTx(), PROTOCOL_VERSION); | |||||
if (blockSizeWithTx >= nMaxGeneratedBlockSize) { | |||||
if (nBlockSize > nMaxGeneratedBlockSize - 100 || lastFewTxs > 50) { | |||||
return TestForBlockResult::BlockFinished; | |||||
} | |||||
if (nBlockSize > nMaxGeneratedBlockSize - 1000) { | |||||
lastFewTxs++; | |||||
} | |||||
return TestForBlockResult::TXCantFit; | |||||
} | |||||
auto maxBlockSigOps = GetMaxBlockSigOpsCount(blockSizeWithTx); | |||||
if (nBlockSigOps + it->GetSigOpCount() >= maxBlockSigOps) { | |||||
// If the block has room for no more sig ops then flag that the block is | |||||
// finished. | |||||
// TODO: We should consider adding another transaction that isn't very | |||||
// dense in sigops instead of bailing out so easily. | |||||
if (nBlockSigOps > maxBlockSigOps - 2) { | |||||
return TestForBlockResult::BlockFinished; | |||||
} | |||||
// Otherwise attempt to find another tx with fewer sigops to put in the | |||||
// block. | |||||
return TestForBlockResult::TXCantFit; | |||||
} | |||||
// Must check that lock times are still valid. This can be removed once MTP | |||||
// is always enforced as long as reorgs keep the mempool consistent. | |||||
CValidationState state; | |||||
if (!ContextualCheckTransaction(chainparams.GetConsensus(), it->GetTx(), | |||||
state, nHeight, nLockTimeCutoff, | |||||
nMedianTimePast)) { | |||||
return TestForBlockResult::TXCantFit; | |||||
} | |||||
return TestForBlockResult::TXFits; | |||||
} | |||||
void BlockAssembler::AddToBlock(CTxMemPool::txiter iter) { | void BlockAssembler::AddToBlock(CTxMemPool::txiter iter) { | ||||
pblocktemplate->entries.emplace_back(iter->GetSharedTx(), iter->GetFee(), | pblocktemplate->entries.emplace_back(iter->GetSharedTx(), iter->GetFee(), | ||||
iter->GetTxSize(), | iter->GetTxSize(), | ||||
iter->GetSigOpCount()); | iter->GetSigOpCount()); | ||||
nBlockSize += iter->GetTxSize(); | nBlockSize += iter->GetTxSize(); | ||||
++nBlockTx; | ++nBlockTx; | ||||
nBlockSigOps += iter->GetSigOpCount(); | nBlockSigOps += iter->GetSigOpCount(); | ||||
nFees += iter->GetFee(); | nFees += iter->GetFee(); | ||||
▲ Show 20 Lines • Show All 261 Lines • Show Last 20 Lines |