]> Git Repo - VerusCoin.git/blob - src/miner.cpp
Chainparams: Explicit CChainParams arg for main (pre miner):
[VerusCoin.git] / src / miner.cpp
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.
5
6 #include "miner.h"
7 #ifdef ENABLE_MINING
8 #include "pow/tromp/equi_miner.h"
9 #endif
10
11 #include "amount.h"
12 #include "chainparams.h"
13 #include "consensus/consensus.h"
14 #include "consensus/upgrades.h"
15 #include "consensus/validation.h"
16 #ifdef ENABLE_MINING
17 #include "crypto/equihash.h"
18 #endif
19 #include "hash.h"
20 #include "key_io.h"
21 #include "main.h"
22 #include "metrics.h"
23 #include "net.h"
24 #include "pow.h"
25 #include "primitives/transaction.h"
26 #include "random.h"
27 #include "timedata.h"
28 #include "ui_interface.h"
29 #include "util.h"
30 #include "utilmoneystr.h"
31 #include "validationinterface.h"
32
33 #include "sodium.h"
34
35 #include <boost/thread.hpp>
36 #include <boost/tuple/tuple.hpp>
37 #ifdef ENABLE_MINING
38 #include <functional>
39 #endif
40 #include <mutex>
41
42 using namespace std;
43
44 //////////////////////////////////////////////////////////////////////////////
45 //
46 // BitcoinMiner
47 //
48
49 //
50 // Unconfirmed transactions in the memory pool often depend on other
51 // transactions in the memory pool. When we select transactions from the
52 // pool, we select by highest priority or fee rate, so we might consider
53 // transactions that depend on transactions that aren't yet in the block.
54 // The COrphan class keeps track of these 'temporary orphans' while
55 // CreateBlock is figuring out which transactions to include.
56 //
57 class COrphan
58 {
59 public:
60     const CTransaction* ptx;
61     set<uint256> setDependsOn;
62     CFeeRate feeRate;
63     double dPriority;
64
65     COrphan(const CTransaction* ptxIn) : ptx(ptxIn), feeRate(0), dPriority(0)
66     {
67     }
68 };
69
70 uint64_t nLastBlockTx = 0;
71 uint64_t nLastBlockSize = 0;
72
73 // We want to sort transactions by priority and fee rate, so:
74 typedef boost::tuple<double, CFeeRate, const CTransaction*> TxPriority;
75 class TxPriorityCompare
76 {
77     bool byFee;
78
79 public:
80     TxPriorityCompare(bool _byFee) : byFee(_byFee) { }
81
82     bool operator()(const TxPriority& a, const TxPriority& b)
83     {
84         if (byFee)
85         {
86             if (a.get<1>() == b.get<1>())
87                 return a.get<0>() < b.get<0>();
88             return a.get<1>() < b.get<1>();
89         }
90         else
91         {
92             if (a.get<0>() == b.get<0>())
93                 return a.get<1>() < b.get<1>();
94             return a.get<0>() < b.get<0>();
95         }
96     }
97 };
98
99 void UpdateTime(CBlockHeader* pblock, const Consensus::Params& consensusParams, const CBlockIndex* pindexPrev)
100 {
101     pblock->nTime = std::max(pindexPrev->GetMedianTimePast()+1, GetAdjustedTime());
102
103     // Updating time can change work required on testnet:
104     if (consensusParams.nPowAllowMinDifficultyBlocksAfterHeight != boost::none) {
105         pblock->nBits = GetNextWorkRequired(pindexPrev, pblock, consensusParams);
106     }
107 }
108
109 CBlockTemplate* CreateNewBlock(const CScript& scriptPubKeyIn)
110 {
111     const CChainParams& chainparams = Params();
112     // Create new block
113     std::unique_ptr<CBlockTemplate> pblocktemplate(new CBlockTemplate());
114     if(!pblocktemplate.get())
115         return NULL;
116     CBlock *pblock = &pblocktemplate->block; // pointer for convenience
117
118     // -regtest only: allow overriding block.nVersion with
119     // -blockversion=N to test forking scenarios
120     if (Params().MineBlocksOnDemand())
121         pblock->nVersion = GetArg("-blockversion", pblock->nVersion);
122
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
127
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));
132
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);
137
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);
142
143     // Collect memory pool transactions into the block
144     CAmount nFees = 0;
145
146     {
147         LOCK2(cs_main, mempool.cs);
148         CBlockIndex* pindexPrev = chainActive.Tip();
149         const int nHeight = pindexPrev->nHeight + 1;
150         uint32_t consensusBranchId = CurrentEpochBranchId(nHeight, chainparams.GetConsensus());
151         pblock->nTime = GetAdjustedTime();
152         const int64_t nMedianTimePast = pindexPrev->GetMedianTimePast();
153         CCoinsViewCache view(pcoinsTip);
154
155         SaplingMerkleTree sapling_tree;
156         assert(view.GetSaplingAnchorAt(view.GetBestAnchor(SAPLING), sapling_tree));
157
158         // Priority order to process transactions
159         list<COrphan> vOrphan; // list memory doesn't move
160         map<uint256, vector<COrphan*> > mapDependers;
161         bool fPrintPriority = GetBoolArg("-printpriority", false);
162
163         // This vector will be sorted into a priority queue:
164         vector<TxPriority> vecPriority;
165         vecPriority.reserve(mempool.mapTx.size());
166         for (CTxMemPool::indexed_transaction_set::iterator mi = mempool.mapTx.begin();
167              mi != mempool.mapTx.end(); ++mi)
168         {
169             const CTransaction& tx = mi->GetTx();
170
171             int64_t nLockTimeCutoff = (STANDARD_LOCKTIME_VERIFY_FLAGS & LOCKTIME_MEDIAN_TIME_PAST)
172                                     ? nMedianTimePast
173                                     : pblock->GetBlockTime();
174
175             if (tx.IsCoinBase() || !IsFinalTx(tx, nHeight, nLockTimeCutoff) || IsExpiredTx(tx, nHeight))
176                 continue;
177
178             COrphan* porphan = NULL;
179             double dPriority = 0;
180             CAmount nTotalIn = 0;
181             bool fMissingInputs = false;
182             BOOST_FOREACH(const CTxIn& txin, tx.vin)
183             {
184                 // Read prev transaction
185                 if (!view.HaveCoins(txin.prevout.hash))
186                 {
187                     // This should never happen; all transactions in the memory
188                     // pool should connect to either transactions in the chain
189                     // or other transactions in the memory pool.
190                     if (!mempool.mapTx.count(txin.prevout.hash))
191                     {
192                         LogPrintf("ERROR: mempool transaction missing input\n");
193                         if (fDebug) assert("mempool transaction missing input" == 0);
194                         fMissingInputs = true;
195                         if (porphan)
196                             vOrphan.pop_back();
197                         break;
198                     }
199
200                     // Has to wait for dependencies
201                     if (!porphan)
202                     {
203                         // Use list for automatic deletion
204                         vOrphan.push_back(COrphan(&tx));
205                         porphan = &vOrphan.back();
206                     }
207                     mapDependers[txin.prevout.hash].push_back(porphan);
208                     porphan->setDependsOn.insert(txin.prevout.hash);
209                     nTotalIn += mempool.mapTx.find(txin.prevout.hash)->GetTx().vout[txin.prevout.n].nValue;
210                     continue;
211                 }
212                 const CCoins* coins = view.AccessCoins(txin.prevout.hash);
213                 assert(coins);
214
215                 CAmount nValueIn = coins->vout[txin.prevout.n].nValue;
216                 nTotalIn += nValueIn;
217
218                 int nConf = nHeight - coins->nHeight;
219
220                 dPriority += (double)nValueIn * nConf;
221             }
222             nTotalIn += tx.GetShieldedValueIn();
223
224             if (fMissingInputs) continue;
225
226             // Priority is sum(valuein * age) / modified_txsize
227             unsigned int nTxSize = ::GetSerializeSize(tx, SER_NETWORK, PROTOCOL_VERSION);
228             dPriority = tx.ComputePriority(dPriority, nTxSize);
229
230             uint256 hash = tx.GetHash();
231             mempool.ApplyDeltas(hash, dPriority, nTotalIn);
232
233             CFeeRate feeRate(nTotalIn-tx.GetValueOut(), nTxSize);
234
235             if (porphan)
236             {
237                 porphan->dPriority = dPriority;
238                 porphan->feeRate = feeRate;
239             }
240             else
241                 vecPriority.push_back(TxPriority(dPriority, feeRate, &(mi->GetTx())));
242         }
243
244         // Collect transactions into block
245         uint64_t nBlockSize = 1000;
246         uint64_t nBlockTx = 0;
247         int nBlockSigOps = 100;
248         bool fSortedByFee = (nBlockPrioritySize <= 0);
249
250         TxPriorityCompare comparer(fSortedByFee);
251         std::make_heap(vecPriority.begin(), vecPriority.end(), comparer);
252
253         // We want to track the value pool, but if the miner gets
254         // invoked on an old block before the hardcoded fallback
255         // is active we don't want to trip up any assertions. So,
256         // we only adhere to the turnstile (as a miner) if we
257         // actually have all of the information necessary to do
258         // so.
259         CAmount sproutValue = 0;
260         CAmount saplingValue = 0;
261         bool monitoring_pool_balances = true;
262         if (chainparams.ZIP209Enabled()) {
263             if (pindexPrev->nChainSproutValue) {
264                 sproutValue = *pindexPrev->nChainSproutValue;
265             } else {
266                 monitoring_pool_balances = false;
267             }
268             if (pindexPrev->nChainSaplingValue) {
269                 saplingValue = *pindexPrev->nChainSaplingValue;
270             } else {
271                 monitoring_pool_balances = false;
272             }
273         }
274
275         while (!vecPriority.empty())
276         {
277             // Take highest priority transaction off the priority queue:
278             double dPriority = vecPriority.front().get<0>();
279             CFeeRate feeRate = vecPriority.front().get<1>();
280             const CTransaction& tx = *(vecPriority.front().get<2>());
281
282             std::pop_heap(vecPriority.begin(), vecPriority.end(), comparer);
283             vecPriority.pop_back();
284
285             // Size limits
286             unsigned int nTxSize = ::GetSerializeSize(tx, SER_NETWORK, PROTOCOL_VERSION);
287             if (nBlockSize + nTxSize >= nBlockMaxSize)
288                 continue;
289
290             // Legacy limits on sigOps:
291             unsigned int nTxSigOps = GetLegacySigOpCount(tx);
292             if (nBlockSigOps + nTxSigOps >= MAX_BLOCK_SIGOPS)
293                 continue;
294
295             // Skip free transactions if we're past the minimum block size:
296             const uint256& hash = tx.GetHash();
297             double dPriorityDelta = 0;
298             CAmount nFeeDelta = 0;
299             mempool.ApplyDeltas(hash, dPriorityDelta, nFeeDelta);
300             if (fSortedByFee && (dPriorityDelta <= 0) && (nFeeDelta <= 0) && (feeRate < ::minRelayTxFee) && (nBlockSize + nTxSize >= nBlockMinSize))
301                 continue;
302
303             // Prioritise by fee once past the priority size or we run out of high-priority
304             // transactions:
305             if (!fSortedByFee &&
306                 ((nBlockSize + nTxSize >= nBlockPrioritySize) || !AllowFree(dPriority)))
307             {
308                 fSortedByFee = true;
309                 comparer = TxPriorityCompare(fSortedByFee);
310                 std::make_heap(vecPriority.begin(), vecPriority.end(), comparer);
311             }
312
313             if (!view.HaveInputs(tx))
314                 continue;
315
316             CAmount nTxFees = view.GetValueIn(tx)-tx.GetValueOut();
317
318             nTxSigOps += GetP2SHSigOpCount(tx, view);
319             if (nBlockSigOps + nTxSigOps >= MAX_BLOCK_SIGOPS)
320                 continue;
321
322             // Note that flags: we don't want to set mempool/IsStandard()
323             // policy here, but we still have to ensure that the block we
324             // create only contains transactions that are valid in new blocks.
325             CValidationState state;
326             PrecomputedTransactionData txdata(tx);
327             if (!ContextualCheckInputs(tx, state, view, true, MANDATORY_SCRIPT_VERIFY_FLAGS, true, txdata, Params().GetConsensus(), consensusBranchId))
328                 continue;
329
330             if (chainparams.ZIP209Enabled() && monitoring_pool_balances) {
331                 // Does this transaction lead to a turnstile violation?
332
333                 CAmount sproutValueDummy = sproutValue;
334                 CAmount saplingValueDummy = saplingValue;
335
336                 saplingValueDummy += -tx.valueBalance;
337
338                 for (auto js : tx.vjoinsplit) {
339                     sproutValueDummy += js.vpub_old;
340                     sproutValueDummy -= js.vpub_new;
341                 }
342
343                 if (sproutValueDummy < 0) {
344                     LogPrintf("CreateNewBlock(): tx %s appears to violate Sprout turnstile\n", tx.GetHash().ToString());
345                     continue;
346                 }
347                 if (saplingValueDummy < 0) {
348                     LogPrintf("CreateNewBlock(): tx %s appears to violate Sapling turnstile\n", tx.GetHash().ToString());
349                     continue;
350                 }
351
352                 sproutValue = sproutValueDummy;
353                 saplingValue = saplingValueDummy;
354             }
355
356             UpdateCoins(tx, view, nHeight);
357
358             BOOST_FOREACH(const OutputDescription &outDescription, tx.vShieldedOutput) {
359                 sapling_tree.append(outDescription.cm);
360             }
361
362             // Added
363             pblock->vtx.push_back(tx);
364             pblocktemplate->vTxFees.push_back(nTxFees);
365             pblocktemplate->vTxSigOps.push_back(nTxSigOps);
366             nBlockSize += nTxSize;
367             ++nBlockTx;
368             nBlockSigOps += nTxSigOps;
369             nFees += nTxFees;
370
371             if (fPrintPriority)
372             {
373                 LogPrintf("priority %.1f fee %s txid %s\n",
374                     dPriority, feeRate.ToString(), tx.GetHash().ToString());
375             }
376
377             // Add transactions that depend on this one to the priority queue
378             if (mapDependers.count(hash))
379             {
380                 BOOST_FOREACH(COrphan* porphan, mapDependers[hash])
381                 {
382                     if (!porphan->setDependsOn.empty())
383                     {
384                         porphan->setDependsOn.erase(hash);
385                         if (porphan->setDependsOn.empty())
386                         {
387                             vecPriority.push_back(TxPriority(porphan->dPriority, porphan->feeRate, porphan->ptx));
388                             std::push_heap(vecPriority.begin(), vecPriority.end(), comparer);
389                         }
390                     }
391                 }
392             }
393         }
394
395         nLastBlockTx = nBlockTx;
396         nLastBlockSize = nBlockSize;
397         LogPrintf("CreateNewBlock(): total size %u\n", nBlockSize);
398
399         // Create coinbase tx
400         CMutableTransaction txNew = CreateNewContextualCMutableTransaction(chainparams.GetConsensus(), nHeight);
401         txNew.vin.resize(1);
402         txNew.vin[0].prevout.SetNull();
403         txNew.vout.resize(1);
404         txNew.vout[0].scriptPubKey = scriptPubKeyIn;
405         txNew.vout[0].nValue = GetBlockSubsidy(nHeight, chainparams.GetConsensus());
406         // Set to 0 so expiry height does not apply to coinbase txs
407         txNew.nExpiryHeight = 0;
408
409         if ((nHeight > 0) && (nHeight <= chainparams.GetConsensus().GetLastFoundersRewardBlockHeight())) {
410             // Founders reward is 20% of the block subsidy
411             auto vFoundersReward = txNew.vout[0].nValue / 5;
412             // Take some reward away from us
413             txNew.vout[0].nValue -= vFoundersReward;
414
415             // And give it to the founders
416             txNew.vout.push_back(CTxOut(vFoundersReward, chainparams.GetFoundersRewardScriptAtHeight(nHeight)));
417         }
418
419         // Add fees
420         txNew.vout[0].nValue += nFees;
421         txNew.vin[0].scriptSig = CScript() << nHeight << OP_0;
422
423         pblock->vtx[0] = txNew;
424         pblocktemplate->vTxFees[0] = -nFees;
425
426         // Randomise nonce
427         arith_uint256 nonce = UintToArith256(GetRandHash());
428         // Clear the top and bottom 16 bits (for local use as thread flags and counters)
429         nonce <<= 32;
430         nonce >>= 16;
431         pblock->nNonce = ArithToUint256(nonce);
432
433         // Fill in header
434         pblock->hashPrevBlock  = pindexPrev->GetBlockHash();
435         pblock->hashFinalSaplingRoot   = sapling_tree.root();
436         UpdateTime(pblock, Params().GetConsensus(), pindexPrev);
437         pblock->nBits          = GetNextWorkRequired(pindexPrev, pblock, Params().GetConsensus());
438         pblock->nSolution.clear();
439         pblocktemplate->vTxSigOps[0] = GetLegacySigOpCount(pblock->vtx[0]);
440
441         CValidationState state;
442         if (!TestBlockValidity(state, chainparams, *pblock, pindexPrev, false, false))
443             throw std::runtime_error("CreateNewBlock(): TestBlockValidity failed");
444     }
445
446     return pblocktemplate.release();
447 }
448
449 //////////////////////////////////////////////////////////////////////////////
450 //
451 // Internal miner
452 //
453
454 #ifdef ENABLE_MINING
455
456 class MinerAddressScript : public CReserveScript
457 {
458     // CReserveScript requires implementing this function, so that if an
459     // internal (not-visible) wallet address is used, the wallet can mark it as
460     // important when a block is mined (so it then appears to the user).
461     // If -mineraddress is set, the user already knows about and is managing the
462     // address, so we don't need to do anything here.
463     void KeepScript() {}
464 };
465
466 void GetScriptForMinerAddress(boost::shared_ptr<CReserveScript> &script)
467 {
468     CTxDestination addr = DecodeDestination(GetArg("-mineraddress", ""));
469     if (!IsValidDestination(addr)) {
470         return;
471     }
472
473     boost::shared_ptr<MinerAddressScript> mAddr(new MinerAddressScript());
474     CKeyID keyID = boost::get<CKeyID>(addr);
475
476     script = mAddr;
477     script->reserveScript = CScript() << OP_DUP << OP_HASH160 << ToByteVector(keyID) << OP_EQUALVERIFY << OP_CHECKSIG;
478 }
479
480 void IncrementExtraNonce(CBlock* pblock, const CBlockIndex* pindexPrev, unsigned int& nExtraNonce)
481 {
482     // Update nExtraNonce
483     static uint256 hashPrevBlock;
484     if (hashPrevBlock != pblock->hashPrevBlock)
485     {
486         nExtraNonce = 0;
487         hashPrevBlock = pblock->hashPrevBlock;
488     }
489     ++nExtraNonce;
490     unsigned int nHeight = pindexPrev->nHeight+1; // Height first in coinbase required for block.version=2
491     CMutableTransaction txCoinbase(pblock->vtx[0]);
492     txCoinbase.vin[0].scriptSig = (CScript() << nHeight << CScriptNum(nExtraNonce)) + COINBASE_FLAGS;
493     assert(txCoinbase.vin[0].scriptSig.size() <= 100);
494
495     pblock->vtx[0] = txCoinbase;
496     pblock->hashMerkleRoot = pblock->BuildMerkleTree();
497 }
498
499 static bool ProcessBlockFound(const CBlock* pblock, const CChainParams& chainparams)
500 {
501     LogPrintf("%s\n", pblock->ToString());
502     LogPrintf("generated %s\n", FormatMoney(pblock->vtx[0].vout[0].nValue));
503
504     // Found a solution
505     {
506         LOCK(cs_main);
507         if (pblock->hashPrevBlock != chainActive.Tip()->GetBlockHash())
508             return error("ZcashMiner: generated block is stale");
509     }
510
511     // Inform about the new block
512     GetMainSignals().BlockFound(pblock->GetHash());
513
514     // Process this block the same as if we had received it from another node
515     CValidationState state;
516     if (!ProcessNewBlock(state, chainparams, NULL, pblock, true, NULL))
517         return error("ZcashMiner: ProcessNewBlock, block not accepted");
518
519     TrackMinedBlock(pblock->GetHash());
520
521     return true;
522 }
523
524 void static BitcoinMiner(const CChainParams& chainparams)
525 {
526     LogPrintf("ZcashMiner started\n");
527     SetThreadPriority(THREAD_PRIORITY_LOWEST);
528     RenameThread("zcash-miner");
529
530     // Each thread has its own counter
531     unsigned int nExtraNonce = 0;
532
533     boost::shared_ptr<CReserveScript> coinbaseScript;
534     GetMainSignals().ScriptForMining(coinbaseScript);
535
536     unsigned int n = chainparams.GetConsensus().nEquihashN;
537     unsigned int k = chainparams.GetConsensus().nEquihashK;
538
539     std::string solver = GetArg("-equihashsolver", "default");
540     assert(solver == "tromp" || solver == "default");
541     LogPrint("pow", "Using Equihash solver \"%s\" with n = %u, k = %u\n", solver, n, k);
542
543     std::mutex m_cs;
544     bool cancelSolver = false;
545     boost::signals2::connection c = uiInterface.NotifyBlockTip.connect(
546         [&m_cs, &cancelSolver](const uint256& hashNewTip) mutable {
547             std::lock_guard<std::mutex> lock{m_cs};
548             cancelSolver = true;
549         }
550     );
551     miningTimer.start();
552
553     try {
554         //throw an error if no script was provided
555         if (!coinbaseScript->reserveScript.size())
556             throw std::runtime_error("No coinbase script available (mining requires a wallet or -mineraddress)");
557
558         while (true) {
559             if (chainparams.MiningRequiresPeers()) {
560                 // Busy-wait for the network to come online so we don't waste time mining
561                 // on an obsolete chain. In regtest mode we expect to fly solo.
562                 miningTimer.stop();
563                 do {
564                     bool fvNodesEmpty;
565                     {
566                         LOCK(cs_vNodes);
567                         fvNodesEmpty = vNodes.empty();
568                     }
569                     if (!fvNodesEmpty && !IsInitialBlockDownload())
570                         break;
571                     MilliSleep(1000);
572                 } while (true);
573                 miningTimer.start();
574             }
575
576             //
577             // Create new block
578             //
579             unsigned int nTransactionsUpdatedLast = mempool.GetTransactionsUpdated();
580             CBlockIndex* pindexPrev = chainActive.Tip();
581
582             unique_ptr<CBlockTemplate> pblocktemplate(CreateNewBlock(coinbaseScript->reserveScript));
583             if (!pblocktemplate.get())
584             {
585                 if (GetArg("-mineraddress", "").empty()) {
586                     LogPrintf("Error in ZcashMiner: Keypool ran out, please call keypoolrefill before restarting the mining thread\n");
587                 } else {
588                     // Should never reach here, because -mineraddress validity is checked in init.cpp
589                     LogPrintf("Error in ZcashMiner: Invalid -mineraddress\n");
590                 }
591                 return;
592             }
593             CBlock *pblock = &pblocktemplate->block;
594             IncrementExtraNonce(pblock, pindexPrev, nExtraNonce);
595
596             LogPrintf("Running ZcashMiner with %u transactions in block (%u bytes)\n", pblock->vtx.size(),
597                 ::GetSerializeSize(*pblock, SER_NETWORK, PROTOCOL_VERSION));
598
599             //
600             // Search
601             //
602             int64_t nStart = GetTime();
603             arith_uint256 hashTarget = arith_uint256().SetCompact(pblock->nBits);
604
605             while (true) {
606                 // Hash state
607                 crypto_generichash_blake2b_state state;
608                 EhInitialiseState(n, k, state);
609
610                 // I = the block header minus nonce and solution.
611                 CEquihashInput I{*pblock};
612                 CDataStream ss(SER_NETWORK, PROTOCOL_VERSION);
613                 ss << I;
614
615                 // H(I||...
616                 crypto_generichash_blake2b_update(&state, (unsigned char*)&ss[0], ss.size());
617
618                 // H(I||V||...
619                 crypto_generichash_blake2b_state curr_state;
620                 curr_state = state;
621                 crypto_generichash_blake2b_update(&curr_state,
622                                                   pblock->nNonce.begin(),
623                                                   pblock->nNonce.size());
624
625                 // (x_1, x_2, ...) = A(I, V, n, k)
626                 LogPrint("pow", "Running Equihash solver \"%s\" with nNonce = %s\n",
627                          solver, pblock->nNonce.ToString());
628
629                 std::function<bool(std::vector<unsigned char>)> validBlock =
630                         [&pblock, &hashTarget, &chainparams, &m_cs, &cancelSolver, &coinbaseScript]
631                         (std::vector<unsigned char> soln) {
632                     // Write the solution to the hash and compute the result.
633                     LogPrint("pow", "- Checking solution against target\n");
634                     pblock->nSolution = soln;
635                     solutionTargetChecks.increment();
636
637                     if (UintToArith256(pblock->GetHash()) > hashTarget) {
638                         return false;
639                     }
640
641                     // Found a solution
642                     SetThreadPriority(THREAD_PRIORITY_NORMAL);
643                     LogPrintf("ZcashMiner:\n");
644                     LogPrintf("proof-of-work found  \n  hash: %s  \ntarget: %s\n", pblock->GetHash().GetHex(), hashTarget.GetHex());
645                     if (ProcessBlockFound(pblock, chainparams)) {
646                         // Ignore chain updates caused by us
647                         std::lock_guard<std::mutex> lock{m_cs};
648                         cancelSolver = false;
649                     }
650                     SetThreadPriority(THREAD_PRIORITY_LOWEST);
651                     coinbaseScript->KeepScript();
652
653                     // In regression test mode, stop mining after a block is found.
654                     if (chainparams.MineBlocksOnDemand()) {
655                         // Increment here because throwing skips the call below
656                         ehSolverRuns.increment();
657                         throw boost::thread_interrupted();
658                     }
659
660                     return true;
661                 };
662                 std::function<bool(EhSolverCancelCheck)> cancelled = [&m_cs, &cancelSolver](EhSolverCancelCheck pos) {
663                     std::lock_guard<std::mutex> lock{m_cs};
664                     return cancelSolver;
665                 };
666
667                 // TODO: factor this out into a function with the same API for each solver.
668                 if (solver == "tromp") {
669                     // Create solver and initialize it.
670                     equi eq(1);
671                     eq.setstate(&curr_state);
672
673                     // Initialization done, start algo driver.
674                     eq.digit0(0);
675                     eq.xfull = eq.bfull = eq.hfull = 0;
676                     eq.showbsizes(0);
677                     for (u32 r = 1; r < WK; r++) {
678                         (r&1) ? eq.digitodd(r, 0) : eq.digiteven(r, 0);
679                         eq.xfull = eq.bfull = eq.hfull = 0;
680                         eq.showbsizes(r);
681                     }
682                     eq.digitK(0);
683                     ehSolverRuns.increment();
684
685                     // Convert solution indices to byte array (decompress) and pass it to validBlock method.
686                     for (size_t s = 0; s < eq.nsols; s++) {
687                         LogPrint("pow", "Checking solution %d\n", s+1);
688                         std::vector<eh_index> index_vector(PROOFSIZE);
689                         for (size_t i = 0; i < PROOFSIZE; i++) {
690                             index_vector[i] = eq.sols[s][i];
691                         }
692                         std::vector<unsigned char> sol_char = GetMinimalFromIndices(index_vector, DIGITBITS);
693
694                         if (validBlock(sol_char)) {
695                             // If we find a POW solution, do not try other solutions
696                             // because they become invalid as we created a new block in blockchain.
697                             break;
698                         }
699                     }
700                 } else {
701                     try {
702                         // If we find a valid block, we rebuild
703                         bool found = EhOptimisedSolve(n, k, curr_state, validBlock, cancelled);
704                         ehSolverRuns.increment();
705                         if (found) {
706                             break;
707                         }
708                     } catch (EhSolverCancelledException&) {
709                         LogPrint("pow", "Equihash solver cancelled\n");
710                         std::lock_guard<std::mutex> lock{m_cs};
711                         cancelSolver = false;
712                     }
713                 }
714
715                 // Check for stop or if block needs to be rebuilt
716                 boost::this_thread::interruption_point();
717                 // Regtest mode doesn't require peers
718                 if (vNodes.empty() && chainparams.MiningRequiresPeers())
719                     break;
720                 if ((UintToArith256(pblock->nNonce) & 0xffff) == 0xffff)
721                     break;
722                 if (mempool.GetTransactionsUpdated() != nTransactionsUpdatedLast && GetTime() - nStart > 60)
723                     break;
724                 if (pindexPrev != chainActive.Tip())
725                     break;
726
727                 // Update nNonce and nTime
728                 pblock->nNonce = ArithToUint256(UintToArith256(pblock->nNonce) + 1);
729                 UpdateTime(pblock, chainparams.GetConsensus(), pindexPrev);
730                 if (chainparams.GetConsensus().nPowAllowMinDifficultyBlocksAfterHeight != boost::none)
731                 {
732                     // Changing pblock->nTime can change work required on testnet:
733                     hashTarget.SetCompact(pblock->nBits);
734                 }
735             }
736         }
737     }
738     catch (const boost::thread_interrupted&)
739     {
740         miningTimer.stop();
741         c.disconnect();
742         LogPrintf("ZcashMiner terminated\n");
743         throw;
744     }
745     catch (const std::runtime_error &e)
746     {
747         miningTimer.stop();
748         c.disconnect();
749         LogPrintf("ZcashMiner runtime error: %s\n", e.what());
750         return;
751     }
752     miningTimer.stop();
753     c.disconnect();
754 }
755
756 void GenerateBitcoins(bool fGenerate, int nThreads, const CChainParams& chainparams)
757 {
758     static boost::thread_group* minerThreads = NULL;
759
760     if (nThreads < 0)
761         nThreads = GetNumCores();
762
763     if (minerThreads != NULL)
764     {
765         minerThreads->interrupt_all();
766         minerThreads->join_all();
767         delete minerThreads;
768         minerThreads = NULL;
769     }
770
771     if (nThreads == 0 || !fGenerate)
772         return;
773
774     minerThreads = new boost::thread_group();
775     for (int i = 0; i < nThreads; i++) {
776         minerThreads->create_thread(boost::bind(&BitcoinMiner, boost::cref(chainparams)));
777     }
778 }
779
780 #endif // ENABLE_MINING
This page took 0.068468 seconds and 4 git commands to generate.