1 // Copyright (c) 2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2012 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.
12 #include "bitcoinrpc.h"
14 using namespace json_spirit;
17 extern uint256 nPoWBase;
19 Value getsubsidy(const Array& params, bool fHelp)
21 if (fHelp || params.size() > 1)
23 "getsubsidy [nTarget]\n"
24 "Returns proof-of-work subsidy value for the specified value of target.");
26 unsigned int nBits = 0;
28 if (params.size() != 0)
30 CBigNum bnTarget(uint256(params[0].get_str()));
31 nBits = bnTarget.GetCompact();
35 nBits = GetNextTargetRequired(pindexBest, false);
38 return (uint64_t)GetProofOfWorkReward(nBits);
41 Value getmininginfo(const Array& params, bool fHelp)
43 if (fHelp || params.size() != 0)
46 "Returns an object containing mining-related information.");
48 float nKernelsRate = 0, nCoinDaysRate = 0;
49 pwalletMain->GetStakeStats(nKernelsRate, nCoinDaysRate);
51 Object obj, diff, weight;
52 obj.push_back(Pair("blocks", (int)nBestHeight));
53 obj.push_back(Pair("currentblocksize",(uint64_t)nLastBlockSize));
54 obj.push_back(Pair("currentblocktx",(uint64_t)nLastBlockTx));
56 diff.push_back(Pair("proof-of-work", GetDifficulty()));
57 diff.push_back(Pair("proof-of-stake", GetDifficulty(GetLastBlockIndex(pindexBest, true))));
58 diff.push_back(Pair("search-interval", (int)nLastCoinStakeSearchInterval));
59 obj.push_back(Pair("difficulty", diff));
61 obj.push_back(Pair("blockvalue", (uint64_t)GetProofOfWorkReward(GetLastBlockIndex(pindexBest, false)->nBits)));
62 obj.push_back(Pair("netmhashps", GetPoWMHashPS()));
63 obj.push_back(Pair("netstakeweight", GetPoSKernelPS()));
64 obj.push_back(Pair("errors", GetWarnings("statusbar")));
65 obj.push_back(Pair("pooledtx", (uint64_t)mempool.size()));
67 weight.push_back(Pair("kernelsrate", nKernelsRate));
68 weight.push_back(Pair("cdaysrate", nCoinDaysRate));
69 obj.push_back(Pair("stakestats", weight));
71 obj.push_back(Pair("stakeinterest", (uint64_t)GetProofOfStakeReward(0, GetLastBlockIndex(pindexBest, true)->nBits, GetLastBlockIndex(pindexBest, true)->nTime, true)));
72 obj.push_back(Pair("testnet", fTestNet));
76 Value scaninput(const Array& params, bool fHelp)
78 if (fHelp || params.size() > 4 || params.size() < 2)
80 "scaninput <txid> <nout> [difficulty] [days]\n"
81 "Scan specified input for suitable kernel solutions.\n"
82 " [difficulty] - upper limit for difficulty, current difficulty by default;\n"
83 " [days] - time window, 365 days by default.\n"
88 hash.SetHex(params[0].get_str());
90 uint32_t nOut = params[1].get_int(), nBits = GetNextTargetRequired(pindexBest, true), nDays = 365;
92 if (params.size() > 2)
94 CBigNum bnTarget(nPoWBase);
96 bnTarget /= (int) (params[2].get_real() * 1000);
97 nBits = bnTarget.GetCompact();
100 if (params.size() > 3)
102 nDays = params[3].get_int();
106 uint256 hashBlock = 0;
107 if (GetTransaction(hash, tx, hashBlock))
109 std::pair<uint256, uint32_t> solution;
110 if (ScanInputForStakeKernelHash(tx, nOut, nBits, nDays * 86400, solution))
113 r.push_back(Pair("hash", solution.first.GetHex()));
114 r.push_back(Pair("time", DateTimeStrFormat(solution.second)));
120 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "No information available about transaction");
125 Value getworkex(const Array& params, bool fHelp)
127 if (fHelp || params.size() > 2)
129 "getworkex [data, coinbase]\n"
130 "If [data, coinbase] is not specified, returns extended work data.\n"
134 throw JSONRPCError(-9, "NovaCoin is not connected!");
136 if (IsInitialBlockDownload())
137 throw JSONRPCError(-10, "NovaCoin is downloading blocks...");
139 typedef map<uint256, pair<CBlock*, CScript> > mapNewBlock_t;
140 static mapNewBlock_t mapNewBlock;
141 static vector<CBlock*> vNewBlock;
142 static CReserveKey reservekey(pwalletMain);
144 if (params.size() == 0)
147 static unsigned int nTransactionsUpdatedLast;
148 static CBlockIndex* pindexPrev;
149 static int64_t nStart;
150 static CBlock* pblock;
151 if (pindexPrev != pindexBest ||
152 (nTransactionsUpdated != nTransactionsUpdatedLast && GetTime() - nStart > 60))
154 if (pindexPrev != pindexBest)
156 // Deallocate old blocks since they're obsolete now
158 BOOST_FOREACH(CBlock* pblock, vNewBlock)
162 nTransactionsUpdatedLast = nTransactionsUpdated;
163 pindexPrev = pindexBest;
167 pblock = CreateNewBlock(pwalletMain);
169 throw JSONRPCError(-7, "Out of memory");
170 vNewBlock.push_back(pblock);
174 pblock->nTime = max(pindexPrev->GetMedianTimePast()+1, GetAdjustedTime());
177 // Update nExtraNonce
178 static unsigned int nExtraNonce = 0;
179 IncrementExtraNonce(pblock, pindexPrev, nExtraNonce);
182 mapNewBlock[pblock->hashMerkleRoot] = make_pair(pblock, pblock->vtx[0].vin[0].scriptSig);
184 // Prebuild hash buffers
188 FormatHashBuffers(pblock, pmidstate, pdata, phash1);
190 uint256 hashTarget = CBigNum().SetCompact(pblock->nBits).getuint256();
192 CTransaction coinbaseTx = pblock->vtx[0];
193 std::vector<uint256> merkle = pblock->GetMerkleBranch(0);
196 result.push_back(Pair("data", HexStr(BEGIN(pdata), END(pdata))));
197 result.push_back(Pair("target", HexStr(BEGIN(hashTarget), END(hashTarget))));
199 CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION);
201 result.push_back(Pair("coinbase", HexStr(ssTx.begin(), ssTx.end())));
205 BOOST_FOREACH(uint256 merkleh, merkle) {
206 merkle_arr.push_back(HexStr(BEGIN(merkleh), END(merkleh)));
209 result.push_back(Pair("merkle", merkle_arr));
217 vector<unsigned char> vchData = ParseHex(params[0].get_str());
218 vector<unsigned char> coinbase;
220 if(params.size() == 2)
221 coinbase = ParseHex(params[1].get_str());
223 if (vchData.size() != 128)
224 throw JSONRPCError(-8, "Invalid parameter");
226 CBlock* pdata = (CBlock*)&vchData[0];
229 for (int i = 0; i < 128/4; i++)
230 ((unsigned int*)pdata)[i] = ByteReverse(((unsigned int*)pdata)[i]);
233 if (!mapNewBlock.count(pdata->hashMerkleRoot))
235 CBlock* pblock = mapNewBlock[pdata->hashMerkleRoot].first;
237 pblock->nTime = pdata->nTime;
238 pblock->nNonce = pdata->nNonce;
240 if(coinbase.size() == 0)
241 pblock->vtx[0].vin[0].scriptSig = mapNewBlock[pdata->hashMerkleRoot].second;
243 CDataStream(coinbase, SER_NETWORK, PROTOCOL_VERSION) >> pblock->vtx[0]; // FIXME - HACK!
245 pblock->hashMerkleRoot = pblock->BuildMerkleTree();
247 return CheckWork(pblock, *pwalletMain, reservekey);
252 Value getwork(const Array& params, bool fHelp)
254 if (fHelp || params.size() > 1)
257 "If [data] is not specified, returns formatted hash data to work on:\n"
258 " \"midstate\" : precomputed hash state after hashing the first half of the data (DEPRECATED)\n" // deprecated
259 " \"data\" : block data\n"
260 " \"hash1\" : formatted hash buffer for second hash (DEPRECATED)\n" // deprecated
261 " \"target\" : little endian hash target\n"
262 "If [data] is specified, tries to solve the block and returns true if it was successful.");
265 throw JSONRPCError(RPC_CLIENT_NOT_CONNECTED, "NovaCoin is not connected!");
267 if (IsInitialBlockDownload())
268 throw JSONRPCError(RPC_CLIENT_IN_INITIAL_DOWNLOAD, "NovaCoin is downloading blocks...");
270 typedef map<uint256, pair<CBlock*, CScript> > mapNewBlock_t;
271 static mapNewBlock_t mapNewBlock; // FIXME: thread safety
272 static vector<CBlock*> vNewBlock;
273 static CReserveKey reservekey(pwalletMain);
275 if (params.size() == 0)
278 static unsigned int nTransactionsUpdatedLast;
279 static CBlockIndex* pindexPrev;
280 static int64_t nStart;
281 static CBlock* pblock;
282 if (pindexPrev != pindexBest ||
283 (nTransactionsUpdated != nTransactionsUpdatedLast && GetTime() - nStart > 60))
285 if (pindexPrev != pindexBest)
287 // Deallocate old blocks since they're obsolete now
289 BOOST_FOREACH(CBlock* pblock, vNewBlock)
294 // Clear pindexPrev so future getworks make a new block, despite any failures from here on
297 // Store the pindexBest used before CreateNewBlock, to avoid races
298 nTransactionsUpdatedLast = nTransactionsUpdated;
299 CBlockIndex* pindexPrevNew = pindexBest;
303 pblock = CreateNewBlock(pwalletMain);
305 throw JSONRPCError(RPC_OUT_OF_MEMORY, "Out of memory");
306 vNewBlock.push_back(pblock);
308 // Need to update only after we know CreateNewBlock succeeded
309 pindexPrev = pindexPrevNew;
313 pblock->UpdateTime(pindexPrev);
316 // Update nExtraNonce
317 static unsigned int nExtraNonce = 0;
318 IncrementExtraNonce(pblock, pindexPrev, nExtraNonce);
321 mapNewBlock[pblock->hashMerkleRoot] = make_pair(pblock, pblock->vtx[0].vin[0].scriptSig);
323 // Pre-build hash buffers
327 FormatHashBuffers(pblock, pmidstate, pdata, phash1);
329 uint256 hashTarget = CBigNum().SetCompact(pblock->nBits).getuint256();
332 result.push_back(Pair("midstate", HexStr(BEGIN(pmidstate), END(pmidstate)))); // deprecated
333 result.push_back(Pair("data", HexStr(BEGIN(pdata), END(pdata))));
334 result.push_back(Pair("hash1", HexStr(BEGIN(phash1), END(phash1)))); // deprecated
335 result.push_back(Pair("target", HexStr(BEGIN(hashTarget), END(hashTarget))));
341 vector<unsigned char> vchData = ParseHex(params[0].get_str());
342 if (vchData.size() != 128)
343 throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter");
344 CBlock* pdata = (CBlock*)&vchData[0];
347 for (int i = 0; i < 128/4; i++)
348 ((unsigned int*)pdata)[i] = ByteReverse(((unsigned int*)pdata)[i]);
351 if (!mapNewBlock.count(pdata->hashMerkleRoot))
353 CBlock* pblock = mapNewBlock[pdata->hashMerkleRoot].first;
355 pblock->nTime = pdata->nTime;
356 pblock->nNonce = pdata->nNonce;
357 pblock->vtx[0].vin[0].scriptSig = mapNewBlock[pdata->hashMerkleRoot].second;
358 pblock->hashMerkleRoot = pblock->BuildMerkleTree();
360 return CheckWork(pblock, *pwalletMain, reservekey);
365 Value getblocktemplate(const Array& params, bool fHelp)
367 if (fHelp || params.size() > 1)
369 "getblocktemplate [params]\n"
370 "Returns data needed to construct a block to work on:\n"
371 " \"version\" : block version\n"
372 " \"previousblockhash\" : hash of current highest block\n"
373 " \"transactions\" : contents of non-coinbase transactions that should be included in the next block\n"
374 " \"coinbaseaux\" : data that should be included in coinbase\n"
375 " \"coinbasevalue\" : maximum allowable input to coinbase transaction, including the generation award and transaction fees\n"
376 " \"target\" : hash target\n"
377 " \"mintime\" : minimum timestamp appropriate for next block\n"
378 " \"curtime\" : current timestamp\n"
379 " \"mutable\" : list of ways the block template may be changed\n"
380 " \"noncerange\" : range of valid nonces\n"
381 " \"sigoplimit\" : limit of sigops in blocks\n"
382 " \"sizelimit\" : limit of block size\n"
383 " \"bits\" : compressed target of next block\n"
384 " \"height\" : height of the next block\n"
385 "See https://en.bitcoin.it/wiki/BIP_0022 for full specification.");
387 std::string strMode = "template";
388 if (params.size() > 0)
390 const Object& oparam = params[0].get_obj();
391 const Value& modeval = find_value(oparam, "mode");
392 if (modeval.type() == str_type)
393 strMode = modeval.get_str();
394 else if (modeval.type() == null_type)
399 throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid mode");
402 if (strMode != "template")
403 throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid mode");
406 throw JSONRPCError(RPC_CLIENT_NOT_CONNECTED, "NovaCoin is not connected!");
408 if (IsInitialBlockDownload())
409 throw JSONRPCError(RPC_CLIENT_IN_INITIAL_DOWNLOAD, "NovaCoin is downloading blocks...");
411 static CReserveKey reservekey(pwalletMain);
414 static unsigned int nTransactionsUpdatedLast;
415 static CBlockIndex* pindexPrev;
416 static int64_t nStart;
417 static CBlock* pblock;
418 if (pindexPrev != pindexBest ||
419 (nTransactionsUpdated != nTransactionsUpdatedLast && GetTime() - nStart > 5))
421 // Clear pindexPrev so future calls make a new block, despite any failures from here on
424 // Store the pindexBest used before CreateNewBlock, to avoid races
425 nTransactionsUpdatedLast = nTransactionsUpdated;
426 CBlockIndex* pindexPrevNew = pindexBest;
435 pblock = CreateNewBlock(pwalletMain);
437 throw JSONRPCError(RPC_OUT_OF_MEMORY, "Out of memory");
439 // Need to update only after we know CreateNewBlock succeeded
440 pindexPrev = pindexPrevNew;
444 pblock->UpdateTime(pindexPrev);
448 map<uint256, int64_t> setTxIndex;
451 BOOST_FOREACH (CTransaction& tx, pblock->vtx)
453 uint256 txHash = tx.GetHash();
454 setTxIndex[txHash] = i++;
456 if (tx.IsCoinBase() || tx.IsCoinStake())
461 CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION);
463 entry.push_back(Pair("data", HexStr(ssTx.begin(), ssTx.end())));
465 entry.push_back(Pair("hash", txHash.GetHex()));
468 map<uint256, CTxIndex> mapUnused;
469 bool fInvalid = false;
470 if (tx.FetchInputs(txdb, mapUnused, false, false, mapInputs, fInvalid))
472 entry.push_back(Pair("fee", (int64_t)(tx.GetValueIn(mapInputs) - tx.GetValueOut())));
475 BOOST_FOREACH (MapPrevTx::value_type& inp, mapInputs)
477 if (setTxIndex.count(inp.first))
478 deps.push_back(setTxIndex[inp.first]);
480 entry.push_back(Pair("depends", deps));
482 int64_t nSigOps = tx.GetLegacySigOpCount();
483 nSigOps += tx.GetP2SHSigOpCount(mapInputs);
484 entry.push_back(Pair("sigops", nSigOps));
487 transactions.push_back(entry);
491 aux.push_back(Pair("flags", HexStr(COINBASE_FLAGS.begin(), COINBASE_FLAGS.end())));
493 uint256 hashTarget = CBigNum().SetCompact(pblock->nBits).getuint256();
495 static Array aMutable;
496 if (aMutable.empty())
498 aMutable.push_back("time");
499 aMutable.push_back("transactions");
500 aMutable.push_back("prevblock");
504 result.push_back(Pair("version", pblock->nVersion));
505 result.push_back(Pair("previousblockhash", pblock->hashPrevBlock.GetHex()));
506 result.push_back(Pair("transactions", transactions));
507 result.push_back(Pair("coinbaseaux", aux));
508 result.push_back(Pair("coinbasevalue", (int64_t)pblock->vtx[0].vout[0].nValue));
509 result.push_back(Pair("target", hashTarget.GetHex()));
510 result.push_back(Pair("mintime", (int64_t)pindexPrev->GetMedianTimePast()+1));
511 result.push_back(Pair("mutable", aMutable));
512 result.push_back(Pair("noncerange", "00000000ffffffff"));
513 result.push_back(Pair("sigoplimit", (int64_t)MAX_BLOCK_SIGOPS));
514 result.push_back(Pair("sizelimit", (int64_t)MAX_BLOCK_SIZE));
515 result.push_back(Pair("curtime", (int64_t)pblock->nTime));
516 result.push_back(Pair("bits", HexBits(pblock->nBits)));
517 result.push_back(Pair("height", (int64_t)(pindexPrev->nHeight+1)));
522 Value submitblock(const Array& params, bool fHelp)
524 if (fHelp || params.size() < 1 || params.size() > 2)
526 "submitblock <hex data> [optional-params-obj]\n"
527 "[optional-params-obj] parameter is currently ignored.\n"
528 "Attempts to submit new block to network.\n"
529 "See https://en.bitcoin.it/wiki/BIP_0022 for full specification.");
531 vector<unsigned char> blockData(ParseHex(params[0].get_str()));
532 CDataStream ssBlock(blockData, SER_NETWORK, PROTOCOL_VERSION);
537 catch (std::exception &e) {
538 throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "Block decode failed");
541 bool fAccepted = ProcessBlock(NULL, &block);