18 #include "json/json_spirit_utils.h"
19 #include "json/json_spirit_value.h"
43 delete pMiningKey; pMiningKey = NULL;
68 lookup = pb->
nHeight % 2016 + 1;
76 int64_t maxTime = minTime;
77 for (
int i = 0; i < lookup; i++) {
80 minTime = std::min(time, minTime);
81 maxTime = std::max(time, maxTime);
85 if (minTime == maxTime)
89 int64_t timeDiff = maxTime - minTime;
91 return (int64_t)(workDiff.
getdouble() / timeDiff);
96 if (fHelp || params.size() > 2)
98 "getnetworkhashps ( blocks height )\n"
99 "\nReturns the estimated network hashes per second based on the last n blocks.\n"
100 "Pass in [blocks] to override # of blocks, -1 specifies since last difficulty change.\n"
101 "Pass in [height] to estimate the network speed at the time when a certain block was found.\n"
103 "1. blocks (numeric, optional, default=120) The number of blocks, or -1 for blocks since last difficulty change.\n"
104 "2. height (numeric, optional, default=-1) To estimate at the time of the given height.\n"
106 "x (numeric) Hashes per second estimated\n"
112 return GetNetworkHashPS(params.size() > 0 ? params[0].get_int() : 120, params.size() > 1 ? params[1].get_int() : -1);
118 if (fHelp || params.size() != 0)
121 "\nReturn if the server is set to generate coins or not. The default is false.\n"
122 "It is set with the command line argument -gen (or bitcoin.conf setting gen)\n"
123 "It can also be set with the setgenerate call.\n"
125 "true|false (boolean) If the server is set to generate coins or not\n"
140 if (fHelp || params.size() < 1 || params.size() > 2)
142 "setgenerate generate ( genproclimit )\n"
143 "\nSet 'generate' true or false to turn generation on or off.\n"
144 "Generation is limited to 'genproclimit' processors, -1 is unlimited.\n"
145 "See the getgenerate call for the current setting.\n"
147 "1. generate (boolean, required) Set to true to turn on generation, off to turn off.\n"
148 "2. genproclimit (numeric, optional) Set the processor limit for when generation is on. Can be -1 for unlimited.\n"
149 " Note: in -regtest mode, genproclimit controls how many blocks are generated immediately.\n"
151 "\nSet the generation on with a limit of one processor\n"
153 "\nCheck the setting\n"
155 "\nTurn off generation\n"
164 bool fGenerate =
true;
165 if (params.size() > 0)
166 fGenerate = params[0].get_bool();
168 int nGenProcLimit = -1;
169 if (params.size() > 1)
171 nGenProcLimit = params[1].get_int();
172 if (nGenProcLimit == 0)
179 int nHeightStart = 0;
182 int nGenerate = (nGenProcLimit > 0 ? nGenProcLimit : 1);
186 nHeight = nHeightStart;
187 nHeightEnd = nHeightStart+nGenerate;
189 int nHeightLast = -1;
190 while (nHeight < nHeightEnd)
192 if (nHeightLast != nHeight)
194 nHeightLast = nHeight;
206 mapArgs[
"-gen"] = (fGenerate ?
"1" :
"0");
216 if (fHelp || params.size() != 0)
219 "\nReturns a recent hashes per second performance measurement while generating.\n"
220 "See the getgenerate and setgenerate calls to turn generation on and off.\n"
222 "n (numeric) The recent hashes per second when generation is on (will return 0 if generation is off)\n"
237 if (fHelp || params.size() != 0)
240 "\nReturns a json object containing mining-related information."
243 " \"blocks\": nnn, (numeric) The current block\n"
244 " \"currentblocksize\": nnn, (numeric) The last block size\n"
245 " \"currentblocktx\": nnn, (numeric) The last block transaction\n"
246 " \"difficulty\": xxx.xxxxx (numeric) The current difficulty\n"
247 " \"errors\": \"...\" (string) Current errors\n"
248 " \"generate\": true|false (boolean) If the generation is on or off (see getgenerate or setgenerate calls)\n"
249 " \"genproclimit\": n (numeric) The processor limit for generation. -1 if no generation. (see getgenerate or setgenerate calls)\n"
250 " \"hashespersec\": n (numeric) The hashes per second of the generation, or 0 if no generation.\n"
251 " \"pooledtx\": n (numeric) The size of the mem pool\n"
252 " \"testnet\": true|false (boolean) If using testnet or not\n"
261 obj.push_back(Pair(
"currentblocksize", (uint64_t)
nLastBlockSize));
262 obj.push_back(Pair(
"currentblocktx", (uint64_t)
nLastBlockTx));
264 obj.push_back(Pair(
"errors",
GetWarnings(
"statusbar")));
265 obj.push_back(Pair(
"genproclimit", (
int)
GetArg(
"-genproclimit", -1)));
267 obj.push_back(Pair(
"pooledtx", (uint64_t)
mempool.
size()));
268 obj.push_back(Pair(
"testnet",
TestNet()));
270 obj.push_back(Pair(
"generate",
getgenerate(params,
false)));
278 Value
getwork(
const Array& params,
bool fHelp)
280 if (fHelp || params.size() > 1)
282 "getwork ( \"data\" )\n"
283 "\nIf 'data' is not specified, it returns the formatted hash data to work on.\n"
284 "If 'data' is specified, tries to solve the block and returns true if it was successful.\n"
286 "1. \"data\" (string, optional) The hex encoded data to solve\n"
287 "\nResult (when 'data' is not specified):\n"
289 " \"midstate\" : \"xxxx\", (string) The precomputed hash state after hashing the first half of the data (DEPRECATED)\n"
290 " \"data\" : \"xxxxx\", (string) The block data\n"
291 " \"hash1\" : \"xxxxx\", (string) The formatted hash buffer for second hash (DEPRECATED)\n"
292 " \"target\" : \"xxxx\" (string) The little endian hash target\n"
294 "\nResult (when 'data' is specified):\n"
295 "true|false (boolean) If solving the block specified in the 'data' was successfull\n"
307 typedef map<uint256, pair<CBlock*, CScript> > mapNewBlock_t;
308 static mapNewBlock_t mapNewBlock;
309 static vector<CBlockTemplate*> vNewBlockTemplate;
311 if (params.size() == 0)
314 static unsigned int nTransactionsUpdatedLast;
316 static int64_t nStart;
326 delete pblocktemplate;
327 vNewBlockTemplate.clear();
334 nTransactionsUpdatedLast =
mempool.GetTransactionsUpdated();
342 vNewBlockTemplate.push_back(pblocktemplate);
345 pindexPrev = pindexPrevNew;
347 CBlock* pblock = &pblocktemplate->block;
354 static
unsigned int nExtraNonce = 0;
358 mapNewBlock[pblock->hashMerkleRoot] = make_pair(pblock, pblock->vtx[0].vin[0].scriptSig);
366 uint256 hashTarget =
CBigNum().SetCompact(pblock->nBits).getuint256();
369 result.push_back(Pair("midstate",
HexStr(
BEGIN(pmidstate),
END(pmidstate))));
371 result.push_back(Pair("hash1",
HexStr(
BEGIN(phash1),
END(phash1))));
372 result.push_back(Pair("target",
HexStr(
BEGIN(hashTarget),
END(hashTarget))));
378 vector<unsigned char> vchData =
ParseHex(params[0].get_str());
379 if (vchData.size() != 128)
384 for (
int i = 0; i < 128/4; i++)
385 ((
unsigned int*)pdata)[i] =
ByteReverse(((
unsigned int*)pdata)[i]);
405 if (fHelp || params.size() > 1)
407 "getblocktemplate ( \"jsonrequestobject\" )\n"
408 "\nIf the request parameters include a 'mode' key, that is used to explicitly select between the default 'template' request or a 'proposal'.\n"
409 "It returns data needed to construct a block to work on.\n"
410 "See https://en.bitcoin.it/wiki/BIP_0022 for full specification.\n"
413 "1. \"jsonrequestobject\" (string, optional) A json object in the following spec\n"
415 " \"mode\":\"template\" (string, optional) This must be set to \"template\" or omitted\n"
416 " \"capabilities\":[ (array, optional) A list of strings\n"
417 " \"support\" (string) client side supported feature, 'longpoll', 'coinbasetxn', 'coinbasevalue', 'proposal', 'serverlist', 'workid'\n"
425 " \"version\" : n, (numeric) The block version\n"
426 " \"previousblockhash\" : \"xxxx\", (string) The hash of current highest block\n"
427 " \"transactions\" : [ (array) contents of non-coinbase transactions that should be included in the next block\n"
429 " \"data\" : \"xxxx\", (string) transaction data encoded in hexadecimal (byte-for-byte)\n"
430 " \"hash\" : \"xxxx\", (string) hash/id encoded in little-endian hexadecimal\n"
431 " \"depends\" : [ (array) array of numbers \n"
432 " 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"
435 " \"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"
436 " \"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"
437 " \"required\" : true|false (boolean) if provided and true, this transaction must be in the final block\n"
441 " \"coinbaseaux\" : { (json object) data that should be included in the coinbase's scriptSig content\n"
442 " \"flags\" : \"flags\" (string) \n"
444 " \"coinbasevalue\" : n, (numeric) maximum allowable input to coinbase transaction, including the generation award and transaction fees (in Satoshis)\n"
445 " \"coinbasetxn\" : { ... }, (json object) information for coinbase transaction\n"
446 " \"target\" : \"xxxx\", (string) The hash target\n"
447 " \"mintime\" : xxx, (numeric) The minimum timestamp appropriate for next block time in seconds since epoch (Jan 1 1970 GMT)\n"
448 " \"mutable\" : [ (array of string) list of ways the block template may be changed \n"
449 " \"value\" (string) A way the block template may be changed, e.g. 'time', 'transactions', 'prevblock'\n"
452 " \"noncerange\" : \"00000000ffffffff\", (string) A range of valid nonces\n"
453 " \"sigoplimit\" : n, (numeric) limit of sigops in blocks\n"
454 " \"sizelimit\" : n, (numeric) limit of block size\n"
455 " \"curtime\" : ttt, (numeric) current timestamp in seconds since epoch (Jan 1 1970 GMT)\n"
456 " \"bits\" : \"xxx\", (string) compressed target of next block\n"
457 " \"height\" : n (numeric) The height of the next block\n"
465 std::string strMode =
"template";
466 if (params.size() > 0)
468 const Object& oparam = params[0].get_obj();
469 const Value& modeval = find_value(oparam,
"mode");
470 if (modeval.type() == str_type)
471 strMode = modeval.get_str();
472 else if (modeval.type() == null_type)
480 if (strMode !=
"template")
490 static unsigned int nTransactionsUpdatedLast;
492 static int64_t nStart;
508 delete pblocktemplate;
509 pblocktemplate = NULL;
517 pindexPrev = pindexPrevNew;
526 map<uint256, int64_t> setTxIndex;
531 setTxIndex[txHash] = i++;
542 entry.push_back(Pair(
"hash", txHash.
GetHex()));
545 BOOST_FOREACH (
const CTxIn &in, tx.vin)
550 entry.push_back(Pair(
"depends", deps));
552 int index_in_template = i - 1;
553 entry.push_back(Pair(
"fee", pblocktemplate->
vTxFees[index_in_template]));
554 entry.push_back(Pair(
"sigops", pblocktemplate->
vTxSigOps[index_in_template]));
556 transactions.push_back(entry);
564 static Array aMutable;
565 if (aMutable.empty())
567 aMutable.push_back(
"time");
568 aMutable.push_back(
"transactions");
569 aMutable.push_back(
"prevblock");
573 result.push_back(Pair(
"version", pblock->
nVersion));
575 result.push_back(Pair(
"transactions", transactions));
576 result.push_back(Pair(
"coinbaseaux", aux));
577 result.push_back(Pair(
"coinbasevalue", (int64_t)pblock->
vtx[0].vout[0].nValue));
578 result.push_back(Pair(
"target", hashTarget.
GetHex()));
580 result.push_back(Pair(
"mutable", aMutable));
581 result.push_back(Pair(
"noncerange",
"00000000ffffffff"));
584 result.push_back(Pair(
"curtime", (int64_t)pblock->
nTime));
586 result.push_back(Pair(
"height", (int64_t)(pindexPrev->
nHeight+1)));
593 if (fHelp || params.size() < 1 || params.size() > 2)
595 "submitblock \"hexdata\" ( \"jsonparametersobject\" )\n"
596 "\nAttempts to submit new block to network.\n"
597 "The 'jsonparametersobject' parameter is currently ignored.\n"
598 "See https://en.bitcoin.it/wiki/BIP_0022 for full specification.\n"
601 "1. \"hexdata\" (string, required) the hex-encoded block data to submit\n"
602 "2. \"jsonparametersobject\" (string, optional) object of optional parameters\n"
604 " \"workid\" : \"id\" (string, optional) if the server provided a workid, it MUST be included with submissions\n"
612 vector<unsigned char> blockData(
ParseHex(params[0].get_str()));
618 catch (std::exception &e) {
json_spirit::Value setgenerate(const json_spirit::Array ¶ms, bool fHelp)
CBlockTemplate * CreateNewBlockWithKey(CReserveKey &reservekey)
void IncrementExtraNonce(CBlock *pblock, CBlockIndex *pindexPrev, unsigned int &nExtraNonce)
Modify the extranonce in a block.
const_iterator begin() const
uint256 getuint256() const
void GenerateBitcoins(bool fGenerate, CWallet *pwallet, int nThreads)
Run the miner threads.
void MilliSleep(int64_t n)
std::string HelpExampleRpc(string methodname, string args)
Value GetNetworkHashPS(int lookup, int height)
std::vector< int64_t > vTxFees
string GetWarnings(string strFor)
Format a string that describes several potential problems detected by the core.
CBlockTemplate * CreateNewBlock(const CScript &scriptPubKeyIn)
Generate a new block, without valid proof-of-work.
Double ended buffer combining vector and stream-like interfaces.
Object JSONRPCError(int code, const string &message)
CChain chainActive
The currently-connected chain of blocks.
bool CheckWork(CBlock *pblock, CWallet &wallet, CReserveKey &reservekey)
Check mined block.
unsigned int GetTransactionsUpdated() const
uint256 BuildMerkleTree() const
void UpdateTime(CBlockHeader &block, const CBlockIndex *pindexPrev)
CBigNum & SetCompact(unsigned int nCompact)
CBlockIndex * Tip() const
Returns the index entry for the tip of this chain, or NULL if none.
int Height() const
Return the maximal height in the chain.
std::string HexBits(unsigned int nBits)
bool GetBoolArg(const std::string &strArg, bool fDefault)
Return boolean argument or default value.
static const unsigned int MAX_BLOCK_SIGOPS
The maximum allowed number of signature check operations in a block (network rule) ...
json_spirit::Value gethashespersec(const json_spirit::Array ¶ms, bool fHelp)
bool IsInitialBlockDownload()
Check whether we are doing an initial block download (synchronizing from disk or network) ...
An input of a transaction.
std::string itostr(int n)
void FormatHashBuffers(CBlock *pblock, char *pmidstate, char *pdata, char *phash1)
Do mining precalculation.
Value getmininginfo(const Array ¶ms, bool fHelp)
C++ wrapper for BIGNUM (OpenSSL bignum)
static const unsigned int MAX_BLOCK_SIZE
The maximum allowed size for a serialized block, in bytes (network rule)
std::string GetHex() const
Value submitblock(const Array ¶ms, bool fHelp)
json_spirit::Value getgenerate(const json_spirit::Array ¶ms, bool fHelp)
Capture information about block/transaction validation.
bool ProcessBlock(CValidationState &state, CNode *pfrom, CBlock *pblock, CDiskBlockPos *dbp)
Process an incoming block.
A key allocated from the key pool.
uint32_t ByteReverse(uint32_t value)
The block chain is a tree shaped structure starting with the genesis block at the root...
const CChainParams & Params()
Return the currently selected parameters.
Serialized script, used inside transaction inputs and outputs.
static const int PROTOCOL_VERSION
std::vector< int64_t > vTxSigOps
double GetDifficulty(const CBlockIndex *blockindex)
std::string HelpExampleCli(string methodname, string args)
Value getnetworkhashps(const Array ¶ms, bool fHelp)
std::vector< CTransaction > vtx
std::string GetArg(const std::string &strArg, const std::string &strDefault)
Return string argument or default value.
Value getblocktemplate(const Array ¶ms, bool fHelp)
The basic transaction that is broadcasted on the network and contained in blocks. ...
static const CCheckpointData data
std::string HexStr(const T itbegin, const T itend, bool fSpaces=false)
int64_t GetBlockTime() const
json_spirit::Value getwork(const json_spirit::Array ¶ms, bool fHelp)
int64_t GetMedianTimePast() const
vector< unsigned char > ParseHex(const char *psz)
map< string, string > mapArgs
const_iterator end() const