Skip to content

Commit 088e38d

Browse files
theStackMacroFake
andcommitted
add chain interface methods for using BIP 157 block filters
This is useful for speeding up wallet rescans and is based on an earlier version from PR #15845 ("wallet: Fast rescan with BIP157 block filters"), which was never merged. Co-authored-by: MacroFake <[email protected]>
1 parent e7a0e96 commit 088e38d

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

src/interfaces/chain.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#ifndef BITCOIN_INTERFACES_CHAIN_H
66
#define BITCOIN_INTERFACES_CHAIN_H
77

8+
#include <blockfilter.h>
89
#include <primitives/transaction.h> // For CTransactionRef
910
#include <util/settings.h> // For util::SettingsValue
1011

@@ -143,6 +144,13 @@ class Chain
143144
//! or one of its ancestors.
144145
virtual std::optional<int> findLocatorFork(const CBlockLocator& locator) = 0;
145146

147+
//! Returns whether a block filter index is available.
148+
virtual bool hasBlockFilterIndex(BlockFilterType filter_type) = 0;
149+
150+
//! Returns whether any of the elements match the block via a BIP 157 block filter
151+
//! or std::nullopt if the block filter for this block couldn't be found.
152+
virtual std::optional<bool> blockFilterMatchesAny(BlockFilterType filter_type, const uint256& block_hash, const GCSFilter::ElementSet& filter_set) = 0;
153+
146154
//! Return whether node has the block and optionally return block metadata
147155
//! or contents.
148156
virtual bool findBlock(const uint256& hash, const FoundBlock& block={}) = 0;

src/node/interfaces.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44

55
#include <addrdb.h>
66
#include <banman.h>
7+
#include <blockfilter.h>
78
#include <chain.h>
89
#include <chainparams.h>
910
#include <deploymentstatus.h>
1011
#include <external_signer.h>
12+
#include <index/blockfilterindex.h>
1113
#include <init.h>
1214
#include <interfaces/chain.h>
1315
#include <interfaces/handler.h>
@@ -536,6 +538,20 @@ class ChainImpl : public Chain
536538
}
537539
return std::nullopt;
538540
}
541+
bool hasBlockFilterIndex(BlockFilterType filter_type) override
542+
{
543+
return GetBlockFilterIndex(filter_type) != nullptr;
544+
}
545+
std::optional<bool> blockFilterMatchesAny(BlockFilterType filter_type, const uint256& block_hash, const GCSFilter::ElementSet& filter_set) override
546+
{
547+
const BlockFilterIndex* block_filter_index{GetBlockFilterIndex(filter_type)};
548+
if (!block_filter_index) return std::nullopt;
549+
550+
BlockFilter filter;
551+
const CBlockIndex* index{WITH_LOCK(::cs_main, return chainman().m_blockman.LookupBlockIndex(block_hash))};
552+
if (index == nullptr || !block_filter_index->LookupFilter(index, filter)) return std::nullopt;
553+
return filter.GetFilter().MatchAny(filter_set);
554+
}
539555
bool findBlock(const uint256& hash, const FoundBlock& block) override
540556
{
541557
WAIT_LOCK(cs_main, lock);

0 commit comments

Comments
 (0)