Dash Core  0.12.2.1
P2P Digital Currency
rawtransactions.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 re-org scenarios with a mempool that contains transactions
8 # that spend (directly or indirectly) coinbase transactions.
9 #
10 
11 from test_framework.test_framework import BitcoinTestFramework
12 from test_framework.util import *
13 
14 # Create one-input, one-output, no-fee transaction:
16 
17  def setup_chain(self):
18  print("Initializing test directory "+self.options.tmpdir)
19  initialize_chain_clean(self.options.tmpdir, 3)
20 
21  def setup_network(self, split=False):
22  self.nodes = start_nodes(3, self.options.tmpdir)
23 
24  #connect to a local machine for debugging
25  #url = "http://bitcoinrpc:DP6DvqZtqXarpeNWyN3LZTFchCCyCUuHwNF7E8pX99x1@%s:%d" % ('127.0.0.1', 18332)
26  #proxy = AuthServiceProxy(url)
27  #proxy.url = url # store URL on proxy for info
28  #self.nodes.append(proxy)
29 
30  connect_nodes_bi(self.nodes,0,1)
31  connect_nodes_bi(self.nodes,1,2)
32  connect_nodes_bi(self.nodes,0,2)
33 
34  self.is_network_split=False
35  self.sync_all()
36 
37  def run_test(self):
38 
39  #prepare some coins for multiple *rawtransaction commands
40  self.nodes[2].generate(1)
41  self.sync_all()
42  self.nodes[0].generate(101)
43  self.sync_all()
44  self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(),1.5)
45  self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(),1.0)
46  self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(),5.0)
47  self.sync_all()
48  self.nodes[0].generate(5)
49  self.sync_all()
50 
51 
54  inputs = [ {'txid' : "1d1d4e24ed99057e84c3f80fd8fbec79ed9e1acee37da269356ecea000000000", 'vout' : 1}] #won't exists
55  outputs = { self.nodes[0].getnewaddress() : 4.998 }
56  rawtx = self.nodes[2].createrawtransaction(inputs, outputs)
57  rawtx = self.nodes[2].signrawtransaction(rawtx)
58 
59  try:
60  rawtx = self.nodes[2].sendrawtransaction(rawtx['hex'])
61  except JSONRPCException as e:
62  assert("Missing inputs" in e.error['message'])
63  else:
64  assert(False)
65 
66 
67 
71  addr1 = self.nodes[2].getnewaddress()
72  addr2 = self.nodes[2].getnewaddress()
73 
74  addr1Obj = self.nodes[2].validateaddress(addr1)
75  addr2Obj = self.nodes[2].validateaddress(addr2)
76 
77  mSigObj = self.nodes[2].addmultisigaddress(2, [addr1Obj['pubkey'], addr2Obj['pubkey']])
78  mSigObjValid = self.nodes[2].validateaddress(mSigObj)
79 
80  #use balance deltas instead of absolute values
81  bal = self.nodes[2].getbalance()
82 
83  # send 1.2 BTC to msig adr
84  txId = self.nodes[0].sendtoaddress(mSigObj, 1.2)
85  self.sync_all()
86  self.nodes[0].generate(1)
87  self.sync_all()
88  assert_equal(self.nodes[2].getbalance(), bal+Decimal('1.20000000')) #node2 has both keys of the 2of2 ms addr., tx should affect the balance
89 
90 
91  # 2of3 test from different nodes
92  bal = self.nodes[2].getbalance()
93  addr1 = self.nodes[1].getnewaddress()
94  addr2 = self.nodes[2].getnewaddress()
95  addr3 = self.nodes[2].getnewaddress()
96 
97  addr1Obj = self.nodes[1].validateaddress(addr1)
98  addr2Obj = self.nodes[2].validateaddress(addr2)
99  addr3Obj = self.nodes[2].validateaddress(addr3)
100 
101  mSigObj = self.nodes[2].addmultisigaddress(2, [addr1Obj['pubkey'], addr2Obj['pubkey'], addr3Obj['pubkey']])
102  mSigObjValid = self.nodes[2].validateaddress(mSigObj)
103 
104  txId = self.nodes[0].sendtoaddress(mSigObj, 2.2)
105  decTx = self.nodes[0].gettransaction(txId)
106  rawTx = self.nodes[0].decoderawtransaction(decTx['hex'])
107  sPK = rawTx['vout'][0]['scriptPubKey']['hex']
108  self.sync_all()
109  self.nodes[0].generate(1)
110  self.sync_all()
111 
112  #THIS IS A INCOMPLETE FEATURE
113  #NODE2 HAS TWO OF THREE KEY AND THE FUNDS SHOULD BE SPENDABLE AND COUNT AT BALANCE CALCULATION
114  assert_equal(self.nodes[2].getbalance(), bal) #for now, assume the funds of a 2of3 multisig tx are not marked as spendable
115 
116  txDetails = self.nodes[0].gettransaction(txId, True)
117  rawTx = self.nodes[0].decoderawtransaction(txDetails['hex'])
118  vout = False
119  for outpoint in rawTx['vout']:
120  if outpoint['value'] == Decimal('2.20000000'):
121  vout = outpoint
122  break
123 
124  bal = self.nodes[0].getbalance()
125  inputs = [{ "txid" : txId, "vout" : vout['n'], "scriptPubKey" : vout['scriptPubKey']['hex']}]
126  outputs = { self.nodes[0].getnewaddress() : 2.19 }
127  rawTx = self.nodes[2].createrawtransaction(inputs, outputs)
128  rawTxPartialSigned = self.nodes[1].signrawtransaction(rawTx, inputs)
129  assert_equal(rawTxPartialSigned['complete'], False) #node1 only has one key, can't comp. sign the tx
130 
131  rawTxSigned = self.nodes[2].signrawtransaction(rawTx, inputs)
132  assert_equal(rawTxSigned['complete'], True) #node2 can sign the tx compl., own two of three keys
133  self.nodes[2].sendrawtransaction(rawTxSigned['hex'])
134  rawTx = self.nodes[0].decoderawtransaction(rawTxSigned['hex'])
135  self.sync_all()
136  self.nodes[0].generate(1)
137  self.sync_all()
138  assert_equal(self.nodes[0].getbalance(), bal+Decimal('500.00000000')+Decimal('2.19000000')) #block reward + tx
139 
140 if __name__ == '__main__':
UniValue addmultisigaddress(const UniValue &params, bool fHelp)
Definition: rpcwallet.cpp:1151
UniValue validateaddress(const UniValue &params, bool fHelp)
Definition: misc.cpp:270
UniValue getbalance(const UniValue &params, bool fHelp)
Definition: rpcwallet.cpp:792
def start_nodes(num_nodes, dirname, extra_args=None, rpchost=None, binary=None)
Definition: util.py:305
UniValue sendtoaddress(const UniValue &params, bool fHelp)
Definition: rpcwallet.cpp:409
UniValue getnewaddress(const UniValue &params, bool fHelp)
Definition: rpcwallet.cpp:113
def initialize_chain_clean(test_dir, num_nodes)
Definition: util.py:252
UniValue signrawtransaction(const UniValue &params, bool fHelp)
UniValue createrawtransaction(const UniValue &params, bool fHelp)
UniValue decoderawtransaction(const UniValue &params, bool fHelp)
def setup_network(self, split=False)
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
def connect_nodes_bi(nodes, a, b)
Definition: util.py:351
UniValue gettransaction(const UniValue &params, bool fHelp)
Definition: rpcwallet.cpp:1821