1 // Copyright (c) 2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2014 The Bitcoin Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
6 #include "checkpoints.h"
7 #include "consensus/validation.h"
9 #include "primitives/transaction.h"
10 #include "rpcserver.h"
16 #include "json/json_spirit_value.h"
18 using namespace json_spirit;
21 extern void TxToJSON(const CTransaction& tx, const uint256 hashBlock, Object& entry);
22 void ScriptPubKeyToJSON(const CScript& scriptPubKey, Object& out, bool fIncludeHex);
24 double GetDifficultyINTERNAL(const CBlockIndex* blockindex, bool networkDifficulty)
26 // Floating point number that is a multiple of the minimum difficulty,
27 // minimum difficulty = 1.0.
28 if (blockindex == NULL)
30 if (chainActive.Tip() == NULL)
33 blockindex = chainActive.Tip();
37 if (networkDifficulty) {
38 bits = GetNextWorkRequired(blockindex, nullptr, Params().GetConsensus());
40 bits = blockindex->nBits;
44 UintToArith256(Params().GetConsensus().powLimit).GetCompact();
45 int nShift = (bits >> 24) & 0xff;
46 int nShiftAmount = (powLimit >> 24) & 0xff;
49 (double)(powLimit & 0x00ffffff) /
50 (double)(bits & 0x00ffffff);
52 while (nShift < nShiftAmount)
57 while (nShift > nShiftAmount)
66 double GetDifficulty(const CBlockIndex* blockindex)
68 return GetDifficultyINTERNAL(blockindex, false);
71 double GetNetworkDifficulty(const CBlockIndex* blockindex)
73 return GetDifficultyINTERNAL(blockindex, true);
77 Object blockToJSON(const CBlock& block, const CBlockIndex* blockindex, bool txDetails = false)
80 result.push_back(Pair("hash", block.GetHash().GetHex()));
81 int confirmations = -1;
82 // Only report confirmations if the block is on the main chain
83 if (chainActive.Contains(blockindex))
84 confirmations = chainActive.Height() - blockindex->nHeight + 1;
85 result.push_back(Pair("confirmations", confirmations));
86 result.push_back(Pair("size", (int)::GetSerializeSize(block, SER_NETWORK, PROTOCOL_VERSION)));
87 result.push_back(Pair("height", blockindex->nHeight));
88 result.push_back(Pair("version", block.nVersion));
89 result.push_back(Pair("merkleroot", block.hashMerkleRoot.GetHex()));
91 BOOST_FOREACH(const CTransaction&tx, block.vtx)
96 TxToJSON(tx, uint256(), objTx);
100 txs.push_back(tx.GetHash().GetHex());
102 result.push_back(Pair("tx", txs));
103 result.push_back(Pair("time", block.GetBlockTime()));
104 result.push_back(Pair("nonce", block.nNonce.GetHex()));
105 result.push_back(Pair("solution", HexStr(block.nSolution)));
106 result.push_back(Pair("bits", strprintf("%08x", block.nBits)));
107 result.push_back(Pair("difficulty", GetDifficulty(blockindex)));
108 result.push_back(Pair("chainwork", blockindex->nChainWork.GetHex()));
110 if (blockindex->pprev)
111 result.push_back(Pair("previousblockhash", blockindex->pprev->GetBlockHash().GetHex()));
112 CBlockIndex *pnext = chainActive.Next(blockindex);
114 result.push_back(Pair("nextblockhash", pnext->GetBlockHash().GetHex()));
119 Value getblockcount(const Array& params, bool fHelp)
121 if (fHelp || params.size() != 0)
124 "\nReturns the number of blocks in the longest block chain.\n"
126 "n (numeric) The current block count\n"
128 + HelpExampleCli("getblockcount", "")
129 + HelpExampleRpc("getblockcount", "")
133 return chainActive.Height();
136 Value getbestblockhash(const Array& params, bool fHelp)
138 if (fHelp || params.size() != 0)
141 "\nReturns the hash of the best (tip) block in the longest block chain.\n"
143 "\"hex\" (string) the block hash hex encoded\n"
145 + HelpExampleCli("getbestblockhash", "")
146 + HelpExampleRpc("getbestblockhash", "")
150 return chainActive.Tip()->GetBlockHash().GetHex();
153 Value getdifficulty(const Array& params, bool fHelp)
155 if (fHelp || params.size() != 0)
158 "\nReturns the proof-of-work difficulty as a multiple of the minimum difficulty.\n"
160 "n.nnn (numeric) the proof-of-work difficulty as a multiple of the minimum difficulty.\n"
162 + HelpExampleCli("getdifficulty", "")
163 + HelpExampleRpc("getdifficulty", "")
167 return GetNetworkDifficulty();
171 Value getrawmempool(const Array& params, bool fHelp)
173 if (fHelp || params.size() > 1)
175 "getrawmempool ( verbose )\n"
176 "\nReturns all transaction ids in memory pool as a json array of string transaction ids.\n"
178 "1. verbose (boolean, optional, default=false) true for a json object, false for array of transaction ids\n"
179 "\nResult: (for verbose = false):\n"
180 "[ (json array of string)\n"
181 " \"transactionid\" (string) The transaction id\n"
184 "\nResult: (for verbose = true):\n"
186 " \"transactionid\" : { (json object)\n"
187 " \"size\" : n, (numeric) transaction size in bytes\n"
188 " \"fee\" : n, (numeric) transaction fee in bitcoins\n"
189 " \"time\" : n, (numeric) local time transaction entered pool in seconds since 1 Jan 1970 GMT\n"
190 " \"height\" : n, (numeric) block height when transaction entered pool\n"
191 " \"startingpriority\" : n, (numeric) priority when transaction entered pool\n"
192 " \"currentpriority\" : n, (numeric) transaction priority now\n"
193 " \"depends\" : [ (array) unconfirmed transactions used as inputs for this transaction\n"
194 " \"transactionid\", (string) parent transaction id\n"
199 + HelpExampleCli("getrawmempool", "true")
200 + HelpExampleRpc("getrawmempool", "true")
205 bool fVerbose = false;
206 if (params.size() > 0)
207 fVerbose = params[0].get_bool();
213 BOOST_FOREACH(const PAIRTYPE(uint256, CTxMemPoolEntry)& entry, mempool.mapTx)
215 const uint256& hash = entry.first;
216 const CTxMemPoolEntry& e = entry.second;
218 info.push_back(Pair("size", (int)e.GetTxSize()));
219 info.push_back(Pair("fee", ValueFromAmount(e.GetFee())));
220 info.push_back(Pair("time", e.GetTime()));
221 info.push_back(Pair("height", (int)e.GetHeight()));
222 info.push_back(Pair("startingpriority", e.GetPriority(e.GetHeight())));
223 info.push_back(Pair("currentpriority", e.GetPriority(chainActive.Height())));
224 const CTransaction& tx = e.GetTx();
225 set<string> setDepends;
226 BOOST_FOREACH(const CTxIn& txin, tx.vin)
228 if (mempool.exists(txin.prevout.hash))
229 setDepends.insert(txin.prevout.hash.ToString());
231 Array depends(setDepends.begin(), setDepends.end());
232 info.push_back(Pair("depends", depends));
233 o.push_back(Pair(hash.ToString(), info));
239 vector<uint256> vtxid;
240 mempool.queryHashes(vtxid);
243 BOOST_FOREACH(const uint256& hash, vtxid)
244 a.push_back(hash.ToString());
250 Value getblockhash(const Array& params, bool fHelp)
252 if (fHelp || params.size() != 1)
254 "getblockhash index\n"
255 "\nReturns hash of block in best-block-chain at index provided.\n"
257 "1. index (numeric, required) The block index\n"
259 "\"hash\" (string) The block hash\n"
261 + HelpExampleCli("getblockhash", "1000")
262 + HelpExampleRpc("getblockhash", "1000")
267 int nHeight = params[0].get_int();
268 if (nHeight < 0 || nHeight > chainActive.Height())
269 throw JSONRPCError(RPC_INVALID_PARAMETER, "Block height out of range");
271 CBlockIndex* pblockindex = chainActive[nHeight];
272 return pblockindex->GetBlockHash().GetHex();
275 Value getblock(const Array& params, bool fHelp)
277 if (fHelp || params.size() < 1 || params.size() > 2)
279 "getblock \"hash\" ( verbose )\n"
280 "\nIf verbose is false, returns a string that is serialized, hex-encoded data for block 'hash'.\n"
281 "If verbose is true, returns an Object with information about block <hash>.\n"
283 "1. \"hash\" (string, required) The block hash\n"
284 "2. verbose (boolean, optional, default=true) true for a json object, false for the hex encoded data\n"
285 "\nResult (for verbose = true):\n"
287 " \"hash\" : \"hash\", (string) the block hash (same as provided)\n"
288 " \"confirmations\" : n, (numeric) The number of confirmations, or -1 if the block is not on the main chain\n"
289 " \"size\" : n, (numeric) The block size\n"
290 " \"height\" : n, (numeric) The block height or index\n"
291 " \"version\" : n, (numeric) The block version\n"
292 " \"merkleroot\" : \"xxxx\", (string) The merkle root\n"
293 " \"tx\" : [ (array of string) The transaction ids\n"
294 " \"transactionid\" (string) The transaction id\n"
297 " \"time\" : ttt, (numeric) The block time in seconds since epoch (Jan 1 1970 GMT)\n"
298 " \"nonce\" : n, (numeric) The nonce\n"
299 " \"bits\" : \"1d00ffff\", (string) The bits\n"
300 " \"difficulty\" : x.xxx, (numeric) The difficulty\n"
301 " \"previousblockhash\" : \"hash\", (string) The hash of the previous block\n"
302 " \"nextblockhash\" : \"hash\" (string) The hash of the next block\n"
304 "\nResult (for verbose=false):\n"
305 "\"data\" (string) A string that is serialized, hex-encoded data for block 'hash'.\n"
307 + HelpExampleCli("getblock", "\"00000000c937983704a73af28acdec37b049d214adbda81d7e2a3dd146f6ed09\"")
308 + HelpExampleRpc("getblock", "\"00000000c937983704a73af28acdec37b049d214adbda81d7e2a3dd146f6ed09\"")
313 std::string strHash = params[0].get_str();
314 uint256 hash(uint256S(strHash));
316 bool fVerbose = true;
317 if (params.size() > 1)
318 fVerbose = params[1].get_bool();
320 if (mapBlockIndex.count(hash) == 0)
321 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block not found");
324 CBlockIndex* pblockindex = mapBlockIndex[hash];
326 if (fHavePruned && !(pblockindex->nStatus & BLOCK_HAVE_DATA) && pblockindex->nTx > 0)
327 throw JSONRPCError(RPC_INTERNAL_ERROR, "Block not available (pruned data)");
329 if(!ReadBlockFromDisk(block, pblockindex))
330 throw JSONRPCError(RPC_INTERNAL_ERROR, "Can't read block from disk");
334 CDataStream ssBlock(SER_NETWORK, PROTOCOL_VERSION);
336 std::string strHex = HexStr(ssBlock.begin(), ssBlock.end());
340 return blockToJSON(block, pblockindex);
343 Value gettxoutsetinfo(const Array& params, bool fHelp)
345 if (fHelp || params.size() != 0)
348 "\nReturns statistics about the unspent transaction output set.\n"
349 "Note this call may take some time.\n"
352 " \"height\":n, (numeric) The current block height (index)\n"
353 " \"bestblock\": \"hex\", (string) the best block hash hex\n"
354 " \"transactions\": n, (numeric) The number of transactions\n"
355 " \"txouts\": n, (numeric) The number of output transactions\n"
356 " \"bytes_serialized\": n, (numeric) The serialized size\n"
357 " \"hash_serialized\": \"hash\", (string) The serialized hash\n"
358 " \"total_amount\": x.xxx (numeric) The total amount\n"
361 + HelpExampleCli("gettxoutsetinfo", "")
362 + HelpExampleRpc("gettxoutsetinfo", "")
371 if (pcoinsTip->GetStats(stats)) {
372 ret.push_back(Pair("height", (int64_t)stats.nHeight));
373 ret.push_back(Pair("bestblock", stats.hashBlock.GetHex()));
374 ret.push_back(Pair("transactions", (int64_t)stats.nTransactions));
375 ret.push_back(Pair("txouts", (int64_t)stats.nTransactionOutputs));
376 ret.push_back(Pair("bytes_serialized", (int64_t)stats.nSerializedSize));
377 ret.push_back(Pair("hash_serialized", stats.hashSerialized.GetHex()));
378 ret.push_back(Pair("total_amount", ValueFromAmount(stats.nTotalAmount)));
383 uint64_t komodo_interest(int32_t txheight,uint64_t nValue,uint32_t nLockTime,uint32_t tiptime);
384 uint32_t komodo_txtime(uint256 hash);
385 uint64_t komodo_paxprice(uint64_t *seedp,int32_t height,char *base,char *rel,uint64_t basevolume);
386 int32_t komodo_paxprices(int32_t *heights,uint64_t *prices,int32_t max,char *base,char *rel);
387 int32_t komodo_notaries(uint8_t pubkeys[64][33],int32_t height);
388 char *bitcoin_address(char *coinaddr,uint8_t addrtype,uint8_t *pubkey_or_rmd160,int32_t len);
389 uint32_t komodo_interest_args(int32_t *txheightp,uint32_t *tiptimep,uint64_t *valuep,uint256 hash,int32_t n);
391 Value notaries(const Array& params, bool fHelp)
393 Array a; Object ret; int32_t i,j,n,m; char *hexstr; uint8_t pubkeys[64][33]; char btcaddr[64],kmdaddr[64],*ptr;
394 if ( fHelp || params.size() != 1 )
395 throw runtime_error("notaries height\n");
397 int32_t height = atoi(params[0].get_str().c_str());
400 //fprintf(stderr,"notaries as of height.%d\n",height);
401 if ( height > chainActive.Height()+20000 )
402 throw JSONRPCError(RPC_INVALID_PARAMETER, "Block height out of range");
405 if ( (n= komodo_notaries(pubkeys,height)) > 0 )
410 std::string btcaddress,kmdaddress,hex;
412 hexstr = (char *)hex.data();
414 sprintf(&hexstr[j*2],"%02x",pubkeys[i][j]);
415 item.push_back(Pair("pubkey", hex));
417 bitcoin_address(btcaddr,0,pubkeys[i],33);
418 m = (int32_t)strlen(btcaddr);
419 btcaddress.resize(m);
420 ptr = (char *)btcaddress.data();
421 memcpy(ptr,btcaddr,m);
422 item.push_back(Pair("BTCaddress", btcaddress));
424 bitcoin_address(kmdaddr,60,pubkeys[i],33);
425 m = (int32_t)strlen(kmdaddr);
426 kmdaddress.resize(m);
427 ptr = (char *)kmdaddress.data();
428 memcpy(ptr,kmdaddr,m);
429 item.push_back(Pair("KMDaddress", kmdaddress));
433 ret.push_back(Pair("notaries", a));
438 Value paxprice(const Array& params, bool fHelp)
440 if ( fHelp || params.size() < 3 || params.size() > 4 )
441 throw runtime_error("paxprice \"base\" \"rel\" height amount\n");
443 Object ret; uint64_t basevolume=0,relvolume,seed;
444 std::string base = params[0].get_str();
445 std::string rel = params[1].get_str();
446 int32_t height = atoi(params[2].get_str().c_str());
447 if ( params.size() == 3 || (basevolume= COIN * atof(params[3].get_str().c_str())) == 0 )
449 relvolume = komodo_paxprice(&seed,height,(char *)base.c_str(),(char *)rel.c_str(),basevolume);
450 ret.push_back(Pair("base", base));
451 ret.push_back(Pair("rel", rel));
452 ret.push_back(Pair("height", height));
454 sprintf(seedstr,"%llu",(long long)seed);
455 ret.push_back(Pair("seed", seedstr));
456 if ( height < 0 || height > chainActive.Height() )
457 throw JSONRPCError(RPC_INVALID_PARAMETER, "Block height out of range");
460 CBlockIndex *pblockindex = chainActive[height];
461 ret.push_back(Pair("timestamp", (int64_t)pblockindex->nTime));
462 if ( basevolume != 0 && relvolume != 0 )
464 ret.push_back(Pair("price",((double)relvolume / (double)basevolume)));
465 ret.push_back(Pair("invprice",((double)basevolume / (double)relvolume)));
466 ret.push_back(Pair("basevolume", ValueFromAmount(basevolume)));
467 ret.push_back(Pair("relvolume", ValueFromAmount(relvolume)));
468 } else ret.push_back(Pair("error", "overflow or error in one or more of parameters"));
473 Value paxprices(const Array& params, bool fHelp)
475 if ( fHelp || params.size() != 3 )
476 throw runtime_error("paxprices \"base\" \"rel\" maxsamples\n");
478 Object ret; uint64_t relvolume,prices[4096]; uint32_t i,n; int32_t heights[sizeof(prices)/sizeof(*prices)];
479 std::string base = params[0].get_str();
480 std::string rel = params[1].get_str();
481 int32_t maxsamples = atoi(params[2].get_str().c_str());
482 if ( maxsamples < 1 )
484 else if ( maxsamples > sizeof(heights)/sizeof(*heights) )
485 maxsamples = sizeof(heights)/sizeof(*heights);
486 ret.push_back(Pair("base", base));
487 ret.push_back(Pair("rel", rel));
488 n = komodo_paxprices(heights,prices,maxsamples,(char *)base.c_str(),(char *)rel.c_str());
493 if ( heights[i] < 0 || heights[i] > chainActive.Height() )
494 throw JSONRPCError(RPC_INVALID_PARAMETER, "Block height out of range");
497 CBlockIndex *pblockindex = chainActive[heights[i]];
499 item.push_back(Pair("t", (int64_t)pblockindex->nTime));
500 item.push_back(Pair("p", (double)prices[i] / COIN));
504 ret.push_back(Pair("array", a));
508 uint64_t komodo_accrued_interest(int32_t *txheightp,uint32_t *locktimep,uint256 hash,int32_t n,int32_t checkheight,uint64_t checkvalue);
510 Value gettxout(const Array& params, bool fHelp)
512 if (fHelp || params.size() < 2 || params.size() > 3)
514 "gettxout \"txid\" n ( includemempool )\n"
515 "\nReturns details about an unspent transaction output.\n"
517 "1. \"txid\" (string, required) The transaction id\n"
518 "2. n (numeric, required) vout value\n"
519 "3. includemempool (boolean, optional) Whether to included the mem pool\n"
522 " \"bestblock\" : \"hash\", (string) the block hash\n"
523 " \"confirmations\" : n, (numeric) The number of confirmations\n"
524 " \"value\" : x.xxx, (numeric) The transaction value in btc\n"
525 " \"scriptPubKey\" : { (json object)\n"
526 " \"asm\" : \"code\", (string) \n"
527 " \"hex\" : \"hex\", (string) \n"
528 " \"reqSigs\" : n, (numeric) Number of required signatures\n"
529 " \"type\" : \"pubkeyhash\", (string) The type, eg pubkeyhash\n"
530 " \"addresses\" : [ (array of string) array of bitcoin addresses\n"
531 " \"bitcoinaddress\" (string) bitcoin address\n"
535 " \"version\" : n, (numeric) The version\n"
536 " \"coinbase\" : true|false (boolean) Coinbase or not\n"
540 "\nGet unspent transactions\n"
541 + HelpExampleCli("listunspent", "") +
542 "\nView the details\n"
543 + HelpExampleCli("gettxout", "\"txid\" 1") +
544 "\nAs a json rpc call\n"
545 + HelpExampleRpc("gettxout", "\"txid\", 1")
552 std::string strHash = params[0].get_str();
553 uint256 hash(uint256S(strHash));
554 int n = params[1].get_int();
555 bool fMempool = true;
556 if (params.size() > 2)
557 fMempool = params[2].get_bool();
562 CCoinsViewMemPool view(pcoinsTip, mempool);
563 if (!view.GetCoins(hash, coins))
565 mempool.pruneSpent(hash, coins); // TODO: this should be done by the CCoinsViewMemPool
567 if (!pcoinsTip->GetCoins(hash, coins))
570 if (n<0 || (unsigned int)n>=coins.vout.size() || coins.vout[n].IsNull())
573 BlockMap::iterator it = mapBlockIndex.find(pcoinsTip->GetBestBlock());
574 CBlockIndex *pindex = it->second;
575 ret.push_back(Pair("bestblock", pindex->GetBlockHash().GetHex()));
576 if ((unsigned int)coins.nHeight == MEMPOOL_HEIGHT)
577 ret.push_back(Pair("confirmations", 0));
578 else ret.push_back(Pair("confirmations", pindex->nHeight - coins.nHeight + 1));
579 ret.push_back(Pair("value", ValueFromAmount(coins.vout[n].nValue)));
580 uint64_t interest; int32_t txheight; uint32_t locktime;
581 if ( (interest= komodo_accrued_interest(&txheight,&locktime,hash,n,coins.nHeight,coins.vout[n].nValue)) != 0 )
582 ret.push_back(Pair("interest", ValueFromAmount(interest)));
584 ScriptPubKeyToJSON(coins.vout[n].scriptPubKey, o, true);
585 ret.push_back(Pair("scriptPubKey", o));
586 ret.push_back(Pair("version", coins.nVersion));
587 ret.push_back(Pair("coinbase", coins.fCoinBase));
592 Value verifychain(const Array& params, bool fHelp)
594 if (fHelp || params.size() > 2)
596 "verifychain ( checklevel numblocks )\n"
597 "\nVerifies blockchain database.\n"
599 "1. checklevel (numeric, optional, 0-4, default=3) How thorough the block verification is.\n"
600 "2. numblocks (numeric, optional, default=288, 0=all) The number of blocks to check.\n"
602 "true|false (boolean) Verified or not\n"
604 + HelpExampleCli("verifychain", "")
605 + HelpExampleRpc("verifychain", "")
610 int nCheckLevel = GetArg("-checklevel", 3);
611 int nCheckDepth = GetArg("-checkblocks", 288);
612 if (params.size() > 0)
613 nCheckLevel = params[0].get_int();
614 if (params.size() > 1)
615 nCheckDepth = params[1].get_int();
617 return CVerifyDB().VerifyDB(pcoinsTip, nCheckLevel, nCheckDepth);
620 /** Implementation of IsSuperMajority with better feedback */
621 Object SoftForkMajorityDesc(int minVersion, CBlockIndex* pindex, int nRequired, const Consensus::Params& consensusParams)
624 CBlockIndex* pstart = pindex;
625 for (int i = 0; i < consensusParams.nMajorityWindow && pstart != NULL; i++)
627 if (pstart->nVersion >= minVersion)
629 pstart = pstart->pprev;
633 rv.push_back(Pair("status", nFound >= nRequired));
634 rv.push_back(Pair("found", nFound));
635 rv.push_back(Pair("required", nRequired));
636 rv.push_back(Pair("window", consensusParams.nMajorityWindow));
640 Object SoftForkDesc(const std::string &name, int version, CBlockIndex* pindex, const Consensus::Params& consensusParams)
643 rv.push_back(Pair("id", name));
644 rv.push_back(Pair("version", version));
645 rv.push_back(Pair("enforce", SoftForkMajorityDesc(version, pindex, consensusParams.nMajorityEnforceBlockUpgrade, consensusParams)));
646 rv.push_back(Pair("reject", SoftForkMajorityDesc(version, pindex, consensusParams.nMajorityRejectBlockOutdated, consensusParams)));
650 Value getblockchaininfo(const Array& params, bool fHelp)
652 if (fHelp || params.size() != 0)
654 "getblockchaininfo\n"
655 "Returns an object containing various state info regarding block chain processing.\n"
658 " \"chain\": \"xxxx\", (string) current network name as defined in BIP70 (main, test, regtest)\n"
659 " \"blocks\": xxxxxx, (numeric) the current number of blocks processed in the server\n"
660 " \"headers\": xxxxxx, (numeric) the current number of headers we have validated\n"
661 " \"bestblockhash\": \"...\", (string) the hash of the currently best block\n"
662 " \"difficulty\": xxxxxx, (numeric) the current difficulty\n"
663 " \"verificationprogress\": xxxx, (numeric) estimate of verification progress [0..1]\n"
664 " \"chainwork\": \"xxxx\" (string) total amount of work in active chain, in hexadecimal\n"
665 " \"softforks\": [ (array) status of softforks in progress\n"
667 " \"id\": \"xxxx\", (string) name of softfork\n"
668 " \"version\": xx, (numeric) block version\n"
669 " \"enforce\": { (object) progress toward enforcing the softfork rules for new-version blocks\n"
670 " \"status\": xx, (boolean) true if threshold reached\n"
671 " \"found\": xx, (numeric) number of blocks with the new version found\n"
672 " \"required\": xx, (numeric) number of blocks required to trigger\n"
673 " \"window\": xx, (numeric) maximum size of examined window of recent blocks\n"
675 " \"reject\": { ... } (object) progress toward rejecting pre-softfork blocks (same fields as \"enforce\")\n"
680 + HelpExampleCli("getblockchaininfo", "")
681 + HelpExampleRpc("getblockchaininfo", "")
687 obj.push_back(Pair("chain", Params().NetworkIDString()));
688 obj.push_back(Pair("blocks", (int)chainActive.Height()));
689 obj.push_back(Pair("headers", pindexBestHeader ? pindexBestHeader->nHeight : -1));
690 obj.push_back(Pair("bestblockhash", chainActive.Tip()->GetBlockHash().GetHex()));
691 obj.push_back(Pair("difficulty", (double)GetNetworkDifficulty()));
692 obj.push_back(Pair("verificationprogress", Checkpoints::GuessVerificationProgress(Params().Checkpoints(), chainActive.Tip())));
693 obj.push_back(Pair("chainwork", chainActive.Tip()->nChainWork.GetHex()));
694 obj.push_back(Pair("pruned", fPruneMode));
696 const Consensus::Params& consensusParams = Params().GetConsensus();
697 CBlockIndex* tip = chainActive.Tip();
699 softforks.push_back(SoftForkDesc("bip34", 2, tip, consensusParams));
700 softforks.push_back(SoftForkDesc("bip66", 3, tip, consensusParams));
701 softforks.push_back(SoftForkDesc("bip65", 4, tip, consensusParams));
702 obj.push_back(Pair("softforks", softforks));
706 CBlockIndex *block = chainActive.Tip();
707 while (block && block->pprev && (block->pprev->nStatus & BLOCK_HAVE_DATA))
708 block = block->pprev;
710 obj.push_back(Pair("pruneheight", block->nHeight));
715 /** Comparison function for sorting the getchaintips heads. */
716 struct CompareBlocksByHeight
718 bool operator()(const CBlockIndex* a, const CBlockIndex* b) const
720 /* Make sure that unequal blocks with the same height do not compare
721 equal. Use the pointers themselves to make a distinction. */
723 if (a->nHeight != b->nHeight)
724 return (a->nHeight > b->nHeight);
730 Value getchaintips(const Array& params, bool fHelp)
732 if (fHelp || params.size() != 0)
735 "Return information about all known tips in the block tree,"
736 " including the main chain as well as orphaned branches.\n"
740 " \"height\": xxxx, (numeric) height of the chain tip\n"
741 " \"hash\": \"xxxx\", (string) block hash of the tip\n"
742 " \"branchlen\": 0 (numeric) zero for main chain\n"
743 " \"status\": \"active\" (string) \"active\" for the main chain\n"
746 " \"height\": xxxx,\n"
747 " \"hash\": \"xxxx\",\n"
748 " \"branchlen\": 1 (numeric) length of branch connecting the tip to the main chain\n"
749 " \"status\": \"xxxx\" (string) status of the chain (active, valid-fork, valid-headers, headers-only, invalid)\n"
752 "Possible values for status:\n"
753 "1. \"invalid\" This branch contains at least one invalid block\n"
754 "2. \"headers-only\" Not all blocks for this branch are available, but the headers are valid\n"
755 "3. \"valid-headers\" All blocks are available for this branch, but they were never fully validated\n"
756 "4. \"valid-fork\" This branch is not part of the active chain, but is fully validated\n"
757 "5. \"active\" This is the tip of the active main chain, which is certainly valid\n"
759 + HelpExampleCli("getchaintips", "")
760 + HelpExampleRpc("getchaintips", "")
765 /* Build up a list of chain tips. We start with the list of all
766 known blocks, and successively remove blocks that appear as pprev
768 std::set<const CBlockIndex*, CompareBlocksByHeight> setTips;
769 BOOST_FOREACH(const PAIRTYPE(const uint256, CBlockIndex*)& item, mapBlockIndex)
770 setTips.insert(item.second);
771 BOOST_FOREACH(const PAIRTYPE(const uint256, CBlockIndex*)& item, mapBlockIndex)
773 const CBlockIndex* pprev = item.second->pprev;
775 setTips.erase(pprev);
778 // Always report the currently active tip.
779 setTips.insert(chainActive.Tip());
781 /* Construct the output array. */
783 BOOST_FOREACH(const CBlockIndex* block, setTips)
786 obj.push_back(Pair("height", block->nHeight));
787 obj.push_back(Pair("hash", block->phashBlock->GetHex()));
789 const int branchLen = block->nHeight - chainActive.FindFork(block)->nHeight;
790 obj.push_back(Pair("branchlen", branchLen));
793 if (chainActive.Contains(block)) {
794 // This block is part of the currently active chain.
796 } else if (block->nStatus & BLOCK_FAILED_MASK) {
797 // This block or one of its ancestors is invalid.
799 } else if (block->nChainTx == 0) {
800 // This block cannot be connected because full block data for it or one of its parents is missing.
801 status = "headers-only";
802 } else if (block->IsValid(BLOCK_VALID_SCRIPTS)) {
803 // This block is fully validated, but no longer part of the active chain. It was probably the active block once, but was reorganized.
804 status = "valid-fork";
805 } else if (block->IsValid(BLOCK_VALID_TREE)) {
806 // The headers for this block are valid, but it has not been validated. It was probably never part of the most-work chain.
807 status = "valid-headers";
812 obj.push_back(Pair("status", status));
820 Value getmempoolinfo(const Array& params, bool fHelp)
822 if (fHelp || params.size() != 0)
825 "\nReturns details on the active state of the TX memory pool.\n"
828 " \"size\": xxxxx (numeric) Current tx count\n"
829 " \"bytes\": xxxxx (numeric) Sum of all tx sizes\n"
832 + HelpExampleCli("getmempoolinfo", "")
833 + HelpExampleRpc("getmempoolinfo", "")
837 ret.push_back(Pair("size", (int64_t) mempool.size()));
838 ret.push_back(Pair("bytes", (int64_t) mempool.GetTotalTxSize()));
843 Value invalidateblock(const Array& params, bool fHelp)
845 if (fHelp || params.size() != 1)
847 "invalidateblock \"hash\"\n"
848 "\nPermanently marks a block as invalid, as if it violated a consensus rule.\n"
850 "1. hash (string, required) the hash of the block to mark as invalid\n"
853 + HelpExampleCli("invalidateblock", "\"blockhash\"")
854 + HelpExampleRpc("invalidateblock", "\"blockhash\"")
857 std::string strHash = params[0].get_str();
858 uint256 hash(uint256S(strHash));
859 CValidationState state;
863 if (mapBlockIndex.count(hash) == 0)
864 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block not found");
866 CBlockIndex* pblockindex = mapBlockIndex[hash];
867 InvalidateBlock(state, pblockindex);
870 if (state.IsValid()) {
871 ActivateBestChain(state);
874 if (!state.IsValid()) {
875 throw JSONRPCError(RPC_DATABASE_ERROR, state.GetRejectReason());
881 Value reconsiderblock(const Array& params, bool fHelp)
883 if (fHelp || params.size() != 1)
885 "reconsiderblock \"hash\"\n"
886 "\nRemoves invalidity status of a block and its descendants, reconsider them for activation.\n"
887 "This can be used to undo the effects of invalidateblock.\n"
889 "1. hash (string, required) the hash of the block to reconsider\n"
892 + HelpExampleCli("reconsiderblock", "\"blockhash\"")
893 + HelpExampleRpc("reconsiderblock", "\"blockhash\"")
896 std::string strHash = params[0].get_str();
897 uint256 hash(uint256S(strHash));
898 CValidationState state;
902 if (mapBlockIndex.count(hash) == 0)
903 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block not found");
905 CBlockIndex* pblockindex = mapBlockIndex[hash];
906 ReconsiderBlock(state, pblockindex);
909 if (state.IsValid()) {
910 ActivateBestChain(state);
913 if (!state.IsValid()) {
914 throw JSONRPCError(RPC_DATABASE_ERROR, state.GetRejectReason());