]>
Commit | Line | Data |
---|---|---|
c625ae04 | 1 | // Copyright (c) 2010 Satoshi Nakamoto |
f914f1a7 | 2 | // Copyright (c) 2009-2014 The Bitcoin Core developers |
72fb3d29 | 3 | // Distributed under the MIT software license, see the accompanying |
c625ae04 JG |
4 | // file COPYING or http://www.opensource.org/licenses/mit-license.php. |
5 | ||
ac14bcc1 | 6 | #include "checkpoints.h" |
da29ecbc | 7 | #include "consensus/validation.h" |
51ed9ec9 | 8 | #include "main.h" |
da29ecbc | 9 | #include "primitives/transaction.h" |
ac14bcc1 | 10 | #include "rpcserver.h" |
51ed9ec9 | 11 | #include "sync.h" |
ad49c256 | 12 | #include "util.h" |
51ed9ec9 BD |
13 | |
14 | #include <stdint.h> | |
15 | ||
16 | #include "json/json_spirit_value.h" | |
c625ae04 JG |
17 | |
18 | using namespace json_spirit; | |
19 | using namespace std; | |
20 | ||
73351c36 | 21 | extern void TxToJSON(const CTransaction& tx, const uint256 hashBlock, Object& entry); |
be066fad | 22 | void ScriptPubKeyToJSON(const CScript& scriptPubKey, Object& out, bool fIncludeHex); |
4e68391a | 23 | |
695a7a88 | 24 | double GetDifficultyINTERNAL(const CBlockIndex* blockindex, bool networkDifficulty) |
c625ae04 JG |
25 | { |
26 | // Floating point number that is a multiple of the minimum difficulty, | |
27 | // minimum difficulty = 1.0. | |
28 | if (blockindex == NULL) | |
29 | { | |
4c6d41b8 | 30 | if (chainActive.Tip() == NULL) |
c625ae04 JG |
31 | return 1.0; |
32 | else | |
4c6d41b8 | 33 | blockindex = chainActive.Tip(); |
c625ae04 JG |
34 | } |
35 | ||
333ea3c4 JG |
36 | uint32_t bits; |
37 | if (networkDifficulty) { | |
38 | bits = GetNextWorkRequired(blockindex, nullptr, Params().GetConsensus()); | |
39 | } else { | |
40 | bits = blockindex->nBits; | |
695a7a88 JG |
41 | } |
42 | ||
333ea3c4 | 43 | uint32_t powLimit = |
f50e8313 | 44 | UintToArith256(Params().GetConsensus().powLimit).GetCompact(); |
333ea3c4 | 45 | int nShift = (bits >> 24) & 0xff; |
24809b16 | 46 | int nShiftAmount = (powLimit >> 24) & 0xff; |
c625ae04 JG |
47 | |
48 | double dDiff = | |
24809b16 | 49 | (double)(powLimit & 0x00ffffff) / |
333ea3c4 | 50 | (double)(bits & 0x00ffffff); |
c625ae04 | 51 | |
24809b16 | 52 | while (nShift < nShiftAmount) |
c625ae04 JG |
53 | { |
54 | dDiff *= 256.0; | |
55 | nShift++; | |
56 | } | |
24809b16 | 57 | while (nShift > nShiftAmount) |
c625ae04 JG |
58 | { |
59 | dDiff /= 256.0; | |
60 | nShift--; | |
61 | } | |
62 | ||
63 | return dDiff; | |
64 | } | |
65 | ||
695a7a88 JG |
66 | double GetDifficulty(const CBlockIndex* blockindex) |
67 | { | |
68 | return GetDifficultyINTERNAL(blockindex, false); | |
69 | } | |
70 | ||
71 | double GetNetworkDifficulty(const CBlockIndex* blockindex) | |
72 | { | |
73 | return GetDifficultyINTERNAL(blockindex, true); | |
74 | } | |
75 | ||
c625ae04 | 76 | |
73351c36 | 77 | Object blockToJSON(const CBlock& block, const CBlockIndex* blockindex, bool txDetails = false) |
c625ae04 JG |
78 | { |
79 | Object result; | |
80 | result.push_back(Pair("hash", block.GetHash().GetHex())); | |
57153d4e WL |
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)); | |
c625ae04 JG |
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())); | |
90 | Array txs; | |
91 | BOOST_FOREACH(const CTransaction&tx, block.vtx) | |
73351c36 JS |
92 | { |
93 | if(txDetails) | |
94 | { | |
95 | Object objTx; | |
4f152496 | 96 | TxToJSON(tx, uint256(), objTx); |
73351c36 JS |
97 | txs.push_back(objTx); |
98 | } | |
99 | else | |
805344dc | 100 | txs.push_back(tx.GetHash().GetHex()); |
73351c36 | 101 | } |
c625ae04 | 102 | result.push_back(Pair("tx", txs)); |
d56e30ca | 103 | result.push_back(Pair("time", block.GetBlockTime())); |
fdda3c50 | 104 | result.push_back(Pair("nonce", block.nNonce.GetHex())); |
e1dde421 | 105 | result.push_back(Pair("solution", HexStr(block.nSolution))); |
645d497a | 106 | result.push_back(Pair("bits", strprintf("%08x", block.nBits))); |
c625ae04 | 107 | result.push_back(Pair("difficulty", GetDifficulty(blockindex))); |
1b3656d5 | 108 | result.push_back(Pair("chainwork", blockindex->nChainWork.GetHex())); |
c625ae04 JG |
109 | |
110 | if (blockindex->pprev) | |
111 | result.push_back(Pair("previousblockhash", blockindex->pprev->GetBlockHash().GetHex())); | |
4c6d41b8 | 112 | CBlockIndex *pnext = chainActive.Next(blockindex); |
0fe8010a PW |
113 | if (pnext) |
114 | result.push_back(Pair("nextblockhash", pnext->GetBlockHash().GetHex())); | |
c625ae04 JG |
115 | return result; |
116 | } | |
117 | ||
118 | ||
119 | Value getblockcount(const Array& params, bool fHelp) | |
120 | { | |
121 | if (fHelp || params.size() != 0) | |
122 | throw runtime_error( | |
123 | "getblockcount\n" | |
a6099ef3 | 124 | "\nReturns the number of blocks in the longest block chain.\n" |
125 | "\nResult:\n" | |
126 | "n (numeric) The current block count\n" | |
127 | "\nExamples:\n" | |
128 | + HelpExampleCli("getblockcount", "") | |
129 | + HelpExampleRpc("getblockcount", "") | |
130 | ); | |
c625ae04 | 131 | |
4401b2d7 | 132 | LOCK(cs_main); |
4c6d41b8 | 133 | return chainActive.Height(); |
c625ae04 JG |
134 | } |
135 | ||
091aa8da JG |
136 | Value getbestblockhash(const Array& params, bool fHelp) |
137 | { | |
138 | if (fHelp || params.size() != 0) | |
139 | throw runtime_error( | |
140 | "getbestblockhash\n" | |
a6099ef3 | 141 | "\nReturns the hash of the best (tip) block in the longest block chain.\n" |
142 | "\nResult\n" | |
143 | "\"hex\" (string) the block hash hex encoded\n" | |
144 | "\nExamples\n" | |
145 | + HelpExampleCli("getbestblockhash", "") | |
146 | + HelpExampleRpc("getbestblockhash", "") | |
147 | ); | |
091aa8da | 148 | |
4401b2d7 | 149 | LOCK(cs_main); |
4c6d41b8 | 150 | return chainActive.Tip()->GetBlockHash().GetHex(); |
091aa8da | 151 | } |
c625ae04 JG |
152 | |
153 | Value getdifficulty(const Array& params, bool fHelp) | |
154 | { | |
155 | if (fHelp || params.size() != 0) | |
156 | throw runtime_error( | |
157 | "getdifficulty\n" | |
a6099ef3 | 158 | "\nReturns the proof-of-work difficulty as a multiple of the minimum difficulty.\n" |
159 | "\nResult:\n" | |
160 | "n.nnn (numeric) the proof-of-work difficulty as a multiple of the minimum difficulty.\n" | |
161 | "\nExamples:\n" | |
162 | + HelpExampleCli("getdifficulty", "") | |
163 | + HelpExampleRpc("getdifficulty", "") | |
164 | ); | |
c625ae04 | 165 | |
4401b2d7 | 166 | LOCK(cs_main); |
695a7a88 | 167 | return GetNetworkDifficulty(); |
c625ae04 JG |
168 | } |
169 | ||
170 | ||
c625ae04 JG |
171 | Value getrawmempool(const Array& params, bool fHelp) |
172 | { | |
4d707d51 | 173 | if (fHelp || params.size() > 1) |
c625ae04 | 174 | throw runtime_error( |
4d707d51 | 175 | "getrawmempool ( verbose )\n" |
a6099ef3 | 176 | "\nReturns all transaction ids in memory pool as a json array of string transaction ids.\n" |
4d707d51 GA |
177 | "\nArguments:\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" | |
a6099ef3 | 181 | " \"transactionid\" (string) The transaction id\n" |
182 | " ,...\n" | |
183 | "]\n" | |
4d707d51 GA |
184 | "\nResult: (for verbose = true):\n" |
185 | "{ (json object)\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" | |
195 | " ... ]\n" | |
196 | " }, ...\n" | |
803f51ef | 197 | "}\n" |
a6099ef3 | 198 | "\nExamples\n" |
4d707d51 GA |
199 | + HelpExampleCli("getrawmempool", "true") |
200 | + HelpExampleRpc("getrawmempool", "true") | |
a6099ef3 | 201 | ); |
c625ae04 | 202 | |
4401b2d7 EL |
203 | LOCK(cs_main); |
204 | ||
4d707d51 GA |
205 | bool fVerbose = false; |
206 | if (params.size() > 0) | |
207 | fVerbose = params[0].get_bool(); | |
c625ae04 | 208 | |
4d707d51 GA |
209 | if (fVerbose) |
210 | { | |
211 | LOCK(mempool.cs); | |
212 | Object o; | |
213 | BOOST_FOREACH(const PAIRTYPE(uint256, CTxMemPoolEntry)& entry, mempool.mapTx) | |
214 | { | |
215 | const uint256& hash = entry.first; | |
216 | const CTxMemPoolEntry& e = entry.second; | |
217 | Object info; | |
218 | info.push_back(Pair("size", (int)e.GetTxSize())); | |
219 | info.push_back(Pair("fee", ValueFromAmount(e.GetFee()))); | |
d56e30ca | 220 | info.push_back(Pair("time", e.GetTime())); |
4d707d51 GA |
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) | |
227 | { | |
228 | if (mempool.exists(txin.prevout.hash)) | |
229 | setDepends.insert(txin.prevout.hash.ToString()); | |
230 | } | |
231 | Array depends(setDepends.begin(), setDepends.end()); | |
232 | info.push_back(Pair("depends", depends)); | |
233 | o.push_back(Pair(hash.ToString(), info)); | |
234 | } | |
235 | return o; | |
236 | } | |
237 | else | |
238 | { | |
239 | vector<uint256> vtxid; | |
240 | mempool.queryHashes(vtxid); | |
c625ae04 | 241 | |
4d707d51 GA |
242 | Array a; |
243 | BOOST_FOREACH(const uint256& hash, vtxid) | |
244 | a.push_back(hash.ToString()); | |
245 | ||
246 | return a; | |
247 | } | |
c625ae04 JG |
248 | } |
249 | ||
250 | Value getblockhash(const Array& params, bool fHelp) | |
251 | { | |
252 | if (fHelp || params.size() != 1) | |
253 | throw runtime_error( | |
a6099ef3 | 254 | "getblockhash index\n" |
255 | "\nReturns hash of block in best-block-chain at index provided.\n" | |
256 | "\nArguments:\n" | |
257 | "1. index (numeric, required) The block index\n" | |
258 | "\nResult:\n" | |
259 | "\"hash\" (string) The block hash\n" | |
260 | "\nExamples:\n" | |
261 | + HelpExampleCli("getblockhash", "1000") | |
262 | + HelpExampleRpc("getblockhash", "1000") | |
263 | ); | |
c625ae04 | 264 | |
4401b2d7 EL |
265 | LOCK(cs_main); |
266 | ||
c625ae04 | 267 | int nHeight = params[0].get_int(); |
4c6d41b8 | 268 | if (nHeight < 0 || nHeight > chainActive.Height()) |
6261e6e6 | 269 | throw JSONRPCError(RPC_INVALID_PARAMETER, "Block height out of range"); |
c625ae04 | 270 | |
4c6d41b8 PW |
271 | CBlockIndex* pblockindex = chainActive[nHeight]; |
272 | return pblockindex->GetBlockHash().GetHex(); | |
c625ae04 JG |
273 | } |
274 | ||
275 | Value getblock(const Array& params, bool fHelp) | |
276 | { | |
23319521 | 277 | if (fHelp || params.size() < 1 || params.size() > 2) |
c625ae04 | 278 | throw runtime_error( |
a6099ef3 | 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" | |
282 | "\nArguments:\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" | |
286 | "{\n" | |
287 | " \"hash\" : \"hash\", (string) the block hash (same as provided)\n" | |
57153d4e | 288 | " \"confirmations\" : n, (numeric) The number of confirmations, or -1 if the block is not on the main chain\n" |
a6099ef3 | 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" | |
295 | " ,...\n" | |
296 | " ],\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" | |
303 | "}\n" | |
304 | "\nResult (for verbose=false):\n" | |
305 | "\"data\" (string) A string that is serialized, hex-encoded data for block 'hash'.\n" | |
306 | "\nExamples:\n" | |
307 | + HelpExampleCli("getblock", "\"00000000c937983704a73af28acdec37b049d214adbda81d7e2a3dd146f6ed09\"") | |
308 | + HelpExampleRpc("getblock", "\"00000000c937983704a73af28acdec37b049d214adbda81d7e2a3dd146f6ed09\"") | |
23319521 | 309 | ); |
c625ae04 | 310 | |
4401b2d7 EL |
311 | LOCK(cs_main); |
312 | ||
c625ae04 | 313 | std::string strHash = params[0].get_str(); |
34cdc411 | 314 | uint256 hash(uint256S(strHash)); |
c625ae04 | 315 | |
23319521 LD |
316 | bool fVerbose = true; |
317 | if (params.size() > 1) | |
318 | fVerbose = params[1].get_bool(); | |
319 | ||
c625ae04 | 320 | if (mapBlockIndex.count(hash) == 0) |
738835d7 | 321 | throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block not found"); |
c625ae04 JG |
322 | |
323 | CBlock block; | |
324 | CBlockIndex* pblockindex = mapBlockIndex[hash]; | |
954d2e72 | 325 | |
03c56872 JS |
326 | if (fHavePruned && !(pblockindex->nStatus & BLOCK_HAVE_DATA) && pblockindex->nTx > 0) |
327 | throw JSONRPCError(RPC_INTERNAL_ERROR, "Block not available (pruned data)"); | |
328 | ||
f2dd868d | 329 | if(!ReadBlockFromDisk(block, pblockindex)) |
954d2e72 | 330 | throw JSONRPCError(RPC_INTERNAL_ERROR, "Can't read block from disk"); |
c625ae04 | 331 | |
23319521 LD |
332 | if (!fVerbose) |
333 | { | |
334 | CDataStream ssBlock(SER_NETWORK, PROTOCOL_VERSION); | |
335 | ssBlock << block; | |
336 | std::string strHex = HexStr(ssBlock.begin(), ssBlock.end()); | |
337 | return strHex; | |
338 | } | |
339 | ||
c625ae04 JG |
340 | return blockToJSON(block, pblockindex); |
341 | } | |
342 | ||
beeb5761 PW |
343 | Value gettxoutsetinfo(const Array& params, bool fHelp) |
344 | { | |
345 | if (fHelp || params.size() != 0) | |
346 | throw runtime_error( | |
347 | "gettxoutsetinfo\n" | |
a6099ef3 | 348 | "\nReturns statistics about the unspent transaction output set.\n" |
349 | "Note this call may take some time.\n" | |
350 | "\nResult:\n" | |
351 | "{\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" | |
359 | "}\n" | |
360 | "\nExamples:\n" | |
361 | + HelpExampleCli("gettxoutsetinfo", "") | |
362 | + HelpExampleRpc("gettxoutsetinfo", "") | |
363 | ); | |
beeb5761 | 364 | |
4401b2d7 EL |
365 | LOCK(cs_main); |
366 | ||
beeb5761 PW |
367 | Object ret; |
368 | ||
369 | CCoinsStats stats; | |
51ce901a | 370 | FlushStateToDisk(); |
beeb5761 | 371 | if (pcoinsTip->GetStats(stats)) { |
4b61a6a4 | 372 | ret.push_back(Pair("height", (int64_t)stats.nHeight)); |
e31aa7c9 | 373 | ret.push_back(Pair("bestblock", stats.hashBlock.GetHex())); |
4b61a6a4 KD |
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)); | |
e31aa7c9 PW |
377 | ret.push_back(Pair("hash_serialized", stats.hashSerialized.GetHex())); |
378 | ret.push_back(Pair("total_amount", ValueFromAmount(stats.nTotalAmount))); | |
beeb5761 PW |
379 | } |
380 | return ret; | |
381 | } | |
c625ae04 | 382 | |
17878015 | 383 | uint64_t komodo_interest(int32_t txheight,uint64_t nValue,uint32_t nLockTime,uint32_t tiptime); |
7a77c443 | 384 | uint32_t komodo_txtime(uint256 hash); |
1f346363 | 385 | uint64_t komodo_paxprice(int32_t height,char *base,char *rel,uint64_t basevolume); |
a4ebaad7 | 386 | int32_t komodo_paxprices(uint32_t *timestamps,uint64_t *prices,int32_t max,int32_t width,char *base,char *rel); |
a9869d0d | 387 | |
388 | Value paxprice(const Array& params, bool fHelp) | |
389 | { | |
1f346363 | 390 | if ( fHelp || params.size() < 3 || params.size() > 4 ) |
a9869d0d | 391 | throw runtime_error("paxprice \"base\" \"rel\" height\n"); |
392 | LOCK(cs_main); | |
a4ebaad7 | 393 | Object ret; uint64_t basevolume=0,relvolume; |
a9869d0d | 394 | std::string base = params[0].get_str(); |
395 | std::string rel = params[1].get_str(); | |
96eb75ee | 396 | int32_t height = atoi(params[2].get_str().c_str()); |
303e9e00 | 397 | if ( basevolume == 0 ) |
1f346363 | 398 | basevolume = COIN; |
1f346363 | 399 | relvolume = komodo_paxprice(height,(char *)base.c_str(),(char *)rel.c_str(),basevolume); |
a9869d0d | 400 | ret.push_back(Pair("base", base)); |
401 | ret.push_back(Pair("rel", rel)); | |
402 | ret.push_back(Pair("height", height)); | |
a4ebaad7 | 403 | if ( basevolume != 0 && relvolume != 0 ) |
1f346363 | 404 | { |
3000de34 | 405 | ret.push_back(Pair("price",((double)relvolume / (double)basevolume))); |
a4ebaad7 | 406 | ret.push_back(Pair("invprice",((double)basevolume / (double)relvolume))); |
407 | ret.push_back(Pair("basevolume", ValueFromAmount(basevolume))); | |
1f346363 | 408 | ret.push_back(Pair("relvolume", ValueFromAmount(relvolume))); |
a4ebaad7 | 409 | } |
410 | return ret; | |
411 | } | |
412 | ||
413 | Value paxprices(const Array& params, bool fHelp) | |
414 | { | |
415 | if ( fHelp || params.size() != 3 ) | |
416 | throw runtime_error("paxprices \"base\" \"rel\" width\n"); | |
417 | LOCK(cs_main); | |
418 | Object ret; uint64_t relvolume,prices[1024]; uint32_t i,n,timestamps[1024]; | |
419 | std::string base = params[0].get_str(); | |
420 | std::string rel = params[1].get_str(); | |
421 | int32_t width = atoi(params[2].get_str().c_str()); | |
6710be90 | 422 | if ( width < 60 ) |
423 | width = 60; | |
a4ebaad7 | 424 | ret.push_back(Pair("base", base)); |
425 | ret.push_back(Pair("rel", rel)); | |
96eb75ee | 426 | n = komodo_paxprices(timestamps,prices,(int32_t)(sizeof(prices)/sizeof(*prices)),width,(char *)base.c_str(),(char *)rel.c_str()); |
427 | Array a; | |
428 | for (i=0; i<n; i++) | |
a4ebaad7 | 429 | { |
96eb75ee | 430 | Object item; |
431 | item.push_back(Pair("t", (int64_t)timestamps[i])); | |
432 | item.push_back(Pair("p", (double)prices[i] / COIN)); | |
433 | a.push_back(item); | |
1f346363 | 434 | } |
96eb75ee | 435 | ret.push_back(Pair("array", a)); |
a9869d0d | 436 | return ret; |
437 | } | |
7a77c443 | 438 | |
beeb5761 PW |
439 | Value gettxout(const Array& params, bool fHelp) |
440 | { | |
441 | if (fHelp || params.size() < 2 || params.size() > 3) | |
442 | throw runtime_error( | |
a6099ef3 | 443 | "gettxout \"txid\" n ( includemempool )\n" |
444 | "\nReturns details about an unspent transaction output.\n" | |
445 | "\nArguments:\n" | |
446 | "1. \"txid\" (string, required) The transaction id\n" | |
447 | "2. n (numeric, required) vout value\n" | |
448 | "3. includemempool (boolean, optional) Whether to included the mem pool\n" | |
449 | "\nResult:\n" | |
450 | "{\n" | |
451 | " \"bestblock\" : \"hash\", (string) the block hash\n" | |
452 | " \"confirmations\" : n, (numeric) The number of confirmations\n" | |
453 | " \"value\" : x.xxx, (numeric) The transaction value in btc\n" | |
454 | " \"scriptPubKey\" : { (json object)\n" | |
455 | " \"asm\" : \"code\", (string) \n" | |
456 | " \"hex\" : \"hex\", (string) \n" | |
457 | " \"reqSigs\" : n, (numeric) Number of required signatures\n" | |
458 | " \"type\" : \"pubkeyhash\", (string) The type, eg pubkeyhash\n" | |
459 | " \"addresses\" : [ (array of string) array of bitcoin addresses\n" | |
460 | " \"bitcoinaddress\" (string) bitcoin address\n" | |
461 | " ,...\n" | |
462 | " ]\n" | |
463 | " },\n" | |
464 | " \"version\" : n, (numeric) The version\n" | |
465 | " \"coinbase\" : true|false (boolean) Coinbase or not\n" | |
466 | "}\n" | |
467 | ||
468 | "\nExamples:\n" | |
469 | "\nGet unspent transactions\n" | |
470 | + HelpExampleCli("listunspent", "") + | |
471 | "\nView the details\n" | |
472 | + HelpExampleCli("gettxout", "\"txid\" 1") + | |
473 | "\nAs a json rpc call\n" | |
474 | + HelpExampleRpc("gettxout", "\"txid\", 1") | |
475 | ); | |
c625ae04 | 476 | |
4401b2d7 EL |
477 | LOCK(cs_main); |
478 | ||
beeb5761 PW |
479 | Object ret; |
480 | ||
481 | std::string strHash = params[0].get_str(); | |
34cdc411 | 482 | uint256 hash(uint256S(strHash)); |
beeb5761 PW |
483 | int n = params[1].get_int(); |
484 | bool fMempool = true; | |
485 | if (params.size() > 2) | |
486 | fMempool = params[2].get_bool(); | |
487 | ||
488 | CCoins coins; | |
489 | if (fMempool) { | |
490 | LOCK(mempool.cs); | |
7c70438d | 491 | CCoinsViewMemPool view(pcoinsTip, mempool); |
beeb5761 PW |
492 | if (!view.GetCoins(hash, coins)) |
493 | return Value::null; | |
494 | mempool.pruneSpent(hash, coins); // TODO: this should be done by the CCoinsViewMemPool | |
495 | } else { | |
496 | if (!pcoinsTip->GetCoins(hash, coins)) | |
497 | return Value::null; | |
498 | } | |
499 | if (n<0 || (unsigned int)n>=coins.vout.size() || coins.vout[n].IsNull()) | |
500 | return Value::null; | |
501 | ||
145d5be8 | 502 | BlockMap::iterator it = mapBlockIndex.find(pcoinsTip->GetBestBlock()); |
7a77c443 | 503 | CBlockIndex *pindex = it->second; |
84674082 | 504 | ret.push_back(Pair("bestblock", pindex->GetBlockHash().GetHex())); |
beeb5761 PW |
505 | if ((unsigned int)coins.nHeight == MEMPOOL_HEIGHT) |
506 | ret.push_back(Pair("confirmations", 0)); | |
cad0d1ca | 507 | else ret.push_back(Pair("confirmations", pindex->nHeight - coins.nHeight + 1)); |
4e68391a | 508 | ret.push_back(Pair("value", ValueFromAmount(coins.vout[n].nValue))); |
7a77c443 | 509 | |
2860f747 | 510 | CBlockIndex *pblockindex = chainActive[coins.nHeight]; |
2860f747 | 511 | uint64_t interest; uint32_t timestamp=0; |
512 | if ( pblockindex != 0 ) | |
17878015 | 513 | timestamp = pblockindex->nTime; // this is approx, but cant figure out how to get tx here |
514 | interest = komodo_interest(coins.nHeight,coins.vout[n].nValue,timestamp,pindex->nTime); | |
515 | //fprintf(stderr,"nValue %llu lock.%u:%u nTime.%u -> %llu\n",(long long)coins.vout[n].nValue,coins.nLockTime,timestamp,pindex->nTime,(long long)interest); | |
7a77c443 | 516 | ret.push_back(Pair("interest", ValueFromAmount(interest))); |
517 | ||
beeb5761 | 518 | Object o; |
be066fad | 519 | ScriptPubKeyToJSON(coins.vout[n].scriptPubKey, o, true); |
beeb5761 PW |
520 | ret.push_back(Pair("scriptPubKey", o)); |
521 | ret.push_back(Pair("version", coins.nVersion)); | |
522 | ret.push_back(Pair("coinbase", coins.fCoinBase)); | |
523 | ||
524 | return ret; | |
525 | } | |
c625ae04 | 526 | |
f5906533 JG |
527 | Value verifychain(const Array& params, bool fHelp) |
528 | { | |
529 | if (fHelp || params.size() > 2) | |
530 | throw runtime_error( | |
a6099ef3 | 531 | "verifychain ( checklevel numblocks )\n" |
532 | "\nVerifies blockchain database.\n" | |
533 | "\nArguments:\n" | |
6943cb9b PK |
534 | "1. checklevel (numeric, optional, 0-4, default=3) How thorough the block verification is.\n" |
535 | "2. numblocks (numeric, optional, default=288, 0=all) The number of blocks to check.\n" | |
a6099ef3 | 536 | "\nResult:\n" |
537 | "true|false (boolean) Verified or not\n" | |
538 | "\nExamples:\n" | |
539 | + HelpExampleCli("verifychain", "") | |
540 | + HelpExampleRpc("verifychain", "") | |
541 | ); | |
f5906533 | 542 | |
4401b2d7 EL |
543 | LOCK(cs_main); |
544 | ||
f5906533 JG |
545 | int nCheckLevel = GetArg("-checklevel", 3); |
546 | int nCheckDepth = GetArg("-checkblocks", 288); | |
547 | if (params.size() > 0) | |
548 | nCheckLevel = params[0].get_int(); | |
549 | if (params.size() > 1) | |
550 | nCheckDepth = params[1].get_int(); | |
551 | ||
2e280311 | 552 | return CVerifyDB().VerifyDB(pcoinsTip, nCheckLevel, nCheckDepth); |
f5906533 | 553 | } |
c625ae04 | 554 | |
ba1da90b WL |
555 | /** Implementation of IsSuperMajority with better feedback */ |
556 | Object SoftForkMajorityDesc(int minVersion, CBlockIndex* pindex, int nRequired, const Consensus::Params& consensusParams) | |
557 | { | |
558 | int nFound = 0; | |
559 | CBlockIndex* pstart = pindex; | |
560 | for (int i = 0; i < consensusParams.nMajorityWindow && pstart != NULL; i++) | |
561 | { | |
562 | if (pstart->nVersion >= minVersion) | |
563 | ++nFound; | |
564 | pstart = pstart->pprev; | |
565 | } | |
566 | ||
567 | Object rv; | |
568 | rv.push_back(Pair("status", nFound >= nRequired)); | |
569 | rv.push_back(Pair("found", nFound)); | |
570 | rv.push_back(Pair("required", nRequired)); | |
571 | rv.push_back(Pair("window", consensusParams.nMajorityWindow)); | |
572 | return rv; | |
573 | } | |
574 | ||
575 | Object SoftForkDesc(const std::string &name, int version, CBlockIndex* pindex, const Consensus::Params& consensusParams) | |
576 | { | |
577 | Object rv; | |
578 | rv.push_back(Pair("id", name)); | |
579 | rv.push_back(Pair("version", version)); | |
580 | rv.push_back(Pair("enforce", SoftForkMajorityDesc(version, pindex, consensusParams.nMajorityEnforceBlockUpgrade, consensusParams))); | |
581 | rv.push_back(Pair("reject", SoftForkMajorityDesc(version, pindex, consensusParams.nMajorityRejectBlockOutdated, consensusParams))); | |
582 | return rv; | |
583 | } | |
584 | ||
d387b8ec WL |
585 | Value getblockchaininfo(const Array& params, bool fHelp) |
586 | { | |
587 | if (fHelp || params.size() != 0) | |
588 | throw runtime_error( | |
589 | "getblockchaininfo\n" | |
590 | "Returns an object containing various state info regarding block chain processing.\n" | |
591 | "\nResult:\n" | |
592 | "{\n" | |
f6984e81 | 593 | " \"chain\": \"xxxx\", (string) current network name as defined in BIP70 (main, test, regtest)\n" |
d387b8ec | 594 | " \"blocks\": xxxxxx, (numeric) the current number of blocks processed in the server\n" |
ad6e6017 | 595 | " \"headers\": xxxxxx, (numeric) the current number of headers we have validated\n" |
d387b8ec WL |
596 | " \"bestblockhash\": \"...\", (string) the hash of the currently best block\n" |
597 | " \"difficulty\": xxxxxx, (numeric) the current difficulty\n" | |
598 | " \"verificationprogress\": xxxx, (numeric) estimate of verification progress [0..1]\n" | |
599 | " \"chainwork\": \"xxxx\" (string) total amount of work in active chain, in hexadecimal\n" | |
ba1da90b WL |
600 | " \"softforks\": [ (array) status of softforks in progress\n" |
601 | " {\n" | |
602 | " \"id\": \"xxxx\", (string) name of softfork\n" | |
603 | " \"version\": xx, (numeric) block version\n" | |
604 | " \"enforce\": { (object) progress toward enforcing the softfork rules for new-version blocks\n" | |
605 | " \"status\": xx, (boolean) true if threshold reached\n" | |
606 | " \"found\": xx, (numeric) number of blocks with the new version found\n" | |
607 | " \"required\": xx, (numeric) number of blocks required to trigger\n" | |
608 | " \"window\": xx, (numeric) maximum size of examined window of recent blocks\n" | |
609 | " },\n" | |
610 | " \"reject\": { ... } (object) progress toward rejecting pre-softfork blocks (same fields as \"enforce\")\n" | |
611 | " }, ...\n" | |
612 | " ]\n" | |
d387b8ec WL |
613 | "}\n" |
614 | "\nExamples:\n" | |
615 | + HelpExampleCli("getblockchaininfo", "") | |
616 | + HelpExampleRpc("getblockchaininfo", "") | |
617 | ); | |
618 | ||
4401b2d7 EL |
619 | LOCK(cs_main); |
620 | ||
d387b8ec | 621 | Object obj; |
f5ae6c98 PK |
622 | obj.push_back(Pair("chain", Params().NetworkIDString())); |
623 | obj.push_back(Pair("blocks", (int)chainActive.Height())); | |
ad6e6017 | 624 | obj.push_back(Pair("headers", pindexBestHeader ? pindexBestHeader->nHeight : -1)); |
f5ae6c98 | 625 | obj.push_back(Pair("bestblockhash", chainActive.Tip()->GetBlockHash().GetHex())); |
695a7a88 | 626 | obj.push_back(Pair("difficulty", (double)GetNetworkDifficulty())); |
11982d36 | 627 | obj.push_back(Pair("verificationprogress", Checkpoints::GuessVerificationProgress(Params().Checkpoints(), chainActive.Tip()))); |
f5ae6c98 | 628 | obj.push_back(Pair("chainwork", chainActive.Tip()->nChainWork.GetHex())); |
1b2e5555 | 629 | obj.push_back(Pair("pruned", fPruneMode)); |
ba1da90b WL |
630 | |
631 | const Consensus::Params& consensusParams = Params().GetConsensus(); | |
632 | CBlockIndex* tip = chainActive.Tip(); | |
633 | Array softforks; | |
634 | softforks.push_back(SoftForkDesc("bip34", 2, tip, consensusParams)); | |
635 | softforks.push_back(SoftForkDesc("bip66", 3, tip, consensusParams)); | |
6af25b0f | 636 | softforks.push_back(SoftForkDesc("bip65", 4, tip, consensusParams)); |
ba1da90b WL |
637 | obj.push_back(Pair("softforks", softforks)); |
638 | ||
1b2e5555 JS |
639 | if (fPruneMode) |
640 | { | |
641 | CBlockIndex *block = chainActive.Tip(); | |
642 | while (block && block->pprev && (block->pprev->nStatus & BLOCK_HAVE_DATA)) | |
643 | block = block->pprev; | |
644 | ||
645 | obj.push_back(Pair("pruneheight", block->nHeight)); | |
646 | } | |
d387b8ec WL |
647 | return obj; |
648 | } | |
b33bd7a3 | 649 | |
72fb3d29 | 650 | /** Comparison function for sorting the getchaintips heads. */ |
b33bd7a3 DK |
651 | struct CompareBlocksByHeight |
652 | { | |
653 | bool operator()(const CBlockIndex* a, const CBlockIndex* b) const | |
654 | { | |
655 | /* Make sure that unequal blocks with the same height do not compare | |
771d5002 | 656 | equal. Use the pointers themselves to make a distinction. */ |
b33bd7a3 DK |
657 | |
658 | if (a->nHeight != b->nHeight) | |
659 | return (a->nHeight > b->nHeight); | |
660 | ||
661 | return a < b; | |
662 | } | |
663 | }; | |
664 | ||
665 | Value getchaintips(const Array& params, bool fHelp) | |
666 | { | |
667 | if (fHelp || params.size() != 0) | |
668 | throw runtime_error( | |
669 | "getchaintips\n" | |
670 | "Return information about all known tips in the block tree," | |
671 | " including the main chain as well as orphaned branches.\n" | |
672 | "\nResult:\n" | |
673 | "[\n" | |
674 | " {\n" | |
675 | " \"height\": xxxx, (numeric) height of the chain tip\n" | |
676 | " \"hash\": \"xxxx\", (string) block hash of the tip\n" | |
677 | " \"branchlen\": 0 (numeric) zero for main chain\n" | |
1b91be49 | 678 | " \"status\": \"active\" (string) \"active\" for the main chain\n" |
b33bd7a3 DK |
679 | " },\n" |
680 | " {\n" | |
681 | " \"height\": xxxx,\n" | |
682 | " \"hash\": \"xxxx\",\n" | |
683 | " \"branchlen\": 1 (numeric) length of branch connecting the tip to the main chain\n" | |
1b91be49 | 684 | " \"status\": \"xxxx\" (string) status of the chain (active, valid-fork, valid-headers, headers-only, invalid)\n" |
b33bd7a3 DK |
685 | " }\n" |
686 | "]\n" | |
32b93a1b PW |
687 | "Possible values for status:\n" |
688 | "1. \"invalid\" This branch contains at least one invalid block\n" | |
689 | "2. \"headers-only\" Not all blocks for this branch are available, but the headers are valid\n" | |
690 | "3. \"valid-headers\" All blocks are available for this branch, but they were never fully validated\n" | |
691 | "4. \"valid-fork\" This branch is not part of the active chain, but is fully validated\n" | |
692 | "5. \"active\" This is the tip of the active main chain, which is certainly valid\n" | |
b33bd7a3 DK |
693 | "\nExamples:\n" |
694 | + HelpExampleCli("getchaintips", "") | |
695 | + HelpExampleRpc("getchaintips", "") | |
696 | ); | |
697 | ||
4401b2d7 EL |
698 | LOCK(cs_main); |
699 | ||
b33bd7a3 DK |
700 | /* Build up a list of chain tips. We start with the list of all |
701 | known blocks, and successively remove blocks that appear as pprev | |
702 | of another block. */ | |
703 | std::set<const CBlockIndex*, CompareBlocksByHeight> setTips; | |
704 | BOOST_FOREACH(const PAIRTYPE(const uint256, CBlockIndex*)& item, mapBlockIndex) | |
705 | setTips.insert(item.second); | |
706 | BOOST_FOREACH(const PAIRTYPE(const uint256, CBlockIndex*)& item, mapBlockIndex) | |
707 | { | |
708 | const CBlockIndex* pprev = item.second->pprev; | |
709 | if (pprev) | |
710 | setTips.erase(pprev); | |
711 | } | |
712 | ||
1b91be49 PW |
713 | // Always report the currently active tip. |
714 | setTips.insert(chainActive.Tip()); | |
715 | ||
b33bd7a3 DK |
716 | /* Construct the output array. */ |
717 | Array res; | |
718 | BOOST_FOREACH(const CBlockIndex* block, setTips) | |
719 | { | |
720 | Object obj; | |
721 | obj.push_back(Pair("height", block->nHeight)); | |
722 | obj.push_back(Pair("hash", block->phashBlock->GetHex())); | |
723 | ||
724 | const int branchLen = block->nHeight - chainActive.FindFork(block)->nHeight; | |
725 | obj.push_back(Pair("branchlen", branchLen)); | |
726 | ||
1b91be49 PW |
727 | string status; |
728 | if (chainActive.Contains(block)) { | |
729 | // This block is part of the currently active chain. | |
730 | status = "active"; | |
731 | } else if (block->nStatus & BLOCK_FAILED_MASK) { | |
732 | // This block or one of its ancestors is invalid. | |
733 | status = "invalid"; | |
734 | } else if (block->nChainTx == 0) { | |
735 | // This block cannot be connected because full block data for it or one of its parents is missing. | |
736 | status = "headers-only"; | |
737 | } else if (block->IsValid(BLOCK_VALID_SCRIPTS)) { | |
738 | // This block is fully validated, but no longer part of the active chain. It was probably the active block once, but was reorganized. | |
739 | status = "valid-fork"; | |
740 | } else if (block->IsValid(BLOCK_VALID_TREE)) { | |
741 | // The headers for this block are valid, but it has not been validated. It was probably never part of the most-work chain. | |
742 | status = "valid-headers"; | |
743 | } else { | |
744 | // No clue. | |
745 | status = "unknown"; | |
746 | } | |
747 | obj.push_back(Pair("status", status)); | |
748 | ||
b33bd7a3 DK |
749 | res.push_back(obj); |
750 | } | |
751 | ||
752 | return res; | |
753 | } | |
6f2c26a4 JG |
754 | |
755 | Value getmempoolinfo(const Array& params, bool fHelp) | |
756 | { | |
757 | if (fHelp || params.size() != 0) | |
758 | throw runtime_error( | |
759 | "getmempoolinfo\n" | |
760 | "\nReturns details on the active state of the TX memory pool.\n" | |
761 | "\nResult:\n" | |
762 | "{\n" | |
763 | " \"size\": xxxxx (numeric) Current tx count\n" | |
764 | " \"bytes\": xxxxx (numeric) Sum of all tx sizes\n" | |
765 | "}\n" | |
766 | "\nExamples:\n" | |
767 | + HelpExampleCli("getmempoolinfo", "") | |
768 | + HelpExampleRpc("getmempoolinfo", "") | |
769 | ); | |
770 | ||
771 | Object ret; | |
772 | ret.push_back(Pair("size", (int64_t) mempool.size())); | |
773 | ret.push_back(Pair("bytes", (int64_t) mempool.GetTotalTxSize())); | |
774 | ||
775 | return ret; | |
776 | } | |
777 | ||
9b0a8d31 PW |
778 | Value invalidateblock(const Array& params, bool fHelp) |
779 | { | |
780 | if (fHelp || params.size() != 1) | |
781 | throw runtime_error( | |
782 | "invalidateblock \"hash\"\n" | |
783 | "\nPermanently marks a block as invalid, as if it violated a consensus rule.\n" | |
784 | "\nArguments:\n" | |
785 | "1. hash (string, required) the hash of the block to mark as invalid\n" | |
786 | "\nResult:\n" | |
787 | "\nExamples:\n" | |
788 | + HelpExampleCli("invalidateblock", "\"blockhash\"") | |
789 | + HelpExampleRpc("invalidateblock", "\"blockhash\"") | |
790 | ); | |
791 | ||
792 | std::string strHash = params[0].get_str(); | |
34cdc411 | 793 | uint256 hash(uint256S(strHash)); |
9b0a8d31 PW |
794 | CValidationState state; |
795 | ||
796 | { | |
797 | LOCK(cs_main); | |
798 | if (mapBlockIndex.count(hash) == 0) | |
799 | throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block not found"); | |
800 | ||
801 | CBlockIndex* pblockindex = mapBlockIndex[hash]; | |
802 | InvalidateBlock(state, pblockindex); | |
803 | } | |
804 | ||
805 | if (state.IsValid()) { | |
806 | ActivateBestChain(state); | |
807 | } | |
808 | ||
809 | if (!state.IsValid()) { | |
810 | throw JSONRPCError(RPC_DATABASE_ERROR, state.GetRejectReason()); | |
811 | } | |
812 | ||
813 | return Value::null; | |
814 | } | |
815 | ||
816 | Value reconsiderblock(const Array& params, bool fHelp) | |
817 | { | |
818 | if (fHelp || params.size() != 1) | |
819 | throw runtime_error( | |
820 | "reconsiderblock \"hash\"\n" | |
821 | "\nRemoves invalidity status of a block and its descendants, reconsider them for activation.\n" | |
822 | "This can be used to undo the effects of invalidateblock.\n" | |
823 | "\nArguments:\n" | |
824 | "1. hash (string, required) the hash of the block to reconsider\n" | |
825 | "\nResult:\n" | |
826 | "\nExamples:\n" | |
827 | + HelpExampleCli("reconsiderblock", "\"blockhash\"") | |
828 | + HelpExampleRpc("reconsiderblock", "\"blockhash\"") | |
829 | ); | |
830 | ||
831 | std::string strHash = params[0].get_str(); | |
34cdc411 | 832 | uint256 hash(uint256S(strHash)); |
9b0a8d31 PW |
833 | CValidationState state; |
834 | ||
835 | { | |
836 | LOCK(cs_main); | |
837 | if (mapBlockIndex.count(hash) == 0) | |
838 | throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block not found"); | |
839 | ||
840 | CBlockIndex* pblockindex = mapBlockIndex[hash]; | |
841 | ReconsiderBlock(state, pblockindex); | |
842 | } | |
843 | ||
844 | if (state.IsValid()) { | |
845 | ActivateBestChain(state); | |
846 | } | |
847 | ||
848 | if (!state.IsValid()) { | |
849 | throw JSONRPCError(RPC_DATABASE_ERROR, state.GetRejectReason()); | |
850 | } | |
851 | ||
852 | return Value::null; | |
853 | } |