1 // Copyright (c) 2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2013 The Bitcoin developers
3 // Distributed under the MIT/X11 software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
6 #include "bitcoinrpc.h"
7 #include "chainparams.h"
17 #include "json/json_spirit_utils.h"
18 #include "json/json_spirit_value.h"
20 using namespace json_spirit;
23 // Key used by getwork/getblocktemplate miners.
24 // Allocated in InitRPCMining, free'd in ShutdownRPCMining
25 static CReserveKey* pMiningKey = NULL;
32 // getwork/getblocktemplate mining rewards paid here:
33 pMiningKey = new CReserveKey(pwalletMain);
36 void ShutdownRPCMining()
41 delete pMiningKey; pMiningKey = NULL;
44 // Return average network hashes per second based on the last 'lookup' blocks,
45 // or from the last difficulty change if 'lookup' is nonpositive.
46 // If 'height' is nonnegative, compute the estimate at the time when a given block was found.
47 Value GetNetworkHashPS(int lookup, int height) {
48 CBlockIndex *pb = chainActive[height];
50 if (pb == NULL || !pb->nHeight)
53 // If lookup is -1, then use blocks since last difficulty change.
55 lookup = pb->nHeight % 2016 + 1;
57 // If lookup is larger than chain, then set it to chain length.
58 if (lookup > pb->nHeight)
61 CBlockIndex *pb0 = pb;
62 int64_t minTime = pb0->GetBlockTime();
63 int64_t maxTime = minTime;
64 for (int i = 0; i < lookup; i++) {
66 int64_t time = pb0->GetBlockTime();
67 minTime = std::min(time, minTime);
68 maxTime = std::max(time, maxTime);
71 // In case there's a situation where minTime == maxTime, we don't want a divide by zero exception.
72 if (minTime == maxTime)
75 uint256 workDiff = pb->nChainWork - pb0->nChainWork;
76 int64_t timeDiff = maxTime - minTime;
78 return (boost::int64_t)(workDiff.getdouble() / timeDiff);
81 Value getnetworkhashps(const Array& params, bool fHelp)
83 if (fHelp || params.size() > 2)
85 "getnetworkhashps ( blocks height )\n"
86 "\nReturns the estimated network hashes per second based on the last n blocks.\n"
87 "Pass in [blocks] to override # of blocks, -1 specifies since last difficulty change.\n"
88 "Pass in [height] to estimate the network speed at the time when a certain block was found.\n"
90 "1. blocks (numeric, optional, default=120) The number of blocks, or -1 for blocks since last difficulty change.\n"
91 "2. height (numeric, optional, default=-1) To estimate at the time of the given height.\n"
93 "x (numeric) Hashes per second estimated\n"
95 + HelpExampleCli("getnetworkhashps", "")
96 + HelpExampleRpc("getnetworkhashps", "")
99 return GetNetworkHashPS(params.size() > 0 ? params[0].get_int() : 120, params.size() > 1 ? params[1].get_int() : -1);
103 Value getgenerate(const Array& params, bool fHelp)
105 if (fHelp || params.size() != 0)
108 "\nReturn if the server is set to generate coins or not. The default is false.\n"
109 "It is set with the command line argument -gen (or bitcoin.conf setting gen)\n"
110 "It can also be set with the setgenerate call.\n"
112 "true|false (boolean) If the server is set to generate coins or not\n"
114 + HelpExampleCli("getgenerate", "")
115 + HelpExampleRpc("getgenerate", "")
121 return GetBoolArg("-gen", false);
125 Value setgenerate(const Array& params, bool fHelp)
127 if (fHelp || params.size() < 1 || params.size() > 2)
129 "setgenerate generate ( genproclimit )\n"
130 "\nSet 'generate' true or false to turn generation on or off.\n"
131 "Generation is limited to 'genproclimit' processors, -1 is unlimited.\n"
132 "See the getgenerate call for the current setting.\n"
134 "1. generate (boolean, required) Set to true to turn on generation, off to turn off.\n"
135 "2. genproclimit (numeric, optional) Set the processor limit for when generation is on. Can be -1 for unlimited.\n"
136 " Note: in -regtest mode, genproclimit controls how many blocks are generated immediately.\n"
138 "\nSet the generation on with a limit of one processor\n"
139 + HelpExampleCli("setgenerate", "true 1") +
140 "\nCheck the setting\n"
141 + HelpExampleCli("getgenerate", "") +
142 "\nTurn off generation\n"
143 + HelpExampleCli("setgenerate", "false") +
145 + HelpExampleRpc("setgenerate", "true, 1")
148 if (pwalletMain == NULL)
149 throw JSONRPCError(RPC_METHOD_NOT_FOUND, "Method not found (disabled)");
151 bool fGenerate = true;
152 if (params.size() > 0)
153 fGenerate = params[0].get_bool();
155 int nGenProcLimit = -1;
156 if (params.size() > 1)
158 nGenProcLimit = params[1].get_int();
159 if (nGenProcLimit == 0)
163 // -regtest mode: don't return until nGenProcLimit blocks are generated
164 if (fGenerate && Params().NetworkID() == CChainParams::REGTEST)
166 int nHeightStart = 0;
169 int nGenerate = (nGenProcLimit > 0 ? nGenProcLimit : 1);
170 { // Don't keep cs_main locked
172 nHeightStart = chainActive.Height();
173 nHeight = nHeightStart;
174 nHeightEnd = nHeightStart+nGenerate;
176 int nHeightLast = -1;
177 while (nHeight < nHeightEnd)
179 if (nHeightLast != nHeight)
181 nHeightLast = nHeight;
182 GenerateBitcoins(fGenerate, pwalletMain, 1);
185 { // Don't keep cs_main locked
187 nHeight = chainActive.Height();
191 else // Not -regtest: start generate thread, return immediately
193 mapArgs["-gen"] = (fGenerate ? "1" : "0");
194 GenerateBitcoins(fGenerate, pwalletMain, nGenProcLimit);
201 Value gethashespersec(const Array& params, bool fHelp)
203 if (fHelp || params.size() != 0)
206 "\nReturns a recent hashes per second performance measurement while generating.\n"
207 "See the getgenerate and setgenerate calls to turn generation on and off.\n"
209 "n (numeric) The recent hashes per second when generation is on (will return 0 if generation is off)\n"
211 + HelpExampleCli("gethashespersec", "")
212 + HelpExampleRpc("gethashespersec", "")
215 if (GetTimeMillis() - nHPSTimerStart > 8000)
216 return (boost::int64_t)0;
217 return (boost::int64_t)dHashesPerSec;
221 Value getmininginfo(const Array& params, bool fHelp)
223 if (fHelp || params.size() != 0)
226 "\nReturns a json object containing mining-related information."
229 " \"blocks\": nnn, (numeric) The current block\n"
230 " \"currentblocksize\": nnn, (numeric) The last block size\n"
231 " \"currentblocktx\": nnn, (numeric) The last block transaction\n"
232 " \"difficulty\": xxx.xxxxx (numeric) The current difficulty\n"
233 " \"errors\": \"...\" (string) Current errors\n"
234 " \"generate\": true|false (boolean) If the generation is on or off (see getgenerate or setgenerate calls)\n"
235 " \"genproclimit\": n (numeric) The processor limit for generation. -1 if no generation. (see getgenerate or setgenerate calls)\n"
236 " \"hashespersec\": n (numeric) The hashes per second of the generation, or 0 if no generation.\n"
237 " \"pooledtx\": n (numeric) The size of the mem pool\n"
238 " \"testnet\": true|false (boolean) If using testnet or not\n"
241 + HelpExampleCli("getmininginfo", "")
242 + HelpExampleRpc("getmininginfo", "")
246 obj.push_back(Pair("blocks", (int)chainActive.Height()));
247 obj.push_back(Pair("currentblocksize", (uint64_t)nLastBlockSize));
248 obj.push_back(Pair("currentblocktx", (uint64_t)nLastBlockTx));
249 obj.push_back(Pair("difficulty", (double)GetDifficulty()));
250 obj.push_back(Pair("errors", GetWarnings("statusbar")));
251 obj.push_back(Pair("generate", getgenerate(params, false)));
252 obj.push_back(Pair("genproclimit", (int)GetArg("-genproclimit", -1)));
253 obj.push_back(Pair("hashespersec", gethashespersec(params, false)));
254 obj.push_back(Pair("networkhashps", getnetworkhashps(params, false)));
255 obj.push_back(Pair("pooledtx", (uint64_t)mempool.size()));
256 obj.push_back(Pair("testnet", TestNet()));
261 Value getwork(const Array& params, bool fHelp)
263 if (fHelp || params.size() > 1)
265 "getwork ( \"data\" )\n"
266 "\nIf 'data' is not specified, it returns the formatted hash data to work on.\n"
267 "If 'data' is specified, tries to solve the block and returns true if it was successful.\n"
269 "1. \"data\" (string, optional) The hex encoded data to solve\n"
270 "\nResult (when 'data' is not specified):\n"
272 " \"midstate\" : \"xxxx\", (string) The precomputed hash state after hashing the first half of the data (DEPRECATED)\n" // deprecated
273 " \"data\" : \"xxxxx\", (string) The block data\n"
274 " \"hash1\" : \"xxxxx\", (string) The formatted hash buffer for second hash (DEPRECATED)\n" // deprecated
275 " \"target\" : \"xxxx\" (string) The little endian hash target\n"
277 "\nResult (when 'data' is specified):\n"
278 "true|false (boolean) If solving the block specified in the 'data' was successfull\n"
280 + HelpExampleCli("getwork", "")
281 + HelpExampleRpc("getwork", "")
285 throw JSONRPCError(RPC_CLIENT_NOT_CONNECTED, "Bitcoin is not connected!");
287 if (IsInitialBlockDownload())
288 throw JSONRPCError(RPC_CLIENT_IN_INITIAL_DOWNLOAD, "Bitcoin is downloading blocks...");
290 typedef map<uint256, pair<CBlock*, CScript> > mapNewBlock_t;
291 static mapNewBlock_t mapNewBlock; // FIXME: thread safety
292 static vector<CBlockTemplate*> vNewBlockTemplate;
294 if (params.size() == 0)
297 static unsigned int nTransactionsUpdatedLast;
298 static CBlockIndex* pindexPrev;
299 static int64_t nStart;
300 static CBlockTemplate* pblocktemplate;
301 if (pindexPrev != chainActive.Tip() ||
302 (mempool.GetTransactionsUpdated() != nTransactionsUpdatedLast && GetTime() - nStart > 60))
304 if (pindexPrev != chainActive.Tip())
306 // Deallocate old blocks since they're obsolete now
308 BOOST_FOREACH(CBlockTemplate* pblocktemplate, vNewBlockTemplate)
309 delete pblocktemplate;
310 vNewBlockTemplate.clear();
313 // Clear pindexPrev so future getworks make a new block, despite any failures from here on
316 // Store the pindexBest used before CreateNewBlock, to avoid races
317 nTransactionsUpdatedLast = mempool.GetTransactionsUpdated();
318 CBlockIndex* pindexPrevNew = chainActive.Tip();
322 pblocktemplate = CreateNewBlockWithKey(*pMiningKey);
324 throw JSONRPCError(RPC_OUT_OF_MEMORY, "Out of memory");
325 vNewBlockTemplate.push_back(pblocktemplate);
327 // Need to update only after we know CreateNewBlock succeeded
328 pindexPrev = pindexPrevNew;
330 CBlock* pblock = &pblocktemplate->block; // pointer for convenience
333 UpdateTime(*pblock, pindexPrev);
336 // Update nExtraNonce
337 static unsigned int nExtraNonce = 0;
338 IncrementExtraNonce(pblock, pindexPrev, nExtraNonce);
341 mapNewBlock[pblock->hashMerkleRoot] = make_pair(pblock, pblock->vtx[0].vin[0].scriptSig);
343 // Pre-build hash buffers
347 FormatHashBuffers(pblock, pmidstate, pdata, phash1);
349 uint256 hashTarget = CBigNum().SetCompact(pblock->nBits).getuint256();
352 result.push_back(Pair("midstate", HexStr(BEGIN(pmidstate), END(pmidstate)))); // deprecated
353 result.push_back(Pair("data", HexStr(BEGIN(pdata), END(pdata))));
354 result.push_back(Pair("hash1", HexStr(BEGIN(phash1), END(phash1)))); // deprecated
355 result.push_back(Pair("target", HexStr(BEGIN(hashTarget), END(hashTarget))));
361 vector<unsigned char> vchData = ParseHex(params[0].get_str());
362 if (vchData.size() != 128)
363 throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter");
364 CBlock* pdata = (CBlock*)&vchData[0];
367 for (int i = 0; i < 128/4; i++)
368 ((unsigned int*)pdata)[i] = ByteReverse(((unsigned int*)pdata)[i]);
371 if (!mapNewBlock.count(pdata->hashMerkleRoot))
373 CBlock* pblock = mapNewBlock[pdata->hashMerkleRoot].first;
375 pblock->nTime = pdata->nTime;
376 pblock->nNonce = pdata->nNonce;
377 pblock->vtx[0].vin[0].scriptSig = mapNewBlock[pdata->hashMerkleRoot].second;
378 pblock->hashMerkleRoot = pblock->BuildMerkleTree();
380 assert(pwalletMain != NULL);
381 return CheckWork(pblock, *pwalletMain, *pMiningKey);
386 Value getblocktemplate(const Array& params, bool fHelp)
388 if (fHelp || params.size() > 1)
390 "getblocktemplate ( \"jsonrequestobject\" )\n"
391 "\nIf the request parameters include a 'mode' key, that is used to explicitly select between the default 'template' request or a 'proposal'.\n"
392 "It returns data needed to construct a block to work on.\n"
393 "See https://en.bitcoin.it/wiki/BIP_0022 for full specification.\n"
396 "1. \"jsonrequestobject\" (string, optional) A json object in the following spec\n"
398 " \"mode\":\"template\" (string, optional) This must be set to \"template\" or omitted\n"
399 " \"capabilities\":[ (array, optional) A list of strings\n"
400 " \"support\" (string) client side supported feature, 'longpoll', 'coinbasetxn', 'coinbasevalue', 'proposal', 'serverlist', 'workid'\n"
408 " \"version\" : n, (numeric) The block version\n"
409 " \"previousblockhash\" : \"xxxx\", (string) The hash of current highest block\n"
410 " \"transactions\" : [ (array) contents of non-coinbase transactions that should be included in the next block\n"
412 " \"data\" : \"xxxx\", (string) transaction data encoded in hexadecimal (byte-for-byte)\n"
413 " \"hash\" : \"xxxx\", (string) hash/id encoded in little-endian hexadecimal\n"
414 " \"depends\" : [ (array) array of numbers \n"
415 " n (numeric) transactions before this one (by 1-based index in 'transactions' list) that must be present in the final block if this one is\n"
418 " \"fee\": n, (numeric) difference in value between transaction inputs and outputs (in Satoshis); for coinbase transactions, this is a negative Number of the total collected block fees (ie, not including the block subsidy); if key is not present, fee is unknown and clients MUST NOT assume there isn't one\n"
419 " \"sigops\" : n, (numeric) total number of SigOps, as counted for purposes of block limits; if key is not present, sigop count is unknown and clients MUST NOT assume there aren't any\n"
420 " \"required\" : true|false (boolean) if provided and true, this transaction must be in the final block\n"
424 " \"coinbaseaux\" : { (json object) data that should be included in the coinbase's scriptSig content\n"
425 " \"flags\" : \"flags\" (string) \n"
427 " \"coinbasevalue\" : n, (numeric) maximum allowable input to coinbase transaction, including the generation award and transaction fees (in Satoshis)\n"
428 " \"coinbasetxn\" : { ... }, (json object) information for coinbase transaction\n"
429 " \"target\" : \"xxxx\", (string) The hash target\n"
430 " \"mintime\" : xxx, (numeric) The minimum timestamp appropriate for next block time in seconds since epoch (Jan 1 1970 GMT)\n"
431 " \"mutable\" : [ (array of string) list of ways the block template may be changed \n"
432 " \"value\" (string) A way the block template may be changed, e.g. 'time', 'transactions', 'prevblock'\n"
435 " \"noncerange\" : \"00000000ffffffff\", (string) A range of valid nonces\n"
436 " \"sigoplimit\" : n, (numeric) limit of sigops in blocks\n"
437 " \"sizelimit\" : n, (numeric) limit of block size\n"
438 " \"curtime\" : ttt, (numeric) current timestamp in seconds since epoch (Jan 1 1970 GMT)\n"
439 " \"bits\" : \"xxx\", (string) compressed target of next block\n"
440 " \"height\" : n (numeric) The height of the next block\n"
444 + HelpExampleCli("getblocktemplate", "")
445 + HelpExampleRpc("getblocktemplate", "")
448 std::string strMode = "template";
449 if (params.size() > 0)
451 const Object& oparam = params[0].get_obj();
452 const Value& modeval = find_value(oparam, "mode");
453 if (modeval.type() == str_type)
454 strMode = modeval.get_str();
455 else if (modeval.type() == null_type)
460 throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid mode");
463 if (strMode != "template")
464 throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid mode");
467 throw JSONRPCError(RPC_CLIENT_NOT_CONNECTED, "Bitcoin is not connected!");
469 if (IsInitialBlockDownload())
470 throw JSONRPCError(RPC_CLIENT_IN_INITIAL_DOWNLOAD, "Bitcoin is downloading blocks...");
473 static unsigned int nTransactionsUpdatedLast;
474 static CBlockIndex* pindexPrev;
475 static int64_t nStart;
476 static CBlockTemplate* pblocktemplate;
477 if (pindexPrev != chainActive.Tip() ||
478 (mempool.GetTransactionsUpdated() != nTransactionsUpdatedLast && GetTime() - nStart > 5))
480 // Clear pindexPrev so future calls make a new block, despite any failures from here on
483 // Store the pindexBest used before CreateNewBlock, to avoid races
484 nTransactionsUpdatedLast = mempool.GetTransactionsUpdated();
485 CBlockIndex* pindexPrevNew = chainActive.Tip();
491 delete pblocktemplate;
492 pblocktemplate = NULL;
494 CScript scriptDummy = CScript() << OP_TRUE;
495 pblocktemplate = CreateNewBlock(scriptDummy);
497 throw JSONRPCError(RPC_OUT_OF_MEMORY, "Out of memory");
499 // Need to update only after we know CreateNewBlock succeeded
500 pindexPrev = pindexPrevNew;
502 CBlock* pblock = &pblocktemplate->block; // pointer for convenience
505 UpdateTime(*pblock, pindexPrev);
509 map<uint256, int64_t> setTxIndex;
511 BOOST_FOREACH (CTransaction& tx, pblock->vtx)
513 uint256 txHash = tx.GetHash();
514 setTxIndex[txHash] = i++;
521 CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION);
523 entry.push_back(Pair("data", HexStr(ssTx.begin(), ssTx.end())));
525 entry.push_back(Pair("hash", txHash.GetHex()));
528 BOOST_FOREACH (const CTxIn &in, tx.vin)
530 if (setTxIndex.count(in.prevout.hash))
531 deps.push_back(setTxIndex[in.prevout.hash]);
533 entry.push_back(Pair("depends", deps));
535 int index_in_template = i - 1;
536 entry.push_back(Pair("fee", pblocktemplate->vTxFees[index_in_template]));
537 entry.push_back(Pair("sigops", pblocktemplate->vTxSigOps[index_in_template]));
539 transactions.push_back(entry);
543 aux.push_back(Pair("flags", HexStr(COINBASE_FLAGS.begin(), COINBASE_FLAGS.end())));
545 uint256 hashTarget = CBigNum().SetCompact(pblock->nBits).getuint256();
547 static Array aMutable;
548 if (aMutable.empty())
550 aMutable.push_back("time");
551 aMutable.push_back("transactions");
552 aMutable.push_back("prevblock");
556 result.push_back(Pair("version", pblock->nVersion));
557 result.push_back(Pair("previousblockhash", pblock->hashPrevBlock.GetHex()));
558 result.push_back(Pair("transactions", transactions));
559 result.push_back(Pair("coinbaseaux", aux));
560 result.push_back(Pair("coinbasevalue", (int64_t)pblock->vtx[0].vout[0].nValue));
561 result.push_back(Pair("target", hashTarget.GetHex()));
562 result.push_back(Pair("mintime", (int64_t)pindexPrev->GetMedianTimePast()+1));
563 result.push_back(Pair("mutable", aMutable));
564 result.push_back(Pair("noncerange", "00000000ffffffff"));
565 result.push_back(Pair("sigoplimit", (int64_t)MAX_BLOCK_SIGOPS));
566 result.push_back(Pair("sizelimit", (int64_t)MAX_BLOCK_SIZE));
567 result.push_back(Pair("curtime", (int64_t)pblock->nTime));
568 result.push_back(Pair("bits", HexBits(pblock->nBits)));
569 result.push_back(Pair("height", (int64_t)(pindexPrev->nHeight+1)));
574 Value submitblock(const Array& params, bool fHelp)
576 if (fHelp || params.size() < 1 || params.size() > 2)
578 "submitblock \"hexdata\" ( \"jsonparametersobject\" )\n"
579 "\nAttempts to submit new block to network.\n"
580 "The 'jsonparametersobject' parameter is currently ignored.\n"
581 "See https://en.bitcoin.it/wiki/BIP_0022 for full specification.\n"
584 "1. \"hexdata\" (string, required) the hex-encoded block data to submit\n"
585 "2. \"jsonparametersobject\" (string, optional) object of optional parameters\n"
587 " \"workid\" : \"id\" (string, optional) if the server provided a workid, it MUST be included with submissions\n"
591 + HelpExampleCli("submitblock", "\"mydata\"")
592 + HelpExampleRpc("submitblock", "\"mydata\"")
595 vector<unsigned char> blockData(ParseHex(params[0].get_str()));
596 CDataStream ssBlock(blockData, SER_NETWORK, PROTOCOL_VERSION);
601 catch (std::exception &e) {
602 throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "Block decode failed");
605 CValidationState state;
606 bool fAccepted = ProcessBlock(state, NULL, &pblock);
608 return "rejected"; // TODO: report validation state