[tests] Remove unused variables
[bitcoinplatinum.git] / test / functional / fundrawtransaction.py
blob1dc00f2ba1ecb54ece45c48252124a12d4b282c7
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 fundrawtransaction RPC."""
7 from test_framework.test_framework import BitcoinTestFramework
8 from test_framework.util import *
11 def get_unspent(listunspent, amount):
12 for utx in listunspent:
13 if utx['amount'] == amount:
14 return utx
15 raise AssertionError('Could not find unspent with amount={}'.format(amount))
18 class RawTransactionsTest(BitcoinTestFramework):
20 def __init__(self):
21 super().__init__()
22 self.setup_clean_chain = True
23 self.num_nodes = 4
25 def setup_network(self, split=False):
26 self.nodes = start_nodes(self.num_nodes, self.options.tmpdir)
28 connect_nodes_bi(self.nodes,0,1)
29 connect_nodes_bi(self.nodes,1,2)
30 connect_nodes_bi(self.nodes,0,2)
31 connect_nodes_bi(self.nodes,0,3)
33 self.is_network_split=False
34 self.sync_all()
36 def run_test(self):
37 min_relay_tx_fee = self.nodes[0].getnetworkinfo()['relayfee']
38 # This test is not meant to test fee estimation and we'd like
39 # to be sure all txs are sent at a consistent desired feerate
40 for node in self.nodes:
41 node.settxfee(min_relay_tx_fee)
43 # if the fee's positive delta is higher than this value tests will fail,
44 # neg. delta always fail the tests.
45 # The size of the signature of every input may be at most 2 bytes larger
46 # than a minimum sized signature.
48 # = 2 bytes * minRelayTxFeePerByte
49 feeTolerance = 2 * min_relay_tx_fee/1000
51 self.nodes[2].generate(1)
52 self.sync_all()
53 self.nodes[0].generate(121)
54 self.sync_all()
56 watchonly_address = self.nodes[0].getnewaddress()
57 watchonly_pubkey = self.nodes[0].validateaddress(watchonly_address)["pubkey"]
58 watchonly_amount = Decimal(200)
59 self.nodes[3].importpubkey(watchonly_pubkey, "", True)
60 watchonly_txid = self.nodes[0].sendtoaddress(watchonly_address, watchonly_amount)
61 self.nodes[0].sendtoaddress(self.nodes[3].getnewaddress(), watchonly_amount / 10)
63 self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(), 1.5)
64 self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(), 1.0)
65 self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(), 5.0)
67 self.nodes[0].generate(1)
68 self.sync_all()
70 ###############
71 # simple test #
72 ###############
73 inputs = [ ]
74 outputs = { self.nodes[0].getnewaddress() : 1.0 }
75 rawtx = self.nodes[2].createrawtransaction(inputs, outputs)
76 dec_tx = self.nodes[2].decoderawtransaction(rawtx)
77 rawtxfund = self.nodes[2].fundrawtransaction(rawtx)
78 fee = rawtxfund['fee']
79 dec_tx = self.nodes[2].decoderawtransaction(rawtxfund['hex'])
80 assert(len(dec_tx['vin']) > 0) #test that we have enough inputs
82 ##############################
83 # simple test with two coins #
84 ##############################
85 inputs = [ ]
86 outputs = { self.nodes[0].getnewaddress() : 2.2 }
87 rawtx = self.nodes[2].createrawtransaction(inputs, outputs)
88 dec_tx = self.nodes[2].decoderawtransaction(rawtx)
90 rawtxfund = self.nodes[2].fundrawtransaction(rawtx)
91 fee = rawtxfund['fee']
92 dec_tx = self.nodes[2].decoderawtransaction(rawtxfund['hex'])
93 assert(len(dec_tx['vin']) > 0) #test if we have enough inputs
95 ##############################
96 # simple test with two coins #
97 ##############################
98 inputs = [ ]
99 outputs = { self.nodes[0].getnewaddress() : 2.6 }
100 rawtx = self.nodes[2].createrawtransaction(inputs, outputs)
101 dec_tx = self.nodes[2].decoderawtransaction(rawtx)
103 rawtxfund = self.nodes[2].fundrawtransaction(rawtx)
104 fee = rawtxfund['fee']
105 dec_tx = self.nodes[2].decoderawtransaction(rawtxfund['hex'])
106 assert(len(dec_tx['vin']) > 0)
107 assert_equal(dec_tx['vin'][0]['scriptSig']['hex'], '')
110 ################################
111 # simple test with two outputs #
112 ################################
113 inputs = [ ]
114 outputs = { self.nodes[0].getnewaddress() : 2.6, self.nodes[1].getnewaddress() : 2.5 }
115 rawtx = self.nodes[2].createrawtransaction(inputs, outputs)
116 dec_tx = self.nodes[2].decoderawtransaction(rawtx)
118 rawtxfund = self.nodes[2].fundrawtransaction(rawtx)
119 fee = rawtxfund['fee']
120 dec_tx = self.nodes[2].decoderawtransaction(rawtxfund['hex'])
121 totalOut = 0
122 for out in dec_tx['vout']:
123 totalOut += out['value']
125 assert(len(dec_tx['vin']) > 0)
126 assert_equal(dec_tx['vin'][0]['scriptSig']['hex'], '')
129 #########################################################################
130 # test a fundrawtransaction with a VIN greater than the required amount #
131 #########################################################################
132 utx = get_unspent(self.nodes[2].listunspent(), 5)
134 inputs = [ {'txid' : utx['txid'], 'vout' : utx['vout']}]
135 outputs = { self.nodes[0].getnewaddress() : 1.0 }
136 rawtx = self.nodes[2].createrawtransaction(inputs, outputs)
137 dec_tx = self.nodes[2].decoderawtransaction(rawtx)
138 assert_equal(utx['txid'], dec_tx['vin'][0]['txid'])
140 rawtxfund = self.nodes[2].fundrawtransaction(rawtx)
141 fee = rawtxfund['fee']
142 dec_tx = self.nodes[2].decoderawtransaction(rawtxfund['hex'])
143 totalOut = 0
144 for out in dec_tx['vout']:
145 totalOut += out['value']
147 assert_equal(fee + totalOut, utx['amount']) #compare vin total and totalout+fee
150 #####################################################################
151 # test a fundrawtransaction with which will not get a change output #
152 #####################################################################
153 utx = get_unspent(self.nodes[2].listunspent(), 5)
155 inputs = [ {'txid' : utx['txid'], 'vout' : utx['vout']}]
156 outputs = { self.nodes[0].getnewaddress() : Decimal(5.0) - fee - feeTolerance }
157 rawtx = self.nodes[2].createrawtransaction(inputs, outputs)
158 dec_tx = self.nodes[2].decoderawtransaction(rawtx)
159 assert_equal(utx['txid'], dec_tx['vin'][0]['txid'])
161 rawtxfund = self.nodes[2].fundrawtransaction(rawtx)
162 fee = rawtxfund['fee']
163 dec_tx = self.nodes[2].decoderawtransaction(rawtxfund['hex'])
164 totalOut = 0
165 for out in dec_tx['vout']:
166 totalOut += out['value']
168 assert_equal(rawtxfund['changepos'], -1)
169 assert_equal(fee + totalOut, utx['amount']) #compare vin total and totalout+fee
172 ####################################################
173 # test a fundrawtransaction with an invalid option #
174 ####################################################
175 utx = get_unspent(self.nodes[2].listunspent(), 5)
177 inputs = [ {'txid' : utx['txid'], 'vout' : utx['vout']} ]
178 outputs = { self.nodes[0].getnewaddress() : Decimal(4.0) }
179 rawtx = self.nodes[2].createrawtransaction(inputs, outputs)
180 dec_tx = self.nodes[2].decoderawtransaction(rawtx)
181 assert_equal(utx['txid'], dec_tx['vin'][0]['txid'])
183 assert_raises_jsonrpc(-3, "Unexpected key foo", self.nodes[2].fundrawtransaction, rawtx, {'foo':'bar'})
185 ############################################################
186 # test a fundrawtransaction with an invalid change address #
187 ############################################################
188 utx = get_unspent(self.nodes[2].listunspent(), 5)
190 inputs = [ {'txid' : utx['txid'], 'vout' : utx['vout']} ]
191 outputs = { self.nodes[0].getnewaddress() : Decimal(4.0) }
192 rawtx = self.nodes[2].createrawtransaction(inputs, outputs)
193 dec_tx = self.nodes[2].decoderawtransaction(rawtx)
194 assert_equal(utx['txid'], dec_tx['vin'][0]['txid'])
196 assert_raises_jsonrpc(-5, "changeAddress must be a valid bitcoin address", self.nodes[2].fundrawtransaction, rawtx, {'changeAddress':'foobar'})
198 ############################################################
199 # test a fundrawtransaction with a provided change address #
200 ############################################################
201 utx = get_unspent(self.nodes[2].listunspent(), 5)
203 inputs = [ {'txid' : utx['txid'], 'vout' : utx['vout']} ]
204 outputs = { self.nodes[0].getnewaddress() : Decimal(4.0) }
205 rawtx = self.nodes[2].createrawtransaction(inputs, outputs)
206 dec_tx = self.nodes[2].decoderawtransaction(rawtx)
207 assert_equal(utx['txid'], dec_tx['vin'][0]['txid'])
209 change = self.nodes[2].getnewaddress()
210 assert_raises_jsonrpc(-8, "changePosition out of bounds", self.nodes[2].fundrawtransaction, rawtx, {'changeAddress':change, 'changePosition':2})
211 rawtxfund = self.nodes[2].fundrawtransaction(rawtx, {'changeAddress': change, 'changePosition': 0})
212 dec_tx = self.nodes[2].decoderawtransaction(rawtxfund['hex'])
213 out = dec_tx['vout'][0]
214 assert_equal(change, out['scriptPubKey']['addresses'][0])
217 #########################################################################
218 # test a fundrawtransaction with a VIN smaller than the required amount #
219 #########################################################################
220 utx = get_unspent(self.nodes[2].listunspent(), 1)
222 inputs = [ {'txid' : utx['txid'], 'vout' : utx['vout']}]
223 outputs = { self.nodes[0].getnewaddress() : 1.0 }
224 rawtx = self.nodes[2].createrawtransaction(inputs, outputs)
226 # 4-byte version + 1-byte vin count + 36-byte prevout then script_len
227 rawtx = rawtx[:82] + "0100" + rawtx[84:]
229 dec_tx = self.nodes[2].decoderawtransaction(rawtx)
230 assert_equal(utx['txid'], dec_tx['vin'][0]['txid'])
231 assert_equal("00", dec_tx['vin'][0]['scriptSig']['hex'])
233 rawtxfund = self.nodes[2].fundrawtransaction(rawtx)
234 fee = rawtxfund['fee']
235 dec_tx = self.nodes[2].decoderawtransaction(rawtxfund['hex'])
236 totalOut = 0
237 matchingOuts = 0
238 for i, out in enumerate(dec_tx['vout']):
239 totalOut += out['value']
240 if out['scriptPubKey']['addresses'][0] in outputs:
241 matchingOuts+=1
242 else:
243 assert_equal(i, rawtxfund['changepos'])
245 assert_equal(utx['txid'], dec_tx['vin'][0]['txid'])
246 assert_equal("00", dec_tx['vin'][0]['scriptSig']['hex'])
248 assert_equal(matchingOuts, 1)
249 assert_equal(len(dec_tx['vout']), 2)
252 ###########################################
253 # test a fundrawtransaction with two VINs #
254 ###########################################
255 utx = get_unspent(self.nodes[2].listunspent(), 1)
256 utx2 = get_unspent(self.nodes[2].listunspent(), 5)
258 inputs = [ {'txid' : utx['txid'], 'vout' : utx['vout']},{'txid' : utx2['txid'], 'vout' : utx2['vout']} ]
259 outputs = { self.nodes[0].getnewaddress() : 6.0 }
260 rawtx = self.nodes[2].createrawtransaction(inputs, outputs)
261 dec_tx = self.nodes[2].decoderawtransaction(rawtx)
262 assert_equal(utx['txid'], dec_tx['vin'][0]['txid'])
264 rawtxfund = self.nodes[2].fundrawtransaction(rawtx)
265 fee = rawtxfund['fee']
266 dec_tx = self.nodes[2].decoderawtransaction(rawtxfund['hex'])
267 totalOut = 0
268 matchingOuts = 0
269 for out in dec_tx['vout']:
270 totalOut += out['value']
271 if out['scriptPubKey']['addresses'][0] in outputs:
272 matchingOuts+=1
274 assert_equal(matchingOuts, 1)
275 assert_equal(len(dec_tx['vout']), 2)
277 matchingIns = 0
278 for vinOut in dec_tx['vin']:
279 for vinIn in inputs:
280 if vinIn['txid'] == vinOut['txid']:
281 matchingIns+=1
283 assert_equal(matchingIns, 2) #we now must see two vins identical to vins given as params
285 #########################################################
286 # test a fundrawtransaction with two VINs and two vOUTs #
287 #########################################################
288 utx = get_unspent(self.nodes[2].listunspent(), 1)
289 utx2 = get_unspent(self.nodes[2].listunspent(), 5)
291 inputs = [ {'txid' : utx['txid'], 'vout' : utx['vout']},{'txid' : utx2['txid'], 'vout' : utx2['vout']} ]
292 outputs = { self.nodes[0].getnewaddress() : 6.0, self.nodes[0].getnewaddress() : 1.0 }
293 rawtx = self.nodes[2].createrawtransaction(inputs, outputs)
294 dec_tx = self.nodes[2].decoderawtransaction(rawtx)
295 assert_equal(utx['txid'], dec_tx['vin'][0]['txid'])
297 rawtxfund = self.nodes[2].fundrawtransaction(rawtx)
298 fee = rawtxfund['fee']
299 dec_tx = self.nodes[2].decoderawtransaction(rawtxfund['hex'])
300 totalOut = 0
301 matchingOuts = 0
302 for out in dec_tx['vout']:
303 totalOut += out['value']
304 if out['scriptPubKey']['addresses'][0] in outputs:
305 matchingOuts+=1
307 assert_equal(matchingOuts, 2)
308 assert_equal(len(dec_tx['vout']), 3)
310 ##############################################
311 # test a fundrawtransaction with invalid vin #
312 ##############################################
313 listunspent = self.nodes[2].listunspent()
314 inputs = [ {'txid' : "1c7f966dab21119bac53213a2bc7532bff1fa844c124fd750a7d0b1332440bd1", 'vout' : 0} ] #invalid vin!
315 outputs = { self.nodes[0].getnewaddress() : 1.0}
316 rawtx = self.nodes[2].createrawtransaction(inputs, outputs)
317 dec_tx = self.nodes[2].decoderawtransaction(rawtx)
319 assert_raises_jsonrpc(-4, "Insufficient funds", self.nodes[2].fundrawtransaction, rawtx)
321 ############################################################
322 #compare fee of a standard pubkeyhash transaction
323 inputs = []
324 outputs = {self.nodes[1].getnewaddress():1.1}
325 rawTx = self.nodes[0].createrawtransaction(inputs, outputs)
326 fundedTx = self.nodes[0].fundrawtransaction(rawTx)
328 #create same transaction over sendtoaddress
329 txId = self.nodes[0].sendtoaddress(self.nodes[1].getnewaddress(), 1.1)
330 signedFee = self.nodes[0].getrawmempool(True)[txId]['fee']
332 #compare fee
333 feeDelta = Decimal(fundedTx['fee']) - Decimal(signedFee)
334 assert(feeDelta >= 0 and feeDelta <= feeTolerance)
335 ############################################################
337 ############################################################
338 #compare fee of a standard pubkeyhash transaction with multiple outputs
339 inputs = []
340 outputs = {self.nodes[1].getnewaddress():1.1,self.nodes[1].getnewaddress():1.2,self.nodes[1].getnewaddress():0.1,self.nodes[1].getnewaddress():1.3,self.nodes[1].getnewaddress():0.2,self.nodes[1].getnewaddress():0.3}
341 rawTx = self.nodes[0].createrawtransaction(inputs, outputs)
342 fundedTx = self.nodes[0].fundrawtransaction(rawTx)
343 #create same transaction over sendtoaddress
344 txId = self.nodes[0].sendmany("", outputs)
345 signedFee = self.nodes[0].getrawmempool(True)[txId]['fee']
347 #compare fee
348 feeDelta = Decimal(fundedTx['fee']) - Decimal(signedFee)
349 assert(feeDelta >= 0 and feeDelta <= feeTolerance)
350 ############################################################
353 ############################################################
354 #compare fee of a 2of2 multisig p2sh transaction
356 # create 2of2 addr
357 addr1 = self.nodes[1].getnewaddress()
358 addr2 = self.nodes[1].getnewaddress()
360 addr1Obj = self.nodes[1].validateaddress(addr1)
361 addr2Obj = self.nodes[1].validateaddress(addr2)
363 mSigObj = self.nodes[1].addmultisigaddress(2, [addr1Obj['pubkey'], addr2Obj['pubkey']])
365 inputs = []
366 outputs = {mSigObj:1.1}
367 rawTx = self.nodes[0].createrawtransaction(inputs, outputs)
368 fundedTx = self.nodes[0].fundrawtransaction(rawTx)
370 #create same transaction over sendtoaddress
371 txId = self.nodes[0].sendtoaddress(mSigObj, 1.1)
372 signedFee = self.nodes[0].getrawmempool(True)[txId]['fee']
374 #compare fee
375 feeDelta = Decimal(fundedTx['fee']) - Decimal(signedFee)
376 assert(feeDelta >= 0 and feeDelta <= feeTolerance)
377 ############################################################
380 ############################################################
381 #compare fee of a standard pubkeyhash transaction
383 # create 4of5 addr
384 addr1 = self.nodes[1].getnewaddress()
385 addr2 = self.nodes[1].getnewaddress()
386 addr3 = self.nodes[1].getnewaddress()
387 addr4 = self.nodes[1].getnewaddress()
388 addr5 = self.nodes[1].getnewaddress()
390 addr1Obj = self.nodes[1].validateaddress(addr1)
391 addr2Obj = self.nodes[1].validateaddress(addr2)
392 addr3Obj = self.nodes[1].validateaddress(addr3)
393 addr4Obj = self.nodes[1].validateaddress(addr4)
394 addr5Obj = self.nodes[1].validateaddress(addr5)
396 mSigObj = self.nodes[1].addmultisigaddress(4, [addr1Obj['pubkey'], addr2Obj['pubkey'], addr3Obj['pubkey'], addr4Obj['pubkey'], addr5Obj['pubkey']])
398 inputs = []
399 outputs = {mSigObj:1.1}
400 rawTx = self.nodes[0].createrawtransaction(inputs, outputs)
401 fundedTx = self.nodes[0].fundrawtransaction(rawTx)
403 #create same transaction over sendtoaddress
404 txId = self.nodes[0].sendtoaddress(mSigObj, 1.1)
405 signedFee = self.nodes[0].getrawmempool(True)[txId]['fee']
407 #compare fee
408 feeDelta = Decimal(fundedTx['fee']) - Decimal(signedFee)
409 assert(feeDelta >= 0 and feeDelta <= feeTolerance)
410 ############################################################
413 ############################################################
414 # spend a 2of2 multisig transaction over fundraw
416 # create 2of2 addr
417 addr1 = self.nodes[2].getnewaddress()
418 addr2 = self.nodes[2].getnewaddress()
420 addr1Obj = self.nodes[2].validateaddress(addr1)
421 addr2Obj = self.nodes[2].validateaddress(addr2)
423 mSigObj = self.nodes[2].addmultisigaddress(2, [addr1Obj['pubkey'], addr2Obj['pubkey']])
426 # send 1.2 BTC to msig addr
427 txId = self.nodes[0].sendtoaddress(mSigObj, 1.2)
428 self.sync_all()
429 self.nodes[1].generate(1)
430 self.sync_all()
432 oldBalance = self.nodes[1].getbalance()
433 inputs = []
434 outputs = {self.nodes[1].getnewaddress():1.1}
435 rawTx = self.nodes[2].createrawtransaction(inputs, outputs)
436 fundedTx = self.nodes[2].fundrawtransaction(rawTx)
438 signedTx = self.nodes[2].signrawtransaction(fundedTx['hex'])
439 txId = self.nodes[2].sendrawtransaction(signedTx['hex'])
440 self.sync_all()
441 self.nodes[1].generate(1)
442 self.sync_all()
444 # make sure funds are received at node1
445 assert_equal(oldBalance+Decimal('1.10000000'), self.nodes[1].getbalance())
447 ############################################################
448 # locked wallet test
449 self.nodes[1].encryptwallet("test")
450 self.nodes.pop(1)
451 stop_node(self.nodes[0], 0)
452 stop_node(self.nodes[1], 2)
453 stop_node(self.nodes[2], 3)
455 self.nodes = start_nodes(self.num_nodes, self.options.tmpdir)
456 # This test is not meant to test fee estimation and we'd like
457 # to be sure all txs are sent at a consistent desired feerate
458 for node in self.nodes:
459 node.settxfee(min_relay_tx_fee)
461 connect_nodes_bi(self.nodes,0,1)
462 connect_nodes_bi(self.nodes,1,2)
463 connect_nodes_bi(self.nodes,0,2)
464 connect_nodes_bi(self.nodes,0,3)
465 self.is_network_split=False
466 self.sync_all()
468 # drain the keypool
469 self.nodes[1].getnewaddress()
470 inputs = []
471 outputs = {self.nodes[0].getnewaddress():1.1}
472 rawTx = self.nodes[1].createrawtransaction(inputs, outputs)
473 # fund a transaction that requires a new key for the change output
474 # creating the key must be impossible because the wallet is locked
475 assert_raises_jsonrpc(-4, "Insufficient funds", self.nodes[1].fundrawtransaction, rawtx)
477 #refill the keypool
478 self.nodes[1].walletpassphrase("test", 100)
479 self.nodes[1].walletlock()
481 assert_raises_jsonrpc(-13, "walletpassphrase", self.nodes[1].sendtoaddress, self.nodes[0].getnewaddress(), 1.2)
483 oldBalance = self.nodes[0].getbalance()
485 inputs = []
486 outputs = {self.nodes[0].getnewaddress():1.1}
487 rawTx = self.nodes[1].createrawtransaction(inputs, outputs)
488 fundedTx = self.nodes[1].fundrawtransaction(rawTx)
490 #now we need to unlock
491 self.nodes[1].walletpassphrase("test", 600)
492 signedTx = self.nodes[1].signrawtransaction(fundedTx['hex'])
493 txId = self.nodes[1].sendrawtransaction(signedTx['hex'])
494 self.nodes[1].generate(1)
495 self.sync_all()
497 # make sure funds are received at node1
498 assert_equal(oldBalance+Decimal('51.10000000'), self.nodes[0].getbalance())
501 ###############################################
502 # multiple (~19) inputs tx test | Compare fee #
503 ###############################################
505 #empty node1, send some small coins from node0 to node1
506 self.nodes[1].sendtoaddress(self.nodes[0].getnewaddress(), self.nodes[1].getbalance(), "", "", True)
507 self.sync_all()
508 self.nodes[0].generate(1)
509 self.sync_all()
511 for i in range(0,20):
512 self.nodes[0].sendtoaddress(self.nodes[1].getnewaddress(), 0.01)
513 self.nodes[0].generate(1)
514 self.sync_all()
516 #fund a tx with ~20 small inputs
517 inputs = []
518 outputs = {self.nodes[0].getnewaddress():0.15,self.nodes[0].getnewaddress():0.04}
519 rawTx = self.nodes[1].createrawtransaction(inputs, outputs)
520 fundedTx = self.nodes[1].fundrawtransaction(rawTx)
522 #create same transaction over sendtoaddress
523 txId = self.nodes[1].sendmany("", outputs)
524 signedFee = self.nodes[1].getrawmempool(True)[txId]['fee']
526 #compare fee
527 feeDelta = Decimal(fundedTx['fee']) - Decimal(signedFee)
528 assert(feeDelta >= 0 and feeDelta <= feeTolerance*19) #~19 inputs
531 #############################################
532 # multiple (~19) inputs tx test | sign/send #
533 #############################################
535 #again, empty node1, send some small coins from node0 to node1
536 self.nodes[1].sendtoaddress(self.nodes[0].getnewaddress(), self.nodes[1].getbalance(), "", "", True)
537 self.sync_all()
538 self.nodes[0].generate(1)
539 self.sync_all()
541 for i in range(0,20):
542 self.nodes[0].sendtoaddress(self.nodes[1].getnewaddress(), 0.01)
543 self.nodes[0].generate(1)
544 self.sync_all()
546 #fund a tx with ~20 small inputs
547 oldBalance = self.nodes[0].getbalance()
549 inputs = []
550 outputs = {self.nodes[0].getnewaddress():0.15,self.nodes[0].getnewaddress():0.04}
551 rawTx = self.nodes[1].createrawtransaction(inputs, outputs)
552 fundedTx = self.nodes[1].fundrawtransaction(rawTx)
553 fundedAndSignedTx = self.nodes[1].signrawtransaction(fundedTx['hex'])
554 txId = self.nodes[1].sendrawtransaction(fundedAndSignedTx['hex'])
555 self.sync_all()
556 self.nodes[0].generate(1)
557 self.sync_all()
558 assert_equal(oldBalance+Decimal('50.19000000'), self.nodes[0].getbalance()) #0.19+block reward
560 #####################################################
561 # test fundrawtransaction with OP_RETURN and no vin #
562 #####################################################
564 rawtx = "0100000000010000000000000000066a047465737400000000"
565 dec_tx = self.nodes[2].decoderawtransaction(rawtx)
567 assert_equal(len(dec_tx['vin']), 0)
568 assert_equal(len(dec_tx['vout']), 1)
570 rawtxfund = self.nodes[2].fundrawtransaction(rawtx)
571 dec_tx = self.nodes[2].decoderawtransaction(rawtxfund['hex'])
573 assert_greater_than(len(dec_tx['vin']), 0) # at least one vin
574 assert_equal(len(dec_tx['vout']), 2) # one change output added
577 ##################################################
578 # test a fundrawtransaction using only watchonly #
579 ##################################################
581 inputs = []
582 outputs = {self.nodes[2].getnewaddress() : watchonly_amount / 2}
583 rawtx = self.nodes[3].createrawtransaction(inputs, outputs)
585 result = self.nodes[3].fundrawtransaction(rawtx, {'includeWatching': True })
586 res_dec = self.nodes[0].decoderawtransaction(result["hex"])
587 assert_equal(len(res_dec["vin"]), 1)
588 assert_equal(res_dec["vin"][0]["txid"], watchonly_txid)
590 assert("fee" in result.keys())
591 assert_greater_than(result["changepos"], -1)
593 ###############################################################
594 # test fundrawtransaction using the entirety of watched funds #
595 ###############################################################
597 inputs = []
598 outputs = {self.nodes[2].getnewaddress() : watchonly_amount}
599 rawtx = self.nodes[3].createrawtransaction(inputs, outputs)
601 # Backward compatibility test (2nd param is includeWatching)
602 result = self.nodes[3].fundrawtransaction(rawtx, True)
603 res_dec = self.nodes[0].decoderawtransaction(result["hex"])
604 assert_equal(len(res_dec["vin"]), 2)
605 assert(res_dec["vin"][0]["txid"] == watchonly_txid or res_dec["vin"][1]["txid"] == watchonly_txid)
607 assert_greater_than(result["fee"], 0)
608 assert_greater_than(result["changepos"], -1)
609 assert_equal(result["fee"] + res_dec["vout"][result["changepos"]]["value"], watchonly_amount / 10)
611 signedtx = self.nodes[3].signrawtransaction(result["hex"])
612 assert(not signedtx["complete"])
613 signedtx = self.nodes[0].signrawtransaction(signedtx["hex"])
614 assert(signedtx["complete"])
615 self.nodes[0].sendrawtransaction(signedtx["hex"])
616 self.nodes[0].generate(1)
617 self.sync_all()
619 #######################
620 # Test feeRate option #
621 #######################
623 # Make sure there is exactly one input so coin selection can't skew the result
624 assert_equal(len(self.nodes[3].listunspent(1)), 1)
626 inputs = []
627 outputs = {self.nodes[3].getnewaddress() : 1}
628 rawtx = self.nodes[3].createrawtransaction(inputs, outputs)
629 result = self.nodes[3].fundrawtransaction(rawtx) # uses min_relay_tx_fee (set by settxfee)
630 result2 = self.nodes[3].fundrawtransaction(rawtx, {"feeRate": 2*min_relay_tx_fee})
631 result3 = self.nodes[3].fundrawtransaction(rawtx, {"feeRate": 10*min_relay_tx_fee})
632 result_fee_rate = result['fee'] * 1000 / count_bytes(result['hex'])
633 assert_fee_amount(result2['fee'], count_bytes(result2['hex']), 2 * result_fee_rate)
634 assert_fee_amount(result3['fee'], count_bytes(result3['hex']), 10 * result_fee_rate)
636 #############################
637 # Test address reuse option #
638 #############################
640 result3 = self.nodes[3].fundrawtransaction(rawtx, {"reserveChangeKey": False})
641 res_dec = self.nodes[0].decoderawtransaction(result3["hex"])
642 changeaddress = ""
643 for out in res_dec['vout']:
644 if out['value'] > 1.0:
645 changeaddress += out['scriptPubKey']['addresses'][0]
646 assert(changeaddress != "")
647 nextaddr = self.nodes[3].getnewaddress()
648 # frt should not have removed the key from the keypool
649 assert(changeaddress == nextaddr)
651 result3 = self.nodes[3].fundrawtransaction(rawtx)
652 res_dec = self.nodes[0].decoderawtransaction(result3["hex"])
653 changeaddress = ""
654 for out in res_dec['vout']:
655 if out['value'] > 1.0:
656 changeaddress += out['scriptPubKey']['addresses'][0]
657 assert(changeaddress != "")
658 nextaddr = self.nodes[3].getnewaddress()
659 # Now the change address key should be removed from the keypool
660 assert(changeaddress != nextaddr)
662 ######################################
663 # Test subtractFeeFromOutputs option #
664 ######################################
666 # Make sure there is exactly one input so coin selection can't skew the result
667 assert_equal(len(self.nodes[3].listunspent(1)), 1)
669 inputs = []
670 outputs = {self.nodes[2].getnewaddress(): 1}
671 rawtx = self.nodes[3].createrawtransaction(inputs, outputs)
673 result = [self.nodes[3].fundrawtransaction(rawtx), # uses min_relay_tx_fee (set by settxfee)
674 self.nodes[3].fundrawtransaction(rawtx, {"subtractFeeFromOutputs": []}), # empty subtraction list
675 self.nodes[3].fundrawtransaction(rawtx, {"subtractFeeFromOutputs": [0]}), # uses min_relay_tx_fee (set by settxfee)
676 self.nodes[3].fundrawtransaction(rawtx, {"feeRate": 2*min_relay_tx_fee}),
677 self.nodes[3].fundrawtransaction(rawtx, {"feeRate": 2*min_relay_tx_fee, "subtractFeeFromOutputs": [0]})]
679 dec_tx = [self.nodes[3].decoderawtransaction(tx['hex']) for tx in result]
680 output = [d['vout'][1 - r['changepos']]['value'] for d, r in zip(dec_tx, result)]
681 change = [d['vout'][r['changepos']]['value'] for d, r in zip(dec_tx, result)]
683 assert_equal(result[0]['fee'], result[1]['fee'], result[2]['fee'])
684 assert_equal(result[3]['fee'], result[4]['fee'])
685 assert_equal(change[0], change[1])
686 assert_equal(output[0], output[1])
687 assert_equal(output[0], output[2] + result[2]['fee'])
688 assert_equal(change[0] + result[0]['fee'], change[2])
689 assert_equal(output[3], output[4] + result[4]['fee'])
690 assert_equal(change[3] + result[3]['fee'], change[4])
692 inputs = []
693 outputs = {self.nodes[2].getnewaddress(): value for value in (1.0, 1.1, 1.2, 1.3)}
694 rawtx = self.nodes[3].createrawtransaction(inputs, outputs)
696 result = [self.nodes[3].fundrawtransaction(rawtx),
697 # split the fee between outputs 0, 2, and 3, but not output 1
698 self.nodes[3].fundrawtransaction(rawtx, {"subtractFeeFromOutputs": [0, 2, 3]})]
700 dec_tx = [self.nodes[3].decoderawtransaction(result[0]['hex']),
701 self.nodes[3].decoderawtransaction(result[1]['hex'])]
703 # Nested list of non-change output amounts for each transaction
704 output = [[out['value'] for i, out in enumerate(d['vout']) if i != r['changepos']]
705 for d, r in zip(dec_tx, result)]
707 # List of differences in output amounts between normal and subtractFee transactions
708 share = [o0 - o1 for o0, o1 in zip(output[0], output[1])]
710 # output 1 is the same in both transactions
711 assert_equal(share[1], 0)
713 # the other 3 outputs are smaller as a result of subtractFeeFromOutputs
714 assert_greater_than(share[0], 0)
715 assert_greater_than(share[2], 0)
716 assert_greater_than(share[3], 0)
718 # outputs 2 and 3 take the same share of the fee
719 assert_equal(share[2], share[3])
721 # output 0 takes at least as much share of the fee, and no more than 2 satoshis more, than outputs 2 and 3
722 assert_greater_than_or_equal(share[0], share[2])
723 assert_greater_than_or_equal(share[2] + Decimal(2e-8), share[0])
725 # the fee is the same in both transactions
726 assert_equal(result[0]['fee'], result[1]['fee'])
728 # the total subtracted from the outputs is equal to the fee
729 assert_equal(share[0] + share[2] + share[3], result[0]['fee'])
731 if __name__ == '__main__':
732 RawTransactionsTest().main()