2017-01-11 12:02:25 +01:00
#!/usr/bin/env python3
2020-12-31 09:48:25 +01:00
# Copyright (c) 2017-2020 The Bitcoin Core developers
2017-01-11 12:02:25 +01:00
# Distributed under the MIT software license, see the accompanying
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
2019-11-20 16:22:53 +01:00
""" Test the listsinceblock RPC. """
2017-01-11 12:02:25 +01:00
2020-04-06 21:50:25 +02:00
from test_framework . address import key_to_p2wpkh
2021-05-17 16:38:19 +02:00
from test_framework . blocktools import COINBASE_MATURITY
2020-04-06 21:50:25 +02:00
from test_framework . key import ECKey
2017-01-11 12:02:25 +01:00
from test_framework . test_framework import BitcoinTestFramework
2019-10-21 21:42:06 +02:00
from test_framework . messages import BIP125_SEQUENCE_NUMBER
2019-09-16 15:22:49 +02:00
from test_framework . util import (
assert_array_result ,
assert_equal ,
assert_raises_rpc_error ,
)
2020-04-06 21:50:25 +02:00
from test_framework . wallet_util import bytes_to_wif
2017-01-11 12:02:25 +01:00
2019-10-21 21:42:06 +02:00
from decimal import Decimal
2019-09-16 15:22:49 +02:00
class ListSinceBlockTest ( BitcoinTestFramework ) :
2017-06-10 00:21:21 +02:00
def set_test_params ( self ) :
2017-08-24 17:11:56 +02:00
self . num_nodes = 4
2017-01-11 12:02:25 +01:00
self . setup_clean_chain = True
2018-09-09 19:32:37 +02:00
def skip_test_if_missing_module ( self ) :
self . skip_if_no_wallet ( )
2017-01-24 06:28:34 +01:00
def run_test ( self ) :
2019-09-16 15:22:49 +02:00
# All nodes are in IBD from genesis, so they'll need the miner (node2) to be an outbound connection, or have
# only one connection. (See fPreferredDownload in net_processing)
2020-09-17 09:46:07 +02:00
self . connect_nodes ( 1 , 2 )
2021-08-19 17:10:24 +02:00
self . generate ( self . nodes [ 2 ] , COINBASE_MATURITY + 1 )
2017-01-24 06:28:34 +01:00
self . sync_all ( )
2017-10-26 13:10:59 +02:00
self . test_no_blockhash ( )
self . test_invalid_blockhash ( )
2017-01-24 06:28:34 +01:00
self . test_reorg ( )
self . test_double_spend ( )
self . test_double_send ( )
2019-10-21 21:42:06 +02:00
self . double_spends_filtered ( )
2020-08-02 09:53:41 +02:00
self . test_targetconfirmations ( )
2017-01-24 06:28:34 +01:00
2017-10-26 13:10:59 +02:00
def test_no_blockhash ( self ) :
2019-11-20 16:22:53 +01:00
self . log . info ( " Test no blockhash " )
2017-10-26 13:10:59 +02:00
txid = self . nodes [ 2 ] . sendtoaddress ( self . nodes [ 0 ] . getnewaddress ( ) , 1 )
2021-09-29 18:10:12 +02:00
self . sync_all ( )
assert_array_result ( self . nodes [ 0 ] . listtransactions ( ) , { " txid " : txid } , {
" category " : " receive " ,
" amount " : 1 ,
" confirmations " : 0 ,
" trusted " : False ,
} )
2021-08-19 17:10:24 +02:00
blockhash , = self . generate ( self . nodes [ 2 ] , 1 )
2019-11-11 01:40:11 +01:00
blockheight = self . nodes [ 2 ] . getblockheader ( blockhash ) [ ' height ' ]
2017-10-26 13:10:59 +02:00
txs = self . nodes [ 0 ] . listtransactions ( )
assert_array_result ( txs , { " txid " : txid } , {
" category " : " receive " ,
" amount " : 1 ,
" blockhash " : blockhash ,
2019-11-11 01:40:11 +01:00
" blockheight " : blockheight ,
2017-10-26 13:10:59 +02:00
" confirmations " : 1 ,
} )
2021-09-29 18:10:12 +02:00
assert_equal ( len ( txs ) , 1 )
assert " trusted " not in txs [ 0 ]
2017-10-26 13:10:59 +02:00
assert_equal (
self . nodes [ 0 ] . listsinceblock ( ) ,
{ " lastblock " : blockhash ,
" removed " : [ ] ,
" transactions " : txs } )
assert_equal (
self . nodes [ 0 ] . listsinceblock ( " " ) ,
{ " lastblock " : blockhash ,
" removed " : [ ] ,
" transactions " : txs } )
def test_invalid_blockhash ( self ) :
2019-11-20 16:22:53 +01:00
self . log . info ( " Test invalid blockhash " )
2017-10-26 13:10:59 +02:00
assert_raises_rpc_error ( - 5 , " Block not found " , self . nodes [ 0 ] . listsinceblock ,
" 42759cde25462784395a337460bde75f58e73d3f08bd31fdc3507cbac856a2c4 " )
assert_raises_rpc_error ( - 5 , " Block not found " , self . nodes [ 0 ] . listsinceblock ,
" 0000000000000000000000000000000000000000000000000000000000000000 " )
2018-06-08 20:16:07 +02:00
assert_raises_rpc_error ( - 8 , " blockhash must be of length 64 (not 11, for ' invalid-hex ' ) " , self . nodes [ 0 ] . listsinceblock ,
2017-10-26 13:10:59 +02:00
" invalid-hex " )
2018-06-08 20:16:07 +02:00
assert_raises_rpc_error ( - 8 , " blockhash must be hexadecimal string (not ' Z000000000000000000000000000000000000000000000000000000000000000 ' ) " , self . nodes [ 0 ] . listsinceblock ,
" Z000000000000000000000000000000000000000000000000000000000000000 " )
2017-10-26 13:10:59 +02:00
2020-08-02 09:53:41 +02:00
def test_targetconfirmations ( self ) :
'''
This tests when the value of target_confirmations exceeds the number of
blocks in the main chain . In this case , the genesis block hash should be
given for the ` lastblock ` property . If target_confirmations is < 1 , then
a - 8 invalid parameter error is thrown .
'''
self . log . info ( " Test target_confirmations " )
2021-08-19 17:10:24 +02:00
blockhash , = self . generate ( self . nodes [ 2 ] , 1 )
2020-08-02 09:53:41 +02:00
blockheight = self . nodes [ 2 ] . getblockheader ( blockhash ) [ ' height ' ]
assert_equal (
self . nodes [ 0 ] . getblockhash ( 0 ) ,
self . nodes [ 0 ] . listsinceblock ( blockhash , blockheight + 1 ) [ ' lastblock ' ] )
assert_equal (
self . nodes [ 0 ] . getblockhash ( 0 ) ,
self . nodes [ 0 ] . listsinceblock ( blockhash , blockheight + 1000 ) [ ' lastblock ' ] )
assert_raises_rpc_error ( - 8 , " Invalid parameter " ,
self . nodes [ 0 ] . listsinceblock , blockhash , 0 )
2017-01-24 06:28:34 +01:00
def test_reorg ( self ) :
2017-01-11 12:02:25 +01:00
'''
` listsinceblock ` did not behave correctly when handed a block that was
no longer in the main chain :
ab0
/ \
aa1 [ tx0 ] bb1
| |
aa2 bb2
| |
aa3 bb3
|
bb4
Consider a client that has only seen block ` aa3 ` above . It asks the node
to ` listsinceblock aa3 ` . But at some point prior the main chain switched
to the bb chain .
Previously : listsinceblock would find height = 4 for block aa3 and compare
this to height = 5 for the tip of the chain ( bb4 ) . It would then return
results restricted to bb3 - bb4 .
Now : listsinceblock finds the fork at ab0 and returns results in the
range bb1 - bb4 .
This test only checks that [ tx0 ] is present .
'''
2019-11-20 16:22:53 +01:00
self . log . info ( " Test reorg " )
2017-01-11 12:02:25 +01:00
# Split network into two
self . split_network ( )
# send to nodes[0] from nodes[2]
senttx = self . nodes [ 2 ] . sendtoaddress ( self . nodes [ 0 ] . getnewaddress ( ) , 1 )
# generate on both sides
2020-11-10 18:02:31 +01:00
nodes1_last_blockhash = self . generate ( self . nodes [ 1 ] , 6 , sync_fun = lambda : self . sync_all ( self . nodes [ : 2 ] ) ) [ - 1 ]
nodes2_first_blockhash = self . generate ( self . nodes [ 2 ] , 7 , sync_fun = lambda : self . sync_all ( self . nodes [ 2 : ] ) ) [ 0 ]
2019-11-20 16:20:33 +01:00
self . log . debug ( " nodes[1] last blockhash = {} " . format ( nodes1_last_blockhash ) )
self . log . debug ( " nodes[2] first blockhash = {} " . format ( nodes2_first_blockhash ) )
2017-01-11 12:02:25 +01:00
self . join_network ( )
2019-11-20 16:20:33 +01:00
# listsinceblock(nodes1_last_blockhash) should now include tx as seen from nodes[0]
# and return the block height which listsinceblock now exposes since a5e7795.
transactions = self . nodes [ 0 ] . listsinceblock ( nodes1_last_blockhash ) [ ' transactions ' ]
found = next ( tx for tx in transactions if tx [ ' txid ' ] == senttx )
assert_equal ( found [ ' blockheight ' ] , self . nodes [ 0 ] . getblockheader ( nodes2_first_blockhash ) [ ' height ' ] )
2017-01-24 06:28:34 +01:00
def test_double_spend ( self ) :
'''
This tests the case where the same UTXO is spent twice on two separate
blocks as part of a reorg .
ab0
/ \
aa1 [ tx1 ] bb1 [ tx2 ]
| |
aa2 bb2
| |
aa3 bb3
|
bb4
Problematic case :
1. User 1 receives BTC in tx1 from utxo1 in block aa1 .
2. User 2 receives BTC in tx2 from utxo1 ( same ) in block bb1
3. User 1 sees 2 confirmations at block aa3 .
4. Reorg into bb chain .
5. User 1 asks ` listsinceblock aa3 ` and does not see that tx1 is now
invalidated .
Currently the solution to this is to detect that a reorg ' d block is
asked for in listsinceblock , and to iterate back over existing blocks up
until the fork point , and to include all transactions that relate to the
node wallet .
'''
2019-11-20 16:22:53 +01:00
self . log . info ( " Test double spend " )
2017-01-24 06:28:34 +01:00
self . sync_all ( )
# share utxo between nodes[1] and nodes[2]
2020-04-06 21:50:25 +02:00
eckey = ECKey ( )
eckey . generate ( )
privkey = bytes_to_wif ( eckey . get_bytes ( ) )
address = key_to_p2wpkh ( eckey . get_pubkey ( ) . get_bytes ( ) )
self . nodes [ 2 ] . sendtoaddress ( address , 10 )
2021-08-19 17:10:24 +02:00
self . generate ( self . nodes [ 2 ] , 6 )
2020-11-05 20:00:02 +01:00
self . sync_all ( )
2020-04-06 21:50:25 +02:00
self . nodes [ 2 ] . importprivkey ( privkey )
2017-01-24 06:28:34 +01:00
utxos = self . nodes [ 2 ] . listunspent ( )
2020-04-06 21:50:25 +02:00
utxo = [ u for u in utxos if u [ " address " ] == address ] [ 0 ]
2017-01-24 06:28:34 +01:00
self . nodes [ 1 ] . importprivkey ( privkey )
2020-04-06 21:50:25 +02:00
# Split network into two
self . split_network ( )
2017-01-24 06:28:34 +01:00
# send from nodes[1] using utxo to nodes[0]
change = ' %.8f ' % ( float ( utxo [ ' amount ' ] ) - 1.0003 )
2018-04-24 21:55:53 +02:00
recipient_dict = {
2017-01-24 06:28:34 +01:00
self . nodes [ 0 ] . getnewaddress ( ) : 1 ,
self . nodes [ 1 ] . getnewaddress ( ) : change ,
}
2018-04-24 21:55:53 +02:00
utxo_dicts = [ {
2017-01-24 06:28:34 +01:00
' txid ' : utxo [ ' txid ' ] ,
' vout ' : utxo [ ' vout ' ] ,
} ]
txid1 = self . nodes [ 1 ] . sendrawtransaction (
2017-09-06 01:49:18 +02:00
self . nodes [ 1 ] . signrawtransactionwithwallet (
2018-04-24 21:55:53 +02:00
self . nodes [ 1 ] . createrawtransaction ( utxo_dicts , recipient_dict ) ) [ ' hex ' ] )
2017-01-24 06:28:34 +01:00
# send from nodes[2] using utxo to nodes[3]
2018-04-24 21:55:53 +02:00
recipient_dict2 = {
2017-01-24 06:28:34 +01:00
self . nodes [ 3 ] . getnewaddress ( ) : 1 ,
self . nodes [ 2 ] . getnewaddress ( ) : change ,
}
self . nodes [ 2 ] . sendrawtransaction (
2017-09-06 01:49:18 +02:00
self . nodes [ 2 ] . signrawtransactionwithwallet (
2018-04-24 21:55:53 +02:00
self . nodes [ 2 ] . createrawtransaction ( utxo_dicts , recipient_dict2 ) ) [ ' hex ' ] )
2017-01-24 06:28:34 +01:00
# generate on both sides
2020-11-10 18:02:31 +01:00
lastblockhash = self . generate ( self . nodes [ 1 ] , 3 , sync_fun = self . no_op ) [ 2 ]
self . generate ( self . nodes [ 2 ] , 4 , sync_fun = self . no_op )
2017-01-24 06:28:34 +01:00
self . join_network ( )
self . sync_all ( )
# gettransaction should work for txid1
assert self . nodes [ 0 ] . gettransaction ( txid1 ) [ ' txid ' ] == txid1 , " gettransaction failed to find txid1 "
# listsinceblock(lastblockhash) should now include txid1, as seen from nodes[0]
lsbres = self . nodes [ 0 ] . listsinceblock ( lastblockhash )
assert any ( tx [ ' txid ' ] == txid1 for tx in lsbres [ ' removed ' ] )
# but it should not include 'removed' if include_removed=false
lsbres2 = self . nodes [ 0 ] . listsinceblock ( blockhash = lastblockhash , include_removed = False )
assert ' removed ' not in lsbres2
def test_double_send ( self ) :
'''
This tests the case where the same transaction is submitted twice on two
separate blocks as part of a reorg . The former will vanish and the
latter will appear as the true transaction ( with confirmations dropping
as a result ) .
ab0
/ \
aa1 [ tx1 ] bb1
| |
aa2 bb2
| |
aa3 bb3 [ tx1 ]
|
bb4
Asserted :
1. tx1 is listed in listsinceblock .
2. It is included in ' removed ' as it was removed , even though it is now
present in a different block .
2018-03-18 15:26:45 +01:00
3. It is listed with a confirmation count of 2 ( bb3 , bb4 ) , not
2017-01-24 06:28:34 +01:00
3 ( aa1 , aa2 , aa3 ) .
'''
2019-11-20 16:22:53 +01:00
self . log . info ( " Test double send " )
2017-01-24 06:28:34 +01:00
self . sync_all ( )
# Split network into two
self . split_network ( )
# create and sign a transaction
utxos = self . nodes [ 2 ] . listunspent ( )
utxo = utxos [ 0 ]
change = ' %.8f ' % ( float ( utxo [ ' amount ' ] ) - 1.0003 )
2018-04-24 21:55:53 +02:00
recipient_dict = {
2017-01-24 06:28:34 +01:00
self . nodes [ 0 ] . getnewaddress ( ) : 1 ,
self . nodes [ 2 ] . getnewaddress ( ) : change ,
}
2018-04-24 21:55:53 +02:00
utxo_dicts = [ {
2017-01-24 06:28:34 +01:00
' txid ' : utxo [ ' txid ' ] ,
' vout ' : utxo [ ' vout ' ] ,
} ]
2017-09-06 01:49:18 +02:00
signedtxres = self . nodes [ 2 ] . signrawtransactionwithwallet (
2018-04-24 21:55:53 +02:00
self . nodes [ 2 ] . createrawtransaction ( utxo_dicts , recipient_dict ) )
2017-01-24 06:28:34 +01:00
assert signedtxres [ ' complete ' ]
signedtx = signedtxres [ ' hex ' ]
# send from nodes[1]; this will end up in aa1
txid1 = self . nodes [ 1 ] . sendrawtransaction ( signedtx )
# generate bb1-bb2 on right side
2020-11-10 18:02:31 +01:00
self . generate ( self . nodes [ 2 ] , 2 , sync_fun = self . no_op )
2017-01-24 06:28:34 +01:00
# send from nodes[2]; this will end up in bb3
txid2 = self . nodes [ 2 ] . sendrawtransaction ( signedtx )
assert_equal ( txid1 , txid2 )
# generate on both sides
2020-11-10 18:02:31 +01:00
lastblockhash = self . generate ( self . nodes [ 1 ] , 3 , sync_fun = self . no_op ) [ 2 ]
self . generate ( self . nodes [ 2 ] , 2 , sync_fun = self . no_op )
2017-01-24 06:28:34 +01:00
self . join_network ( )
self . sync_all ( )
# gettransaction should work for txid1
2019-11-11 01:40:11 +01:00
tx1 = self . nodes [ 0 ] . gettransaction ( txid1 )
assert_equal ( tx1 [ ' blockheight ' ] , self . nodes [ 0 ] . getblockheader ( tx1 [ ' blockhash ' ] ) [ ' height ' ] )
2017-01-24 06:28:34 +01:00
# listsinceblock(lastblockhash) should now include txid1 in transactions
# as well as in removed
lsbres = self . nodes [ 0 ] . listsinceblock ( lastblockhash )
assert any ( tx [ ' txid ' ] == txid1 for tx in lsbres [ ' transactions ' ] )
assert any ( tx [ ' txid ' ] == txid1 for tx in lsbres [ ' removed ' ] )
# find transaction and ensure confirmations is valid
for tx in lsbres [ ' transactions ' ] :
if tx [ ' txid ' ] == txid1 :
assert_equal ( tx [ ' confirmations ' ] , 2 )
# the same check for the removed array; confirmations should STILL be 2
for tx in lsbres [ ' removed ' ] :
if tx [ ' txid ' ] == txid1 :
assert_equal ( tx [ ' confirmations ' ] , 2 )
2017-01-11 12:02:25 +01:00
2019-10-21 21:42:06 +02:00
def double_spends_filtered ( self ) :
'''
` listsinceblock ` was returning conflicted transactions even if they
occurred before the specified cutoff blockhash
'''
2019-11-20 16:22:53 +01:00
self . log . info ( " Test spends filtered " )
2019-10-21 21:42:06 +02:00
spending_node = self . nodes [ 2 ]
dest_address = spending_node . getnewaddress ( )
tx_input = dict (
sequence = BIP125_SEQUENCE_NUMBER , * * next ( u for u in spending_node . listunspent ( ) ) )
rawtx = spending_node . createrawtransaction (
[ tx_input ] , { dest_address : tx_input [ " amount " ] - Decimal ( " 0.00051000 " ) ,
spending_node . getrawchangeaddress ( ) : Decimal ( " 0.00050000 " ) } )
signedtx = spending_node . signrawtransactionwithwallet ( rawtx )
orig_tx_id = spending_node . sendrawtransaction ( signedtx [ " hex " ] )
original_tx = spending_node . gettransaction ( orig_tx_id )
double_tx = spending_node . bumpfee ( orig_tx_id )
# check that both transactions exist
block_hash = spending_node . listsinceblock (
spending_node . getblockhash ( spending_node . getblockcount ( ) ) )
original_found = False
double_found = False
for tx in block_hash [ ' transactions ' ] :
if tx [ ' txid ' ] == original_tx [ ' txid ' ] :
original_found = True
if tx [ ' txid ' ] == double_tx [ ' txid ' ] :
double_found = True
assert_equal ( original_found , True )
assert_equal ( double_found , True )
2021-08-19 17:10:24 +02:00
lastblockhash = self . generate ( spending_node , 1 ) [ 0 ]
2019-10-21 21:42:06 +02:00
# check that neither transaction exists
block_hash = spending_node . listsinceblock ( lastblockhash )
original_found = False
double_found = False
for tx in block_hash [ ' transactions ' ] :
if tx [ ' txid ' ] == original_tx [ ' txid ' ] :
original_found = True
if tx [ ' txid ' ] == double_tx [ ' txid ' ] :
double_found = True
assert_equal ( original_found , False )
assert_equal ( double_found , False )
2017-01-11 12:02:25 +01:00
if __name__ == ' __main__ ' :
ListSinceBlockTest ( ) . main ( )