Dash Core  0.12.2.1
P2P Digital Currency
spentindex.py
Go to the documentation of this file.
1 #!/usr/bin/env python2
2 # Copyright (c) 2014-2015 The Bitcoin Core developers
3 # Distributed under the MIT software license, see the accompanying
4 # file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
6 #
7 # Test addressindex generation and fetching
8 #
9 
10 import time
11 from test_framework.test_framework import BitcoinTestFramework
12 from test_framework.util import *
13 from test_framework.script import *
14 from test_framework.mininode import *
15 import binascii
16 
18 
19  def setup_chain(self):
20  print("Initializing test directory "+self.options.tmpdir)
21  initialize_chain_clean(self.options.tmpdir, 4)
22 
23  def setup_network(self):
24  self.nodes = []
25  # Nodes 0/1 are "wallet" nodes
26  self.nodes.append(start_node(0, self.options.tmpdir, ["-debug"]))
27  self.nodes.append(start_node(1, self.options.tmpdir, ["-debug", "-spentindex"]))
28  # Nodes 2/3 are used for testing
29  self.nodes.append(start_node(2, self.options.tmpdir, ["-debug", "-spentindex"]))
30  self.nodes.append(start_node(3, self.options.tmpdir, ["-debug", "-spentindex", "-txindex"]))
31  connect_nodes(self.nodes[0], 1)
32  connect_nodes(self.nodes[0], 2)
33  connect_nodes(self.nodes[0], 3)
34 
35  self.is_network_split = False
36  self.sync_all()
37 
38  def run_test(self):
39  print "Mining blocks..."
40  self.nodes[0].generate(105)
41  self.sync_all()
42 
43  chain_height = self.nodes[1].getblockcount()
44  assert_equal(chain_height, 105)
45 
46  # Check that
47  print "Testing spent index..."
48 
49  privkey = "cU4zhap7nPJAWeMFu4j6jLrfPmqakDAzy8zn8Fhb3oEevdm4e5Lc"
50  address = "yeMpGzMj3rhtnz48XsfpB8itPHhHtgxLc3"
51  addressHash = "C5E4FB9171C22409809A3E8047A29C83886E325D".decode("hex")
52  scriptPubKey = CScript([OP_DUP, OP_HASH160, addressHash, OP_EQUALVERIFY, OP_CHECKSIG])
53  unspent = self.nodes[0].listunspent()
54  tx = CTransaction()
55  amount = unspent[0]["amount"] * 100000000
56  tx.vin = [CTxIn(COutPoint(int(unspent[0]["txid"], 16), unspent[0]["vout"]))]
57  tx.vout = [CTxOut(amount, scriptPubKey)]
58  tx.rehash()
59 
60  signed_tx = self.nodes[0].signrawtransaction(binascii.hexlify(tx.serialize()).decode("utf-8"))
61  txid = self.nodes[0].sendrawtransaction(signed_tx["hex"], True)
62  self.nodes[0].generate(1)
63  self.sync_all()
64 
65  print "Testing getspentinfo method..."
66 
67  # Check that the spentinfo works standalone
68  info = self.nodes[1].getspentinfo({"txid": unspent[0]["txid"], "index": unspent[0]["vout"]})
69  assert_equal(info["txid"], txid)
70  assert_equal(info["index"], 0)
71  assert_equal(info["height"], 106)
72 
73  print "Testing getrawtransaction method..."
74 
75  # Check that verbose raw transaction includes spent info
76  txVerbose = self.nodes[3].getrawtransaction(unspent[0]["txid"], 1)
77  assert_equal(txVerbose["vout"][unspent[0]["vout"]]["spentTxId"], txid)
78  assert_equal(txVerbose["vout"][unspent[0]["vout"]]["spentIndex"], 0)
79  assert_equal(txVerbose["vout"][unspent[0]["vout"]]["spentHeight"], 106)
80 
81  # Check that verbose raw transaction includes input values
82  txVerbose2 = self.nodes[3].getrawtransaction(txid, 1)
83  assert_equal(txVerbose2["vin"][0]["value"], Decimal(unspent[0]["amount"]))
84  assert_equal(txVerbose2["vin"][0]["valueSat"], amount)
85 
86  # Check that verbose raw transaction includes address values and input values
87  privkey2 = "cU4zhap7nPJAWeMFu4j6jLrfPmqakDAzy8zn8Fhb3oEevdm4e5Lc"
88  address2 = "yeMpGzMj3rhtnz48XsfpB8itPHhHtgxLc3"
89  addressHash2 = "C5E4FB9171C22409809A3E8047A29C83886E325D".decode("hex")
90  scriptPubKey2 = CScript([OP_DUP, OP_HASH160, addressHash2, OP_EQUALVERIFY, OP_CHECKSIG])
91  tx2 = CTransaction()
92  tx2.vin = [CTxIn(COutPoint(int(txid, 16), 0))]
93  tx2.vout = [CTxOut(amount, scriptPubKey2)]
94  tx.rehash()
95  self.nodes[0].importprivkey(privkey)
96  signed_tx2 = self.nodes[0].signrawtransaction(binascii.hexlify(tx2.serialize()).decode("utf-8"))
97  txid2 = self.nodes[0].sendrawtransaction(signed_tx2["hex"], True)
98 
99  # Check the mempool index
100  self.sync_all()
101  txVerbose3 = self.nodes[1].getrawtransaction(txid2, 1)
102  assert_equal(txVerbose3["vin"][0]["address"], address2)
103  assert_equal(txVerbose3["vin"][0]["value"], Decimal(unspent[0]["amount"]))
104  assert_equal(txVerbose3["vin"][0]["valueSat"], amount)
105 
106  # Check the database index
107  self.nodes[0].generate(1)
108  self.sync_all()
109 
110  txVerbose4 = self.nodes[3].getrawtransaction(txid2, 1)
111  assert_equal(txVerbose4["vin"][0]["address"], address2)
112  assert_equal(txVerbose4["vin"][0]["value"], Decimal(unspent[0]["amount"]))
113  assert_equal(txVerbose4["vin"][0]["valueSat"], amount)
114 
115  print "Passed\n"
116 
117 
118 if __name__ == '__main__':
119  SpentIndexTest().main()
UniValue importprivkey(const UniValue &params, bool fHelp)
Definition: rpcdump.cpp:76
UniValue getspentinfo(const UniValue &params, bool fHelp)
Definition: misc.cpp:948
UniValue getrawtransaction(const UniValue &params, bool fHelp)
UniValue listunspent(const UniValue &params, bool fHelp)
Definition: rpcwallet.cpp:2533
def connect_nodes(from_connection, node_num)
Definition: util.py:343
UniValue getblockcount(const UniValue &params, bool fHelp)
Definition: blockchain.cpp:131
def initialize_chain_clean(test_dir, num_nodes)
Definition: util.py:252
UniValue signrawtransaction(const UniValue &params, bool fHelp)
def start_node(i, dirname, extra_args=None, rpchost=None, timewait=None, binary=None)
Definition: util.py:281
UniValue generate(const UniValue &params, bool fHelp)
Definition: mining.cpp:122
UniValue sendrawtransaction(const UniValue &params, bool fHelp)
def assert_equal(thing1, thing2)
Definition: util.py:461