|
4 | 4 | // file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
5 | 5 |
|
6 | 6 | #include "amount.h"
|
| 7 | +#include "base58.h" |
7 | 8 | #include "chain.h"
|
8 | 9 | #include "chainparams.h"
|
9 | 10 | #include "checkpoints.h"
|
|
13 | 14 | #include "policy/policy.h"
|
14 | 15 | #include "primitives/transaction.h"
|
15 | 16 | #include "rpcserver.h"
|
| 17 | +#include "script/script.h" |
| 18 | +#include "script/script_error.h" |
| 19 | +#include "script/sign.h" |
| 20 | +#include "script/standard.h" |
16 | 21 | #include "streams.h"
|
17 | 22 | #include "sync.h"
|
18 | 23 | #include "txmempool.h"
|
@@ -86,6 +91,112 @@ UniValue blockheaderToJSON(const CBlockIndex* blockindex)
|
86 | 91 | return result;
|
87 | 92 | }
|
88 | 93 |
|
| 94 | +UniValue blockToDeltasJSON(const CBlock& block, const CBlockIndex* blockindex) |
| 95 | +{ |
| 96 | + UniValue result(UniValue::VOBJ); |
| 97 | + result.push_back(Pair("hash", block.GetHash().GetHex())); |
| 98 | + int confirmations = -1; |
| 99 | + // Only report confirmations if the block is on the main chain |
| 100 | + if (chainActive.Contains(blockindex)) { |
| 101 | + confirmations = chainActive.Height() - blockindex->nHeight + 1; |
| 102 | + } else { |
| 103 | + throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block is an orphan"); |
| 104 | + } |
| 105 | + result.push_back(Pair("confirmations", confirmations)); |
| 106 | + result.push_back(Pair("size", (int)::GetSerializeSize(block, SER_NETWORK, PROTOCOL_VERSION))); |
| 107 | + result.push_back(Pair("height", blockindex->nHeight)); |
| 108 | + result.push_back(Pair("version", block.nVersion)); |
| 109 | + result.push_back(Pair("merkleroot", block.hashMerkleRoot.GetHex())); |
| 110 | + |
| 111 | + UniValue deltas(UniValue::VARR); |
| 112 | + |
| 113 | + for (unsigned int i = 0; i < block.vtx.size(); i++) { |
| 114 | + const CTransaction &tx = block.vtx[i]; |
| 115 | + const uint256 txhash = tx.GetHash(); |
| 116 | + |
| 117 | + UniValue entry(UniValue::VOBJ); |
| 118 | + entry.push_back(Pair("txid", txhash.GetHex())); |
| 119 | + entry.push_back(Pair("index", (int)i)); |
| 120 | + |
| 121 | + UniValue inputs(UniValue::VARR); |
| 122 | + |
| 123 | + if (!tx.IsCoinBase()) { |
| 124 | + |
| 125 | + for (size_t j = 0; j < tx.vin.size(); j++) { |
| 126 | + const CTxIn input = tx.vin[j]; |
| 127 | + |
| 128 | + UniValue delta(UniValue::VOBJ); |
| 129 | + |
| 130 | + CSpentIndexValue spentInfo; |
| 131 | + CSpentIndexKey spentKey(input.prevout.hash, input.prevout.n); |
| 132 | + |
| 133 | + if (GetSpentIndex(spentKey, spentInfo)) { |
| 134 | + if (spentInfo.addressType == 1) { |
| 135 | + delta.push_back(Pair("address", CBitcoinAddress(CKeyID(spentInfo.addressHash)).ToString())); |
| 136 | + } else if (spentInfo.addressType == 2) { |
| 137 | + delta.push_back(Pair("address", CBitcoinAddress(CScriptID(spentInfo.addressHash)).ToString())); |
| 138 | + } else { |
| 139 | + continue; |
| 140 | + } |
| 141 | + delta.push_back(Pair("satoshis", -1 * spentInfo.satoshis)); |
| 142 | + delta.push_back(Pair("index", (int)j)); |
| 143 | + delta.push_back(Pair("prevtxid", input.prevout.hash.GetHex())); |
| 144 | + delta.push_back(Pair("prevout", (int)input.prevout.n)); |
| 145 | + |
| 146 | + inputs.push_back(delta); |
| 147 | + } else { |
| 148 | + throw JSONRPCError(RPC_INTERNAL_ERROR, "Spent information not available"); |
| 149 | + } |
| 150 | + |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + entry.push_back(Pair("inputs", inputs)); |
| 155 | + |
| 156 | + UniValue outputs(UniValue::VARR); |
| 157 | + |
| 158 | + for (unsigned int k = 0; k < tx.vout.size(); k++) { |
| 159 | + const CTxOut &out = tx.vout[k]; |
| 160 | + |
| 161 | + UniValue delta(UniValue::VOBJ); |
| 162 | + |
| 163 | + if (out.scriptPubKey.IsPayToScriptHash()) { |
| 164 | + vector<unsigned char> hashBytes(out.scriptPubKey.begin()+2, out.scriptPubKey.begin()+22); |
| 165 | + delta.push_back(Pair("address", CBitcoinAddress(CScriptID(uint160(hashBytes))).ToString())); |
| 166 | + |
| 167 | + } else if (out.scriptPubKey.IsPayToPublicKeyHash()) { |
| 168 | + vector<unsigned char> hashBytes(out.scriptPubKey.begin()+3, out.scriptPubKey.begin()+23); |
| 169 | + delta.push_back(Pair("address", CBitcoinAddress(CKeyID(uint160(hashBytes))).ToString())); |
| 170 | + } else { |
| 171 | + continue; |
| 172 | + } |
| 173 | + |
| 174 | + delta.push_back(Pair("satoshis", out.nValue)); |
| 175 | + delta.push_back(Pair("index", (int)k)); |
| 176 | + |
| 177 | + outputs.push_back(delta); |
| 178 | + } |
| 179 | + |
| 180 | + entry.push_back(Pair("outputs", outputs)); |
| 181 | + deltas.push_back(entry); |
| 182 | + |
| 183 | + } |
| 184 | + result.push_back(Pair("deltas", deltas)); |
| 185 | + result.push_back(Pair("time", block.GetBlockTime())); |
| 186 | + result.push_back(Pair("mediantime", (int64_t)blockindex->GetMedianTimePast())); |
| 187 | + result.push_back(Pair("nonce", (uint64_t)block.nNonce)); |
| 188 | + result.push_back(Pair("bits", strprintf("%08x", block.nBits))); |
| 189 | + result.push_back(Pair("difficulty", GetDifficulty(blockindex))); |
| 190 | + result.push_back(Pair("chainwork", blockindex->nChainWork.GetHex())); |
| 191 | + |
| 192 | + if (blockindex->pprev) |
| 193 | + result.push_back(Pair("previousblockhash", blockindex->pprev->GetBlockHash().GetHex())); |
| 194 | + CBlockIndex *pnext = chainActive.Next(blockindex); |
| 195 | + if (pnext) |
| 196 | + result.push_back(Pair("nextblockhash", pnext->GetBlockHash().GetHex())); |
| 197 | + return result; |
| 198 | +} |
| 199 | + |
89 | 200 | UniValue blockToJSON(const CBlock& block, const CBlockIndex* blockindex, bool txDetails = false)
|
90 | 201 | {
|
91 | 202 | UniValue result(UniValue::VOBJ);
|
@@ -275,6 +386,29 @@ UniValue getrawmempool(const UniValue& params, bool fHelp)
|
275 | 386 | return mempoolToJSON(fVerbose);
|
276 | 387 | }
|
277 | 388 |
|
| 389 | +UniValue getblockdeltas(const UniValue& params, bool fHelp) |
| 390 | +{ |
| 391 | + if (fHelp || params.size() != 1) |
| 392 | + throw runtime_error(""); |
| 393 | + |
| 394 | + std::string strHash = params[0].get_str(); |
| 395 | + uint256 hash(uint256S(strHash)); |
| 396 | + |
| 397 | + if (mapBlockIndex.count(hash) == 0) |
| 398 | + throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block not found"); |
| 399 | + |
| 400 | + CBlock block; |
| 401 | + CBlockIndex* pblockindex = mapBlockIndex[hash]; |
| 402 | + |
| 403 | + if (fHavePruned && !(pblockindex->nStatus & BLOCK_HAVE_DATA) && pblockindex->nTx > 0) |
| 404 | + throw JSONRPCError(RPC_INTERNAL_ERROR, "Block not available (pruned data)"); |
| 405 | + |
| 406 | + if(!ReadBlockFromDisk(block, pblockindex, Params().GetConsensus())) |
| 407 | + throw JSONRPCError(RPC_INTERNAL_ERROR, "Can't read block from disk"); |
| 408 | + |
| 409 | + return blockToDeltasJSON(block, pblockindex); |
| 410 | +} |
| 411 | + |
278 | 412 | UniValue getblockhashes(const UniValue& params, bool fHelp)
|
279 | 413 | {
|
280 | 414 | if (fHelp || params.size() < 2)
|
|
0 commit comments