Skip to content

Commit 5496253

Browse files
committed
add CReserveScript to allow modular script keeping/returning
- use one CReserveScript per mining thread
1 parent 087e65d commit 5496253

File tree

6 files changed

+42
-23
lines changed

6 files changed

+42
-23
lines changed

src/miner.cpp

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -421,15 +421,22 @@ static bool ProcessBlockFound(CBlock* pblock, const CChainParams& chainparams)
421421
return true;
422422
}
423423

424-
void static BitcoinMiner(const CChainParams& chainparams, const CScript& coinbaseScript)
424+
void static BitcoinMiner(const CChainParams& chainparams)
425425
{
426426
LogPrintf("BitcoinMiner started\n");
427427
SetThreadPriority(THREAD_PRIORITY_LOWEST);
428428
RenameThread("bitcoin-miner");
429429

430430
unsigned int nExtraNonce = 0;
431431

432+
boost::shared_ptr<CReserveScript> coinbaseScript;
433+
GetMainSignals().ScriptForMining(coinbaseScript);
434+
432435
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+
433440
while (true) {
434441
if (chainparams.MiningRequiresPeers()) {
435442
// Busy-wait for the network to come online so we don't waste time mining
@@ -452,7 +459,7 @@ void static BitcoinMiner(const CChainParams& chainparams, const CScript& coinbas
452459
unsigned int nTransactionsUpdatedLast = mempool.GetTransactionsUpdated();
453460
CBlockIndex* pindexPrev = chainActive.Tip();
454461

455-
auto_ptr<CBlockTemplate> pblocktemplate(CreateNewBlock(coinbaseScript));
462+
auto_ptr<CBlockTemplate> pblocktemplate(CreateNewBlock(coinbaseScript->reserveScript));
456463
if (!pblocktemplate.get())
457464
{
458465
LogPrintf("Error in BitcoinMiner: Keypool ran out, please call keypoolrefill before restarting the mining thread\n");
@@ -486,6 +493,7 @@ void static BitcoinMiner(const CChainParams& chainparams, const CScript& coinbas
486493
LogPrintf("proof-of-work found \n hash: %s \ntarget: %s\n", hash.GetHex(), hashTarget.GetHex());
487494
ProcessBlockFound(pblock, chainparams);
488495
SetThreadPriority(THREAD_PRIORITY_LOWEST);
496+
coinbaseScript->KeepScript();
489497

490498
// In regression test mode, stop mining after a block is found.
491499
if (chainparams.MineBlocksOnDemand())
@@ -551,14 +559,7 @@ void GenerateBitcoins(bool fGenerate, int nThreads, const CChainParams& chainpar
551559
if (nThreads == 0 || !fGenerate)
552560
return;
553561

554-
CScript coinbaseScript;
555-
GetMainSignals().ScriptForMining(coinbaseScript);
556-
557-
//throw an error if no script was provided
558-
if (!coinbaseScript.size())
559-
throw std::runtime_error("No coinbase script available (mining requires a wallet)");
560-
561562
minerThreads = new boost::thread_group();
562563
for (int i = 0; i < nThreads; i++)
563-
minerThreads->create_thread(boost::bind(&BitcoinMiner, boost::cref(chainparams), coinbaseScript));
564+
minerThreads->create_thread(boost::bind(&BitcoinMiner, boost::cref(chainparams)));
564565
}

src/rpcmining.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <stdint.h>
2121

2222
#include <boost/assign/list_of.hpp>
23+
#include <boost/shared_ptr.hpp>
2324

2425
#include "univalue/univalue.h"
2526

@@ -131,11 +132,11 @@ UniValue generate(const UniValue& params, bool fHelp)
131132
int nHeight = 0;
132133
int nGenerate = params[0].get_int();
133134

134-
CScript coinbaseScript;
135+
boost::shared_ptr<CReserveScript> coinbaseScript;
135136
GetMainSignals().ScriptForMining(coinbaseScript);
136137

137138
//throw an error if no script was provided
138-
if (!coinbaseScript.size())
139+
if (!coinbaseScript->reserveScript.size())
139140
throw JSONRPCError(RPC_INTERNAL_ERROR, "No coinbase script available (mining requires a wallet)");
140141

141142
{ // Don't keep cs_main locked
@@ -148,7 +149,7 @@ UniValue generate(const UniValue& params, bool fHelp)
148149
UniValue blockHashes(UniValue::VARR);
149150
while (nHeight < nHeightEnd)
150151
{
151-
auto_ptr<CBlockTemplate> pblocktemplate(CreateNewBlock(coinbaseScript));
152+
auto_ptr<CBlockTemplate> pblocktemplate(CreateNewBlock(coinbaseScript->reserveScript));
152153
if (!pblocktemplate.get())
153154
throw JSONRPCError(RPC_INTERNAL_ERROR, "Couldn't create new block");
154155
CBlock *pblock = &pblocktemplate->block;
@@ -166,6 +167,9 @@ UniValue generate(const UniValue& params, bool fHelp)
166167
throw JSONRPCError(RPC_INTERNAL_ERROR, "ProcessNewBlock, block not accepted");
167168
++nHeight;
168169
blockHashes.push_back(pblock->GetHash().GetHex());
170+
171+
//mark script as important because it was used at least for one coinbase output
172+
coinbaseScript->KeepScript();
169173
}
170174
return blockHashes;
171175
}

src/script/script.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -609,4 +609,13 @@ class CScript : public std::vector<unsigned char>
609609
}
610610
};
611611

612+
class CReserveScript
613+
{
614+
public:
615+
CScript reserveScript;
616+
virtual void KeepScript() {}
617+
CReserveScript() {}
618+
virtual ~CReserveScript() {}
619+
};
620+
612621
#endif // BITCOIN_SCRIPT_SCRIPT_H

src/validationinterface.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@
77
#define BITCOIN_VALIDATIONINTERFACE_H
88

99
#include <boost/signals2/signal.hpp>
10+
#include <boost/shared_ptr.hpp>
1011

1112
class CBlock;
1213
struct CBlockLocator;
13-
class CScript;
14+
class CReserveScript;
1415
class CTransaction;
1516
class CValidationInterface;
1617
class CValidationState;
@@ -35,7 +36,7 @@ class CValidationInterface {
3536
virtual void Inventory(const uint256 &hash) {}
3637
virtual void ResendWalletTransactions(int64_t nBestBlockTime) {}
3738
virtual void BlockChecked(const CBlock&, const CValidationState&) {}
38-
virtual void GetScriptForMining(CScript &script) {};
39+
virtual void GetScriptForMining(boost::shared_ptr<CReserveScript>&) {};
3940
virtual void UpdateRequestCount(const CBlock&) {};
4041
friend void ::RegisterValidationInterface(CValidationInterface*);
4142
friend void ::UnregisterValidationInterface(CValidationInterface*);
@@ -56,7 +57,7 @@ struct CMainSignals {
5657
/** Notifies listeners of a block validation result */
5758
boost::signals2::signal<void (const CBlock&, const CValidationState&)> BlockChecked;
5859
/** Notifies listeners that a key for mining is required (coinbase) */
59-
boost::signals2::signal<void (CScript &script)> ScriptForMining;
60+
boost::signals2::signal<void (boost::shared_ptr<CReserveScript>&)> ScriptForMining;
6061
/** Notifies listeners that a block has been successfully mined */
6162
boost::signals2::signal<void (const CBlock&)> BlockFound;
6263
};

src/wallet/wallet.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2583,14 +2583,15 @@ void CWallet::UpdatedTransaction(const uint256 &hashTx)
25832583
}
25842584
}
25852585

2586-
void CWallet::GetScriptForMining(CScript &script)
2586+
void CWallet::GetScriptForMining(boost::shared_ptr<CReserveScript> &script)
25872587
{
2588-
CReserveKey reservekey(this);
2588+
boost::shared_ptr<CReserveKey> rKey(new CReserveKey(this));
25892589
CPubKey pubkey;
2590-
if (!reservekey.GetReservedKey(pubkey))
2590+
if (!rKey->GetReservedKey(pubkey))
25912591
return;
2592-
script = CScript() << ToByteVector(pubkey) << OP_CHECKSIG;
2593-
reservekey.KeepKey();
2592+
2593+
script = rKey;
2594+
script->reserveScript = CScript() << ToByteVector(pubkey) << OP_CHECKSIG;
25942595
}
25952596

25962597
void CWallet::LockCoin(COutPoint& output)

src/wallet/wallet.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
#include <utility>
2929
#include <vector>
3030

31+
#include <boost/shared_ptr.hpp>
32+
3133
/**
3234
* Settings
3335
*/
@@ -680,7 +682,7 @@ class CWallet : public CCryptoKeyStore, public CValidationInterface
680682
}
681683
}
682684

683-
void GetScriptForMining(CScript &script);
685+
void GetScriptForMining(boost::shared_ptr<CReserveScript> &script);
684686
void UpdateRequestCount(const CBlock& block)
685687
{
686688
LOCK(cs_wallet);
@@ -742,7 +744,7 @@ class CWallet : public CCryptoKeyStore, public CValidationInterface
742744
};
743745

744746
/** A key allocated from the key pool. */
745-
class CReserveKey
747+
class CReserveKey : public CReserveScript
746748
{
747749
protected:
748750
CWallet* pwallet;
@@ -763,6 +765,7 @@ class CReserveKey
763765
void ReturnKey();
764766
bool GetReservedKey(CPubKey &pubkey);
765767
void KeepKey();
768+
void KeepScript() { KeepKey(); }
766769
};
767770

768771

0 commit comments

Comments
 (0)