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
):
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)
19 self
.setup_clean_chain
= True
21 self
.extra_args
= [['-usehd={:d}'.format(i
%2==0)] for i
in range(4)]
23 def setup_network(self
):
24 self
.add_nodes(4, self
.extra_args
)
28 connect_nodes_bi(self
.nodes
,0,1)
29 connect_nodes_bi(self
.nodes
,1,2)
30 connect_nodes_bi(self
.nodes
,0,2)
31 self
.sync_all([self
.nodes
[0:3]])
35 # Check that there's no UTXO on none of the nodes
36 assert_equal(len(self
.nodes
[0].listunspent()), 0)
37 assert_equal(len(self
.nodes
[1].listunspent()), 0)
38 assert_equal(len(self
.nodes
[2].listunspent()), 0)
40 self
.log
.info("Mining blocks...")
42 self
.nodes
[0].generate(1)
44 walletinfo
= self
.nodes
[0].getwalletinfo()
45 assert_equal(walletinfo
['immature_balance'], 50)
46 assert_equal(walletinfo
['balance'], 0)
48 self
.sync_all([self
.nodes
[0:3]])
49 self
.nodes
[1].generate(101)
50 self
.sync_all([self
.nodes
[0:3]])
52 assert_equal(self
.nodes
[0].getbalance(), 50)
53 assert_equal(self
.nodes
[1].getbalance(), 50)
54 assert_equal(self
.nodes
[2].getbalance(), 0)
56 # Check that only first and second nodes have UTXOs
57 utxos
= self
.nodes
[0].listunspent()
58 assert_equal(len(utxos
), 1)
59 assert_equal(len(self
.nodes
[1].listunspent()), 1)
60 assert_equal(len(self
.nodes
[2].listunspent()), 0)
62 # Send 21 BTC from 0 to 2 using sendtoaddress call.
63 # Locked memory should use at least 32 bytes to sign each transaction
64 self
.log
.info("test getmemoryinfo")
65 memory_before
= self
.nodes
[0].getmemoryinfo()
66 self
.nodes
[0].sendtoaddress(self
.nodes
[2].getnewaddress(), 11)
67 mempool_txid
= self
.nodes
[0].sendtoaddress(self
.nodes
[2].getnewaddress(), 10)
68 memory_after
= self
.nodes
[0].getmemoryinfo()
69 assert(memory_before
['locked']['used'] + 64 <= memory_after
['locked']['used'])
71 self
.log
.info("test gettxout")
72 # utxo spent in mempool should be visible if you exclude mempool
73 # but invisible if you include mempool
74 confirmed_txid
, confirmed_index
= utxos
[0]["txid"], utxos
[0]["vout"]
75 txout
= self
.nodes
[0].gettxout(confirmed_txid
, confirmed_index
, False)
76 assert_equal(txout
['value'], 50)
77 txout
= self
.nodes
[0].gettxout(confirmed_txid
, confirmed_index
, True)
79 # new utxo from mempool should be invisible if you exclude mempool
80 # but visible if you include mempool
81 txout
= self
.nodes
[0].gettxout(mempool_txid
, 0, False)
83 txout1
= self
.nodes
[0].gettxout(mempool_txid
, 0, True)
84 txout2
= self
.nodes
[0].gettxout(mempool_txid
, 1, True)
85 # note the mempool tx will have randomly assigned indices
86 # but 10 will go to node2 and the rest will go to node0
87 balance
= self
.nodes
[0].getbalance()
88 assert_equal(set([txout1
['value'], txout2
['value']]), set([10, balance
]))
89 walletinfo
= self
.nodes
[0].getwalletinfo()
90 assert_equal(walletinfo
['immature_balance'], 0)
92 # Have node0 mine a block, thus it will collect its own fee.
93 self
.nodes
[0].generate(1)
94 self
.sync_all([self
.nodes
[0:3]])
96 # Exercise locking of unspent outputs
97 unspent_0
= self
.nodes
[2].listunspent()[0]
98 unspent_0
= {"txid": unspent_0
["txid"], "vout": unspent_0
["vout"]}
99 self
.nodes
[2].lockunspent(False, [unspent_0
])
100 assert_raises_jsonrpc(-4, "Insufficient funds", self
.nodes
[2].sendtoaddress
, self
.nodes
[2].getnewaddress(), 20)
101 assert_equal([unspent_0
], self
.nodes
[2].listlockunspent())
102 self
.nodes
[2].lockunspent(True, [unspent_0
])
103 assert_equal(len(self
.nodes
[2].listlockunspent()), 0)
105 # Have node1 generate 100 blocks (so node0 can recover the fee)
106 self
.nodes
[1].generate(100)
107 self
.sync_all([self
.nodes
[0:3]])
109 # node0 should end up with 100 btc in block rewards plus fees, but
110 # minus the 21 plus fees sent to node2
111 assert_equal(self
.nodes
[0].getbalance(), 100-21)
112 assert_equal(self
.nodes
[2].getbalance(), 21)
114 # Node0 should have two unspent outputs.
115 # Create a couple of transactions to send them to node2, submit them through
116 # node1, and make sure both node0 and node2 pick them up properly:
117 node0utxos
= self
.nodes
[0].listunspent(1)
118 assert_equal(len(node0utxos
), 2)
120 # create both transactions
122 for utxo
in node0utxos
:
125 inputs
.append({ "txid" : utxo
["txid"], "vout" : utxo
["vout"]})
126 outputs
[self
.nodes
[2].getnewaddress("from1")] = utxo
["amount"] - 3
127 raw_tx
= self
.nodes
[0].createrawtransaction(inputs
, outputs
)
128 txns_to_send
.append(self
.nodes
[0].signrawtransaction(raw_tx
))
130 # Have node 1 (miner) send the transactions
131 self
.nodes
[1].sendrawtransaction(txns_to_send
[0]["hex"], True)
132 self
.nodes
[1].sendrawtransaction(txns_to_send
[1]["hex"], True)
134 # Have node1 mine a block to confirm transactions:
135 self
.nodes
[1].generate(1)
136 self
.sync_all([self
.nodes
[0:3]])
138 assert_equal(self
.nodes
[0].getbalance(), 0)
139 assert_equal(self
.nodes
[2].getbalance(), 94)
140 assert_equal(self
.nodes
[2].getbalance("from1"), 94-21)
143 address
= self
.nodes
[0].getnewaddress("test")
144 fee_per_byte
= Decimal('0.001') / 1000
145 self
.nodes
[2].settxfee(fee_per_byte
* 1000)
146 txid
= self
.nodes
[2].sendtoaddress(address
, 10, "", "", False)
147 self
.nodes
[2].generate(1)
148 self
.sync_all([self
.nodes
[0:3]])
149 node_2_bal
= self
.check_fee_amount(self
.nodes
[2].getbalance(), Decimal('84'), fee_per_byte
, count_bytes(self
.nodes
[2].getrawtransaction(txid
)))
150 assert_equal(self
.nodes
[0].getbalance(), Decimal('10'))
152 # Send 10 BTC with subtract fee from amount
153 txid
= self
.nodes
[2].sendtoaddress(address
, 10, "", "", True)
154 self
.nodes
[2].generate(1)
155 self
.sync_all([self
.nodes
[0:3]])
156 node_2_bal
-= Decimal('10')
157 assert_equal(self
.nodes
[2].getbalance(), node_2_bal
)
158 node_0_bal
= self
.check_fee_amount(self
.nodes
[0].getbalance(), Decimal('20'), fee_per_byte
, count_bytes(self
.nodes
[2].getrawtransaction(txid
)))
161 txid
= self
.nodes
[2].sendmany('from1', {address
: 10}, 0, "", [])
162 self
.nodes
[2].generate(1)
163 self
.sync_all([self
.nodes
[0:3]])
164 node_0_bal
+= Decimal('10')
165 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
)))
166 assert_equal(self
.nodes
[0].getbalance(), node_0_bal
)
168 # Sendmany 10 BTC with subtract fee from amount
169 txid
= self
.nodes
[2].sendmany('from1', {address
: 10}, 0, "", [address
])
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(), node_0_bal
+ Decimal('10'), fee_per_byte
, count_bytes(self
.nodes
[2].getrawtransaction(txid
)))
176 # Test ResendWalletTransactions:
177 # Create a couple of transactions, then start up a fourth
178 # node (nodes[3]) and ask nodes[0] to rebroadcast.
179 # EXPECT: nodes[3] should have those transactions in its mempool.
180 txid1
= self
.nodes
[0].sendtoaddress(self
.nodes
[1].getnewaddress(), 1)
181 txid2
= self
.nodes
[1].sendtoaddress(self
.nodes
[0].getnewaddress(), 1)
182 sync_mempools(self
.nodes
[0:2])
185 connect_nodes_bi(self
.nodes
, 0, 3)
186 sync_blocks(self
.nodes
)
188 relayed
= self
.nodes
[0].resendwallettransactions()
189 assert_equal(set(relayed
), {txid1
, txid2
})
190 sync_mempools(self
.nodes
)
192 assert(txid1
in self
.nodes
[3].getrawmempool())
194 # Exercise balance rpcs
195 assert_equal(self
.nodes
[0].getwalletinfo()["unconfirmed_balance"], 1)
196 assert_equal(self
.nodes
[0].getunconfirmedbalance(), 1)
198 #check if we can list zero value tx as available coins
200 #2. hex-changed one output to 0.0
202 #4. check if recipient (node0) can list the zero value tx
203 usp
= self
.nodes
[1].listunspent()
204 inputs
= [{"txid":usp
[0]['txid'], "vout":usp
[0]['vout']}]
205 outputs
= {self
.nodes
[1].getnewaddress(): 49.998, self
.nodes
[0].getnewaddress(): 11.11}
207 rawTx
= self
.nodes
[1].createrawtransaction(inputs
, outputs
).replace("c0833842", "00000000") #replace 11.11 with 0.0 (int32)
208 decRawTx
= self
.nodes
[1].decoderawtransaction(rawTx
)
209 signedRawTx
= self
.nodes
[1].signrawtransaction(rawTx
)
210 decRawTx
= self
.nodes
[1].decoderawtransaction(signedRawTx
['hex'])
211 zeroValueTxid
= decRawTx
['txid']
212 sendResp
= self
.nodes
[1].sendrawtransaction(signedRawTx
['hex'])
215 self
.nodes
[1].generate(1) #mine a block
218 unspentTxs
= self
.nodes
[0].listunspent() #zero value tx must be in listunspents output
220 for uTx
in unspentTxs
:
221 if uTx
['txid'] == zeroValueTxid
:
223 assert_equal(uTx
['amount'], Decimal('0'))
226 #do some -walletbroadcast tests
228 self
.start_node(0, ["-walletbroadcast=0"])
229 self
.start_node(1, ["-walletbroadcast=0"])
230 self
.start_node(2, ["-walletbroadcast=0"])
231 connect_nodes_bi(self
.nodes
,0,1)
232 connect_nodes_bi(self
.nodes
,1,2)
233 connect_nodes_bi(self
.nodes
,0,2)
234 self
.sync_all([self
.nodes
[0:3]])
236 txIdNotBroadcasted
= self
.nodes
[0].sendtoaddress(self
.nodes
[2].getnewaddress(), 2)
237 txObjNotBroadcasted
= self
.nodes
[0].gettransaction(txIdNotBroadcasted
)
238 self
.nodes
[1].generate(1) #mine a block, tx should not be in there
239 self
.sync_all([self
.nodes
[0:3]])
240 assert_equal(self
.nodes
[2].getbalance(), node_2_bal
) #should not be changed because tx was not broadcasted
242 #now broadcast from another node, mine a block, sync, and check the balance
243 self
.nodes
[1].sendrawtransaction(txObjNotBroadcasted
['hex'])
244 self
.nodes
[1].generate(1)
245 self
.sync_all([self
.nodes
[0:3]])
247 txObjNotBroadcasted
= self
.nodes
[0].gettransaction(txIdNotBroadcasted
)
248 assert_equal(self
.nodes
[2].getbalance(), node_2_bal
)
251 txIdNotBroadcasted
= self
.nodes
[0].sendtoaddress(self
.nodes
[2].getnewaddress(), 2)
253 #restart the nodes with -walletbroadcast=1
258 connect_nodes_bi(self
.nodes
,0,1)
259 connect_nodes_bi(self
.nodes
,1,2)
260 connect_nodes_bi(self
.nodes
,0,2)
261 sync_blocks(self
.nodes
[0:3])
263 self
.nodes
[0].generate(1)
264 sync_blocks(self
.nodes
[0:3])
267 #tx should be added to balance because after restarting the nodes tx should be broadcastet
268 assert_equal(self
.nodes
[2].getbalance(), node_2_bal
)
270 #send a tx with value in a string (PR#6380 +)
271 txId
= self
.nodes
[0].sendtoaddress(self
.nodes
[2].getnewaddress(), "2")
272 txObj
= self
.nodes
[0].gettransaction(txId
)
273 assert_equal(txObj
['amount'], Decimal('-2'))
275 txId
= self
.nodes
[0].sendtoaddress(self
.nodes
[2].getnewaddress(), "0.0001")
276 txObj
= self
.nodes
[0].gettransaction(txId
)
277 assert_equal(txObj
['amount'], Decimal('-0.0001'))
279 #check if JSON parser can handle scientific notation in strings
280 txId
= self
.nodes
[0].sendtoaddress(self
.nodes
[2].getnewaddress(), "1e-4")
281 txObj
= self
.nodes
[0].gettransaction(txId
)
282 assert_equal(txObj
['amount'], Decimal('-0.0001'))
284 # This will raise an exception because the amount type is wrong
285 assert_raises_jsonrpc(-3, "Invalid amount", self
.nodes
[0].sendtoaddress
, self
.nodes
[2].getnewaddress(), "1f-4")
287 # This will raise an exception since generate does not accept a string
288 assert_raises_jsonrpc(-1, "not an integer", self
.nodes
[0].generate
, "2")
290 # Import address and private key to check correct behavior of spendable unspents
291 # 1. Send some coins to generate new UTXO
292 address_to_import
= self
.nodes
[2].getnewaddress()
293 txid
= self
.nodes
[0].sendtoaddress(address_to_import
, 1)
294 self
.nodes
[0].generate(1)
295 self
.sync_all([self
.nodes
[0:3]])
297 # 2. Import address from node2 to node1
298 self
.nodes
[1].importaddress(address_to_import
)
300 # 3. Validate that the imported address is watch-only on node1
301 assert(self
.nodes
[1].validateaddress(address_to_import
)["iswatchonly"])
303 # 4. Check that the unspents after import are not spendable
304 assert_array_result(self
.nodes
[1].listunspent(),
305 {"address": address_to_import
},
306 {"spendable": False})
308 # 5. Import private key of the previously imported address on node1
309 priv_key
= self
.nodes
[2].dumpprivkey(address_to_import
)
310 self
.nodes
[1].importprivkey(priv_key
)
312 # 6. Check that the unspents are now spendable on node1
313 assert_array_result(self
.nodes
[1].listunspent(),
314 {"address": address_to_import
},
317 # Mine a block from node0 to an address from node1
318 cbAddr
= self
.nodes
[1].getnewaddress()
319 blkHash
= self
.nodes
[0].generatetoaddress(1, cbAddr
)[0]
320 cbTxId
= self
.nodes
[0].getblock(blkHash
)['tx'][0]
321 self
.sync_all([self
.nodes
[0:3]])
323 # Check that the txid and balance is found by node1
324 self
.nodes
[1].gettransaction(cbTxId
)
326 # check if wallet or blockchain maintenance changes the balance
327 self
.sync_all([self
.nodes
[0:3]])
328 blocks
= self
.nodes
[0].generate(2)
329 self
.sync_all([self
.nodes
[0:3]])
330 balance_nodes
= [self
.nodes
[i
].getbalance() for i
in range(3)]
331 block_count
= self
.nodes
[0].getblockcount()
334 # - True: unicode escaped as \u....
335 # - False: unicode directly as UTF-8
336 for mode
in [True, False]:
337 self
.nodes
[0].ensure_ascii
= mode
338 # unicode check: Basic Multilingual Plane, Supplementary Plane respectively
339 for s
in [u
'рыба', u
'𝅘𝅥𝅯']:
340 addr
= self
.nodes
[0].getaccountaddress(s
)
341 label
= self
.nodes
[0].getaccount(addr
)
342 assert_equal(label
, s
)
343 assert(s
in self
.nodes
[0].listaccounts().keys())
344 self
.nodes
[0].ensure_ascii
= True # restore to default
352 # disabled until issue is fixed: https://github.com/bitcoin/bitcoin/issues/7463
356 for m
in maintenance
:
357 self
.log
.info("check " + m
)
359 # set lower ancestor limit for later
360 self
.start_node(0, [m
, "-limitancestorcount="+str(chainlimit
)])
361 self
.start_node(1, [m
, "-limitancestorcount="+str(chainlimit
)])
362 self
.start_node(2, [m
, "-limitancestorcount="+str(chainlimit
)])
363 while m
== '-reindex' and [block_count
] * 3 != [self
.nodes
[i
].getblockcount() for i
in range(3)]:
364 # reindex will leave rpc warm up "early"; Wait for it to finish
366 assert_equal(balance_nodes
, [self
.nodes
[i
].getbalance() for i
in range(3)])
368 # Exercise listsinceblock with the last two blocks
369 coinbase_tx_1
= self
.nodes
[0].listsinceblock(blocks
[0])
370 assert_equal(coinbase_tx_1
["lastblock"], blocks
[1])
371 assert_equal(len(coinbase_tx_1
["transactions"]), 1)
372 assert_equal(coinbase_tx_1
["transactions"][0]["blockhash"], blocks
[1])
373 assert_equal(len(self
.nodes
[0].listsinceblock(blocks
[1])["transactions"]), 0)
375 # ==Check that wallet prefers to use coins that don't exceed mempool limits =====
377 # Get all non-zero utxos together
378 chain_addrs
= [self
.nodes
[0].getnewaddress(), self
.nodes
[0].getnewaddress()]
379 singletxid
= self
.nodes
[0].sendtoaddress(chain_addrs
[0], self
.nodes
[0].getbalance(), "", "", True)
380 self
.nodes
[0].generate(1)
381 node0_balance
= self
.nodes
[0].getbalance()
382 # Split into two chains
383 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')})
384 signedtx
= self
.nodes
[0].signrawtransaction(rawtx
)
385 singletxid
= self
.nodes
[0].sendrawtransaction(signedtx
["hex"])
386 self
.nodes
[0].generate(1)
388 # Make a long chain of unconfirmed payments without hitting mempool limit
389 # Each tx we make leaves only one output of change on a chain 1 longer
390 # Since the amount to send is always much less than the outputs, we only ever need one output
391 # So we should be able to generate exactly chainlimit txs for each original output
392 sending_addr
= self
.nodes
[1].getnewaddress()
394 for i
in range(chainlimit
*2):
395 txid_list
.append(self
.nodes
[0].sendtoaddress(sending_addr
, Decimal('0.0001')))
396 assert_equal(self
.nodes
[0].getmempoolinfo()['size'], chainlimit
*2)
397 assert_equal(len(txid_list
), chainlimit
*2)
399 # Without walletrejectlongchains, we will still generate a txid
400 # The tx will be stored in the wallet but not accepted to the mempool
401 extra_txid
= self
.nodes
[0].sendtoaddress(sending_addr
, Decimal('0.0001'))
402 assert(extra_txid
not in self
.nodes
[0].getrawmempool())
403 assert(extra_txid
in [tx
["txid"] for tx
in self
.nodes
[0].listtransactions()])
404 self
.nodes
[0].abandontransaction(extra_txid
)
405 total_txs
= len(self
.nodes
[0].listtransactions("*",99999))
407 # Try with walletrejectlongchains
408 # Double chain limit but require combining inputs, so we pass SelectCoinsMinConf
410 self
.start_node(0, extra_args
=["-walletrejectlongchains", "-limitancestorcount="+str(2*chainlimit
)])
412 # wait for loadmempool
414 while (timeout
> 0 and len(self
.nodes
[0].getrawmempool()) < chainlimit
*2):
417 assert_equal(len(self
.nodes
[0].getrawmempool()), chainlimit
*2)
419 node0_balance
= self
.nodes
[0].getbalance()
420 # With walletrejectlongchains we will not create the tx and store it in our wallet.
421 assert_raises_jsonrpc(-4, "Transaction has too long of a mempool chain", self
.nodes
[0].sendtoaddress
, sending_addr
, node0_balance
- Decimal('0.01'))
423 # Verify nothing new in wallet
424 assert_equal(total_txs
, len(self
.nodes
[0].listtransactions("*",99999)))
426 if __name__
== '__main__':