Browse Source

miner: indentation fixes, remove for (;;)

- change a for (;;) into while (true), as we nowhere else use the first
- init nNonceFound to 0
- fix indentation in BitcoinMiner try/catch block
0.10
Philip Kaufmann 11 years ago
parent
commit
0655fac0b1
  1. 199
      src/miner.cpp

199
src/miner.cpp

@ -49,7 +49,6 @@ public:
} }
}; };
uint64_t nLastBlockTx = 0; uint64_t nLastBlockTx = 0;
uint64_t nLastBlockSize = 0; uint64_t nLastBlockSize = 0;
@ -58,8 +57,10 @@ typedef boost::tuple<double, CFeeRate, const CTransaction*> TxPriority;
class TxPriorityCompare class TxPriorityCompare
{ {
bool byFee; bool byFee;
public: public:
TxPriorityCompare(bool _byFee) : byFee(_byFee) { } TxPriorityCompare(bool _byFee) : byFee(_byFee) { }
bool operator()(const TxPriority& a, const TxPriority& b) bool operator()(const TxPriority& a, const TxPriority& b)
{ {
if (byFee) if (byFee)
@ -114,6 +115,7 @@ CBlockTemplate* CreateNewBlock(const CScript& scriptPubKeyIn)
// Collect memory pool transactions into the block // Collect memory pool transactions into the block
int64_t nFees = 0; int64_t nFees = 0;
{ {
LOCK2(cs_main, mempool.cs); LOCK2(cs_main, mempool.cs);
CBlockIndex* pindexPrev = chainActive.Tip(); CBlockIndex* pindexPrev = chainActive.Tip();
@ -269,7 +271,7 @@ CBlockTemplate* CreateNewBlock(const CScript& scriptPubKeyIn)
if (fPrintPriority) if (fPrintPriority)
{ {
LogPrintf("priority %.1f fee %s txid %s\n", LogPrintf("priority %.1f fee %s txid %s\n",
dPriority, feeRate.ToString(), tx.GetHash().ToString()); dPriority, feeRate.ToString(), tx.GetHash().ToString());
} }
// Add transactions that depend on this one to the priority queue // Add transactions that depend on this one to the priority queue
@ -334,7 +336,6 @@ void IncrementExtraNonce(CBlock* pblock, CBlockIndex* pindexPrev, unsigned int&
pblock->hashMerkleRoot = pblock->BuildMerkleTree(); pblock->hashMerkleRoot = pblock->BuildMerkleTree();
} }
#ifdef ENABLE_WALLET #ifdef ENABLE_WALLET
////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////
// //
@ -349,7 +350,8 @@ int64_t nHPSTimerStart = 0;
// nonce is 0xffff0000 or above, the block is rebuilt and nNonce starts over at // nonce is 0xffff0000 or above, the block is rebuilt and nNonce starts over at
// zero. // zero.
// //
bool static ScanHash(const CBlockHeader *pblock, uint32_t& nNonce, uint256 *phash) { bool static ScanHash(const CBlockHeader *pblock, uint32_t& nNonce, uint256 *phash)
{
// Write the first 76 bytes of the block header to a double-SHA256 state. // Write the first 76 bytes of the block header to a double-SHA256 state.
CHash256 hasher; CHash256 hasher;
CDataStream ss(SER_NETWORK, PROTOCOL_VERSION); CDataStream ss(SER_NETWORK, PROTOCOL_VERSION);
@ -357,7 +359,7 @@ bool static ScanHash(const CBlockHeader *pblock, uint32_t& nNonce, uint256 *phas
assert(ss.size() == 80); assert(ss.size() == 80);
hasher.Write((unsigned char*)&ss[0], 76); hasher.Write((unsigned char*)&ss[0], 76);
for (;;) { while (true) {
nNonce++; nNonce++;
// Write the last 4 bytes of the block header (the nonce) to a copy of // Write the last 4 bytes of the block header (the nonce) to a copy of
@ -435,114 +437,115 @@ void static BitcoinMiner(CWallet *pwallet)
CReserveKey reservekey(pwallet); CReserveKey reservekey(pwallet);
unsigned int nExtraNonce = 0; unsigned int nExtraNonce = 0;
try { while (true) { try {
if (Params().MiningRequiresPeers()) { while (true) {
// Busy-wait for the network to come online so we don't waste time mining if (Params().MiningRequiresPeers()) {
// on an obsolete chain. In regtest mode we expect to fly solo. // Busy-wait for the network to come online so we don't waste time mining
while (vNodes.empty()) // on an obsolete chain. In regtest mode we expect to fly solo.
MilliSleep(1000); while (vNodes.empty())
} MilliSleep(1000);
}
//
// Create new block
//
unsigned int nTransactionsUpdatedLast = mempool.GetTransactionsUpdated();
CBlockIndex* pindexPrev = chainActive.Tip();
auto_ptr<CBlockTemplate> pblocktemplate(CreateNewBlockWithKey(reservekey));
if (!pblocktemplate.get())
return;
CBlock *pblock = &pblocktemplate->block;
IncrementExtraNonce(pblock, pindexPrev, nExtraNonce);
LogPrintf("Running BitcoinMiner with %u transactions in block (%u bytes)\n", pblock->vtx.size(),
::GetSerializeSize(*pblock, SER_NETWORK, PROTOCOL_VERSION));
//
// Search
//
int64_t nStart = GetTime();
uint256 hashTarget = uint256().SetCompact(pblock->nBits);
uint256 hash;
uint32_t nNonce = 0;
uint32_t nOldNonce = 0;
while (true)
{
bool fFound = ScanHash(pblock, nNonce, &hash);
uint32_t nHashesDone = nNonce - nOldNonce;
nOldNonce = nNonce;
// Check if something found //
if (fFound) // Create new block
{ //
if (hash <= hashTarget) unsigned int nTransactionsUpdatedLast = mempool.GetTransactionsUpdated();
CBlockIndex* pindexPrev = chainActive.Tip();
auto_ptr<CBlockTemplate> pblocktemplate(CreateNewBlockWithKey(reservekey));
if (!pblocktemplate.get())
return;
CBlock *pblock = &pblocktemplate->block;
IncrementExtraNonce(pblock, pindexPrev, nExtraNonce);
LogPrintf("Running BitcoinMiner with %u transactions in block (%u bytes)\n", pblock->vtx.size(),
::GetSerializeSize(*pblock, SER_NETWORK, PROTOCOL_VERSION));
//
// Search
//
int64_t nStart = GetTime();
uint256 hashTarget = uint256().SetCompact(pblock->nBits);
uint256 hash;
uint32_t nNonce = 0;
uint32_t nOldNonce = 0;
while (true) {
bool fFound = ScanHash(pblock, nNonce, &hash);
uint32_t nHashesDone = nNonce - nOldNonce;
nOldNonce = nNonce;
// Check if something found
if (fFound)
{ {
// Found a solution if (hash <= hashTarget)
pblock->nNonce = nNonce; {
assert(hash == pblock->GetHash()); // Found a solution
pblock->nNonce = nNonce;
assert(hash == pblock->GetHash());
SetThreadPriority(THREAD_PRIORITY_NORMAL); SetThreadPriority(THREAD_PRIORITY_NORMAL);
CheckWork(pblock, *pwallet, reservekey); CheckWork(pblock, *pwallet, reservekey);
SetThreadPriority(THREAD_PRIORITY_LOWEST); SetThreadPriority(THREAD_PRIORITY_LOWEST);
// In regression test mode, stop mining after a block is found. // In regression test mode, stop mining after a block is found.
if (Params().MineBlocksOnDemand()) if (Params().MineBlocksOnDemand())
throw boost::thread_interrupted(); throw boost::thread_interrupted();
break; break;
}
} }
}
// Meter hashes/sec // Meter hashes/sec
static int64_t nHashCounter; static int64_t nHashCounter;
if (nHPSTimerStart == 0) if (nHPSTimerStart == 0)
{
nHPSTimerStart = GetTimeMillis();
nHashCounter = 0;
}
else
nHashCounter += nHashesDone;
if (GetTimeMillis() - nHPSTimerStart > 4000)
{
static CCriticalSection cs;
{ {
LOCK(cs); nHPSTimerStart = GetTimeMillis();
if (GetTimeMillis() - nHPSTimerStart > 4000) nHashCounter = 0;
}
else
nHashCounter += nHashesDone;
if (GetTimeMillis() - nHPSTimerStart > 4000)
{
static CCriticalSection cs;
{ {
dHashesPerSec = 1000.0 * nHashCounter / (GetTimeMillis() - nHPSTimerStart); LOCK(cs);
nHPSTimerStart = GetTimeMillis(); if (GetTimeMillis() - nHPSTimerStart > 4000)
nHashCounter = 0;
static int64_t nLogTime;
if (GetTime() - nLogTime > 30 * 60)
{ {
nLogTime = GetTime(); dHashesPerSec = 1000.0 * nHashCounter / (GetTimeMillis() - nHPSTimerStart);
LogPrintf("hashmeter %6.0f khash/s\n", dHashesPerSec/1000.0); nHPSTimerStart = GetTimeMillis();
nHashCounter = 0;
static int64_t nLogTime;
if (GetTime() - nLogTime > 30 * 60)
{
nLogTime = GetTime();
LogPrintf("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();
// Regtest mode doesn't require peers // Regtest mode doesn't require peers
if (vNodes.empty() && Params().MiningRequiresPeers()) if (vNodes.empty() && Params().MiningRequiresPeers())
break; break;
if (nNonce >= 0xffff0000) if (nNonce >= 0xffff0000)
break; break;
if (mempool.GetTransactionsUpdated() != nTransactionsUpdatedLast && GetTime() - nStart > 60) if (mempool.GetTransactionsUpdated() != nTransactionsUpdatedLast && GetTime() - nStart > 60)
break; break;
if (pindexPrev != chainActive.Tip()) if (pindexPrev != chainActive.Tip())
break; break;
// Update nTime every few seconds // Update nTime every few seconds
UpdateTime(*pblock, pindexPrev); UpdateTime(*pblock, pindexPrev);
if (Params().AllowMinDifficultyBlocks()) if (Params().AllowMinDifficultyBlocks())
{ {
// Changing pblock->nTime can change work required on testnet: // Changing pblock->nTime can change work required on testnet:
hashTarget.SetCompact(pblock->nBits); hashTarget.SetCompact(pblock->nBits);
}
} }
} }
} } }
catch (boost::thread_interrupted) catch (boost::thread_interrupted)
{ {
LogPrintf("BitcoinMiner terminated\n"); LogPrintf("BitcoinMiner terminated\n");
@ -577,4 +580,4 @@ void GenerateBitcoins(bool fGenerate, CWallet* pwallet, int nThreads)
minerThreads->create_thread(boost::bind(&BitcoinMiner, pwallet)); minerThreads->create_thread(boost::bind(&BitcoinMiner, pwallet));
} }
#endif #endif // ENABLE_WALLET

Loading…
Cancel
Save