[tests] Add -blocknotify functional test
[bitcoinplatinum.git] / test / functional / listsinceblock.py
blob6f428388ecbbd91bf9dfc27ef0822b8b98cd41f3
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.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_reorg()
20 self.test_double_spend()
21 self.test_double_send()
23 def test_reorg(self):
24 '''
25 `listsinceblock` did not behave correctly when handed a block that was
26 no longer in the main chain:
28 ab0
29 / \
30 aa1 [tx0] bb1
31 | |
32 aa2 bb2
33 | |
34 aa3 bb3
36 bb4
38 Consider a client that has only seen block `aa3` above. It asks the node
39 to `listsinceblock aa3`. But at some point prior the main chain switched
40 to the bb chain.
42 Previously: listsinceblock would find height=4 for block aa3 and compare
43 this to height=5 for the tip of the chain (bb4). It would then return
44 results restricted to bb3-bb4.
46 Now: listsinceblock finds the fork at ab0 and returns results in the
47 range bb1-bb4.
49 This test only checks that [tx0] is present.
50 '''
52 # Split network into two
53 self.split_network()
55 # send to nodes[0] from nodes[2]
56 senttx = self.nodes[2].sendtoaddress(self.nodes[0].getnewaddress(), 1)
58 # generate on both sides
59 lastblockhash = self.nodes[1].generate(6)[5]
60 self.nodes[2].generate(7)
61 self.log.info('lastblockhash=%s' % (lastblockhash))
63 self.sync_all([self.nodes[:2], self.nodes[2:]])
65 self.join_network()
67 # listsinceblock(lastblockhash) should now include tx, as seen from nodes[0]
68 lsbres = self.nodes[0].listsinceblock(lastblockhash)
69 found = False
70 for tx in lsbres['transactions']:
71 if tx['txid'] == senttx:
72 found = True
73 break
74 assert found
76 def test_double_spend(self):
77 '''
78 This tests the case where the same UTXO is spent twice on two separate
79 blocks as part of a reorg.
81 ab0
82 / \
83 aa1 [tx1] bb1 [tx2]
84 | |
85 aa2 bb2
86 | |
87 aa3 bb3
89 bb4
91 Problematic case:
93 1. User 1 receives BTC in tx1 from utxo1 in block aa1.
94 2. User 2 receives BTC in tx2 from utxo1 (same) in block bb1
95 3. User 1 sees 2 confirmations at block aa3.
96 4. Reorg into bb chain.
97 5. User 1 asks `listsinceblock aa3` and does not see that tx1 is now
98 invalidated.
100 Currently the solution to this is to detect that a reorg'd block is
101 asked for in listsinceblock, and to iterate back over existing blocks up
102 until the fork point, and to include all transactions that relate to the
103 node wallet.
106 self.sync_all()
108 # Split network into two
109 self.split_network()
111 # share utxo between nodes[1] and nodes[2]
112 utxos = self.nodes[2].listunspent()
113 utxo = utxos[0]
114 privkey = self.nodes[2].dumpprivkey(utxo['address'])
115 self.nodes[1].importprivkey(privkey)
117 # send from nodes[1] using utxo to nodes[0]
118 change = '%.8f' % (float(utxo['amount']) - 1.0003)
119 recipientDict = {
120 self.nodes[0].getnewaddress(): 1,
121 self.nodes[1].getnewaddress(): change,
123 utxoDicts = [{
124 'txid': utxo['txid'],
125 'vout': utxo['vout'],
127 txid1 = self.nodes[1].sendrawtransaction(
128 self.nodes[1].signrawtransaction(
129 self.nodes[1].createrawtransaction(utxoDicts, recipientDict))['hex'])
131 # send from nodes[2] using utxo to nodes[3]
132 recipientDict2 = {
133 self.nodes[3].getnewaddress(): 1,
134 self.nodes[2].getnewaddress(): change,
136 self.nodes[2].sendrawtransaction(
137 self.nodes[2].signrawtransaction(
138 self.nodes[2].createrawtransaction(utxoDicts, recipientDict2))['hex'])
140 # generate on both sides
141 lastblockhash = self.nodes[1].generate(3)[2]
142 self.nodes[2].generate(4)
144 self.join_network()
146 self.sync_all()
148 # gettransaction should work for txid1
149 assert self.nodes[0].gettransaction(txid1)['txid'] == txid1, "gettransaction failed to find txid1"
151 # listsinceblock(lastblockhash) should now include txid1, as seen from nodes[0]
152 lsbres = self.nodes[0].listsinceblock(lastblockhash)
153 assert any(tx['txid'] == txid1 for tx in lsbres['removed'])
155 # but it should not include 'removed' if include_removed=false
156 lsbres2 = self.nodes[0].listsinceblock(blockhash=lastblockhash, include_removed=False)
157 assert 'removed' not in lsbres2
159 def test_double_send(self):
161 This tests the case where the same transaction is submitted twice on two
162 separate blocks as part of a reorg. The former will vanish and the
163 latter will appear as the true transaction (with confirmations dropping
164 as a result).
168 aa1 [tx1] bb1
170 aa2 bb2
172 aa3 bb3 [tx1]
176 Asserted:
178 1. tx1 is listed in listsinceblock.
179 2. It is included in 'removed' as it was removed, even though it is now
180 present in a different block.
181 3. It is listed with a confirmations count of 2 (bb3, bb4), not
182 3 (aa1, aa2, aa3).
185 self.sync_all()
187 # Split network into two
188 self.split_network()
190 # create and sign a transaction
191 utxos = self.nodes[2].listunspent()
192 utxo = utxos[0]
193 change = '%.8f' % (float(utxo['amount']) - 1.0003)
194 recipientDict = {
195 self.nodes[0].getnewaddress(): 1,
196 self.nodes[2].getnewaddress(): change,
198 utxoDicts = [{
199 'txid': utxo['txid'],
200 'vout': utxo['vout'],
202 signedtxres = self.nodes[2].signrawtransaction(
203 self.nodes[2].createrawtransaction(utxoDicts, recipientDict))
204 assert signedtxres['complete']
206 signedtx = signedtxres['hex']
208 # send from nodes[1]; this will end up in aa1
209 txid1 = self.nodes[1].sendrawtransaction(signedtx)
211 # generate bb1-bb2 on right side
212 self.nodes[2].generate(2)
214 # send from nodes[2]; this will end up in bb3
215 txid2 = self.nodes[2].sendrawtransaction(signedtx)
217 assert_equal(txid1, txid2)
219 # generate on both sides
220 lastblockhash = self.nodes[1].generate(3)[2]
221 self.nodes[2].generate(2)
223 self.join_network()
225 self.sync_all()
227 # gettransaction should work for txid1
228 self.nodes[0].gettransaction(txid1)
230 # listsinceblock(lastblockhash) should now include txid1 in transactions
231 # as well as in removed
232 lsbres = self.nodes[0].listsinceblock(lastblockhash)
233 assert any(tx['txid'] == txid1 for tx in lsbres['transactions'])
234 assert any(tx['txid'] == txid1 for tx in lsbres['removed'])
236 # find transaction and ensure confirmations is valid
237 for tx in lsbres['transactions']:
238 if tx['txid'] == txid1:
239 assert_equal(tx['confirmations'], 2)
241 # the same check for the removed array; confirmations should STILL be 2
242 for tx in lsbres['removed']:
243 if tx['txid'] == txid1:
244 assert_equal(tx['confirmations'], 2)
246 if __name__ == '__main__':
247 ListSinceBlockTest().main()