changeset 29673:67db818ef672

rpc: add input value and address to getrawtransaction if spentindex enabled
author Braydon Fuller <braydon@bitpay.com>
date Fri, 13 May 2016 11:43:29 -0400
parents b32a72bc5d66
children db8ab86c45c6
files qa/rpc-tests/spentindex.py src/rpcrawtransaction.cpp
diffstat 2 files changed, 43 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/qa/rpc-tests/spentindex.py	Fri May 13 11:43:01 2016 -0400
+++ b/qa/rpc-tests/spentindex.py	Fri May 13 11:43:29 2016 -0400
@@ -62,18 +62,47 @@
         self.nodes[0].generate(1)
         self.sync_all()
 
+        print "Testing getspentinfo method..."
+
         # 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)
         assert_equal(info["height"], 106)
 
+        print "Testing getrawtransaction method..."
+
         # 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)
         assert_equal(txVerbose["vout"][unspent[0]["vout"]]["spentHeight"], 106)
 
+        # Check that verbose raw transaction includes input values
+        txVerbose2 = self.nodes[3].getrawtransaction(txid, 1)
+        assert_equal(txVerbose2["vin"][0]["value"], Decimal(unspent[0]["amount"]))
+        assert_equal(txVerbose2["vin"][0]["valueSat"], amount)
+
+        # Check that verbose raw transaction includes address values and input values
+        privkey2 = "cSdkPxkAjA4HDr5VHgsebAPDEh9Gyub4HK8UJr2DFGGqKKy4K5sG"
+        address2 = "mgY65WSfEmsyYaYPQaXhmXMeBhwp4EcsQW"
+        addressHash2 = "0b2f0a0c31bfe0406b0ccc1381fdbe311946dadc".decode("hex")
+        scriptPubKey2 = CScript([OP_DUP, OP_HASH160, addressHash2, OP_EQUALVERIFY, OP_CHECKSIG])
+        tx2 = CTransaction()
+        tx2.vin = [CTxIn(COutPoint(int(txid, 16), 0))]
+        tx2.vout = [CTxOut(amount, scriptPubKey2)]
+        tx.rehash()
+        self.nodes[0].importprivkey(privkey)
+        signed_tx2 = self.nodes[0].signrawtransaction(binascii.hexlify(tx2.serialize()).decode("utf-8"))
+        txid2 = self.nodes[0].sendrawtransaction(signed_tx2["hex"], True)
+        self.nodes[0].generate(1)
+        self.sync_all()
+
+        txVerbose3 = self.nodes[3].getrawtransaction(txid2, 1)
+        assert_equal(txVerbose3["vin"][0]["address"], address2)
+        assert_equal(txVerbose2["vin"][0]["value"], Decimal(unspent[0]["amount"]))
+        assert_equal(txVerbose2["vin"][0]["valueSat"], amount)
+
         print "Passed\n"
 
 
--- a/src/rpcrawtransaction.cpp	Fri May 13 11:43:01 2016 -0400
+++ b/src/rpcrawtransaction.cpp	Fri May 13 11:43:29 2016 -0400
@@ -78,6 +78,20 @@
             o.push_back(Pair("asm", ScriptToAsmStr(txin.scriptSig, true)));
             o.push_back(Pair("hex", HexStr(txin.scriptSig.begin(), txin.scriptSig.end())));
             in.push_back(Pair("scriptSig", o));
+
+            // Add address and value info if spentindex enabled
+            CSpentIndexValue spentInfo;
+            CSpentIndexKey spentKey(txin.prevout.hash, txin.prevout.n);
+            if (GetSpentIndex(spentKey, spentInfo)) {
+                in.push_back(Pair("value", ValueFromAmount(spentInfo.satoshis)));
+                in.push_back(Pair("valueSat", spentInfo.satoshis));
+                if (spentInfo.addressType == 1) {
+                    in.push_back(Pair("address", CBitcoinAddress(CKeyID(spentInfo.addressHash)).ToString()));
+                } else if (spentInfo.addressType == 2)  {
+                    in.push_back(Pair("address", CBitcoinAddress(CScriptID(spentInfo.addressHash)).ToString()));
+                }
+            }
+
         }
         in.push_back(Pair("sequence", (int64_t)txin.nSequence));
         vin.push_back(in);