Untitled


SUBMITTED BY: Guest

DATE: June 8, 2014, 6:12 p.m.

FORMAT: C++

SIZE: 25.5 kB

HITS: 1943

  1. // Copyright (c) 2009-2010 Satoshi Nakamoto
  2. // Copyright (c) 2009-2014 The Bitcoin developers
  3. // Copyright (c) 2011-2013 The Litecoin developers
  4. // Copyright (c) 2013-2014 The Dogecoin developers
  5. // Copyright (c) 2014 The Inutoshi developers
  6. // Distributed under the MIT/X11 software license, see the accompanying
  7. // file COPYING or http://www.opensource.org/licenses/mit-license.php.
  8. #include "miner.h"
  9. #include "core.h"
  10. #include "main.h"
  11. #include "net.h"
  12. #include "scrypt.h"
  13. #ifdef ENABLE_WALLET
  14. #include "wallet.h"
  15. #endif
  16. //////////////////////////////////////////////////////////////////////////////
  17. //
  18. // BitcoinMiner
  19. //
  20. int static FormatHashBlocks(void* pbuffer, unsigned int len)
  21. {
  22. unsigned char* pdata = (unsigned char*)pbuffer;
  23. unsigned int blocks = 1 + ((len + 8) / 64);
  24. unsigned char* pend = pdata + 64 * blocks;
  25. memset(pdata + len, 0, 64 * blocks - len);
  26. pdata[len] = 0x80;
  27. unsigned int bits = len * 8;
  28. pend[-1] = (bits >> 0) & 0xff;
  29. pend[-2] = (bits >> 8) & 0xff;
  30. pend[-3] = (bits >> 16) & 0xff;
  31. pend[-4] = (bits >> 24) & 0xff;
  32. return blocks;
  33. }
  34. static const unsigned int pSHA256InitState[8] =
  35. {0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19};
  36. void SHA256Transform(void* pstate, void* pinput, const void* pinit)
  37. {
  38. SHA256_CTX ctx;
  39. unsigned char data[64];
  40. SHA256_Init(&ctx);
  41. for (int i = 0; i < 16; i++)
  42. ((uint32_t*)data)[i] = ByteReverse(((uint32_t*)pinput)[i]);
  43. for (int i = 0; i < 8; i++)
  44. ctx.h[i] = ((uint32_t*)pinit)[i];
  45. SHA256_Update(&ctx, data, sizeof(data));
  46. for (int i = 0; i < 8; i++)
  47. ((uint32_t*)pstate)[i] = ctx.h[i];
  48. }
  49. // Some explaining would be appreciated
  50. class COrphan
  51. {
  52. public:
  53. const CTransaction* ptx;
  54. set<uint256> setDependsOn;
  55. double dPriority;
  56. double dFeePerKb;
  57. COrphan(const CTransaction* ptxIn)
  58. {
  59. ptx = ptxIn;
  60. dPriority = dFeePerKb = 0;
  61. }
  62. void print() const
  63. {
  64. LogPrintf("COrphan(hash=%s, dPriority=%.1f, dFeePerKb=%.1f)\n",
  65. ptx->GetHash().ToString(), dPriority, dFeePerKb);
  66. BOOST_FOREACH(uint256 hash, setDependsOn)
  67. LogPrintf(" setDependsOn %s\n", hash.ToString());
  68. }
  69. };
  70. uint64_t nLastBlockTx = 0;
  71. uint64_t nLastBlockSize = 0;
  72. // We want to sort transactions by priority and fee, so:
  73. typedef boost::tuple<double, double, const CTransaction*> TxPriority;
  74. class TxPriorityCompare
  75. {
  76. bool byFee;
  77. public:
  78. TxPriorityCompare(bool _byFee) : byFee(_byFee) { }
  79. bool operator()(const TxPriority& a, const TxPriority& b)
  80. {
  81. if (byFee)
  82. {
  83. if (a.get<1>() == b.get<1>())
  84. return a.get<0>() < b.get<0>();
  85. return a.get<1>() < b.get<1>();
  86. }
  87. else
  88. {
  89. if (a.get<0>() == b.get<0>())
  90. return a.get<1>() < b.get<1>();
  91. return a.get<0>() < b.get<0>();
  92. }
  93. }
  94. };
  95. CBlockTemplate* CreateNewBlock(const CScript& scriptPubKeyIn)
  96. {
  97. // Create new block
  98. auto_ptr<CBlockTemplate> pblocktemplate(new CBlockTemplate());
  99. if(!pblocktemplate.get())
  100. return NULL;
  101. CBlock *pblock = &pblocktemplate->block; // pointer for convenience
  102. // Create coinbase tx
  103. CTransaction txNew;
  104. txNew.vin.resize(1);
  105. txNew.vin[0].prevout.SetNull();
  106. txNew.vout.resize(1);
  107. txNew.vout[0].scriptPubKey = scriptPubKeyIn;
  108. // Add our coinbase tx as first transaction
  109. pblock->vtx.push_back(txNew);
  110. pblocktemplate->vTxFees.push_back(-1); // updated at end
  111. pblocktemplate->vTxSigOps.push_back(-1); // updated at end
  112. // Largest block you're willing to create:
  113. unsigned int nBlockMaxSize = GetArg("-blockmaxsize", DEFAULT_BLOCK_MAX_SIZE);
  114. // Limit to betweeen 1K and MAX_BLOCK_SIZE-1K for sanity:
  115. nBlockMaxSize = std::max((unsigned int)1000, std::min((unsigned int)(MAX_BLOCK_SIZE-1000), nBlockMaxSize));
  116. // How much of the block should be dedicated to high-priority transactions,
  117. // included regardless of the fees they pay
  118. unsigned int nBlockPrioritySize = GetArg("-blockprioritysize", DEFAULT_BLOCK_PRIORITY_SIZE);
  119. nBlockPrioritySize = std::min(nBlockMaxSize, nBlockPrioritySize);
  120. // Minimum block size you want to create; block will be filled with free transactions
  121. // until there are no more or the block reaches this size:
  122. unsigned int nBlockMinSize = GetArg("-blockminsize", DEFAULT_BLOCK_MIN_SIZE);
  123. nBlockMinSize = std::min(nBlockMaxSize, nBlockMinSize);
  124. // Collect memory pool transactions into the block
  125. int64_t nFees = 0;
  126. {
  127. LOCK2(cs_main, mempool.cs);
  128. CBlockIndex* pindexPrev = chainActive.Tip();
  129. CCoinsViewCache view(*pcoinsTip, true);
  130. // Priority order to process transactions
  131. list<COrphan> vOrphan; // list memory doesn't move
  132. map<uint256, vector<COrphan*> > mapDependers;
  133. bool fPrintPriority = GetBoolArg("-printpriority", false);
  134. // This vector will be sorted into a priority queue:
  135. vector<TxPriority> vecPriority;
  136. vecPriority.reserve(mempool.mapTx.size());
  137. for (map<uint256, CTxMemPoolEntry>::iterator mi = mempool.mapTx.begin();
  138. mi != mempool.mapTx.end(); ++mi)
  139. {
  140. const CTransaction& tx = mi->second.GetTx();
  141. if (tx.IsCoinBase() || !IsFinalTx(tx, pindexPrev->nHeight + 1))
  142. continue;
  143. COrphan* porphan = NULL;
  144. double dPriority = 0;
  145. int64_t nTotalIn = 0;
  146. bool fMissingInputs = false;
  147. BOOST_FOREACH(const CTxIn& txin, tx.vin)
  148. {
  149. // Read prev transaction
  150. if (!view.HaveCoins(txin.prevout.hash))
  151. {
  152. // This should never happen; all transactions in the memory
  153. // pool should connect to either transactions in the chain
  154. // or other transactions in the memory pool.
  155. if (!mempool.mapTx.count(txin.prevout.hash))
  156. {
  157. LogPrintf("ERROR: mempool transaction missing input\n");
  158. if (fDebug) assert("mempool transaction missing input" == 0);
  159. fMissingInputs = true;
  160. if (porphan)
  161. vOrphan.pop_back();
  162. break;
  163. }
  164. // Has to wait for dependencies
  165. if (!porphan)
  166. {
  167. // Use list for automatic deletion
  168. vOrphan.push_back(COrphan(&tx));
  169. porphan = &vOrphan.back();
  170. }
  171. mapDependers[txin.prevout.hash].push_back(porphan);
  172. porphan->setDependsOn.insert(txin.prevout.hash);
  173. nTotalIn += mempool.mapTx[txin.prevout.hash].GetTx().vout[txin.prevout.n].nValue;
  174. continue;
  175. }
  176. const CCoins &coins = view.GetCoins(txin.prevout.hash);
  177. int64_t nValueIn = coins.vout[txin.prevout.n].nValue;
  178. nTotalIn += nValueIn;
  179. int nConf = pindexPrev->nHeight - coins.nHeight + 1;
  180. dPriority += (double)nValueIn * nConf;
  181. }
  182. if (fMissingInputs) continue;
  183. // Priority is sum(valuein * age) / modified_txsize
  184. unsigned int nTxSize = ::GetSerializeSize(tx, SER_NETWORK, PROTOCOL_VERSION);
  185. dPriority = tx.ComputePriority(dPriority, nTxSize);
  186. // This is a more accurate fee-per-kilobyte than is used by the client code, because the
  187. // client code rounds up the size to the nearest 1K. That's good, because it gives an
  188. // incentive to create smaller transactions.
  189. double dFeePerKb = double(nTotalIn-tx.GetValueOut()) / (double(nTxSize)/1000.0);
  190. if (porphan)
  191. {
  192. porphan->dPriority = dPriority;
  193. porphan->dFeePerKb = dFeePerKb;
  194. }
  195. else
  196. vecPriority.push_back(TxPriority(dPriority, dFeePerKb, &mi->second.GetTx()));
  197. }
  198. // Collect transactions into block
  199. uint64_t nBlockSize = 1000;
  200. uint64_t nBlockTx = 0;
  201. int nBlockSigOps = 100;
  202. bool fSortedByFee = (nBlockPrioritySize <= 0);
  203. TxPriorityCompare comparer(fSortedByFee);
  204. std::make_heap(vecPriority.begin(), vecPriority.end(), comparer);
  205. while (!vecPriority.empty())
  206. {
  207. // Take highest priority transaction off the priority queue:
  208. double dPriority = vecPriority.front().get<0>();
  209. double dFeePerKb = vecPriority.front().get<1>();
  210. const CTransaction& tx = *(vecPriority.front().get<2>());
  211. std::pop_heap(vecPriority.begin(), vecPriority.end(), comparer);
  212. vecPriority.pop_back();
  213. // Size limits
  214. unsigned int nTxSize = ::GetSerializeSize(tx, SER_NETWORK, PROTOCOL_VERSION);
  215. if (nBlockSize + nTxSize >= nBlockMaxSize)
  216. continue;
  217. // Legacy limits on sigOps:
  218. unsigned int nTxSigOps = GetLegacySigOpCount(tx);
  219. if (nBlockSigOps + nTxSigOps >= MAX_BLOCK_SIGOPS)
  220. continue;
  221. // Skip free transactions if we're past the minimum block size:
  222. if (fSortedByFee && (dFeePerKb < CTransaction::nMinRelayTxFee) && (nBlockSize + nTxSize >= nBlockMinSize))
  223. continue;
  224. // Prioritize by fee once past the priority size or we run out of high-priority
  225. // transactions:
  226. if (!fSortedByFee &&
  227. ((nBlockSize + nTxSize >= nBlockPrioritySize) || !AllowFree(dPriority)))
  228. {
  229. fSortedByFee = true;
  230. comparer = TxPriorityCompare(fSortedByFee);
  231. std::make_heap(vecPriority.begin(), vecPriority.end(), comparer);
  232. }
  233. if (!view.HaveInputs(tx))
  234. continue;
  235. int64_t nTxFees = view.GetValueIn(tx)-tx.GetValueOut();
  236. nTxSigOps += GetP2SHSigOpCount(tx, view);
  237. if (nBlockSigOps + nTxSigOps >= MAX_BLOCK_SIGOPS)
  238. continue;
  239. CValidationState state;
  240. if (!CheckInputs(tx, state, view, true, SCRIPT_VERIFY_P2SH))
  241. continue;
  242. CTxUndo txundo;
  243. uint256 hash = tx.GetHash();
  244. UpdateCoins(tx, state, view, txundo, pindexPrev->nHeight+1, hash);
  245. // Added
  246. pblock->vtx.push_back(tx);
  247. pblocktemplate->vTxFees.push_back(nTxFees);
  248. pblocktemplate->vTxSigOps.push_back(nTxSigOps);
  249. nBlockSize += nTxSize;
  250. ++nBlockTx;
  251. nBlockSigOps += nTxSigOps;
  252. nFees += nTxFees;
  253. if (fPrintPriority)
  254. {
  255. LogPrintf("priority %.1f feeperkb %.1f txid %s\n",
  256. dPriority, dFeePerKb, tx.GetHash().ToString());
  257. }
  258. // Add transactions that depend on this one to the priority queue
  259. if (mapDependers.count(hash))
  260. {
  261. BOOST_FOREACH(COrphan* porphan, mapDependers[hash])
  262. {
  263. if (!porphan->setDependsOn.empty())
  264. {
  265. porphan->setDependsOn.erase(hash);
  266. if (porphan->setDependsOn.empty())
  267. {
  268. vecPriority.push_back(TxPriority(porphan->dPriority, porphan->dFeePerKb, porphan->ptx));
  269. std::push_heap(vecPriority.begin(), vecPriority.end(), comparer);
  270. }
  271. }
  272. }
  273. }
  274. }
  275. nLastBlockTx = nBlockTx;
  276. nLastBlockSize = nBlockSize;
  277. LogPrintf("CreateNewBlock(): total size %u\n", nBlockSize);
  278. pblock->vtx[0].vout[0].nValue = GetBlockValue(pindexPrev->nHeight+1, nFees, pindexPrev->GetBlockHash());
  279. pblocktemplate->vTxFees[0] = -nFees;
  280. // Fill in header
  281. pblock->hashPrevBlock = pindexPrev->GetBlockHash();
  282. UpdateTime(*pblock, pindexPrev);
  283. pblock->nBits = GetNextWorkRequired(pindexPrev, pblock);
  284. pblock->nNonce = 0;
  285. pblock->vtx[0].vin[0].scriptSig = CScript() << OP_0 << OP_0;
  286. pblocktemplate->vTxSigOps[0] = GetLegacySigOpCount(pblock->vtx[0]);
  287. CBlockIndex indexDummy(*pblock);
  288. indexDummy.pprev = pindexPrev;
  289. indexDummy.nHeight = pindexPrev->nHeight + 1;
  290. CCoinsViewCache viewNew(*pcoinsTip, true);
  291. CValidationState state;
  292. if (!ConnectBlock(*pblock, state, &indexDummy, viewNew, true))
  293. throw std::runtime_error("CreateNewBlock() : ConnectBlock failed");
  294. }
  295. return pblocktemplate.release();
  296. }
  297. void IncrementExtraNonce(CBlock* pblock, CBlockIndex* pindexPrev, unsigned int& nExtraNonce)
  298. {
  299. // Update nExtraNonce
  300. static uint256 hashPrevBlock;
  301. if (hashPrevBlock != pblock->hashPrevBlock)
  302. {
  303. nExtraNonce = 0;
  304. hashPrevBlock = pblock->hashPrevBlock;
  305. }
  306. ++nExtraNonce;
  307. unsigned int nHeight = pindexPrev->nHeight+1; // Height first in coinbase required for block.version=2
  308. pblock->vtx[0].vin[0].scriptSig = (CScript() << nHeight << CBigNum(nExtraNonce)) + COINBASE_FLAGS;
  309. assert(pblock->vtx[0].vin[0].scriptSig.size() <= 100);
  310. pblock->hashMerkleRoot = pblock->BuildMerkleTree();
  311. }
  312. void FormatHashBuffers(CBlock* pblock, char* pmidstate, char* pdata, char* phash1)
  313. {
  314. //
  315. // Pre-build hash buffers
  316. //
  317. struct
  318. {
  319. struct unnamed2
  320. {
  321. int nVersion;
  322. uint256 hashPrevBlock;
  323. uint256 hashMerkleRoot;
  324. unsigned int nTime;
  325. unsigned int nBits;
  326. unsigned int nNonce;
  327. }
  328. block;
  329. unsigned char pchPadding0[64];
  330. uint256 hash1;
  331. unsigned char pchPadding1[64];
  332. }
  333. tmp;
  334. memset(&tmp, 0, sizeof(tmp));
  335. tmp.block.nVersion = pblock->nVersion;
  336. tmp.block.hashPrevBlock = pblock->hashPrevBlock;
  337. tmp.block.hashMerkleRoot = pblock->hashMerkleRoot;
  338. tmp.block.nTime = pblock->nTime;
  339. tmp.block.nBits = pblock->nBits;
  340. tmp.block.nNonce = pblock->nNonce;
  341. FormatHashBlocks(&tmp.block, sizeof(tmp.block));
  342. FormatHashBlocks(&tmp.hash1, sizeof(tmp.hash1));
  343. // Byte swap all the input buffer
  344. for (unsigned int i = 0; i < sizeof(tmp)/4; i++)
  345. ((unsigned int*)&tmp)[i] = ByteReverse(((unsigned int*)&tmp)[i]);
  346. // Precalc the first half of the first hash, which stays constant
  347. SHA256Transform(pmidstate, &tmp.block, pSHA256InitState);
  348. memcpy(pdata, &tmp.block, 128);
  349. memcpy(phash1, &tmp.hash1, 64);
  350. }
  351. #ifdef ENABLE_WALLET
  352. //////////////////////////////////////////////////////////////////////////////
  353. //
  354. // Internal miner
  355. //
  356. double dHashesPerSec = 0.0;
  357. int64_t nHPSTimerStart = 0;
  358. //
  359. // ScanHash scans nonces looking for a hash with at least some zero bits.
  360. // It operates on big endian data. Caller does the byte reversing.
  361. // All input buffers are 16-byte aligned. nNonce is usually preserved
  362. // between calls, but periodically or if nNonce is 0xffff0000 or above,
  363. // the block is rebuilt and nNonce starts over at zero.
  364. //
  365. unsigned int static ScanHash_CryptoPP(char* pmidstate, char* pdata, char* phash1, char* phash, unsigned int& nHashesDone)
  366. {
  367. unsigned int& nNonce = *(unsigned int*)(pdata + 12);
  368. for (;;)
  369. {
  370. // Crypto++ SHA256
  371. // Hash pdata using pmidstate as the starting state into
  372. // pre-formatted buffer phash1, then hash phash1 into phash
  373. nNonce++;
  374. SHA256Transform(phash1, pdata, pmidstate);
  375. SHA256Transform(phash, phash1, pSHA256InitState);
  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 (((unsigned short*)phash)[14] == 0)
  379. return nNonce;
  380. // If nothing found after trying for a while, return -1
  381. if ((nNonce & 0xffff) == 0)
  382. {
  383. nHashesDone = 0xffff+1;
  384. return (unsigned int) -1;
  385. }
  386. if ((nNonce & 0xfff) == 0)
  387. boost::this_thread::interruption_point();
  388. }
  389. }
  390. CBlockTemplate* CreateNewBlockWithKey(CReserveKey& reservekey)
  391. {
  392. CPubKey pubkey;
  393. if (!reservekey.GetReservedKey(pubkey))
  394. return NULL;
  395. CScript scriptPubKey = CScript() << pubkey << OP_CHECKSIG;
  396. return CreateNewBlock(scriptPubKey);
  397. }
  398. bool CheckWork(CBlock* pblock, CWallet& wallet, CReserveKey& reservekey)
  399. {
  400. uint256 hash = pblock->GetPoWHash();
  401. uint256 hashTarget = CBigNum().SetCompact(pblock->nBits).getuint256();
  402. if (hash > hashTarget)
  403. return false;
  404. //// debug print
  405. printf("DogecoinMiner:\n");
  406. printf("proof-of-work found \n hash: %s \ntarget: %s\n", hash.GetHex().c_str(), hashTarget.GetHex().c_str());
  407. pblock->print();
  408. printf("generated %s\n", FormatMoney(pblock->vtx[0].vout[0].nValue).c_str());
  409. // Found a solution
  410. {
  411. LOCK(cs_main);
  412. if (pblock->hashPrevBlock != chainActive.Tip()->GetBlockHash())
  413. return error("DogecoinMiner : generated block is stale");
  414. // Remove key from key pool
  415. reservekey.KeepKey();
  416. // Track how many getdata requests this block gets
  417. {
  418. LOCK(wallet.cs_wallet);
  419. wallet.mapRequestCount[pblock->GetHash()] = 0;
  420. }
  421. // Process this block the same as if we had received it from another node
  422. CValidationState state;
  423. if (!ProcessBlock(state, NULL, pblock))
  424. return error("DogecoinMiner : ProcessBlock, block not accepted");
  425. }
  426. return true;
  427. }
  428. void static DogecoinMiner(CWallet *pwallet)
  429. {
  430. printf("DogecoinMiner started\n");
  431. SetThreadPriority(THREAD_PRIORITY_LOWEST);
  432. RenameThread("dogecoin-miner");
  433. // Each thread has its own key and counter
  434. CReserveKey reservekey(pwallet);
  435. unsigned int nExtraNonce = 0;
  436. try { while (true) {
  437. if (Params().NetworkID() != CChainParams::REGTEST) {
  438. // Busy-wait for the network to come online so we don't waste time mining
  439. // on an obsolete chain. In regtest mode we expect to fly solo.
  440. while (vNodes.empty())
  441. MilliSleep(1000);
  442. }
  443. //
  444. // Create new block
  445. //
  446. unsigned int nTransactionsUpdatedLast = mempool.GetTransactionsUpdated();
  447. CBlockIndex* pindexPrev = chainActive.Tip();
  448. auto_ptr<CBlockTemplate> pblocktemplate(CreateNewBlockWithKey(reservekey));
  449. if (!pblocktemplate.get())
  450. return;
  451. CBlock *pblock = &pblocktemplate->block;
  452. IncrementExtraNonce(pblock, pindexPrev, nExtraNonce);
  453. printf("Running DogecoinMiner with %" PRIszu" transactions in block (%u bytes)\n", pblock->vtx.size(),
  454. ::GetSerializeSize(*pblock, SER_NETWORK, PROTOCOL_VERSION));
  455. //
  456. // Pre-build hash buffers
  457. //
  458. char pmidstatebuf[32+16]; char* pmidstate = alignup<16>(pmidstatebuf);
  459. char pdatabuf[128+16]; char* pdata = alignup<16>(pdatabuf);
  460. char phash1buf[64+16]; char* phash1 = alignup<16>(phash1buf);
  461. FormatHashBuffers(pblock, pmidstate, pdata, phash1);
  462. unsigned int& nBlockTime = *(unsigned int*)(pdata + 64 + 4);
  463. unsigned int& nBlockBits = *(unsigned int*)(pdata + 64 + 8);
  464. //unsigned int& nBlockNonce = *(unsigned int*)(pdata + 64 + 12);
  465. //
  466. // Search
  467. //
  468. int64_t nStart = GetTime();
  469. uint256 hashTarget = CBigNum().SetCompact(pblock->nBits).getuint256();
  470. while (true)
  471. {
  472. unsigned int nHashesDone = 0;
  473. uint256 thash;
  474. char scratchpad[SCRYPT_SCRATCHPAD_SIZE];
  475. while (true)
  476. {
  477. scrypt_1024_1_1_256_sp(BEGIN(pblock->nVersion), BEGIN(thash), scratchpad);
  478. if (thash <= hashTarget)
  479. {
  480. // Found a solution
  481. SetThreadPriority(THREAD_PRIORITY_NORMAL);
  482. CheckWork(pblock, *pwallet, reservekey);
  483. SetThreadPriority(THREAD_PRIORITY_LOWEST);
  484. break;
  485. }
  486. pblock->nNonce += 1;
  487. nHashesDone += 1;
  488. if ((pblock->nNonce & 0xFF) == 0)
  489. break;
  490. }
  491. // Meter hashes/sec
  492. static int64_t nHashCounter;
  493. if (nHPSTimerStart == 0)
  494. {
  495. nHPSTimerStart = GetTimeMillis();
  496. nHashCounter = 0;
  497. }
  498. else
  499. nHashCounter += nHashesDone;
  500. if (GetTimeMillis() - nHPSTimerStart > 4000)
  501. {
  502. static CCriticalSection cs;
  503. {
  504. LOCK(cs);
  505. if (GetTimeMillis() - nHPSTimerStart > 4000)
  506. {
  507. dHashesPerSec = 1000.0 * nHashCounter / (GetTimeMillis() - nHPSTimerStart);
  508. nHPSTimerStart = GetTimeMillis();
  509. nHashCounter = 0;
  510. static int64_t nLogTime;
  511. if (GetTime() - nLogTime > 30 * 60)
  512. {
  513. nLogTime = GetTime();
  514. LogPrintf("hashmeter %6.0f khash/s\n", dHashesPerSec/1000.0);
  515. }
  516. }
  517. }
  518. }
  519. // Check for stop or if block needs to be rebuilt
  520. boost::this_thread::interruption_point();
  521. if (vNodes.empty() && Params().NetworkID() != CChainParams::REGTEST)
  522. break;
  523. if (pblock->nNonce >= 0xffff0000)
  524. break;
  525. if (mempool.GetTransactionsUpdated() != nTransactionsUpdatedLast && GetTime() - nStart > 60)
  526. break;
  527. if (pindexPrev != chainActive.Tip())
  528. break;
  529. // Update nTime every few seconds
  530. UpdateTime(*pblock, pindexPrev);
  531. nBlockTime = ByteReverse(pblock->nTime);
  532. if (TestNet())
  533. {
  534. // Changing pblock->nTime can change work required on testnet:
  535. nBlockBits = ByteReverse(pblock->nBits);
  536. hashTarget = CBigNum().SetCompact(pblock->nBits).getuint256();
  537. }
  538. }
  539. } }
  540. catch (boost::thread_interrupted)
  541. {
  542. printf("DogecoinMiner terminated\n");
  543. throw;
  544. }
  545. }
  546. void GenerateBitcoins(bool fGenerate, CWallet* pwallet, int nThreads)
  547. {
  548. static boost::thread_group* minerThreads = NULL;
  549. if (nThreads < 0) {
  550. if (Params().NetworkID() == CChainParams::REGTEST)
  551. nThreads = 1;
  552. else
  553. nThreads = boost::thread::hardware_concurrency();
  554. }
  555. if (minerThreads != NULL)
  556. {
  557. minerThreads->interrupt_all();
  558. delete minerThreads;
  559. minerThreads = NULL;
  560. }
  561. if (nThreads == 0 || !fGenerate)
  562. return;
  563. minerThreads = new boost::thread_group();
  564. for (int i = 0; i < nThreads; i++)
  565. minerThreads->create_thread(boost::bind(&DogecoinMiner, pwallet));
  566. }
  567. #endif

comments powered by Disqus