From: CryptoManiac Date: Wed, 23 Sep 2015 22:00:16 +0000 (-0700) Subject: Remove StakeMiner function, mine blocks directly in the thread function. X-Git-Tag: nvc-v0.5.4~6 X-Git-Url: https://git.novaco.in/?p=novacoin.git;a=commitdiff_plain;h=309244e6fd8edbd6e34e472d5287c1d597af967d Remove StakeMiner function, mine blocks directly in the thread function. --- diff --git a/src/miner.cpp b/src/miner.cpp index c03b37d..41a4303 100644 --- a/src/miner.cpp +++ b/src/miner.cpp @@ -649,13 +649,14 @@ bool ScanMap(const MidstateMap &inputsMap, uint32_t nBits, MidstateMap::key_type return false; } -// TODO: Get rid of nested loops -void StakeMiner(CWallet* pwallet) +// Stake miner thread +void ThreadStakeMiner(void* parg) { SetThreadPriority(THREAD_PRIORITY_LOWEST); // Make this thread recognisable as the mining thread RenameThread("novacoin-miner"); + CWallet* pwallet = (CWallet*)parg; MidstateMap inputsMap; if (!FillMap(pwallet, GetAdjustedTime(), inputsMap)) @@ -666,120 +667,120 @@ void StakeMiner(CWallet* pwallet) CBlockIndex* pindexPrev = pindexBest; uint32_t nBits = GetNextTargetRequired(pindexPrev, true); - while (true) + printf("ThreadStakeMinter started\n"); + + try { - if (fShutdown) - return; + vnThreadsRunning[THREAD_MINTER]++; - while (pwallet->IsLocked()) - { - Sleep(1000); - if (fShutdown) - return; - } + MidstateMap::key_type LuckyInput; + std::pair solution; - while (vNodes.empty() || IsInitialBlockDownload()) + // Main miner loop + do { - fTrySync = true; - - Sleep(1000); if (fShutdown) - return; - } + goto _endloop; - if (fTrySync) - { - fTrySync = false; - if (vNodes.size() < 3 || nBestHeight < GetNumBlocksOfPeers()) + while (pwallet->IsLocked()) { Sleep(1000); - continue; + if (fShutdown) + goto _endloop; // Don't be afraid to use a goto if that's the best option. } - } - MidstateMap::key_type LuckyInput; - std::pair solution; - - if (ScanMap(inputsMap, nBits, LuckyInput, solution)) - { - SetThreadPriority(THREAD_PRIORITY_NORMAL); - - // Remove lucky input from the map - inputsMap.erase(inputsMap.find(LuckyInput)); + while (vNodes.empty() || IsInitialBlockDownload()) + { + fTrySync = true; - CKey key; - CTransaction txCoinStake; + Sleep(1000); + if (fShutdown) + goto _endloop; + } - // Create new coinstake transaction - if (!pwallet->CreateCoinStake(LuckyInput.first, LuckyInput.second, solution.second, nBits, txCoinStake, key)) + if (fTrySync) { - string strMessage = _("Warning: Unable to create coinstake transaction, see debug.log for the details. Mining thread has been stopped."); - strMiscWarning = strMessage; - printf("*** %s\n", strMessage.c_str()); - - return; + // Don't try mine blocks unless we're at the top of chain and have at least three p2p connections. + fTrySync = false; + if (vNodes.size() < 3 || nBestHeight < GetNumBlocksOfPeers()) + { + Sleep(1000); + continue; + } } - // Now we have new coinstake, it's time to create the block ... - CBlock* pblock; - pblock = CreateNewBlock(pwallet, &txCoinStake); - if (!pblock) + if (ScanMap(inputsMap, nBits, LuckyInput, solution)) { - string strMessage = _("Warning: Unable to allocate memory for the new block object. Mining thread has been stopped."); - strMiscWarning = strMessage; - printf("*** %s\n", strMessage.c_str()); + SetThreadPriority(THREAD_PRIORITY_NORMAL); - return; - } + // Remove lucky input from the map + inputsMap.erase(inputsMap.find(LuckyInput)); - unsigned int nExtraNonce = 0; - IncrementExtraNonce(pblock, pindexPrev, nExtraNonce); + CKey key; + CTransaction txCoinStake; - // ... and sign it - if (!key.Sign(pblock->GetHash(), pblock->vchBlockSig)) - { - string strMessage = _("Warning: Proof-of-Stake miner is unable to sign the block (locked wallet?). Mining thread has been stopped."); - strMiscWarning = strMessage; - printf("*** %s\n", strMessage.c_str()); + // Create new coinstake transaction + if (!pwallet->CreateCoinStake(LuckyInput.first, LuckyInput.second, solution.second, nBits, txCoinStake, key)) + { + string strMessage = _("Warning: Unable to create coinstake transaction, see debug.log for the details. Mining thread has been stopped."); + strMiscWarning = strMessage; + printf("*** %s\n", strMessage.c_str()); - return; - } + break; + } - CheckStake(pblock, *pwallet); - SetThreadPriority(THREAD_PRIORITY_LOWEST); - Sleep(500); - } + // Now we have new coinstake, it's time to create the block ... + CBlock* pblock; + pblock = CreateNewBlock(pwallet, &txCoinStake); + if (!pblock) + { + string strMessage = _("Warning: Unable to allocate memory for the new block object. Mining thread has been stopped."); + strMiscWarning = strMessage; + printf("*** %s\n", strMessage.c_str()); - if (pindexPrev != pindexBest) - { - // The best block has been changed, we need to refill the map - if (FillMap(pwallet, GetAdjustedTime(), inputsMap)) - { - pindexPrev = pindexBest; - nBits = GetNextTargetRequired(pindexPrev, true); + break; + } + + unsigned int nExtraNonce = 0; + IncrementExtraNonce(pblock, pindexPrev, nExtraNonce); + + // ... and sign it + if (!key.Sign(pblock->GetHash(), pblock->vchBlockSig)) + { + string strMessage = _("Warning: Proof-of-Stake miner is unable to sign the block (locked wallet?). Mining thread has been stopped."); + strMiscWarning = strMessage; + printf("*** %s\n", strMessage.c_str()); + + break; + } + + CheckStake(pblock, *pwallet); + SetThreadPriority(THREAD_PRIORITY_LOWEST); + Sleep(500); } - else + + if (pindexPrev != pindexBest) { - // Clear existent data if FillMap failed - inputsMap.clear(); + // The best block has been changed, we need to refill the map + if (FillMap(pwallet, GetAdjustedTime(), inputsMap)) + { + pindexPrev = pindexBest; + nBits = GetNextTargetRequired(pindexPrev, true); + } + else + { + // Clear existent data if FillMap failed + inputsMap.clear(); + } } - } - Sleep(500); + Sleep(500); - continue; - } -} + _endloop: + (void)0; // do nothing + } + while(!fShutdown); -// Stake minter thread -void ThreadStakeMinter(void* parg) -{ - printf("ThreadStakeMinter started\n"); - CWallet* pwallet = (CWallet*)parg; - try - { - vnThreadsRunning[THREAD_MINTER]++; - StakeMiner(pwallet); vnThreadsRunning[THREAD_MINTER]--; } catch (std::exception& e) { diff --git a/src/miner.h b/src/miner.h index 09bb460..d19dc21 100644 --- a/src/miner.h +++ b/src/miner.h @@ -28,6 +28,6 @@ bool CheckStake(CBlock* pblock, CWallet& wallet); void SHA256Transform(void* pstate, void* pinput, const void* pinit); /** Stake miner thread */ -void ThreadStakeMinter(void* parg); +void ThreadStakeMiner(void* parg); #endif // NOVACOIN_MINER_H diff --git a/src/net.cpp b/src/net.cpp index 632c9b9..23becde 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -1925,9 +1925,9 @@ void StartNode(void* parg) if (!NewThread(ThreadDumpAddress, NULL)) printf("Error; NewThread(ThreadDumpAddress) failed\n"); - // Mint proof-of-stake blocks in the background - if (!NewThread(ThreadStakeMinter, pwalletMain)) - printf("Error: NewThread(ThreadStakeMinter) failed\n"); + // Mine proof-of-stake blocks in the background + if (!NewThread(ThreadStakeMiner, pwalletMain)) + printf("Error: NewThread(ThreadStakeMiner) failed\n"); // Trusted NTP server, it's localhost by default. strTrustedUpstream = GetArg("-ntp", "localhost");