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
15 self
.nodes
[2].generate(101)
19 self
.test_double_spend()
20 self
.test_double_send()
24 `listsinceblock` did not behave correctly when handed a block that was
25 no longer in the main chain:
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
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
48 This test only checks that [tx0] is present.
51 # Split network into two
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:]])
66 # listsinceblock(lastblockhash) should now include tx, as seen from nodes[0]
67 lsbres
= self
.nodes
[0].listsinceblock(lastblockhash
)
69 for tx
in lsbres
['transactions']:
70 if tx
['txid'] == senttx
:
75 def test_double_spend(self
):
77 This tests the case where the same UTXO is spent twice on two separate
78 blocks as part of a reorg.
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
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
107 # Split network into two
110 # share utxo between nodes[1] and nodes[2]
111 utxos
= self
.nodes
[2].listunspent()
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)
119 self
.nodes
[0].getnewaddress(): 1,
120 self
.nodes
[1].getnewaddress(): change
,
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]
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)
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
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
186 # Split network into two
189 # create and sign a transaction
190 utxos
= self
.nodes
[2].listunspent()
192 change
= '%.8f' % (float(utxo
['amount']) - 1.0003)
194 self
.nodes
[0].getnewaddress(): 1,
195 self
.nodes
[2].getnewaddress(): change
,
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)
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()