Merge #11121: TestNode tidyups
[bitcoinplatinum.git] / test / functional / rawtransactions.py
blobd7255daa0a67a482966807ce1dae3f03fd996dac
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']])
65 #use balance deltas instead of absolute values
66 bal = self.nodes[2].getbalance()
68 # send 1.2 BTC to msig adr
69 txId = self.nodes[0].sendtoaddress(mSigObj, 1.2)
70 self.sync_all()
71 self.nodes[0].generate(1)
72 self.sync_all()
73 assert_equal(self.nodes[2].getbalance(), bal+Decimal('1.20000000')) #node2 has both keys of the 2of2 ms addr., tx should affect the balance
76 # 2of3 test from different nodes
77 bal = self.nodes[2].getbalance()
78 addr1 = self.nodes[1].getnewaddress()
79 addr2 = self.nodes[2].getnewaddress()
80 addr3 = self.nodes[2].getnewaddress()
82 addr1Obj = self.nodes[1].validateaddress(addr1)
83 addr2Obj = self.nodes[2].validateaddress(addr2)
84 addr3Obj = self.nodes[2].validateaddress(addr3)
86 mSigObj = self.nodes[2].addmultisigaddress(2, [addr1Obj['pubkey'], addr2Obj['pubkey'], addr3Obj['pubkey']])
88 txId = self.nodes[0].sendtoaddress(mSigObj, 2.2)
89 decTx = self.nodes[0].gettransaction(txId)
90 rawTx = self.nodes[0].decoderawtransaction(decTx['hex'])
91 self.sync_all()
92 self.nodes[0].generate(1)
93 self.sync_all()
95 #THIS IS A INCOMPLETE FEATURE
96 #NODE2 HAS TWO OF THREE KEY AND THE FUNDS SHOULD BE SPENDABLE AND COUNT AT BALANCE CALCULATION
97 assert_equal(self.nodes[2].getbalance(), bal) #for now, assume the funds of a 2of3 multisig tx are not marked as spendable
99 txDetails = self.nodes[0].gettransaction(txId, True)
100 rawTx = self.nodes[0].decoderawtransaction(txDetails['hex'])
101 vout = False
102 for outpoint in rawTx['vout']:
103 if outpoint['value'] == Decimal('2.20000000'):
104 vout = outpoint
105 break
107 bal = self.nodes[0].getbalance()
108 inputs = [{ "txid" : txId, "vout" : vout['n'], "scriptPubKey" : vout['scriptPubKey']['hex']}]
109 outputs = { self.nodes[0].getnewaddress() : 2.19 }
110 rawTx = self.nodes[2].createrawtransaction(inputs, outputs)
111 rawTxPartialSigned = self.nodes[1].signrawtransaction(rawTx, inputs)
112 assert_equal(rawTxPartialSigned['complete'], False) #node1 only has one key, can't comp. sign the tx
114 rawTxSigned = self.nodes[2].signrawtransaction(rawTx, inputs)
115 assert_equal(rawTxSigned['complete'], True) #node2 can sign the tx compl., own two of three keys
116 self.nodes[2].sendrawtransaction(rawTxSigned['hex'])
117 rawTx = self.nodes[0].decoderawtransaction(rawTxSigned['hex'])
118 self.sync_all()
119 self.nodes[0].generate(1)
120 self.sync_all()
121 assert_equal(self.nodes[0].getbalance(), bal+Decimal('50.00000000')+Decimal('2.19000000')) #block reward + tx
123 # 2of2 test for combining transactions
124 bal = self.nodes[2].getbalance()
125 addr1 = self.nodes[1].getnewaddress()
126 addr2 = self.nodes[2].getnewaddress()
128 addr1Obj = self.nodes[1].validateaddress(addr1)
129 addr2Obj = self.nodes[2].validateaddress(addr2)
131 self.nodes[1].addmultisigaddress(2, [addr1Obj['pubkey'], addr2Obj['pubkey']])
132 mSigObj = self.nodes[2].addmultisigaddress(2, [addr1Obj['pubkey'], addr2Obj['pubkey']])
133 mSigObjValid = self.nodes[2].validateaddress(mSigObj)
135 txId = self.nodes[0].sendtoaddress(mSigObj, 2.2)
136 decTx = self.nodes[0].gettransaction(txId)
137 rawTx2 = self.nodes[0].decoderawtransaction(decTx['hex'])
138 self.sync_all()
139 self.nodes[0].generate(1)
140 self.sync_all()
142 assert_equal(self.nodes[2].getbalance(), bal) # the funds of a 2of2 multisig tx should not be marked as spendable
144 txDetails = self.nodes[0].gettransaction(txId, True)
145 rawTx2 = self.nodes[0].decoderawtransaction(txDetails['hex'])
146 vout = False
147 for outpoint in rawTx2['vout']:
148 if outpoint['value'] == Decimal('2.20000000'):
149 vout = outpoint
150 break
152 bal = self.nodes[0].getbalance()
153 inputs = [{ "txid" : txId, "vout" : vout['n'], "scriptPubKey" : vout['scriptPubKey']['hex'], "redeemScript" : mSigObjValid['hex']}]
154 outputs = { self.nodes[0].getnewaddress() : 2.19 }
155 rawTx2 = self.nodes[2].createrawtransaction(inputs, outputs)
156 rawTxPartialSigned1 = self.nodes[1].signrawtransaction(rawTx2, inputs)
157 self.log.info(rawTxPartialSigned1)
158 assert_equal(rawTxPartialSigned['complete'], False) #node1 only has one key, can't comp. sign the tx
160 rawTxPartialSigned2 = self.nodes[2].signrawtransaction(rawTx2, inputs)
161 self.log.info(rawTxPartialSigned2)
162 assert_equal(rawTxPartialSigned2['complete'], False) #node2 only has one key, can't comp. sign the tx
163 rawTxComb = self.nodes[2].combinerawtransaction([rawTxPartialSigned1['hex'], rawTxPartialSigned2['hex']])
164 self.log.info(rawTxComb)
165 self.nodes[2].sendrawtransaction(rawTxComb)
166 rawTx2 = self.nodes[0].decoderawtransaction(rawTxComb)
167 self.sync_all()
168 self.nodes[0].generate(1)
169 self.sync_all()
170 assert_equal(self.nodes[0].getbalance(), bal+Decimal('50.00000000')+Decimal('2.19000000')) #block reward + tx
172 # getrawtransaction tests
173 # 1. valid parameters - only supply txid
174 txHash = rawTx["hash"]
175 assert_equal(self.nodes[0].getrawtransaction(txHash), rawTxSigned['hex'])
177 # 2. valid parameters - supply txid and 0 for non-verbose
178 assert_equal(self.nodes[0].getrawtransaction(txHash, 0), rawTxSigned['hex'])
180 # 3. valid parameters - supply txid and False for non-verbose
181 assert_equal(self.nodes[0].getrawtransaction(txHash, False), rawTxSigned['hex'])
183 # 4. valid parameters - supply txid and 1 for verbose.
184 # We only check the "hex" field of the output so we don't need to update this test every time the output format changes.
185 assert_equal(self.nodes[0].getrawtransaction(txHash, 1)["hex"], rawTxSigned['hex'])
187 # 5. valid parameters - supply txid and True for non-verbose
188 assert_equal(self.nodes[0].getrawtransaction(txHash, True)["hex"], rawTxSigned['hex'])
190 # 6. invalid parameters - supply txid and string "Flase"
191 assert_raises_jsonrpc(-3,"Invalid type", self.nodes[0].getrawtransaction, txHash, "Flase")
193 # 7. invalid parameters - supply txid and empty array
194 assert_raises_jsonrpc(-3,"Invalid type", self.nodes[0].getrawtransaction, txHash, [])
196 # 8. invalid parameters - supply txid and empty dict
197 assert_raises_jsonrpc(-3,"Invalid type", self.nodes[0].getrawtransaction, txHash, {})
199 inputs = [ {'txid' : "1d1d4e24ed99057e84c3f80fd8fbec79ed9e1acee37da269356ecea000000000", 'vout' : 1, 'sequence' : 1000}]
200 outputs = { self.nodes[0].getnewaddress() : 1 }
201 rawtx = self.nodes[0].createrawtransaction(inputs, outputs)
202 decrawtx= self.nodes[0].decoderawtransaction(rawtx)
203 assert_equal(decrawtx['vin'][0]['sequence'], 1000)
205 # 9. invalid parameters - sequence number out of range
206 inputs = [ {'txid' : "1d1d4e24ed99057e84c3f80fd8fbec79ed9e1acee37da269356ecea000000000", 'vout' : 1, 'sequence' : -1}]
207 outputs = { self.nodes[0].getnewaddress() : 1 }
208 assert_raises_jsonrpc(-8, 'Invalid parameter, sequence number is out of range', self.nodes[0].createrawtransaction, inputs, outputs)
210 # 10. invalid parameters - sequence number out of range
211 inputs = [ {'txid' : "1d1d4e24ed99057e84c3f80fd8fbec79ed9e1acee37da269356ecea000000000", 'vout' : 1, 'sequence' : 4294967296}]
212 outputs = { self.nodes[0].getnewaddress() : 1 }
213 assert_raises_jsonrpc(-8, 'Invalid parameter, sequence number is out of range', self.nodes[0].createrawtransaction, inputs, outputs)
215 inputs = [ {'txid' : "1d1d4e24ed99057e84c3f80fd8fbec79ed9e1acee37da269356ecea000000000", 'vout' : 1, 'sequence' : 4294967294}]
216 outputs = { self.nodes[0].getnewaddress() : 1 }
217 rawtx = self.nodes[0].createrawtransaction(inputs, outputs)
218 decrawtx= self.nodes[0].decoderawtransaction(rawtx)
219 assert_equal(decrawtx['vin'][0]['sequence'], 4294967294)
221 if __name__ == '__main__':
222 RawTransactionsTest().main()