1
0
mirror of https://github.com/GOSTSec/gostcoin synced 2025-03-13 05:41:11 +00:00

start mining from randon nonce

This commit is contained in:
orignal 2017-03-31 09:43:19 -04:00
parent cbee8adc0a
commit b2efd40551

View File

@ -4704,96 +4704,100 @@ void static GostcoinMiner(CWallet *pwallet)
CReserveKey reservekey(pwallet); CReserveKey reservekey(pwallet);
unsigned int nExtraNonce = 0; unsigned int nExtraNonce = 0;
try { loop { try
// TODO: {
/* while (vNodes.empty()) loop
MilliSleep(1000);*/ {
while (vNodes.empty())
MilliSleep(1000);
// //
// Create new block // Create new block
// //
unsigned int nTransactionsUpdatedLast = nTransactionsUpdated; unsigned int nTransactionsUpdatedLast = nTransactionsUpdated;
CBlockIndex* pindexPrev = pindexBest; CBlockIndex* pindexPrev = pindexBest;
auto_ptr<CBlockTemplate> pblocktemplate(CreateNewBlockWithKey(reservekey)); auto_ptr<CBlockTemplate> pblocktemplate(CreateNewBlockWithKey(reservekey));
if (!pblocktemplate.get()) if (!pblocktemplate.get())
return; return;
CBlock *pblock = &pblocktemplate->block; CBlock *pblock = &pblocktemplate->block;
IncrementExtraNonce(pblock, pindexPrev, nExtraNonce); RAND_bytes ((uint8_t *)&pblock->nNonce, 4);
IncrementExtraNonce(pblock, pindexPrev, nExtraNonce);
printf("Running GostcoinMiner with %" PRIszu " transactions in block (%u bytes)\n", pblock->vtx.size(), printf("Running GostcoinMiner with %" PRIszu " transactions in block (%u bytes)\n", pblock->vtx.size(),
::GetSerializeSize(*pblock, SER_NETWORK, PROTOCOL_VERSION)); ::GetSerializeSize(*pblock, SER_NETWORK, PROTOCOL_VERSION));
// //
// Solve // Solve
// //
int64 nStart = GetTime(); int64 nStart = GetTime();
uint256 hashTarget = CBigNum().SetCompact(pblock->nBits).getuint256(); uint256 hashTarget = CBigNum().SetCompact(pblock->nBits).getuint256();
loop loop
{ {
unsigned int nHashesDone = 0; unsigned int nHashesDone = 0;
loop loop
{ {
if (pblock->GetHash() <= hashTarget) if (pblock->GetHash() <= hashTarget)
{ {
// Found a solution // Found a solution
SetThreadPriority(THREAD_PRIORITY_NORMAL); SetThreadPriority(THREAD_PRIORITY_NORMAL);
CheckWork(pblock, *pwallet, reservekey); CheckWork(pblock, *pwallet, reservekey);
SetThreadPriority(THREAD_PRIORITY_LOWEST); SetThreadPriority(THREAD_PRIORITY_LOWEST);
break; break;
} }
pblock->nNonce += 1; pblock->nNonce += 1;
nHashesDone += 1; nHashesDone += 1;
if ((pblock->nNonce & 0xFF) == 0) if ((pblock->nNonce & 0xFF) == 0)
break; break;
} }
// Meter hashes/sec // Meter hashes/sec
static int64 nHashCounter; static int64 nHashCounter;
if (nHPSTimerStart == 0) if (nHPSTimerStart == 0)
{ {
nHPSTimerStart = GetTimeMillis(); nHPSTimerStart = GetTimeMillis();
nHashCounter = 0; nHashCounter = 0;
} }
else else
nHashCounter += nHashesDone; nHashCounter += nHashesDone;
if (GetTimeMillis() - nHPSTimerStart > 4000) if (GetTimeMillis() - nHPSTimerStart > 4000)
{ {
static CCriticalSection cs; static CCriticalSection cs;
{ {
LOCK(cs); LOCK(cs);
if (GetTimeMillis() - nHPSTimerStart > 4000) if (GetTimeMillis() - nHPSTimerStart > 4000)
{ {
dHashesPerSec = 1000.0 * nHashCounter / (GetTimeMillis() - nHPSTimerStart); dHashesPerSec = 1000.0 * nHashCounter / (GetTimeMillis() - nHPSTimerStart);
nHPSTimerStart = GetTimeMillis(); nHPSTimerStart = GetTimeMillis();
nHashCounter = 0; nHashCounter = 0;
static int64 nLogTime; static int64 nLogTime;
if (GetTime() - nLogTime > 30 * 60) if (GetTime() - nLogTime > 30 * 60)
{ {
nLogTime = GetTime(); nLogTime = GetTime();
printf("hashmeter %6.0f khash/s\n", dHashesPerSec/1000.0); printf("hashmeter %6.0f khash/s\n", dHashesPerSec/1000.0);
} }
} }
} }
} }
// Check for stop or if block needs to be rebuilt // Check for stop or if block needs to be rebuilt
boost::this_thread::interruption_point(); boost::this_thread::interruption_point();
// TODO: if (vNodes.empty())
/* if (vNodes.empty()) break;
break;*/ if (pblock->nNonce >= 0xffff0000)
if (pblock->nNonce >= 0xffff0000) break;
break; if (nTransactionsUpdated != nTransactionsUpdatedLast && GetTime() - nStart > 60)
if (nTransactionsUpdated != nTransactionsUpdatedLast && GetTime() - nStart > 60) break;
break; if (pindexPrev != pindexBest)
if (pindexPrev != pindexBest) break;
break;
// Update nTime every few seconds // Update nTime every few seconds
pblock->UpdateTime(pindexPrev); pblock->UpdateTime(pindexPrev);
} }
} } }
}
catch (boost::thread_interrupted) catch (boost::thread_interrupted)
{ {
printf("GostcoinMiner terminated\n"); printf("GostcoinMiner terminated\n");