Skip to content

Commit acfa033

Browse files
committed
Move internal miner functionality together
1 parent 4a85e06 commit acfa033

File tree

1 file changed

+52
-53
lines changed

1 file changed

+52
-53
lines changed

src/miner.cpp

Lines changed: 52 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,6 @@
1010
#include "net.h"
1111
#include "wallet.h"
1212

13-
#ifdef ENABLE_WALLET
14-
// These globals are only used by the built-in miner
15-
double dHashesPerSec = 0.0;
16-
int64_t nHPSTimerStart = 0;
17-
#endif
18-
1913
//////////////////////////////////////////////////////////////////////////////
2014
//
2115
// BitcoinMiner
@@ -57,41 +51,6 @@ void SHA256Transform(void* pstate, void* pinput, const void* pinit)
5751
((uint32_t*)pstate)[i] = ctx.h[i];
5852
}
5953

60-
//
61-
// ScanHash scans nonces looking for a hash with at least some zero bits.
62-
// It operates on big endian data. Caller does the byte reversing.
63-
// All input buffers are 16-byte aligned. nNonce is usually preserved
64-
// between calls, but periodically or if nNonce is 0xffff0000 or above,
65-
// the block is rebuilt and nNonce starts over at zero.
66-
//
67-
unsigned int static ScanHash_CryptoPP(char* pmidstate, char* pdata, char* phash1, char* phash, unsigned int& nHashesDone)
68-
{
69-
unsigned int& nNonce = *(unsigned int*)(pdata + 12);
70-
for (;;)
71-
{
72-
// Crypto++ SHA256
73-
// Hash pdata using pmidstate as the starting state into
74-
// pre-formatted buffer phash1, then hash phash1 into phash
75-
nNonce++;
76-
SHA256Transform(phash1, pdata, pmidstate);
77-
SHA256Transform(phash, phash1, pSHA256InitState);
78-
79-
// Return the nonce if the hash has at least some zero bits,
80-
// caller will check if it has enough to reach the target
81-
if (((unsigned short*)phash)[14] == 0)
82-
return nNonce;
83-
84-
// If nothing found after trying for a while, return -1
85-
if ((nNonce & 0xffff) == 0)
86-
{
87-
nHashesDone = 0xffff+1;
88-
return (unsigned int) -1;
89-
}
90-
if ((nNonce & 0xfff) == 0)
91-
boost::this_thread::interruption_point();
92-
}
93-
}
94-
9554
// Some explaining would be appreciated
9655
class COrphan
9756
{
@@ -384,18 +343,6 @@ CBlockTemplate* CreateNewBlock(const CScript& scriptPubKeyIn)
384343
return pblocktemplate.release();
385344
}
386345

387-
#ifdef ENABLE_WALLET
388-
CBlockTemplate* CreateNewBlockWithKey(CReserveKey& reservekey)
389-
{
390-
CPubKey pubkey;
391-
if (!reservekey.GetReservedKey(pubkey))
392-
return NULL;
393-
394-
CScript scriptPubKey = CScript() << pubkey << OP_CHECKSIG;
395-
return CreateNewBlock(scriptPubKey);
396-
}
397-
#endif
398-
399346
void IncrementExtraNonce(CBlock* pblock, CBlockIndex* pindexPrev, unsigned int& nExtraNonce)
400347
{
401348
// Update nExtraNonce
@@ -460,6 +407,58 @@ void FormatHashBuffers(CBlock* pblock, char* pmidstate, char* pdata, char* phash
460407
}
461408

462409
#ifdef ENABLE_WALLET
410+
//////////////////////////////////////////////////////////////////////////////
411+
//
412+
// Internal miner
413+
//
414+
double dHashesPerSec = 0.0;
415+
int64_t nHPSTimerStart = 0;
416+
417+
//
418+
// ScanHash scans nonces looking for a hash with at least some zero bits.
419+
// It operates on big endian data. Caller does the byte reversing.
420+
// All input buffers are 16-byte aligned. nNonce is usually preserved
421+
// between calls, but periodically or if nNonce is 0xffff0000 or above,
422+
// the block is rebuilt and nNonce starts over at zero.
423+
//
424+
unsigned int static ScanHash_CryptoPP(char* pmidstate, char* pdata, char* phash1, char* phash, unsigned int& nHashesDone)
425+
{
426+
unsigned int& nNonce = *(unsigned int*)(pdata + 12);
427+
for (;;)
428+
{
429+
// Crypto++ SHA256
430+
// Hash pdata using pmidstate as the starting state into
431+
// pre-formatted buffer phash1, then hash phash1 into phash
432+
nNonce++;
433+
SHA256Transform(phash1, pdata, pmidstate);
434+
SHA256Transform(phash, phash1, pSHA256InitState);
435+
436+
// Return the nonce if the hash has at least some zero bits,
437+
// caller will check if it has enough to reach the target
438+
if (((unsigned short*)phash)[14] == 0)
439+
return nNonce;
440+
441+
// If nothing found after trying for a while, return -1
442+
if ((nNonce & 0xffff) == 0)
443+
{
444+
nHashesDone = 0xffff+1;
445+
return (unsigned int) -1;
446+
}
447+
if ((nNonce & 0xfff) == 0)
448+
boost::this_thread::interruption_point();
449+
}
450+
}
451+
452+
CBlockTemplate* CreateNewBlockWithKey(CReserveKey& reservekey)
453+
{
454+
CPubKey pubkey;
455+
if (!reservekey.GetReservedKey(pubkey))
456+
return NULL;
457+
458+
CScript scriptPubKey = CScript() << pubkey << OP_CHECKSIG;
459+
return CreateNewBlock(scriptPubKey);
460+
}
461+
463462
bool CheckWork(CBlock* pblock, CWallet& wallet, CReserveKey& reservekey)
464463
{
465464
uint256 hash = pblock->GetHash();

0 commit comments

Comments
 (0)