rpc: include spent info if spentindex enabled with getrawtransaction verbose

This commit is contained in:
Braydon Fuller 2016-04-12 12:31:21 -04:00 committed by Larry Ruane
parent caae164e68
commit 74d888e963
3 changed files with 19 additions and 3 deletions

View File

@ -27,7 +27,7 @@ class SpentIndexTest(BitcoinTestFramework):
self.nodes.append(start_node(1, self.options.tmpdir, ["-debug", "-spentindex"])) self.nodes.append(start_node(1, self.options.tmpdir, ["-debug", "-spentindex"]))
# Nodes 2/3 are used for testing # Nodes 2/3 are used for testing
self.nodes.append(start_node(2, self.options.tmpdir, ["-debug", "-spentindex"])) self.nodes.append(start_node(2, self.options.tmpdir, ["-debug", "-spentindex"]))
self.nodes.append(start_node(3, self.options.tmpdir, ["-debug", "-spentindex"])) self.nodes.append(start_node(3, self.options.tmpdir, ["-debug", "-spentindex", "-txindex"]))
connect_nodes(self.nodes[0], 1) connect_nodes(self.nodes[0], 1)
connect_nodes(self.nodes[0], 2) connect_nodes(self.nodes[0], 2)
connect_nodes(self.nodes[0], 3) connect_nodes(self.nodes[0], 3)
@ -62,10 +62,16 @@ class SpentIndexTest(BitcoinTestFramework):
self.nodes[0].generate(1) self.nodes[0].generate(1)
self.sync_all() self.sync_all()
# Check that the spentinfo works standalone
info = self.nodes[1].getspentinfo({"txid": unspent[0]["txid"], "index": unspent[0]["vout"]}) info = self.nodes[1].getspentinfo({"txid": unspent[0]["txid"], "index": unspent[0]["vout"]})
assert_equal(info["txid"], txid) assert_equal(info["txid"], txid)
assert_equal(info["index"], 0) assert_equal(info["index"], 0)
# Check that verbose raw transaction includes spent info
txVerbose = self.nodes[3].getrawtransaction(unspent[0]["txid"], 1)
assert_equal(txVerbose["vout"][unspent[0]["vout"]]["spentTxId"], txid)
assert_equal(txVerbose["vout"][unspent[0]["vout"]]["spentIndex"], 0)
print "Passed\n" print "Passed\n"

View File

@ -1622,7 +1622,7 @@ bool GetTimestampIndex(const unsigned int &high, const unsigned int &low, std::v
bool GetSpentIndex(CSpentIndexKey &key, CSpentIndexValue &value) bool GetSpentIndex(CSpentIndexKey &key, CSpentIndexValue &value)
{ {
if (!fSpentIndex) if (!fSpentIndex)
return error("spent index not enabled"); return false;
if (!pblocktree->ReadSpentIndex(key, value)) if (!pblocktree->ReadSpentIndex(key, value))
return error("unable to get spent info"); return error("unable to get spent info");

View File

@ -147,7 +147,8 @@ UniValue TxShieldedOutputsToJSON(const CTransaction& tx) {
void TxToJSON(const CTransaction& tx, const uint256 hashBlock, UniValue& entry) void TxToJSON(const CTransaction& tx, const uint256 hashBlock, UniValue& entry)
{ {
entry.push_back(Pair("txid", tx.GetHash().GetHex())); uint256 txid = tx.GetHash();
entry.push_back(Pair("txid", txid.GetHex()));
entry.push_back(Pair("overwintered", tx.fOverwintered)); entry.push_back(Pair("overwintered", tx.fOverwintered));
entry.push_back(Pair("version", tx.nVersion)); entry.push_back(Pair("version", tx.nVersion));
if (tx.fOverwintered) { if (tx.fOverwintered) {
@ -184,6 +185,15 @@ void TxToJSON(const CTransaction& tx, const uint256 hashBlock, UniValue& entry)
UniValue o(UniValue::VOBJ); UniValue o(UniValue::VOBJ);
ScriptPubKeyToJSON(txout.scriptPubKey, o, true); ScriptPubKeyToJSON(txout.scriptPubKey, o, true);
out.push_back(Pair("scriptPubKey", o)); out.push_back(Pair("scriptPubKey", o));
// Add spent information if spentindex is enabled
CSpentIndexValue spentInfo;
CSpentIndexKey spentKey(txid, i);
if (GetSpentIndex(spentKey, spentInfo)) {
out.push_back(Pair("spentTxId", spentInfo.txid.GetHex()));
out.push_back(Pair("spentIndex", (int)spentInfo.inputIndex));
}
vout.push_back(out); vout.push_back(out);
} }
entry.push_back(Pair("vout", vout)); entry.push_back(Pair("vout", vout));