Skip to content

Commit 1a0f09a

Browse files
author
Braydon Fuller
committed
Add method to get address deltas from a block
1 parent e0d02ff commit 1a0f09a

File tree

4 files changed

+157
-1
lines changed

4 files changed

+157
-1
lines changed

qa/rpc-tests/spentindex.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,14 +104,34 @@ def run_test(self):
104104
assert_equal(txVerbose3["vin"][0]["valueSat"], amount)
105105

106106
# Check the database index
107-
self.nodes[0].generate(1)
107+
block_hash = self.nodes[0].generate(1)
108108
self.sync_all()
109109

110110
txVerbose4 = self.nodes[3].getrawtransaction(txid2, 1)
111111
assert_equal(txVerbose4["vin"][0]["address"], address2)
112112
assert_equal(txVerbose4["vin"][0]["value"], Decimal(unspent[0]["amount"]))
113113
assert_equal(txVerbose4["vin"][0]["valueSat"], amount)
114114

115+
116+
# Check block deltas
117+
print "Testing getblockdeltas..."
118+
119+
block = self.nodes[3].getblockdeltas(block_hash[0])
120+
assert_equal(len(block["deltas"]), 2)
121+
assert_equal(block["deltas"][0]["index"], 0)
122+
assert_equal(len(block["deltas"][0]["inputs"]), 0)
123+
assert_equal(len(block["deltas"][0]["outputs"]), 0)
124+
assert_equal(block["deltas"][1]["index"], 1)
125+
assert_equal(block["deltas"][1]["txid"], txid2)
126+
assert_equal(block["deltas"][1]["inputs"][0]["index"], 0)
127+
assert_equal(block["deltas"][1]["inputs"][0]["address"], "mgY65WSfEmsyYaYPQaXhmXMeBhwp4EcsQW")
128+
assert_equal(block["deltas"][1]["inputs"][0]["satoshis"], amount * -1)
129+
assert_equal(block["deltas"][1]["inputs"][0]["prevtxid"], txid)
130+
assert_equal(block["deltas"][1]["inputs"][0]["prevout"], 0)
131+
assert_equal(block["deltas"][1]["outputs"][0]["index"], 0)
132+
assert_equal(block["deltas"][1]["outputs"][0]["address"], "mgY65WSfEmsyYaYPQaXhmXMeBhwp4EcsQW")
133+
assert_equal(block["deltas"][1]["outputs"][0]["satoshis"], amount)
134+
115135
print "Passed\n"
116136

117137

src/rpcblockchain.cpp

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
55

66
#include "amount.h"
7+
#include "base58.h"
78
#include "chain.h"
89
#include "chainparams.h"
910
#include "checkpoints.h"
@@ -13,6 +14,10 @@
1314
#include "policy/policy.h"
1415
#include "primitives/transaction.h"
1516
#include "rpcserver.h"
17+
#include "script/script.h"
18+
#include "script/script_error.h"
19+
#include "script/sign.h"
20+
#include "script/standard.h"
1621
#include "streams.h"
1722
#include "sync.h"
1823
#include "txmempool.h"
@@ -86,6 +91,112 @@ UniValue blockheaderToJSON(const CBlockIndex* blockindex)
8691
return result;
8792
}
8893

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+
89200
UniValue blockToJSON(const CBlock& block, const CBlockIndex* blockindex, bool txDetails = false)
90201
{
91202
UniValue result(UniValue::VOBJ);
@@ -275,6 +386,29 @@ UniValue getrawmempool(const UniValue& params, bool fHelp)
275386
return mempoolToJSON(fVerbose);
276387
}
277388

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+
278412
UniValue getblockhashes(const UniValue& params, bool fHelp)
279413
{
280414
if (fHelp || params.size() != 2)

src/rpcserver.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,7 @@ static const CRPCCommand vRPCCommands[] =
278278
{ "blockchain", "getbestblockhash", &getbestblockhash, true },
279279
{ "blockchain", "getblockcount", &getblockcount, true },
280280
{ "blockchain", "getblock", &getblock, true },
281+
{ "blockchain", "getblockdeltas", &getblockdeltas, false },
281282
{ "blockchain", "getblockhashes", &getblockhashes, true },
282283
{ "blockchain", "getblockhash", &getblockhash, true },
283284
{ "blockchain", "getblockheader", &getblockheader, true },

src/rpcserver.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,7 @@ extern UniValue settxfee(const UniValue& params, bool fHelp);
262262
extern UniValue getmempoolinfo(const UniValue& params, bool fHelp);
263263
extern UniValue getrawmempool(const UniValue& params, bool fHelp);
264264
extern UniValue getblockhashes(const UniValue& params, bool fHelp);
265+
extern UniValue getblockdeltas(const UniValue& params, bool fHelp);
265266
extern UniValue getblockhash(const UniValue& params, bool fHelp);
266267
extern UniValue getblockheader(const UniValue& params, bool fHelp);
267268
extern UniValue getblock(const UniValue& params, bool fHelp);

0 commit comments

Comments
 (0)