[wallet] Remove redundant initialization
[bitcoinplatinum.git] / qa / rpc-tests / wallet.py
blobddeac657e51a82c556ec385166446c25cfef5681
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, split=False):
24 self.nodes = 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.is_network_split=False
29 self.sync_all()
31 def run_test (self):
33 # Check that there's no UTXO on none of the nodes
34 assert_equal(len(self.nodes[0].listunspent()), 0)
35 assert_equal(len(self.nodes[1].listunspent()), 0)
36 assert_equal(len(self.nodes[2].listunspent()), 0)
38 print("Mining blocks...")
40 self.nodes[0].generate(1)
42 walletinfo = self.nodes[0].getwalletinfo()
43 assert_equal(walletinfo['immature_balance'], 50)
44 assert_equal(walletinfo['balance'], 0)
46 self.sync_all()
47 self.nodes[1].generate(101)
48 self.sync_all()
50 assert_equal(self.nodes[0].getbalance(), 50)
51 assert_equal(self.nodes[1].getbalance(), 50)
52 assert_equal(self.nodes[2].getbalance(), 0)
54 # Check that only first and second nodes have UTXOs
55 assert_equal(len(self.nodes[0].listunspent()), 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 self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(), 11)
61 self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(), 10)
63 walletinfo = self.nodes[0].getwalletinfo()
64 assert_equal(walletinfo['immature_balance'], 0)
66 # Have node0 mine a block, thus it will collect its own fee.
67 self.nodes[0].generate(1)
68 self.sync_all()
70 # Exercise locking of unspent outputs
71 unspent_0 = self.nodes[2].listunspent()[0]
72 unspent_0 = {"txid": unspent_0["txid"], "vout": unspent_0["vout"]}
73 self.nodes[2].lockunspent(False, [unspent_0])
74 assert_raises_message(JSONRPCException, "Insufficient funds", self.nodes[2].sendtoaddress, self.nodes[2].getnewaddress(), 20)
75 assert_equal([unspent_0], self.nodes[2].listlockunspent())
76 self.nodes[2].lockunspent(True, [unspent_0])
77 assert_equal(len(self.nodes[2].listlockunspent()), 0)
79 # Have node1 generate 100 blocks (so node0 can recover the fee)
80 self.nodes[1].generate(100)
81 self.sync_all()
83 # node0 should end up with 100 btc in block rewards plus fees, but
84 # minus the 21 plus fees sent to node2
85 assert_equal(self.nodes[0].getbalance(), 100-21)
86 assert_equal(self.nodes[2].getbalance(), 21)
88 # Node0 should have two unspent outputs.
89 # Create a couple of transactions to send them to node2, submit them through
90 # node1, and make sure both node0 and node2 pick them up properly:
91 node0utxos = self.nodes[0].listunspent(1)
92 assert_equal(len(node0utxos), 2)
94 # create both transactions
95 txns_to_send = []
96 for utxo in node0utxos:
97 inputs = []
98 outputs = {}
99 inputs.append({ "txid" : utxo["txid"], "vout" : utxo["vout"]})
100 outputs[self.nodes[2].getnewaddress("from1")] = utxo["amount"] - 3
101 raw_tx = self.nodes[0].createrawtransaction(inputs, outputs)
102 txns_to_send.append(self.nodes[0].signrawtransaction(raw_tx))
104 # Have node 1 (miner) send the transactions
105 self.nodes[1].sendrawtransaction(txns_to_send[0]["hex"], True)
106 self.nodes[1].sendrawtransaction(txns_to_send[1]["hex"], True)
108 # Have node1 mine a block to confirm transactions:
109 self.nodes[1].generate(1)
110 self.sync_all()
112 assert_equal(self.nodes[0].getbalance(), 0)
113 assert_equal(self.nodes[2].getbalance(), 94)
114 assert_equal(self.nodes[2].getbalance("from1"), 94-21)
116 # Send 10 BTC normal
117 address = self.nodes[0].getnewaddress("test")
118 fee_per_byte = Decimal('0.001') / 1000
119 self.nodes[2].settxfee(fee_per_byte * 1000)
120 txid = self.nodes[2].sendtoaddress(address, 10, "", "", False)
121 self.nodes[2].generate(1)
122 self.sync_all()
123 node_2_bal = self.check_fee_amount(self.nodes[2].getbalance(), Decimal('84'), fee_per_byte, count_bytes(self.nodes[2].getrawtransaction(txid)))
124 assert_equal(self.nodes[0].getbalance(), Decimal('10'))
126 # Send 10 BTC with subtract fee from amount
127 txid = self.nodes[2].sendtoaddress(address, 10, "", "", True)
128 self.nodes[2].generate(1)
129 self.sync_all()
130 node_2_bal -= Decimal('10')
131 assert_equal(self.nodes[2].getbalance(), node_2_bal)
132 node_0_bal = self.check_fee_amount(self.nodes[0].getbalance(), Decimal('20'), fee_per_byte, count_bytes(self.nodes[2].getrawtransaction(txid)))
134 # Sendmany 10 BTC
135 txid = self.nodes[2].sendmany('from1', {address: 10}, 0, "", [])
136 self.nodes[2].generate(1)
137 self.sync_all()
138 node_0_bal += Decimal('10')
139 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)))
140 assert_equal(self.nodes[0].getbalance(), node_0_bal)
142 # Sendmany 10 BTC with subtract fee from amount
143 txid = self.nodes[2].sendmany('from1', {address: 10}, 0, "", [address])
144 self.nodes[2].generate(1)
145 self.sync_all()
146 node_2_bal -= Decimal('10')
147 assert_equal(self.nodes[2].getbalance(), node_2_bal)
148 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)))
150 # Test ResendWalletTransactions:
151 # Create a couple of transactions, then start up a fourth
152 # node (nodes[3]) and ask nodes[0] to rebroadcast.
153 # EXPECT: nodes[3] should have those transactions in its mempool.
154 txid1 = self.nodes[0].sendtoaddress(self.nodes[1].getnewaddress(), 1)
155 txid2 = self.nodes[1].sendtoaddress(self.nodes[0].getnewaddress(), 1)
156 sync_mempools(self.nodes)
158 self.nodes.append(start_node(3, self.options.tmpdir, self.extra_args[3]))
159 connect_nodes_bi(self.nodes, 0, 3)
160 sync_blocks(self.nodes)
162 relayed = self.nodes[0].resendwallettransactions()
163 assert_equal(set(relayed), {txid1, txid2})
164 sync_mempools(self.nodes)
166 assert(txid1 in self.nodes[3].getrawmempool())
168 # Exercise balance rpcs
169 assert_equal(self.nodes[0].getwalletinfo()["unconfirmed_balance"], 1)
170 assert_equal(self.nodes[0].getunconfirmedbalance(), 1)
172 #check if we can list zero value tx as available coins
173 #1. create rawtx
174 #2. hex-changed one output to 0.0
175 #3. sign and send
176 #4. check if recipient (node0) can list the zero value tx
177 usp = self.nodes[1].listunspent()
178 inputs = [{"txid":usp[0]['txid'], "vout":usp[0]['vout']}]
179 outputs = {self.nodes[1].getnewaddress(): 49.998, self.nodes[0].getnewaddress(): 11.11}
181 rawTx = self.nodes[1].createrawtransaction(inputs, outputs).replace("c0833842", "00000000") #replace 11.11 with 0.0 (int32)
182 decRawTx = self.nodes[1].decoderawtransaction(rawTx)
183 signedRawTx = self.nodes[1].signrawtransaction(rawTx)
184 decRawTx = self.nodes[1].decoderawtransaction(signedRawTx['hex'])
185 zeroValueTxid= decRawTx['txid']
186 sendResp = self.nodes[1].sendrawtransaction(signedRawTx['hex'])
188 self.sync_all()
189 self.nodes[1].generate(1) #mine a block
190 self.sync_all()
192 unspentTxs = self.nodes[0].listunspent() #zero value tx must be in listunspents output
193 found = False
194 for uTx in unspentTxs:
195 if uTx['txid'] == zeroValueTxid:
196 found = True
197 assert_equal(uTx['amount'], Decimal('0'))
198 assert(found)
200 #do some -walletbroadcast tests
201 stop_nodes(self.nodes)
202 self.nodes = start_nodes(3, self.options.tmpdir, [["-walletbroadcast=0"],["-walletbroadcast=0"],["-walletbroadcast=0"]])
203 connect_nodes_bi(self.nodes,0,1)
204 connect_nodes_bi(self.nodes,1,2)
205 connect_nodes_bi(self.nodes,0,2)
206 self.sync_all()
208 txIdNotBroadcasted = self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(), 2)
209 txObjNotBroadcasted = self.nodes[0].gettransaction(txIdNotBroadcasted)
210 self.nodes[1].generate(1) #mine a block, tx should not be in there
211 self.sync_all()
212 assert_equal(self.nodes[2].getbalance(), node_2_bal) #should not be changed because tx was not broadcasted
214 #now broadcast from another node, mine a block, sync, and check the balance
215 self.nodes[1].sendrawtransaction(txObjNotBroadcasted['hex'])
216 self.nodes[1].generate(1)
217 self.sync_all()
218 node_2_bal += 2
219 txObjNotBroadcasted = self.nodes[0].gettransaction(txIdNotBroadcasted)
220 assert_equal(self.nodes[2].getbalance(), node_2_bal)
222 #create another tx
223 txIdNotBroadcasted = self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(), 2)
225 #restart the nodes with -walletbroadcast=1
226 stop_nodes(self.nodes)
227 self.nodes = start_nodes(3, self.options.tmpdir)
228 connect_nodes_bi(self.nodes,0,1)
229 connect_nodes_bi(self.nodes,1,2)
230 connect_nodes_bi(self.nodes,0,2)
231 sync_blocks(self.nodes)
233 self.nodes[0].generate(1)
234 sync_blocks(self.nodes)
235 node_2_bal += 2
237 #tx should be added to balance because after restarting the nodes tx should be broadcastet
238 assert_equal(self.nodes[2].getbalance(), node_2_bal)
240 #send a tx with value in a string (PR#6380 +)
241 txId = self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(), "2")
242 txObj = self.nodes[0].gettransaction(txId)
243 assert_equal(txObj['amount'], Decimal('-2'))
245 txId = self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(), "0.0001")
246 txObj = self.nodes[0].gettransaction(txId)
247 assert_equal(txObj['amount'], Decimal('-0.0001'))
249 #check if JSON parser can handle scientific notation in strings
250 txId = self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(), "1e-4")
251 txObj = self.nodes[0].gettransaction(txId)
252 assert_equal(txObj['amount'], Decimal('-0.0001'))
254 try:
255 txId = self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(), "1f-4")
256 except JSONRPCException as e:
257 assert("Invalid amount" in e.error['message'])
258 else:
259 raise AssertionError("Must not parse invalid amounts")
262 try:
263 self.nodes[0].generate("2")
264 raise AssertionError("Must not accept strings as numeric")
265 except JSONRPCException as e:
266 assert("not an integer" in e.error['message'])
268 # Import address and private key to check correct behavior of spendable unspents
269 # 1. Send some coins to generate new UTXO
270 address_to_import = self.nodes[2].getnewaddress()
271 txid = self.nodes[0].sendtoaddress(address_to_import, 1)
272 self.nodes[0].generate(1)
273 self.sync_all()
275 # 2. Import address from node2 to node1
276 self.nodes[1].importaddress(address_to_import)
278 # 3. Validate that the imported address is watch-only on node1
279 assert(self.nodes[1].validateaddress(address_to_import)["iswatchonly"])
281 # 4. Check that the unspents after import are not spendable
282 assert_array_result(self.nodes[1].listunspent(),
283 {"address": address_to_import},
284 {"spendable": False})
286 # 5. Import private key of the previously imported address on node1
287 priv_key = self.nodes[2].dumpprivkey(address_to_import)
288 self.nodes[1].importprivkey(priv_key)
290 # 6. Check that the unspents are now spendable on node1
291 assert_array_result(self.nodes[1].listunspent(),
292 {"address": address_to_import},
293 {"spendable": True})
295 # Mine a block from node0 to an address from node1
296 cbAddr = self.nodes[1].getnewaddress()
297 blkHash = self.nodes[0].generatetoaddress(1, cbAddr)[0]
298 cbTxId = self.nodes[0].getblock(blkHash)['tx'][0]
299 self.sync_all()
301 # Check that the txid and balance is found by node1
302 self.nodes[1].gettransaction(cbTxId)
304 # check if wallet or blockchain maintenance changes the balance
305 self.sync_all()
306 blocks = self.nodes[0].generate(2)
307 self.sync_all()
308 balance_nodes = [self.nodes[i].getbalance() for i in range(3)]
309 block_count = self.nodes[0].getblockcount()
311 # Check modes:
312 # - True: unicode escaped as \u....
313 # - False: unicode directly as UTF-8
314 for mode in [True, False]:
315 self.nodes[0].ensure_ascii = mode
316 # unicode check: Basic Multilingual Plane, Supplementary Plane respectively
317 for s in [u'рыба', u'𝅘𝅥𝅯']:
318 addr = self.nodes[0].getaccountaddress(s)
319 label = self.nodes[0].getaccount(addr)
320 assert_equal(label, s)
321 assert(s in self.nodes[0].listaccounts().keys())
322 self.nodes[0].ensure_ascii = True # restore to default
324 # maintenance tests
325 maintenance = [
326 '-rescan',
327 '-reindex',
328 '-zapwallettxes=1',
329 '-zapwallettxes=2',
330 # disabled until issue is fixed: https://github.com/bitcoin/bitcoin/issues/7463
331 # '-salvagewallet',
333 chainlimit = 6
334 for m in maintenance:
335 print("check " + m)
336 stop_nodes(self.nodes)
337 # set lower ancestor limit for later
338 self.nodes = start_nodes(3, self.options.tmpdir, [[m, "-limitancestorcount="+str(chainlimit)]] * 3)
339 while m == '-reindex' and [block_count] * 3 != [self.nodes[i].getblockcount() for i in range(3)]:
340 # reindex will leave rpc warm up "early"; Wait for it to finish
341 time.sleep(0.1)
342 assert_equal(balance_nodes, [self.nodes[i].getbalance() for i in range(3)])
344 # Exercise listsinceblock with the last two blocks
345 coinbase_tx_1 = self.nodes[0].listsinceblock(blocks[0])
346 assert_equal(coinbase_tx_1["lastblock"], blocks[1])
347 assert_equal(len(coinbase_tx_1["transactions"]), 1)
348 assert_equal(coinbase_tx_1["transactions"][0]["blockhash"], blocks[1])
349 assert_equal(len(self.nodes[0].listsinceblock(blocks[1])["transactions"]), 0)
351 # ==Check that wallet prefers to use coins that don't exceed mempool limits =====
353 # Get all non-zero utxos together
354 chain_addrs = [self.nodes[0].getnewaddress(), self.nodes[0].getnewaddress()]
355 singletxid = self.nodes[0].sendtoaddress(chain_addrs[0], self.nodes[0].getbalance(), "", "", True)
356 self.nodes[0].generate(1)
357 node0_balance = self.nodes[0].getbalance()
358 # Split into two chains
359 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')})
360 signedtx = self.nodes[0].signrawtransaction(rawtx)
361 singletxid = self.nodes[0].sendrawtransaction(signedtx["hex"])
362 self.nodes[0].generate(1)
364 # Make a long chain of unconfirmed payments without hitting mempool limit
365 # Each tx we make leaves only one output of change on a chain 1 longer
366 # Since the amount to send is always much less than the outputs, we only ever need one output
367 # So we should be able to generate exactly chainlimit txs for each original output
368 sending_addr = self.nodes[1].getnewaddress()
369 txid_list = []
370 for i in range(chainlimit*2):
371 txid_list.append(self.nodes[0].sendtoaddress(sending_addr, Decimal('0.0001')))
372 assert_equal(self.nodes[0].getmempoolinfo()['size'], chainlimit*2)
373 assert_equal(len(txid_list), chainlimit*2)
375 # Without walletrejectlongchains, we will still generate a txid
376 # The tx will be stored in the wallet but not accepted to the mempool
377 extra_txid = self.nodes[0].sendtoaddress(sending_addr, Decimal('0.0001'))
378 assert(extra_txid not in self.nodes[0].getrawmempool())
379 assert(extra_txid in [tx["txid"] for tx in self.nodes[0].listtransactions()])
380 self.nodes[0].abandontransaction(extra_txid)
381 total_txs = len(self.nodes[0].listtransactions("*",99999))
383 # Try with walletrejectlongchains
384 # Double chain limit but require combining inputs, so we pass SelectCoinsMinConf
385 stop_node(self.nodes[0],0)
386 self.nodes[0] = start_node(0, self.options.tmpdir, ["-walletrejectlongchains", "-limitancestorcount="+str(2*chainlimit)])
388 # wait for loadmempool
389 timeout = 10
390 while (timeout > 0 and len(self.nodes[0].getrawmempool()) < chainlimit*2):
391 time.sleep(0.5)
392 timeout -= 0.5
393 assert_equal(len(self.nodes[0].getrawmempool()), chainlimit*2)
395 node0_balance = self.nodes[0].getbalance()
396 # With walletrejectlongchains we will not create the tx and store it in our wallet.
397 assert_raises_message(JSONRPCException, "mempool chain", self.nodes[0].sendtoaddress, sending_addr, node0_balance - Decimal('0.01'))
399 # Verify nothing new in wallet
400 assert_equal(total_txs, len(self.nodes[0].listtransactions("*",99999)))
402 if __name__ == '__main__':
403 WalletTest().main()