From b752fbe09e73e22101c151f139c2001f08d9bd00 Mon Sep 17 00:00:00 2001 From: Braydon Fuller Date: Tue, 12 Apr 2016 12:31:21 -0400 Subject: [PATCH] rpc: include spent info if spentindex enabled with getrawtransaction verbose --- qa/rpc-tests/spentindex.py | 8 +++++++- src/main.cpp | 2 +- src/rpcrawtransaction.cpp | 12 +++++++++++- 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/qa/rpc-tests/spentindex.py b/qa/rpc-tests/spentindex.py index 0fe98dcbd5..44ba286609 100755 --- a/qa/rpc-tests/spentindex.py +++ b/qa/rpc-tests/spentindex.py @@ -27,7 +27,7 @@ class SpentIndexTest(BitcoinTestFramework): self.nodes.append(start_node(1, self.options.tmpdir, ["-debug", "-spentindex"])) # Nodes 2/3 are used for testing 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], 2) connect_nodes(self.nodes[0], 3) @@ -62,10 +62,16 @@ class SpentIndexTest(BitcoinTestFramework): self.nodes[0].generate(1) self.sync_all() + # Check that the spentinfo works standalone info = self.nodes[1].getspentinfo({"txid": unspent[0]["txid"], "index": unspent[0]["vout"]}) assert_equal(info["txid"], txid) 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" diff --git a/src/main.cpp b/src/main.cpp index e96d58aa6e..d750477e92 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1458,7 +1458,7 @@ bool GetTimestampIndex(const unsigned int &high, const unsigned int &low, std::v bool GetSpentIndex(CSpentIndexKey &key, CSpentIndexValue &value) { if (!fSpentIndex) - return error("spent index not enabled"); + return false; if (!pblocktree->ReadSpentIndex(key, value)) return error("unable to get spent info"); diff --git a/src/rpcrawtransaction.cpp b/src/rpcrawtransaction.cpp index 6ab1807d46..0e8df7faed 100644 --- a/src/rpcrawtransaction.cpp +++ b/src/rpcrawtransaction.cpp @@ -61,7 +61,8 @@ void ScriptPubKeyToJSON(const CScript& scriptPubKey, UniValue& out, bool fInclud 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("size", (int)::GetSerializeSize(tx, SER_NETWORK, PROTOCOL_VERSION))); entry.push_back(Pair("version", tx.nVersion)); entry.push_back(Pair("locktime", (int64_t)tx.nLockTime)); @@ -91,6 +92,15 @@ void TxToJSON(const CTransaction& tx, const uint256 hashBlock, UniValue& entry) UniValue o(UniValue::VOBJ); ScriptPubKeyToJSON(txout.scriptPubKey, o, true); 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); } entry.push_back(Pair("vout", vout));