30 #include <boost/thread.hpp>
31 #include <boost/tuple/tuple.hpp>
63 int64_t nOldTime = pblock->
nTime;
66 if (nOldTime < nNewTime)
67 pblock->
nTime = nNewTime;
73 return nNewTime - nOldTime;
77 : chainparams(_chainparams)
85 bool fWeightSet =
false;
156 const int32_t nVersion = VERSIONBITS_LAST_OLD_BLOCK_VERSION;
179 int nPackagesSelected = 0;
180 int nDescendantsUpdated = 0;
191 coinbaseTx.
vin.resize(1);
192 coinbaseTx.
vin[0].prevout.SetNull();
193 coinbaseTx.
vout.resize(1);
194 coinbaseTx.
vout[0].scriptPubKey = scriptPubKeyIn;
198 pblock->
vtx[0] = MakeTransactionRef(std::move(coinbaseTx));
218 LogPrint(
"bench",
"CreateNewBlock() packages: %.2fms (%d packages, %d updated descendants), validity: %.2fms (total %.2fms)\n", 0.001 * (nTime1 - nTimeStart), nPackagesSelected, nDescendantsUpdated, 0.001 * (nTime2 - nTime1), 0.001 * (nTime2 - nTimeStart));
236 for (CTxMemPool::setEntries::iterator iit = testSet.begin(); iit != testSet.end(); ) {
239 testSet.erase(iit++);
275 nPotentialBlockSize += nTxSize;
335 pblock->
vtx.emplace_back(iter->GetSharedTx());
344 nFees += iter->GetFee();
347 bool fPrintPriority =
GetBoolArg(
"-printpriority", DEFAULT_PRINTPRIORITY);
348 if (fPrintPriority) {
349 double dPriority = iter->GetPriority(
nHeight);
352 LogPrintf(
"priority %.1f fee %s txid %s\n",
355 iter->GetTx().GetHash().ToString());
362 int nDescendantsUpdated = 0;
368 if (alreadyAdded.count(desc))
370 ++nDescendantsUpdated;
371 modtxiter mit = mapModifiedTx.find(desc);
372 if (mit == mapModifiedTx.end()) {
377 mapModifiedTx.insert(modEntry);
383 return nDescendantsUpdated;
398 if (mapModifiedTx.count(it) ||
inBlock.count(it) || failedTx.count(it))
409 sortedEntries.clear();
410 sortedEntries.insert(sortedEntries.begin(), package.begin(), package.end());
442 const int64_t MAX_CONSECUTIVE_FAILURES = 1000;
443 int64_t nConsecutiveFailed = 0;
456 bool fUsingModified =
false;
462 fUsingModified =
true;
472 fUsingModified =
true;
484 uint64_t packageSize = iter->GetSizeWithAncestors();
485 CAmount packageFees = iter->GetModFeesWithAncestors();
486 int64_t packageSigOpsCost = iter->GetSigOpCostWithAncestors();
487 if (fUsingModified) {
488 packageSize = modit->nSizeWithAncestors;
489 packageFees = modit->nModFeesWithAncestors;
490 packageSigOpsCost = modit->nSigOpCostWithAncestors;
498 if (!
TestPackage(packageSize, packageSigOpsCost)) {
499 if (fUsingModified) {
504 failedTx.insert(iter);
507 ++nConsecutiveFailed;
509 if (nConsecutiveFailed > MAX_CONSECUTIVE_FAILURES &&
nBlockWeight >
518 uint64_t nNoLimit = std::numeric_limits<uint64_t>::max();
523 ancestors.insert(iter);
527 if (fUsingModified) {
529 failedTx.insert(iter);
535 nConsecutiveFailed = 0;
538 std::vector<CTxMemPool::txiter> sortedEntries;
541 for (
size_t i=0; i<sortedEntries.size(); ++i) {
544 mapModifiedTx.erase(sortedEntries[i]);
558 unsigned int nBlockPrioritySize =
GetArg(
"-blockprioritysize", DEFAULT_BLOCK_PRIORITY_SIZE);
559 nBlockPrioritySize = std::min(
nBlockMaxSize, nBlockPrioritySize);
561 if (nBlockPrioritySize == 0) {
569 std::vector<TxCoinAgePriority> vecPriority;
571 std::map<CTxMemPool::txiter, double, CTxMemPool::CompareIteratorByHash> waitPriMap;
572 typedef std::map<CTxMemPool::txiter, double, CTxMemPool::CompareIteratorByHash>::iterator waitPriIter;
573 double actualPriority = -1;
576 for (CTxMemPool::indexed_transaction_set::iterator mi =
mempool.
mapTx.begin();
579 double dPriority = mi->GetPriority(
nHeight);
584 std::make_heap(vecPriority.begin(), vecPriority.end(), pricomparer);
588 iter = vecPriority.front().second;
589 actualPriority = vecPriority.front().first;
590 std::pop_heap(vecPriority.begin(), vecPriority.end(), pricomparer);
591 vecPriority.pop_back();
606 waitPriMap.insert(std::make_pair(iter, actualPriority));
624 waitPriIter wpiter = waitPriMap.find(child);
625 if (wpiter != waitPriMap.end()) {
627 std::push_heap(vecPriority.begin(), vecPriority.end(), pricomparer);
628 waitPriMap.erase(wpiter);
646 unsigned int nHeight = pindexPrev->
nHeight+1;
649 assert(txCoinbase.
vin[0].scriptSig.size() <= 100);
651 pblock->
vtx[0] = MakeTransactionRef(std::move(txCoinbase));
int64_t CAmount
Amount in satoshis (Can be negative)
int64_t GetBlockWeight(const CBlock &block)
Compute the consensus-critical block weight (see BIP 141).
bool TestForBlock(CTxMemPool::txiter iter)
Test if tx will still "fit" in the block.
void AddToBlock(CTxMemPool::txiter iter)
Add a tx to the block.
CTxMemPool::setEntries inBlock
bool isStillDependent(CTxMemPool::txiter iter)
Test if tx still has unconfirmed parents not yet in block.
bool TestPackage(uint64_t packageSize, int64_t packageSigOpsCost)
Test if a new package would "fit" in the block.
unsigned int nBlockMaxSize
void SortForBlock(const CTxMemPool::setEntries &package, CTxMemPool::txiter entry, std::vector< CTxMemPool::txiter > &sortedEntries)
Sort the package in an order that is valid to appear in a block.
void onlyUnconfirmed(CTxMemPool::setEntries &testSet)
Remove confirmed (inBlock) entries from given set.
bool TestPackageTransactions(const CTxMemPool::setEntries &package)
Perform checks on each transaction in a package: locktime, premature-witness, serialized size (if nec...
std::unique_ptr< CBlockTemplate > CreateNewBlock(const CScript &scriptPubKeyIn, bool fMineWitnessTx)
Construct a new block template with coinbase to scriptPubKeyIn.
BlockAssembler(const CChainParams &chainparams)
void resetBlock()
Clear the block's state and prepare for assembling a new block.
std::unique_ptr< CBlockTemplate > pblocktemplate
const CChainParams & chainparams
unsigned int nBlockMaxWeight
bool SkipMapTxEntry(CTxMemPool::txiter it, indexed_modified_transaction_set &mapModifiedTx, CTxMemPool::setEntries &failedTx)
Return true if given transaction from mapTx has already been evaluated, or if the transaction's cache...
void addPriorityTxs()
Add transactions based on tx "priority".
void addPackageTxs(int &nPackagesSelected, int &nDescendantsUpdated)
Add transactions based on feerate including unconfirmed ancestors Increments nPackagesSelected / nDes...
int UpdatePackagesForAdded(const CTxMemPool::setEntries &alreadyAdded, indexed_modified_transaction_set &mapModifiedTx)
Add descendants of given transactions to mapModifiedTx with ancestor state updated assuming given tra...
uint64_t nBlockSigOpsCost
std::vector< CTransactionRef > vtx
The block chain is a tree shaped structure starting with the genesis block at the root,...
uint256 GetBlockHash() const
int64_t GetMedianTimePast() const
int nHeight
height of the entry in the chain. The genesis block has height 0
CBlockIndex * Tip() const
Returns the index entry for the tip of this chain, or NULL if none.
CChainParams defines various tweakable parameters of a given instance of the Bitcoin system.
bool MineBlocksOnDemand() const
Make miner stop after a block is found.
const Consensus::Params & GetConsensus(uint32_t nTargetHeight) const
Fee rate in satoshis per kilobyte: CAmount / kB.
CAmount GetFee(size_t nBytes) const
Return the fee in satoshis for the given size in bytes.
std::string ToString() const
Serialized script, used inside transaction inputs and outputs.
const setEntries & GetMemPoolChildren(txiter entry) const
bool CalculateMemPoolAncestors(const CTxMemPoolEntry &entry, setEntries &setAncestors, uint64_t limitAncestorCount, uint64_t limitAncestorSize, uint64_t limitDescendantCount, uint64_t limitDescendantSize, std::string &errString, bool fSearchForParents=true) const
Try to calculate all in-mempool ancestors of entry.
void ApplyDeltas(const uint256 hash, double &dPriorityDelta, CAmount &nFeeDelta) const
std::set< txiter, CompareIteratorByHash > setEntries
indexed_transaction_set::nth_index< 0 >::type::iterator txiter
const setEntries & GetMemPoolParents(txiter entry) const
indexed_transaction_set mapTx
void CalculateDescendants(txiter it, setEntries &setDescendants)
Populate setDescendants with all in-mempool descendants of hash.
Capture information about block/transaction validation.
Sort by score of entry ((fee+delta)/size) in descending order.
bool operator()(const CTxMemPool::txiter a, const CTxMemPool::txiter b)
@ LOCKTIME_MEDIAN_TIME_PAST
CAmount GetDogecoinBlockSubsidy(int nHeight, const Consensus::Params &consensusParams, uint256 prevHash)
uint256 BlockMerkleRoot(const CBlock &block, bool *mutated)
void IncrementExtraNonce(CBlock *pblock, const CBlockIndex *pindexPrev, unsigned int &nExtraNonce)
Modify the extranonce in a block.
int64_t UpdateTime(CBlockHeader *pblock, const Consensus::Params &consensusParams, const CBlockIndex *pindexPrev)
uint64_t nLastBlockWeight
indexed_modified_transaction_set::nth_index< 0 >::type::iterator modtxiter
indexed_modified_transaction_set::index< ancestor_score >::type::iterator modtxscoreiter
boost::multi_index_container< CTxMemPoolModifiedEntry, boost::multi_index::indexed_by< boost::multi_index::ordered_unique< modifiedentry_iter, CompareCTxMemPoolIter >, boost::multi_index::ordered_non_unique< boost::multi_index::tag< ancestor_score >, boost::multi_index::identity< CTxMemPoolModifiedEntry >, CompareModifiedEntry > >> indexed_modified_transaction_set
unsigned int GetNextWorkRequired(const CBlockIndex *pindexLast, const CBlockHeader *pblock, const Consensus::Params ¶ms)
size_t GetSerializeSize(const T &t, int nType, int nVersion=0)
A mutable version of CTransaction.
std::vector< CTxOut > vout
uint64_t nSizeWithAncestors
CAmount nModFeesWithAncestors
int64_t nSigOpCostWithAncestors
Parameters that influence chain consensus.
int32_t nAuxpowChainId
Auxpow parameters.
bool fPowAllowMinDifficultyBlocks
int64_t GetAdjustedTime()
bool AllowFree(double dPriority)
std::pair< double, CTxMemPool::txiter > TxCoinAgePriority
std::string GetArg(const std::string &strArg, const std::string &strDefault)
Return string argument or default value.
bool GetBoolArg(const std::string &strArg, bool fDefault)
Return boolean argument or default value.
bool IsArgSet(const std::string &strArg)
Return true if the given argument has been manually set.
#define LogPrint(category,...)
bool ParseMoney(const string &str, CAmount &nRet)
CCriticalSection cs_main
Global state.
CTxMemPool mempool(::minRelayTxFee)
CScript COINBASE_FLAGS
Constant stuff for coinbase transactions we create:
bool TestBlockValidity(CValidationState &state, const CChainParams &chainparams, const CBlock &block, CBlockIndex *pindexPrev, bool fCheckPOW, bool fCheckMerkleRoot)
Check a block is completely valid from start to finish (only works on top of our current best block,...
std::vector< unsigned char > GenerateCoinbaseCommitment(CBlock &block, const CBlockIndex *pindexPrev, const Consensus::Params &consensusParams)
Produce the necessary coinbase commitment for a block (modifies the hash, don't call for mined blocks...
std::string FormatStateMessage(const CValidationState &state)
Convert CValidationState to a human-readable message for logging.
bool IsWitnessEnabled(const CBlockIndex *pindexPrev, const Consensus::Params ¶ms)
Check whether witness commitments are required for block.
unsigned int GetLegacySigOpCount(const CTransaction &tx)
Count ECDSA signature operations the old-fashioned (pre-0.6) way.
CChain chainActive
The currently-connected chain of blocks (protected by cs_main).
bool IsFinalTx(const CTransaction &tx, int nBlockHeight, int64_t nBlockTime)
Check if transaction is final and can be included in a block with the specified height and time.