[tests] don't override __init__() in individual tests
[bitcoinplatinum.git] / test / functional / listsinceblock.py
blobed1315e805b1640432af450c5fe7b976f9f8d712
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
10 class ListSinceBlockTest (BitcoinTestFramework):
11 def set_test_params(self):
12 self.setup_clean_chain = True
14 def run_test(self):
15 self.nodes[2].generate(101)
16 self.sync_all()
18 self.test_reorg()
19 self.test_double_spend()
20 self.test_double_send()
22 def test_reorg(self):
23 '''
24 `listsinceblock` did not behave correctly when handed a block that was
25 no longer in the main chain:
27 ab0
28 / \
29 aa1 [tx0] bb1
30 | |
31 aa2 bb2
32 | |
33 aa3 bb3
35 bb4
37 Consider a client that has only seen block `aa3` above. It asks the node
38 to `listsinceblock aa3`. But at some point prior the main chain switched
39 to the bb chain.
41 Previously: listsinceblock would find height=4 for block aa3 and compare
42 this to height=5 for the tip of the chain (bb4). It would then return
43 results restricted to bb3-bb4.
45 Now: listsinceblock finds the fork at ab0 and returns results in the
46 range bb1-bb4.
48 This test only checks that [tx0] is present.
49 '''
51 # Split network into two
52 self.split_network()
54 # send to nodes[0] from nodes[2]
55 senttx = self.nodes[2].sendtoaddress(self.nodes[0].getnewaddress(), 1)
57 # generate on both sides
58 lastblockhash = self.nodes[1].generate(6)[5]
59 self.nodes[2].generate(7)
60 self.log.info('lastblockhash=%s' % (lastblockhash))
62 self.sync_all([self.nodes[:2], self.nodes[2:]])
64 self.join_network()
66 # listsinceblock(lastblockhash) should now include tx, as seen from nodes[0]
67 lsbres = self.nodes[0].listsinceblock(lastblockhash)
68 found = False
69 for tx in lsbres['transactions']:
70 if tx['txid'] == senttx:
71 found = True
72 break
73 assert found
75 def test_double_spend(self):
76 '''
77 This tests the case where the same UTXO is spent twice on two separate
78 blocks as part of a reorg.
80 ab0
81 / \
82 aa1 [tx1] bb1 [tx2]
83 | |
84 aa2 bb2
85 | |
86 aa3 bb3
88 bb4
90 Problematic case:
92 1. User 1 receives BTC in tx1 from utxo1 in block aa1.
93 2. User 2 receives BTC in tx2 from utxo1 (same) in block bb1
94 3. User 1 sees 2 confirmations at block aa3.
95 4. Reorg into bb chain.
96 5. User 1 asks `listsinceblock aa3` and does not see that tx1 is now
97 invalidated.
99 Currently the solution to this is to detect that a reorg'd block is
100 asked for in listsinceblock, and to iterate back over existing blocks up
101 until the fork point, and to include all transactions that relate to the
102 node wallet.
105 self.sync_all()
107 # Split network into two
108 self.split_network()
110 # share utxo between nodes[1] and nodes[2]
111 utxos = self.nodes[2].listunspent()
112 utxo = utxos[0]
113 privkey = self.nodes[2].dumpprivkey(utxo['address'])
114 self.nodes[1].importprivkey(privkey)
116 # send from nodes[1] using utxo to nodes[0]
117 change = '%.8f' % (float(utxo['amount']) - 1.0003)
118 recipientDict = {
119 self.nodes[0].getnewaddress(): 1,
120 self.nodes[1].getnewaddress(): change,
122 utxoDicts = [{
123 'txid': utxo['txid'],
124 'vout': utxo['vout'],
126 txid1 = self.nodes[1].sendrawtransaction(
127 self.nodes[1].signrawtransaction(
128 self.nodes[1].createrawtransaction(utxoDicts, recipientDict))['hex'])
130 # send from nodes[2] using utxo to nodes[3]
131 recipientDict2 = {
132 self.nodes[3].getnewaddress(): 1,
133 self.nodes[2].getnewaddress(): change,
135 self.nodes[2].sendrawtransaction(
136 self.nodes[2].signrawtransaction(
137 self.nodes[2].createrawtransaction(utxoDicts, recipientDict2))['hex'])
139 # generate on both sides
140 lastblockhash = self.nodes[1].generate(3)[2]
141 self.nodes[2].generate(4)
143 self.join_network()
145 self.sync_all()
147 # gettransaction should work for txid1
148 assert self.nodes[0].gettransaction(txid1)['txid'] == txid1, "gettransaction failed to find txid1"
150 # listsinceblock(lastblockhash) should now include txid1, as seen from nodes[0]
151 lsbres = self.nodes[0].listsinceblock(lastblockhash)
152 assert any(tx['txid'] == txid1 for tx in lsbres['removed'])
154 # but it should not include 'removed' if include_removed=false
155 lsbres2 = self.nodes[0].listsinceblock(blockhash=lastblockhash, include_removed=False)
156 assert 'removed' not in lsbres2
158 def test_double_send(self):
160 This tests the case where the same transaction is submitted twice on two
161 separate blocks as part of a reorg. The former will vanish and the
162 latter will appear as the true transaction (with confirmations dropping
163 as a result).
167 aa1 [tx1] bb1
169 aa2 bb2
171 aa3 bb3 [tx1]
175 Asserted:
177 1. tx1 is listed in listsinceblock.
178 2. It is included in 'removed' as it was removed, even though it is now
179 present in a different block.
180 3. It is listed with a confirmations count of 2 (bb3, bb4), not
181 3 (aa1, aa2, aa3).
184 self.sync_all()
186 # Split network into two
187 self.split_network()
189 # create and sign a transaction
190 utxos = self.nodes[2].listunspent()
191 utxo = utxos[0]
192 change = '%.8f' % (float(utxo['amount']) - 1.0003)
193 recipientDict = {
194 self.nodes[0].getnewaddress(): 1,
195 self.nodes[2].getnewaddress(): change,
197 utxoDicts = [{
198 'txid': utxo['txid'],
199 'vout': utxo['vout'],
201 signedtxres = self.nodes[2].signrawtransaction(
202 self.nodes[2].createrawtransaction(utxoDicts, recipientDict))
203 assert signedtxres['complete']
205 signedtx = signedtxres['hex']
207 # send from nodes[1]; this will end up in aa1
208 txid1 = self.nodes[1].sendrawtransaction(signedtx)
210 # generate bb1-bb2 on right side
211 self.nodes[2].generate(2)
213 # send from nodes[2]; this will end up in bb3
214 txid2 = self.nodes[2].sendrawtransaction(signedtx)
216 assert_equal(txid1, txid2)
218 # generate on both sides
219 lastblockhash = self.nodes[1].generate(3)[2]
220 self.nodes[2].generate(2)
222 self.join_network()
224 self.sync_all()
226 # gettransaction should work for txid1
227 self.nodes[0].gettransaction(txid1)
229 # listsinceblock(lastblockhash) should now include txid1 in transactions
230 # as well as in removed
231 lsbres = self.nodes[0].listsinceblock(lastblockhash)
232 assert any(tx['txid'] == txid1 for tx in lsbres['transactions'])
233 assert any(tx['txid'] == txid1 for tx in lsbres['removed'])
235 # find transaction and ensure confirmations is valid
236 for tx in lsbres['transactions']:
237 if tx['txid'] == txid1:
238 assert_equal(tx['confirmations'], 2)
240 # the same check for the removed array; confirmations should STILL be 2
241 for tx in lsbres['removed']:
242 if tx['txid'] == txid1:
243 assert_equal(tx['confirmations'], 2)
245 if __name__ == '__main__':
246 ListSinceBlockTest().main()