]> Git Repo - VerusCoin.git/blame - src/rpcblockchain.cpp
Merge pull request #4318 from laanwj/2014_06_script_unused_debugging
[VerusCoin.git] / src / rpcblockchain.cpp
CommitLineData
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
fb78cc23 6#include "rpcserver.h"
51ed9ec9
BD
7#include "main.h"
8#include "sync.h"
d387b8ec 9#include "checkpoints.h"
51ed9ec9
BD
10
11#include <stdint.h>
12
13#include "json/json_spirit_value.h"
c625ae04
JG
14
15using namespace json_spirit;
16using namespace std;
17
be066fad 18void ScriptPubKeyToJSON(const CScript& scriptPubKey, Object& out, bool fIncludeHex);
4e68391a 19
c625ae04
JG
20double 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
52Object 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));
c625ae04
JG
69 result.push_back(Pair("bits", HexBits(block.nBits)));
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
82Value 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
98Value 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
114Value 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
131Value 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
208Value 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
231Value 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];
7db120d5 279 ReadBlockFromDisk(block, pblockindex);
c625ae04 280
23319521
LD
281 if (!fVerbose)
282 {
283 CDataStream ssBlock(SER_NETWORK, PROTOCOL_VERSION);
284 ssBlock << block;
285 std::string strHex = HexStr(ssBlock.begin(), ssBlock.end());
286 return strHex;
287 }
288
c625ae04
JG
289 return blockToJSON(block, pblockindex);
290}
291
beeb5761
PW
292Value gettxoutsetinfo(const Array& params, bool fHelp)
293{
294 if (fHelp || params.size() != 0)
295 throw runtime_error(
296 "gettxoutsetinfo\n"
a6099ef3 297 "\nReturns statistics about the unspent transaction output set.\n"
298 "Note this call may take some time.\n"
299 "\nResult:\n"
300 "{\n"
301 " \"height\":n, (numeric) The current block height (index)\n"
302 " \"bestblock\": \"hex\", (string) the best block hash hex\n"
303 " \"transactions\": n, (numeric) The number of transactions\n"
304 " \"txouts\": n, (numeric) The number of output transactions\n"
305 " \"bytes_serialized\": n, (numeric) The serialized size\n"
306 " \"hash_serialized\": \"hash\", (string) The serialized hash\n"
307 " \"total_amount\": x.xxx (numeric) The total amount\n"
308 "}\n"
309 "\nExamples:\n"
310 + HelpExampleCli("gettxoutsetinfo", "")
311 + HelpExampleRpc("gettxoutsetinfo", "")
312 );
beeb5761
PW
313
314 Object ret;
315
316 CCoinsStats stats;
317 if (pcoinsTip->GetStats(stats)) {
4b61a6a4 318 ret.push_back(Pair("height", (int64_t)stats.nHeight));
e31aa7c9 319 ret.push_back(Pair("bestblock", stats.hashBlock.GetHex()));
4b61a6a4
KD
320 ret.push_back(Pair("transactions", (int64_t)stats.nTransactions));
321 ret.push_back(Pair("txouts", (int64_t)stats.nTransactionOutputs));
322 ret.push_back(Pair("bytes_serialized", (int64_t)stats.nSerializedSize));
e31aa7c9
PW
323 ret.push_back(Pair("hash_serialized", stats.hashSerialized.GetHex()));
324 ret.push_back(Pair("total_amount", ValueFromAmount(stats.nTotalAmount)));
beeb5761
PW
325 }
326 return ret;
327}
c625ae04 328
beeb5761
PW
329Value gettxout(const Array& params, bool fHelp)
330{
331 if (fHelp || params.size() < 2 || params.size() > 3)
332 throw runtime_error(
a6099ef3 333 "gettxout \"txid\" n ( includemempool )\n"
334 "\nReturns details about an unspent transaction output.\n"
335 "\nArguments:\n"
336 "1. \"txid\" (string, required) The transaction id\n"
337 "2. n (numeric, required) vout value\n"
338 "3. includemempool (boolean, optional) Whether to included the mem pool\n"
339 "\nResult:\n"
340 "{\n"
341 " \"bestblock\" : \"hash\", (string) the block hash\n"
342 " \"confirmations\" : n, (numeric) The number of confirmations\n"
343 " \"value\" : x.xxx, (numeric) The transaction value in btc\n"
344 " \"scriptPubKey\" : { (json object)\n"
345 " \"asm\" : \"code\", (string) \n"
346 " \"hex\" : \"hex\", (string) \n"
347 " \"reqSigs\" : n, (numeric) Number of required signatures\n"
348 " \"type\" : \"pubkeyhash\", (string) The type, eg pubkeyhash\n"
349 " \"addresses\" : [ (array of string) array of bitcoin addresses\n"
350 " \"bitcoinaddress\" (string) bitcoin address\n"
351 " ,...\n"
352 " ]\n"
353 " },\n"
354 " \"version\" : n, (numeric) The version\n"
355 " \"coinbase\" : true|false (boolean) Coinbase or not\n"
356 "}\n"
357
358 "\nExamples:\n"
359 "\nGet unspent transactions\n"
360 + HelpExampleCli("listunspent", "") +
361 "\nView the details\n"
362 + HelpExampleCli("gettxout", "\"txid\" 1") +
363 "\nAs a json rpc call\n"
364 + HelpExampleRpc("gettxout", "\"txid\", 1")
365 );
c625ae04 366
beeb5761
PW
367 Object ret;
368
369 std::string strHash = params[0].get_str();
370 uint256 hash(strHash);
371 int n = params[1].get_int();
372 bool fMempool = true;
373 if (params.size() > 2)
374 fMempool = params[2].get_bool();
375
376 CCoins coins;
377 if (fMempool) {
378 LOCK(mempool.cs);
379 CCoinsViewMemPool view(*pcoinsTip, mempool);
380 if (!view.GetCoins(hash, coins))
381 return Value::null;
382 mempool.pruneSpent(hash, coins); // TODO: this should be done by the CCoinsViewMemPool
383 } else {
384 if (!pcoinsTip->GetCoins(hash, coins))
385 return Value::null;
386 }
387 if (n<0 || (unsigned int)n>=coins.vout.size() || coins.vout[n].IsNull())
388 return Value::null;
389
84674082
PW
390 std::map<uint256, CBlockIndex*>::iterator it = mapBlockIndex.find(pcoinsTip->GetBestBlock());
391 CBlockIndex *pindex = it->second;
392 ret.push_back(Pair("bestblock", pindex->GetBlockHash().GetHex()));
beeb5761
PW
393 if ((unsigned int)coins.nHeight == MEMPOOL_HEIGHT)
394 ret.push_back(Pair("confirmations", 0));
395 else
84674082 396 ret.push_back(Pair("confirmations", pindex->nHeight - coins.nHeight + 1));
4e68391a 397 ret.push_back(Pair("value", ValueFromAmount(coins.vout[n].nValue)));
beeb5761 398 Object o;
be066fad 399 ScriptPubKeyToJSON(coins.vout[n].scriptPubKey, o, true);
beeb5761
PW
400 ret.push_back(Pair("scriptPubKey", o));
401 ret.push_back(Pair("version", coins.nVersion));
402 ret.push_back(Pair("coinbase", coins.fCoinBase));
403
404 return ret;
405}
c625ae04 406
f5906533
JG
407Value verifychain(const Array& params, bool fHelp)
408{
409 if (fHelp || params.size() > 2)
410 throw runtime_error(
a6099ef3 411 "verifychain ( checklevel numblocks )\n"
412 "\nVerifies blockchain database.\n"
413 "\nArguments:\n"
6943cb9b
PK
414 "1. checklevel (numeric, optional, 0-4, default=3) How thorough the block verification is.\n"
415 "2. numblocks (numeric, optional, default=288, 0=all) The number of blocks to check.\n"
a6099ef3 416 "\nResult:\n"
417 "true|false (boolean) Verified or not\n"
418 "\nExamples:\n"
419 + HelpExampleCli("verifychain", "")
420 + HelpExampleRpc("verifychain", "")
421 );
f5906533
JG
422
423 int nCheckLevel = GetArg("-checklevel", 3);
424 int nCheckDepth = GetArg("-checkblocks", 288);
425 if (params.size() > 0)
426 nCheckLevel = params[0].get_int();
427 if (params.size() > 1)
428 nCheckDepth = params[1].get_int();
429
06a91d96 430 return CVerifyDB().VerifyDB(nCheckLevel, nCheckDepth);
f5906533 431}
c625ae04 432
d387b8ec
WL
433Value getblockchaininfo(const Array& params, bool fHelp)
434{
435 if (fHelp || params.size() != 0)
436 throw runtime_error(
437 "getblockchaininfo\n"
438 "Returns an object containing various state info regarding block chain processing.\n"
439 "\nResult:\n"
440 "{\n"
441 " \"chain\": \"xxxx\", (string) current chain (main, testnet3, regtest)\n"
442 " \"blocks\": xxxxxx, (numeric) the current number of blocks processed in the server\n"
443 " \"bestblockhash\": \"...\", (string) the hash of the currently best block\n"
444 " \"difficulty\": xxxxxx, (numeric) the current difficulty\n"
445 " \"verificationprogress\": xxxx, (numeric) estimate of verification progress [0..1]\n"
446 " \"chainwork\": \"xxxx\" (string) total amount of work in active chain, in hexadecimal\n"
447 "}\n"
448 "\nExamples:\n"
449 + HelpExampleCli("getblockchaininfo", "")
450 + HelpExampleRpc("getblockchaininfo", "")
451 );
452
453 proxyType proxy;
454 GetProxy(NET_IPV4, proxy);
455
456 Object obj;
457 std::string chain = Params().DataDir();
458 if(chain.empty())
459 chain = "main";
460 obj.push_back(Pair("chain", chain));
461 obj.push_back(Pair("blocks", (int)chainActive.Height()));
462 obj.push_back(Pair("bestblockhash", chainActive.Tip()->GetBlockHash().GetHex()));
463 obj.push_back(Pair("difficulty", (double)GetDifficulty()));
464 obj.push_back(Pair("verificationprogress", Checkpoints::GuessVerificationProgress(chainActive.Tip())));
465 obj.push_back(Pair("chainwork", chainActive.Tip()->nChainWork.GetHex()));
466 return obj;
467}
This page took 0.14711 seconds and 4 git commands to generate.