2 # Copyright (c) 2014-2017 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 *
19 class multidict(dict):
20 """Dictionary that allows duplicate keys.
22 Constructed with a list of (key, value) tuples. When dumped by the json module,
23 will output invalid json with repeated keys, eg:
24 >>> json.dumps(multidict([(1,2),(1,2)])
27 Used to test calls to rpc methods with repeated keys in the json object."""
29 def __init__(self
, x
):
30 dict.__init
__(self
, x
)
37 # Create one-input, one-output, no-fee transaction:
38 class RawTransactionsTest(BitcoinTestFramework
):
39 def set_test_params(self
):
40 self
.setup_clean_chain
= True
43 def setup_network(self
, split
=False):
44 super().setup_network()
45 connect_nodes_bi(self
.nodes
,0,2)
49 #prepare some coins for multiple *rawtransaction commands
50 self
.nodes
[2].generate(1)
52 self
.nodes
[0].generate(101)
54 self
.nodes
[0].sendtoaddress(self
.nodes
[2].getnewaddress(),1.5)
55 self
.nodes
[0].sendtoaddress(self
.nodes
[2].getnewaddress(),1.0)
56 self
.nodes
[0].sendtoaddress(self
.nodes
[2].getnewaddress(),5.0)
58 self
.nodes
[0].generate(5)
61 # Test `createrawtransaction` required parameters
62 assert_raises_rpc_error(-1, "createrawtransaction", self
.nodes
[0].createrawtransaction
)
63 assert_raises_rpc_error(-1, "createrawtransaction", self
.nodes
[0].createrawtransaction
, [])
65 # Test `createrawtransaction` invalid extra parameters
66 assert_raises_rpc_error(-1, "createrawtransaction", self
.nodes
[0].createrawtransaction
, [], {}, 0, False, 'foo')
68 # Test `createrawtransaction` invalid `inputs`
69 txid
= '1d1d4e24ed99057e84c3f80fd8fbec79ed9e1acee37da269356ecea000000000'
70 assert_raises_rpc_error(-3, "Expected type array", self
.nodes
[0].createrawtransaction
, 'foo', {})
71 assert_raises_rpc_error(-1, "JSON value is not an object as expected", self
.nodes
[0].createrawtransaction
, ['foo'], {})
72 assert_raises_rpc_error(-8, "txid must be hexadecimal string", self
.nodes
[0].createrawtransaction
, [{}], {})
73 assert_raises_rpc_error(-8, "txid must be hexadecimal string", self
.nodes
[0].createrawtransaction
, [{'txid': 'foo'}], {})
74 assert_raises_rpc_error(-8, "Invalid parameter, missing vout key", self
.nodes
[0].createrawtransaction
, [{'txid': txid
}], {})
75 assert_raises_rpc_error(-8, "Invalid parameter, missing vout key", self
.nodes
[0].createrawtransaction
, [{'txid': txid
, 'vout': 'foo'}], {})
76 assert_raises_rpc_error(-8, "Invalid parameter, vout must be positive", self
.nodes
[0].createrawtransaction
, [{'txid': txid
, 'vout': -1}], {})
77 assert_raises_rpc_error(-8, "Invalid parameter, sequence number is out of range", self
.nodes
[0].createrawtransaction
, [{'txid': txid
, 'vout': 0, 'sequence': -1}], {})
79 # Test `createrawtransaction` invalid `outputs`
80 address
= self
.nodes
[0].getnewaddress()
81 assert_raises_rpc_error(-3, "Expected type object", self
.nodes
[0].createrawtransaction
, [], 'foo')
82 assert_raises_rpc_error(-8, "Data must be hexadecimal string", self
.nodes
[0].createrawtransaction
, [], {'data': 'foo'})
83 assert_raises_rpc_error(-5, "Invalid Bitcoin address", self
.nodes
[0].createrawtransaction
, [], {'foo': 0})
84 assert_raises_rpc_error(-3, "Invalid amount", self
.nodes
[0].createrawtransaction
, [], {address
: 'foo'})
85 assert_raises_rpc_error(-3, "Amount out of range", self
.nodes
[0].createrawtransaction
, [], {address
: -1})
86 assert_raises_rpc_error(-8, "Invalid parameter, duplicated address: %s" % address
, self
.nodes
[0].createrawtransaction
, [], multidict([(address
, 1), (address
, 1)]))
88 # Test `createrawtransaction` invalid `locktime`
89 assert_raises_rpc_error(-3, "Expected type number", self
.nodes
[0].createrawtransaction
, [], {}, 'foo')
90 assert_raises_rpc_error(-8, "Invalid parameter, locktime out of range", self
.nodes
[0].createrawtransaction
, [], {}, -1)
91 assert_raises_rpc_error(-8, "Invalid parameter, locktime out of range", self
.nodes
[0].createrawtransaction
, [], {}, 4294967296)
93 # Test `createrawtransaction` invalid `replaceable`
94 assert_raises_rpc_error(-3, "Expected type bool", self
.nodes
[0].createrawtransaction
, [], {}, 0, 'foo')
96 #########################################
97 # sendrawtransaction with missing input #
98 #########################################
99 inputs
= [ {'txid' : "1d1d4e24ed99057e84c3f80fd8fbec79ed9e1acee37da269356ecea000000000", 'vout' : 1}] #won't exists
100 outputs
= { self
.nodes
[0].getnewaddress() : 4.998 }
101 rawtx
= self
.nodes
[2].createrawtransaction(inputs
, outputs
)
102 rawtx
= self
.nodes
[2].signrawtransaction(rawtx
)
104 # This will raise an exception since there are missing inputs
105 assert_raises_rpc_error(-25, "Missing inputs", self
.nodes
[2].sendrawtransaction
, rawtx
['hex'])
107 #####################################
108 # getrawtransaction with block hash #
109 #####################################
111 # make a tx by sending then generate 2 blocks; block1 has the tx in it
112 tx
= self
.nodes
[2].sendtoaddress(self
.nodes
[1].getnewaddress(), 1)
113 block1
, block2
= self
.nodes
[2].generate(2)
115 # We should be able to get the raw transaction by providing the correct block
116 gottx
= self
.nodes
[0].getrawtransaction(tx
, True, block1
)
117 assert_equal(gottx
['txid'], tx
)
118 assert_equal(gottx
['in_active_chain'], True)
119 # We should not have the 'in_active_chain' flag when we don't provide a block
120 gottx
= self
.nodes
[0].getrawtransaction(tx
, True)
121 assert_equal(gottx
['txid'], tx
)
122 assert 'in_active_chain' not in gottx
123 # We should not get the tx if we provide an unrelated block
124 assert_raises_rpc_error(-5, "No such transaction found", self
.nodes
[0].getrawtransaction
, tx
, True, block2
)
125 # An invalid block hash should raise the correct errors
126 assert_raises_rpc_error(-8, "parameter 3 must be hexadecimal", self
.nodes
[0].getrawtransaction
, tx
, True, True)
127 assert_raises_rpc_error(-8, "parameter 3 must be hexadecimal", self
.nodes
[0].getrawtransaction
, tx
, True, "foobar")
128 assert_raises_rpc_error(-8, "parameter 3 must be of length 64", self
.nodes
[0].getrawtransaction
, tx
, True, "abcd1234")
129 assert_raises_rpc_error(-5, "Block hash not found", self
.nodes
[0].getrawtransaction
, tx
, True, "0000000000000000000000000000000000000000000000000000000000000000")
130 # Undo the blocks and check in_active_chain
131 self
.nodes
[0].invalidateblock(block1
)
132 gottx
= self
.nodes
[0].getrawtransaction(txid
=tx
, verbose
=True, blockhash
=block1
)
133 assert_equal(gottx
['in_active_chain'], False)
134 self
.nodes
[0].reconsiderblock(block1
)
135 assert_equal(self
.nodes
[0].getbestblockhash(), block2
)
137 #########################
138 # RAW TX MULTISIG TESTS #
139 #########################
141 addr1
= self
.nodes
[2].getnewaddress()
142 addr2
= self
.nodes
[2].getnewaddress()
144 addr1Obj
= self
.nodes
[2].validateaddress(addr1
)
145 addr2Obj
= self
.nodes
[2].validateaddress(addr2
)
147 mSigObj
= self
.nodes
[2].addmultisigaddress(2, [addr1Obj
['pubkey'], addr2Obj
['pubkey']])
149 #use balance deltas instead of absolute values
150 bal
= self
.nodes
[2].getbalance()
152 # send 1.2 BTC to msig adr
153 txId
= self
.nodes
[0].sendtoaddress(mSigObj
, 1.2)
155 self
.nodes
[0].generate(1)
157 assert_equal(self
.nodes
[2].getbalance(), bal
+Decimal('1.20000000')) #node2 has both keys of the 2of2 ms addr., tx should affect the balance
160 # 2of3 test from different nodes
161 bal
= self
.nodes
[2].getbalance()
162 addr1
= self
.nodes
[1].getnewaddress()
163 addr2
= self
.nodes
[2].getnewaddress()
164 addr3
= self
.nodes
[2].getnewaddress()
166 addr1Obj
= self
.nodes
[1].validateaddress(addr1
)
167 addr2Obj
= self
.nodes
[2].validateaddress(addr2
)
168 addr3Obj
= self
.nodes
[2].validateaddress(addr3
)
170 mSigObj
= self
.nodes
[2].addmultisigaddress(2, [addr1Obj
['pubkey'], addr2Obj
['pubkey'], addr3Obj
['pubkey']])
172 txId
= self
.nodes
[0].sendtoaddress(mSigObj
, 2.2)
173 decTx
= self
.nodes
[0].gettransaction(txId
)
174 rawTx
= self
.nodes
[0].decoderawtransaction(decTx
['hex'])
176 self
.nodes
[0].generate(1)
179 #THIS IS A INCOMPLETE FEATURE
180 #NODE2 HAS TWO OF THREE KEY AND THE FUNDS SHOULD BE SPENDABLE AND COUNT AT BALANCE CALCULATION
181 assert_equal(self
.nodes
[2].getbalance(), bal
) #for now, assume the funds of a 2of3 multisig tx are not marked as spendable
183 txDetails
= self
.nodes
[0].gettransaction(txId
, True)
184 rawTx
= self
.nodes
[0].decoderawtransaction(txDetails
['hex'])
186 for outpoint
in rawTx
['vout']:
187 if outpoint
['value'] == Decimal('2.20000000'):
191 bal
= self
.nodes
[0].getbalance()
192 inputs
= [{ "txid" : txId
, "vout" : vout
['n'], "scriptPubKey" : vout
['scriptPubKey']['hex']}]
193 outputs
= { self
.nodes
[0].getnewaddress() : 2.19 }
194 rawTx
= self
.nodes
[2].createrawtransaction(inputs
, outputs
)
195 rawTxPartialSigned
= self
.nodes
[1].signrawtransaction(rawTx
, inputs
)
196 assert_equal(rawTxPartialSigned
['complete'], False) #node1 only has one key, can't comp. sign the tx
198 rawTxSigned
= self
.nodes
[2].signrawtransaction(rawTx
, inputs
)
199 assert_equal(rawTxSigned
['complete'], True) #node2 can sign the tx compl., own two of three keys
200 self
.nodes
[2].sendrawtransaction(rawTxSigned
['hex'])
201 rawTx
= self
.nodes
[0].decoderawtransaction(rawTxSigned
['hex'])
203 self
.nodes
[0].generate(1)
205 assert_equal(self
.nodes
[0].getbalance(), bal
+Decimal('50.00000000')+Decimal('2.19000000')) #block reward + tx
207 # 2of2 test for combining transactions
208 bal
= self
.nodes
[2].getbalance()
209 addr1
= self
.nodes
[1].getnewaddress()
210 addr2
= self
.nodes
[2].getnewaddress()
212 addr1Obj
= self
.nodes
[1].validateaddress(addr1
)
213 addr2Obj
= self
.nodes
[2].validateaddress(addr2
)
215 self
.nodes
[1].addmultisigaddress(2, [addr1Obj
['pubkey'], addr2Obj
['pubkey']])
216 mSigObj
= self
.nodes
[2].addmultisigaddress(2, [addr1Obj
['pubkey'], addr2Obj
['pubkey']])
217 mSigObjValid
= self
.nodes
[2].validateaddress(mSigObj
)
219 txId
= self
.nodes
[0].sendtoaddress(mSigObj
, 2.2)
220 decTx
= self
.nodes
[0].gettransaction(txId
)
221 rawTx2
= self
.nodes
[0].decoderawtransaction(decTx
['hex'])
223 self
.nodes
[0].generate(1)
226 assert_equal(self
.nodes
[2].getbalance(), bal
) # the funds of a 2of2 multisig tx should not be marked as spendable
228 txDetails
= self
.nodes
[0].gettransaction(txId
, True)
229 rawTx2
= self
.nodes
[0].decoderawtransaction(txDetails
['hex'])
231 for outpoint
in rawTx2
['vout']:
232 if outpoint
['value'] == Decimal('2.20000000'):
236 bal
= self
.nodes
[0].getbalance()
237 inputs
= [{ "txid" : txId
, "vout" : vout
['n'], "scriptPubKey" : vout
['scriptPubKey']['hex'], "redeemScript" : mSigObjValid
['hex']}]
238 outputs
= { self
.nodes
[0].getnewaddress() : 2.19 }
239 rawTx2
= self
.nodes
[2].createrawtransaction(inputs
, outputs
)
240 rawTxPartialSigned1
= self
.nodes
[1].signrawtransaction(rawTx2
, inputs
)
241 self
.log
.info(rawTxPartialSigned1
)
242 assert_equal(rawTxPartialSigned
['complete'], False) #node1 only has one key, can't comp. sign the tx
244 rawTxPartialSigned2
= self
.nodes
[2].signrawtransaction(rawTx2
, inputs
)
245 self
.log
.info(rawTxPartialSigned2
)
246 assert_equal(rawTxPartialSigned2
['complete'], False) #node2 only has one key, can't comp. sign the tx
247 rawTxComb
= self
.nodes
[2].combinerawtransaction([rawTxPartialSigned1
['hex'], rawTxPartialSigned2
['hex']])
248 self
.log
.info(rawTxComb
)
249 self
.nodes
[2].sendrawtransaction(rawTxComb
)
250 rawTx2
= self
.nodes
[0].decoderawtransaction(rawTxComb
)
252 self
.nodes
[0].generate(1)
254 assert_equal(self
.nodes
[0].getbalance(), bal
+Decimal('50.00000000')+Decimal('2.19000000')) #block reward + tx
256 # decoderawtransaction tests
257 # witness transaction
258 encrawtx
= "010000000001010000000000000072c1a6a246ae63f74f931e8365e15a089c68d61900000000000000000000ffffffff0100e1f50500000000000000000000"
259 decrawtx
= self
.nodes
[0].decoderawtransaction(encrawtx
, True) # decode as witness transaction
260 assert_equal(decrawtx
['vout'][0]['value'], Decimal('1.00000000'))
261 assert_raises_rpc_error(-22, 'TX decode failed', self
.nodes
[0].decoderawtransaction
, encrawtx
, False) # force decode as non-witness transaction
262 # non-witness transaction
263 encrawtx
= "01000000010000000000000072c1a6a246ae63f74f931e8365e15a089c68d61900000000000000000000ffffffff0100e1f505000000000000000000"
264 decrawtx
= self
.nodes
[0].decoderawtransaction(encrawtx
, False) # decode as non-witness transaction
265 assert_equal(decrawtx
['vout'][0]['value'], Decimal('1.00000000'))
267 # getrawtransaction tests
268 # 1. valid parameters - only supply txid
269 txHash
= rawTx
["hash"]
270 assert_equal(self
.nodes
[0].getrawtransaction(txHash
), rawTxSigned
['hex'])
272 # 2. valid parameters - supply txid and 0 for non-verbose
273 assert_equal(self
.nodes
[0].getrawtransaction(txHash
, 0), rawTxSigned
['hex'])
275 # 3. valid parameters - supply txid and False for non-verbose
276 assert_equal(self
.nodes
[0].getrawtransaction(txHash
, False), rawTxSigned
['hex'])
278 # 4. valid parameters - supply txid and 1 for verbose.
279 # We only check the "hex" field of the output so we don't need to update this test every time the output format changes.
280 assert_equal(self
.nodes
[0].getrawtransaction(txHash
, 1)["hex"], rawTxSigned
['hex'])
282 # 5. valid parameters - supply txid and True for non-verbose
283 assert_equal(self
.nodes
[0].getrawtransaction(txHash
, True)["hex"], rawTxSigned
['hex'])
285 # 6. invalid parameters - supply txid and string "Flase"
286 assert_raises_rpc_error(-1, "not a boolean", self
.nodes
[0].getrawtransaction
, txHash
, "Flase")
288 # 7. invalid parameters - supply txid and empty array
289 assert_raises_rpc_error(-1, "not a boolean", self
.nodes
[0].getrawtransaction
, txHash
, [])
291 # 8. invalid parameters - supply txid and empty dict
292 assert_raises_rpc_error(-1, "not a boolean", self
.nodes
[0].getrawtransaction
, txHash
, {})
294 inputs
= [ {'txid' : "1d1d4e24ed99057e84c3f80fd8fbec79ed9e1acee37da269356ecea000000000", 'vout' : 1, 'sequence' : 1000}]
295 outputs
= { self
.nodes
[0].getnewaddress() : 1 }
296 rawtx
= self
.nodes
[0].createrawtransaction(inputs
, outputs
)
297 decrawtx
= self
.nodes
[0].decoderawtransaction(rawtx
)
298 assert_equal(decrawtx
['vin'][0]['sequence'], 1000)
300 # 9. invalid parameters - sequence number out of range
301 inputs
= [ {'txid' : "1d1d4e24ed99057e84c3f80fd8fbec79ed9e1acee37da269356ecea000000000", 'vout' : 1, 'sequence' : -1}]
302 outputs
= { self
.nodes
[0].getnewaddress() : 1 }
303 assert_raises_rpc_error(-8, 'Invalid parameter, sequence number is out of range', self
.nodes
[0].createrawtransaction
, inputs
, outputs
)
305 # 10. invalid parameters - sequence number out of range
306 inputs
= [ {'txid' : "1d1d4e24ed99057e84c3f80fd8fbec79ed9e1acee37da269356ecea000000000", 'vout' : 1, 'sequence' : 4294967296}]
307 outputs
= { self
.nodes
[0].getnewaddress() : 1 }
308 assert_raises_rpc_error(-8, 'Invalid parameter, sequence number is out of range', self
.nodes
[0].createrawtransaction
, inputs
, outputs
)
310 inputs
= [ {'txid' : "1d1d4e24ed99057e84c3f80fd8fbec79ed9e1acee37da269356ecea000000000", 'vout' : 1, 'sequence' : 4294967294}]
311 outputs
= { self
.nodes
[0].getnewaddress() : 1 }
312 rawtx
= self
.nodes
[0].createrawtransaction(inputs
, outputs
)
313 decrawtx
= self
.nodes
[0].decoderawtransaction(rawtx
)
314 assert_equal(decrawtx
['vin'][0]['sequence'], 4294967294)
316 if __name__
== '__main__':
317 RawTransactionsTest().main()