1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2014 The Bitcoin developers
3 // Distributed under the MIT/X11 software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
14 #include "utilmoneystr.h"
19 #include <boost/thread.hpp>
23 //////////////////////////////////////////////////////////////////////////////
29 // Unconfirmed transactions in the memory pool often depend on other
30 // transactions in the memory pool. When we select transactions from the
31 // pool, we select by highest priority or fee rate, so we might consider
32 // transactions that depend on transactions that aren't yet in the block.
33 // The COrphan class keeps track of these 'temporary orphans' while
34 // CreateBlock is figuring out which transactions to include.
39 const CTransaction* ptx;
40 set<uint256> setDependsOn;
44 COrphan(const CTransaction* ptxIn) : ptx(ptxIn), feeRate(0), dPriority(0)
49 uint64_t nLastBlockTx = 0;
50 uint64_t nLastBlockSize = 0;
52 // We want to sort transactions by priority and fee rate, so:
53 typedef boost::tuple<double, CFeeRate, const CTransaction*> TxPriority;
54 class TxPriorityCompare
59 TxPriorityCompare(bool _byFee) : byFee(_byFee) { }
61 bool operator()(const TxPriority& a, const TxPriority& b)
65 if (a.get<1>() == b.get<1>())
66 return a.get<0>() < b.get<0>();
67 return a.get<1>() < b.get<1>();
71 if (a.get<0>() == b.get<0>())
72 return a.get<1>() < b.get<1>();
73 return a.get<0>() < b.get<0>();
78 CBlockTemplate* CreateNewBlock(const CScript& scriptPubKeyIn)
81 auto_ptr<CBlockTemplate> pblocktemplate(new CBlockTemplate());
82 if(!pblocktemplate.get())
84 CBlock *pblock = &pblocktemplate->block; // pointer for convenience
87 CMutableTransaction txNew;
89 txNew.vin[0].prevout.SetNull();
91 txNew.vout[0].scriptPubKey = scriptPubKeyIn;
93 // Add dummy coinbase tx as first transaction
94 pblock->vtx.push_back(CTransaction());
95 pblocktemplate->vTxFees.push_back(-1); // updated at end
96 pblocktemplate->vTxSigOps.push_back(-1); // updated at end
98 // Largest block you're willing to create:
99 unsigned int nBlockMaxSize = GetArg("-blockmaxsize", DEFAULT_BLOCK_MAX_SIZE);
100 // Limit to betweeen 1K and MAX_BLOCK_SIZE-1K for sanity:
101 nBlockMaxSize = std::max((unsigned int)1000, std::min((unsigned int)(MAX_BLOCK_SIZE-1000), nBlockMaxSize));
103 // How much of the block should be dedicated to high-priority transactions,
104 // included regardless of the fees they pay
105 unsigned int nBlockPrioritySize = GetArg("-blockprioritysize", DEFAULT_BLOCK_PRIORITY_SIZE);
106 nBlockPrioritySize = std::min(nBlockMaxSize, nBlockPrioritySize);
108 // Minimum block size you want to create; block will be filled with free transactions
109 // until there are no more or the block reaches this size:
110 unsigned int nBlockMinSize = GetArg("-blockminsize", DEFAULT_BLOCK_MIN_SIZE);
111 nBlockMinSize = std::min(nBlockMaxSize, nBlockMinSize);
113 // Collect memory pool transactions into the block
117 LOCK2(cs_main, mempool.cs);
118 CBlockIndex* pindexPrev = chainActive.Tip();
119 CCoinsViewCache view(*pcoinsTip, true);
121 // Priority order to process transactions
122 list<COrphan> vOrphan; // list memory doesn't move
123 map<uint256, vector<COrphan*> > mapDependers;
124 bool fPrintPriority = GetBoolArg("-printpriority", false);
126 // This vector will be sorted into a priority queue:
127 vector<TxPriority> vecPriority;
128 vecPriority.reserve(mempool.mapTx.size());
129 for (map<uint256, CTxMemPoolEntry>::iterator mi = mempool.mapTx.begin();
130 mi != mempool.mapTx.end(); ++mi)
132 const CTransaction& tx = mi->second.GetTx();
133 if (tx.IsCoinBase() || !IsFinalTx(tx, pindexPrev->nHeight + 1))
136 COrphan* porphan = NULL;
137 double dPriority = 0;
138 int64_t nTotalIn = 0;
139 bool fMissingInputs = false;
140 BOOST_FOREACH(const CTxIn& txin, tx.vin)
142 // Read prev transaction
143 if (!view.HaveCoins(txin.prevout.hash))
145 // This should never happen; all transactions in the memory
146 // pool should connect to either transactions in the chain
147 // or other transactions in the memory pool.
148 if (!mempool.mapTx.count(txin.prevout.hash))
150 LogPrintf("ERROR: mempool transaction missing input\n");
151 if (fDebug) assert("mempool transaction missing input" == 0);
152 fMissingInputs = true;
158 // Has to wait for dependencies
161 // Use list for automatic deletion
162 vOrphan.push_back(COrphan(&tx));
163 porphan = &vOrphan.back();
165 mapDependers[txin.prevout.hash].push_back(porphan);
166 porphan->setDependsOn.insert(txin.prevout.hash);
167 nTotalIn += mempool.mapTx[txin.prevout.hash].GetTx().vout[txin.prevout.n].nValue;
170 const CCoins &coins = view.GetCoins(txin.prevout.hash);
172 int64_t nValueIn = coins.vout[txin.prevout.n].nValue;
173 nTotalIn += nValueIn;
175 int nConf = pindexPrev->nHeight - coins.nHeight + 1;
177 dPriority += (double)nValueIn * nConf;
179 if (fMissingInputs) continue;
181 // Priority is sum(valuein * age) / modified_txsize
182 unsigned int nTxSize = ::GetSerializeSize(tx, SER_NETWORK, PROTOCOL_VERSION);
183 dPriority = tx.ComputePriority(dPriority, nTxSize);
185 uint256 hash = tx.GetHash();
186 mempool.ApplyDeltas(hash, dPriority, nTotalIn);
188 CFeeRate feeRate(nTotalIn-tx.GetValueOut(), nTxSize);
192 porphan->dPriority = dPriority;
193 porphan->feeRate = feeRate;
196 vecPriority.push_back(TxPriority(dPriority, feeRate, &mi->second.GetTx()));
199 // Collect transactions into block
200 uint64_t nBlockSize = 1000;
201 uint64_t nBlockTx = 0;
202 int nBlockSigOps = 100;
203 bool fSortedByFee = (nBlockPrioritySize <= 0);
205 TxPriorityCompare comparer(fSortedByFee);
206 std::make_heap(vecPriority.begin(), vecPriority.end(), comparer);
208 while (!vecPriority.empty())
210 // Take highest priority transaction off the priority queue:
211 double dPriority = vecPriority.front().get<0>();
212 CFeeRate feeRate = vecPriority.front().get<1>();
213 const CTransaction& tx = *(vecPriority.front().get<2>());
215 std::pop_heap(vecPriority.begin(), vecPriority.end(), comparer);
216 vecPriority.pop_back();
219 unsigned int nTxSize = ::GetSerializeSize(tx, SER_NETWORK, PROTOCOL_VERSION);
220 if (nBlockSize + nTxSize >= nBlockMaxSize)
223 // Legacy limits on sigOps:
224 unsigned int nTxSigOps = GetLegacySigOpCount(tx);
225 if (nBlockSigOps + nTxSigOps >= MAX_BLOCK_SIGOPS)
228 // Skip free transactions if we're past the minimum block size:
229 const uint256& hash = tx.GetHash();
230 double dPriorityDelta = 0;
231 int64_t nFeeDelta = 0;
232 mempool.ApplyDeltas(hash, dPriorityDelta, nFeeDelta);
233 if (fSortedByFee && (dPriorityDelta <= 0) && (nFeeDelta <= 0) && (feeRate < ::minRelayTxFee) && (nBlockSize + nTxSize >= nBlockMinSize))
236 // Prioritise by fee once past the priority size or we run out of high-priority
239 ((nBlockSize + nTxSize >= nBlockPrioritySize) || !AllowFree(dPriority)))
242 comparer = TxPriorityCompare(fSortedByFee);
243 std::make_heap(vecPriority.begin(), vecPriority.end(), comparer);
246 if (!view.HaveInputs(tx))
249 int64_t nTxFees = view.GetValueIn(tx)-tx.GetValueOut();
251 nTxSigOps += GetP2SHSigOpCount(tx, view);
252 if (nBlockSigOps + nTxSigOps >= MAX_BLOCK_SIGOPS)
255 // Note that flags: we don't want to set mempool/IsStandard()
256 // policy here, but we still have to ensure that the block we
257 // create only contains transactions that are valid in new blocks.
258 CValidationState state;
259 if (!CheckInputs(tx, state, view, true, MANDATORY_SCRIPT_VERIFY_FLAGS))
263 UpdateCoins(tx, state, view, txundo, pindexPrev->nHeight+1);
266 pblock->vtx.push_back(tx);
267 pblocktemplate->vTxFees.push_back(nTxFees);
268 pblocktemplate->vTxSigOps.push_back(nTxSigOps);
269 nBlockSize += nTxSize;
271 nBlockSigOps += nTxSigOps;
276 LogPrintf("priority %.1f fee %s txid %s\n",
277 dPriority, feeRate.ToString(), tx.GetHash().ToString());
280 // Add transactions that depend on this one to the priority queue
281 if (mapDependers.count(hash))
283 BOOST_FOREACH(COrphan* porphan, mapDependers[hash])
285 if (!porphan->setDependsOn.empty())
287 porphan->setDependsOn.erase(hash);
288 if (porphan->setDependsOn.empty())
290 vecPriority.push_back(TxPriority(porphan->dPriority, porphan->feeRate, porphan->ptx));
291 std::push_heap(vecPriority.begin(), vecPriority.end(), comparer);
298 nLastBlockTx = nBlockTx;
299 nLastBlockSize = nBlockSize;
300 LogPrintf("CreateNewBlock(): total size %u\n", nBlockSize);
302 // Compute final coinbase transaction.
303 txNew.vout[0].nValue = GetBlockValue(pindexPrev->nHeight+1, nFees);
304 txNew.vin[0].scriptSig = CScript() << OP_0 << OP_0;
305 pblock->vtx[0] = txNew;
306 pblocktemplate->vTxFees[0] = -nFees;
309 pblock->hashPrevBlock = pindexPrev->GetBlockHash();
310 UpdateTime(pblock, pindexPrev);
311 pblock->nBits = GetNextWorkRequired(pindexPrev, pblock);
313 pblocktemplate->vTxSigOps[0] = GetLegacySigOpCount(pblock->vtx[0]);
315 CBlockIndex indexDummy(*pblock);
316 indexDummy.pprev = pindexPrev;
317 indexDummy.nHeight = pindexPrev->nHeight + 1;
318 CCoinsViewCache viewNew(*pcoinsTip, true);
319 CValidationState state;
320 if (!ConnectBlock(*pblock, state, &indexDummy, viewNew, true))
321 throw std::runtime_error("CreateNewBlock() : ConnectBlock failed");
324 return pblocktemplate.release();
327 void IncrementExtraNonce(CBlock* pblock, CBlockIndex* pindexPrev, unsigned int& nExtraNonce)
329 // Update nExtraNonce
330 static uint256 hashPrevBlock;
331 if (hashPrevBlock != pblock->hashPrevBlock)
334 hashPrevBlock = pblock->hashPrevBlock;
337 unsigned int nHeight = pindexPrev->nHeight+1; // Height first in coinbase required for block.version=2
338 CMutableTransaction txCoinbase(pblock->vtx[0]);
339 txCoinbase.vin[0].scriptSig = (CScript() << nHeight << CScriptNum(nExtraNonce)) + COINBASE_FLAGS;
340 assert(txCoinbase.vin[0].scriptSig.size() <= 100);
342 pblock->vtx[0] = txCoinbase;
343 pblock->hashMerkleRoot = pblock->BuildMerkleTree();
347 //////////////////////////////////////////////////////////////////////////////
351 double dHashesPerSec = 0.0;
352 int64_t nHPSTimerStart = 0;
355 // ScanHash scans nonces looking for a hash with at least some zero bits.
356 // The nonce is usually preserved between calls, but periodically or if the
357 // nonce is 0xffff0000 or above, the block is rebuilt and nNonce starts over at
360 bool static ScanHash(const CBlockHeader *pblock, uint32_t& nNonce, uint256 *phash)
362 // Write the first 76 bytes of the block header to a double-SHA256 state.
364 CDataStream ss(SER_NETWORK, PROTOCOL_VERSION);
366 assert(ss.size() == 80);
367 hasher.Write((unsigned char*)&ss[0], 76);
372 // Write the last 4 bytes of the block header (the nonce) to a copy of
373 // the double-SHA256 state, and compute the result.
374 CHash256(hasher).Write((unsigned char*)&nNonce, 4).Finalize((unsigned char*)phash);
376 // Return the nonce if the hash has at least some zero bits,
377 // caller will check if it has enough to reach the target
378 if (((uint16_t*)phash)[15] == 0)
381 // If nothing found after trying for a while, return -1
382 if ((nNonce & 0xffff) == 0)
384 if ((nNonce & 0xfff) == 0)
385 boost::this_thread::interruption_point();
389 CBlockTemplate* CreateNewBlockWithKey(CReserveKey& reservekey)
392 if (!reservekey.GetReservedKey(pubkey))
395 CScript scriptPubKey = CScript() << pubkey << OP_CHECKSIG;
396 return CreateNewBlock(scriptPubKey);
399 bool ProcessBlockFound(CBlock* pblock, CWallet& wallet, CReserveKey& reservekey)
401 LogPrintf("%s\n", pblock->ToString());
402 LogPrintf("generated %s\n", FormatMoney(pblock->vtx[0].vout[0].nValue));
407 if (pblock->hashPrevBlock != chainActive.Tip()->GetBlockHash())
408 return error("BitcoinMiner : generated block is stale");
411 // Remove key from key pool
412 reservekey.KeepKey();
414 // Track how many getdata requests this block gets
416 LOCK(wallet.cs_wallet);
417 wallet.mapRequestCount[pblock->GetHash()] = 0;
420 // Process this block the same as if we had received it from another node
421 CValidationState state;
422 if (!ProcessBlock(state, NULL, pblock))
423 return error("BitcoinMiner : ProcessBlock, block not accepted");
428 void static BitcoinMiner(CWallet *pwallet)
430 LogPrintf("BitcoinMiner started\n");
431 SetThreadPriority(THREAD_PRIORITY_LOWEST);
432 RenameThread("bitcoin-miner");
434 // Each thread has its own key and counter
435 CReserveKey reservekey(pwallet);
436 unsigned int nExtraNonce = 0;
440 if (Params().MiningRequiresPeers()) {
441 // Busy-wait for the network to come online so we don't waste time mining
442 // on an obsolete chain. In regtest mode we expect to fly solo.
443 while (vNodes.empty())
450 unsigned int nTransactionsUpdatedLast = mempool.GetTransactionsUpdated();
451 CBlockIndex* pindexPrev = chainActive.Tip();
453 auto_ptr<CBlockTemplate> pblocktemplate(CreateNewBlockWithKey(reservekey));
454 if (!pblocktemplate.get())
456 LogPrintf("Error in BitcoinMiner: Keypool ran out, please call keypoolrefill before restarting the mining thread\n");
459 CBlock *pblock = &pblocktemplate->block;
460 IncrementExtraNonce(pblock, pindexPrev, nExtraNonce);
462 LogPrintf("Running BitcoinMiner with %u transactions in block (%u bytes)\n", pblock->vtx.size(),
463 ::GetSerializeSize(*pblock, SER_NETWORK, PROTOCOL_VERSION));
468 int64_t nStart = GetTime();
469 uint256 hashTarget = uint256().SetCompact(pblock->nBits);
472 uint32_t nOldNonce = 0;
474 bool fFound = ScanHash(pblock, nNonce, &hash);
475 uint32_t nHashesDone = nNonce - nOldNonce;
478 // Check if something found
481 if (hash <= hashTarget)
484 pblock->nNonce = nNonce;
485 assert(hash == pblock->GetHash());
487 SetThreadPriority(THREAD_PRIORITY_NORMAL);
488 LogPrintf("BitcoinMiner:\n");
489 LogPrintf("proof-of-work found \n hash: %s \ntarget: %s\n", hash.GetHex(), hashTarget.GetHex());
490 ProcessBlockFound(pblock, *pwallet, reservekey);
491 SetThreadPriority(THREAD_PRIORITY_LOWEST);
493 // In regression test mode, stop mining after a block is found.
494 if (Params().MineBlocksOnDemand())
495 throw boost::thread_interrupted();
502 static int64_t nHashCounter;
503 if (nHPSTimerStart == 0)
505 nHPSTimerStart = GetTimeMillis();
509 nHashCounter += nHashesDone;
510 if (GetTimeMillis() - nHPSTimerStart > 4000)
512 static CCriticalSection cs;
515 if (GetTimeMillis() - nHPSTimerStart > 4000)
517 dHashesPerSec = 1000.0 * nHashCounter / (GetTimeMillis() - nHPSTimerStart);
518 nHPSTimerStart = GetTimeMillis();
520 static int64_t nLogTime;
521 if (GetTime() - nLogTime > 30 * 60)
523 nLogTime = GetTime();
524 LogPrintf("hashmeter %6.0f khash/s\n", dHashesPerSec/1000.0);
530 // Check for stop or if block needs to be rebuilt
531 boost::this_thread::interruption_point();
532 // Regtest mode doesn't require peers
533 if (vNodes.empty() && Params().MiningRequiresPeers())
535 if (nNonce >= 0xffff0000)
537 if (mempool.GetTransactionsUpdated() != nTransactionsUpdatedLast && GetTime() - nStart > 60)
539 if (pindexPrev != chainActive.Tip())
542 // Update nTime every few seconds
543 UpdateTime(pblock, pindexPrev);
544 if (Params().AllowMinDifficultyBlocks())
546 // Changing pblock->nTime can change work required on testnet:
547 hashTarget.SetCompact(pblock->nBits);
552 catch (boost::thread_interrupted)
554 LogPrintf("BitcoinMiner terminated\n");
559 void GenerateBitcoins(bool fGenerate, CWallet* pwallet, int nThreads)
561 static boost::thread_group* minerThreads = NULL;
564 // In regtest threads defaults to 1
565 if (Params().DefaultMinerThreads())
566 nThreads = Params().DefaultMinerThreads();
568 nThreads = boost::thread::hardware_concurrency();
571 if (minerThreads != NULL)
573 minerThreads->interrupt_all();
578 if (nThreads == 0 || !fGenerate)
581 minerThreads = new boost::thread_group();
582 for (int i = 0; i < nThreads; i++)
583 minerThreads->create_thread(boost::bind(&BitcoinMiner, pwallet));
586 #endif // ENABLE_WALLET