Merge pull request #6599
[bitcoinplatinum.git] / src / miner.cpp
blob9dd1d459b5cf082fea4d102a681f332370b217f2
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2014 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.
6 #include "miner.h"
8 #include "amount.h"
9 #include "chain.h"
10 #include "chainparams.h"
11 #include "coins.h"
12 #include "consensus/consensus.h"
13 #include "consensus/validation.h"
14 #include "hash.h"
15 #include "main.h"
16 #include "net.h"
17 #include "policy/policy.h"
18 #include "pow.h"
19 #include "primitives/transaction.h"
20 #include "script/standard.h"
21 #include "timedata.h"
22 #include "txmempool.h"
23 #include "util.h"
24 #include "utilmoneystr.h"
25 #include "validationinterface.h"
27 #include <boost/thread.hpp>
28 #include <boost/tuple/tuple.hpp>
30 using namespace std;
32 //////////////////////////////////////////////////////////////////////////////
34 // BitcoinMiner
38 // Unconfirmed transactions in the memory pool often depend on other
39 // transactions in the memory pool. When we select transactions from the
40 // pool, we select by highest priority or fee rate, so we might consider
41 // transactions that depend on transactions that aren't yet in the block.
42 // The COrphan class keeps track of these 'temporary orphans' while
43 // CreateBlock is figuring out which transactions to include.
45 class COrphan
47 public:
48 const CTransaction* ptx;
49 set<uint256> setDependsOn;
50 CFeeRate feeRate;
51 double dPriority;
53 COrphan(const CTransaction* ptxIn) : ptx(ptxIn), feeRate(0), dPriority(0)
58 uint64_t nLastBlockTx = 0;
59 uint64_t nLastBlockSize = 0;
61 // We want to sort transactions by priority and fee rate, so:
62 typedef boost::tuple<double, CFeeRate, const CTransaction*> TxPriority;
63 class TxPriorityCompare
65 bool byFee;
67 public:
68 TxPriorityCompare(bool _byFee) : byFee(_byFee) { }
70 bool operator()(const TxPriority& a, const TxPriority& b)
72 if (byFee)
74 if (a.get<1>() == b.get<1>())
75 return a.get<0>() < b.get<0>();
76 return a.get<1>() < b.get<1>();
78 else
80 if (a.get<0>() == b.get<0>())
81 return a.get<1>() < b.get<1>();
82 return a.get<0>() < b.get<0>();
87 int64_t UpdateTime(CBlockHeader* pblock, const Consensus::Params& consensusParams, const CBlockIndex* pindexPrev)
89 int64_t nOldTime = pblock->nTime;
90 int64_t nNewTime = std::max(pindexPrev->GetMedianTimePast()+1, GetAdjustedTime());
92 if (nOldTime < nNewTime)
93 pblock->nTime = nNewTime;
95 // Updating time can change work required on testnet:
96 if (consensusParams.fPowAllowMinDifficultyBlocks)
97 pblock->nBits = GetNextWorkRequired(pindexPrev, pblock, consensusParams);
99 return nNewTime - nOldTime;
102 CBlockTemplate* CreateNewBlock(const CScript& scriptPubKeyIn)
104 const CChainParams& chainparams = Params();
105 // Create new block
106 auto_ptr<CBlockTemplate> pblocktemplate(new CBlockTemplate());
107 if(!pblocktemplate.get())
108 return NULL;
109 CBlock *pblock = &pblocktemplate->block; // pointer for convenience
111 // -regtest only: allow overriding block.nVersion with
112 // -blockversion=N to test forking scenarios
113 if (Params().MineBlocksOnDemand())
114 pblock->nVersion = GetArg("-blockversion", pblock->nVersion);
116 // Create coinbase tx
117 CMutableTransaction txNew;
118 txNew.vin.resize(1);
119 txNew.vin[0].prevout.SetNull();
120 txNew.vout.resize(1);
121 txNew.vout[0].scriptPubKey = scriptPubKeyIn;
123 // Add dummy coinbase tx as first transaction
124 pblock->vtx.push_back(CTransaction());
125 pblocktemplate->vTxFees.push_back(-1); // updated at end
126 pblocktemplate->vTxSigOps.push_back(-1); // updated at end
128 // Largest block you're willing to create:
129 unsigned int nBlockMaxSize = GetArg("-blockmaxsize", DEFAULT_BLOCK_MAX_SIZE);
130 // Limit to betweeen 1K and MAX_BLOCK_SIZE-1K for sanity:
131 nBlockMaxSize = std::max((unsigned int)1000, std::min((unsigned int)(MAX_BLOCK_SIZE-1000), nBlockMaxSize));
133 // How much of the block should be dedicated to high-priority transactions,
134 // included regardless of the fees they pay
135 unsigned int nBlockPrioritySize = GetArg("-blockprioritysize", DEFAULT_BLOCK_PRIORITY_SIZE);
136 nBlockPrioritySize = std::min(nBlockMaxSize, nBlockPrioritySize);
138 // Minimum block size you want to create; block will be filled with free transactions
139 // until there are no more or the block reaches this size:
140 unsigned int nBlockMinSize = GetArg("-blockminsize", DEFAULT_BLOCK_MIN_SIZE);
141 nBlockMinSize = std::min(nBlockMaxSize, nBlockMinSize);
143 // Collect memory pool transactions into the block
144 CAmount nFees = 0;
147 LOCK2(cs_main, mempool.cs);
148 CBlockIndex* pindexPrev = chainActive.Tip();
149 const int nHeight = pindexPrev->nHeight + 1;
150 pblock->nTime = GetAdjustedTime();
151 CCoinsViewCache view(pcoinsTip);
153 // Priority order to process transactions
154 list<COrphan> vOrphan; // list memory doesn't move
155 map<uint256, vector<COrphan*> > mapDependers;
156 bool fPrintPriority = GetBoolArg("-printpriority", false);
158 // This vector will be sorted into a priority queue:
159 vector<TxPriority> vecPriority;
160 vecPriority.reserve(mempool.mapTx.size());
161 for (map<uint256, CTxMemPoolEntry>::iterator mi = mempool.mapTx.begin();
162 mi != mempool.mapTx.end(); ++mi)
164 const CTransaction& tx = mi->second.GetTx();
165 if (tx.IsCoinBase() || !IsFinalTx(tx, nHeight, pblock->nTime))
166 continue;
168 COrphan* porphan = NULL;
169 double dPriority = 0;
170 CAmount nTotalIn = 0;
171 bool fMissingInputs = false;
172 BOOST_FOREACH(const CTxIn& txin, tx.vin)
174 // Read prev transaction
175 if (!view.HaveCoins(txin.prevout.hash))
177 // This should never happen; all transactions in the memory
178 // pool should connect to either transactions in the chain
179 // or other transactions in the memory pool.
180 if (!mempool.mapTx.count(txin.prevout.hash))
182 LogPrintf("ERROR: mempool transaction missing input\n");
183 if (fDebug) assert("mempool transaction missing input" == 0);
184 fMissingInputs = true;
185 if (porphan)
186 vOrphan.pop_back();
187 break;
190 // Has to wait for dependencies
191 if (!porphan)
193 // Use list for automatic deletion
194 vOrphan.push_back(COrphan(&tx));
195 porphan = &vOrphan.back();
197 mapDependers[txin.prevout.hash].push_back(porphan);
198 porphan->setDependsOn.insert(txin.prevout.hash);
199 nTotalIn += mempool.mapTx[txin.prevout.hash].GetTx().vout[txin.prevout.n].nValue;
200 continue;
202 const CCoins* coins = view.AccessCoins(txin.prevout.hash);
203 assert(coins);
205 CAmount nValueIn = coins->vout[txin.prevout.n].nValue;
206 nTotalIn += nValueIn;
208 int nConf = nHeight - coins->nHeight;
210 dPriority += (double)nValueIn * nConf;
212 if (fMissingInputs) continue;
214 // Priority is sum(valuein * age) / modified_txsize
215 unsigned int nTxSize = ::GetSerializeSize(tx, SER_NETWORK, PROTOCOL_VERSION);
216 dPriority = tx.ComputePriority(dPriority, nTxSize);
218 uint256 hash = tx.GetHash();
219 mempool.ApplyDeltas(hash, dPriority, nTotalIn);
221 CFeeRate feeRate(nTotalIn-tx.GetValueOut(), nTxSize);
223 if (porphan)
225 porphan->dPriority = dPriority;
226 porphan->feeRate = feeRate;
228 else
229 vecPriority.push_back(TxPriority(dPriority, feeRate, &mi->second.GetTx()));
232 // Collect transactions into block
233 uint64_t nBlockSize = 1000;
234 uint64_t nBlockTx = 0;
235 int nBlockSigOps = 100;
236 bool fSortedByFee = (nBlockPrioritySize <= 0);
238 TxPriorityCompare comparer(fSortedByFee);
239 std::make_heap(vecPriority.begin(), vecPriority.end(), comparer);
241 while (!vecPriority.empty())
243 // Take highest priority transaction off the priority queue:
244 double dPriority = vecPriority.front().get<0>();
245 CFeeRate feeRate = vecPriority.front().get<1>();
246 const CTransaction& tx = *(vecPriority.front().get<2>());
248 std::pop_heap(vecPriority.begin(), vecPriority.end(), comparer);
249 vecPriority.pop_back();
251 // Size limits
252 unsigned int nTxSize = ::GetSerializeSize(tx, SER_NETWORK, PROTOCOL_VERSION);
253 if (nBlockSize + nTxSize >= nBlockMaxSize)
254 continue;
256 // Legacy limits on sigOps:
257 unsigned int nTxSigOps = GetLegacySigOpCount(tx);
258 if (nBlockSigOps + nTxSigOps >= MAX_BLOCK_SIGOPS)
259 continue;
261 // Skip free transactions if we're past the minimum block size:
262 const uint256& hash = tx.GetHash();
263 double dPriorityDelta = 0;
264 CAmount nFeeDelta = 0;
265 mempool.ApplyDeltas(hash, dPriorityDelta, nFeeDelta);
266 if (fSortedByFee && (dPriorityDelta <= 0) && (nFeeDelta <= 0) && (feeRate < ::minRelayTxFee) && (nBlockSize + nTxSize >= nBlockMinSize))
267 continue;
269 // Prioritise by fee once past the priority size or we run out of high-priority
270 // transactions:
271 if (!fSortedByFee &&
272 ((nBlockSize + nTxSize >= nBlockPrioritySize) || !AllowFree(dPriority)))
274 fSortedByFee = true;
275 comparer = TxPriorityCompare(fSortedByFee);
276 std::make_heap(vecPriority.begin(), vecPriority.end(), comparer);
279 if (!view.HaveInputs(tx))
280 continue;
282 CAmount nTxFees = view.GetValueIn(tx)-tx.GetValueOut();
284 nTxSigOps += GetP2SHSigOpCount(tx, view);
285 if (nBlockSigOps + nTxSigOps >= MAX_BLOCK_SIGOPS)
286 continue;
288 // Note that flags: we don't want to set mempool/IsStandard()
289 // policy here, but we still have to ensure that the block we
290 // create only contains transactions that are valid in new blocks.
291 CValidationState state;
292 if (!CheckInputs(tx, state, view, true, MANDATORY_SCRIPT_VERIFY_FLAGS, true))
293 continue;
295 UpdateCoins(tx, state, view, nHeight);
297 // Added
298 pblock->vtx.push_back(tx);
299 pblocktemplate->vTxFees.push_back(nTxFees);
300 pblocktemplate->vTxSigOps.push_back(nTxSigOps);
301 nBlockSize += nTxSize;
302 ++nBlockTx;
303 nBlockSigOps += nTxSigOps;
304 nFees += nTxFees;
306 if (fPrintPriority)
308 LogPrintf("priority %.1f fee %s txid %s\n",
309 dPriority, feeRate.ToString(), tx.GetHash().ToString());
312 // Add transactions that depend on this one to the priority queue
313 if (mapDependers.count(hash))
315 BOOST_FOREACH(COrphan* porphan, mapDependers[hash])
317 if (!porphan->setDependsOn.empty())
319 porphan->setDependsOn.erase(hash);
320 if (porphan->setDependsOn.empty())
322 vecPriority.push_back(TxPriority(porphan->dPriority, porphan->feeRate, porphan->ptx));
323 std::push_heap(vecPriority.begin(), vecPriority.end(), comparer);
330 nLastBlockTx = nBlockTx;
331 nLastBlockSize = nBlockSize;
332 LogPrintf("CreateNewBlock(): total size %u\n", nBlockSize);
334 // Compute final coinbase transaction.
335 txNew.vout[0].nValue = nFees + GetBlockSubsidy(nHeight, chainparams.GetConsensus());
336 txNew.vin[0].scriptSig = CScript() << nHeight << OP_0;
337 pblock->vtx[0] = txNew;
338 pblocktemplate->vTxFees[0] = -nFees;
340 // Fill in header
341 pblock->hashPrevBlock = pindexPrev->GetBlockHash();
342 UpdateTime(pblock, Params().GetConsensus(), pindexPrev);
343 pblock->nBits = GetNextWorkRequired(pindexPrev, pblock, Params().GetConsensus());
344 pblock->nNonce = 0;
345 pblocktemplate->vTxSigOps[0] = GetLegacySigOpCount(pblock->vtx[0]);
347 CValidationState state;
348 if (!TestBlockValidity(state, *pblock, pindexPrev, false, false))
349 throw std::runtime_error("CreateNewBlock(): TestBlockValidity failed");
352 return pblocktemplate.release();
355 void IncrementExtraNonce(CBlock* pblock, const CBlockIndex* pindexPrev, unsigned int& nExtraNonce)
357 // Update nExtraNonce
358 static uint256 hashPrevBlock;
359 if (hashPrevBlock != pblock->hashPrevBlock)
361 nExtraNonce = 0;
362 hashPrevBlock = pblock->hashPrevBlock;
364 ++nExtraNonce;
365 unsigned int nHeight = pindexPrev->nHeight+1; // Height first in coinbase required for block.version=2
366 CMutableTransaction txCoinbase(pblock->vtx[0]);
367 txCoinbase.vin[0].scriptSig = (CScript() << nHeight << CScriptNum(nExtraNonce)) + COINBASE_FLAGS;
368 assert(txCoinbase.vin[0].scriptSig.size() <= 100);
370 pblock->vtx[0] = txCoinbase;
371 pblock->hashMerkleRoot = pblock->BuildMerkleTree();
374 //////////////////////////////////////////////////////////////////////////////
376 // Internal miner
380 // ScanHash scans nonces looking for a hash with at least some zero bits.
381 // The nonce is usually preserved between calls, but periodically or if the
382 // nonce is 0xffff0000 or above, the block is rebuilt and nNonce starts over at
383 // zero.
385 bool static ScanHash(const CBlockHeader *pblock, uint32_t& nNonce, uint256 *phash)
387 // Write the first 76 bytes of the block header to a double-SHA256 state.
388 CHash256 hasher;
389 CDataStream ss(SER_NETWORK, PROTOCOL_VERSION);
390 ss << *pblock;
391 assert(ss.size() == 80);
392 hasher.Write((unsigned char*)&ss[0], 76);
394 while (true) {
395 nNonce++;
397 // Write the last 4 bytes of the block header (the nonce) to a copy of
398 // the double-SHA256 state, and compute the result.
399 CHash256(hasher).Write((unsigned char*)&nNonce, 4).Finalize((unsigned char*)phash);
401 // Return the nonce if the hash has at least some zero bits,
402 // caller will check if it has enough to reach the target
403 if (((uint16_t*)phash)[15] == 0)
404 return true;
406 // If nothing found after trying for a while, return -1
407 if ((nNonce & 0xfff) == 0)
408 return false;
412 static bool ProcessBlockFound(const CBlock* pblock, const CChainParams& chainparams)
414 LogPrintf("%s\n", pblock->ToString());
415 LogPrintf("generated %s\n", FormatMoney(pblock->vtx[0].vout[0].nValue));
417 // Found a solution
419 LOCK(cs_main);
420 if (pblock->hashPrevBlock != chainActive.Tip()->GetBlockHash())
421 return error("BitcoinMiner: generated block is stale");
424 // Inform about the new block
425 GetMainSignals().BlockFound(pblock->GetHash());
427 // Process this block the same as if we had received it from another node
428 CValidationState state;
429 if (!ProcessNewBlock(state, NULL, pblock, true, NULL))
430 return error("BitcoinMiner: ProcessNewBlock, block not accepted");
432 return true;
435 void static BitcoinMiner(const CChainParams& chainparams)
437 LogPrintf("BitcoinMiner started\n");
438 SetThreadPriority(THREAD_PRIORITY_LOWEST);
439 RenameThread("bitcoin-miner");
441 unsigned int nExtraNonce = 0;
443 boost::shared_ptr<CReserveScript> coinbaseScript;
444 GetMainSignals().ScriptForMining(coinbaseScript);
446 try {
447 // Throw an error if no script was provided. This can happen
448 // due to some internal error but also if the keypool is empty.
449 // In the latter case, already the pointer is NULL.
450 if (!coinbaseScript || coinbaseScript->reserveScript.empty())
451 throw std::runtime_error("No coinbase script available (mining requires a wallet)");
453 while (true) {
454 if (chainparams.MiningRequiresPeers()) {
455 // Busy-wait for the network to come online so we don't waste time mining
456 // on an obsolete chain. In regtest mode we expect to fly solo.
457 do {
458 bool fvNodesEmpty;
460 LOCK(cs_vNodes);
461 fvNodesEmpty = vNodes.empty();
463 if (!fvNodesEmpty && !IsInitialBlockDownload())
464 break;
465 MilliSleep(1000);
466 } while (true);
470 // Create new block
472 unsigned int nTransactionsUpdatedLast = mempool.GetTransactionsUpdated();
473 CBlockIndex* pindexPrev = chainActive.Tip();
475 auto_ptr<CBlockTemplate> pblocktemplate(CreateNewBlock(coinbaseScript->reserveScript));
476 if (!pblocktemplate.get())
478 LogPrintf("Error in BitcoinMiner: Keypool ran out, please call keypoolrefill before restarting the mining thread\n");
479 return;
481 CBlock *pblock = &pblocktemplate->block;
482 IncrementExtraNonce(pblock, pindexPrev, nExtraNonce);
484 LogPrintf("Running BitcoinMiner with %u transactions in block (%u bytes)\n", pblock->vtx.size(),
485 ::GetSerializeSize(*pblock, SER_NETWORK, PROTOCOL_VERSION));
488 // Search
490 int64_t nStart = GetTime();
491 arith_uint256 hashTarget = arith_uint256().SetCompact(pblock->nBits);
492 uint256 hash;
493 uint32_t nNonce = 0;
494 while (true) {
495 // Check if something found
496 if (ScanHash(pblock, nNonce, &hash))
498 if (UintToArith256(hash) <= hashTarget)
500 // Found a solution
501 pblock->nNonce = nNonce;
502 assert(hash == pblock->GetHash());
504 SetThreadPriority(THREAD_PRIORITY_NORMAL);
505 LogPrintf("BitcoinMiner:\n");
506 LogPrintf("proof-of-work found \n hash: %s \ntarget: %s\n", hash.GetHex(), hashTarget.GetHex());
507 ProcessBlockFound(pblock, chainparams);
508 SetThreadPriority(THREAD_PRIORITY_LOWEST);
509 coinbaseScript->KeepScript();
511 // In regression test mode, stop mining after a block is found.
512 if (chainparams.MineBlocksOnDemand())
513 throw boost::thread_interrupted();
515 break;
519 // Check for stop or if block needs to be rebuilt
520 boost::this_thread::interruption_point();
521 // Regtest mode doesn't require peers
522 if (vNodes.empty() && chainparams.MiningRequiresPeers())
523 break;
524 if (nNonce >= 0xffff0000)
525 break;
526 if (mempool.GetTransactionsUpdated() != nTransactionsUpdatedLast && GetTime() - nStart > 60)
527 break;
528 if (pindexPrev != chainActive.Tip())
529 break;
531 // Update nTime every few seconds
532 if (UpdateTime(pblock, chainparams.GetConsensus(), pindexPrev) < 0)
533 break; // Recreate the block if the clock has run backwards,
534 // so that we can use the correct time.
535 if (chainparams.GetConsensus().fPowAllowMinDifficultyBlocks)
537 // Changing pblock->nTime can change work required on testnet:
538 hashTarget.SetCompact(pblock->nBits);
543 catch (const boost::thread_interrupted&)
545 LogPrintf("BitcoinMiner terminated\n");
546 throw;
548 catch (const std::runtime_error &e)
550 LogPrintf("BitcoinMiner runtime error: %s\n", e.what());
551 return;
555 void GenerateBitcoins(bool fGenerate, int nThreads, const CChainParams& chainparams)
557 static boost::thread_group* minerThreads = NULL;
559 if (nThreads < 0)
560 nThreads = GetNumCores();
562 if (minerThreads != NULL)
564 minerThreads->interrupt_all();
565 delete minerThreads;
566 minerThreads = NULL;
569 if (nThreads == 0 || !fGenerate)
570 return;
572 minerThreads = new boost::thread_group();
573 for (int i = 0; i < nThreads; i++)
574 minerThreads->create_thread(boost::bind(&BitcoinMiner, boost::cref(chainparams)));