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:
11 - decoderawtransaction
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
24 def setup_network(self
, split
=False):
25 super().setup_network()
26 connect_nodes_bi(self
.nodes
,0,2)
30 #prepare some coins for multiple *rawtransaction commands
31 self
.nodes
[2].generate(1)
33 self
.nodes
[0].generate(101)
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)
39 self
.nodes
[0].generate(5)
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_rpc_error(-25, "Missing inputs", self
.nodes
[2].sendrawtransaction
, rawtx
['hex'])
53 #########################
54 # RAW TX MULTISIG TESTS #
55 #########################
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)
71 self
.nodes
[0].generate(1)
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'])
92 self
.nodes
[0].generate(1)
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'])
102 for outpoint
in rawTx
['vout']:
103 if outpoint
['value'] == Decimal('2.20000000'):
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'])
119 self
.nodes
[0].generate(1)
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'])
139 self
.nodes
[0].generate(1)
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'])
147 for outpoint
in rawTx2
['vout']:
148 if outpoint
['value'] == Decimal('2.20000000'):
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
)
168 self
.nodes
[0].generate(1)
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_rpc_error(-3,"Invalid type", self
.nodes
[0].getrawtransaction
, txHash
, "Flase")
193 # 7. invalid parameters - supply txid and empty array
194 assert_raises_rpc_error(-3,"Invalid type", self
.nodes
[0].getrawtransaction
, txHash
, [])
196 # 8. invalid parameters - supply txid and empty dict
197 assert_raises_rpc_error(-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_rpc_error(-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_rpc_error(-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()