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.
6 from test_framework
.test_framework
import BitcoinTestFramework
7 from test_framework
.util
import *
9 class WalletTest(BitcoinTestFramework
):
10 def set_test_params(self
):
12 self
.setup_clean_chain
= True
13 self
.extra_args
= [['-usehd={:d}'.format(i
%2==0)] for i
in range(4)]
15 def setup_network(self
):
16 self
.add_nodes(4, self
.extra_args
)
20 connect_nodes_bi(self
.nodes
,0,1)
21 connect_nodes_bi(self
.nodes
,1,2)
22 connect_nodes_bi(self
.nodes
,0,2)
23 self
.sync_all([self
.nodes
[0:3]])
25 def check_fee_amount(self
, curr_balance
, balance_with_fee
, fee_per_byte
, tx_size
):
26 """Return curr_balance after asserting the fee was in range"""
27 fee
= balance_with_fee
- curr_balance
28 assert_fee_amount(fee
, tx_size
, fee_per_byte
* 1000)
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([self
.nodes
[0:3]])
46 self
.nodes
[1].generate(101)
47 self
.sync_all([self
.nodes
[0:3]])
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 self
.log
.info("test gettxout")
60 confirmed_txid
, confirmed_index
= utxos
[0]["txid"], utxos
[0]["vout"]
61 # First, outputs that are unspent both in the chain and in the
62 # mempool should appear with or without include_mempool
63 txout
= self
.nodes
[0].gettxout(txid
=confirmed_txid
, n
=confirmed_index
, include_mempool
=False)
64 assert_equal(txout
['value'], 50)
65 txout
= self
.nodes
[0].gettxout(txid
=confirmed_txid
, n
=confirmed_index
, include_mempool
=True)
66 assert_equal(txout
['value'], 50)
68 # Send 21 BTC from 0 to 2 using sendtoaddress call.
69 # Locked memory should use at least 32 bytes to sign each transaction
70 self
.log
.info("test getmemoryinfo")
71 memory_before
= self
.nodes
[0].getmemoryinfo()
72 self
.nodes
[0].sendtoaddress(self
.nodes
[2].getnewaddress(), 11)
73 mempool_txid
= self
.nodes
[0].sendtoaddress(self
.nodes
[2].getnewaddress(), 10)
74 memory_after
= self
.nodes
[0].getmemoryinfo()
75 assert(memory_before
['locked']['used'] + 64 <= memory_after
['locked']['used'])
77 self
.log
.info("test gettxout (second part)")
78 # utxo spent in mempool should be visible if you exclude mempool
79 # but invisible if you include mempool
80 txout
= self
.nodes
[0].gettxout(confirmed_txid
, confirmed_index
, False)
81 assert_equal(txout
['value'], 50)
82 txout
= self
.nodes
[0].gettxout(confirmed_txid
, confirmed_index
, True)
84 # new utxo from mempool should be invisible if you exclude mempool
85 # but visible if you include mempool
86 txout
= self
.nodes
[0].gettxout(mempool_txid
, 0, False)
88 txout1
= self
.nodes
[0].gettxout(mempool_txid
, 0, True)
89 txout2
= self
.nodes
[0].gettxout(mempool_txid
, 1, True)
90 # note the mempool tx will have randomly assigned indices
91 # but 10 will go to node2 and the rest will go to node0
92 balance
= self
.nodes
[0].getbalance()
93 assert_equal(set([txout1
['value'], txout2
['value']]), set([10, balance
]))
94 walletinfo
= self
.nodes
[0].getwalletinfo()
95 assert_equal(walletinfo
['immature_balance'], 0)
97 # Have node0 mine a block, thus it will collect its own fee.
98 self
.nodes
[0].generate(1)
99 self
.sync_all([self
.nodes
[0:3]])
101 # Exercise locking of unspent outputs
102 unspent_0
= self
.nodes
[2].listunspent()[0]
103 unspent_0
= {"txid": unspent_0
["txid"], "vout": unspent_0
["vout"]}
104 self
.nodes
[2].lockunspent(False, [unspent_0
])
105 assert_raises_jsonrpc(-4, "Insufficient funds", self
.nodes
[2].sendtoaddress
, self
.nodes
[2].getnewaddress(), 20)
106 assert_equal([unspent_0
], self
.nodes
[2].listlockunspent())
107 self
.nodes
[2].lockunspent(True, [unspent_0
])
108 assert_equal(len(self
.nodes
[2].listlockunspent()), 0)
110 # Have node1 generate 100 blocks (so node0 can recover the fee)
111 self
.nodes
[1].generate(100)
112 self
.sync_all([self
.nodes
[0:3]])
114 # node0 should end up with 100 btc in block rewards plus fees, but
115 # minus the 21 plus fees sent to node2
116 assert_equal(self
.nodes
[0].getbalance(), 100-21)
117 assert_equal(self
.nodes
[2].getbalance(), 21)
119 # Node0 should have two unspent outputs.
120 # Create a couple of transactions to send them to node2, submit them through
121 # node1, and make sure both node0 and node2 pick them up properly:
122 node0utxos
= self
.nodes
[0].listunspent(1)
123 assert_equal(len(node0utxos
), 2)
125 # create both transactions
127 for utxo
in node0utxos
:
130 inputs
.append({ "txid" : utxo
["txid"], "vout" : utxo
["vout"]})
131 outputs
[self
.nodes
[2].getnewaddress("from1")] = utxo
["amount"] - 3
132 raw_tx
= self
.nodes
[0].createrawtransaction(inputs
, outputs
)
133 txns_to_send
.append(self
.nodes
[0].signrawtransaction(raw_tx
))
135 # Have node 1 (miner) send the transactions
136 self
.nodes
[1].sendrawtransaction(txns_to_send
[0]["hex"], True)
137 self
.nodes
[1].sendrawtransaction(txns_to_send
[1]["hex"], True)
139 # Have node1 mine a block to confirm transactions:
140 self
.nodes
[1].generate(1)
141 self
.sync_all([self
.nodes
[0:3]])
143 assert_equal(self
.nodes
[0].getbalance(), 0)
144 assert_equal(self
.nodes
[2].getbalance(), 94)
145 assert_equal(self
.nodes
[2].getbalance("from1"), 94-21)
148 address
= self
.nodes
[0].getnewaddress("test")
149 fee_per_byte
= Decimal('0.001') / 1000
150 self
.nodes
[2].settxfee(fee_per_byte
* 1000)
151 txid
= self
.nodes
[2].sendtoaddress(address
, 10, "", "", False)
152 self
.nodes
[2].generate(1)
153 self
.sync_all([self
.nodes
[0:3]])
154 node_2_bal
= self
.check_fee_amount(self
.nodes
[2].getbalance(), Decimal('84'), fee_per_byte
, count_bytes(self
.nodes
[2].getrawtransaction(txid
)))
155 assert_equal(self
.nodes
[0].getbalance(), Decimal('10'))
157 # Send 10 BTC with subtract fee from amount
158 txid
= self
.nodes
[2].sendtoaddress(address
, 10, "", "", True)
159 self
.nodes
[2].generate(1)
160 self
.sync_all([self
.nodes
[0:3]])
161 node_2_bal
-= Decimal('10')
162 assert_equal(self
.nodes
[2].getbalance(), node_2_bal
)
163 node_0_bal
= self
.check_fee_amount(self
.nodes
[0].getbalance(), Decimal('20'), fee_per_byte
, count_bytes(self
.nodes
[2].getrawtransaction(txid
)))
166 txid
= self
.nodes
[2].sendmany('from1', {address
: 10}, 0, "", [])
167 self
.nodes
[2].generate(1)
168 self
.sync_all([self
.nodes
[0:3]])
169 node_0_bal
+= Decimal('10')
170 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
)))
171 assert_equal(self
.nodes
[0].getbalance(), node_0_bal
)
173 # Sendmany 10 BTC with subtract fee from amount
174 txid
= self
.nodes
[2].sendmany('from1', {address
: 10}, 0, "", [address
])
175 self
.nodes
[2].generate(1)
176 self
.sync_all([self
.nodes
[0:3]])
177 node_2_bal
-= Decimal('10')
178 assert_equal(self
.nodes
[2].getbalance(), node_2_bal
)
179 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
)))
181 # Test ResendWalletTransactions:
182 # Create a couple of transactions, then start up a fourth
183 # node (nodes[3]) and ask nodes[0] to rebroadcast.
184 # EXPECT: nodes[3] should have those transactions in its mempool.
185 txid1
= self
.nodes
[0].sendtoaddress(self
.nodes
[1].getnewaddress(), 1)
186 txid2
= self
.nodes
[1].sendtoaddress(self
.nodes
[0].getnewaddress(), 1)
187 sync_mempools(self
.nodes
[0:2])
190 connect_nodes_bi(self
.nodes
, 0, 3)
191 sync_blocks(self
.nodes
)
193 relayed
= self
.nodes
[0].resendwallettransactions()
194 assert_equal(set(relayed
), {txid1
, txid2
})
195 sync_mempools(self
.nodes
)
197 assert(txid1
in self
.nodes
[3].getrawmempool())
199 # Exercise balance rpcs
200 assert_equal(self
.nodes
[0].getwalletinfo()["unconfirmed_balance"], 1)
201 assert_equal(self
.nodes
[0].getunconfirmedbalance(), 1)
203 #check if we can list zero value tx as available coins
205 #2. hex-changed one output to 0.0
207 #4. check if recipient (node0) can list the zero value tx
208 usp
= self
.nodes
[1].listunspent()
209 inputs
= [{"txid":usp
[0]['txid'], "vout":usp
[0]['vout']}]
210 outputs
= {self
.nodes
[1].getnewaddress(): 49.998, self
.nodes
[0].getnewaddress(): 11.11}
212 rawTx
= self
.nodes
[1].createrawtransaction(inputs
, outputs
).replace("c0833842", "00000000") #replace 11.11 with 0.0 (int32)
213 decRawTx
= self
.nodes
[1].decoderawtransaction(rawTx
)
214 signedRawTx
= self
.nodes
[1].signrawtransaction(rawTx
)
215 decRawTx
= self
.nodes
[1].decoderawtransaction(signedRawTx
['hex'])
216 zeroValueTxid
= decRawTx
['txid']
217 self
.nodes
[1].sendrawtransaction(signedRawTx
['hex'])
220 self
.nodes
[1].generate(1) #mine a block
223 unspentTxs
= self
.nodes
[0].listunspent() #zero value tx must be in listunspents output
225 for uTx
in unspentTxs
:
226 if uTx
['txid'] == zeroValueTxid
:
228 assert_equal(uTx
['amount'], Decimal('0'))
231 #do some -walletbroadcast tests
233 self
.start_node(0, ["-walletbroadcast=0"])
234 self
.start_node(1, ["-walletbroadcast=0"])
235 self
.start_node(2, ["-walletbroadcast=0"])
236 connect_nodes_bi(self
.nodes
,0,1)
237 connect_nodes_bi(self
.nodes
,1,2)
238 connect_nodes_bi(self
.nodes
,0,2)
239 self
.sync_all([self
.nodes
[0:3]])
241 txIdNotBroadcasted
= self
.nodes
[0].sendtoaddress(self
.nodes
[2].getnewaddress(), 2)
242 txObjNotBroadcasted
= self
.nodes
[0].gettransaction(txIdNotBroadcasted
)
243 self
.nodes
[1].generate(1) #mine a block, tx should not be in there
244 self
.sync_all([self
.nodes
[0:3]])
245 assert_equal(self
.nodes
[2].getbalance(), node_2_bal
) #should not be changed because tx was not broadcasted
247 #now broadcast from another node, mine a block, sync, and check the balance
248 self
.nodes
[1].sendrawtransaction(txObjNotBroadcasted
['hex'])
249 self
.nodes
[1].generate(1)
250 self
.sync_all([self
.nodes
[0:3]])
252 txObjNotBroadcasted
= self
.nodes
[0].gettransaction(txIdNotBroadcasted
)
253 assert_equal(self
.nodes
[2].getbalance(), node_2_bal
)
256 txIdNotBroadcasted
= self
.nodes
[0].sendtoaddress(self
.nodes
[2].getnewaddress(), 2)
258 #restart the nodes with -walletbroadcast=1
263 connect_nodes_bi(self
.nodes
,0,1)
264 connect_nodes_bi(self
.nodes
,1,2)
265 connect_nodes_bi(self
.nodes
,0,2)
266 sync_blocks(self
.nodes
[0:3])
268 self
.nodes
[0].generate(1)
269 sync_blocks(self
.nodes
[0:3])
272 #tx should be added to balance because after restarting the nodes tx should be broadcastet
273 assert_equal(self
.nodes
[2].getbalance(), node_2_bal
)
275 #send a tx with value in a string (PR#6380 +)
276 txId
= self
.nodes
[0].sendtoaddress(self
.nodes
[2].getnewaddress(), "2")
277 txObj
= self
.nodes
[0].gettransaction(txId
)
278 assert_equal(txObj
['amount'], Decimal('-2'))
280 txId
= self
.nodes
[0].sendtoaddress(self
.nodes
[2].getnewaddress(), "0.0001")
281 txObj
= self
.nodes
[0].gettransaction(txId
)
282 assert_equal(txObj
['amount'], Decimal('-0.0001'))
284 #check if JSON parser can handle scientific notation in strings
285 txId
= self
.nodes
[0].sendtoaddress(self
.nodes
[2].getnewaddress(), "1e-4")
286 txObj
= self
.nodes
[0].gettransaction(txId
)
287 assert_equal(txObj
['amount'], Decimal('-0.0001'))
289 # This will raise an exception because the amount type is wrong
290 assert_raises_jsonrpc(-3, "Invalid amount", self
.nodes
[0].sendtoaddress
, self
.nodes
[2].getnewaddress(), "1f-4")
292 # This will raise an exception since generate does not accept a string
293 assert_raises_jsonrpc(-1, "not an integer", self
.nodes
[0].generate
, "2")
295 # Import address and private key to check correct behavior of spendable unspents
296 # 1. Send some coins to generate new UTXO
297 address_to_import
= self
.nodes
[2].getnewaddress()
298 txid
= self
.nodes
[0].sendtoaddress(address_to_import
, 1)
299 self
.nodes
[0].generate(1)
300 self
.sync_all([self
.nodes
[0:3]])
302 # 2. Import address from node2 to node1
303 self
.nodes
[1].importaddress(address_to_import
)
305 # 3. Validate that the imported address is watch-only on node1
306 assert(self
.nodes
[1].validateaddress(address_to_import
)["iswatchonly"])
308 # 4. Check that the unspents after import are not spendable
309 assert_array_result(self
.nodes
[1].listunspent(),
310 {"address": address_to_import
},
311 {"spendable": False})
313 # 5. Import private key of the previously imported address on node1
314 priv_key
= self
.nodes
[2].dumpprivkey(address_to_import
)
315 self
.nodes
[1].importprivkey(priv_key
)
317 # 6. Check that the unspents are now spendable on node1
318 assert_array_result(self
.nodes
[1].listunspent(),
319 {"address": address_to_import
},
322 # Mine a block from node0 to an address from node1
323 cbAddr
= self
.nodes
[1].getnewaddress()
324 blkHash
= self
.nodes
[0].generatetoaddress(1, cbAddr
)[0]
325 cbTxId
= self
.nodes
[0].getblock(blkHash
)['tx'][0]
326 self
.sync_all([self
.nodes
[0:3]])
328 # Check that the txid and balance is found by node1
329 self
.nodes
[1].gettransaction(cbTxId
)
331 # check if wallet or blockchain maintenance changes the balance
332 self
.sync_all([self
.nodes
[0:3]])
333 blocks
= self
.nodes
[0].generate(2)
334 self
.sync_all([self
.nodes
[0:3]])
335 balance_nodes
= [self
.nodes
[i
].getbalance() for i
in range(3)]
336 block_count
= self
.nodes
[0].getblockcount()
339 # - True: unicode escaped as \u....
340 # - False: unicode directly as UTF-8
341 for mode
in [True, False]:
342 self
.nodes
[0].ensure_ascii
= mode
343 # unicode check: Basic Multilingual Plane, Supplementary Plane respectively
344 for s
in [u
'рыба', u
'𝅘𝅥𝅯']:
345 addr
= self
.nodes
[0].getaccountaddress(s
)
346 label
= self
.nodes
[0].getaccount(addr
)
347 assert_equal(label
, s
)
348 assert(s
in self
.nodes
[0].listaccounts().keys())
349 self
.nodes
[0].ensure_ascii
= True # restore to default
357 # disabled until issue is fixed: https://github.com/bitcoin/bitcoin/issues/7463
361 for m
in maintenance
:
362 self
.log
.info("check " + m
)
364 # set lower ancestor limit for later
365 self
.start_node(0, [m
, "-limitancestorcount="+str(chainlimit
)])
366 self
.start_node(1, [m
, "-limitancestorcount="+str(chainlimit
)])
367 self
.start_node(2, [m
, "-limitancestorcount="+str(chainlimit
)])
368 while m
== '-reindex' and [block_count
] * 3 != [self
.nodes
[i
].getblockcount() for i
in range(3)]:
369 # reindex will leave rpc warm up "early"; Wait for it to finish
371 assert_equal(balance_nodes
, [self
.nodes
[i
].getbalance() for i
in range(3)])
373 # Exercise listsinceblock with the last two blocks
374 coinbase_tx_1
= self
.nodes
[0].listsinceblock(blocks
[0])
375 assert_equal(coinbase_tx_1
["lastblock"], blocks
[1])
376 assert_equal(len(coinbase_tx_1
["transactions"]), 1)
377 assert_equal(coinbase_tx_1
["transactions"][0]["blockhash"], blocks
[1])
378 assert_equal(len(self
.nodes
[0].listsinceblock(blocks
[1])["transactions"]), 0)
380 # ==Check that wallet prefers to use coins that don't exceed mempool limits =====
382 # Get all non-zero utxos together
383 chain_addrs
= [self
.nodes
[0].getnewaddress(), self
.nodes
[0].getnewaddress()]
384 singletxid
= self
.nodes
[0].sendtoaddress(chain_addrs
[0], self
.nodes
[0].getbalance(), "", "", True)
385 self
.nodes
[0].generate(1)
386 node0_balance
= self
.nodes
[0].getbalance()
387 # Split into two chains
388 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')})
389 signedtx
= self
.nodes
[0].signrawtransaction(rawtx
)
390 singletxid
= self
.nodes
[0].sendrawtransaction(signedtx
["hex"])
391 self
.nodes
[0].generate(1)
393 # Make a long chain of unconfirmed payments without hitting mempool limit
394 # Each tx we make leaves only one output of change on a chain 1 longer
395 # Since the amount to send is always much less than the outputs, we only ever need one output
396 # So we should be able to generate exactly chainlimit txs for each original output
397 sending_addr
= self
.nodes
[1].getnewaddress()
399 for i
in range(chainlimit
*2):
400 txid_list
.append(self
.nodes
[0].sendtoaddress(sending_addr
, Decimal('0.0001')))
401 assert_equal(self
.nodes
[0].getmempoolinfo()['size'], chainlimit
*2)
402 assert_equal(len(txid_list
), chainlimit
*2)
404 # Without walletrejectlongchains, we will still generate a txid
405 # The tx will be stored in the wallet but not accepted to the mempool
406 extra_txid
= self
.nodes
[0].sendtoaddress(sending_addr
, Decimal('0.0001'))
407 assert(extra_txid
not in self
.nodes
[0].getrawmempool())
408 assert(extra_txid
in [tx
["txid"] for tx
in self
.nodes
[0].listtransactions()])
409 self
.nodes
[0].abandontransaction(extra_txid
)
410 total_txs
= len(self
.nodes
[0].listtransactions("*",99999))
412 # Try with walletrejectlongchains
413 # Double chain limit but require combining inputs, so we pass SelectCoinsMinConf
415 self
.start_node(0, extra_args
=["-walletrejectlongchains", "-limitancestorcount="+str(2*chainlimit
)])
417 # wait for loadmempool
419 while (timeout
> 0 and len(self
.nodes
[0].getrawmempool()) < chainlimit
*2):
422 assert_equal(len(self
.nodes
[0].getrawmempool()), chainlimit
*2)
424 node0_balance
= self
.nodes
[0].getbalance()
425 # With walletrejectlongchains we will not create the tx and store it in our wallet.
426 assert_raises_jsonrpc(-4, "Transaction has too long of a mempool chain", self
.nodes
[0].sendtoaddress
, sending_addr
, node0_balance
- Decimal('0.01'))
428 # Verify nothing new in wallet
429 assert_equal(total_txs
, len(self
.nodes
[0].listtransactions("*",99999)))
431 if __name__
== '__main__':