Merge #11340: Trivial: Fix validation comments
[bitcoinplatinum.git] / test / functional / wallet.py
bloba643684c7fe5cb68f1405b70e1e3ff1b048a8d8c
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 self.nodes[2].lockunspent(False, [unspent_0])
104 assert_raises_jsonrpc(-4, "Insufficient funds", self.nodes[2].sendtoaddress, self.nodes[2].getnewaddress(), 20)
105 assert_equal([unspent_0], self.nodes[2].listlockunspent())
106 self.nodes[2].lockunspent(True, [unspent_0])
107 assert_equal(len(self.nodes[2].listlockunspent()), 0)
109 # Have node1 generate 100 blocks (so node0 can recover the fee)
110 self.nodes[1].generate(100)
111 self.sync_all([self.nodes[0:3]])
113 # node0 should end up with 100 btc in block rewards plus fees, but
114 # minus the 21 plus fees sent to node2
115 assert_equal(self.nodes[0].getbalance(), 100-21)
116 assert_equal(self.nodes[2].getbalance(), 21)
118 # Node0 should have two unspent outputs.
119 # Create a couple of transactions to send them to node2, submit them through
120 # node1, and make sure both node0 and node2 pick them up properly:
121 node0utxos = self.nodes[0].listunspent(1)
122 assert_equal(len(node0utxos), 2)
124 # create both transactions
125 txns_to_send = []
126 for utxo in node0utxos:
127 inputs = []
128 outputs = {}
129 inputs.append({ "txid" : utxo["txid"], "vout" : utxo["vout"]})
130 outputs[self.nodes[2].getnewaddress("from1")] = utxo["amount"] - 3
131 raw_tx = self.nodes[0].createrawtransaction(inputs, outputs)
132 txns_to_send.append(self.nodes[0].signrawtransaction(raw_tx))
134 # Have node 1 (miner) send the transactions
135 self.nodes[1].sendrawtransaction(txns_to_send[0]["hex"], True)
136 self.nodes[1].sendrawtransaction(txns_to_send[1]["hex"], True)
138 # Have node1 mine a block to confirm transactions:
139 self.nodes[1].generate(1)
140 self.sync_all([self.nodes[0:3]])
142 assert_equal(self.nodes[0].getbalance(), 0)
143 assert_equal(self.nodes[2].getbalance(), 94)
144 assert_equal(self.nodes[2].getbalance("from1"), 94-21)
146 # Send 10 BTC normal
147 address = self.nodes[0].getnewaddress("test")
148 fee_per_byte = Decimal('0.001') / 1000
149 self.nodes[2].settxfee(fee_per_byte * 1000)
150 txid = self.nodes[2].sendtoaddress(address, 10, "", "", False)
151 self.nodes[2].generate(1)
152 self.sync_all([self.nodes[0:3]])
153 node_2_bal = self.check_fee_amount(self.nodes[2].getbalance(), Decimal('84'), fee_per_byte, count_bytes(self.nodes[2].getrawtransaction(txid)))
154 assert_equal(self.nodes[0].getbalance(), Decimal('10'))
156 # Send 10 BTC with subtract fee from amount
157 txid = self.nodes[2].sendtoaddress(address, 10, "", "", True)
158 self.nodes[2].generate(1)
159 self.sync_all([self.nodes[0:3]])
160 node_2_bal -= Decimal('10')
161 assert_equal(self.nodes[2].getbalance(), node_2_bal)
162 node_0_bal = self.check_fee_amount(self.nodes[0].getbalance(), Decimal('20'), fee_per_byte, count_bytes(self.nodes[2].getrawtransaction(txid)))
164 # Sendmany 10 BTC
165 txid = self.nodes[2].sendmany('from1', {address: 10}, 0, "", [])
166 self.nodes[2].generate(1)
167 self.sync_all([self.nodes[0:3]])
168 node_0_bal += Decimal('10')
169 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)))
170 assert_equal(self.nodes[0].getbalance(), node_0_bal)
172 # Sendmany 10 BTC with subtract fee from amount
173 txid = self.nodes[2].sendmany('from1', {address: 10}, 0, "", [address])
174 self.nodes[2].generate(1)
175 self.sync_all([self.nodes[0:3]])
176 node_2_bal -= Decimal('10')
177 assert_equal(self.nodes[2].getbalance(), node_2_bal)
178 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)))
180 # Test ResendWalletTransactions:
181 # Create a couple of transactions, then start up a fourth
182 # node (nodes[3]) and ask nodes[0] to rebroadcast.
183 # EXPECT: nodes[3] should have those transactions in its mempool.
184 txid1 = self.nodes[0].sendtoaddress(self.nodes[1].getnewaddress(), 1)
185 txid2 = self.nodes[1].sendtoaddress(self.nodes[0].getnewaddress(), 1)
186 sync_mempools(self.nodes[0:2])
188 self.start_node(3)
189 connect_nodes_bi(self.nodes, 0, 3)
190 sync_blocks(self.nodes)
192 relayed = self.nodes[0].resendwallettransactions()
193 assert_equal(set(relayed), {txid1, txid2})
194 sync_mempools(self.nodes)
196 assert(txid1 in self.nodes[3].getrawmempool())
198 # Exercise balance rpcs
199 assert_equal(self.nodes[0].getwalletinfo()["unconfirmed_balance"], 1)
200 assert_equal(self.nodes[0].getunconfirmedbalance(), 1)
202 #check if we can list zero value tx as available coins
203 #1. create rawtx
204 #2. hex-changed one output to 0.0
205 #3. sign and send
206 #4. check if recipient (node0) can list the zero value tx
207 usp = self.nodes[1].listunspent()
208 inputs = [{"txid":usp[0]['txid'], "vout":usp[0]['vout']}]
209 outputs = {self.nodes[1].getnewaddress(): 49.998, self.nodes[0].getnewaddress(): 11.11}
211 rawTx = self.nodes[1].createrawtransaction(inputs, outputs).replace("c0833842", "00000000") #replace 11.11 with 0.0 (int32)
212 decRawTx = self.nodes[1].decoderawtransaction(rawTx)
213 signedRawTx = self.nodes[1].signrawtransaction(rawTx)
214 decRawTx = self.nodes[1].decoderawtransaction(signedRawTx['hex'])
215 zeroValueTxid= decRawTx['txid']
216 self.nodes[1].sendrawtransaction(signedRawTx['hex'])
218 self.sync_all()
219 self.nodes[1].generate(1) #mine a block
220 self.sync_all()
222 unspentTxs = self.nodes[0].listunspent() #zero value tx must be in listunspents output
223 found = False
224 for uTx in unspentTxs:
225 if uTx['txid'] == zeroValueTxid:
226 found = True
227 assert_equal(uTx['amount'], Decimal('0'))
228 assert(found)
230 #do some -walletbroadcast tests
231 self.stop_nodes()
232 self.start_node(0, ["-walletbroadcast=0"])
233 self.start_node(1, ["-walletbroadcast=0"])
234 self.start_node(2, ["-walletbroadcast=0"])
235 connect_nodes_bi(self.nodes,0,1)
236 connect_nodes_bi(self.nodes,1,2)
237 connect_nodes_bi(self.nodes,0,2)
238 self.sync_all([self.nodes[0:3]])
240 txIdNotBroadcasted = self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(), 2)
241 txObjNotBroadcasted = self.nodes[0].gettransaction(txIdNotBroadcasted)
242 self.nodes[1].generate(1) #mine a block, tx should not be in there
243 self.sync_all([self.nodes[0:3]])
244 assert_equal(self.nodes[2].getbalance(), node_2_bal) #should not be changed because tx was not broadcasted
246 #now broadcast from another node, mine a block, sync, and check the balance
247 self.nodes[1].sendrawtransaction(txObjNotBroadcasted['hex'])
248 self.nodes[1].generate(1)
249 self.sync_all([self.nodes[0:3]])
250 node_2_bal += 2
251 txObjNotBroadcasted = self.nodes[0].gettransaction(txIdNotBroadcasted)
252 assert_equal(self.nodes[2].getbalance(), node_2_bal)
254 #create another tx
255 txIdNotBroadcasted = self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(), 2)
257 #restart the nodes with -walletbroadcast=1
258 self.stop_nodes()
259 self.start_node(0)
260 self.start_node(1)
261 self.start_node(2)
262 connect_nodes_bi(self.nodes,0,1)
263 connect_nodes_bi(self.nodes,1,2)
264 connect_nodes_bi(self.nodes,0,2)
265 sync_blocks(self.nodes[0:3])
267 self.nodes[0].generate(1)
268 sync_blocks(self.nodes[0:3])
269 node_2_bal += 2
271 #tx should be added to balance because after restarting the nodes tx should be broadcastet
272 assert_equal(self.nodes[2].getbalance(), node_2_bal)
274 #send a tx with value in a string (PR#6380 +)
275 txId = self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(), "2")
276 txObj = self.nodes[0].gettransaction(txId)
277 assert_equal(txObj['amount'], Decimal('-2'))
279 txId = self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(), "0.0001")
280 txObj = self.nodes[0].gettransaction(txId)
281 assert_equal(txObj['amount'], Decimal('-0.0001'))
283 #check if JSON parser can handle scientific notation in strings
284 txId = self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(), "1e-4")
285 txObj = self.nodes[0].gettransaction(txId)
286 assert_equal(txObj['amount'], Decimal('-0.0001'))
288 # This will raise an exception because the amount type is wrong
289 assert_raises_jsonrpc(-3, "Invalid amount", self.nodes[0].sendtoaddress, self.nodes[2].getnewaddress(), "1f-4")
291 # This will raise an exception since generate does not accept a string
292 assert_raises_jsonrpc(-1, "not an integer", self.nodes[0].generate, "2")
294 # Import address and private key to check correct behavior of spendable unspents
295 # 1. Send some coins to generate new UTXO
296 address_to_import = self.nodes[2].getnewaddress()
297 txid = self.nodes[0].sendtoaddress(address_to_import, 1)
298 self.nodes[0].generate(1)
299 self.sync_all([self.nodes[0:3]])
301 # 2. Import address from node2 to node1
302 self.nodes[1].importaddress(address_to_import)
304 # 3. Validate that the imported address is watch-only on node1
305 assert(self.nodes[1].validateaddress(address_to_import)["iswatchonly"])
307 # 4. Check that the unspents after import are not spendable
308 assert_array_result(self.nodes[1].listunspent(),
309 {"address": address_to_import},
310 {"spendable": False})
312 # 5. Import private key of the previously imported address on node1
313 priv_key = self.nodes[2].dumpprivkey(address_to_import)
314 self.nodes[1].importprivkey(priv_key)
316 # 6. Check that the unspents are now spendable on node1
317 assert_array_result(self.nodes[1].listunspent(),
318 {"address": address_to_import},
319 {"spendable": True})
321 # Mine a block from node0 to an address from node1
322 cbAddr = self.nodes[1].getnewaddress()
323 blkHash = self.nodes[0].generatetoaddress(1, cbAddr)[0]
324 cbTxId = self.nodes[0].getblock(blkHash)['tx'][0]
325 self.sync_all([self.nodes[0:3]])
327 # Check that the txid and balance is found by node1
328 self.nodes[1].gettransaction(cbTxId)
330 # check if wallet or blockchain maintenance changes the balance
331 self.sync_all([self.nodes[0:3]])
332 blocks = self.nodes[0].generate(2)
333 self.sync_all([self.nodes[0:3]])
334 balance_nodes = [self.nodes[i].getbalance() for i in range(3)]
335 block_count = self.nodes[0].getblockcount()
337 # Check modes:
338 # - True: unicode escaped as \u....
339 # - False: unicode directly as UTF-8
340 for mode in [True, False]:
341 self.nodes[0].ensure_ascii = mode
342 # unicode check: Basic Multilingual Plane, Supplementary Plane respectively
343 for s in [u'рыба', u'𝅘𝅥𝅯']:
344 addr = self.nodes[0].getaccountaddress(s)
345 label = self.nodes[0].getaccount(addr)
346 assert_equal(label, s)
347 assert(s in self.nodes[0].listaccounts().keys())
348 self.nodes[0].ensure_ascii = True # restore to default
350 # maintenance tests
351 maintenance = [
352 '-rescan',
353 '-reindex',
354 '-zapwallettxes=1',
355 '-zapwallettxes=2',
356 # disabled until issue is fixed: https://github.com/bitcoin/bitcoin/issues/7463
357 # '-salvagewallet',
359 chainlimit = 6
360 for m in maintenance:
361 self.log.info("check " + m)
362 self.stop_nodes()
363 # set lower ancestor limit for later
364 self.start_node(0, [m, "-limitancestorcount="+str(chainlimit)])
365 self.start_node(1, [m, "-limitancestorcount="+str(chainlimit)])
366 self.start_node(2, [m, "-limitancestorcount="+str(chainlimit)])
367 while m == '-reindex' and [block_count] * 3 != [self.nodes[i].getblockcount() for i in range(3)]:
368 # reindex will leave rpc warm up "early"; Wait for it to finish
369 time.sleep(0.1)
370 assert_equal(balance_nodes, [self.nodes[i].getbalance() for i in range(3)])
372 # Exercise listsinceblock with the last two blocks
373 coinbase_tx_1 = self.nodes[0].listsinceblock(blocks[0])
374 assert_equal(coinbase_tx_1["lastblock"], blocks[1])
375 assert_equal(len(coinbase_tx_1["transactions"]), 1)
376 assert_equal(coinbase_tx_1["transactions"][0]["blockhash"], blocks[1])
377 assert_equal(len(self.nodes[0].listsinceblock(blocks[1])["transactions"]), 0)
379 # ==Check that wallet prefers to use coins that don't exceed mempool limits =====
381 # Get all non-zero utxos together
382 chain_addrs = [self.nodes[0].getnewaddress(), self.nodes[0].getnewaddress()]
383 singletxid = self.nodes[0].sendtoaddress(chain_addrs[0], self.nodes[0].getbalance(), "", "", True)
384 self.nodes[0].generate(1)
385 node0_balance = self.nodes[0].getbalance()
386 # Split into two chains
387 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')})
388 signedtx = self.nodes[0].signrawtransaction(rawtx)
389 singletxid = self.nodes[0].sendrawtransaction(signedtx["hex"])
390 self.nodes[0].generate(1)
392 # Make a long chain of unconfirmed payments without hitting mempool limit
393 # Each tx we make leaves only one output of change on a chain 1 longer
394 # Since the amount to send is always much less than the outputs, we only ever need one output
395 # So we should be able to generate exactly chainlimit txs for each original output
396 sending_addr = self.nodes[1].getnewaddress()
397 txid_list = []
398 for i in range(chainlimit*2):
399 txid_list.append(self.nodes[0].sendtoaddress(sending_addr, Decimal('0.0001')))
400 assert_equal(self.nodes[0].getmempoolinfo()['size'], chainlimit*2)
401 assert_equal(len(txid_list), chainlimit*2)
403 # Without walletrejectlongchains, we will still generate a txid
404 # The tx will be stored in the wallet but not accepted to the mempool
405 extra_txid = self.nodes[0].sendtoaddress(sending_addr, Decimal('0.0001'))
406 assert(extra_txid not in self.nodes[0].getrawmempool())
407 assert(extra_txid in [tx["txid"] for tx in self.nodes[0].listtransactions()])
408 self.nodes[0].abandontransaction(extra_txid)
409 total_txs = len(self.nodes[0].listtransactions("*",99999))
411 # Try with walletrejectlongchains
412 # Double chain limit but require combining inputs, so we pass SelectCoinsMinConf
413 self.stop_node(0)
414 self.start_node(0, extra_args=["-walletrejectlongchains", "-limitancestorcount="+str(2*chainlimit)])
416 # wait for loadmempool
417 timeout = 10
418 while (timeout > 0 and len(self.nodes[0].getrawmempool()) < chainlimit*2):
419 time.sleep(0.5)
420 timeout -= 0.5
421 assert_equal(len(self.nodes[0].getrawmempool()), chainlimit*2)
423 node0_balance = self.nodes[0].getbalance()
424 # With walletrejectlongchains we will not create the tx and store it in our wallet.
425 assert_raises_jsonrpc(-4, "Transaction has too long of a mempool chain", self.nodes[0].sendtoaddress, sending_addr, node0_balance - Decimal('0.01'))
427 # Verify nothing new in wallet
428 assert_equal(total_txs, len(self.nodes[0].listtransactions("*",99999)))
430 if __name__ == '__main__':
431 WalletTest().main()