1717#include " timedata.h"
1818#include " util.h"
1919#include " utilmoneystr.h"
20- #ifdef ENABLE_WALLET
21- #include " wallet/wallet.h"
22- #endif
20+ #include " validationinterface.h"
2321
2422#include < boost/thread.hpp>
2523#include < boost/tuple/tuple.hpp>
@@ -362,7 +360,6 @@ void IncrementExtraNonce(CBlock* pblock, CBlockIndex* pindexPrev, unsigned int&
362360 pblock->hashMerkleRoot = pblock->BuildMerkleTree ();
363361}
364362
365- #ifdef ENABLE_WALLET
366363// ////////////////////////////////////////////////////////////////////////////
367364//
368365// Internal miner
@@ -401,17 +398,7 @@ bool static ScanHash(const CBlockHeader *pblock, uint32_t& nNonce, uint256 *phas
401398 }
402399}
403400
404- CBlockTemplate* CreateNewBlockWithKey (CReserveKey& reservekey)
405- {
406- CPubKey pubkey;
407- if (!reservekey.GetReservedKey (pubkey))
408- return NULL ;
409-
410- CScript scriptPubKey = CScript () << ToByteVector (pubkey) << OP_CHECKSIG;
411- return CreateNewBlock (scriptPubKey);
412- }
413-
414- static bool ProcessBlockFound (CBlock* pblock, CWallet& wallet, CReserveKey& reservekey)
401+ static bool ProcessBlockFound (CBlock* pblock, const CChainParams& chainparams)
415402{
416403 LogPrintf (" %s\n " , pblock->ToString ());
417404 LogPrintf (" generated %s\n " , FormatMoney (pblock->vtx [0 ].vout [0 ].nValue ));
@@ -423,14 +410,8 @@ static bool ProcessBlockFound(CBlock* pblock, CWallet& wallet, CReserveKey& rese
423410 return error (" BitcoinMiner: generated block is stale" );
424411 }
425412
426- // Remove key from key pool
427- reservekey.KeepKey ();
428-
429- // Track how many getdata requests this block gets
430- {
431- LOCK (wallet.cs_wallet );
432- wallet.mapRequestCount [pblock->GetHash ()] = 0 ;
433- }
413+ // Inform about the new block
414+ GetMainSignals ().BlockFound (pblock->GetHash ());
434415
435416 // Process this block the same as if we had received it from another node
436417 CValidationState state;
@@ -440,18 +421,22 @@ static bool ProcessBlockFound(CBlock* pblock, CWallet& wallet, CReserveKey& rese
440421 return true ;
441422}
442423
443- void static BitcoinMiner (CWallet *pwallet )
424+ void static BitcoinMiner (const CChainParams& chainparams )
444425{
445426 LogPrintf (" BitcoinMiner started\n " );
446427 SetThreadPriority (THREAD_PRIORITY_LOWEST);
447428 RenameThread (" bitcoin-miner" );
448- const CChainParams& chainparams = Params ();
449429
450- // Each thread has its own key and counter
451- CReserveKey reservekey (pwallet);
452430 unsigned int nExtraNonce = 0 ;
453431
432+ boost::shared_ptr<CReserveScript> coinbaseScript;
433+ GetMainSignals ().ScriptForMining (coinbaseScript);
434+
454435 try {
436+ // throw an error if no script was provided
437+ if (!coinbaseScript->reserveScript .size ())
438+ throw std::runtime_error (" No coinbase script available (mining requires a wallet)" );
439+
455440 while (true ) {
456441 if (chainparams.MiningRequiresPeers ()) {
457442 // Busy-wait for the network to come online so we don't waste time mining
@@ -474,7 +459,7 @@ void static BitcoinMiner(CWallet *pwallet)
474459 unsigned int nTransactionsUpdatedLast = mempool.GetTransactionsUpdated ();
475460 CBlockIndex* pindexPrev = chainActive.Tip ();
476461
477- auto_ptr<CBlockTemplate> pblocktemplate (CreateNewBlockWithKey (reservekey ));
462+ auto_ptr<CBlockTemplate> pblocktemplate (CreateNewBlock (coinbaseScript-> reserveScript ));
478463 if (!pblocktemplate.get ())
479464 {
480465 LogPrintf (" Error in BitcoinMiner: Keypool ran out, please call keypoolrefill before restarting the mining thread\n " );
@@ -506,8 +491,9 @@ void static BitcoinMiner(CWallet *pwallet)
506491 SetThreadPriority (THREAD_PRIORITY_NORMAL);
507492 LogPrintf (" BitcoinMiner:\n " );
508493 LogPrintf (" proof-of-work found \n hash: %s \n target: %s\n " , hash.GetHex (), hashTarget.GetHex ());
509- ProcessBlockFound (pblock, *pwallet, reservekey );
494+ ProcessBlockFound (pblock, chainparams );
510495 SetThreadPriority (THREAD_PRIORITY_LOWEST);
496+ coinbaseScript->KeepScript ();
511497
512498 // In regression test mode, stop mining after a block is found.
513499 if (chainparams.MineBlocksOnDemand ())
@@ -551,7 +537,7 @@ void static BitcoinMiner(CWallet *pwallet)
551537 }
552538}
553539
554- void GenerateBitcoins (bool fGenerate , CWallet* pwallet, int nThreads )
540+ void GenerateBitcoins (bool fGenerate , int nThreads, const CChainParams& chainparams )
555541{
556542 static boost::thread_group* minerThreads = NULL ;
557543
@@ -575,7 +561,5 @@ void GenerateBitcoins(bool fGenerate, CWallet* pwallet, int nThreads)
575561
576562 minerThreads = new boost::thread_group ();
577563 for (int i = 0 ; i < nThreads; i++)
578- minerThreads->create_thread (boost::bind (&BitcoinMiner, pwallet ));
564+ minerThreads->create_thread (boost::bind (&BitcoinMiner, boost::cref (chainparams) ));
579565}
580-
581- #endif // ENABLE_WALLET
0 commit comments