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

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