You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

fundrawtransaction.py 31KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720
  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."""
  6. from test_framework.test_framework import BitcoinTestFramework
  7. from test_framework.util import *
  8. def get_unspent(listunspent, amount):
  9. for utx in listunspent:
  10. if utx['amount'] == amount:
  11. return utx
  12. raise AssertionError('Could not find unspent with amount={}'.format(amount))
  13. class RawTransactionsTest(BitcoinTestFramework):
  14. def set_test_params(self):
  15. self.num_nodes = 4
  16. self.setup_clean_chain = True
  17. def setup_network(self, split=False):
  18. self.setup_nodes()
  19. connect_nodes_bi(self.nodes, 0, 1)
  20. connect_nodes_bi(self.nodes, 1, 2)
  21. connect_nodes_bi(self.nodes, 0, 2)
  22. connect_nodes_bi(self.nodes, 0, 3)
  23. def run_test(self):
  24. min_relay_tx_fee = self.nodes[0].getnetworkinfo()['relayfee']
  25. # This test is not meant to test fee estimation and we'd like
  26. # to be sure all txs are sent at a consistent desired feerate
  27. for node in self.nodes:
  28. node.settxfee(min_relay_tx_fee)
  29. # if the fee's positive delta is higher than this value tests will fail,
  30. # neg. delta always fail the tests.
  31. # The size of the signature of every input may be at most 2 bytes larger
  32. # than a minimum sized signature.
  33. # = 2 bytes * minRelayTxFeePerByte
  34. feeTolerance = 2 * min_relay_tx_fee/1000
  35. self.nodes[2].generate(1)
  36. self.sync_all()
  37. self.nodes[0].generate(121)
  38. self.sync_all()
  39. # ensure that setting changePosition in fundraw with an exact match is handled properly
  40. rawmatch = self.nodes[2].createrawtransaction([], {self.nodes[2].getnewaddress():50})
  41. rawmatch = self.nodes[2].fundrawtransaction(rawmatch, {"changePosition":1, "subtractFeeFromOutputs":[0]})
  42. assert_equal(rawmatch["changepos"], -1)
  43. watchonly_address = self.nodes[0].getnewaddress()
  44. watchonly_pubkey = self.nodes[0].validateaddress(watchonly_address)["pubkey"]
  45. watchonly_amount = Decimal(200)
  46. self.nodes[3].importpubkey(watchonly_pubkey, "", True)
  47. watchonly_txid = self.nodes[0].sendtoaddress(watchonly_address, watchonly_amount)
  48. self.nodes[0].sendtoaddress(self.nodes[3].getnewaddress(), watchonly_amount / 10)
  49. self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(), 1.5)
  50. self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(), 1.0)
  51. self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(), 5.0)
  52. self.nodes[0].generate(1)
  53. self.sync_all()
  54. ###############
  55. # simple test #
  56. ###############
  57. inputs = [ ]
  58. outputs = { self.nodes[0].getnewaddress() : 1.0 }
  59. rawtx = self.nodes[2].createrawtransaction(inputs, outputs)
  60. dec_tx = self.nodes[2].decoderawtransaction(rawtx)
  61. rawtxfund = self.nodes[2].fundrawtransaction(rawtx)
  62. fee = rawtxfund['fee']
  63. dec_tx = self.nodes[2].decoderawtransaction(rawtxfund['hex'])
  64. assert(len(dec_tx['vin']) > 0) #test that we have enough inputs
  65. ##############################
  66. # simple test with two coins #
  67. ##############################
  68. inputs = [ ]
  69. outputs = { self.nodes[0].getnewaddress() : 2.2 }
  70. rawtx = self.nodes[2].createrawtransaction(inputs, outputs)
  71. dec_tx = self.nodes[2].decoderawtransaction(rawtx)
  72. rawtxfund = self.nodes[2].fundrawtransaction(rawtx)
  73. fee = rawtxfund['fee']
  74. dec_tx = self.nodes[2].decoderawtransaction(rawtxfund['hex'])
  75. assert(len(dec_tx['vin']) > 0) #test if we have enough inputs
  76. ##############################
  77. # simple test with two coins #
  78. ##############################
  79. inputs = [ ]
  80. outputs = { self.nodes[0].getnewaddress() : 2.6 }
  81. rawtx = self.nodes[2].createrawtransaction(inputs, outputs)
  82. dec_tx = self.nodes[2].decoderawtransaction(rawtx)
  83. rawtxfund = self.nodes[2].fundrawtransaction(rawtx)
  84. fee = rawtxfund['fee']
  85. dec_tx = self.nodes[2].decoderawtransaction(rawtxfund['hex'])
  86. assert(len(dec_tx['vin']) > 0)
  87. assert_equal(dec_tx['vin'][0]['scriptSig']['hex'], '')
  88. ################################
  89. # simple test with two outputs #
  90. ################################
  91. inputs = [ ]
  92. outputs = { self.nodes[0].getnewaddress() : 2.6, self.nodes[1].getnewaddress() : 2.5 }
  93. rawtx = self.nodes[2].createrawtransaction(inputs, outputs)
  94. dec_tx = self.nodes[2].decoderawtransaction(rawtx)
  95. rawtxfund = self.nodes[2].fundrawtransaction(rawtx)
  96. fee = rawtxfund['fee']
  97. dec_tx = self.nodes[2].decoderawtransaction(rawtxfund['hex'])
  98. totalOut = 0
  99. for out in dec_tx['vout']:
  100. totalOut += out['value']
  101. assert(len(dec_tx['vin']) > 0)
  102. assert_equal(dec_tx['vin'][0]['scriptSig']['hex'], '')
  103. #########################################################################
  104. # test a fundrawtransaction with a VIN greater than the required amount #
  105. #########################################################################
  106. utx = get_unspent(self.nodes[2].listunspent(), 5)
  107. inputs = [ {'txid' : utx['txid'], 'vout' : utx['vout']}]
  108. outputs = { self.nodes[0].getnewaddress() : 1.0 }
  109. rawtx = self.nodes[2].createrawtransaction(inputs, outputs)
  110. dec_tx = self.nodes[2].decoderawtransaction(rawtx)
  111. assert_equal(utx['txid'], dec_tx['vin'][0]['txid'])
  112. rawtxfund = self.nodes[2].fundrawtransaction(rawtx)
  113. fee = rawtxfund['fee']
  114. dec_tx = self.nodes[2].decoderawtransaction(rawtxfund['hex'])
  115. totalOut = 0
  116. for out in dec_tx['vout']:
  117. totalOut += out['value']
  118. assert_equal(fee + totalOut, utx['amount']) #compare vin total and totalout+fee
  119. #####################################################################
  120. # test a fundrawtransaction with which will not get a change output #
  121. #####################################################################
  122. utx = get_unspent(self.nodes[2].listunspent(), 5)
  123. inputs = [ {'txid' : utx['txid'], 'vout' : utx['vout']}]
  124. outputs = { self.nodes[0].getnewaddress() : Decimal(5.0) - fee - feeTolerance }
  125. rawtx = self.nodes[2].createrawtransaction(inputs, outputs)
  126. dec_tx = self.nodes[2].decoderawtransaction(rawtx)
  127. assert_equal(utx['txid'], dec_tx['vin'][0]['txid'])
  128. rawtxfund = self.nodes[2].fundrawtransaction(rawtx)
  129. fee = rawtxfund['fee']
  130. dec_tx = self.nodes[2].decoderawtransaction(rawtxfund['hex'])
  131. totalOut = 0
  132. for out in dec_tx['vout']:
  133. totalOut += out['value']
  134. assert_equal(rawtxfund['changepos'], -1)
  135. assert_equal(fee + totalOut, utx['amount']) #compare vin total and totalout+fee
  136. ####################################################
  137. # test a fundrawtransaction with an invalid option #
  138. ####################################################
  139. utx = get_unspent(self.nodes[2].listunspent(), 5)
  140. inputs = [ {'txid' : utx['txid'], 'vout' : utx['vout']} ]
  141. outputs = { self.nodes[0].getnewaddress() : Decimal(4.0) }
  142. rawtx = self.nodes[2].createrawtransaction(inputs, outputs)
  143. dec_tx = self.nodes[2].decoderawtransaction(rawtx)
  144. assert_equal(utx['txid'], dec_tx['vin'][0]['txid'])
  145. assert_raises_rpc_error(-3, "Unexpected key foo", self.nodes[2].fundrawtransaction, rawtx, {'foo':'bar'})
  146. ############################################################
  147. # test a fundrawtransaction with an invalid change address #
  148. ############################################################
  149. utx = get_unspent(self.nodes[2].listunspent(), 5)
  150. inputs = [ {'txid' : utx['txid'], 'vout' : utx['vout']} ]
  151. outputs = { self.nodes[0].getnewaddress() : Decimal(4.0) }
  152. rawtx = self.nodes[2].createrawtransaction(inputs, outputs)
  153. dec_tx = self.nodes[2].decoderawtransaction(rawtx)
  154. assert_equal(utx['txid'], dec_tx['vin'][0]['txid'])
  155. assert_raises_rpc_error(-5, "changeAddress must be a valid bitcoin address", self.nodes[2].fundrawtransaction, rawtx, {'changeAddress':'foobar'})
  156. ############################################################
  157. # test a fundrawtransaction with a provided change address #
  158. ############################################################
  159. utx = get_unspent(self.nodes[2].listunspent(), 5)
  160. inputs = [ {'txid' : utx['txid'], 'vout' : utx['vout']} ]
  161. outputs = { self.nodes[0].getnewaddress() : Decimal(4.0) }
  162. rawtx = self.nodes[2].createrawtransaction(inputs, outputs)
  163. dec_tx = self.nodes[2].decoderawtransaction(rawtx)
  164. assert_equal(utx['txid'], dec_tx['vin'][0]['txid'])
  165. change = self.nodes[2].getnewaddress()
  166. assert_raises_rpc_error(-8, "changePosition out of bounds", self.nodes[2].fundrawtransaction, rawtx, {'changeAddress':change, 'changePosition':2})
  167. rawtxfund = self.nodes[2].fundrawtransaction(rawtx, {'changeAddress': change, 'changePosition': 0})
  168. dec_tx = self.nodes[2].decoderawtransaction(rawtxfund['hex'])
  169. out = dec_tx['vout'][0]
  170. assert_equal(change, out['scriptPubKey']['addresses'][0])
  171. #########################################################################
  172. # test a fundrawtransaction with a VIN smaller than the required amount #
  173. #########################################################################
  174. utx = get_unspent(self.nodes[2].listunspent(), 1)
  175. inputs = [ {'txid' : utx['txid'], 'vout' : utx['vout']}]
  176. outputs = { self.nodes[0].getnewaddress() : 1.0 }
  177. rawtx = self.nodes[2].createrawtransaction(inputs, outputs)
  178. # 4-byte version + 1-byte vin count + 36-byte prevout then script_len
  179. rawtx = rawtx[:82] + "0100" + rawtx[84:]
  180. dec_tx = self.nodes[2].decoderawtransaction(rawtx)
  181. assert_equal(utx['txid'], dec_tx['vin'][0]['txid'])
  182. assert_equal("00", dec_tx['vin'][0]['scriptSig']['hex'])
  183. rawtxfund = self.nodes[2].fundrawtransaction(rawtx)
  184. fee = rawtxfund['fee']
  185. dec_tx = self.nodes[2].decoderawtransaction(rawtxfund['hex'])
  186. totalOut = 0
  187. matchingOuts = 0
  188. for i, out in enumerate(dec_tx['vout']):
  189. totalOut += out['value']
  190. if out['scriptPubKey']['addresses'][0] in outputs:
  191. matchingOuts+=1
  192. else:
  193. assert_equal(i, rawtxfund['changepos'])
  194. assert_equal(utx['txid'], dec_tx['vin'][0]['txid'])
  195. assert_equal("00", dec_tx['vin'][0]['scriptSig']['hex'])
  196. assert_equal(matchingOuts, 1)
  197. assert_equal(len(dec_tx['vout']), 2)
  198. ###########################################
  199. # test a fundrawtransaction with two VINs #
  200. ###########################################
  201. utx = get_unspent(self.nodes[2].listunspent(), 1)
  202. utx2 = get_unspent(self.nodes[2].listunspent(), 5)
  203. inputs = [ {'txid' : utx['txid'], 'vout' : utx['vout']},{'txid' : utx2['txid'], 'vout' : utx2['vout']} ]
  204. outputs = { self.nodes[0].getnewaddress() : 6.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'])
  208. rawtxfund = self.nodes[2].fundrawtransaction(rawtx)
  209. fee = rawtxfund['fee']
  210. dec_tx = self.nodes[2].decoderawtransaction(rawtxfund['hex'])
  211. totalOut = 0
  212. matchingOuts = 0
  213. for out in dec_tx['vout']:
  214. totalOut += out['value']
  215. if out['scriptPubKey']['addresses'][0] in outputs:
  216. matchingOuts+=1
  217. assert_equal(matchingOuts, 1)
  218. assert_equal(len(dec_tx['vout']), 2)
  219. matchingIns = 0
  220. for vinOut in dec_tx['vin']:
  221. for vinIn in inputs:
  222. if vinIn['txid'] == vinOut['txid']:
  223. matchingIns+=1
  224. assert_equal(matchingIns, 2) #we now must see two vins identical to vins given as params
  225. #########################################################
  226. # test a fundrawtransaction with two VINs and two vOUTs #
  227. #########################################################
  228. utx = get_unspent(self.nodes[2].listunspent(), 1)
  229. utx2 = get_unspent(self.nodes[2].listunspent(), 5)
  230. inputs = [ {'txid' : utx['txid'], 'vout' : utx['vout']},{'txid' : utx2['txid'], 'vout' : utx2['vout']} ]
  231. outputs = { self.nodes[0].getnewaddress() : 6.0, self.nodes[0].getnewaddress() : 1.0 }
  232. rawtx = self.nodes[2].createrawtransaction(inputs, outputs)
  233. dec_tx = self.nodes[2].decoderawtransaction(rawtx)
  234. assert_equal(utx['txid'], dec_tx['vin'][0]['txid'])
  235. rawtxfund = self.nodes[2].fundrawtransaction(rawtx)
  236. fee = rawtxfund['fee']
  237. dec_tx = self.nodes[2].decoderawtransaction(rawtxfund['hex'])
  238. totalOut = 0
  239. matchingOuts = 0
  240. for out in dec_tx['vout']:
  241. totalOut += out['value']
  242. if out['scriptPubKey']['addresses'][0] in outputs:
  243. matchingOuts+=1
  244. assert_equal(matchingOuts, 2)
  245. assert_equal(len(dec_tx['vout']), 3)
  246. ##############################################
  247. # test a fundrawtransaction with invalid vin #
  248. ##############################################
  249. listunspent = self.nodes[2].listunspent()
  250. inputs = [ {'txid' : "1c7f966dab21119bac53213a2bc7532bff1fa844c124fd750a7d0b1332440bd1", 'vout' : 0} ] #invalid vin!
  251. outputs = { self.nodes[0].getnewaddress() : 1.0}
  252. rawtx = self.nodes[2].createrawtransaction(inputs, outputs)
  253. dec_tx = self.nodes[2].decoderawtransaction(rawtx)
  254. assert_raises_rpc_error(-4, "Insufficient funds", self.nodes[2].fundrawtransaction, rawtx)
  255. ############################################################
  256. #compare fee of a standard pubkeyhash transaction
  257. inputs = []
  258. outputs = {self.nodes[1].getnewaddress():1.1}
  259. rawtx = self.nodes[0].createrawtransaction(inputs, outputs)
  260. fundedTx = self.nodes[0].fundrawtransaction(rawtx)
  261. #create same transaction over sendtoaddress
  262. txId = self.nodes[0].sendtoaddress(self.nodes[1].getnewaddress(), 1.1)
  263. signedFee = self.nodes[0].getrawmempool(True)[txId]['fee']
  264. #compare fee
  265. feeDelta = Decimal(fundedTx['fee']) - Decimal(signedFee)
  266. assert(feeDelta >= 0 and feeDelta <= feeTolerance)
  267. ############################################################
  268. ############################################################
  269. #compare fee of a standard pubkeyhash transaction with multiple outputs
  270. inputs = []
  271. 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}
  272. rawtx = self.nodes[0].createrawtransaction(inputs, outputs)
  273. fundedTx = self.nodes[0].fundrawtransaction(rawtx)
  274. #create same transaction over sendtoaddress
  275. txId = self.nodes[0].sendmany("", outputs)
  276. signedFee = self.nodes[0].getrawmempool(True)[txId]['fee']
  277. #compare fee
  278. feeDelta = Decimal(fundedTx['fee']) - Decimal(signedFee)
  279. assert(feeDelta >= 0 and feeDelta <= feeTolerance)
  280. ############################################################
  281. ############################################################
  282. #compare fee of a 2of2 multisig p2sh transaction
  283. # create 2of2 addr
  284. addr1 = self.nodes[1].getnewaddress()
  285. addr2 = self.nodes[1].getnewaddress()
  286. addr1Obj = self.nodes[1].validateaddress(addr1)
  287. addr2Obj = self.nodes[1].validateaddress(addr2)
  288. mSigObj = self.nodes[1].addmultisigaddress(2, [addr1Obj['pubkey'], addr2Obj['pubkey']])
  289. inputs = []
  290. outputs = {mSigObj:1.1}
  291. rawtx = self.nodes[0].createrawtransaction(inputs, outputs)
  292. fundedTx = self.nodes[0].fundrawtransaction(rawtx)
  293. #create same transaction over sendtoaddress
  294. txId = self.nodes[0].sendtoaddress(mSigObj, 1.1)
  295. signedFee = self.nodes[0].getrawmempool(True)[txId]['fee']
  296. #compare fee
  297. feeDelta = Decimal(fundedTx['fee']) - Decimal(signedFee)
  298. assert(feeDelta >= 0 and feeDelta <= feeTolerance)
  299. ############################################################
  300. ############################################################
  301. #compare fee of a standard pubkeyhash transaction
  302. # create 4of5 addr
  303. addr1 = self.nodes[1].getnewaddress()
  304. addr2 = self.nodes[1].getnewaddress()
  305. addr3 = self.nodes[1].getnewaddress()
  306. addr4 = self.nodes[1].getnewaddress()
  307. addr5 = self.nodes[1].getnewaddress()
  308. addr1Obj = self.nodes[1].validateaddress(addr1)
  309. addr2Obj = self.nodes[1].validateaddress(addr2)
  310. addr3Obj = self.nodes[1].validateaddress(addr3)
  311. addr4Obj = self.nodes[1].validateaddress(addr4)
  312. addr5Obj = self.nodes[1].validateaddress(addr5)
  313. mSigObj = self.nodes[1].addmultisigaddress(4, [addr1Obj['pubkey'], addr2Obj['pubkey'], addr3Obj['pubkey'], addr4Obj['pubkey'], addr5Obj['pubkey']])
  314. inputs = []
  315. outputs = {mSigObj:1.1}
  316. rawtx = self.nodes[0].createrawtransaction(inputs, outputs)
  317. fundedTx = self.nodes[0].fundrawtransaction(rawtx)
  318. #create same transaction over sendtoaddress
  319. txId = self.nodes[0].sendtoaddress(mSigObj, 1.1)
  320. signedFee = self.nodes[0].getrawmempool(True)[txId]['fee']
  321. #compare fee
  322. feeDelta = Decimal(fundedTx['fee']) - Decimal(signedFee)
  323. assert(feeDelta >= 0 and feeDelta <= feeTolerance)
  324. ############################################################
  325. ############################################################
  326. # spend a 2of2 multisig transaction over fundraw
  327. # create 2of2 addr
  328. addr1 = self.nodes[2].getnewaddress()
  329. addr2 = self.nodes[2].getnewaddress()
  330. addr1Obj = self.nodes[2].validateaddress(addr1)
  331. addr2Obj = self.nodes[2].validateaddress(addr2)
  332. mSigObj = self.nodes[2].addmultisigaddress(2, [addr1Obj['pubkey'], addr2Obj['pubkey']])
  333. # send 1.2 BTC to msig addr
  334. txId = self.nodes[0].sendtoaddress(mSigObj, 1.2)
  335. self.sync_all()
  336. self.nodes[1].generate(1)
  337. self.sync_all()
  338. oldBalance = self.nodes[1].getbalance()
  339. inputs = []
  340. outputs = {self.nodes[1].getnewaddress():1.1}
  341. rawtx = self.nodes[2].createrawtransaction(inputs, outputs)
  342. fundedTx = self.nodes[2].fundrawtransaction(rawtx)
  343. signedTx = self.nodes[2].signrawtransaction(fundedTx['hex'])
  344. txId = self.nodes[2].sendrawtransaction(signedTx['hex'])
  345. self.sync_all()
  346. self.nodes[1].generate(1)
  347. self.sync_all()
  348. # make sure funds are received at node1
  349. assert_equal(oldBalance+Decimal('1.10000000'), self.nodes[1].getbalance())
  350. ############################################################
  351. # locked wallet test
  352. self.stop_node(0)
  353. self.nodes[1].node_encrypt_wallet("test")
  354. self.stop_node(2)
  355. self.stop_node(3)
  356. self.start_nodes()
  357. # This test is not meant to test fee estimation and we'd like
  358. # to be sure all txs are sent at a consistent desired feerate
  359. for node in self.nodes:
  360. node.settxfee(min_relay_tx_fee)
  361. connect_nodes_bi(self.nodes,0,1)
  362. connect_nodes_bi(self.nodes,1,2)
  363. connect_nodes_bi(self.nodes,0,2)
  364. connect_nodes_bi(self.nodes,0,3)
  365. self.sync_all()
  366. # drain the keypool
  367. self.nodes[1].getnewaddress()
  368. self.nodes[1].getrawchangeaddress()
  369. inputs = []
  370. outputs = {self.nodes[0].getnewaddress():1.1}
  371. rawtx = self.nodes[1].createrawtransaction(inputs, outputs)
  372. # fund a transaction that requires a new key for the change output
  373. # creating the key must be impossible because the wallet is locked
  374. assert_raises_rpc_error(-4, "Keypool ran out, please call keypoolrefill first", self.nodes[1].fundrawtransaction, rawtx)
  375. #refill the keypool
  376. self.nodes[1].walletpassphrase("test", 100)
  377. self.nodes[1].keypoolrefill(8) #need to refill the keypool to get an internal change address
  378. self.nodes[1].walletlock()
  379. assert_raises_rpc_error(-13, "walletpassphrase", self.nodes[1].sendtoaddress, self.nodes[0].getnewaddress(), 1.2)
  380. oldBalance = self.nodes[0].getbalance()
  381. inputs = []
  382. outputs = {self.nodes[0].getnewaddress():1.1}
  383. rawtx = self.nodes[1].createrawtransaction(inputs, outputs)
  384. fundedTx = self.nodes[1].fundrawtransaction(rawtx)
  385. #now we need to unlock
  386. self.nodes[1].walletpassphrase("test", 600)
  387. signedTx = self.nodes[1].signrawtransaction(fundedTx['hex'])
  388. txId = self.nodes[1].sendrawtransaction(signedTx['hex'])
  389. self.nodes[1].generate(1)
  390. self.sync_all()
  391. # make sure funds are received at node1
  392. assert_equal(oldBalance+Decimal('51.10000000'), self.nodes[0].getbalance())
  393. ###############################################
  394. # multiple (~19) inputs tx test | Compare fee #
  395. ###############################################
  396. #empty node1, send some small coins from node0 to node1
  397. self.nodes[1].sendtoaddress(self.nodes[0].getnewaddress(), self.nodes[1].getbalance(), "", "", True)
  398. self.sync_all()
  399. self.nodes[0].generate(1)
  400. self.sync_all()
  401. for i in range(0,20):
  402. self.nodes[0].sendtoaddress(self.nodes[1].getnewaddress(), 0.01)
  403. self.nodes[0].generate(1)
  404. self.sync_all()
  405. #fund a tx with ~20 small inputs
  406. inputs = []
  407. outputs = {self.nodes[0].getnewaddress():0.15,self.nodes[0].getnewaddress():0.04}
  408. rawtx = self.nodes[1].createrawtransaction(inputs, outputs)
  409. fundedTx = self.nodes[1].fundrawtransaction(rawtx)
  410. #create same transaction over sendtoaddress
  411. txId = self.nodes[1].sendmany("", outputs)
  412. signedFee = self.nodes[1].getrawmempool(True)[txId]['fee']
  413. #compare fee
  414. feeDelta = Decimal(fundedTx['fee']) - Decimal(signedFee)
  415. assert(feeDelta >= 0 and feeDelta <= feeTolerance*19) #~19 inputs
  416. #############################################
  417. # multiple (~19) inputs tx test | sign/send #
  418. #############################################
  419. #again, empty node1, send some small coins from node0 to node1
  420. self.nodes[1].sendtoaddress(self.nodes[0].getnewaddress(), self.nodes[1].getbalance(), "", "", True)
  421. self.sync_all()
  422. self.nodes[0].generate(1)
  423. self.sync_all()
  424. for i in range(0,20):
  425. self.nodes[0].sendtoaddress(self.nodes[1].getnewaddress(), 0.01)
  426. self.nodes[0].generate(1)
  427. self.sync_all()
  428. #fund a tx with ~20 small inputs
  429. oldBalance = self.nodes[0].getbalance()
  430. inputs = []
  431. outputs = {self.nodes[0].getnewaddress():0.15,self.nodes[0].getnewaddress():0.04}
  432. rawtx = self.nodes[1].createrawtransaction(inputs, outputs)
  433. fundedTx = self.nodes[1].fundrawtransaction(rawtx)
  434. fundedAndSignedTx = self.nodes[1].signrawtransaction(fundedTx['hex'])
  435. txId = self.nodes[1].sendrawtransaction(fundedAndSignedTx['hex'])
  436. self.sync_all()
  437. self.nodes[0].generate(1)
  438. self.sync_all()
  439. assert_equal(oldBalance+Decimal('50.19000000'), self.nodes[0].getbalance()) #0.19+block reward
  440. #####################################################
  441. # test fundrawtransaction with OP_RETURN and no vin #
  442. #####################################################
  443. rawtx = "0100000000010000000000000000066a047465737400000000"
  444. dec_tx = self.nodes[2].decoderawtransaction(rawtx)
  445. assert_equal(len(dec_tx['vin']), 0)
  446. assert_equal(len(dec_tx['vout']), 1)
  447. rawtxfund = self.nodes[2].fundrawtransaction(rawtx)
  448. dec_tx = self.nodes[2].decoderawtransaction(rawtxfund['hex'])
  449. assert_greater_than(len(dec_tx['vin']), 0) # at least one vin
  450. assert_equal(len(dec_tx['vout']), 2) # one change output added
  451. ##################################################
  452. # test a fundrawtransaction using only watchonly #
  453. ##################################################
  454. inputs = []
  455. outputs = {self.nodes[2].getnewaddress() : watchonly_amount / 2}
  456. rawtx = self.nodes[3].createrawtransaction(inputs, outputs)
  457. result = self.nodes[3].fundrawtransaction(rawtx, {'includeWatching': True })
  458. res_dec = self.nodes[0].decoderawtransaction(result["hex"])
  459. assert_equal(len(res_dec["vin"]), 1)
  460. assert_equal(res_dec["vin"][0]["txid"], watchonly_txid)
  461. assert("fee" in result.keys())
  462. assert_greater_than(result["changepos"], -1)
  463. ###############################################################
  464. # test fundrawtransaction using the entirety of watched funds #
  465. ###############################################################
  466. inputs = []
  467. outputs = {self.nodes[2].getnewaddress() : watchonly_amount}
  468. rawtx = self.nodes[3].createrawtransaction(inputs, outputs)
  469. # Backward compatibility test (2nd param is includeWatching)
  470. result = self.nodes[3].fundrawtransaction(rawtx, True)
  471. res_dec = self.nodes[0].decoderawtransaction(result["hex"])
  472. assert_equal(len(res_dec["vin"]), 2)
  473. assert(res_dec["vin"][0]["txid"] == watchonly_txid or res_dec["vin"][1]["txid"] == watchonly_txid)
  474. assert_greater_than(result["fee"], 0)
  475. assert_greater_than(result["changepos"], -1)
  476. assert_equal(result["fee"] + res_dec["vout"][result["changepos"]]["value"], watchonly_amount / 10)
  477. signedtx = self.nodes[3].signrawtransaction(result["hex"])
  478. assert(not signedtx["complete"])
  479. signedtx = self.nodes[0].signrawtransaction(signedtx["hex"])
  480. assert(signedtx["complete"])
  481. self.nodes[0].sendrawtransaction(signedtx["hex"])
  482. self.nodes[0].generate(1)
  483. self.sync_all()
  484. #######################
  485. # Test feeRate option #
  486. #######################
  487. # Make sure there is exactly one input so coin selection can't skew the result
  488. assert_equal(len(self.nodes[3].listunspent(1)), 1)
  489. inputs = []
  490. outputs = {self.nodes[3].getnewaddress() : 1}
  491. rawtx = self.nodes[3].createrawtransaction(inputs, outputs)
  492. result = self.nodes[3].fundrawtransaction(rawtx) # uses min_relay_tx_fee (set by settxfee)
  493. result2 = self.nodes[3].fundrawtransaction(rawtx, {"feeRate": 2*min_relay_tx_fee})
  494. result3 = self.nodes[3].fundrawtransaction(rawtx, {"feeRate": 10*min_relay_tx_fee})
  495. result_fee_rate = result['fee'] * 1000 / count_bytes(result['hex'])
  496. assert_fee_amount(result2['fee'], count_bytes(result2['hex']), 2 * result_fee_rate)
  497. assert_fee_amount(result3['fee'], count_bytes(result3['hex']), 10 * result_fee_rate)
  498. ################################
  499. # Test no address reuse occurs #
  500. ################################
  501. result3 = self.nodes[3].fundrawtransaction(rawtx)
  502. res_dec = self.nodes[0].decoderawtransaction(result3["hex"])
  503. changeaddress = ""
  504. for out in res_dec['vout']:
  505. if out['value'] > 1.0:
  506. changeaddress += out['scriptPubKey']['addresses'][0]
  507. assert(changeaddress != "")
  508. nextaddr = self.nodes[3].getnewaddress()
  509. # Now the change address key should be removed from the keypool
  510. assert(changeaddress != nextaddr)
  511. ######################################
  512. # Test subtractFeeFromOutputs option #
  513. ######################################
  514. # Make sure there is exactly one input so coin selection can't skew the result
  515. assert_equal(len(self.nodes[3].listunspent(1)), 1)
  516. inputs = []
  517. outputs = {self.nodes[2].getnewaddress(): 1}
  518. rawtx = self.nodes[3].createrawtransaction(inputs, outputs)
  519. result = [self.nodes[3].fundrawtransaction(rawtx), # uses min_relay_tx_fee (set by settxfee)
  520. self.nodes[3].fundrawtransaction(rawtx, {"subtractFeeFromOutputs": []}), # empty subtraction list
  521. self.nodes[3].fundrawtransaction(rawtx, {"subtractFeeFromOutputs": [0]}), # uses min_relay_tx_fee (set by settxfee)
  522. self.nodes[3].fundrawtransaction(rawtx, {"feeRate": 2*min_relay_tx_fee}),
  523. self.nodes[3].fundrawtransaction(rawtx, {"feeRate": 2*min_relay_tx_fee, "subtractFeeFromOutputs": [0]})]
  524. dec_tx = [self.nodes[3].decoderawtransaction(tx['hex']) for tx in result]
  525. output = [d['vout'][1 - r['changepos']]['value'] for d, r in zip(dec_tx, result)]
  526. change = [d['vout'][r['changepos']]['value'] for d, r in zip(dec_tx, result)]
  527. assert_equal(result[0]['fee'], result[1]['fee'], result[2]['fee'])
  528. assert_equal(result[3]['fee'], result[4]['fee'])
  529. assert_equal(change[0], change[1])
  530. assert_equal(output[0], output[1])
  531. assert_equal(output[0], output[2] + result[2]['fee'])
  532. assert_equal(change[0] + result[0]['fee'], change[2])
  533. assert_equal(output[3], output[4] + result[4]['fee'])
  534. assert_equal(change[3] + result[3]['fee'], change[4])
  535. inputs = []
  536. outputs = {self.nodes[2].getnewaddress(): value for value in (1.0, 1.1, 1.2, 1.3)}
  537. rawtx = self.nodes[3].createrawtransaction(inputs, outputs)
  538. result = [self.nodes[3].fundrawtransaction(rawtx),
  539. # split the fee between outputs 0, 2, and 3, but not output 1
  540. self.nodes[3].fundrawtransaction(rawtx, {"subtractFeeFromOutputs": [0, 2, 3]})]
  541. dec_tx = [self.nodes[3].decoderawtransaction(result[0]['hex']),
  542. self.nodes[3].decoderawtransaction(result[1]['hex'])]
  543. # Nested list of non-change output amounts for each transaction
  544. output = [[out['value'] for i, out in enumerate(d['vout']) if i != r['changepos']]
  545. for d, r in zip(dec_tx, result)]
  546. # List of differences in output amounts between normal and subtractFee transactions
  547. share = [o0 - o1 for o0, o1 in zip(output[0], output[1])]
  548. # output 1 is the same in both transactions
  549. assert_equal(share[1], 0)
  550. # the other 3 outputs are smaller as a result of subtractFeeFromOutputs
  551. assert_greater_than(share[0], 0)
  552. assert_greater_than(share[2], 0)
  553. assert_greater_than(share[3], 0)
  554. # outputs 2 and 3 take the same share of the fee
  555. assert_equal(share[2], share[3])
  556. # output 0 takes at least as much share of the fee, and no more than 2 satoshis more, than outputs 2 and 3
  557. assert_greater_than_or_equal(share[0], share[2])
  558. assert_greater_than_or_equal(share[2] + Decimal(2e-8), share[0])
  559. # the fee is the same in both transactions
  560. assert_equal(result[0]['fee'], result[1]['fee'])
  561. # the total subtracted from the outputs is equal to the fee
  562. assert_equal(share[0] + share[2] + share[3], result[0]['fee'])
  563. if __name__ == '__main__':
  564. RawTransactionsTest().main()