Skip to content

Commit

Permalink
[net] Signal NODE_COMPACT_FILTERS if we're serving compact filters.
Browse files Browse the repository at this point in the history
if -peercfilters is configured, signal the NODE_COMPACT_FILTERS service
bit to indicate that we are able to serve compact block filters, headers
and checkpoints.
  • Loading branch information
jimpo authored and jnewbery committed May 13, 2020
1 parent 1e8a696 commit 94167bf
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 6 deletions.
4 changes: 4 additions & 0 deletions src/index/base.h
Expand Up @@ -91,6 +91,10 @@ class BaseIndex : public CValidationInterface
/// Destructor interrupts sync thread if running and blocks until it exits.
virtual ~BaseIndex();

/// Returns whether index has completed the initial sync with the active chain.
/// After returning true once, this function will return true on all subsequent calls.
bool IsSynced() const { return m_synced; }

/// Blocks the current thread until the index is caught up to the current
/// state of the block chain. This only blocks if the index has gotten in
/// sync once and only needs to process blocks in the ValidationInterface
Expand Down
25 changes: 20 additions & 5 deletions src/init.cpp
Expand Up @@ -994,11 +994,13 @@ bool AppInitParameterInteraction()
}
}

// Basic filters are the only supported filters. The basic filters index must be enabled
// to serve compact filters
if (gArgs.GetBoolArg("-peerblockfilters", DEFAULT_PEERBLOCKFILTERS) &&
g_enabled_filter_types.count(BlockFilterType::BASIC) != 1) {
return InitError(_("Cannot set -peerblockfilters without -blockfilterindex."));
// Signal NODE_COMPACT_FILTERS if peercfilters and basic filters index are both enabled.
if (gArgs.GetBoolArg("-peerblockfilters", DEFAULT_PEERBLOCKFILTERS)) {
if (g_enabled_filter_types.count(BlockFilterType::BASIC) != 1) {
return InitError(_("Cannot set -peerblockfilters without -blockfilterindex."));
}

nLocalServices = ServiceFlags(nLocalServices | NODE_COMPACT_FILTERS);
}

// if using block pruning, then disallow txindex
Expand Down Expand Up @@ -1782,6 +1784,19 @@ bool AppInitMain(NodeContext& node)
GetBlockFilterIndex(filter_type)->Start();
}

if (nLocalServices & NODE_COMPACT_FILTERS) {
const BlockFilterIndex* const basic_filter_index =
GetBlockFilterIndex(BlockFilterType::BASIC);
if (!basic_filter_index) {
error("NODE_COMPACT_FILTERS is signaled, but filter index is not available");
return false;
}
if (!basic_filter_index->IsSynced()) {
InitError(strprintf(_("Cannot enable -peercfilters until basic block filter index is in sync. Please disable and reenable once filters have been indexed.")));
return false;
}
}

// ********************************************************* Step 9: load wallet
for (const auto& client : node.chain_clients) {
if (!client->load()) {
Expand Down
2 changes: 1 addition & 1 deletion src/net_processing.cpp
Expand Up @@ -1994,7 +1994,7 @@ static bool PrepareBlockFilterRequest(CNode* pfrom, const CChainParams& chain_pa
{
const bool supported_filter_type =
(filter_type == BlockFilterType::BASIC &&
gArgs.GetBoolArg("-peerblockfilters", DEFAULT_PEERBLOCKFILTERS));
(pfrom->GetLocalServices() & NODE_COMPACT_FILTERS));
if (!supported_filter_type) {
LogPrint(BCLog::NET, "peer %d requested unsupported block filter type: %d\n",
pfrom->GetId(), static_cast<uint8_t>(filter_type));
Expand Down
3 changes: 3 additions & 0 deletions src/protocol.h
Expand Up @@ -295,6 +295,9 @@ enum ServiceFlags : uint64_t {
// NODE_WITNESS indicates that a node can be asked for blocks and transactions including
// witness data.
NODE_WITNESS = (1 << 3),
// NODE_COMPACT_FILTERS means the node will service basic block filter requests.
// See BIP157 and BIP158 for details on how this is implemented.
NODE_COMPACT_FILTERS = (1 << 6),
// NODE_NETWORK_LIMITED means the same as NODE_NETWORK with the limitation of only
// serving the last 288 (2 day) blocks
// See BIP159 for details on how this is implemented.
Expand Down
1 change: 1 addition & 0 deletions src/qt/guiutil.cpp
Expand Up @@ -751,6 +751,7 @@ QString serviceFlagToStr(const quint64 mask, const int bit)
case NODE_GETUTXO: return "GETUTXO";
case NODE_BLOOM: return "BLOOM";
case NODE_WITNESS: return "WITNESS";
case NODE_COMPACT_FILTERS: return "COMPACT_FILTERS";
case NODE_NETWORK_LIMITED: return "NETWORK_LIMITED";
// Not using default, so we get warned when a case is missing
}
Expand Down
2 changes: 2 additions & 0 deletions src/rpc/util.cpp
Expand Up @@ -851,6 +851,8 @@ UniValue GetServicesNames(ServiceFlags services)
servicesNames.push_back("BLOOM");
if (services & NODE_WITNESS)
servicesNames.push_back("WITNESS");
if (services & NODE_COMPACT_FILTERS)
servicesNames.push_back("COMPACT_FILTERS");
if (services & NODE_NETWORK_LIMITED)
servicesNames.push_back("NETWORK_LIMITED");

Expand Down

0 comments on commit 94167bf

Please sign in to comment.