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
):
13 self
.setup_clean_chain
= True
16 self
.nodes
[2].generate(101)
19 self
.test_no_blockhash()
20 self
.test_invalid_blockhash()
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)
30 txs
= self
.nodes
[0].listtransactions()
31 assert_array_result(txs
, {"txid": txid
}, {
32 "category": "receive",
34 "blockhash": blockhash
,
38 self
.nodes
[0].listsinceblock(),
39 {"lastblock": blockhash
,
43 self
.nodes
[0].listsinceblock(""),
44 {"lastblock": blockhash
,
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
,
58 `listsinceblock` did not behave correctly when handed a block that was
59 no longer in the main chain:
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
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
82 This test only checks that [tx0] is present.
85 # Split network into two
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:]])
100 # listsinceblock(lastblockhash) should now include tx, as seen from nodes[0]
101 lsbres
= self
.nodes
[0].listsinceblock(lastblockhash
)
103 for tx
in lsbres
['transactions']:
104 if tx
['txid'] == senttx
:
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.
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
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
141 # Split network into two
144 # share utxo between nodes[1] and nodes[2]
145 utxos
= self
.nodes
[2].listunspent()
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)
153 self
.nodes
[0].getnewaddress(): 1,
154 self
.nodes
[1].getnewaddress(): change
,
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]
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)
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
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
220 # Split network into two
223 # create and sign a transaction
224 utxos
= self
.nodes
[2].listunspent()
226 change
= '%.8f' % (float(utxo
['amount']) - 1.0003)
228 self
.nodes
[0].getnewaddress(): 1,
229 self
.nodes
[2].getnewaddress(): change
,
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)
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()