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