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
):
13 self
.setup_clean_chain
= True
16 self
.nodes
[2].generate(101)
20 self
.test_double_spend()
21 self
.test_double_send()
25 `listsinceblock` did not behave correctly when handed a block that was
26 no longer in the main chain:
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
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
49 This test only checks that [tx0] is present.
52 # Split network into two
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:]])
67 # listsinceblock(lastblockhash) should now include tx, as seen from nodes[0]
68 lsbres
= self
.nodes
[0].listsinceblock(lastblockhash
)
70 for tx
in lsbres
['transactions']:
71 if tx
['txid'] == senttx
:
76 def test_double_spend(self
):
78 This tests the case where the same UTXO is spent twice on two separate
79 blocks as part of a reorg.
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
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
108 # Split network into two
111 # share utxo between nodes[1] and nodes[2]
112 utxos
= self
.nodes
[2].listunspent()
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)
120 self
.nodes
[0].getnewaddress(): 1,
121 self
.nodes
[1].getnewaddress(): change
,
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]
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)
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
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
187 # Split network into two
190 # create and sign a transaction
191 utxos
= self
.nodes
[2].listunspent()
193 change
= '%.8f' % (float(utxo
['amount']) - 1.0003)
195 self
.nodes
[0].getnewaddress(): 1,
196 self
.nodes
[2].getnewaddress(): change
,
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)
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()