mirror of
https://github.com/bitcoin/bitcoin.git
synced 2026-05-14 07:54:12 +02:00
Merge #14121: Index for BIP 157 block filters
c7efb652f3blockfilter: Update BIP 158 test vectors. (Jim Posen)19308c9e21rpc: Add getblockfilter RPC method. (Jim Posen)ff35105096init: Add CLI option to enable block filter index. (Jim Posen)accc8b8b18index: Access functions for global block filter indexes. (Jim Posen)2bc90e4e7btest: Unit test for block filter index reorg handling. (Jim Posen)6bcf0998c0test: Unit tests for block index filter. (Jim Posen)b5e8200db7index: Implement lookup methods on block filter index. (Jim Posen)75a76e3619index: Implement block filter index with write operations. (Jim Posen)2ad2338ef9serialize: Serialization support for big-endian 32-bit ints. (Jim Posen)ba6ff9a6f7blockfilter: Functions to translate filter types to/from names. (Jim Posen)62b7a4f094index: Ensure block locator is not stale after chain reorg. (Jim Posen)4368384f1dindex: Allow atomic commits of index state to be extended. (Jim Posen) Pull request description: This introduces a new BlockFilterIndex class, which is required for BIP 157 support. The index is uses the asynchronous BaseIndex infrastructure driven by the ValidationInterface callbacks. Filters are stored sequentially in flat files and the disk location of each filter is indexed in LevelDB along with the filter hash and header. The index is designed to ensure persistence of filters reorganized out of the main chain to simplify the BIP 157 net implementation. Stats (block height = 565500): - Syncing the index from scratch takes 45m - Total index size is 3.8 GiB ACKs for commit c7efb6: MarcoFalke: utACKc7efb652f3ryanofsky: Slightly tested ACKc7efb652f3(I just rebuilt the index with the updated PR and tested the RPC). Changes since last review: rebase, fixed compile errors in internal commits, new comments, updated error messages, tweaked cache size logic, renamed commit method, renamed constants and globals, fixed whitespace, extra BlockFilterIndex::Init error check. Tree-SHA512: f8ed7a9b6f76df45933aa5eba92b27b3af83f6df2ccb3728a5c89eec80f654344dc14f055f6f63eb9b3a7649dd8af6553fe14969889e7e2fd2f8461574d18f28
This commit is contained in:
45
src/init.cpp
45
src/init.cpp
@@ -12,6 +12,7 @@
|
||||
#include <addrman.h>
|
||||
#include <amount.h>
|
||||
#include <banman.h>
|
||||
#include <blockfilter.h>
|
||||
#include <chain.h>
|
||||
#include <chainparams.h>
|
||||
#include <checkpoints.h>
|
||||
@@ -20,6 +21,7 @@
|
||||
#include <fs.h>
|
||||
#include <httpserver.h>
|
||||
#include <httprpc.h>
|
||||
#include <index/blockfilterindex.h>
|
||||
#include <interfaces/chain.h>
|
||||
#include <index/txindex.h>
|
||||
#include <key.h>
|
||||
@@ -191,6 +193,7 @@ void Interrupt()
|
||||
if (g_txindex) {
|
||||
g_txindex->Interrupt();
|
||||
}
|
||||
ForEachBlockFilterIndex([](BlockFilterIndex& index) { index.Interrupt(); });
|
||||
}
|
||||
|
||||
void Shutdown(InitInterfaces& interfaces)
|
||||
@@ -222,6 +225,7 @@ void Shutdown(InitInterfaces& interfaces)
|
||||
if (peerLogic) UnregisterValidationInterface(peerLogic.get());
|
||||
if (g_connman) g_connman->Stop();
|
||||
if (g_txindex) g_txindex->Stop();
|
||||
ForEachBlockFilterIndex([](BlockFilterIndex& index) { index.Stop(); });
|
||||
|
||||
StopTorControl();
|
||||
|
||||
@@ -236,6 +240,7 @@ void Shutdown(InitInterfaces& interfaces)
|
||||
g_connman.reset();
|
||||
g_banman.reset();
|
||||
g_txindex.reset();
|
||||
DestroyAllBlockFilterIndexes();
|
||||
|
||||
if (g_is_mempool_loaded && gArgs.GetArg("-persistmempool", DEFAULT_PERSIST_MEMPOOL)) {
|
||||
DumpMempool();
|
||||
@@ -406,6 +411,10 @@ void SetupServerArgs()
|
||||
hidden_args.emplace_back("-sysperms");
|
||||
#endif
|
||||
gArgs.AddArg("-txindex", strprintf("Maintain a full transaction index, used by the getrawtransaction rpc call (default: %u)", DEFAULT_TXINDEX), false, OptionsCategory::OPTIONS);
|
||||
gArgs.AddArg("-blockfilterindex=<type>",
|
||||
strprintf("Maintain an index of compact filters by block (default: %s, values: %s).", DEFAULT_BLOCKFILTERINDEX, ListBlockFilterTypes()) +
|
||||
" If <type> is not supplied or if <type> = 1, indexes for all known types are enabled.",
|
||||
false, OptionsCategory::OPTIONS);
|
||||
|
||||
gArgs.AddArg("-addnode=<ip>", "Add a node to connect to and attempt to keep the connection open (see the `addnode` RPC command help for more info). This option can be specified multiple times to add multiple nodes.", false, OptionsCategory::CONNECTION);
|
||||
gArgs.AddArg("-banscore=<n>", strprintf("Threshold for disconnecting misbehaving peers (default: %u)", DEFAULT_BANSCORE_THRESHOLD), false, OptionsCategory::CONNECTION);
|
||||
@@ -875,6 +884,7 @@ int nUserMaxConnections;
|
||||
int nFD;
|
||||
ServiceFlags nLocalServices = ServiceFlags(NODE_NETWORK | NODE_NETWORK_LIMITED);
|
||||
int64_t peer_connect_timeout;
|
||||
std::vector<BlockFilterType> g_enabled_filter_types;
|
||||
|
||||
} // namespace
|
||||
|
||||
@@ -956,10 +966,29 @@ bool AppInitParameterInteraction()
|
||||
return InitError(strprintf(_("Specified blocks directory \"%s\" does not exist."), gArgs.GetArg("-blocksdir", "").c_str()));
|
||||
}
|
||||
|
||||
// parse and validate enabled filter types
|
||||
std::string blockfilterindex_value = gArgs.GetArg("-blockfilterindex", DEFAULT_BLOCKFILTERINDEX);
|
||||
if (blockfilterindex_value == "" || blockfilterindex_value == "1") {
|
||||
g_enabled_filter_types = AllBlockFilterTypes();
|
||||
} else if (blockfilterindex_value != "0") {
|
||||
const std::vector<std::string> names = gArgs.GetArgs("-blockfilterindex");
|
||||
g_enabled_filter_types.reserve(names.size());
|
||||
for (const auto& name : names) {
|
||||
BlockFilterType filter_type;
|
||||
if (!BlockFilterTypeByName(name, filter_type)) {
|
||||
return InitError(strprintf(_("Unknown -blockfilterindex value %s."), name));
|
||||
}
|
||||
g_enabled_filter_types.push_back(filter_type);
|
||||
}
|
||||
}
|
||||
|
||||
// if using block pruning, then disallow txindex
|
||||
if (gArgs.GetArg("-prune", 0)) {
|
||||
if (gArgs.GetBoolArg("-txindex", DEFAULT_TXINDEX))
|
||||
return InitError(_("Prune mode is incompatible with -txindex."));
|
||||
if (!g_enabled_filter_types.empty()) {
|
||||
return InitError(_("Prune mode is incompatible with -blockfilterindex."));
|
||||
}
|
||||
}
|
||||
|
||||
// -bind and -whitebind can't be set when not listening
|
||||
@@ -1450,6 +1479,13 @@ bool AppInitMain(InitInterfaces& interfaces)
|
||||
nTotalCache -= nBlockTreeDBCache;
|
||||
int64_t nTxIndexCache = std::min(nTotalCache / 8, gArgs.GetBoolArg("-txindex", DEFAULT_TXINDEX) ? nMaxTxIndexCache << 20 : 0);
|
||||
nTotalCache -= nTxIndexCache;
|
||||
int64_t filter_index_cache = 0;
|
||||
if (!g_enabled_filter_types.empty()) {
|
||||
size_t n_indexes = g_enabled_filter_types.size();
|
||||
int64_t max_cache = std::min(nTotalCache / 8, max_filter_index_cache << 20);
|
||||
filter_index_cache = max_cache / n_indexes;
|
||||
nTotalCache -= filter_index_cache * n_indexes;
|
||||
}
|
||||
int64_t nCoinDBCache = std::min(nTotalCache / 2, (nTotalCache / 4) + (1 << 23)); // use 25%-50% of the remainder for disk cache
|
||||
nCoinDBCache = std::min(nCoinDBCache, nMaxCoinsDBCache << 20); // cap total coins db cache
|
||||
nTotalCache -= nCoinDBCache;
|
||||
@@ -1460,6 +1496,10 @@ bool AppInitMain(InitInterfaces& interfaces)
|
||||
if (gArgs.GetBoolArg("-txindex", DEFAULT_TXINDEX)) {
|
||||
LogPrintf("* Using %.1f MiB for transaction index database\n", nTxIndexCache * (1.0 / 1024 / 1024));
|
||||
}
|
||||
for (BlockFilterType filter_type : g_enabled_filter_types) {
|
||||
LogPrintf("* Using %.1f MiB for %s block filter index database\n",
|
||||
filter_index_cache * (1.0 / 1024 / 1024), BlockFilterTypeName(filter_type));
|
||||
}
|
||||
LogPrintf("* Using %.1f MiB for chain state database\n", nCoinDBCache * (1.0 / 1024 / 1024));
|
||||
LogPrintf("* Using %.1f MiB for in-memory UTXO set (plus up to %.1f MiB of unused mempool space)\n", nCoinCacheUsage * (1.0 / 1024 / 1024), nMempoolSizeMax * (1.0 / 1024 / 1024));
|
||||
|
||||
@@ -1647,6 +1687,11 @@ bool AppInitMain(InitInterfaces& interfaces)
|
||||
g_txindex->Start();
|
||||
}
|
||||
|
||||
for (const auto& filter_type : g_enabled_filter_types) {
|
||||
InitBlockFilterIndex(filter_type, filter_index_cache, false, fReindex);
|
||||
GetBlockFilterIndex(filter_type)->Start();
|
||||
}
|
||||
|
||||
// ********************************************************* Step 9: load wallet
|
||||
for (const auto& client : interfaces.chain_clients) {
|
||||
if (!client->load()) {
|
||||
|
||||
Reference in New Issue
Block a user