2020-12-10 11:43:46 +01:00
#!/usr/bin/env python3
2021-07-28 13:57:16 +02:00
# Copyright (c) 2020-2021 The Bitcoin Core developers
2020-12-10 11:43:46 +01:00
# Distributed under the MIT software license, see the accompanying
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
2021-05-13 19:32:09 +02:00
""" Test indices in conjunction with prune. """
2020-12-10 11:43:46 +01:00
from test_framework . test_framework import BitcoinTestFramework
from test_framework . util import (
2021-02-21 08:15:32 +01:00
assert_equal ,
2020-12-10 11:43:46 +01:00
assert_greater_than ,
2021-02-21 08:15:32 +01:00
assert_raises_rpc_error ,
2020-12-10 11:43:46 +01:00
)
2021-02-18 20:46:39 +01:00
2021-05-13 19:32:09 +02:00
class FeatureIndexPruneTest ( BitcoinTestFramework ) :
2020-12-10 11:43:46 +01:00
def set_test_params ( self ) :
2021-05-13 19:32:09 +02:00
self . num_nodes = 4
self . extra_args = [
[ " -fastprune " , " -prune=1 " , " -blockfilterindex=1 " ] ,
[ " -fastprune " , " -prune=1 " , " -coinstatsindex=1 " ] ,
[ " -fastprune " , " -prune=1 " , " -blockfilterindex=1 " , " -coinstatsindex=1 " ] ,
[ ]
]
2020-12-10 11:43:46 +01:00
2021-02-24 22:56:37 +01:00
def sync_index ( self , height ) :
2021-05-13 19:32:09 +02:00
expected_filter = {
' basic block filter index ' : { ' synced ' : True , ' best_block_height ' : height } ,
}
self . wait_until ( lambda : self . nodes [ 0 ] . getindexinfo ( ) == expected_filter )
expected_stats = {
' coinstatsindex ' : { ' synced ' : True , ' best_block_height ' : height }
}
self . wait_until ( lambda : self . nodes [ 1 ] . getindexinfo ( ) == expected_stats )
expected = { * * expected_filter , * * expected_stats }
self . wait_until ( lambda : self . nodes [ 2 ] . getindexinfo ( ) == expected )
def reconnect_nodes ( self ) :
self . connect_nodes ( 0 , 1 )
self . connect_nodes ( 0 , 2 )
self . connect_nodes ( 0 , 3 )
def mine_batches ( self , blocks ) :
n = blocks / / 250
for _ in range ( n ) :
self . generate ( self . nodes [ 0 ] , 250 )
self . generate ( self . nodes [ 0 ] , blocks % 250 )
self . sync_blocks ( )
def restart_without_indices ( self ) :
for i in range ( 3 ) :
self . restart_node ( i , extra_args = [ " -fastprune " , " -prune=1 " ] )
self . reconnect_nodes ( )
2021-02-24 22:56:37 +01:00
2020-12-10 11:43:46 +01:00
def run_test ( self ) :
2021-05-13 19:32:09 +02:00
filter_nodes = [ self . nodes [ 0 ] , self . nodes [ 2 ] ]
stats_nodes = [ self . nodes [ 1 ] , self . nodes [ 2 ] ]
self . log . info ( " check if we can access blockfilters and coinstats when pruning is enabled but no blocks are actually pruned " )
2021-02-24 22:56:37 +01:00
self . sync_index ( height = 200 )
2021-05-13 19:32:09 +02:00
tip = self . nodes [ 0 ] . getbestblockhash ( )
for node in filter_nodes :
assert_greater_than ( len ( node . getblockfilter ( tip ) [ ' filter ' ] ) , 0 )
for node in stats_nodes :
assert ( node . gettxoutsetinfo ( hash_type = " muhash " , hash_or_height = tip ) [ ' muhash ' ] )
self . mine_batches ( 500 )
2021-02-24 22:56:37 +01:00
self . sync_index ( height = 700 )
2020-12-10 11:43:46 +01:00
self . log . info ( " prune some blocks " )
2021-05-13 19:32:09 +02:00
for node in self . nodes [ : 2 ] :
with node . assert_debug_log ( [ ' limited pruning to height 689 ' ] ) :
pruneheight_new = node . pruneblockchain ( 400 )
# the prune heights used here and below are magic numbers that are determined by the
# thresholds at which block files wrap, so they depend on disk serialization and default block file size.
2022-03-21 19:38:54 +01:00
assert_equal ( pruneheight_new , 248 )
2021-05-13 19:32:09 +02:00
self . log . info ( " check if we can access the tips blockfilter and coinstats when we have pruned some blocks " )
tip = self . nodes [ 0 ] . getbestblockhash ( )
for node in filter_nodes :
assert_greater_than ( len ( node . getblockfilter ( tip ) [ ' filter ' ] ) , 0 )
for node in stats_nodes :
assert ( node . gettxoutsetinfo ( hash_type = " muhash " , hash_or_height = tip ) [ ' muhash ' ] )
self . log . info ( " check if we can access the blockfilter and coinstats of a pruned block " )
height_hash = self . nodes [ 0 ] . getblockhash ( 2 )
for node in filter_nodes :
assert_greater_than ( len ( node . getblockfilter ( height_hash ) [ ' filter ' ] ) , 0 )
for node in stats_nodes :
assert ( node . gettxoutsetinfo ( hash_type = " muhash " , hash_or_height = height_hash ) [ ' muhash ' ] )
2021-02-24 22:56:37 +01:00
2021-10-28 00:59:40 +02:00
# mine and sync index up to a height that will later be the pruneheight
2022-04-01 10:26:54 +02:00
self . generate ( self . nodes [ 0 ] , 51 )
self . sync_index ( height = 751 )
2021-10-28 00:59:40 +02:00
2021-05-13 19:32:09 +02:00
self . restart_without_indices ( )
self . log . info ( " make sure trying to access the indices throws errors " )
for node in filter_nodes :
msg = " Index is not enabled for filtertype basic "
assert_raises_rpc_error ( - 1 , msg , node . getblockfilter , height_hash )
for node in stats_nodes :
msg = " Querying specific block heights requires coinstatsindex "
assert_raises_rpc_error ( - 8 , msg , node . gettxoutsetinfo , " muhash " , height_hash )
2021-02-24 22:56:37 +01:00
2021-05-13 19:32:09 +02:00
self . mine_batches ( 749 )
2021-10-28 00:59:40 +02:00
2021-05-13 19:32:09 +02:00
self . log . info ( " prune exactly up to the indices best blocks while the indices are disabled " )
for i in range ( 3 ) :
pruneheight_2 = self . nodes [ i ] . pruneblockchain ( 1000 )
2022-03-21 19:38:54 +01:00
assert_equal ( pruneheight_2 , 750 )
2021-05-13 19:32:09 +02:00
# Restart the nodes again with the indices activated
self . restart_node ( i , extra_args = self . extra_args [ i ] )
self . log . info ( " make sure that we can continue with the partially synced indices after having pruned up to the index height " )
2021-10-28 00:59:40 +02:00
self . sync_index ( height = 1500 )
2021-02-24 22:56:37 +01:00
2021-05-13 19:32:09 +02:00
self . log . info ( " prune further than the indices best blocks while the indices are disabled " )
self . restart_without_indices ( )
self . mine_batches ( 1000 )
for i in range ( 3 ) :
pruneheight_3 = self . nodes [ i ] . pruneblockchain ( 2000 )
assert_greater_than ( pruneheight_3 , pruneheight_2 )
self . stop_node ( i )
self . log . info ( " make sure we get an init error when starting the nodes again with the indices " )
filter_msg = " Error: basic block filter index best block of the index goes beyond pruned data. Please disable the index or reindex (which will download the whole blockchain again) "
stats_msg = " Error: coinstatsindex best block of the index goes beyond pruned data. Please disable the index or reindex (which will download the whole blockchain again) "
for i , msg in enumerate ( [ filter_msg , stats_msg , filter_msg ] ) :
self . nodes [ i ] . assert_start_raises_init_error ( extra_args = self . extra_args [ i ] , expected_msg = msg )
self . log . info ( " make sure the nodes start again with the indices and an additional -reindex arg " )
for i in range ( 3 ) :
2022-05-13 00:51:48 +02:00
restart_args = self . extra_args [ i ] + [ " -reindex " ]
2021-05-13 19:32:09 +02:00
self . restart_node ( i , extra_args = restart_args )
2022-05-13 00:51:48 +02:00
# The nodes need to be reconnected to the non-pruning node upon restart, otherwise they will be stuck
self . connect_nodes ( i , 3 )
2021-05-13 19:32:09 +02:00
self . sync_blocks ( timeout = 300 )
2021-02-24 22:56:37 +01:00
2021-05-13 19:32:09 +02:00
for node in self . nodes [ : 2 ] :
with node . assert_debug_log ( [ ' limited pruning to height 2489 ' ] ) :
pruneheight_new = node . pruneblockchain ( 2500 )
2022-03-21 19:38:54 +01:00
assert_equal ( pruneheight_new , 2005 )
2021-02-24 22:56:37 +01:00
2021-05-13 19:32:09 +02:00
self . log . info ( " ensure that prune locks don ' t prevent indices from failing in a reorg scenario " )
with self . nodes [ 0 ] . assert_debug_log ( [ ' basic block filter index prune lock moved back to 2480 ' ] ) :
self . nodes [ 3 ] . invalidateblock ( self . nodes [ 0 ] . getblockhash ( 2480 ) )
self . generate ( self . nodes [ 3 ] , 30 )
self . sync_blocks ( )
2020-12-10 11:43:46 +01:00
2021-02-18 20:46:39 +01:00
2020-12-10 11:43:46 +01:00
if __name__ == ' __main__ ' :
2021-05-13 19:32:09 +02:00
FeatureIndexPruneTest ( ) . main ( )