You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

miner.cpp 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. // Copyright (c) 2009-2010 Satoshi Nakamoto
  2. // Copyright (c) 2009-2016 The Bitcoin Core developers
  3. // Distributed under the MIT software license, see the accompanying
  4. // file COPYING or http://www.opensource.org/licenses/mit-license.php.
  5. #include "miner.h"
  6. #include "amount.h"
  7. #include "chain.h"
  8. #include "chainparams.h"
  9. #include "coins.h"
  10. #include "consensus/consensus.h"
  11. #include "consensus/merkle.h"
  12. #include "consensus/validation.h"
  13. #include "hash.h"
  14. #include "validation.h"
  15. #include "net.h"
  16. #include "policy/policy.h"
  17. #include "pow.h"
  18. #include "primitives/transaction.h"
  19. #include "script/standard.h"
  20. #include "timedata.h"
  21. #include "txmempool.h"
  22. #include "util.h"
  23. #include "utilmoneystr.h"
  24. #include "validationinterface.h"
  25. #include <algorithm>
  26. #include <boost/thread.hpp>
  27. #include <boost/tuple/tuple.hpp>
  28. #include <queue>
  29. #include <utility>
  30. //////////////////////////////////////////////////////////////////////////////
  31. //
  32. // BitcoinMiner
  33. //
  34. //
  35. // Unconfirmed transactions in the memory pool often depend on other
  36. // transactions in the memory pool. When we select transactions from the
  37. // pool, we select by highest fee rate of a transaction combined with all
  38. // its ancestors.
  39. uint64_t nLastBlockTx = 0;
  40. uint64_t nLastBlockSize = 0;
  41. uint64_t nLastBlockWeight = 0;
  42. class ScoreCompare
  43. {
  44. public:
  45. ScoreCompare() {}
  46. bool operator()(const CTxMemPool::txiter a, const CTxMemPool::txiter b)
  47. {
  48. return CompareTxMemPoolEntryByScore()(*b,*a); // Convert to less than
  49. }
  50. };
  51. int64_t UpdateTime(CBlockHeader* pblock, const Consensus::Params& consensusParams, const CBlockIndex* pindexPrev)
  52. {
  53. int64_t nOldTime = pblock->nTime;
  54. int64_t nNewTime = std::max(pindexPrev->GetMedianTimePast()+1, GetAdjustedTime());
  55. if (nOldTime < nNewTime)
  56. pblock->nTime = nNewTime;
  57. // Updating time can change work required on testnet:
  58. if (consensusParams.fPowAllowMinDifficultyBlocks)
  59. pblock->nBits = GetNextWorkRequired(pindexPrev, pblock, consensusParams);
  60. return nNewTime - nOldTime;
  61. }
  62. BlockAssembler::BlockAssembler(const CChainParams& _chainparams)
  63. : chainparams(_chainparams)
  64. {
  65. // Block resource limits
  66. // If neither -blockmaxsize or -blockmaxweight is given, limit to DEFAULT_BLOCK_MAX_*
  67. // If only one is given, only restrict the specified resource.
  68. // If both are given, restrict both.
  69. nBlockMaxWeight = DEFAULT_BLOCK_MAX_WEIGHT;
  70. nBlockMaxSize = DEFAULT_BLOCK_MAX_SIZE;
  71. bool fWeightSet = false;
  72. if (IsArgSet("-blockmaxweight")) {
  73. nBlockMaxWeight = GetArg("-blockmaxweight", DEFAULT_BLOCK_MAX_WEIGHT);
  74. nBlockMaxSize = MAX_BLOCK_SERIALIZED_SIZE;
  75. fWeightSet = true;
  76. }
  77. if (IsArgSet("-blockmaxsize")) {
  78. nBlockMaxSize = GetArg("-blockmaxsize", DEFAULT_BLOCK_MAX_SIZE);
  79. if (!fWeightSet) {
  80. nBlockMaxWeight = nBlockMaxSize * WITNESS_SCALE_FACTOR;
  81. }
  82. }
  83. if (IsArgSet("-blockmintxfee")) {
  84. CAmount n = 0;
  85. ParseMoney(GetArg("-blockmintxfee", ""), n);
  86. blockMinFeeRate = CFeeRate(n);
  87. } else {
  88. blockMinFeeRate = CFeeRate(DEFAULT_BLOCK_MIN_TX_FEE);
  89. }
  90. // Limit weight to between 4K and MAX_BLOCK_WEIGHT-4K for sanity:
  91. nBlockMaxWeight = std::max((unsigned int)4000, std::min((unsigned int)(MAX_BLOCK_WEIGHT-4000), nBlockMaxWeight));
  92. // Limit size to between 1K and MAX_BLOCK_SERIALIZED_SIZE-1K for sanity:
  93. nBlockMaxSize = std::max((unsigned int)1000, std::min((unsigned int)(MAX_BLOCK_SERIALIZED_SIZE-1000), nBlockMaxSize));
  94. // Whether we need to account for byte usage (in addition to weight usage)
  95. fNeedSizeAccounting = (nBlockMaxSize < MAX_BLOCK_SERIALIZED_SIZE-1000);
  96. }
  97. void BlockAssembler::resetBlock()
  98. {
  99. inBlock.clear();
  100. // Reserve space for coinbase tx
  101. nBlockSize = 1000;
  102. nBlockWeight = 4000;
  103. nBlockSigOpsCost = 400;
  104. fIncludeWitness = false;
  105. // These counters do not include coinbase tx
  106. nBlockTx = 0;
  107. nFees = 0;
  108. }
  109. std::unique_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock(const CScript& scriptPubKeyIn)
  110. {
  111. resetBlock();
  112. pblocktemplate.reset(new CBlockTemplate());
  113. if(!pblocktemplate.get())
  114. return nullptr;
  115. pblock = &pblocktemplate->block; // pointer for convenience
  116. // Add dummy coinbase tx as first transaction
  117. pblock->vtx.emplace_back();
  118. pblocktemplate->vTxFees.push_back(-1); // updated at end
  119. pblocktemplate->vTxSigOpsCost.push_back(-1); // updated at end
  120. LOCK2(cs_main, mempool.cs);
  121. CBlockIndex* pindexPrev = chainActive.Tip();
  122. nHeight = pindexPrev->nHeight + 1;
  123. pblock->nVersion = ComputeBlockVersion(pindexPrev, chainparams.GetConsensus());
  124. // -regtest only: allow overriding block.nVersion with
  125. // -blockversion=N to test forking scenarios
  126. if (chainparams.MineBlocksOnDemand())
  127. pblock->nVersion = GetArg("-blockversion", pblock->nVersion);
  128. pblock->nTime = GetAdjustedTime();
  129. const int64_t nMedianTimePast = pindexPrev->GetMedianTimePast();
  130. nLockTimeCutoff = (STANDARD_LOCKTIME_VERIFY_FLAGS & LOCKTIME_MEDIAN_TIME_PAST)
  131. ? nMedianTimePast
  132. : pblock->GetBlockTime();
  133. // Decide whether to include witness transactions
  134. // This is only needed in case the witness softfork activation is reverted
  135. // (which would require a very deep reorganization) or when
  136. // -promiscuousmempoolflags is used.
  137. // TODO: replace this with a call to main to assess validity of a mempool
  138. // transaction (which in most cases can be a no-op).
  139. fIncludeWitness = IsWitnessEnabled(pindexPrev, chainparams.GetConsensus());
  140. addPackageTxs();
  141. nLastBlockTx = nBlockTx;
  142. nLastBlockSize = nBlockSize;
  143. nLastBlockWeight = nBlockWeight;
  144. // Create coinbase transaction.
  145. CMutableTransaction coinbaseTx;
  146. coinbaseTx.vin.resize(1);
  147. coinbaseTx.vin[0].prevout.SetNull();
  148. coinbaseTx.vout.resize(1);
  149. coinbaseTx.vout[0].scriptPubKey = scriptPubKeyIn;
  150. coinbaseTx.vout[0].nValue = nFees + GetBlockSubsidy(nHeight, chainparams.GetConsensus());
  151. coinbaseTx.vin[0].scriptSig = CScript() << nHeight << OP_0;
  152. pblock->vtx[0] = MakeTransactionRef(std::move(coinbaseTx));
  153. pblocktemplate->vchCoinbaseCommitment = GenerateCoinbaseCommitment(*pblock, pindexPrev, chainparams.GetConsensus());
  154. pblocktemplate->vTxFees[0] = -nFees;
  155. uint64_t nSerializeSize = GetSerializeSize(*pblock, SER_NETWORK, PROTOCOL_VERSION);
  156. LogPrintf("CreateNewBlock(): total size: %u block weight: %u txs: %u fees: %ld sigops %d\n", nSerializeSize, GetBlockWeight(*pblock), nBlockTx, nFees, nBlockSigOpsCost);
  157. // Fill in header
  158. pblock->hashPrevBlock = pindexPrev->GetBlockHash();
  159. UpdateTime(pblock, chainparams.GetConsensus(), pindexPrev);
  160. pblock->nBits = GetNextWorkRequired(pindexPrev, pblock, chainparams.GetConsensus());
  161. pblock->nNonce = 0;
  162. pblocktemplate->vTxSigOpsCost[0] = WITNESS_SCALE_FACTOR * GetLegacySigOpCount(*pblock->vtx[0]);
  163. CValidationState state;
  164. if (!TestBlockValidity(state, chainparams, *pblock, pindexPrev, false, false)) {
  165. throw std::runtime_error(strprintf("%s: TestBlockValidity failed: %s", __func__, FormatStateMessage(state)));
  166. }
  167. return std::move(pblocktemplate);
  168. }
  169. void BlockAssembler::onlyUnconfirmed(CTxMemPool::setEntries& testSet)
  170. {
  171. for (CTxMemPool::setEntries::iterator iit = testSet.begin(); iit != testSet.end(); ) {
  172. // Only test txs not already in the block
  173. if (inBlock.count(*iit)) {
  174. testSet.erase(iit++);
  175. }
  176. else {
  177. iit++;
  178. }
  179. }
  180. }
  181. bool BlockAssembler::TestPackage(uint64_t packageSize, int64_t packageSigOpsCost)
  182. {
  183. // TODO: switch to weight-based accounting for packages instead of vsize-based accounting.
  184. if (nBlockWeight + WITNESS_SCALE_FACTOR * packageSize >= nBlockMaxWeight)
  185. return false;
  186. if (nBlockSigOpsCost + packageSigOpsCost >= MAX_BLOCK_SIGOPS_COST)
  187. return false;
  188. return true;
  189. }
  190. // Perform transaction-level checks before adding to block:
  191. // - transaction finality (locktime)
  192. // - premature witness (in case segwit transactions are added to mempool before
  193. // segwit activation)
  194. // - serialized size (in case -blockmaxsize is in use)
  195. bool BlockAssembler::TestPackageTransactions(const CTxMemPool::setEntries& package)
  196. {
  197. uint64_t nPotentialBlockSize = nBlockSize; // only used with fNeedSizeAccounting
  198. BOOST_FOREACH (const CTxMemPool::txiter it, package) {
  199. if (!IsFinalTx(it->GetTx(), nHeight, nLockTimeCutoff))
  200. return false;
  201. if (!fIncludeWitness && it->GetTx().HasWitness())
  202. return false;
  203. if (fNeedSizeAccounting) {
  204. uint64_t nTxSize = ::GetSerializeSize(it->GetTx(), SER_NETWORK, PROTOCOL_VERSION);
  205. if (nPotentialBlockSize + nTxSize >= nBlockMaxSize) {
  206. return false;
  207. }
  208. nPotentialBlockSize += nTxSize;
  209. }
  210. }
  211. return true;
  212. }
  213. void BlockAssembler::AddToBlock(CTxMemPool::txiter iter)
  214. {
  215. pblock->vtx.emplace_back(iter->GetSharedTx());
  216. pblocktemplate->vTxFees.push_back(iter->GetFee());
  217. pblocktemplate->vTxSigOpsCost.push_back(iter->GetSigOpCost());
  218. if (fNeedSizeAccounting) {
  219. nBlockSize += ::GetSerializeSize(iter->GetTx(), SER_NETWORK, PROTOCOL_VERSION);
  220. }
  221. nBlockWeight += iter->GetTxWeight();
  222. ++nBlockTx;
  223. nBlockSigOpsCost += iter->GetSigOpCost();
  224. nFees += iter->GetFee();
  225. inBlock.insert(iter);
  226. bool fPrintPriority = GetBoolArg("-printpriority", DEFAULT_PRINTPRIORITY);
  227. if (fPrintPriority) {
  228. double dPriority = iter->GetPriority(nHeight);
  229. CAmount dummy;
  230. mempool.ApplyDeltas(iter->GetTx().GetHash(), dPriority, dummy);
  231. LogPrintf("priority %.1f fee %s txid %s\n",
  232. dPriority,
  233. CFeeRate(iter->GetModifiedFee(), iter->GetTxSize()).ToString(),
  234. iter->GetTx().GetHash().ToString());
  235. }
  236. }
  237. void BlockAssembler::UpdatePackagesForAdded(const CTxMemPool::setEntries& alreadyAdded,
  238. indexed_modified_transaction_set &mapModifiedTx)
  239. {
  240. BOOST_FOREACH(const CTxMemPool::txiter it, alreadyAdded) {
  241. CTxMemPool::setEntries descendants;
  242. mempool.CalculateDescendants(it, descendants);
  243. // Insert all descendants (not yet in block) into the modified set
  244. BOOST_FOREACH(CTxMemPool::txiter desc, descendants) {
  245. if (alreadyAdded.count(desc))
  246. continue;
  247. modtxiter mit = mapModifiedTx.find(desc);
  248. if (mit == mapModifiedTx.end()) {
  249. CTxMemPoolModifiedEntry modEntry(desc);
  250. modEntry.nSizeWithAncestors -= it->GetTxSize();
  251. modEntry.nModFeesWithAncestors -= it->GetModifiedFee();
  252. modEntry.nSigOpCostWithAncestors -= it->GetSigOpCost();
  253. mapModifiedTx.insert(modEntry);
  254. } else {
  255. mapModifiedTx.modify(mit, update_for_parent_inclusion(it));
  256. }
  257. }
  258. }
  259. }
  260. // Skip entries in mapTx that are already in a block or are present
  261. // in mapModifiedTx (which implies that the mapTx ancestor state is
  262. // stale due to ancestor inclusion in the block)
  263. // Also skip transactions that we've already failed to add. This can happen if
  264. // we consider a transaction in mapModifiedTx and it fails: we can then
  265. // potentially consider it again while walking mapTx. It's currently
  266. // guaranteed to fail again, but as a belt-and-suspenders check we put it in
  267. // failedTx and avoid re-evaluation, since the re-evaluation would be using
  268. // cached size/sigops/fee values that are not actually correct.
  269. bool BlockAssembler::SkipMapTxEntry(CTxMemPool::txiter it, indexed_modified_transaction_set &mapModifiedTx, CTxMemPool::setEntries &failedTx)
  270. {
  271. assert (it != mempool.mapTx.end());
  272. if (mapModifiedTx.count(it) || inBlock.count(it) || failedTx.count(it))
  273. return true;
  274. return false;
  275. }
  276. void BlockAssembler::SortForBlock(const CTxMemPool::setEntries& package, CTxMemPool::txiter entry, std::vector<CTxMemPool::txiter>& sortedEntries)
  277. {
  278. // Sort package by ancestor count
  279. // If a transaction A depends on transaction B, then A's ancestor count
  280. // must be greater than B's. So this is sufficient to validly order the
  281. // transactions for block inclusion.
  282. sortedEntries.clear();
  283. sortedEntries.insert(sortedEntries.begin(), package.begin(), package.end());
  284. std::sort(sortedEntries.begin(), sortedEntries.end(), CompareTxIterByAncestorCount());
  285. }
  286. // This transaction selection algorithm orders the mempool based
  287. // on feerate of a transaction including all unconfirmed ancestors.
  288. // Since we don't remove transactions from the mempool as we select them
  289. // for block inclusion, we need an alternate method of updating the feerate
  290. // of a transaction with its not-yet-selected ancestors as we go.
  291. // This is accomplished by walking the in-mempool descendants of selected
  292. // transactions and storing a temporary modified state in mapModifiedTxs.
  293. // Each time through the loop, we compare the best transaction in
  294. // mapModifiedTxs with the next transaction in the mempool to decide what
  295. // transaction package to work on next.
  296. void BlockAssembler::addPackageTxs()
  297. {
  298. // mapModifiedTx will store sorted packages after they are modified
  299. // because some of their txs are already in the block
  300. indexed_modified_transaction_set mapModifiedTx;
  301. // Keep track of entries that failed inclusion, to avoid duplicate work
  302. CTxMemPool::setEntries failedTx;
  303. // Start by adding all descendants of previously added txs to mapModifiedTx
  304. // and modifying them for their already included ancestors
  305. UpdatePackagesForAdded(inBlock, mapModifiedTx);
  306. CTxMemPool::indexed_transaction_set::index<ancestor_score>::type::iterator mi = mempool.mapTx.get<ancestor_score>().begin();
  307. CTxMemPool::txiter iter;
  308. while (mi != mempool.mapTx.get<ancestor_score>().end() || !mapModifiedTx.empty())
  309. {
  310. // First try to find a new transaction in mapTx to evaluate.
  311. if (mi != mempool.mapTx.get<ancestor_score>().end() &&
  312. SkipMapTxEntry(mempool.mapTx.project<0>(mi), mapModifiedTx, failedTx)) {
  313. ++mi;
  314. continue;
  315. }
  316. // Now that mi is not stale, determine which transaction to evaluate:
  317. // the next entry from mapTx, or the best from mapModifiedTx?
  318. bool fUsingModified = false;
  319. modtxscoreiter modit = mapModifiedTx.get<ancestor_score>().begin();
  320. if (mi == mempool.mapTx.get<ancestor_score>().end()) {
  321. // We're out of entries in mapTx; use the entry from mapModifiedTx
  322. iter = modit->iter;
  323. fUsingModified = true;
  324. } else {
  325. // Try to compare the mapTx entry to the mapModifiedTx entry
  326. iter = mempool.mapTx.project<0>(mi);
  327. if (modit != mapModifiedTx.get<ancestor_score>().end() &&
  328. CompareModifiedEntry()(*modit, CTxMemPoolModifiedEntry(iter))) {
  329. // The best entry in mapModifiedTx has higher score
  330. // than the one from mapTx.
  331. // Switch which transaction (package) to consider
  332. iter = modit->iter;
  333. fUsingModified = true;
  334. } else {
  335. // Either no entry in mapModifiedTx, or it's worse than mapTx.
  336. // Increment mi for the next loop iteration.
  337. ++mi;
  338. }
  339. }
  340. // We skip mapTx entries that are inBlock, and mapModifiedTx shouldn't
  341. // contain anything that is inBlock.
  342. assert(!inBlock.count(iter));
  343. uint64_t packageSize = iter->GetSizeWithAncestors();
  344. CAmount packageFees = iter->GetModFeesWithAncestors();
  345. int64_t packageSigOpsCost = iter->GetSigOpCostWithAncestors();
  346. if (fUsingModified) {
  347. packageSize = modit->nSizeWithAncestors;
  348. packageFees = modit->nModFeesWithAncestors;
  349. packageSigOpsCost = modit->nSigOpCostWithAncestors;
  350. }
  351. if (packageFees < blockMinFeeRate.GetFee(packageSize)) {
  352. // Everything else we might consider has a lower fee rate
  353. return;
  354. }
  355. if (!TestPackage(packageSize, packageSigOpsCost)) {
  356. if (fUsingModified) {
  357. // Since we always look at the best entry in mapModifiedTx,
  358. // we must erase failed entries so that we can consider the
  359. // next best entry on the next loop iteration
  360. mapModifiedTx.get<ancestor_score>().erase(modit);
  361. failedTx.insert(iter);
  362. }
  363. continue;
  364. }
  365. CTxMemPool::setEntries ancestors;
  366. uint64_t nNoLimit = std::numeric_limits<uint64_t>::max();
  367. std::string dummy;
  368. mempool.CalculateMemPoolAncestors(*iter, ancestors, nNoLimit, nNoLimit, nNoLimit, nNoLimit, dummy, false);
  369. onlyUnconfirmed(ancestors);
  370. ancestors.insert(iter);
  371. // Test if all tx's are Final
  372. if (!TestPackageTransactions(ancestors)) {
  373. if (fUsingModified) {
  374. mapModifiedTx.get<ancestor_score>().erase(modit);
  375. failedTx.insert(iter);
  376. }
  377. continue;
  378. }
  379. // Package can be added. Sort the entries in a valid order.
  380. std::vector<CTxMemPool::txiter> sortedEntries;
  381. SortForBlock(ancestors, iter, sortedEntries);
  382. for (size_t i=0; i<sortedEntries.size(); ++i) {
  383. AddToBlock(sortedEntries[i]);
  384. // Erase from the modified set, if present
  385. mapModifiedTx.erase(sortedEntries[i]);
  386. }
  387. // Update transactions that depend on each of these
  388. UpdatePackagesForAdded(ancestors, mapModifiedTx);
  389. }
  390. }
  391. void IncrementExtraNonce(CBlock* pblock, const CBlockIndex* pindexPrev, unsigned int& nExtraNonce)
  392. {
  393. // Update nExtraNonce
  394. static uint256 hashPrevBlock;
  395. if (hashPrevBlock != pblock->hashPrevBlock)
  396. {
  397. nExtraNonce = 0;
  398. hashPrevBlock = pblock->hashPrevBlock;
  399. }
  400. ++nExtraNonce;
  401. unsigned int nHeight = pindexPrev->nHeight+1; // Height first in coinbase required for block.version=2
  402. CMutableTransaction txCoinbase(*pblock->vtx[0]);
  403. txCoinbase.vin[0].scriptSig = (CScript() << nHeight << CScriptNum(nExtraNonce)) + COINBASE_FLAGS;
  404. assert(txCoinbase.vin[0].scriptSig.size() <= 100);
  405. pblock->vtx[0] = MakeTransactionRef(std::move(txCoinbase));
  406. pblock->hashMerkleRoot = BlockMerkleRoot(*pblock);
  407. }