8 #if defined(HAVE_CONFIG_H)
24 #include "json/json_spirit_utils.h"
25 #include "json/json_spirit_value.h"
49 delete pMiningKey; pMiningKey = NULL;
74 lookup = pb->
nHeight % 2016 + 1;
82 int64_t maxTime = minTime;
83 for (
int i = 0; i < lookup; i++) {
86 minTime = std::min(time, minTime);
87 maxTime = std::max(time, maxTime);
91 if (minTime == maxTime)
95 int64_t timeDiff = maxTime - minTime;
97 return (int64_t)(workDiff.
getdouble() / timeDiff);
102 if (fHelp || params.size() > 2)
104 "getnetworkhashps ( blocks height )\n"
105 "\nReturns the estimated network hashes per second based on the last n blocks.\n"
106 "Pass in [blocks] to override # of blocks, -1 specifies since last difficulty change.\n"
107 "Pass in [height] to estimate the network speed at the time when a certain block was found.\n"
109 "1. blocks (numeric, optional, default=120) The number of blocks, or -1 for blocks since last difficulty change.\n"
110 "2. height (numeric, optional, default=-1) To estimate at the time of the given height.\n"
112 "x (numeric) Hashes per second estimated\n"
118 return GetNetworkHashPS(params.size() > 0 ? params[0].get_int() : 120, params.size() > 1 ? params[1].get_int() : -1);
124 if (fHelp || params.size() != 0)
127 "\nReturn if the server is set to generate coins or not. The default is false.\n"
128 "It is set with the command line argument -gen (or anoncoin.conf setting gen)\n"
129 "It can also be set with the setgenerate call.\n"
131 "true|false (boolean) If the server is set to generate coins or not\n"
146 if (fHelp || params.size() < 1 || params.size() > 2)
148 "setgenerate generate ( genproclimit )\n"
149 "\nSet 'generate' true or false to turn generation on or off.\n"
150 "Generation is limited to 'genproclimit' processors, -1 is unlimited.\n"
151 "See the getgenerate call for the current setting.\n"
153 "1. generate (boolean, required) Set to true to turn on generation, off to turn off.\n"
154 "2. genproclimit (numeric, optional) Set the processor limit for when generation is on. Can be -1 for unlimited.\n"
155 " Note: in -regtest mode, genproclimit controls how many blocks are generated immediately.\n"
157 "\nSet the generation on with a limit of one processor\n"
159 "\nCheck the setting\n"
161 "\nTurn off generation\n"
170 bool fGenerate =
true;
171 if (params.size() > 0)
172 fGenerate = params[0].get_bool();
174 int nGenProcLimit = -1;
175 if (params.size() > 1)
177 nGenProcLimit = params[1].get_int();
178 if (nGenProcLimit == 0)
185 int nHeightStart = 0;
188 int nGenerate = (nGenProcLimit > 0 ? nGenProcLimit : 1);
192 nHeight = nHeightStart;
193 nHeightEnd = nHeightStart+nGenerate;
195 int nHeightLast = -1;
196 while (nHeight < nHeightEnd)
198 if (nHeightLast != nHeight)
200 nHeightLast = nHeight;
212 mapArgs[
"-gen"] = (fGenerate ?
"1" :
"0");
222 if (fHelp || params.size() != 0)
225 "\nReturns a recent hashes per second performance measurement while generating.\n"
226 "See the getgenerate and setgenerate calls to turn generation on and off.\n"
228 "n (numeric) The recent hashes per second when generation is on (will return 0 if generation is off)\n"
243 if (fHelp || params.size() != 0)
246 "\nReturns a json object containing mining-related information."
249 " \"blocks\": nnn, (numeric) The current block\n"
250 " \"currentblocksize\": nnn, (numeric) The last block size\n"
251 " \"currentblocktx\": nnn, (numeric) The last block transaction\n"
252 " \"difficulty\": xxx.xxxxx (numeric) The current difficulty\n"
253 " \"errors\": \"...\" (string) Current errors\n"
254 " \"generate\": true|false (boolean) If the generation is on or off (see getgenerate or setgenerate calls)\n"
255 " \"genproclimit\": n (numeric) The processor limit for generation. -1 if no generation. (see getgenerate or setgenerate calls)\n"
256 " \"hashespersec\": n (numeric) The hashes per second of the generation, or 0 if no generation.\n"
257 " \"pooledtx\": n (numeric) The size of the mem pool\n"
258 " \"testnet\": true|false (boolean) If using testnet or not\n"
267 obj.push_back(Pair(
"currentblocksize", (uint64_t)
nLastBlockSize));
268 obj.push_back(Pair(
"currentblocktx", (uint64_t)
nLastBlockTx));
270 obj.push_back(Pair(
"errors",
GetWarnings(
"statusbar")));
271 obj.push_back(Pair(
"genproclimit", (
int)
GetArg(
"-genproclimit", -1)));
273 obj.push_back(Pair(
"pooledtx", (uint64_t)
mempool.
size()));
274 obj.push_back(Pair(
"testnet",
TestNet()));
276 obj.push_back(Pair(
"generate",
getgenerate(params,
false)));
284 Value
getwork(
const Array& params,
bool fHelp)
286 if (fHelp || params.size() > 1)
288 "getwork ( \"data\" )\n"
289 "\nIf 'data' is not specified, it returns the formatted hash data to work on.\n"
290 "If 'data' is specified, tries to solve the block and returns true if it was successful.\n"
292 "1. \"data\" (string, optional) The hex encoded data to solve\n"
293 "\nResult (when 'data' is not specified):\n"
295 " \"midstate\" : \"xxxx\", (string) The precomputed hash state after hashing the first half of the data (DEPRECATED)\n"
296 " \"data\" : \"xxxxx\", (string) The block data\n"
297 " \"hash1\" : \"xxxxx\", (string) The formatted hash buffer for second hash (DEPRECATED)\n"
298 " \"target\" : \"xxxx\" (string) The little endian hash target\n"
300 "\nResult (when 'data' is specified):\n"
301 "true|false (boolean) If solving the block specified in the 'data' was successfull\n"
313 typedef map<uint256, pair<CBlock*, CScript> > mapNewBlock_t;
314 static mapNewBlock_t mapNewBlock;
315 static vector<CBlockTemplate*> vNewBlockTemplate;
317 if (params.size() == 0)
320 static unsigned int nTransactionsUpdatedLast;
322 static int64_t nStart;
332 delete pblocktemplate;
333 vNewBlockTemplate.clear();
340 nTransactionsUpdatedLast =
mempool.GetTransactionsUpdated();
348 vNewBlockTemplate.push_back(pblocktemplate);
351 pindexPrev = pindexPrevNew;
353 CBlock* pblock = &pblocktemplate->block;
360 static
unsigned int nExtraNonce = 0;
364 mapNewBlock[pblock->hashMerkleRoot] = make_pair(pblock, pblock->vtx[0].vin[0].scriptSig);
372 uint256 hashTarget =
CBigNum().SetCompact(pblock->nBits).getuint256();
375 result.push_back(Pair("midstate",
HexStr(
BEGIN(pmidstate),
END(pmidstate))));
377 result.push_back(Pair("hash1",
HexStr(
BEGIN(phash1),
END(phash1))));
378 result.push_back(Pair("target",
HexStr(
BEGIN(hashTarget),
END(hashTarget))));
384 vector<unsigned char> vchData =
ParseHex(params[0].get_str());
385 if (vchData.size() != 128)
390 for (
int i = 0; i < 128/4; i++)
391 ((
unsigned int*)pdata)[i] =
ByteReverse(((
unsigned int*)pdata)[i]);
411 if (fHelp || params.size() > 1)
413 "getblocktemplate ( \"jsonrequestobject\" )\n"
414 "\nIf the request parameters include a 'mode' key, that is used to explicitly select between the default 'template' request or a 'proposal'.\n"
415 "It returns data needed to construct a block to work on.\n"
416 "See https://en.bitcoin.it/wiki/BIP_0022 for full specification.\n"
419 "1. \"jsonrequestobject\" (string, optional) A json object in the following spec\n"
421 " \"mode\":\"template\" (string, optional) This must be set to \"template\" or omitted\n"
422 " \"capabilities\":[ (array, optional) A list of strings\n"
423 " \"support\" (string) client side supported feature, 'longpoll', 'coinbasetxn', 'coinbasevalue', 'proposal', 'serverlist', 'workid'\n"
431 " \"version\" : n, (numeric) The block version\n"
432 " \"previousblockhash\" : \"xxxx\", (string) The hash of current highest block\n"
433 " \"transactions\" : [ (array) contents of non-coinbase transactions that should be included in the next block\n"
435 " \"data\" : \"xxxx\", (string) transaction data encoded in hexadecimal (byte-for-byte)\n"
436 " \"hash\" : \"xxxx\", (string) hash/id encoded in little-endian hexadecimal\n"
437 " \"depends\" : [ (array) array of numbers \n"
438 " 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"
441 " \"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"
442 " \"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"
443 " \"required\" : true|false (boolean) if provided and true, this transaction must be in the final block\n"
447 " \"coinbaseaux\" : { (json object) data that should be included in the coinbase's scriptSig content\n"
448 " \"flags\" : \"flags\" (string) \n"
450 " \"coinbasevalue\" : n, (numeric) maximum allowable input to coinbase transaction, including the generation award and transaction fees (in Satoshis)\n"
451 " \"coinbasetxn\" : { ... }, (json object) information for coinbase transaction\n"
452 " \"target\" : \"xxxx\", (string) The hash target\n"
453 " \"mintime\" : xxx, (numeric) The minimum timestamp appropriate for next block time in seconds since epoch (Jan 1 1970 GMT)\n"
454 " \"mutable\" : [ (array of string) list of ways the block template may be changed \n"
455 " \"value\" (string) A way the block template may be changed, e.g. 'time', 'transactions', 'prevblock'\n"
458 " \"noncerange\" : \"00000000ffffffff\", (string) A range of valid nonces\n"
459 " \"sigoplimit\" : n, (numeric) limit of sigops in blocks\n"
460 " \"sizelimit\" : n, (numeric) limit of block size\n"
461 " \"curtime\" : ttt, (numeric) current timestamp in seconds since epoch (Jan 1 1970 GMT)\n"
462 " \"bits\" : \"xxx\", (string) compressed target of next block\n"
463 " \"height\" : n (numeric) The height of the next block\n"
471 std::string strMode =
"template";
472 if (params.size() > 0)
474 const Object& oparam = params[0].get_obj();
475 const Value& modeval = find_value(oparam,
"mode");
476 if (modeval.type() == str_type)
477 strMode = modeval.get_str();
478 else if (modeval.type() == null_type)
486 if (strMode !=
"template")
496 static unsigned int nTransactionsUpdatedLast;
498 static int64_t nStart;
514 delete pblocktemplate;
515 pblocktemplate = NULL;
523 pindexPrev = pindexPrevNew;
532 map<uint256, int64_t> setTxIndex;
537 setTxIndex[txHash] = i++;
548 entry.push_back(Pair(
"hash", txHash.
GetHex()));
551 BOOST_FOREACH (
const CTxIn &in, tx.vin)
556 entry.push_back(Pair(
"depends", deps));
558 int index_in_template = i - 1;
559 entry.push_back(Pair(
"fee", pblocktemplate->
vTxFees[index_in_template]));
560 entry.push_back(Pair(
"sigops", pblocktemplate->
vTxSigOps[index_in_template]));
562 transactions.push_back(entry);
570 static Array aMutable;
571 if (aMutable.empty())
573 aMutable.push_back(
"time");
574 aMutable.push_back(
"transactions");
575 aMutable.push_back(
"prevblock");
579 result.push_back(Pair(
"version", pblock->
nVersion));
581 result.push_back(Pair(
"transactions", transactions));
582 result.push_back(Pair(
"coinbaseaux", aux));
583 result.push_back(Pair(
"coinbasevalue", (int64_t)pblock->
vtx[0].vout[0].nValue));
584 result.push_back(Pair(
"target", hashTarget.
GetHex()));
586 result.push_back(Pair(
"mutable", aMutable));
587 result.push_back(Pair(
"noncerange",
"00000000ffffffff"));
588 result.push_back(Pair(
"sigoplimit", (int64_t)MAX_BLOCK_SIGOPS));
589 result.push_back(Pair(
"sizelimit", (int64_t)MAX_BLOCK_SIZE));
590 result.push_back(Pair(
"curtime", (int64_t)pblock->
nTime));
592 result.push_back(Pair(
"height", (int64_t)(pindexPrev->
nHeight+1)));
599 if (fHelp || params.size() < 1 || params.size() > 2)
601 "submitblock \"hexdata\" ( \"jsonparametersobject\" )\n"
602 "\nAttempts to submit new block to network.\n"
603 "The 'jsonparametersobject' parameter is currently ignored.\n"
604 "See https://en.bitcoin.it/wiki/BIP_0022 for full specification.\n"
607 "1. \"hexdata\" (string, required) the hex-encoded block data to submit\n"
608 "2. \"jsonparametersobject\" (string, optional) object of optional parameters\n"
610 " \"workid\" : \"id\" (string, optional) if the server provided a workid, it MUST be included with submissions\n"
618 vector<unsigned char> blockData(
ParseHex(params[0].get_str()));
624 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 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)
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.
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)
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.
std::vector< int64_t > vTxSigOps
double GetDifficulty(const CBlockIndex *blockindex)
void GenerateAnoncoins(bool fGenerate, CWallet *pwallet, int nThreads)
Run the miner threads.
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. ...
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