Merge #12079: Improve prioritisetransaction test coverage
[bitcoinplatinum.git] / test / functional / listsinceblock.py
blob67e7744bf8433c79134469111a4b0aacca22b60d
1 #!/usr/bin/env python3
2 # Copyright (c) 2017 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 listsincelast RPC."""
7 from test_framework.test_framework import BitcoinTestFramework
8 from test_framework.util import assert_equal, assert_array_result, assert_raises_rpc_error
10 class ListSinceBlockTest (BitcoinTestFramework):
11 def set_test_params(self):
12 self.num_nodes = 4
13 self.setup_clean_chain = True
15 def run_test(self):
16 self.nodes[2].generate(101)
17 self.sync_all()
19 self.test_no_blockhash()
20 self.test_invalid_blockhash()
21 self.test_reorg()
22 self.test_double_spend()
23 self.test_double_send()
25 def test_no_blockhash(self):
26 txid = self.nodes[2].sendtoaddress(self.nodes[0].getnewaddress(), 1)
27 blockhash, = self.nodes[2].generate(1)
28 self.sync_all()
30 txs = self.nodes[0].listtransactions()
31 assert_array_result(txs, {"txid": txid}, {
32 "category": "receive",
33 "amount": 1,
34 "blockhash": blockhash,
35 "confirmations": 1,
37 assert_equal(
38 self.nodes[0].listsinceblock(),
39 {"lastblock": blockhash,
40 "removed": [],
41 "transactions": txs})
42 assert_equal(
43 self.nodes[0].listsinceblock(""),
44 {"lastblock": blockhash,
45 "removed": [],
46 "transactions": txs})
48 def test_invalid_blockhash(self):
49 assert_raises_rpc_error(-5, "Block not found", self.nodes[0].listsinceblock,
50 "42759cde25462784395a337460bde75f58e73d3f08bd31fdc3507cbac856a2c4")
51 assert_raises_rpc_error(-5, "Block not found", self.nodes[0].listsinceblock,
52 "0000000000000000000000000000000000000000000000000000000000000000")
53 assert_raises_rpc_error(-5, "Block not found", self.nodes[0].listsinceblock,
54 "invalid-hex")
56 def test_reorg(self):
57 '''
58 `listsinceblock` did not behave correctly when handed a block that was
59 no longer in the main chain:
61 ab0
62 / \
63 aa1 [tx0] bb1
64 | |
65 aa2 bb2
66 | |
67 aa3 bb3
69 bb4
71 Consider a client that has only seen block `aa3` above. It asks the node
72 to `listsinceblock aa3`. But at some point prior the main chain switched
73 to the bb chain.
75 Previously: listsinceblock would find height=4 for block aa3 and compare
76 this to height=5 for the tip of the chain (bb4). It would then return
77 results restricted to bb3-bb4.
79 Now: listsinceblock finds the fork at ab0 and returns results in the
80 range bb1-bb4.
82 This test only checks that [tx0] is present.
83 '''
85 # Split network into two
86 self.split_network()
88 # send to nodes[0] from nodes[2]
89 senttx = self.nodes[2].sendtoaddress(self.nodes[0].getnewaddress(), 1)
91 # generate on both sides
92 lastblockhash = self.nodes[1].generate(6)[5]
93 self.nodes[2].generate(7)
94 self.log.info('lastblockhash=%s' % (lastblockhash))
96 self.sync_all([self.nodes[:2], self.nodes[2:]])
98 self.join_network()
100 # listsinceblock(lastblockhash) should now include tx, as seen from nodes[0]
101 lsbres = self.nodes[0].listsinceblock(lastblockhash)
102 found = False
103 for tx in lsbres['transactions']:
104 if tx['txid'] == senttx:
105 found = True
106 break
107 assert found
109 def test_double_spend(self):
111 This tests the case where the same UTXO is spent twice on two separate
112 blocks as part of a reorg.
116 aa1 [tx1] bb1 [tx2]
118 aa2 bb2
120 aa3 bb3
124 Problematic case:
126 1. User 1 receives BTC in tx1 from utxo1 in block aa1.
127 2. User 2 receives BTC in tx2 from utxo1 (same) in block bb1
128 3. User 1 sees 2 confirmations at block aa3.
129 4. Reorg into bb chain.
130 5. User 1 asks `listsinceblock aa3` and does not see that tx1 is now
131 invalidated.
133 Currently the solution to this is to detect that a reorg'd block is
134 asked for in listsinceblock, and to iterate back over existing blocks up
135 until the fork point, and to include all transactions that relate to the
136 node wallet.
139 self.sync_all()
141 # Split network into two
142 self.split_network()
144 # share utxo between nodes[1] and nodes[2]
145 utxos = self.nodes[2].listunspent()
146 utxo = utxos[0]
147 privkey = self.nodes[2].dumpprivkey(utxo['address'])
148 self.nodes[1].importprivkey(privkey)
150 # send from nodes[1] using utxo to nodes[0]
151 change = '%.8f' % (float(utxo['amount']) - 1.0003)
152 recipientDict = {
153 self.nodes[0].getnewaddress(): 1,
154 self.nodes[1].getnewaddress(): change,
156 utxoDicts = [{
157 'txid': utxo['txid'],
158 'vout': utxo['vout'],
160 txid1 = self.nodes[1].sendrawtransaction(
161 self.nodes[1].signrawtransaction(
162 self.nodes[1].createrawtransaction(utxoDicts, recipientDict))['hex'])
164 # send from nodes[2] using utxo to nodes[3]
165 recipientDict2 = {
166 self.nodes[3].getnewaddress(): 1,
167 self.nodes[2].getnewaddress(): change,
169 self.nodes[2].sendrawtransaction(
170 self.nodes[2].signrawtransaction(
171 self.nodes[2].createrawtransaction(utxoDicts, recipientDict2))['hex'])
173 # generate on both sides
174 lastblockhash = self.nodes[1].generate(3)[2]
175 self.nodes[2].generate(4)
177 self.join_network()
179 self.sync_all()
181 # gettransaction should work for txid1
182 assert self.nodes[0].gettransaction(txid1)['txid'] == txid1, "gettransaction failed to find txid1"
184 # listsinceblock(lastblockhash) should now include txid1, as seen from nodes[0]
185 lsbres = self.nodes[0].listsinceblock(lastblockhash)
186 assert any(tx['txid'] == txid1 for tx in lsbres['removed'])
188 # but it should not include 'removed' if include_removed=false
189 lsbres2 = self.nodes[0].listsinceblock(blockhash=lastblockhash, include_removed=False)
190 assert 'removed' not in lsbres2
192 def test_double_send(self):
194 This tests the case where the same transaction is submitted twice on two
195 separate blocks as part of a reorg. The former will vanish and the
196 latter will appear as the true transaction (with confirmations dropping
197 as a result).
201 aa1 [tx1] bb1
203 aa2 bb2
205 aa3 bb3 [tx1]
209 Asserted:
211 1. tx1 is listed in listsinceblock.
212 2. It is included in 'removed' as it was removed, even though it is now
213 present in a different block.
214 3. It is listed with a confirmations count of 2 (bb3, bb4), not
215 3 (aa1, aa2, aa3).
218 self.sync_all()
220 # Split network into two
221 self.split_network()
223 # create and sign a transaction
224 utxos = self.nodes[2].listunspent()
225 utxo = utxos[0]
226 change = '%.8f' % (float(utxo['amount']) - 1.0003)
227 recipientDict = {
228 self.nodes[0].getnewaddress(): 1,
229 self.nodes[2].getnewaddress(): change,
231 utxoDicts = [{
232 'txid': utxo['txid'],
233 'vout': utxo['vout'],
235 signedtxres = self.nodes[2].signrawtransaction(
236 self.nodes[2].createrawtransaction(utxoDicts, recipientDict))
237 assert signedtxres['complete']
239 signedtx = signedtxres['hex']
241 # send from nodes[1]; this will end up in aa1
242 txid1 = self.nodes[1].sendrawtransaction(signedtx)
244 # generate bb1-bb2 on right side
245 self.nodes[2].generate(2)
247 # send from nodes[2]; this will end up in bb3
248 txid2 = self.nodes[2].sendrawtransaction(signedtx)
250 assert_equal(txid1, txid2)
252 # generate on both sides
253 lastblockhash = self.nodes[1].generate(3)[2]
254 self.nodes[2].generate(2)
256 self.join_network()
258 self.sync_all()
260 # gettransaction should work for txid1
261 self.nodes[0].gettransaction(txid1)
263 # listsinceblock(lastblockhash) should now include txid1 in transactions
264 # as well as in removed
265 lsbres = self.nodes[0].listsinceblock(lastblockhash)
266 assert any(tx['txid'] == txid1 for tx in lsbres['transactions'])
267 assert any(tx['txid'] == txid1 for tx in lsbres['removed'])
269 # find transaction and ensure confirmations is valid
270 for tx in lsbres['transactions']:
271 if tx['txid'] == txid1:
272 assert_equal(tx['confirmations'], 2)
274 # the same check for the removed array; confirmations should STILL be 2
275 for tx in lsbres['removed']:
276 if tx['txid'] == txid1:
277 assert_equal(tx['confirmations'], 2)
279 if __name__ == '__main__':
280 ListSinceBlockTest().main()