Dash Core Source Documentation (0.16.0.1)

Find detailed information regarding the Dash Core source code.

miner.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2015 The Bitcoin Core developers
3 // Copyright (c) 2014-2019 The Dash Core developers
4 // Distributed under the MIT software license, see the accompanying
5 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
6 
7 #include <miner.h>
8 
9 #include <amount.h>
10 #include <chain.h>
11 #include <chainparams.h>
12 #include <coins.h>
13 #include <consensus/consensus.h>
14 #include <consensus/tx_verify.h>
15 #include <consensus/merkle.h>
16 #include <consensus/validation.h>
17 #include <hash.h>
18 #include <validation.h>
19 #include <net.h>
20 #include <policy/feerate.h>
21 #include <policy/policy.h>
22 #include <pow.h>
23 #include <primitives/transaction.h>
24 #include <script/standard.h>
25 #include <timedata.h>
26 #include <util.h>
27 #include <utilmoneystr.h>
30 #include <validationinterface.h>
31 
32 #include <evo/specialtx.h>
33 #include <evo/cbtx.h>
34 #include <evo/simplifiedmns.h>
35 #include <evo/deterministicmns.h>
36 
39 
40 #include <algorithm>
41 #include <memory>
42 #include <queue>
43 #include <utility>
44 
46 //
47 // DashMiner
48 //
49 
50 //
51 // Unconfirmed transactions in the memory pool often depend on other
52 // transactions in the memory pool. When we select transactions from the
53 // pool, we select by highest fee rate of a transaction combined with all
54 // its ancestors.
55 
56 uint64_t nLastBlockTx = 0;
57 uint64_t nLastBlockSize = 0;
58 
59 int64_t UpdateTime(CBlockHeader* pblock, const Consensus::Params& consensusParams, const CBlockIndex* pindexPrev)
60 {
61  int64_t nOldTime = pblock->nTime;
62  int64_t nNewTime = std::max(pindexPrev->GetMedianTimePast()+1, GetAdjustedTime());
63 
64  if (nOldTime < nNewTime)
65  pblock->nTime = nNewTime;
66 
67  // Updating time can change work required on testnet:
68  if (consensusParams.fPowAllowMinDifficultyBlocks)
69  pblock->nBits = GetNextWorkRequired(pindexPrev, pblock, consensusParams);
70 
71  return nNewTime - nOldTime;
72 }
73 
77 }
78 
79 BlockAssembler::BlockAssembler(const CChainParams& params, const Options& options) : chainparams(params)
80 {
82  // Limit size to between 1K and MaxBlockSize()-1K for sanity:
83  nBlockMaxSize = std::max((unsigned int)1000, std::min((unsigned int)(MaxBlockSize(fDIP0001ActiveAtTip) - 1000), (unsigned int)options.nBlockMaxSize));
84 }
85 
87 {
88  // Block resource limits
91  if (gArgs.IsArgSet("-blockmaxsize")) {
92  options.nBlockMaxSize = gArgs.GetArg("-blockmaxsize", DEFAULT_BLOCK_MAX_SIZE);
93  }
94  if (gArgs.IsArgSet("-blockmintxfee")) {
95  CAmount n = 0;
96  ParseMoney(gArgs.GetArg("-blockmintxfee", ""), n);
97  options.blockMinFeeRate = CFeeRate(n);
98  } else {
100  }
101  return options;
102 }
103 
105 
107 {
108  inBlock.clear();
109 
110  // Reserve space for coinbase tx
111  nBlockSize = 1000;
112  nBlockSigOps = 100;
113 
114  // These counters do not include coinbase tx
115  nBlockTx = 0;
116  nFees = 0;
117 }
118 
119 std::unique_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock(const CScript& scriptPubKeyIn)
120 {
121  int64_t nTimeStart = GetTimeMicros();
122 
123  resetBlock();
124 
125  pblocktemplate.reset(new CBlockTemplate());
126 
127  if(!pblocktemplate.get())
128  return nullptr;
129  pblock = &pblocktemplate->block; // pointer for convenience
130 
131  // Add dummy coinbase tx as first transaction
132  pblock->vtx.emplace_back();
133  pblocktemplate->vTxFees.push_back(-1); // updated at end
134  pblocktemplate->vTxSigOps.push_back(-1); // updated at end
135 
137 
138  CBlockIndex* pindexPrev = chainActive.Tip();
139  assert(pindexPrev != nullptr);
140  nHeight = pindexPrev->nHeight + 1;
141 
142  bool fDIP0003Active_context = nHeight >= chainparams.GetConsensus().DIP0003Height;
144 
146  // -regtest only: allow overriding block.nVersion with
147  // -blockversion=N to test forking scenarios
149  pblock->nVersion = gArgs.GetArg("-blockversion", pblock->nVersion);
150 
152  const int64_t nMedianTimePast = pindexPrev->GetMedianTimePast();
153 
155  ? nMedianTimePast
156  : pblock->GetBlockTime();
157 
158  if (fDIP0003Active_context) {
159  for (auto& p : chainparams.GetConsensus().llmqs) {
160  CTransactionRef qcTx;
161  if (llmq::quorumBlockProcessor->GetMinableCommitmentTx(p.first, nHeight, qcTx)) {
162  pblock->vtx.emplace_back(qcTx);
163  pblocktemplate->vTxFees.emplace_back(0);
164  pblocktemplate->vTxSigOps.emplace_back(0);
165  nBlockSize += qcTx->GetTotalSize();
166  ++nBlockTx;
167  }
168  }
169  }
170 
171  int nPackagesSelected = 0;
172  int nDescendantsUpdated = 0;
173  addPackageTxs(nPackagesSelected, nDescendantsUpdated);
174 
175  int64_t nTime1 = GetTimeMicros();
176 
179  LogPrintf("CreateNewBlock(): total size %u txs: %u fees: %ld sigops %d\n", nBlockSize, nBlockTx, nFees, nBlockSigOps);
180 
181  // Create coinbase transaction.
182  CMutableTransaction coinbaseTx;
183  coinbaseTx.vin.resize(1);
184  coinbaseTx.vin[0].prevout.SetNull();
185  coinbaseTx.vout.resize(1);
186  coinbaseTx.vout[0].scriptPubKey = scriptPubKeyIn;
187 
188  // NOTE: unlike in bitcoin, we need to pass PREVIOUS block height here
189  CAmount blockReward = nFees + GetBlockSubsidy(pindexPrev->nBits, pindexPrev->nHeight, Params().GetConsensus());
190 
191  // Compute regular coinbase transaction.
192  coinbaseTx.vout[0].nValue = blockReward;
193 
194  if (!fDIP0003Active_context) {
195  coinbaseTx.vin[0].scriptSig = CScript() << nHeight << OP_0;
196  } else {
197  coinbaseTx.vin[0].scriptSig = CScript() << OP_RETURN;
198 
199  coinbaseTx.nVersion = 3;
200  coinbaseTx.nType = TRANSACTION_COINBASE;
201 
202  CCbTx cbTx;
203 
204  if (fDIP0008Active_context) {
205  cbTx.nVersion = 2;
206  } else {
207  cbTx.nVersion = 1;
208  }
209 
210  cbTx.nHeight = nHeight;
211 
212  CValidationState state;
213  if (!CalcCbTxMerkleRootMNList(*pblock, pindexPrev, cbTx.merkleRootMNList, state)) {
214  throw std::runtime_error(strprintf("%s: CalcCbTxMerkleRootMNList failed: %s", __func__, FormatStateMessage(state)));
215  }
216  if (fDIP0008Active_context) {
217  if (!CalcCbTxMerkleRootQuorums(*pblock, pindexPrev, cbTx.merkleRootQuorums, state)) {
218  throw std::runtime_error(strprintf("%s: CalcCbTxMerkleRootQuorums failed: %s", __func__, FormatStateMessage(state)));
219  }
220  }
221 
222  SetTxPayload(coinbaseTx, cbTx);
223  }
224 
225  // Update coinbase transaction with additional info about masternode and governance payments,
226  // get some info back to pass to getblocktemplate
227  FillBlockPayments(coinbaseTx, nHeight, blockReward, pblocktemplate->voutMasternodePayments, pblocktemplate->voutSuperblockPayments);
228 
229  pblock->vtx[0] = MakeTransactionRef(std::move(coinbaseTx));
230  pblocktemplate->vTxFees[0] = -nFees;
231 
232  // Fill in header
233  pblock->hashPrevBlock = pindexPrev->GetBlockHash();
234  UpdateTime(pblock, chainparams.GetConsensus(), pindexPrev);
236  pblock->nNonce = 0;
237  pblocktemplate->nPrevBits = pindexPrev->nBits;
238  pblocktemplate->vTxSigOps[0] = GetLegacySigOpCount(*pblock->vtx[0]);
239 
240  CValidationState state;
241  if (!TestBlockValidity(state, chainparams, *pblock, pindexPrev, false, false)) {
242  throw std::runtime_error(strprintf("%s: TestBlockValidity failed: %s", __func__, FormatStateMessage(state)));
243  }
244  int64_t nTime2 = GetTimeMicros();
245 
246  LogPrint(BCLog::BENCHMARK, "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));
247 
248  return std::move(pblocktemplate);
249 }
250 
252 {
253  for (CTxMemPool::setEntries::iterator iit = testSet.begin(); iit != testSet.end(); ) {
254  // Only test txs not already in the block
255  if (inBlock.count(*iit)) {
256  testSet.erase(iit++);
257  }
258  else {
259  iit++;
260  }
261  }
262 }
263 
264 bool BlockAssembler::TestPackage(uint64_t packageSize, unsigned int packageSigOps) const
265 {
266  if (nBlockSize + packageSize >= nBlockMaxSize)
267  return false;
268  if (nBlockSigOps + packageSigOps >= MaxBlockSigOps(fDIP0001ActiveAtTip))
269  return false;
270  return true;
271 }
272 
273 // Perform transaction-level checks before adding to block:
274 // - transaction finality (locktime)
275 // - safe TXs in regard to ChainLocks
277 {
278  for (const CTxMemPool::txiter it : package) {
279  if (!IsFinalTx(it->GetTx(), nHeight, nLockTimeCutoff))
280  return false;
281  if (!llmq::chainLocksHandler->IsTxSafeForMining(it->GetTx().GetHash())) {
282  return false;
283  }
284  }
285  return true;
286 }
287 
289 {
290  pblock->vtx.emplace_back(iter->GetSharedTx());
291  pblocktemplate->vTxFees.push_back(iter->GetFee());
292  pblocktemplate->vTxSigOps.push_back(iter->GetSigOpCount());
293  nBlockSize += iter->GetTxSize();
294  ++nBlockTx;
295  nBlockSigOps += iter->GetSigOpCount();
296  nFees += iter->GetFee();
297  inBlock.insert(iter);
298 
299  bool fPrintPriority = gArgs.GetBoolArg("-printpriority", DEFAULT_PRINTPRIORITY);
300  if (fPrintPriority) {
301  LogPrintf("fee %s txid %s\n",
302  CFeeRate(iter->GetModifiedFee(), iter->GetTxSize()).ToString(),
303  iter->GetTx().GetHash().ToString());
304  }
305 }
306 
308  indexed_modified_transaction_set &mapModifiedTx)
309 {
310  int nDescendantsUpdated = 0;
311  for (const CTxMemPool::txiter it : alreadyAdded) {
312  CTxMemPool::setEntries descendants;
313  mempool.CalculateDescendants(it, descendants);
314  // Insert all descendants (not yet in block) into the modified set
315  for (CTxMemPool::txiter desc : descendants) {
316  if (alreadyAdded.count(desc))
317  continue;
318  ++nDescendantsUpdated;
319  modtxiter mit = mapModifiedTx.find(desc);
320  if (mit == mapModifiedTx.end()) {
321  CTxMemPoolModifiedEntry modEntry(desc);
322  modEntry.nSizeWithAncestors -= it->GetTxSize();
323  modEntry.nModFeesWithAncestors -= it->GetModifiedFee();
324  modEntry.nSigOpCountWithAncestors -= it->GetSigOpCount();
325  mapModifiedTx.insert(modEntry);
326  } else {
327  mapModifiedTx.modify(mit, update_for_parent_inclusion(it));
328  }
329  }
330  }
331  return nDescendantsUpdated;
332 }
333 
334 // Skip entries in mapTx that are already in a block or are present
335 // in mapModifiedTx (which implies that the mapTx ancestor state is
336 // stale due to ancestor inclusion in the block)
337 // Also skip transactions that we've already failed to add. This can happen if
338 // we consider a transaction in mapModifiedTx and it fails: we can then
339 // potentially consider it again while walking mapTx. It's currently
340 // guaranteed to fail again, but as a belt-and-suspenders check we put it in
341 // failedTx and avoid re-evaluation, since the re-evaluation would be using
342 // cached size/sigops/fee values that are not actually correct.
344 {
345  assert (it != mempool.mapTx.end());
346  return mapModifiedTx.count(it) || inBlock.count(it) || failedTx.count(it);
347 }
348 
349 void BlockAssembler::SortForBlock(const CTxMemPool::setEntries& package, CTxMemPool::txiter entry, std::vector<CTxMemPool::txiter>& sortedEntries)
350 {
351  // Sort package by ancestor count
352  // If a transaction A depends on transaction B, then A's ancestor count
353  // must be greater than B's. So this is sufficient to validly order the
354  // transactions for block inclusion.
355  sortedEntries.clear();
356  sortedEntries.insert(sortedEntries.begin(), package.begin(), package.end());
357  std::sort(sortedEntries.begin(), sortedEntries.end(), CompareTxIterByAncestorCount());
358 }
359 
360 // This transaction selection algorithm orders the mempool based
361 // on feerate of a transaction including all unconfirmed ancestors.
362 // Since we don't remove transactions from the mempool as we select them
363 // for block inclusion, we need an alternate method of updating the feerate
364 // of a transaction with its not-yet-selected ancestors as we go.
365 // This is accomplished by walking the in-mempool descendants of selected
366 // transactions and storing a temporary modified state in mapModifiedTxs.
367 // Each time through the loop, we compare the best transaction in
368 // mapModifiedTxs with the next transaction in the mempool to decide what
369 // transaction package to work on next.
370 void BlockAssembler::addPackageTxs(int &nPackagesSelected, int &nDescendantsUpdated)
371 {
372  // mapModifiedTx will store sorted packages after they are modified
373  // because some of their txs are already in the block
374  indexed_modified_transaction_set mapModifiedTx;
375  // Keep track of entries that failed inclusion, to avoid duplicate work
376  CTxMemPool::setEntries failedTx;
377 
378  // Start by adding all descendants of previously added txs to mapModifiedTx
379  // and modifying them for their already included ancestors
380  UpdatePackagesForAdded(inBlock, mapModifiedTx);
381 
382  CTxMemPool::indexed_transaction_set::index<ancestor_score>::type::iterator mi = mempool.mapTx.get<ancestor_score>().begin();
383  CTxMemPool::txiter iter;
384 
385  // Limit the number of attempts to add transactions to the block when it is
386  // close to full; this is just a simple heuristic to finish quickly if the
387  // mempool has a lot of entries.
388  const int64_t MAX_CONSECUTIVE_FAILURES = 1000;
389  int64_t nConsecutiveFailed = 0;
390 
391  while (mi != mempool.mapTx.get<ancestor_score>().end() || !mapModifiedTx.empty())
392  {
393  // First try to find a new transaction in mapTx to evaluate.
394  if (mi != mempool.mapTx.get<ancestor_score>().end() &&
395  SkipMapTxEntry(mempool.mapTx.project<0>(mi), mapModifiedTx, failedTx)) {
396  ++mi;
397  continue;
398  }
399 
400  // Now that mi is not stale, determine which transaction to evaluate:
401  // the next entry from mapTx, or the best from mapModifiedTx?
402  bool fUsingModified = false;
403 
404  modtxscoreiter modit = mapModifiedTx.get<ancestor_score>().begin();
405  if (mi == mempool.mapTx.get<ancestor_score>().end()) {
406  // We're out of entries in mapTx; use the entry from mapModifiedTx
407  iter = modit->iter;
408  fUsingModified = true;
409  } else {
410  // Try to compare the mapTx entry to the mapModifiedTx entry
411  iter = mempool.mapTx.project<0>(mi);
412  if (modit != mapModifiedTx.get<ancestor_score>().end() &&
414  // The best entry in mapModifiedTx has higher score
415  // than the one from mapTx.
416  // Switch which transaction (package) to consider
417  iter = modit->iter;
418  fUsingModified = true;
419  } else {
420  // Either no entry in mapModifiedTx, or it's worse than mapTx.
421  // Increment mi for the next loop iteration.
422  ++mi;
423  }
424  }
425 
426  // We skip mapTx entries that are inBlock, and mapModifiedTx shouldn't
427  // contain anything that is inBlock.
428  assert(!inBlock.count(iter));
429 
430  uint64_t packageSize = iter->GetSizeWithAncestors();
431  CAmount packageFees = iter->GetModFeesWithAncestors();
432  unsigned int packageSigOps = iter->GetSigOpCountWithAncestors();
433  if (fUsingModified) {
434  packageSize = modit->nSizeWithAncestors;
435  packageFees = modit->nModFeesWithAncestors;
436  packageSigOps = modit->nSigOpCountWithAncestors;
437  }
438 
439  if (packageFees < blockMinFeeRate.GetFee(packageSize)) {
440  // Everything else we might consider has a lower fee rate
441  return;
442  }
443 
444  if (!TestPackage(packageSize, packageSigOps)) {
445  if (fUsingModified) {
446  // Since we always look at the best entry in mapModifiedTx,
447  // we must erase failed entries so that we can consider the
448  // next best entry on the next loop iteration
449  mapModifiedTx.get<ancestor_score>().erase(modit);
450  failedTx.insert(iter);
451  }
452 
453  ++nConsecutiveFailed;
454 
455  if (nConsecutiveFailed > MAX_CONSECUTIVE_FAILURES && nBlockSize > nBlockMaxSize - 1000) {
456  // Give up if we're close to full and haven't succeeded in a while
457  break;
458  }
459  continue;
460  }
461 
462  CTxMemPool::setEntries ancestors;
463  uint64_t nNoLimit = std::numeric_limits<uint64_t>::max();
464  std::string dummy;
465  mempool.CalculateMemPoolAncestors(*iter, ancestors, nNoLimit, nNoLimit, nNoLimit, nNoLimit, dummy, false);
466 
467  onlyUnconfirmed(ancestors);
468  ancestors.insert(iter);
469 
470  // Test if all tx's are Final and safe
471  if (!TestPackageTransactions(ancestors)) {
472  if (fUsingModified) {
473  mapModifiedTx.get<ancestor_score>().erase(modit);
474  failedTx.insert(iter);
475  }
476  continue;
477  }
478 
479  // This transaction will make it in; reset the failed counter.
480  nConsecutiveFailed = 0;
481 
482  // Package can be added. Sort the entries in a valid order.
483  std::vector<CTxMemPool::txiter> sortedEntries;
484  SortForBlock(ancestors, iter, sortedEntries);
485 
486  for (size_t i=0; i<sortedEntries.size(); ++i) {
487  AddToBlock(sortedEntries[i]);
488  // Erase from the modified set, if present
489  mapModifiedTx.erase(sortedEntries[i]);
490  }
491 
492  ++nPackagesSelected;
493 
494  // Update transactions that depend on each of these
495  nDescendantsUpdated += UpdatePackagesForAdded(ancestors, mapModifiedTx);
496  }
497 }
498 
499 void IncrementExtraNonce(CBlock* pblock, const CBlockIndex* pindexPrev, unsigned int& nExtraNonce)
500 {
501  // Update nExtraNonce
502  static uint256 hashPrevBlock;
503  if (hashPrevBlock != pblock->hashPrevBlock)
504  {
505  nExtraNonce = 0;
506  hashPrevBlock = pblock->hashPrevBlock;
507  }
508  ++nExtraNonce;
509  unsigned int nHeight = pindexPrev->nHeight+1; // Height first in coinbase required for block.version=2
510  CMutableTransaction txCoinbase(*pblock->vtx[0]);
511  txCoinbase.vin[0].scriptSig = (CScript() << nHeight << CScriptNum(nExtraNonce)) + COINBASE_FLAGS;
512  assert(txCoinbase.vin[0].scriptSig.size() <= 100);
513 
514  pblock->vtx[0] = MakeTransactionRef(std::move(txCoinbase));
515  pblock->hashMerkleRoot = BlockMerkleRoot(*pblock);
516 }
uint32_t nNonce
Definition: block.h:29
bool CalcCbTxMerkleRootMNList(const CBlock &block, const CBlockIndex *pindexPrev, uint256 &merkleRootRet, CValidationState &state)
Definition: cbtx.cpp:103
indexed_modified_transaction_set::nth_index< 0 >::type::iterator modtxiter
Definition: miner.h:108
bool TestPackageTransactions(const CTxMemPool::setEntries &package)
Perform checks on each transaction in a package: locktime These checks should always succeed...
Definition: miner.cpp:276
uint64_t nBlockSize
Definition: miner.h:139
CTxMemPool mempool
bool IsArgSet(const std::string &strArg) const
Return true if the given argument has been manually set.
Definition: util.cpp:784
CFeeRate blockMinFeeRate
Definition: miner.h:154
bool BIP9CheckMasternodesUpgraded() const
Definition: chainparams.h:98
Definition: miner.h:38
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.
Definition: miner.cpp:349
static const unsigned int STANDARD_LOCKTIME_VERIFY_FLAGS
Used as the flags parameter to sequence and nLocktime checks in non-consensus code.
Definition: policy.h:60
void AddToBlock(CTxMemPool::txiter iter)
Add a tx to the block.
Definition: miner.cpp:288
int64_t UpdateTime(CBlockHeader *pblock, const Consensus::Params &consensusParams, const CBlockIndex *pindexPrev)
Definition: miner.cpp:59
uint64_t nLastBlockSize
Definition: miner.cpp:57
Definition: block.h:72
Definition: cbtx.h:16
unsigned int MaxBlockSize(bool fDIP0001Active)
Definition: consensus.h:12
uint64_t nBlockTx
Definition: miner.h:140
#define strprintf
Definition: tinyformat.h:1066
std::vector< CTxIn > vin
Definition: transaction.h:293
CQuorumBlockProcessor * quorumBlockProcessor
void onlyUnconfirmed(CTxMemPool::setEntries &testSet)
Remove confirmed (inBlock) entries from given set.
Definition: miner.cpp:251
int64_t nLockTimeCutoff
Definition: miner.h:147
CCriticalSection cs_main
Definition: validation.cpp:213
CTxMemPool::setEntries inBlock
Definition: miner.h:143
std::unique_ptr< CBlockTemplate > pblocktemplate
Definition: miner.h:130
bool fPowAllowMinDifficultyBlocks
Definition: params.h:174
CBlock * pblock
Definition: miner.h:132
std::set< txiter, CompareIteratorByHash > setEntries
Definition: txmempool.h:499
int64_t GetTimeMicros()
Returns the system time (not mockable)
Definition: utiltime.cpp:63
CChainParams defines various tweakable parameters of a given instance of the Dash system...
Definition: chainparams.h:41
bool GetBoolArg(const std::string &strArg, bool fDefault) const
Return boolean argument or default value.
Definition: util.cpp:824
unsigned int nBlockMaxSize
Definition: miner.h:135
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:345
uint32_t nTime
Definition: block.h:27
indexed_modified_transaction_set::index< ancestor_score >::type::iterator modtxscoreiter
Definition: miner.h:109
indexed_transaction_set mapTx
Definition: txmempool.h:489
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
uint256 GetBlockHash() const
Definition: chain.h:292
static CTransactionRef MakeTransactionRef()
Definition: transaction.h:346
#define LOCK2(cs1, cs2)
Definition: sync.h:179
CChainLocksHandler * chainLocksHandler
#define LogPrintf(...)
Definition: util.h:203
bool MineBlocksOnDemand() const
Make miner stop after a block is found.
Definition: chainparams.h:68
Definition: txmempool.h:281
uint256 hashMerkleRoot
Definition: block.h:26
const CChainParams & chainparams
Definition: miner.h:148
static constexpr unsigned int LOCKTIME_MEDIAN_TIME_PAST
Use GetMedianTimePast() instead of nTime for end point timestamp.
Definition: consensus.h:30
uint256 merkleRootMNList
Definition: cbtx.h:24
unsigned int GetNextWorkRequired(const CBlockIndex *pindexLast, const CBlockHeader *pblock, const Consensus::Params &params)
Definition: pow.cpp:168
uint64_t nLastBlockTx
Definition: miner.cpp:56
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...
Definition: policy.h:22
unsigned int MaxBlockSigOps(bool fDIP0001Active)
The maximum allowed number of signature check operations in a block (network rule) ...
Definition: consensus.h:17
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
Definition: miner.h:106
uint256 merkleRootQuorums
Definition: cbtx.h:25
void CalculateDescendants(txiter it, setEntries &setDescendants)
Populate setDescendants with all in-mempool descendants of hash.
Definition: txmempool.cpp:702
CAmount nFees
Definition: miner.h:142
uint256 hashPrevBlock
Definition: block.h:25
int DIP0003Height
Block height at which DIP0003 becomes active.
Definition: params.h:158
BlockAssembler(const CChainParams &params)
Definition: miner.cpp:104
std::atomic< bool > fDIP0001ActiveAtTip
Definition: validation.cpp:240
Generate a new block, without valid proof-of-work.
Definition: miner.h:126
uint256 BlockMerkleRoot(const CBlock &block, bool *mutated)
Definition: merkle.cpp:66
bool ParseMoney(const std::string &str, CAmount &nRet)
static const unsigned int DEFAULT_BLOCK_MAX_SIZE
Default for -blockmaxsize, which controls the maximum size of block the mining code will create...
Definition: policy.h:20
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.
Definition: txmempool.cpp:154
std::unique_ptr< CBlockTemplate > CreateNewBlock(const CScript &scriptPubKeyIn)
Construct a new block template with coinbase to scriptPubKeyIn.
Definition: miner.cpp:119
Parameters that influence chain consensus.
Definition: params.h:130
CScript COINBASE_FLAGS
Constant stuff for coinbase transactions we create:
Definition: validation.cpp:255
int64_t GetBlockTime() const
Definition: block.h:65
std::vector< CTxOut > vout
Definition: transaction.h:294
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...
Definition: miner.cpp:307
unsigned int nBlockSigOps
Definition: miner.h:141
std::map< LLMQType, LLMQParams > llmqs
Definition: params.h:189
int64_t GetMedianTimePast() const
Definition: chain.h:309
indexed_transaction_set::nth_index< 0 >::type::iterator txiter
Definition: txmempool.h:491
CCriticalSection cs
Definition: txmempool.h:488
VersionBitsCache versionbitscache
uint64_t nSizeWithAncestors
Definition: miner.h:54
int32_t nHeight
Definition: cbtx.h:23
#define LogPrint(category,...)
Definition: util.h:214
void IncrementExtraNonce(CBlock *pblock, const CBlockIndex *pindexPrev, unsigned int &nExtraNonce)
Modify the extranonce in a block.
Definition: miner.cpp:499
Capture information about block/transaction validation.
Definition: validation.h:22
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&#39;s cache...
Definition: miner.cpp:343
256-bit opaque blob.
Definition: uint256.h:123
CAmount nModFeesWithAncestors
Definition: miner.h:55
ArgsManager gArgs
Definition: util.cpp:108
std::vector< CTransactionRef > vtx
Definition: block.h:76
std::string FormatStateMessage(const CValidationState &state)
Convert CValidationState to a human-readable message for logging.
Definition: validation.cpp:513
unsigned int GetLegacySigOpCount(const CTransaction &tx)
Auxiliary functions for transaction validation (ideally should not be exposed)
Definition: tx_verify.cpp:107
CFeeRate blockMinFeeRate
Definition: miner.h:136
The block chain is a tree shaped structure starting with the genesis block at the root...
Definition: chain.h:170
const CChainParams & Params()
Return the currently selected parameters.
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:389
int nHeight
Definition: miner.h:146
std::string GetArg(const std::string &strArg, const std::string &strDefault) const
Return string argument or default value.
Definition: util.cpp:808
int64_t GetAdjustedTime()
Definition: timedata.cpp:35
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...
CBlockIndex * Tip() const
Returns the index entry for the tip of this chain, or nullptr if none.
Definition: chain.h:453
void FillBlockPayments(CMutableTransaction &txNew, int nBlockHeight, CAmount blockReward, std::vector< CTxOut > &voutMasternodePaymentsRet, std::vector< CTxOut > &voutSuperblockPaymentsRet)
Fee rate in satoshis per kilobyte: CAmount / kB.
Definition: feerate.h:19
unsigned int nSigOpCountWithAncestors
Definition: miner.h:56
void resetBlock()
Clear the block&#39;s state and prepare for assembling a new block.
Definition: miner.cpp:106
void addPackageTxs(int &nPackagesSelected, int &nDescendantsUpdated)
Add transactions based on feerate including unconfirmed ancestors Increments nPackagesSelected / nDes...
Definition: miner.cpp:370
A mutable version of CTransaction.
Definition: transaction.h:291
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...
Definition: tx_verify.cpp:17
uint16_t nVersion
Definition: cbtx.h:22
bool CalcCbTxMerkleRootQuorums(const CBlock &block, const CBlockIndex *pindexPrev, uint256 &merkleRootRet, CValidationState &state)
Definition: cbtx.cpp:161
std::string ToString() const
Definition: feerate.cpp:40
int nHeight
height of the entry in the chain. The genesis block has height 0
Definition: chain.h:183
const Consensus::Params & GetConsensus() const
Definition: chainparams.h:54
static const bool DEFAULT_PRINTPRIORITY
Definition: miner.h:24
CAmount GetBlockSubsidy(int nPrevBits, int nPrevHeight, const Consensus::Params &consensusParams, bool fSuperblockPartOnly)
CChain & chainActive
The currently-connected chain of blocks (protected by cs_main).
Definition: validation.cpp:217
static BlockAssembler::Options DefaultOptions(const CChainParams &params)
Definition: miner.cpp:86
ThresholdState VersionBitsState(const CBlockIndex *pindexPrev, const Consensus::Params &params, Consensus::DeploymentPos pos, VersionBitsCache &cache)
Definition: script.h:51
int32_t nVersion
Definition: block.h:24
void SetTxPayload(CMutableTransaction &tx, const T &payload)
Definition: specialtx.h:43
uint32_t nBits
Definition: chain.h:213
bool TestPackage(uint64_t packageSize, unsigned int packageSigOps) const
Test if a new package would "fit" in the block.
Definition: miner.cpp:264
Nodes collect new transactions into a block, hash them into a hash tree, and scan through nonce value...
Definition: block.h:20
uint32_t nBits
Definition: block.h:28
int32_t ComputeBlockVersion(const CBlockIndex *pindexPrev, const Consensus::Params &params, bool fCheckMasternodesUpgraded)
Determine what nVersion a new block should use.
CAmount GetFee(size_t nBytes) const
Return the fee in satoshis for the given size in bytes.
Definition: feerate.cpp:23
Released under the MIT license