You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

rpcblockchain.cpp 22KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  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.
  5. #include "checkpoints.h"
  6. #include "main.h"
  7. #include "rpcserver.h"
  8. #include "sync.h"
  9. #include "util.h"
  10. #include <stdint.h>
  11. #include "json/json_spirit_value.h"
  12. using namespace json_spirit;
  13. using namespace std;
  14. void ScriptPubKeyToJSON(const CScript& scriptPubKey, Object& out, bool fIncludeHex);
  15. double GetDifficulty(const CBlockIndex* blockindex)
  16. {
  17. // Floating point number that is a multiple of the minimum difficulty,
  18. // minimum difficulty = 1.0.
  19. if (blockindex == NULL)
  20. {
  21. if (chainActive.Tip() == NULL)
  22. return 1.0;
  23. else
  24. blockindex = chainActive.Tip();
  25. }
  26. int nShift = (blockindex->nBits >> 24) & 0xff;
  27. double dDiff =
  28. (double)0x0000ffff / (double)(blockindex->nBits & 0x00ffffff);
  29. while (nShift < 29)
  30. {
  31. dDiff *= 256.0;
  32. nShift++;
  33. }
  34. while (nShift > 29)
  35. {
  36. dDiff /= 256.0;
  37. nShift--;
  38. }
  39. return dDiff;
  40. }
  41. Object blockToJSON(const CBlock& block, const CBlockIndex* blockindex)
  42. {
  43. Object result;
  44. result.push_back(Pair("hash", block.GetHash().GetHex()));
  45. int confirmations = -1;
  46. // Only report confirmations if the block is on the main chain
  47. if (chainActive.Contains(blockindex))
  48. confirmations = chainActive.Height() - blockindex->nHeight + 1;
  49. result.push_back(Pair("confirmations", confirmations));
  50. result.push_back(Pair("size", (int)::GetSerializeSize(block, SER_NETWORK, PROTOCOL_VERSION)));
  51. result.push_back(Pair("height", blockindex->nHeight));
  52. result.push_back(Pair("version", block.nVersion));
  53. result.push_back(Pair("merkleroot", block.hashMerkleRoot.GetHex()));
  54. Array txs;
  55. BOOST_FOREACH(const CTransaction&tx, block.vtx)
  56. txs.push_back(tx.GetHash().GetHex());
  57. result.push_back(Pair("tx", txs));
  58. result.push_back(Pair("time", block.GetBlockTime()));
  59. result.push_back(Pair("nonce", (uint64_t)block.nNonce));
  60. result.push_back(Pair("bits", strprintf("%08x", block.nBits)));
  61. result.push_back(Pair("difficulty", GetDifficulty(blockindex)));
  62. result.push_back(Pair("chainwork", blockindex->nChainWork.GetHex()));
  63. if (blockindex->pprev)
  64. result.push_back(Pair("previousblockhash", blockindex->pprev->GetBlockHash().GetHex()));
  65. CBlockIndex *pnext = chainActive.Next(blockindex);
  66. if (pnext)
  67. result.push_back(Pair("nextblockhash", pnext->GetBlockHash().GetHex()));
  68. return result;
  69. }
  70. Value getblockcount(const Array& params, bool fHelp)
  71. {
  72. if (fHelp || params.size() != 0)
  73. throw runtime_error(
  74. "getblockcount\n"
  75. "\nReturns the number of blocks in the longest block chain.\n"
  76. "\nResult:\n"
  77. "n (numeric) The current block count\n"
  78. "\nExamples:\n"
  79. + HelpExampleCli("getblockcount", "")
  80. + HelpExampleRpc("getblockcount", "")
  81. );
  82. return chainActive.Height();
  83. }
  84. Value getbestblockhash(const Array& params, bool fHelp)
  85. {
  86. if (fHelp || params.size() != 0)
  87. throw runtime_error(
  88. "getbestblockhash\n"
  89. "\nReturns the hash of the best (tip) block in the longest block chain.\n"
  90. "\nResult\n"
  91. "\"hex\" (string) the block hash hex encoded\n"
  92. "\nExamples\n"
  93. + HelpExampleCli("getbestblockhash", "")
  94. + HelpExampleRpc("getbestblockhash", "")
  95. );
  96. return chainActive.Tip()->GetBlockHash().GetHex();
  97. }
  98. Value getdifficulty(const Array& params, bool fHelp)
  99. {
  100. if (fHelp || params.size() != 0)
  101. throw runtime_error(
  102. "getdifficulty\n"
  103. "\nReturns the proof-of-work difficulty as a multiple of the minimum difficulty.\n"
  104. "\nResult:\n"
  105. "n.nnn (numeric) the proof-of-work difficulty as a multiple of the minimum difficulty.\n"
  106. "\nExamples:\n"
  107. + HelpExampleCli("getdifficulty", "")
  108. + HelpExampleRpc("getdifficulty", "")
  109. );
  110. return GetDifficulty();
  111. }
  112. Value getrawmempool(const Array& params, bool fHelp)
  113. {
  114. if (fHelp || params.size() > 1)
  115. throw runtime_error(
  116. "getrawmempool ( verbose )\n"
  117. "\nReturns all transaction ids in memory pool as a json array of string transaction ids.\n"
  118. "\nArguments:\n"
  119. "1. verbose (boolean, optional, default=false) true for a json object, false for array of transaction ids\n"
  120. "\nResult: (for verbose = false):\n"
  121. "[ (json array of string)\n"
  122. " \"transactionid\" (string) The transaction id\n"
  123. " ,...\n"
  124. "]\n"
  125. "\nResult: (for verbose = true):\n"
  126. "{ (json object)\n"
  127. " \"transactionid\" : { (json object)\n"
  128. " \"size\" : n, (numeric) transaction size in bytes\n"
  129. " \"fee\" : n, (numeric) transaction fee in bitcoins\n"
  130. " \"time\" : n, (numeric) local time transaction entered pool in seconds since 1 Jan 1970 GMT\n"
  131. " \"height\" : n, (numeric) block height when transaction entered pool\n"
  132. " \"startingpriority\" : n, (numeric) priority when transaction entered pool\n"
  133. " \"currentpriority\" : n, (numeric) transaction priority now\n"
  134. " \"depends\" : [ (array) unconfirmed transactions used as inputs for this transaction\n"
  135. " \"transactionid\", (string) parent transaction id\n"
  136. " ... ]\n"
  137. " }, ...\n"
  138. "]\n"
  139. "\nExamples\n"
  140. + HelpExampleCli("getrawmempool", "true")
  141. + HelpExampleRpc("getrawmempool", "true")
  142. );
  143. bool fVerbose = false;
  144. if (params.size() > 0)
  145. fVerbose = params[0].get_bool();
  146. if (fVerbose)
  147. {
  148. LOCK(mempool.cs);
  149. Object o;
  150. BOOST_FOREACH(const PAIRTYPE(uint256, CTxMemPoolEntry)& entry, mempool.mapTx)
  151. {
  152. const uint256& hash = entry.first;
  153. const CTxMemPoolEntry& e = entry.second;
  154. Object info;
  155. info.push_back(Pair("size", (int)e.GetTxSize()));
  156. info.push_back(Pair("fee", ValueFromAmount(e.GetFee())));
  157. info.push_back(Pair("time", e.GetTime()));
  158. info.push_back(Pair("height", (int)e.GetHeight()));
  159. info.push_back(Pair("startingpriority", e.GetPriority(e.GetHeight())));
  160. info.push_back(Pair("currentpriority", e.GetPriority(chainActive.Height())));
  161. const CTransaction& tx = e.GetTx();
  162. set<string> setDepends;
  163. BOOST_FOREACH(const CTxIn& txin, tx.vin)
  164. {
  165. if (mempool.exists(txin.prevout.hash))
  166. setDepends.insert(txin.prevout.hash.ToString());
  167. }
  168. Array depends(setDepends.begin(), setDepends.end());
  169. info.push_back(Pair("depends", depends));
  170. o.push_back(Pair(hash.ToString(), info));
  171. }
  172. return o;
  173. }
  174. else
  175. {
  176. vector<uint256> vtxid;
  177. mempool.queryHashes(vtxid);
  178. Array a;
  179. BOOST_FOREACH(const uint256& hash, vtxid)
  180. a.push_back(hash.ToString());
  181. return a;
  182. }
  183. }
  184. Value getblockhash(const Array& params, bool fHelp)
  185. {
  186. if (fHelp || params.size() != 1)
  187. throw runtime_error(
  188. "getblockhash index\n"
  189. "\nReturns hash of block in best-block-chain at index provided.\n"
  190. "\nArguments:\n"
  191. "1. index (numeric, required) The block index\n"
  192. "\nResult:\n"
  193. "\"hash\" (string) The block hash\n"
  194. "\nExamples:\n"
  195. + HelpExampleCli("getblockhash", "1000")
  196. + HelpExampleRpc("getblockhash", "1000")
  197. );
  198. int nHeight = params[0].get_int();
  199. if (nHeight < 0 || nHeight > chainActive.Height())
  200. throw JSONRPCError(RPC_INVALID_PARAMETER, "Block height out of range");
  201. CBlockIndex* pblockindex = chainActive[nHeight];
  202. return pblockindex->GetBlockHash().GetHex();
  203. }
  204. Value getblock(const Array& params, bool fHelp)
  205. {
  206. if (fHelp || params.size() < 1 || params.size() > 2)
  207. throw runtime_error(
  208. "getblock \"hash\" ( verbose )\n"
  209. "\nIf verbose is false, returns a string that is serialized, hex-encoded data for block 'hash'.\n"
  210. "If verbose is true, returns an Object with information about block <hash>.\n"
  211. "\nArguments:\n"
  212. "1. \"hash\" (string, required) The block hash\n"
  213. "2. verbose (boolean, optional, default=true) true for a json object, false for the hex encoded data\n"
  214. "\nResult (for verbose = true):\n"
  215. "{\n"
  216. " \"hash\" : \"hash\", (string) the block hash (same as provided)\n"
  217. " \"confirmations\" : n, (numeric) The number of confirmations, or -1 if the block is not on the main chain\n"
  218. " \"size\" : n, (numeric) The block size\n"
  219. " \"height\" : n, (numeric) The block height or index\n"
  220. " \"version\" : n, (numeric) The block version\n"
  221. " \"merkleroot\" : \"xxxx\", (string) The merkle root\n"
  222. " \"tx\" : [ (array of string) The transaction ids\n"
  223. " \"transactionid\" (string) The transaction id\n"
  224. " ,...\n"
  225. " ],\n"
  226. " \"time\" : ttt, (numeric) The block time in seconds since epoch (Jan 1 1970 GMT)\n"
  227. " \"nonce\" : n, (numeric) The nonce\n"
  228. " \"bits\" : \"1d00ffff\", (string) The bits\n"
  229. " \"difficulty\" : x.xxx, (numeric) The difficulty\n"
  230. " \"previousblockhash\" : \"hash\", (string) The hash of the previous block\n"
  231. " \"nextblockhash\" : \"hash\" (string) The hash of the next block\n"
  232. "}\n"
  233. "\nResult (for verbose=false):\n"
  234. "\"data\" (string) A string that is serialized, hex-encoded data for block 'hash'.\n"
  235. "\nExamples:\n"
  236. + HelpExampleCli("getblock", "\"00000000c937983704a73af28acdec37b049d214adbda81d7e2a3dd146f6ed09\"")
  237. + HelpExampleRpc("getblock", "\"00000000c937983704a73af28acdec37b049d214adbda81d7e2a3dd146f6ed09\"")
  238. );
  239. std::string strHash = params[0].get_str();
  240. uint256 hash(strHash);
  241. bool fVerbose = true;
  242. if (params.size() > 1)
  243. fVerbose = params[1].get_bool();
  244. if (mapBlockIndex.count(hash) == 0)
  245. throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block not found");
  246. CBlock block;
  247. CBlockIndex* pblockindex = mapBlockIndex[hash];
  248. if(!ReadBlockFromDisk(block, pblockindex))
  249. throw JSONRPCError(RPC_INTERNAL_ERROR, "Can't read block from disk");
  250. if (!fVerbose)
  251. {
  252. CDataStream ssBlock(SER_NETWORK, PROTOCOL_VERSION);
  253. ssBlock << block;
  254. std::string strHex = HexStr(ssBlock.begin(), ssBlock.end());
  255. return strHex;
  256. }
  257. return blockToJSON(block, pblockindex);
  258. }
  259. Value gettxoutsetinfo(const Array& params, bool fHelp)
  260. {
  261. if (fHelp || params.size() != 0)
  262. throw runtime_error(
  263. "gettxoutsetinfo\n"
  264. "\nReturns statistics about the unspent transaction output set.\n"
  265. "Note this call may take some time.\n"
  266. "\nResult:\n"
  267. "{\n"
  268. " \"height\":n, (numeric) The current block height (index)\n"
  269. " \"bestblock\": \"hex\", (string) the best block hash hex\n"
  270. " \"transactions\": n, (numeric) The number of transactions\n"
  271. " \"txouts\": n, (numeric) The number of output transactions\n"
  272. " \"bytes_serialized\": n, (numeric) The serialized size\n"
  273. " \"hash_serialized\": \"hash\", (string) The serialized hash\n"
  274. " \"total_amount\": x.xxx (numeric) The total amount\n"
  275. "}\n"
  276. "\nExamples:\n"
  277. + HelpExampleCli("gettxoutsetinfo", "")
  278. + HelpExampleRpc("gettxoutsetinfo", "")
  279. );
  280. Object ret;
  281. CCoinsStats stats;
  282. pcoinsTip->Flush();
  283. if (pcoinsTip->GetStats(stats)) {
  284. ret.push_back(Pair("height", (int64_t)stats.nHeight));
  285. ret.push_back(Pair("bestblock", stats.hashBlock.GetHex()));
  286. ret.push_back(Pair("transactions", (int64_t)stats.nTransactions));
  287. ret.push_back(Pair("txouts", (int64_t)stats.nTransactionOutputs));
  288. ret.push_back(Pair("bytes_serialized", (int64_t)stats.nSerializedSize));
  289. ret.push_back(Pair("hash_serialized", stats.hashSerialized.GetHex()));
  290. ret.push_back(Pair("total_amount", ValueFromAmount(stats.nTotalAmount)));
  291. }
  292. return ret;
  293. }
  294. Value gettxout(const Array& params, bool fHelp)
  295. {
  296. if (fHelp || params.size() < 2 || params.size() > 3)
  297. throw runtime_error(
  298. "gettxout \"txid\" n ( includemempool )\n"
  299. "\nReturns details about an unspent transaction output.\n"
  300. "\nArguments:\n"
  301. "1. \"txid\" (string, required) The transaction id\n"
  302. "2. n (numeric, required) vout value\n"
  303. "3. includemempool (boolean, optional) Whether to included the mem pool\n"
  304. "\nResult:\n"
  305. "{\n"
  306. " \"bestblock\" : \"hash\", (string) the block hash\n"
  307. " \"confirmations\" : n, (numeric) The number of confirmations\n"
  308. " \"value\" : x.xxx, (numeric) The transaction value in btc\n"
  309. " \"scriptPubKey\" : { (json object)\n"
  310. " \"asm\" : \"code\", (string) \n"
  311. " \"hex\" : \"hex\", (string) \n"
  312. " \"reqSigs\" : n, (numeric) Number of required signatures\n"
  313. " \"type\" : \"pubkeyhash\", (string) The type, eg pubkeyhash\n"
  314. " \"addresses\" : [ (array of string) array of bitcoin addresses\n"
  315. " \"bitcoinaddress\" (string) bitcoin address\n"
  316. " ,...\n"
  317. " ]\n"
  318. " },\n"
  319. " \"version\" : n, (numeric) The version\n"
  320. " \"coinbase\" : true|false (boolean) Coinbase or not\n"
  321. "}\n"
  322. "\nExamples:\n"
  323. "\nGet unspent transactions\n"
  324. + HelpExampleCli("listunspent", "") +
  325. "\nView the details\n"
  326. + HelpExampleCli("gettxout", "\"txid\" 1") +
  327. "\nAs a json rpc call\n"
  328. + HelpExampleRpc("gettxout", "\"txid\", 1")
  329. );
  330. Object ret;
  331. std::string strHash = params[0].get_str();
  332. uint256 hash(strHash);
  333. int n = params[1].get_int();
  334. bool fMempool = true;
  335. if (params.size() > 2)
  336. fMempool = params[2].get_bool();
  337. CCoins coins;
  338. if (fMempool) {
  339. LOCK(mempool.cs);
  340. CCoinsViewMemPool view(pcoinsTip, mempool);
  341. if (!view.GetCoins(hash, coins))
  342. return Value::null;
  343. mempool.pruneSpent(hash, coins); // TODO: this should be done by the CCoinsViewMemPool
  344. } else {
  345. if (!pcoinsTip->GetCoins(hash, coins))
  346. return Value::null;
  347. }
  348. if (n<0 || (unsigned int)n>=coins.vout.size() || coins.vout[n].IsNull())
  349. return Value::null;
  350. BlockMap::iterator it = mapBlockIndex.find(pcoinsTip->GetBestBlock());
  351. CBlockIndex *pindex = it->second;
  352. ret.push_back(Pair("bestblock", pindex->GetBlockHash().GetHex()));
  353. if ((unsigned int)coins.nHeight == MEMPOOL_HEIGHT)
  354. ret.push_back(Pair("confirmations", 0));
  355. else
  356. ret.push_back(Pair("confirmations", pindex->nHeight - coins.nHeight + 1));
  357. ret.push_back(Pair("value", ValueFromAmount(coins.vout[n].nValue)));
  358. Object o;
  359. ScriptPubKeyToJSON(coins.vout[n].scriptPubKey, o, true);
  360. ret.push_back(Pair("scriptPubKey", o));
  361. ret.push_back(Pair("version", coins.nVersion));
  362. ret.push_back(Pair("coinbase", coins.fCoinBase));
  363. return ret;
  364. }
  365. Value verifychain(const Array& params, bool fHelp)
  366. {
  367. if (fHelp || params.size() > 2)
  368. throw runtime_error(
  369. "verifychain ( checklevel numblocks )\n"
  370. "\nVerifies blockchain database.\n"
  371. "\nArguments:\n"
  372. "1. checklevel (numeric, optional, 0-4, default=3) How thorough the block verification is.\n"
  373. "2. numblocks (numeric, optional, default=288, 0=all) The number of blocks to check.\n"
  374. "\nResult:\n"
  375. "true|false (boolean) Verified or not\n"
  376. "\nExamples:\n"
  377. + HelpExampleCli("verifychain", "")
  378. + HelpExampleRpc("verifychain", "")
  379. );
  380. int nCheckLevel = GetArg("-checklevel", 3);
  381. int nCheckDepth = GetArg("-checkblocks", 288);
  382. if (params.size() > 0)
  383. nCheckLevel = params[0].get_int();
  384. if (params.size() > 1)
  385. nCheckDepth = params[1].get_int();
  386. return CVerifyDB().VerifyDB(pcoinsTip, nCheckLevel, nCheckDepth);
  387. }
  388. Value getblockchaininfo(const Array& params, bool fHelp)
  389. {
  390. if (fHelp || params.size() != 0)
  391. throw runtime_error(
  392. "getblockchaininfo\n"
  393. "Returns an object containing various state info regarding block chain processing.\n"
  394. "\nResult:\n"
  395. "{\n"
  396. " \"chain\": \"xxxx\", (string) current network name as defined in BIP70 (main, test, regtest)\n"
  397. " \"blocks\": xxxxxx, (numeric) the current number of blocks processed in the server\n"
  398. " \"headers\": xxxxxx, (numeric) the current number of headers we have validated\n"
  399. " \"bestblockhash\": \"...\", (string) the hash of the currently best block\n"
  400. " \"difficulty\": xxxxxx, (numeric) the current difficulty\n"
  401. " \"verificationprogress\": xxxx, (numeric) estimate of verification progress [0..1]\n"
  402. " \"chainwork\": \"xxxx\" (string) total amount of work in active chain, in hexadecimal\n"
  403. "}\n"
  404. "\nExamples:\n"
  405. + HelpExampleCli("getblockchaininfo", "")
  406. + HelpExampleRpc("getblockchaininfo", "")
  407. );
  408. Object obj;
  409. obj.push_back(Pair("chain", Params().NetworkIDString()));
  410. obj.push_back(Pair("blocks", (int)chainActive.Height()));
  411. obj.push_back(Pair("headers", pindexBestHeader ? pindexBestHeader->nHeight : -1));
  412. obj.push_back(Pair("bestblockhash", chainActive.Tip()->GetBlockHash().GetHex()));
  413. obj.push_back(Pair("difficulty", (double)GetDifficulty()));
  414. obj.push_back(Pair("verificationprogress", Checkpoints::GuessVerificationProgress(chainActive.Tip())));
  415. obj.push_back(Pair("chainwork", chainActive.Tip()->nChainWork.GetHex()));
  416. return obj;
  417. }
  418. /* Comparison function for sorting the getchaintips heads. */
  419. struct CompareBlocksByHeight
  420. {
  421. bool operator()(const CBlockIndex* a, const CBlockIndex* b) const
  422. {
  423. /* Make sure that unequal blocks with the same height do not compare
  424. equal. Use the pointers themselves to make a distinction. */
  425. if (a->nHeight != b->nHeight)
  426. return (a->nHeight > b->nHeight);
  427. return a < b;
  428. }
  429. };
  430. Value getchaintips(const Array& params, bool fHelp)
  431. {
  432. if (fHelp || params.size() != 0)
  433. throw runtime_error(
  434. "getchaintips\n"
  435. "Return information about all known tips in the block tree,"
  436. " including the main chain as well as orphaned branches.\n"
  437. "\nResult:\n"
  438. "[\n"
  439. " {\n"
  440. " \"height\": xxxx, (numeric) height of the chain tip\n"
  441. " \"hash\": \"xxxx\", (string) block hash of the tip\n"
  442. " \"branchlen\": 0 (numeric) zero for main chain\n"
  443. " },\n"
  444. " {\n"
  445. " \"height\": xxxx,\n"
  446. " \"hash\": \"xxxx\",\n"
  447. " \"branchlen\": 1 (numeric) length of branch connecting the tip to the main chain\n"
  448. " }\n"
  449. "]\n"
  450. "\nExamples:\n"
  451. + HelpExampleCli("getchaintips", "")
  452. + HelpExampleRpc("getchaintips", "")
  453. );
  454. /* Build up a list of chain tips. We start with the list of all
  455. known blocks, and successively remove blocks that appear as pprev
  456. of another block. */
  457. std::set<const CBlockIndex*, CompareBlocksByHeight> setTips;
  458. BOOST_FOREACH(const PAIRTYPE(const uint256, CBlockIndex*)& item, mapBlockIndex)
  459. setTips.insert(item.second);
  460. BOOST_FOREACH(const PAIRTYPE(const uint256, CBlockIndex*)& item, mapBlockIndex)
  461. {
  462. const CBlockIndex* pprev = item.second->pprev;
  463. if (pprev)
  464. setTips.erase(pprev);
  465. }
  466. /* Construct the output array. */
  467. Array res;
  468. BOOST_FOREACH(const CBlockIndex* block, setTips)
  469. {
  470. Object obj;
  471. obj.push_back(Pair("height", block->nHeight));
  472. obj.push_back(Pair("hash", block->phashBlock->GetHex()));
  473. const int branchLen = block->nHeight - chainActive.FindFork(block)->nHeight;
  474. obj.push_back(Pair("branchlen", branchLen));
  475. res.push_back(obj);
  476. }
  477. return res;
  478. }
  479. Value getmempoolinfo(const Array& params, bool fHelp)
  480. {
  481. if (fHelp || params.size() != 0)
  482. throw runtime_error(
  483. "getmempoolinfo\n"
  484. "\nReturns details on the active state of the TX memory pool.\n"
  485. "\nResult:\n"
  486. "{\n"
  487. " \"size\": xxxxx (numeric) Current tx count\n"
  488. " \"bytes\": xxxxx (numeric) Sum of all tx sizes\n"
  489. "}\n"
  490. "\nExamples:\n"
  491. + HelpExampleCli("getmempoolinfo", "")
  492. + HelpExampleRpc("getmempoolinfo", "")
  493. );
  494. Object ret;
  495. ret.push_back(Pair("size", (int64_t) mempool.size()));
  496. ret.push_back(Pair("bytes", (int64_t) mempool.GetTotalTxSize()));
  497. return ret;
  498. }