@ -17,9 +17,7 @@
# include "timedata.h"
# include "timedata.h"
# include "util.h"
# include "util.h"
# include "utilmoneystr.h"
# include "utilmoneystr.h"
# ifdef ENABLE_WALLET
# include "validationinterface.h"
# include "wallet/wallet.h"
# endif
# include <boost/thread.hpp>
# include <boost/thread.hpp>
# include <boost/tuple/tuple.hpp>
# include <boost/tuple/tuple.hpp>
@ -362,7 +360,6 @@ void IncrementExtraNonce(CBlock* pblock, CBlockIndex* pindexPrev, unsigned int&
pblock - > hashMerkleRoot = pblock - > BuildMerkleTree ( ) ;
pblock - > hashMerkleRoot = pblock - > BuildMerkleTree ( ) ;
}
}
# ifdef ENABLE_WALLET
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//
//
// Internal miner
// Internal miner
@ -401,17 +398,7 @@ bool static ScanHash(const CBlockHeader *pblock, uint32_t& nNonce, uint256 *phas
}
}
}
}
CBlockTemplate * CreateNewBlockWithKey ( CReserveKey & reservekey )
static bool ProcessBlockFound ( CBlock * pblock , const CChainParams & chainparams )
{
CPubKey pubkey ;
if ( ! reservekey . GetReservedKey ( pubkey ) )
return NULL ;
CScript scriptPubKey = CScript ( ) < < ToByteVector ( pubkey ) < < OP_CHECKSIG ;
return CreateNewBlock ( scriptPubKey ) ;
}
static bool ProcessBlockFound ( CBlock * pblock , CWallet & wallet , CReserveKey & reservekey )
{
{
LogPrintf ( " %s \n " , pblock - > ToString ( ) ) ;
LogPrintf ( " %s \n " , pblock - > ToString ( ) ) ;
LogPrintf ( " generated %s \n " , FormatMoney ( pblock - > vtx [ 0 ] . vout [ 0 ] . nValue ) ) ;
LogPrintf ( " generated %s \n " , FormatMoney ( pblock - > vtx [ 0 ] . vout [ 0 ] . nValue ) ) ;
@ -423,14 +410,8 @@ static bool ProcessBlockFound(CBlock* pblock, CWallet& wallet, CReserveKey& rese
return error ( " BitcoinMiner: generated block is stale " ) ;
return error ( " BitcoinMiner: generated block is stale " ) ;
}
}
// Remove key from key pool
// Inform about the new block
reservekey . KeepKey ( ) ;
GetMainSignals ( ) . BlockFound ( * pblock ) ;
// Track how many getdata requests this block gets
{
LOCK ( wallet . cs_wallet ) ;
wallet . mapRequestCount [ pblock - > GetHash ( ) ] = 0 ;
}
// Process this block the same as if we had received it from another node
// Process this block the same as if we had received it from another node
CValidationState state ;
CValidationState state ;
@ -440,15 +421,12 @@ static bool ProcessBlockFound(CBlock* pblock, CWallet& wallet, CReserveKey& rese
return true ;
return true ;
}
}
void static BitcoinMiner ( CWallet * pwalle t)
void static BitcoinMiner ( const CChainParams & chainparams , const CScript & coinbaseScrip t)
{
{
LogPrintf ( " BitcoinMiner started \n " ) ;
LogPrintf ( " BitcoinMiner started \n " ) ;
SetThreadPriority ( THREAD_PRIORITY_LOWEST ) ;
SetThreadPriority ( THREAD_PRIORITY_LOWEST ) ;
RenameThread ( " bitcoin-miner " ) ;
RenameThread ( " bitcoin-miner " ) ;
const CChainParams & chainparams = Params ( ) ;
// Each thread has its own key and counter
CReserveKey reservekey ( pwallet ) ;
unsigned int nExtraNonce = 0 ;
unsigned int nExtraNonce = 0 ;
try {
try {
@ -474,7 +452,7 @@ void static BitcoinMiner(CWallet *pwallet)
unsigned int nTransactionsUpdatedLast = mempool . GetTransactionsUpdated ( ) ;
unsigned int nTransactionsUpdatedLast = mempool . GetTransactionsUpdated ( ) ;
CBlockIndex * pindexPrev = chainActive . Tip ( ) ;
CBlockIndex * pindexPrev = chainActive . Tip ( ) ;
auto_ptr < CBlockTemplate > pblocktemplate ( CreateNewBlockWithKey ( reservekey ) ) ;
auto_ptr < CBlockTemplate > pblocktemplate ( CreateNewBlock ( coinbaseScript ) ) ;
if ( ! pblocktemplate . get ( ) )
if ( ! pblocktemplate . get ( ) )
{
{
LogPrintf ( " Error in BitcoinMiner: Keypool ran out, please call keypoolrefill before restarting the mining thread \n " ) ;
LogPrintf ( " Error in BitcoinMiner: Keypool ran out, please call keypoolrefill before restarting the mining thread \n " ) ;
@ -506,7 +484,7 @@ void static BitcoinMiner(CWallet *pwallet)
SetThreadPriority ( THREAD_PRIORITY_NORMAL ) ;
SetThreadPriority ( THREAD_PRIORITY_NORMAL ) ;
LogPrintf ( " BitcoinMiner: \n " ) ;
LogPrintf ( " BitcoinMiner: \n " ) ;
LogPrintf ( " proof-of-work found \n hash: %s \n target: %s \n " , hash . GetHex ( ) , hashTarget . GetHex ( ) ) ;
LogPrintf ( " proof-of-work found \n hash: %s \n target: %s \n " , hash . GetHex ( ) , hashTarget . GetHex ( ) ) ;
ProcessBlockFound ( pblock , * pwallet , reservekey ) ;
ProcessBlockFound ( pblock , chainparams ) ;
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.
@ -551,7 +529,7 @@ void static BitcoinMiner(CWallet *pwallet)
}
}
}
}
void GenerateBitcoins ( bool fGenerate , CWallet * pwallet , int nThread s)
void GenerateBitcoins ( bool fGenerate , int nThreads , const CChainParams & chainparam s)
{
{
static boost : : thread_group * minerThreads = NULL ;
static boost : : thread_group * minerThreads = NULL ;
@ -573,9 +551,14 @@ void GenerateBitcoins(bool fGenerate, CWallet* pwallet, int nThreads)
if ( nThreads = = 0 | | ! fGenerate )
if ( nThreads = = 0 | | ! fGenerate )
return ;
return ;
CScript coinbaseScript ;
GetMainSignals ( ) . ScriptForMining ( coinbaseScript ) ;
//throw an error if no script was provided
if ( ! coinbaseScript . size ( ) )
throw std : : runtime_error ( " No coinbase script available (mining requires a wallet) " ) ;
minerThreads = new boost : : thread_group ( ) ;
minerThreads = new boost : : thread_group ( ) ;
for ( int i = 0 ; i < nThreads ; i + + )
for ( int i = 0 ; i < nThreads ; i + + )
minerThreads - > create_thread ( boost : : bind ( & BitcoinMiner , pwallet ) ) ;
minerThreads - > create_thread ( boost : : bind ( & BitcoinMiner , boost : : cref ( chainparams ) , coinbaseScrip t) ) ;
}
}
# endif // ENABLE_WALLET