Skip to content

Commit

Permalink
net: Signal NODE_COMPACT_FILTERS if the block filter index is on.
Browse files Browse the repository at this point in the history
The node will enable the NODE_COMPACT_FILTERS service flag once the
block filter index catches up to the active chain.
  • Loading branch information
jimpo committed Mar 28, 2020
1 parent 6b4f182 commit 9e182b0
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/index/base.h
Original file line number Diff line number Diff line change
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
7 changes: 7 additions & 0 deletions src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1697,6 +1697,13 @@ bool AppInitMain(NodeContext& node)
GetBlockFilterIndex(filter_type)->Start();
}

bool cfilters_enabled = std::find(g_enabled_filter_types.begin(),
g_enabled_filter_types.end(),
BlockFilterType::BASIC) != g_enabled_filter_types.end();
if (cfilters_enabled) {
nLocalServices = ServiceFlags(nLocalServices | NODE_COMPACT_FILTERS);
}

// ********************************************************* Step 9: load wallet
for (const auto& client : node.chain_clients) {
if (!client->load()) {
Expand Down
15 changes: 14 additions & 1 deletion src/net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@
#include <net.h>

#include <banman.h>
#include <blockfilter.h>
#include <chainparams.h>
#include <clientversion.h>
#include <consensus/consensus.h>
#include <crypto/sha256.h>
#include <index/blockfilterindex.h>
#include <netbase.h>
#include <net_permissions.h>
#include <random.h>
Expand Down Expand Up @@ -2670,7 +2672,18 @@ uint64_t CConnman::GetTotalBytesSent()

ServiceFlags CConnman::GetLocalServices() const
{
return nLocalServices;
uint64_t local_services = nLocalServices;
if (local_services & NODE_COMPACT_FILTERS) {
BlockFilterIndex* basic_filter_index = GetBlockFilterIndex(BlockFilterType::BASIC);
if (!basic_filter_index) {
LogPrintf("WARNING: NODE_COMPACT_FILTERS is signaled, but filter index is not available\n");
}
if (!basic_filter_index || !basic_filter_index->IsSynced()) {
// If block filter index is still syncing, do not advertise the service bit.
local_services &= ~NODE_COMPACT_FILTERS;
}
}
return ServiceFlags(local_services);
}

void CConnman::SetBestHeight(int height)
Expand Down
3 changes: 3 additions & 0 deletions src/protocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,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

0 comments on commit 9e182b0

Please sign in to comment.