Merge #11726: Cleanups + nit fixes for walletdir PR
[bitcoinplatinum.git] / test / functional / wallet.py
blobdb60df18ede0089fd36ccc851c288550bf0c6bfa
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 wallet."""
6 from test_framework.test_framework import BitcoinTestFramework
7 from test_framework.util import *
9 class WalletTest(BitcoinTestFramework):
10 def set_test_params(self):
11 self.num_nodes = 4
12 self.setup_clean_chain = True
14 def setup_network(self):
15 self.add_nodes(4)
16 self.start_node(0)
17 self.start_node(1)
18 self.start_node(2)
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 self.sync_all([self.nodes[0:3]])
24 def check_fee_amount(self, curr_balance, balance_with_fee, fee_per_byte, tx_size):
25 """Return curr_balance after asserting the fee was in range"""
26 fee = balance_with_fee - curr_balance
27 assert_fee_amount(fee, tx_size, fee_per_byte * 1000)
28 return curr_balance
30 def run_test(self):
31 # Check that there's no UTXO on none of the nodes
32 assert_equal(len(self.nodes[0].listunspent()), 0)
33 assert_equal(len(self.nodes[1].listunspent()), 0)
34 assert_equal(len(self.nodes[2].listunspent()), 0)
36 self.log.info("Mining blocks...")
38 self.nodes[0].generate(1)
40 walletinfo = self.nodes[0].getwalletinfo()
41 assert_equal(walletinfo['immature_balance'], 50)
42 assert_equal(walletinfo['balance'], 0)
44 self.sync_all([self.nodes[0:3]])
45 self.nodes[1].generate(101)
46 self.sync_all([self.nodes[0:3]])
48 assert_equal(self.nodes[0].getbalance(), 50)
49 assert_equal(self.nodes[1].getbalance(), 50)
50 assert_equal(self.nodes[2].getbalance(), 0)
52 # Check that only first and second nodes have UTXOs
53 utxos = self.nodes[0].listunspent()
54 assert_equal(len(utxos), 1)
55 assert_equal(len(self.nodes[1].listunspent()), 1)
56 assert_equal(len(self.nodes[2].listunspent()), 0)
58 self.log.info("test gettxout")
59 confirmed_txid, confirmed_index = utxos[0]["txid"], utxos[0]["vout"]
60 # First, outputs that are unspent both in the chain and in the
61 # mempool should appear with or without include_mempool
62 txout = self.nodes[0].gettxout(txid=confirmed_txid, n=confirmed_index, include_mempool=False)
63 assert_equal(txout['value'], 50)
64 txout = self.nodes[0].gettxout(txid=confirmed_txid, n=confirmed_index, include_mempool=True)
65 assert_equal(txout['value'], 50)
67 # Send 21 BTC from 0 to 2 using sendtoaddress call.
68 # Locked memory should use at least 32 bytes to sign each transaction
69 self.log.info("test getmemoryinfo")
70 memory_before = self.nodes[0].getmemoryinfo()
71 self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(), 11)
72 mempool_txid = self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(), 10)
73 memory_after = self.nodes[0].getmemoryinfo()
74 assert(memory_before['locked']['used'] + 64 <= memory_after['locked']['used'])
76 self.log.info("test gettxout (second part)")
77 # utxo spent in mempool should be visible if you exclude mempool
78 # but invisible if you include mempool
79 txout = self.nodes[0].gettxout(confirmed_txid, confirmed_index, False)
80 assert_equal(txout['value'], 50)
81 txout = self.nodes[0].gettxout(confirmed_txid, confirmed_index, True)
82 assert txout is None
83 # new utxo from mempool should be invisible if you exclude mempool
84 # but visible if you include mempool
85 txout = self.nodes[0].gettxout(mempool_txid, 0, False)
86 assert txout is None
87 txout1 = self.nodes[0].gettxout(mempool_txid, 0, True)
88 txout2 = self.nodes[0].gettxout(mempool_txid, 1, True)
89 # note the mempool tx will have randomly assigned indices
90 # but 10 will go to node2 and the rest will go to node0
91 balance = self.nodes[0].getbalance()
92 assert_equal(set([txout1['value'], txout2['value']]), set([10, balance]))
93 walletinfo = self.nodes[0].getwalletinfo()
94 assert_equal(walletinfo['immature_balance'], 0)
96 # Have node0 mine a block, thus it will collect its own fee.
97 self.nodes[0].generate(1)
98 self.sync_all([self.nodes[0:3]])
100 # Exercise locking of unspent outputs
101 unspent_0 = self.nodes[2].listunspent()[0]
102 unspent_0 = {"txid": unspent_0["txid"], "vout": unspent_0["vout"]}
103 assert_raises_rpc_error(-8, "Invalid parameter, expected locked output", self.nodes[2].lockunspent, True, [unspent_0])
104 self.nodes[2].lockunspent(False, [unspent_0])
105 assert_raises_rpc_error(-8, "Invalid parameter, output already locked", self.nodes[2].lockunspent, False, [unspent_0])
106 assert_raises_rpc_error(-4, "Insufficient funds", self.nodes[2].sendtoaddress, self.nodes[2].getnewaddress(), 20)
107 assert_equal([unspent_0], self.nodes[2].listlockunspent())
108 self.nodes[2].lockunspent(True, [unspent_0])
109 assert_equal(len(self.nodes[2].listlockunspent()), 0)
110 assert_raises_rpc_error(-8, "Invalid parameter, unknown transaction",
111 self.nodes[2].lockunspent, False,
112 [{"txid": "0000000000000000000000000000000000", "vout": 0}])
113 assert_raises_rpc_error(-8, "Invalid parameter, vout index out of bounds",
114 self.nodes[2].lockunspent, False,
115 [{"txid": unspent_0["txid"], "vout": 999}])
117 # Have node1 generate 100 blocks (so node0 can recover the fee)
118 self.nodes[1].generate(100)
119 self.sync_all([self.nodes[0:3]])
121 # node0 should end up with 100 btc in block rewards plus fees, but
122 # minus the 21 plus fees sent to node2
123 assert_equal(self.nodes[0].getbalance(), 100-21)
124 assert_equal(self.nodes[2].getbalance(), 21)
126 # Node0 should have two unspent outputs.
127 # Create a couple of transactions to send them to node2, submit them through
128 # node1, and make sure both node0 and node2 pick them up properly:
129 node0utxos = self.nodes[0].listunspent(1)
130 assert_equal(len(node0utxos), 2)
132 # create both transactions
133 txns_to_send = []
134 for utxo in node0utxos:
135 inputs = []
136 outputs = {}
137 inputs.append({ "txid" : utxo["txid"], "vout" : utxo["vout"]})
138 outputs[self.nodes[2].getnewaddress("from1")] = utxo["amount"] - 3
139 raw_tx = self.nodes[0].createrawtransaction(inputs, outputs)
140 txns_to_send.append(self.nodes[0].signrawtransaction(raw_tx))
142 # Have node 1 (miner) send the transactions
143 self.nodes[1].sendrawtransaction(txns_to_send[0]["hex"], True)
144 self.nodes[1].sendrawtransaction(txns_to_send[1]["hex"], True)
146 # Have node1 mine a block to confirm transactions:
147 self.nodes[1].generate(1)
148 self.sync_all([self.nodes[0:3]])
150 assert_equal(self.nodes[0].getbalance(), 0)
151 assert_equal(self.nodes[2].getbalance(), 94)
152 assert_equal(self.nodes[2].getbalance("from1"), 94-21)
154 # Verify that a spent output cannot be locked anymore
155 spent_0 = {"txid": node0utxos[0]["txid"], "vout": node0utxos[0]["vout"]}
156 assert_raises_rpc_error(-8, "Invalid parameter, expected unspent output", self.nodes[0].lockunspent, False, [spent_0])
158 # Send 10 BTC normal
159 address = self.nodes[0].getnewaddress("test")
160 fee_per_byte = Decimal('0.001') / 1000
161 self.nodes[2].settxfee(fee_per_byte * 1000)
162 txid = self.nodes[2].sendtoaddress(address, 10, "", "", False)
163 self.nodes[2].generate(1)
164 self.sync_all([self.nodes[0:3]])
165 node_2_bal = self.check_fee_amount(self.nodes[2].getbalance(), Decimal('84'), fee_per_byte, count_bytes(self.nodes[2].getrawtransaction(txid)))
166 assert_equal(self.nodes[0].getbalance(), Decimal('10'))
168 # Send 10 BTC with subtract fee from amount
169 txid = self.nodes[2].sendtoaddress(address, 10, "", "", True)
170 self.nodes[2].generate(1)
171 self.sync_all([self.nodes[0:3]])
172 node_2_bal -= Decimal('10')
173 assert_equal(self.nodes[2].getbalance(), node_2_bal)
174 node_0_bal = self.check_fee_amount(self.nodes[0].getbalance(), Decimal('20'), fee_per_byte, count_bytes(self.nodes[2].getrawtransaction(txid)))
176 # Sendmany 10 BTC
177 txid = self.nodes[2].sendmany('from1', {address: 10}, 0, "", [])
178 self.nodes[2].generate(1)
179 self.sync_all([self.nodes[0:3]])
180 node_0_bal += Decimal('10')
181 node_2_bal = self.check_fee_amount(self.nodes[2].getbalance(), node_2_bal - Decimal('10'), fee_per_byte, count_bytes(self.nodes[2].getrawtransaction(txid)))
182 assert_equal(self.nodes[0].getbalance(), node_0_bal)
184 # Sendmany 10 BTC with subtract fee from amount
185 txid = self.nodes[2].sendmany('from1', {address: 10}, 0, "", [address])
186 self.nodes[2].generate(1)
187 self.sync_all([self.nodes[0:3]])
188 node_2_bal -= Decimal('10')
189 assert_equal(self.nodes[2].getbalance(), node_2_bal)
190 node_0_bal = self.check_fee_amount(self.nodes[0].getbalance(), node_0_bal + Decimal('10'), fee_per_byte, count_bytes(self.nodes[2].getrawtransaction(txid)))
192 # Test ResendWalletTransactions:
193 # Create a couple of transactions, then start up a fourth
194 # node (nodes[3]) and ask nodes[0] to rebroadcast.
195 # EXPECT: nodes[3] should have those transactions in its mempool.
196 txid1 = self.nodes[0].sendtoaddress(self.nodes[1].getnewaddress(), 1)
197 txid2 = self.nodes[1].sendtoaddress(self.nodes[0].getnewaddress(), 1)
198 sync_mempools(self.nodes[0:2])
200 self.start_node(3)
201 connect_nodes_bi(self.nodes, 0, 3)
202 sync_blocks(self.nodes)
204 relayed = self.nodes[0].resendwallettransactions()
205 assert_equal(set(relayed), {txid1, txid2})
206 sync_mempools(self.nodes)
208 assert(txid1 in self.nodes[3].getrawmempool())
210 # Exercise balance rpcs
211 assert_equal(self.nodes[0].getwalletinfo()["unconfirmed_balance"], 1)
212 assert_equal(self.nodes[0].getunconfirmedbalance(), 1)
214 #check if we can list zero value tx as available coins
215 #1. create rawtx
216 #2. hex-changed one output to 0.0
217 #3. sign and send
218 #4. check if recipient (node0) can list the zero value tx
219 usp = self.nodes[1].listunspent()
220 inputs = [{"txid":usp[0]['txid'], "vout":usp[0]['vout']}]
221 outputs = {self.nodes[1].getnewaddress(): 49.998, self.nodes[0].getnewaddress(): 11.11}
223 rawTx = self.nodes[1].createrawtransaction(inputs, outputs).replace("c0833842", "00000000") #replace 11.11 with 0.0 (int32)
224 decRawTx = self.nodes[1].decoderawtransaction(rawTx)
225 signedRawTx = self.nodes[1].signrawtransaction(rawTx)
226 decRawTx = self.nodes[1].decoderawtransaction(signedRawTx['hex'])
227 zeroValueTxid= decRawTx['txid']
228 self.nodes[1].sendrawtransaction(signedRawTx['hex'])
230 self.sync_all()
231 self.nodes[1].generate(1) #mine a block
232 self.sync_all()
234 unspentTxs = self.nodes[0].listunspent() #zero value tx must be in listunspents output
235 found = False
236 for uTx in unspentTxs:
237 if uTx['txid'] == zeroValueTxid:
238 found = True
239 assert_equal(uTx['amount'], Decimal('0'))
240 assert(found)
242 #do some -walletbroadcast tests
243 self.stop_nodes()
244 self.start_node(0, ["-walletbroadcast=0"])
245 self.start_node(1, ["-walletbroadcast=0"])
246 self.start_node(2, ["-walletbroadcast=0"])
247 connect_nodes_bi(self.nodes,0,1)
248 connect_nodes_bi(self.nodes,1,2)
249 connect_nodes_bi(self.nodes,0,2)
250 self.sync_all([self.nodes[0:3]])
252 txIdNotBroadcasted = self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(), 2)
253 txObjNotBroadcasted = self.nodes[0].gettransaction(txIdNotBroadcasted)
254 self.nodes[1].generate(1) #mine a block, tx should not be in there
255 self.sync_all([self.nodes[0:3]])
256 assert_equal(self.nodes[2].getbalance(), node_2_bal) #should not be changed because tx was not broadcasted
258 #now broadcast from another node, mine a block, sync, and check the balance
259 self.nodes[1].sendrawtransaction(txObjNotBroadcasted['hex'])
260 self.nodes[1].generate(1)
261 self.sync_all([self.nodes[0:3]])
262 node_2_bal += 2
263 txObjNotBroadcasted = self.nodes[0].gettransaction(txIdNotBroadcasted)
264 assert_equal(self.nodes[2].getbalance(), node_2_bal)
266 #create another tx
267 txIdNotBroadcasted = self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(), 2)
269 #restart the nodes with -walletbroadcast=1
270 self.stop_nodes()
271 self.start_node(0)
272 self.start_node(1)
273 self.start_node(2)
274 connect_nodes_bi(self.nodes,0,1)
275 connect_nodes_bi(self.nodes,1,2)
276 connect_nodes_bi(self.nodes,0,2)
277 sync_blocks(self.nodes[0:3])
279 self.nodes[0].generate(1)
280 sync_blocks(self.nodes[0:3])
281 node_2_bal += 2
283 #tx should be added to balance because after restarting the nodes tx should be broadcastet
284 assert_equal(self.nodes[2].getbalance(), node_2_bal)
286 #send a tx with value in a string (PR#6380 +)
287 txId = self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(), "2")
288 txObj = self.nodes[0].gettransaction(txId)
289 assert_equal(txObj['amount'], Decimal('-2'))
291 txId = self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(), "0.0001")
292 txObj = self.nodes[0].gettransaction(txId)
293 assert_equal(txObj['amount'], Decimal('-0.0001'))
295 #check if JSON parser can handle scientific notation in strings
296 txId = self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(), "1e-4")
297 txObj = self.nodes[0].gettransaction(txId)
298 assert_equal(txObj['amount'], Decimal('-0.0001'))
300 # This will raise an exception because the amount type is wrong
301 assert_raises_rpc_error(-3, "Invalid amount", self.nodes[0].sendtoaddress, self.nodes[2].getnewaddress(), "1f-4")
303 # This will raise an exception since generate does not accept a string
304 assert_raises_rpc_error(-1, "not an integer", self.nodes[0].generate, "2")
306 # Import address and private key to check correct behavior of spendable unspents
307 # 1. Send some coins to generate new UTXO
308 address_to_import = self.nodes[2].getnewaddress()
309 txid = self.nodes[0].sendtoaddress(address_to_import, 1)
310 self.nodes[0].generate(1)
311 self.sync_all([self.nodes[0:3]])
313 # 2. Import address from node2 to node1
314 self.nodes[1].importaddress(address_to_import)
316 # 3. Validate that the imported address is watch-only on node1
317 assert(self.nodes[1].validateaddress(address_to_import)["iswatchonly"])
319 # 4. Check that the unspents after import are not spendable
320 assert_array_result(self.nodes[1].listunspent(),
321 {"address": address_to_import},
322 {"spendable": False})
324 # 5. Import private key of the previously imported address on node1
325 priv_key = self.nodes[2].dumpprivkey(address_to_import)
326 self.nodes[1].importprivkey(priv_key)
328 # 6. Check that the unspents are now spendable on node1
329 assert_array_result(self.nodes[1].listunspent(),
330 {"address": address_to_import},
331 {"spendable": True})
333 # Mine a block from node0 to an address from node1
334 cbAddr = self.nodes[1].getnewaddress()
335 blkHash = self.nodes[0].generatetoaddress(1, cbAddr)[0]
336 cbTxId = self.nodes[0].getblock(blkHash)['tx'][0]
337 self.sync_all([self.nodes[0:3]])
339 # Check that the txid and balance is found by node1
340 self.nodes[1].gettransaction(cbTxId)
342 # check if wallet or blockchain maintenance changes the balance
343 self.sync_all([self.nodes[0:3]])
344 blocks = self.nodes[0].generate(2)
345 self.sync_all([self.nodes[0:3]])
346 balance_nodes = [self.nodes[i].getbalance() for i in range(3)]
347 block_count = self.nodes[0].getblockcount()
349 # Check modes:
350 # - True: unicode escaped as \u....
351 # - False: unicode directly as UTF-8
352 for mode in [True, False]:
353 self.nodes[0].ensure_ascii = mode
354 # unicode check: Basic Multilingual Plane, Supplementary Plane respectively
355 for s in [u'рыба', u'𝅘𝅥𝅯']:
356 addr = self.nodes[0].getaccountaddress(s)
357 label = self.nodes[0].getaccount(addr)
358 assert_equal(label, s)
359 assert(s in self.nodes[0].listaccounts().keys())
360 self.nodes[0].ensure_ascii = True # restore to default
362 # maintenance tests
363 maintenance = [
364 '-rescan',
365 '-reindex',
366 '-zapwallettxes=1',
367 '-zapwallettxes=2',
368 # disabled until issue is fixed: https://github.com/bitcoin/bitcoin/issues/7463
369 # '-salvagewallet',
371 chainlimit = 6
372 for m in maintenance:
373 self.log.info("check " + m)
374 self.stop_nodes()
375 # set lower ancestor limit for later
376 self.start_node(0, [m, "-limitancestorcount="+str(chainlimit)])
377 self.start_node(1, [m, "-limitancestorcount="+str(chainlimit)])
378 self.start_node(2, [m, "-limitancestorcount="+str(chainlimit)])
379 while m == '-reindex' and [block_count] * 3 != [self.nodes[i].getblockcount() for i in range(3)]:
380 # reindex will leave rpc warm up "early"; Wait for it to finish
381 time.sleep(0.1)
382 assert_equal(balance_nodes, [self.nodes[i].getbalance() for i in range(3)])
384 # Exercise listsinceblock with the last two blocks
385 coinbase_tx_1 = self.nodes[0].listsinceblock(blocks[0])
386 assert_equal(coinbase_tx_1["lastblock"], blocks[1])
387 assert_equal(len(coinbase_tx_1["transactions"]), 1)
388 assert_equal(coinbase_tx_1["transactions"][0]["blockhash"], blocks[1])
389 assert_equal(len(self.nodes[0].listsinceblock(blocks[1])["transactions"]), 0)
391 # ==Check that wallet prefers to use coins that don't exceed mempool limits =====
393 # Get all non-zero utxos together
394 chain_addrs = [self.nodes[0].getnewaddress(), self.nodes[0].getnewaddress()]
395 singletxid = self.nodes[0].sendtoaddress(chain_addrs[0], self.nodes[0].getbalance(), "", "", True)
396 self.nodes[0].generate(1)
397 node0_balance = self.nodes[0].getbalance()
398 # Split into two chains
399 rawtx = self.nodes[0].createrawtransaction([{"txid":singletxid, "vout":0}], {chain_addrs[0]:node0_balance/2-Decimal('0.01'), chain_addrs[1]:node0_balance/2-Decimal('0.01')})
400 signedtx = self.nodes[0].signrawtransaction(rawtx)
401 singletxid = self.nodes[0].sendrawtransaction(signedtx["hex"])
402 self.nodes[0].generate(1)
404 # Make a long chain of unconfirmed payments without hitting mempool limit
405 # Each tx we make leaves only one output of change on a chain 1 longer
406 # Since the amount to send is always much less than the outputs, we only ever need one output
407 # So we should be able to generate exactly chainlimit txs for each original output
408 sending_addr = self.nodes[1].getnewaddress()
409 txid_list = []
410 for i in range(chainlimit*2):
411 txid_list.append(self.nodes[0].sendtoaddress(sending_addr, Decimal('0.0001')))
412 assert_equal(self.nodes[0].getmempoolinfo()['size'], chainlimit*2)
413 assert_equal(len(txid_list), chainlimit*2)
415 # Without walletrejectlongchains, we will still generate a txid
416 # The tx will be stored in the wallet but not accepted to the mempool
417 extra_txid = self.nodes[0].sendtoaddress(sending_addr, Decimal('0.0001'))
418 assert(extra_txid not in self.nodes[0].getrawmempool())
419 assert(extra_txid in [tx["txid"] for tx in self.nodes[0].listtransactions()])
420 self.nodes[0].abandontransaction(extra_txid)
421 total_txs = len(self.nodes[0].listtransactions("*",99999))
423 # Try with walletrejectlongchains
424 # Double chain limit but require combining inputs, so we pass SelectCoinsMinConf
425 self.stop_node(0)
426 self.start_node(0, extra_args=["-walletrejectlongchains", "-limitancestorcount="+str(2*chainlimit)])
428 # wait for loadmempool
429 timeout = 10
430 while (timeout > 0 and len(self.nodes[0].getrawmempool()) < chainlimit*2):
431 time.sleep(0.5)
432 timeout -= 0.5
433 assert_equal(len(self.nodes[0].getrawmempool()), chainlimit*2)
435 node0_balance = self.nodes[0].getbalance()
436 # With walletrejectlongchains we will not create the tx and store it in our wallet.
437 assert_raises_rpc_error(-4, "Transaction has too long of a mempool chain", self.nodes[0].sendtoaddress, sending_addr, node0_balance - Decimal('0.01'))
439 # Verify nothing new in wallet
440 assert_equal(total_txs, len(self.nodes[0].listtransactions("*",99999)))
442 if __name__ == '__main__':
443 WalletTest().main()