Skip to content

Commit 9017d55

Browse files
committed
Merge #15946: Allow maintaining the blockfilterindex when using prune
84716b1 Add "index/blockfilterindex -> validation -> index/blockfilterindex" to expected circular dependencies (Jonas Schnelli) ab3a0a2 Add functional test for blockfilterindex in prune-mode (Jonas Schnelli) c286a22 Add debug startup parameter -fastprune for more effective pruning tests (Jonas Schnelli) 5e11226 Avoid pruning below the blockfilterindex sync height (Jonas Schnelli) 00d57ff Avoid accessing nullpointer in BaseIndex::GetSummary() (Jonas Schnelli) 6abe9f5 Allow blockfilter in conjunction with prune (Jonas Schnelli) Pull request description: Maintaining the blockfilterindexes in prune mode is possible and may lead to efficient p2p based rescans of wallets (restore backups, import/sweep keys) beyond the prune height (rescans not part of that PR). This PR allows running the blockfilterindex(es) in conjunction with pruning. * Bitcoind/Qt will shutdown during startup when missing block data has been detected ([re]enable `-blockfilterindex` when we already have pruned) * manual block pruning is disabled during blockfilterindex sync * auto-pruning is delayed during blockfilterindex sync ToDos: * [x] Functional tests ACKs for top commit: fjahr: Code review ACK 84716b1 ryanofsky: Code review ACK 84716b1. Only changes since last review were suggested new FindFilesToPrune argument and test. benthecarman: tACK 84716b1 Tree-SHA512: 91d832c6c562c463f7ec7655c08956385413a99a896640b9737bda0183607fac530435d03d87c3c0e70c61ccdfe73fe8f3639bc7d26d33ca7e60925ebb97d77a
2 parents cd66d8b + 84716b1 commit 9017d55

File tree

8 files changed

+111
-12
lines changed

8 files changed

+111
-12
lines changed

src/chainparams.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,7 @@ class CRegTestParams : public CChainParams {
412412
pchMessageStart[2] = 0xb5;
413413
pchMessageStart[3] = 0xda;
414414
nDefaultPort = 18444;
415-
nPruneAfterHeight = 1000;
415+
nPruneAfterHeight = gArgs.GetBoolArg("-fastprune", false) ? 100 : 1000;
416416
m_assumed_blockchain_size = 0;
417417
m_assumed_chain_state_size = 0;
418418

src/index/base.cpp

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,43 @@ bool BaseIndex::Init()
6565
m_best_block_index = g_chainman.m_blockman.FindForkInGlobalIndex(::ChainActive(), locator);
6666
}
6767
m_synced = m_best_block_index.load() == ::ChainActive().Tip();
68+
if (!m_synced) {
69+
bool prune_violation = false;
70+
if (!m_best_block_index) {
71+
// index is not built yet
72+
// make sure we have all block data back to the genesis
73+
const CBlockIndex* block = ::ChainActive().Tip();
74+
while (block->pprev && (block->pprev->nStatus & BLOCK_HAVE_DATA)) {
75+
block = block->pprev;
76+
}
77+
prune_violation = block != ::ChainActive().Genesis();
78+
}
79+
// in case the index has a best block set and is not fully synced
80+
// check if we have the required blocks to continue building the index
81+
else {
82+
const CBlockIndex* block_to_test = m_best_block_index.load();
83+
if (!ChainActive().Contains(block_to_test)) {
84+
// if the bestblock is not part of the mainchain, find the fork
85+
// and make sure we have all data down to the fork
86+
block_to_test = ::ChainActive().FindFork(block_to_test);
87+
}
88+
const CBlockIndex* block = ::ChainActive().Tip();
89+
prune_violation = true;
90+
// check backwards from the tip if we have all block data until we reach the indexes bestblock
91+
while (block_to_test && block->pprev && (block->pprev->nStatus & BLOCK_HAVE_DATA)) {
92+
if (block_to_test == block) {
93+
prune_violation = false;
94+
break;
95+
}
96+
block = block->pprev;
97+
}
98+
}
99+
if (prune_violation) {
100+
// throw error and graceful shutdown if we can't build the index
101+
FatalError("%s: %s best block of the index goes beyond pruned data. Please disable the index or reindex (which will download the whole blockchain again)", __func__, GetName());
102+
return false;
103+
}
104+
}
68105
return true;
69106
}
70107

@@ -177,6 +214,10 @@ bool BaseIndex::Rewind(const CBlockIndex* current_tip, const CBlockIndex* new_ti
177214
assert(current_tip->GetAncestor(new_tip->nHeight) == new_tip);
178215

179216
// In the case of a reorg, ensure persisted block locator is not stale.
217+
// Pruning has a minimum of 288 blocks-to-keep and getting the index
218+
// out of sync may be possible but a users fault.
219+
// In case we reorg beyond the pruned depth, ReadBlockFromDisk would
220+
// throw and lead to a graceful shutdown
180221
m_best_block_index = new_tip;
181222
if (!Commit()) {
182223
// If commit fails, revert the best block index to avoid corruption.
@@ -325,6 +366,6 @@ IndexSummary BaseIndex::GetSummary() const
325366
IndexSummary summary{};
326367
summary.name = GetName();
327368
summary.synced = m_synced;
328-
summary.best_block_height = m_best_block_index.load()->nHeight;
369+
summary.best_block_height = m_best_block_index ? m_best_block_index.load()->nHeight : 0;
329370
return summary;
330371
}

src/init.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,7 @@ void SetupServerArgs(NodeContext& node)
386386
#endif
387387
argsman.AddArg("-assumevalid=<hex>", strprintf("If this block is in the chain assume that it and its ancestors are valid and potentially skip their script verification (0 to verify all, default: %s, testnet: %s, signet: %s)", defaultChainParams->GetConsensus().defaultAssumeValid.GetHex(), testnetChainParams->GetConsensus().defaultAssumeValid.GetHex(), signetChainParams->GetConsensus().defaultAssumeValid.GetHex()), ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
388388
argsman.AddArg("-blocksdir=<dir>", "Specify directory to hold blocks subdirectory for *.dat files (default: <datadir>)", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
389+
argsman.AddArg("-fastprune", "Use smaller block files and lower minimum prune height for testing purposes", ArgsManager::ALLOW_ANY | ArgsManager::DEBUG_ONLY, OptionsCategory::DEBUG_TEST);
389390
#if HAVE_SYSTEM
390391
argsman.AddArg("-blocknotify=<cmd>", "Execute command when the best block changes (%s in cmd is replaced by block hash)", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
391392
#endif
@@ -1028,9 +1029,6 @@ bool AppInitParameterInteraction(const ArgsManager& args)
10281029
if (args.GetArg("-prune", 0)) {
10291030
if (args.GetBoolArg("-txindex", DEFAULT_TXINDEX))
10301031
return InitError(_("Prune mode is incompatible with -txindex."));
1031-
if (!g_enabled_filter_types.empty()) {
1032-
return InitError(_("Prune mode is incompatible with -blockfilterindex."));
1033-
}
10341032
}
10351033

10361034
// -bind and -whitebind can't be set when not listening

src/validation.cpp

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <cuckoocache.h>
1818
#include <flatfile.h>
1919
#include <hash.h>
20+
#include <index/blockfilterindex.h>
2021
#include <index/txindex.h>
2122
#include <logging.h>
2223
#include <logging/timer.h>
@@ -2240,17 +2241,25 @@ bool CChainState::FlushStateToDisk(
22402241
{
22412242
bool fFlushForPrune = false;
22422243
bool fDoFullFlush = false;
2244+
22432245
CoinsCacheSizeState cache_state = GetCoinsCacheSizeState(&m_mempool);
22442246
LOCK(cs_LastBlockFile);
22452247
if (fPruneMode && (fCheckForPruning || nManualPruneHeight > 0) && !fReindex) {
2248+
// make sure we don't prune above the blockfilterindexes bestblocks
2249+
// pruning is height-based
2250+
int last_prune = m_chain.Height(); // last height we can prune
2251+
ForEachBlockFilterIndex([&](BlockFilterIndex& index) {
2252+
last_prune = std::max(1, std::min(last_prune, index.GetSummary().best_block_height));
2253+
});
2254+
22462255
if (nManualPruneHeight > 0) {
22472256
LOG_TIME_MILLIS_WITH_CATEGORY("find files to prune (manual)", BCLog::BENCH);
22482257

2249-
m_blockman.FindFilesToPruneManual(setFilesToPrune, nManualPruneHeight, m_chain.Height());
2258+
m_blockman.FindFilesToPruneManual(setFilesToPrune, std::min(last_prune, nManualPruneHeight), m_chain.Height());
22502259
} else {
22512260
LOG_TIME_MILLIS_WITH_CATEGORY("find files to prune", BCLog::BENCH);
22522261

2253-
m_blockman.FindFilesToPrune(setFilesToPrune, chainparams.PruneAfterHeight(), m_chain.Height(), IsInitialBlockDownload());
2262+
m_blockman.FindFilesToPrune(setFilesToPrune, chainparams.PruneAfterHeight(), m_chain.Height(), last_prune, IsInitialBlockDownload());
22542263
fCheckForPruning = false;
22552264
}
22562265
if (!setFilesToPrune.empty()) {
@@ -3194,7 +3203,7 @@ static bool FindBlockPos(FlatFilePos &pos, unsigned int nAddSize, unsigned int n
31943203

31953204
bool finalize_undo = false;
31963205
if (!fKnown) {
3197-
while (vinfoBlockFile[nFile].nSize + nAddSize >= MAX_BLOCKFILE_SIZE) {
3206+
while (vinfoBlockFile[nFile].nSize + nAddSize >= (gArgs.GetBoolArg("-fastprune", false) ? 0x10000 /* 64kb */ : MAX_BLOCKFILE_SIZE)) {
31983207
// when the undo file is keeping up with the block file, we want to flush it explicitly
31993208
// when it is lagging behind (more blocks arrive than are being connected), we let the
32003209
// undo block write case handle it
@@ -3925,7 +3934,7 @@ void PruneBlockFilesManual(int nManualPruneHeight)
39253934
}
39263935
}
39273936

3928-
void BlockManager::FindFilesToPrune(std::set<int>& setFilesToPrune, uint64_t nPruneAfterHeight, int chain_tip_height, bool is_ibd)
3937+
void BlockManager::FindFilesToPrune(std::set<int>& setFilesToPrune, uint64_t nPruneAfterHeight, int chain_tip_height, int prune_height, bool is_ibd)
39293938
{
39303939
LOCK2(cs_main, cs_LastBlockFile);
39313940
if (chain_tip_height < 0 || nPruneTarget == 0) {
@@ -3935,7 +3944,7 @@ void BlockManager::FindFilesToPrune(std::set<int>& setFilesToPrune, uint64_t nPr
39353944
return;
39363945
}
39373946

3938-
unsigned int nLastBlockWeCanPrune = chain_tip_height - MIN_BLOCKS_TO_KEEP;
3947+
unsigned int nLastBlockWeCanPrune = std::min(prune_height, chain_tip_height - static_cast<int>(MIN_BLOCKS_TO_KEEP));
39393948
uint64_t nCurrentUsage = CalculateCurrentUsage();
39403949
// We don't check to prune until after we've allocated new space for files
39413950
// So we should leave a buffer under our target to account for another allocation
@@ -3986,7 +3995,7 @@ void BlockManager::FindFilesToPrune(std::set<int>& setFilesToPrune, uint64_t nPr
39863995

39873996
static FlatFileSeq BlockFileSeq()
39883997
{
3989-
return FlatFileSeq(GetBlocksDir(), "blk", BLOCKFILE_CHUNK_SIZE);
3998+
return FlatFileSeq(GetBlocksDir(), "blk", gArgs.GetBoolArg("-fastprune", false) ? 0x4000 /* 16kb */ : BLOCKFILE_CHUNK_SIZE);
39903999
}
39914000

39924001
static FlatFileSeq UndoFileSeq()

src/validation.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ class BlockManager
399399
*
400400
* @param[out] setFilesToPrune The set of file indices that can be unlinked will be returned
401401
*/
402-
void FindFilesToPrune(std::set<int>& setFilesToPrune, uint64_t nPruneAfterHeight, int chain_tip_height, bool is_ibd);
402+
void FindFilesToPrune(std::set<int>& setFilesToPrune, uint64_t nPruneAfterHeight, int chain_tip_height, int prune_height, bool is_ibd);
403403

404404
public:
405405
BlockMap m_block_index GUARDED_BY(cs_main);
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#!/usr/bin/env python3
2+
# Copyright (c) 2020 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 blockfilterindex in conjunction with prune."""
6+
from test_framework.test_framework import BitcoinTestFramework
7+
from test_framework.util import (
8+
assert_raises_rpc_error,
9+
assert_greater_than,
10+
)
11+
12+
class FeatureBlockfilterindexPruneTest(BitcoinTestFramework):
13+
def set_test_params(self):
14+
self.num_nodes = 2
15+
self.extra_args = [["-fastprune", "-prune=1"], ["-fastprune", "-prune=1", "-blockfilterindex=1"]]
16+
17+
def run_test(self):
18+
# test basic pruning compatibility & filter access of pruned blocks
19+
self.log.info("check if we can access a blockfilter when pruning is enabled but no blocks are actually pruned")
20+
assert(len(self.nodes[1].getblockfilter(self.nodes[1].getbestblockhash())['filter']) > 0)
21+
self.nodes[1].generate(500)
22+
self.sync_all()
23+
self.log.info("prune some blocks")
24+
pruneheight = self.nodes[1].pruneblockchain(400)
25+
assert(pruneheight != 0)
26+
self.log.info("check if we can access the tips blockfilter when we have pruned some blocks")
27+
assert(len(self.nodes[1].getblockfilter(self.nodes[1].getbestblockhash())['filter']) > 0)
28+
self.log.info("check if we can access the blockfilter of a pruned block")
29+
assert(len(self.nodes[1].getblockfilter(self.nodes[1].getblockhash(2))['filter']) > 0)
30+
self.log.info("start node without blockfilterindex")
31+
self.stop_node(1)
32+
self.start_node(1, extra_args=self.extra_args[0])
33+
self.log.info("make sure accessing the blockfilters throws an error")
34+
assert_raises_rpc_error(-1,"Index is not enabled for filtertype basic", self.nodes[1].getblockfilter, self.nodes[1].getblockhash(2))
35+
self.nodes[1].generate(1000)
36+
self.log.info("prune below the blockfilterindexes best block while blockfilters are disabled")
37+
pruneheight_new = self.nodes[1].pruneblockchain(1000)
38+
assert_greater_than(pruneheight_new, pruneheight)
39+
self.stop_node(1)
40+
self.log.info("make sure we get an init error when starting the node again with block filters")
41+
self.nodes[1].assert_start_raises_init_error(extra_args=self.extra_args[1])
42+
self.nodes[1].assert_debug_log(["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)"])
43+
self.log.info("make sure the node starts again with the -reindex arg")
44+
reindex_args = self.extra_args[1]
45+
reindex_args.append("-reindex")
46+
self.start_node(1, extra_args=reindex_args)
47+
48+
if __name__ == '__main__':
49+
FeatureBlockfilterindexPruneTest().main()

test/functional/test_runner.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,7 @@
289289
'feature_help.py',
290290
'feature_shutdown.py',
291291
'p2p_ibd_txrelay.py',
292+
'feature_blockfilterindex_prune.py'
292293
# Don't append tests at the end to avoid merge conflicts
293294
# Put them in a random line within the section that fits their approximate run-time
294295
]

test/lint/lint-circular-dependencies.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export LC_ALL=C
1111
EXPECTED_CIRCULAR_DEPENDENCIES=(
1212
"chainparamsbase -> util/system -> chainparamsbase"
1313
"index/txindex -> validation -> index/txindex"
14+
"index/blockfilterindex -> validation -> index/blockfilterindex"
1415
"policy/fees -> txmempool -> policy/fees"
1516
"qt/addresstablemodel -> qt/walletmodel -> qt/addresstablemodel"
1617
"qt/bitcoingui -> qt/walletframe -> qt/bitcoingui"

0 commit comments

Comments
 (0)