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