changeset 29655:81c811cfcccf

rpc: include spent info if spentindex enabled with getrawtransaction verbose
author Braydon Fuller <courier@braydon.com>
date Tue, 12 Apr 2016 12:31:21 -0400
parents 034b7814ab95
children 604556232498
files qa/rpc-tests/spentindex.py src/main.cpp src/rpcrawtransaction.cpp
diffstat 3 files changed, 19 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/qa/rpc-tests/spentindex.py	Tue Apr 05 15:53:38 2016 -0400
+++ b/qa/rpc-tests/spentindex.py	Tue Apr 12 12:31:21 2016 -0400
@@ -27,7 +27,7 @@
         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 @@
         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"
 
 
--- a/src/main.cpp	Tue Apr 05 15:53:38 2016 -0400
+++ b/src/main.cpp	Tue Apr 12 12:31:21 2016 -0400
@@ -1458,7 +1458,7 @@
 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");
--- a/src/rpcrawtransaction.cpp	Tue Apr 05 15:53:38 2016 -0400
+++ b/src/rpcrawtransaction.cpp	Tue Apr 12 12:31:21 2016 -0400
@@ -61,7 +61,8 @@
 
 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 @@
         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));