 |
Bitcoin Core
22.99.0
P2P Digital Currency
|
Go to the documentation of this file.
32 int64_t nOldTime = pblock->
nTime;
35 if (nOldTime < nNewTime) {
36 pblock->
nTime = nNewTime;
44 return nNewTime - nOldTime;
121 pblock->
vtx.emplace_back();
127 assert(pindexPrev !=
nullptr);
151 int nPackagesSelected = 0;
152 int nDescendantsUpdated = 0;
162 coinbaseTx.
vin.resize(1);
163 coinbaseTx.
vin[0].prevout.SetNull();
164 coinbaseTx.
vout.resize(1);
165 coinbaseTx.
vout[0].scriptPubKey = scriptPubKeyIn;
183 throw std::runtime_error(
strprintf(
"%s: TestBlockValidity failed: %s", __func__, state.
ToString()));
187 LogPrint(
BCLog::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));
194 for (CTxMemPool::setEntries::iterator iit = testSet.begin(); iit != testSet.end(); ) {
197 testSet.erase(iit++);
241 nFees += iter->GetFee();
245 if (fPrintPriority) {
248 iter->GetTx().GetHash().ToString());
257 int nDescendantsUpdated = 0;
263 if (alreadyAdded.count(desc)) {
266 ++nDescendantsUpdated;
267 modtxiter mit = mapModifiedTx.find(desc);
268 if (mit == mapModifiedTx.end()) {
273 mapModifiedTx.insert(modEntry);
279 return nDescendantsUpdated;
296 return mapModifiedTx.count(it) ||
inBlock.count(it) || failedTx.count(it);
305 sortedEntries.clear();
306 sortedEntries.insert(sortedEntries.begin(), package.begin(), package.end());
334 CTxMemPool::indexed_transaction_set::index<ancestor_score>::type::iterator mi =
m_mempool.mapTx.
get<
ancestor_score>().begin();
340 const int64_t MAX_CONSECUTIVE_FAILURES = 1000;
341 int64_t nConsecutiveFailed = 0;
353 bool fUsingModified =
false;
359 fUsingModified =
true;
369 fUsingModified =
true;
381 uint64_t packageSize = iter->GetSizeWithAncestors();
382 CAmount packageFees = iter->GetModFeesWithAncestors();
383 int64_t packageSigOpsCost = iter->GetSigOpCostWithAncestors();
384 if (fUsingModified) {
385 packageSize = modit->nSizeWithAncestors;
386 packageFees = modit->nModFeesWithAncestors;
387 packageSigOpsCost = modit->nSigOpCostWithAncestors;
395 if (!
TestPackage(packageSize, packageSigOpsCost)) {
396 if (fUsingModified) {
401 failedTx.insert(iter);
404 ++nConsecutiveFailed;
406 if (nConsecutiveFailed > MAX_CONSECUTIVE_FAILURES &&
nBlockWeight >
415 uint64_t nNoLimit = std::numeric_limits<uint64_t>::max();
420 ancestors.insert(iter);
424 if (fUsingModified) {
426 failedTx.insert(iter);
432 nConsecutiveFailed = 0;
435 std::vector<CTxMemPool::txiter> sortedEntries;
438 for (
size_t i = 0; i < sortedEntries.size(); ++i) {
441 mapModifiedTx.erase(sortedEntries[i]);
463 assert(txCoinbase.
vin[0].scriptSig.size() <= 100);
const CChainParams & chainparams
std::set< txiter, CompareIteratorByHash > setEntries
void AddToBlock(CTxMemPool::txiter iter)
Add a tx to the block.
void SortForBlock(const CTxMemPool::setEntries &package, std::vector< CTxMemPool::txiter > &sortedEntries)
Sort the package in an order that is valid to appear in a block.
CAmount GetBlockSubsidy(int nHeight, const Consensus::Params &consensusParams)
unsigned int GetNextWorkRequired(const CBlockIndex *pindexLast, const CBlockHeader *pblock, const Consensus::Params ¶ms)
bool GetBoolArg(const std::string &strArg, bool fDefault) const
Return boolean argument or default value.
void addPackageTxs(int &nPackagesSelected, int &nDescendantsUpdated) EXCLUSIVE_LOCKS_REQUIRED(m_mempool.cs)
Add transactions based on feerate including unconfirmed ancestors Increments nPackagesSelected / nDes...
CBlockIndex * LookupBlockIndex(const uint256 &hash) const EXCLUSIVE_LOCKS_REQUIRED(cs_main)
CChainState & m_chainstate
int32_t ComputeBlockVersion(const CBlockIndex *pindexPrev, const Consensus::Params ¶ms)
Determine what nVersion a new block should use.
static const unsigned int DEFAULT_BLOCK_MAX_WEIGHT
Default for -blockmaxweight, which controls the range of block weights the mining code will create.
void onlyUnconfirmed(CTxMemPool::setEntries &testSet)
Remove confirmed (inBlock) entries from given set.
indexed_modified_transaction_set::index< ancestor_score >::type::iterator modtxscoreiter
unsigned int nBlockMaxWeight
indexed_transaction_set::nth_index< 0 >::type::const_iterator txiter
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
bool DeploymentActiveAfter(const CBlockIndex *pindexPrev, const Consensus::Params ¶ms, Consensus::BuriedDeployment dep)
Determine if a deployment is active for the next block.
bool IsArgSet(const std::string &strArg) const
Return true if the given argument has been manually set.
int nHeight
height of the entry in the chain. The genesis block has height 0
static const bool DEFAULT_PRINTPRIORITY
CChainParams defines various tweakable parameters of a given instance of the Bitcoin system.
int64_t m_lock_time_cutoff
#define WITH_LOCK(cs, code)
Run code while locking a mutex.
static CTransactionRef MakeTransactionRef(Tx &&txIn)
const Consensus::Params & GetConsensus() 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 EXCLUSIVE_LOCKS_REQUIRED(cs)
Try to calculate all in-mempool ancestors of entry.
unsigned int GetLegacySigOpCount(const CTransaction &tx)
Auxiliary functions for transaction validation (ideally should not be exposed)
bool TestPackageTransactions(const CTxMemPool::setEntries &package) const
Perform checks on each transaction in a package: locktime, premature-witness, serialized size (if nec...
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.
CBlockIndex * Tip() const
Returns the index entry for the tip of this chain, or nullptr if none.
void resetBlock()
Clear the block's state and prepare for assembling a new block.
Fee rate in satoshis per kilobyte: CAmount / kB.
void IncrementExtraNonce(CBlock *pblock, const CBlockIndex *pindexPrev, unsigned int &nExtraNonce)
Modify the extranonce in a block.
static const unsigned int DEFAULT_BLOCK_MIN_TX_FEE
Default for -blockmintxfee, which sets the minimum feerate for a transaction in blocks created by min...
std::string ToString() const
Parameters that influence chain consensus.
VersionBitsCache g_versionbitscache
Global cache for versionbits deployment status.
RecursiveMutex cs
This mutex needs to be locked when accessing mapTx or other members that are guarded by it.
CTransactionRef get(const uint256 &hash) const
static std::optional< int64_t > m_last_block_num_txs
static std::optional< int64_t > m_last_block_weight
int64_t GetMedianTimePast() const
static const int64_t MAX_BLOCK_SIGOPS_COST
The maximum allowed number of signature check operations in a block (network rule)
CAmount nModFeesWithAncestors
bool fPowAllowMinDifficultyBlocks
CTxMemPool::setEntries inBlock
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...
const CTxMemPool & m_mempool
std::string GetArg(const std::string &strArg, const std::string &strDefault) const
Return string argument or default value.
bool SkipMapTxEntry(CTxMemPool::txiter it, indexed_modified_transaction_set &mapModifiedTx, CTxMemPool::setEntries &failedTx) EXCLUSIVE_LOCKS_REQUIRED(m_mempool.cs)
Return true if given transaction from mapTx has already been evaluated, or if the transaction's cache...
int64_t CAmount
Amount in satoshis (Can be negative)
std::unique_ptr< CBlockTemplate > CreateNewBlock(const CScript &scriptPubKeyIn)
Construct a new block template with coinbase to scriptPubKeyIn.
std::unique_ptr< CBlockTemplate > pblocktemplate
indexed_modified_transaction_set::nth_index< 0 >::type::iterator modtxiter
std::string ToString(const FeeEstimateMode &fee_estimate_mode=FeeEstimateMode::BTC_KVB) const
CChainState stores and provides an API to update our local knowledge of the current best chain.
#define LogPrint(category,...)
Generate a new block, without valid proof-of-work.
BlockAssembler(CChainState &chainstate, const CTxMemPool &mempool, const CChainParams ¶ms)
int GetWitnessCommitmentIndex(const CBlock &block)
Compute at which vout of the block's coinbase transaction the witness commitment occurs,...
Serialized script, used inside transaction inputs and outputs.
static int64_t GetBlockWeight(const CBlock &block)
uint256 GetBlockHash() const
Provides an interface for creating and interacting with one or two chainstates: an IBD chainstate gen...
std::vector< CTxOut > vout
int64_t GetAdjustedTime()
int64_t nSigOpCostWithAncestors
std::vector< CTransactionRef > vtx
bool TestBlockValidity(BlockValidationState &state, const CChainParams &chainparams, CChainState &chainstate, 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::optional< CAmount > ParseMoney(const std::string &money_string)
Parse an amount denoted in full coins.
static const unsigned int MAX_BLOCK_WEIGHT
The maximum allowed weight for a block, see BIP 141 (network rule)
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 >, CompareTxMemPoolEntryByAncestorFee > >> indexed_modified_transaction_set
bool TestPackage(uint64_t packageSize, int64_t packageSigOpsCost) const
Test if a new package would "fit" in the block.
int64_t UpdateTime(CBlockHeader *pblock, const Consensus::Params &consensusParams, const CBlockIndex *pindexPrev)
void RegenerateCommitments(CBlock &block, ChainstateManager &chainman)
Update an old GenerateCoinbaseCommitment from CreateNewBlock after the block txs have changed.
const CChainParams & Params()
Return the currently selected parameters.
bool MineBlocksOnDemand() const
Whether it is possible to mine blocks on demand (no retargeting)
static BlockAssembler::Options DefaultOptions()
int64_t GetTimeMicros()
Returns the system time (not mockable)
CAmount GetFee(uint32_t num_bytes) const
Return the fee in satoshis for the given size in bytes.
A mutable version of CTransaction.
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
The block chain is a tree shaped structure starting with the genesis block at the root,...
int64_t GetIntArg(const std::string &strArg, int64_t nDefault) const
Return integer argument or default value.
int UpdatePackagesForAdded(const CTxMemPool::setEntries &alreadyAdded, indexed_modified_transaction_set &mapModifiedTx) EXCLUSIVE_LOCKS_REQUIRED(m_mempool.cs)
Add descendants of given transactions to mapModifiedTx with ancestor state updated assuming given tra...
uint64_t nBlockSigOpsCost
static const int WITNESS_SCALE_FACTOR
CChain m_chain
The current chain of blockheaders we consult and build on.
node::BlockManager m_blockman
A single BlockManager instance is shared across each constructed chainstate to avoid duplicating bloc...
uint256 BlockMerkleRoot(const CBlock &block, bool *mutated)
uint64_t nSizeWithAncestors
void CalculateDescendants(txiter it, setEntries &setDescendants) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Populate setDescendants with all in-mempool descendants of hash.