[tests] don't override __init__() in individual tests
[bitcoinplatinum.git] / test / functional / rawtransactions.py
blobc53dc46282fcd27d6ac6bf57ab3efec868d865ad
1 #!/usr/bin/env python3
2 # Copyright (c) 2014-2016 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 """Test the rawtransaction RPCs.
7 Test the following RPCs:
8 - createrawtransaction
9 - signrawtransaction
10 - sendrawtransaction
11 - decoderawtransaction
12 - getrawtransaction
13 """
15 from test_framework.test_framework import BitcoinTestFramework
16 from test_framework.util import *
18 # Create one-input, one-output, no-fee transaction:
19 class RawTransactionsTest(BitcoinTestFramework):
20 def set_test_params(self):
21 self.setup_clean_chain = True
22 self.num_nodes = 3
24 def setup_network(self, split=False):
25 super().setup_network()
26 connect_nodes_bi(self.nodes,0,2)
28 def run_test(self):
30 #prepare some coins for multiple *rawtransaction commands
31 self.nodes[2].generate(1)
32 self.sync_all()
33 self.nodes[0].generate(101)
34 self.sync_all()
35 self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(),1.5)
36 self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(),1.0)
37 self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(),5.0)
38 self.sync_all()
39 self.nodes[0].generate(5)
40 self.sync_all()
42 #########################################
43 # sendrawtransaction with missing input #
44 #########################################
45 inputs = [ {'txid' : "1d1d4e24ed99057e84c3f80fd8fbec79ed9e1acee37da269356ecea000000000", 'vout' : 1}] #won't exists
46 outputs = { self.nodes[0].getnewaddress() : 4.998 }
47 rawtx = self.nodes[2].createrawtransaction(inputs, outputs)
48 rawtx = self.nodes[2].signrawtransaction(rawtx)
50 # This will raise an exception since there are missing inputs
51 assert_raises_jsonrpc(-25, "Missing inputs", self.nodes[2].sendrawtransaction, rawtx['hex'])
53 #########################
54 # RAW TX MULTISIG TESTS #
55 #########################
56 # 2of2 test
57 addr1 = self.nodes[2].getnewaddress()
58 addr2 = self.nodes[2].getnewaddress()
60 addr1Obj = self.nodes[2].validateaddress(addr1)
61 addr2Obj = self.nodes[2].validateaddress(addr2)
63 mSigObj = self.nodes[2].addmultisigaddress(2, [addr1Obj['pubkey'], addr2Obj['pubkey']])
64 mSigObjValid = self.nodes[2].validateaddress(mSigObj)
66 #use balance deltas instead of absolute values
67 bal = self.nodes[2].getbalance()
69 # send 1.2 BTC to msig adr
70 txId = self.nodes[0].sendtoaddress(mSigObj, 1.2)
71 self.sync_all()
72 self.nodes[0].generate(1)
73 self.sync_all()
74 assert_equal(self.nodes[2].getbalance(), bal+Decimal('1.20000000')) #node2 has both keys of the 2of2 ms addr., tx should affect the balance
77 # 2of3 test from different nodes
78 bal = self.nodes[2].getbalance()
79 addr1 = self.nodes[1].getnewaddress()
80 addr2 = self.nodes[2].getnewaddress()
81 addr3 = self.nodes[2].getnewaddress()
83 addr1Obj = self.nodes[1].validateaddress(addr1)
84 addr2Obj = self.nodes[2].validateaddress(addr2)
85 addr3Obj = self.nodes[2].validateaddress(addr3)
87 mSigObj = self.nodes[2].addmultisigaddress(2, [addr1Obj['pubkey'], addr2Obj['pubkey'], addr3Obj['pubkey']])
88 mSigObjValid = self.nodes[2].validateaddress(mSigObj)
90 txId = self.nodes[0].sendtoaddress(mSigObj, 2.2)
91 decTx = self.nodes[0].gettransaction(txId)
92 rawTx = self.nodes[0].decoderawtransaction(decTx['hex'])
93 self.sync_all()
94 self.nodes[0].generate(1)
95 self.sync_all()
97 #THIS IS A INCOMPLETE FEATURE
98 #NODE2 HAS TWO OF THREE KEY AND THE FUNDS SHOULD BE SPENDABLE AND COUNT AT BALANCE CALCULATION
99 assert_equal(self.nodes[2].getbalance(), bal) #for now, assume the funds of a 2of3 multisig tx are not marked as spendable
101 txDetails = self.nodes[0].gettransaction(txId, True)
102 rawTx = self.nodes[0].decoderawtransaction(txDetails['hex'])
103 vout = False
104 for outpoint in rawTx['vout']:
105 if outpoint['value'] == Decimal('2.20000000'):
106 vout = outpoint
107 break
109 bal = self.nodes[0].getbalance()
110 inputs = [{ "txid" : txId, "vout" : vout['n'], "scriptPubKey" : vout['scriptPubKey']['hex']}]
111 outputs = { self.nodes[0].getnewaddress() : 2.19 }
112 rawTx = self.nodes[2].createrawtransaction(inputs, outputs)
113 rawTxPartialSigned = self.nodes[1].signrawtransaction(rawTx, inputs)
114 assert_equal(rawTxPartialSigned['complete'], False) #node1 only has one key, can't comp. sign the tx
116 rawTxSigned = self.nodes[2].signrawtransaction(rawTx, inputs)
117 assert_equal(rawTxSigned['complete'], True) #node2 can sign the tx compl., own two of three keys
118 self.nodes[2].sendrawtransaction(rawTxSigned['hex'])
119 rawTx = self.nodes[0].decoderawtransaction(rawTxSigned['hex'])
120 self.sync_all()
121 self.nodes[0].generate(1)
122 self.sync_all()
123 assert_equal(self.nodes[0].getbalance(), bal+Decimal('50.00000000')+Decimal('2.19000000')) #block reward + tx
125 # 2of2 test for combining transactions
126 bal = self.nodes[2].getbalance()
127 addr1 = self.nodes[1].getnewaddress()
128 addr2 = self.nodes[2].getnewaddress()
130 addr1Obj = self.nodes[1].validateaddress(addr1)
131 addr2Obj = self.nodes[2].validateaddress(addr2)
133 self.nodes[1].addmultisigaddress(2, [addr1Obj['pubkey'], addr2Obj['pubkey']])
134 mSigObj = self.nodes[2].addmultisigaddress(2, [addr1Obj['pubkey'], addr2Obj['pubkey']])
135 mSigObjValid = self.nodes[2].validateaddress(mSigObj)
137 txId = self.nodes[0].sendtoaddress(mSigObj, 2.2)
138 decTx = self.nodes[0].gettransaction(txId)
139 rawTx2 = self.nodes[0].decoderawtransaction(decTx['hex'])
140 self.sync_all()
141 self.nodes[0].generate(1)
142 self.sync_all()
144 assert_equal(self.nodes[2].getbalance(), bal) # the funds of a 2of2 multisig tx should not be marked as spendable
146 txDetails = self.nodes[0].gettransaction(txId, True)
147 rawTx2 = self.nodes[0].decoderawtransaction(txDetails['hex'])
148 vout = False
149 for outpoint in rawTx2['vout']:
150 if outpoint['value'] == Decimal('2.20000000'):
151 vout = outpoint
152 break
154 bal = self.nodes[0].getbalance()
155 inputs = [{ "txid" : txId, "vout" : vout['n'], "scriptPubKey" : vout['scriptPubKey']['hex'], "redeemScript" : mSigObjValid['hex']}]
156 outputs = { self.nodes[0].getnewaddress() : 2.19 }
157 rawTx2 = self.nodes[2].createrawtransaction(inputs, outputs)
158 rawTxPartialSigned1 = self.nodes[1].signrawtransaction(rawTx2, inputs)
159 self.log.info(rawTxPartialSigned1)
160 assert_equal(rawTxPartialSigned['complete'], False) #node1 only has one key, can't comp. sign the tx
162 rawTxPartialSigned2 = self.nodes[2].signrawtransaction(rawTx2, inputs)
163 self.log.info(rawTxPartialSigned2)
164 assert_equal(rawTxPartialSigned2['complete'], False) #node2 only has one key, can't comp. sign the tx
165 rawTxComb = self.nodes[2].combinerawtransaction([rawTxPartialSigned1['hex'], rawTxPartialSigned2['hex']])
166 self.log.info(rawTxComb)
167 self.nodes[2].sendrawtransaction(rawTxComb)
168 rawTx2 = self.nodes[0].decoderawtransaction(rawTxComb)
169 self.sync_all()
170 self.nodes[0].generate(1)
171 self.sync_all()
172 assert_equal(self.nodes[0].getbalance(), bal+Decimal('50.00000000')+Decimal('2.19000000')) #block reward + tx
174 # getrawtransaction tests
175 # 1. valid parameters - only supply txid
176 txHash = rawTx["hash"]
177 assert_equal(self.nodes[0].getrawtransaction(txHash), rawTxSigned['hex'])
179 # 2. valid parameters - supply txid and 0 for non-verbose
180 assert_equal(self.nodes[0].getrawtransaction(txHash, 0), rawTxSigned['hex'])
182 # 3. valid parameters - supply txid and False for non-verbose
183 assert_equal(self.nodes[0].getrawtransaction(txHash, False), rawTxSigned['hex'])
185 # 4. valid parameters - supply txid and 1 for verbose.
186 # We only check the "hex" field of the output so we don't need to update this test every time the output format changes.
187 assert_equal(self.nodes[0].getrawtransaction(txHash, 1)["hex"], rawTxSigned['hex'])
189 # 5. valid parameters - supply txid and True for non-verbose
190 assert_equal(self.nodes[0].getrawtransaction(txHash, True)["hex"], rawTxSigned['hex'])
192 # 6. invalid parameters - supply txid and string "Flase"
193 assert_raises_jsonrpc(-3,"Invalid type", self.nodes[0].getrawtransaction, txHash, "Flase")
195 # 7. invalid parameters - supply txid and empty array
196 assert_raises_jsonrpc(-3,"Invalid type", self.nodes[0].getrawtransaction, txHash, [])
198 # 8. invalid parameters - supply txid and empty dict
199 assert_raises_jsonrpc(-3,"Invalid type", self.nodes[0].getrawtransaction, txHash, {})
201 inputs = [ {'txid' : "1d1d4e24ed99057e84c3f80fd8fbec79ed9e1acee37da269356ecea000000000", 'vout' : 1, 'sequence' : 1000}]
202 outputs = { self.nodes[0].getnewaddress() : 1 }
203 rawtx = self.nodes[0].createrawtransaction(inputs, outputs)
204 decrawtx= self.nodes[0].decoderawtransaction(rawtx)
205 assert_equal(decrawtx['vin'][0]['sequence'], 1000)
207 # 9. invalid parameters - sequence number out of range
208 inputs = [ {'txid' : "1d1d4e24ed99057e84c3f80fd8fbec79ed9e1acee37da269356ecea000000000", 'vout' : 1, 'sequence' : -1}]
209 outputs = { self.nodes[0].getnewaddress() : 1 }
210 assert_raises_jsonrpc(-8, 'Invalid parameter, sequence number is out of range', self.nodes[0].createrawtransaction, inputs, outputs)
212 # 10. invalid parameters - sequence number out of range
213 inputs = [ {'txid' : "1d1d4e24ed99057e84c3f80fd8fbec79ed9e1acee37da269356ecea000000000", 'vout' : 1, 'sequence' : 4294967296}]
214 outputs = { self.nodes[0].getnewaddress() : 1 }
215 assert_raises_jsonrpc(-8, 'Invalid parameter, sequence number is out of range', self.nodes[0].createrawtransaction, inputs, outputs)
217 inputs = [ {'txid' : "1d1d4e24ed99057e84c3f80fd8fbec79ed9e1acee37da269356ecea000000000", 'vout' : 1, 'sequence' : 4294967294}]
218 outputs = { self.nodes[0].getnewaddress() : 1 }
219 rawtx = self.nodes[0].createrawtransaction(inputs, outputs)
220 decrawtx= self.nodes[0].decoderawtransaction(rawtx)
221 assert_equal(decrawtx['vin'][0]['sequence'], 4294967294)
223 if __name__ == '__main__':
224 RawTransactionsTest().main()