]> Git Repo - VerusCoin.git/blob - src/miner.cpp
checkpoints.cpp depends on main, it can use mapBlockIndex directly
[VerusCoin.git] / src / miner.cpp
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.
5
6 #include "miner.h"
7
8 #include "core.h"
9 #include "hash.h"
10 #include "main.h"
11 #include "net.h"
12 #include "pow.h"
13 #include "util.h"
14 #include "utilmoneystr.h"
15 #ifdef ENABLE_WALLET
16 #include "wallet.h"
17 #endif
18
19 #include <boost/thread.hpp>
20
21 using namespace std;
22
23 //////////////////////////////////////////////////////////////////////////////
24 //
25 // BitcoinMiner
26 //
27
28 //
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.
35 //
36 class COrphan
37 {
38 public:
39     const CTransaction* ptx;
40     set<uint256> setDependsOn;
41     CFeeRate feeRate;
42     double dPriority;
43
44     COrphan(const CTransaction* ptxIn) : ptx(ptxIn), feeRate(0), dPriority(0)
45     {
46     }
47 };
48
49 uint64_t nLastBlockTx = 0;
50 uint64_t nLastBlockSize = 0;
51
52 // We want to sort transactions by priority and fee rate, so:
53 typedef boost::tuple<double, CFeeRate, const CTransaction*> TxPriority;
54 class TxPriorityCompare
55 {
56     bool byFee;
57
58 public:
59     TxPriorityCompare(bool _byFee) : byFee(_byFee) { }
60
61     bool operator()(const TxPriority& a, const TxPriority& b)
62     {
63         if (byFee)
64         {
65             if (a.get<1>() == b.get<1>())
66                 return a.get<0>() < b.get<0>();
67             return a.get<1>() < b.get<1>();
68         }
69         else
70         {
71             if (a.get<0>() == b.get<0>())
72                 return a.get<1>() < b.get<1>();
73             return a.get<0>() < b.get<0>();
74         }
75     }
76 };
77
78 CBlockTemplate* CreateNewBlock(const CScript& scriptPubKeyIn)
79 {
80     // Create new block
81     auto_ptr<CBlockTemplate> pblocktemplate(new CBlockTemplate());
82     if(!pblocktemplate.get())
83         return NULL;
84     CBlock *pblock = &pblocktemplate->block; // pointer for convenience
85
86     // Create coinbase tx
87     CMutableTransaction txNew;
88     txNew.vin.resize(1);
89     txNew.vin[0].prevout.SetNull();
90     txNew.vout.resize(1);
91     txNew.vout[0].scriptPubKey = scriptPubKeyIn;
92
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
97
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));
102
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);
107
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);
112
113     // Collect memory pool transactions into the block
114     int64_t nFees = 0;
115
116     {
117         LOCK2(cs_main, mempool.cs);
118         CBlockIndex* pindexPrev = chainActive.Tip();
119         CCoinsViewCache view(*pcoinsTip, true);
120
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);
125
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)
131         {
132             const CTransaction& tx = mi->second.GetTx();
133             if (tx.IsCoinBase() || !IsFinalTx(tx, pindexPrev->nHeight + 1))
134                 continue;
135
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)
141             {
142                 // Read prev transaction
143                 if (!view.HaveCoins(txin.prevout.hash))
144                 {
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))
149                     {
150                         LogPrintf("ERROR: mempool transaction missing input\n");
151                         if (fDebug) assert("mempool transaction missing input" == 0);
152                         fMissingInputs = true;
153                         if (porphan)
154                             vOrphan.pop_back();
155                         break;
156                     }
157
158                     // Has to wait for dependencies
159                     if (!porphan)
160                     {
161                         // Use list for automatic deletion
162                         vOrphan.push_back(COrphan(&tx));
163                         porphan = &vOrphan.back();
164                     }
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;
168                     continue;
169                 }
170                 const CCoins &coins = view.GetCoins(txin.prevout.hash);
171
172                 int64_t nValueIn = coins.vout[txin.prevout.n].nValue;
173                 nTotalIn += nValueIn;
174
175                 int nConf = pindexPrev->nHeight - coins.nHeight + 1;
176
177                 dPriority += (double)nValueIn * nConf;
178             }
179             if (fMissingInputs) continue;
180
181             // Priority is sum(valuein * age) / modified_txsize
182             unsigned int nTxSize = ::GetSerializeSize(tx, SER_NETWORK, PROTOCOL_VERSION);
183             dPriority = tx.ComputePriority(dPriority, nTxSize);
184
185             uint256 hash = tx.GetHash();
186             mempool.ApplyDeltas(hash, dPriority, nTotalIn);
187
188             CFeeRate feeRate(nTotalIn-tx.GetValueOut(), nTxSize);
189
190             if (porphan)
191             {
192                 porphan->dPriority = dPriority;
193                 porphan->feeRate = feeRate;
194             }
195             else
196                 vecPriority.push_back(TxPriority(dPriority, feeRate, &mi->second.GetTx()));
197         }
198
199         // Collect transactions into block
200         uint64_t nBlockSize = 1000;
201         uint64_t nBlockTx = 0;
202         int nBlockSigOps = 100;
203         bool fSortedByFee = (nBlockPrioritySize <= 0);
204
205         TxPriorityCompare comparer(fSortedByFee);
206         std::make_heap(vecPriority.begin(), vecPriority.end(), comparer);
207
208         while (!vecPriority.empty())
209         {
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>());
214
215             std::pop_heap(vecPriority.begin(), vecPriority.end(), comparer);
216             vecPriority.pop_back();
217
218             // Size limits
219             unsigned int nTxSize = ::GetSerializeSize(tx, SER_NETWORK, PROTOCOL_VERSION);
220             if (nBlockSize + nTxSize >= nBlockMaxSize)
221                 continue;
222
223             // Legacy limits on sigOps:
224             unsigned int nTxSigOps = GetLegacySigOpCount(tx);
225             if (nBlockSigOps + nTxSigOps >= MAX_BLOCK_SIGOPS)
226                 continue;
227
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))
234                 continue;
235
236             // Prioritise by fee once past the priority size or we run out of high-priority
237             // transactions:
238             if (!fSortedByFee &&
239                 ((nBlockSize + nTxSize >= nBlockPrioritySize) || !AllowFree(dPriority)))
240             {
241                 fSortedByFee = true;
242                 comparer = TxPriorityCompare(fSortedByFee);
243                 std::make_heap(vecPriority.begin(), vecPriority.end(), comparer);
244             }
245
246             if (!view.HaveInputs(tx))
247                 continue;
248
249             int64_t nTxFees = view.GetValueIn(tx)-tx.GetValueOut();
250
251             nTxSigOps += GetP2SHSigOpCount(tx, view);
252             if (nBlockSigOps + nTxSigOps >= MAX_BLOCK_SIGOPS)
253                 continue;
254
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))
260                 continue;
261
262             CTxUndo txundo;
263             UpdateCoins(tx, state, view, txundo, pindexPrev->nHeight+1);
264
265             // Added
266             pblock->vtx.push_back(tx);
267             pblocktemplate->vTxFees.push_back(nTxFees);
268             pblocktemplate->vTxSigOps.push_back(nTxSigOps);
269             nBlockSize += nTxSize;
270             ++nBlockTx;
271             nBlockSigOps += nTxSigOps;
272             nFees += nTxFees;
273
274             if (fPrintPriority)
275             {
276                 LogPrintf("priority %.1f fee %s txid %s\n",
277                     dPriority, feeRate.ToString(), tx.GetHash().ToString());
278             }
279
280             // Add transactions that depend on this one to the priority queue
281             if (mapDependers.count(hash))
282             {
283                 BOOST_FOREACH(COrphan* porphan, mapDependers[hash])
284                 {
285                     if (!porphan->setDependsOn.empty())
286                     {
287                         porphan->setDependsOn.erase(hash);
288                         if (porphan->setDependsOn.empty())
289                         {
290                             vecPriority.push_back(TxPriority(porphan->dPriority, porphan->feeRate, porphan->ptx));
291                             std::push_heap(vecPriority.begin(), vecPriority.end(), comparer);
292                         }
293                     }
294                 }
295             }
296         }
297
298         nLastBlockTx = nBlockTx;
299         nLastBlockSize = nBlockSize;
300         LogPrintf("CreateNewBlock(): total size %u\n", nBlockSize);
301
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;
307
308         // Fill in header
309         pblock->hashPrevBlock  = pindexPrev->GetBlockHash();
310         UpdateTime(pblock, pindexPrev);
311         pblock->nBits          = GetNextWorkRequired(pindexPrev, pblock);
312         pblock->nNonce         = 0;
313         pblocktemplate->vTxSigOps[0] = GetLegacySigOpCount(pblock->vtx[0]);
314
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");
322     }
323
324     return pblocktemplate.release();
325 }
326
327 void IncrementExtraNonce(CBlock* pblock, CBlockIndex* pindexPrev, unsigned int& nExtraNonce)
328 {
329     // Update nExtraNonce
330     static uint256 hashPrevBlock;
331     if (hashPrevBlock != pblock->hashPrevBlock)
332     {
333         nExtraNonce = 0;
334         hashPrevBlock = pblock->hashPrevBlock;
335     }
336     ++nExtraNonce;
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);
341
342     pblock->vtx[0] = txCoinbase;
343     pblock->hashMerkleRoot = pblock->BuildMerkleTree();
344 }
345
346 #ifdef ENABLE_WALLET
347 //////////////////////////////////////////////////////////////////////////////
348 //
349 // Internal miner
350 //
351 double dHashesPerSec = 0.0;
352 int64_t nHPSTimerStart = 0;
353
354 //
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
358 // zero.
359 //
360 bool static ScanHash(const CBlockHeader *pblock, uint32_t& nNonce, uint256 *phash)
361 {
362     // Write the first 76 bytes of the block header to a double-SHA256 state.
363     CHash256 hasher;
364     CDataStream ss(SER_NETWORK, PROTOCOL_VERSION);
365     ss << *pblock;
366     assert(ss.size() == 80);
367     hasher.Write((unsigned char*)&ss[0], 76);
368
369     while (true) {
370         nNonce++;
371
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);
375
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)
379             return true;
380
381         // If nothing found after trying for a while, return -1
382         if ((nNonce & 0xffff) == 0)
383             return false;
384         if ((nNonce & 0xfff) == 0)
385             boost::this_thread::interruption_point();
386     }
387 }
388
389 CBlockTemplate* CreateNewBlockWithKey(CReserveKey& reservekey)
390 {
391     CPubKey pubkey;
392     if (!reservekey.GetReservedKey(pubkey))
393         return NULL;
394
395     CScript scriptPubKey = CScript() << pubkey << OP_CHECKSIG;
396     return CreateNewBlock(scriptPubKey);
397 }
398
399 bool ProcessBlockFound(CBlock* pblock, CWallet& wallet, CReserveKey& reservekey)
400 {
401     LogPrintf("%s\n", pblock->ToString());
402     LogPrintf("generated %s\n", FormatMoney(pblock->vtx[0].vout[0].nValue));
403
404     // Found a solution
405     {
406         LOCK(cs_main);
407         if (pblock->hashPrevBlock != chainActive.Tip()->GetBlockHash())
408             return error("BitcoinMiner : generated block is stale");
409     }
410
411     // Remove key from key pool
412     reservekey.KeepKey();
413
414     // Track how many getdata requests this block gets
415     {
416         LOCK(wallet.cs_wallet);
417         wallet.mapRequestCount[pblock->GetHash()] = 0;
418     }
419
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");
424
425     return true;
426 }
427
428 void static BitcoinMiner(CWallet *pwallet)
429 {
430     LogPrintf("BitcoinMiner started\n");
431     SetThreadPriority(THREAD_PRIORITY_LOWEST);
432     RenameThread("bitcoin-miner");
433
434     // Each thread has its own key and counter
435     CReserveKey reservekey(pwallet);
436     unsigned int nExtraNonce = 0;
437
438     try {
439         while (true) {
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())
444                     MilliSleep(1000);
445             }
446
447             //
448             // Create new block
449             //
450             unsigned int nTransactionsUpdatedLast = mempool.GetTransactionsUpdated();
451             CBlockIndex* pindexPrev = chainActive.Tip();
452
453             auto_ptr<CBlockTemplate> pblocktemplate(CreateNewBlockWithKey(reservekey));
454             if (!pblocktemplate.get())
455             {
456                 LogPrintf("Error in BitcoinMiner: Keypool ran out, please call keypoolrefill before restarting the mining thread\n");
457                 return;
458             }
459             CBlock *pblock = &pblocktemplate->block;
460             IncrementExtraNonce(pblock, pindexPrev, nExtraNonce);
461
462             LogPrintf("Running BitcoinMiner with %u transactions in block (%u bytes)\n", pblock->vtx.size(),
463                 ::GetSerializeSize(*pblock, SER_NETWORK, PROTOCOL_VERSION));
464
465             //
466             // Search
467             //
468             int64_t nStart = GetTime();
469             uint256 hashTarget = uint256().SetCompact(pblock->nBits);
470             uint256 hash;
471             uint32_t nNonce = 0;
472             uint32_t nOldNonce = 0;
473             while (true) {
474                 bool fFound = ScanHash(pblock, nNonce, &hash);
475                 uint32_t nHashesDone = nNonce - nOldNonce;
476                 nOldNonce = nNonce;
477
478                 // Check if something found
479                 if (fFound)
480                 {
481                     if (hash <= hashTarget)
482                     {
483                         // Found a solution
484                         pblock->nNonce = nNonce;
485                         assert(hash == pblock->GetHash());
486
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);
492
493                         // In regression test mode, stop mining after a block is found.
494                         if (Params().MineBlocksOnDemand())
495                             throw boost::thread_interrupted();
496
497                         break;
498                     }
499                 }
500
501                 // Meter hashes/sec
502                 static int64_t nHashCounter;
503                 if (nHPSTimerStart == 0)
504                 {
505                     nHPSTimerStart = GetTimeMillis();
506                     nHashCounter = 0;
507                 }
508                 else
509                     nHashCounter += nHashesDone;
510                 if (GetTimeMillis() - nHPSTimerStart > 4000)
511                 {
512                     static CCriticalSection cs;
513                     {
514                         LOCK(cs);
515                         if (GetTimeMillis() - nHPSTimerStart > 4000)
516                         {
517                             dHashesPerSec = 1000.0 * nHashCounter / (GetTimeMillis() - nHPSTimerStart);
518                             nHPSTimerStart = GetTimeMillis();
519                             nHashCounter = 0;
520                             static int64_t nLogTime;
521                             if (GetTime() - nLogTime > 30 * 60)
522                             {
523                                 nLogTime = GetTime();
524                                 LogPrintf("hashmeter %6.0f khash/s\n", dHashesPerSec/1000.0);
525                             }
526                         }
527                     }
528                 }
529
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())
534                     break;
535                 if (nNonce >= 0xffff0000)
536                     break;
537                 if (mempool.GetTransactionsUpdated() != nTransactionsUpdatedLast && GetTime() - nStart > 60)
538                     break;
539                 if (pindexPrev != chainActive.Tip())
540                     break;
541
542                 // Update nTime every few seconds
543                 UpdateTime(pblock, pindexPrev);
544                 if (Params().AllowMinDifficultyBlocks())
545                 {
546                     // Changing pblock->nTime can change work required on testnet:
547                     hashTarget.SetCompact(pblock->nBits);
548                 }
549             }
550         }
551     }
552     catch (boost::thread_interrupted)
553     {
554         LogPrintf("BitcoinMiner terminated\n");
555         throw;
556     }
557 }
558
559 void GenerateBitcoins(bool fGenerate, CWallet* pwallet, int nThreads)
560 {
561     static boost::thread_group* minerThreads = NULL;
562
563     if (nThreads < 0) {
564         // In regtest threads defaults to 1
565         if (Params().DefaultMinerThreads())
566             nThreads = Params().DefaultMinerThreads();
567         else
568             nThreads = boost::thread::hardware_concurrency();
569     }
570
571     if (minerThreads != NULL)
572     {
573         minerThreads->interrupt_all();
574         delete minerThreads;
575         minerThreads = NULL;
576     }
577
578     if (nThreads == 0 || !fGenerate)
579         return;
580
581     minerThreads = new boost::thread_group();
582     for (int i = 0; i < nThreads; i++)
583         minerThreads->create_thread(boost::bind(&BitcoinMiner, pwallet));
584 }
585
586 #endif // ENABLE_WALLET
This page took 0.056143 seconds and 4 git commands to generate.