aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/miner.cpp86
-rw-r--r--src/miner.h2
-rw-r--r--src/rpcclient.cpp1
-rw-r--r--src/rpcmining.cpp91
-rw-r--r--src/rpcserver.cpp1
-rw-r--r--src/rpcserver.h1
6 files changed, 101 insertions, 81 deletions
diff --git a/src/miner.cpp b/src/miner.cpp
index 44661fd13e..fdbc47a1c3 100644
--- a/src/miner.cpp
+++ b/src/miner.cpp
@@ -372,8 +372,7 @@ bool static ScanHash(CBlockHeader *pblock, uint256 *phash)
{
while (true) {
pblock->nNonce++;
-
- *phash = (CHashWriter(SER_GETHASH, 0) << *pblock).GetHash();
+ *phash = pblock->GetHash();
// Return the nonce if the hash has at least some zero bits,
// caller will check if it has enough to reach the target
@@ -425,6 +424,56 @@ static bool ProcessBlockFound(CBlock* pblock, CWallet& wallet, CReserveKey& rese
return true;
}
+bool static ScanLoop(CBlock *pblock, CBlockIndex *pindexPrev, CWallet *pwallet, CReserveKey& reservekey)
+{
+ UpdateTime(pblock, pindexPrev);
+ arith_uint256 hashTarget = arith_uint256().SetCompact(pblock->nBits);
+
+ uint256 hash;
+ if (ScanHash(pblock, &hash)) {
+ if (UintToArith256(hash) <= hashTarget) {
+ // Found a solution
+ SetThreadPriority(THREAD_PRIORITY_NORMAL);
+ LogPrintf("BitcoinMiner:\n");
+ LogPrintf("proof-of-work found \n hash: %s \ntarget: %s\n", hash.GetHex(), hashTarget.GetHex());
+ ProcessBlockFound(pblock, *pwallet, reservekey);
+ SetThreadPriority(THREAD_PRIORITY_LOWEST);
+
+ return true;
+ }
+ }
+
+ return false;
+}
+
+bool MineBlock(CWallet *pwallet, uint256& hash)
+{
+ CReserveKey reservekey(pwallet);
+ unsigned int nExtraNonce = 0;
+
+ while (true) {
+ CBlockIndex *pindexPrev = chainActive.Tip(); // Actually needs cs_main...
+
+ auto_ptr<CBlockTemplate> pblocktemplate(CreateNewBlockWithKey(reservekey));
+ if (!pblocktemplate.get()) {
+ return false;
+ }
+
+ CBlock *pblock = &pblocktemplate->block;
+ IncrementExtraNonce(pblock, pindexPrev, nExtraNonce);
+
+ while (true) {
+ if (ScanLoop(pblock, pindexPrev, pwallet, reservekey)) {
+ hash = pblock->GetHash();
+ return true;
+ }
+ boost::this_thread::interruption_point();
+ if (pblock->nNonce >= 0xffff0000)
+ break;
+ }
+ }
+}
+
void static BitcoinMiner(CWallet *pwallet)
{
LogPrintf("BitcoinMiner started\n");
@@ -448,7 +497,7 @@ void static BitcoinMiner(CWallet *pwallet)
// Create new block
//
unsigned int nTransactionsUpdatedLast = mempool.GetTransactionsUpdated();
- CBlockIndex* pindexPrev = chainActive.Tip();
+ CBlockIndex* pindexPrev = chainActive.Tip(); // Actually needs cs_main...
auto_ptr<CBlockTemplate> pblocktemplate(CreateNewBlockWithKey(reservekey));
if (!pblocktemplate.get())
@@ -466,30 +515,10 @@ void static BitcoinMiner(CWallet *pwallet)
// Search
//
int64_t nStart = GetTime();
- arith_uint256 hashTarget = arith_uint256().SetCompact(pblock->nBits);
- uint256 hash;
while (true) {
// Check if something found
- if (ScanHash(pblock, &hash))
- {
- if (UintToArith256(hash) <= hashTarget)
- {
- // Found a solution
- assert(hash == pblock->GetHash());
-
- SetThreadPriority(THREAD_PRIORITY_NORMAL);
- LogPrintf("BitcoinMiner:\n");
- LogPrintf("proof-of-work found \n hash: %s \ntarget: %s\n", hash.GetHex(), hashTarget.GetHex());
- ProcessBlockFound(pblock, *pwallet, reservekey);
- SetThreadPriority(THREAD_PRIORITY_LOWEST);
-
- // In regression test mode, stop mining after a block is found.
- if (Params().MineBlocksOnDemand())
- throw boost::thread_interrupted();
-
- break;
- }
- }
+ if (ScanLoop(pblock, pindexPrev, pwallet, reservekey))
+ break;
// Check for stop or if block needs to be rebuilt
boost::this_thread::interruption_point();
@@ -503,13 +532,6 @@ void static BitcoinMiner(CWallet *pwallet)
if (pindexPrev != chainActive.Tip())
break;
- // Update nTime every few seconds
- UpdateTime(pblock, pindexPrev);
- if (Params().AllowMinDifficultyBlocks())
- {
- // Changing pblock->nTime can change work required on testnet:
- hashTarget.SetCompact(pblock->nBits);
- }
}
}
}
diff --git a/src/miner.h b/src/miner.h
index 5d5c9c86c7..549658ec17 100644
--- a/src/miner.h
+++ b/src/miner.h
@@ -24,6 +24,8 @@ struct CBlockTemplate
/** Run the miner threads */
void GenerateBitcoins(bool fGenerate, CWallet* pwallet, int nThreads);
+/** Create a single block */
+bool MineBlock(CWallet *pwallet, uint256& hash);
/** Generate a new block, without valid proof-of-work */
CBlockTemplate* CreateNewBlock(const CScript& scriptPubKeyIn);
CBlockTemplate* CreateNewBlockWithKey(CReserveKey& reservekey);
diff --git a/src/rpcclient.cpp b/src/rpcclient.cpp
index a45ea9839b..428e1049dc 100644
--- a/src/rpcclient.cpp
+++ b/src/rpcclient.cpp
@@ -29,6 +29,7 @@ static const CRPCConvertParam vRPCConvertParams[] =
{ "getaddednodeinfo", 0 },
{ "setgenerate", 0 },
{ "setgenerate", 1 },
+ { "generate", 0 },
{ "getnetworkhashps", 0 },
{ "getnetworkhashps", 1 },
{ "sendtoaddress", 1 },
diff --git a/src/rpcmining.cpp b/src/rpcmining.cpp
index fcba7e222d..49c5c3ca58 100644
--- a/src/rpcmining.cpp
+++ b/src/rpcmining.cpp
@@ -113,6 +113,45 @@ Value getgenerate(const Array& params, bool fHelp)
return GetBoolArg("-gen", false);
}
+Value generate(const Array& params, bool fHelp)
+{
+ if (fHelp || params.size() < 1 || params.size() > 1)
+ throw runtime_error(
+ "generate numblocks\n"
+ "\nMine blocks immediately (before the RPC call returns)\n"
+ "1. numblocks (numeric) How many blocks are generated immediately.\n"
+ "\nResult\n"
+ "[ blockhashes ] (array) hashes of blocks generated\n"
+ "\nExamples:\n"
+ "\nGenerate 11 blocks\n"
+ + HelpExampleCli("generate", "11")
+ );
+
+ if (pwalletMain == NULL)
+ throw JSONRPCError(RPC_METHOD_NOT_FOUND, "Method not found (disabled)");
+
+ int nHeightStart = 0;
+ int nHeightEnd = 0;
+ int nHeight = 0;
+ int nGenerate = params[0].get_int();
+
+ { // Don't keep cs_main locked
+ LOCK(cs_main);
+ nHeightStart = chainActive.Height();
+ nHeight = nHeightStart;
+ nHeightEnd = nHeightStart+nGenerate;
+ }
+ Array blockHashes;
+ while (nHeight < nHeightEnd) {
+ uint256 hash;
+ if (!MineBlock(pwalletMain, hash))
+ throw JSONRPCError(RPC_INTERNAL_ERROR, "Wallet keypool empty");
+
+ ++nHeight;
+ blockHashes.push_back(hash.GetHex());
+ }
+ return blockHashes;
+}
Value setgenerate(const Array& params, bool fHelp)
{
@@ -125,9 +164,6 @@ Value setgenerate(const Array& params, bool fHelp)
"\nArguments:\n"
"1. generate (boolean, required) Set to true to turn on generation, off to turn off.\n"
"2. genproclimit (numeric, optional) Set the processor limit for when generation is on. Can be -1 for unlimited.\n"
- " Note: in -regtest mode, genproclimit controls how many blocks are generated immediately.\n"
- "\nResult\n"
- "[ blockhashes ] (array, -regtest only) hashes of blocks generated\n"
"\nExamples:\n"
"\nSet the generation on with a limit of one processor\n"
+ HelpExampleCli("setgenerate", "true 1") +
@@ -154,52 +190,9 @@ Value setgenerate(const Array& params, bool fHelp)
fGenerate = false;
}
- // -regtest mode: don't return until nGenProcLimit blocks are generated
- if (fGenerate && Params().MineBlocksOnDemand())
- {
- int nHeightStart = 0;
- int nHeightEnd = 0;
- int nHeight = 0;
- int nGenerate = (nGenProcLimit > 0 ? nGenProcLimit : 1);
- CReserveKey reservekey(pwalletMain);
-
- { // Don't keep cs_main locked
- LOCK(cs_main);
- nHeightStart = chainActive.Height();
- nHeight = nHeightStart;
- nHeightEnd = nHeightStart+nGenerate;
- }
- unsigned int nExtraNonce = 0;
- Array blockHashes;
- while (nHeight < nHeightEnd)
- {
- auto_ptr<CBlockTemplate> pblocktemplate(CreateNewBlockWithKey(reservekey));
- if (!pblocktemplate.get())
- throw JSONRPCError(RPC_INTERNAL_ERROR, "Wallet keypool empty");
- CBlock *pblock = &pblocktemplate->block;
- {
- LOCK(cs_main);
- IncrementExtraNonce(pblock, chainActive.Tip(), nExtraNonce);
- }
- while (!CheckProofOfWork(pblock->GetHash(), pblock->nBits, Params().GetConsensus())) {
- // Yes, there is a chance every nonce could fail to satisfy the -regtest
- // target -- 1 in 2^(2^32). That ain't gonna happen.
- ++pblock->nNonce;
- }
- CValidationState state;
- if (!ProcessNewBlock(state, NULL, pblock))
- throw JSONRPCError(RPC_INTERNAL_ERROR, "ProcessNewBlock, block not accepted");
- ++nHeight;
- blockHashes.push_back(pblock->GetHash().GetHex());
- }
- return blockHashes;
- }
- else // Not -regtest: start generate thread, return immediately
- {
- mapArgs["-gen"] = (fGenerate ? "1" : "0");
- mapArgs ["-genproclimit"] = itostr(nGenProcLimit);
- GenerateBitcoins(fGenerate, pwalletMain, nGenProcLimit);
- }
+ mapArgs["-gen"] = (fGenerate ? "1" : "0");
+ mapArgs ["-genproclimit"] = itostr(nGenProcLimit);
+ GenerateBitcoins(fGenerate, pwalletMain, nGenProcLimit);
return Value::null;
}
diff --git a/src/rpcserver.cpp b/src/rpcserver.cpp
index d30fa32eb4..0fd7769a19 100644
--- a/src/rpcserver.cpp
+++ b/src/rpcserver.cpp
@@ -312,6 +312,7 @@ static const CRPCCommand vRPCCommands[] =
/* Coin generation */
{ "generating", "getgenerate", &getgenerate, true, false },
{ "generating", "setgenerate", &setgenerate, true, false },
+ { "generating", "generate", &generate, true, false },
#endif
/* Raw transactions */
diff --git a/src/rpcserver.h b/src/rpcserver.h
index 7011d41fc1..e7aaed8bdf 100644
--- a/src/rpcserver.h
+++ b/src/rpcserver.h
@@ -162,6 +162,7 @@ extern json_spirit::Value importwallet(const json_spirit::Array& params, bool fH
extern json_spirit::Value getgenerate(const json_spirit::Array& params, bool fHelp); // in rpcmining.cpp
extern json_spirit::Value setgenerate(const json_spirit::Array& params, bool fHelp);
+extern json_spirit::Value generate(const json_spirit::Array& params, bool fHelp);
extern json_spirit::Value getnetworkhashps(const json_spirit::Array& params, bool fHelp);
extern json_spirit::Value getmininginfo(const json_spirit::Array& params, bool fHelp);
extern json_spirit::Value prioritisetransaction(const json_spirit::Array& params, bool fHelp);