Skip to content

Commit b4d0366

Browse files
committed
Merge #19070: p2p: Signal support for compact block filters with NODE_COMPACT_FILTERS
f5c003d [test] Add test for NODE_COMPACT_FILTER. (Jim Posen) 132b30d [net] Signal NODE_COMPACT_FILTERS if we're serving compact filters. (Jim Posen) b3fbc94 Apply cfilters review fixups (John Newbery) Pull request description: If -peerblockfilters is configured, signal the `NODE_COMPACT_FILTERS` service bit to indicate that we are able to serve compact block filters, headers and checkpoints. ACKs for top commit: MarcoFalke: re-review and Concept ACK f5c003d fjahr: Code review ACK f5c003d clarkmoody: Concept ACK f5c003d ariard: Concept and Code Review ACK f5c003d jonatack: ACK f5c003d Tree-SHA512: 34d1c153530a0e55d09046fe548c9dc37344b5d6d50e00af1b4e1de1e7b49de770fca8471346a17c151de9fe164776296bb3dd5af331977f0c3ef1e6fc906f85
2 parents 6757b3a + f5c003d commit b4d0366

File tree

6 files changed

+49
-34
lines changed

6 files changed

+49
-34
lines changed

src/init.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1000,11 +1000,13 @@ bool AppInitParameterInteraction()
10001000
}
10011001
}
10021002

1003-
// Basic filters are the only supported filters. The basic filters index must be enabled
1004-
// to serve compact filters
1005-
if (gArgs.GetBoolArg("-peerblockfilters", DEFAULT_PEERBLOCKFILTERS) &&
1006-
g_enabled_filter_types.count(BlockFilterType::BASIC) != 1) {
1007-
return InitError(_("Cannot set -peerblockfilters without -blockfilterindex."));
1003+
// Signal NODE_COMPACT_FILTERS if peerblockfilters and basic filters index are both enabled.
1004+
if (gArgs.GetBoolArg("-peerblockfilters", DEFAULT_PEERBLOCKFILTERS)) {
1005+
if (g_enabled_filter_types.count(BlockFilterType::BASIC) != 1) {
1006+
return InitError(_("Cannot set -peerblockfilters without -blockfilterindex."));
1007+
}
1008+
1009+
nLocalServices = ServiceFlags(nLocalServices | NODE_COMPACT_FILTERS);
10081010
}
10091011

10101012
// if using block pruning, then disallow txindex

src/net_processing.cpp

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2091,7 +2091,7 @@ void static ProcessOrphanTx(CConnman& connman, CTxMemPool& mempool, std::set<uin
20912091
*
20922092
* May disconnect from the peer in the case of a bad request.
20932093
*
2094-
* @param[in] pfrom The peer that we received the request from
2094+
* @param[in] peer The peer that we received the request from
20952095
* @param[in] chain_params Chain parameters
20962096
* @param[in] filter_type The filter type the request is for. Must be basic filters.
20972097
* @param[in] start_height The start height for the request
@@ -2101,19 +2101,19 @@ void static ProcessOrphanTx(CConnman& connman, CTxMemPool& mempool, std::set<uin
21012101
* @param[out] filter_index The filter index, if the request can be serviced.
21022102
* @return True if the request can be serviced.
21032103
*/
2104-
static bool PrepareBlockFilterRequest(CNode& pfrom, const CChainParams& chain_params,
2104+
static bool PrepareBlockFilterRequest(CNode& peer, const CChainParams& chain_params,
21052105
BlockFilterType filter_type, uint32_t start_height,
21062106
const uint256& stop_hash, uint32_t max_height_diff,
21072107
const CBlockIndex*& stop_index,
21082108
BlockFilterIndex*& filter_index)
21092109
{
21102110
const bool supported_filter_type =
21112111
(filter_type == BlockFilterType::BASIC &&
2112-
gArgs.GetBoolArg("-peerblockfilters", DEFAULT_PEERBLOCKFILTERS));
2112+
(peer.GetLocalServices() & NODE_COMPACT_FILTERS));
21132113
if (!supported_filter_type) {
21142114
LogPrint(BCLog::NET, "peer %d requested unsupported block filter type: %d\n",
2115-
pfrom.GetId(), static_cast<uint8_t>(filter_type));
2116-
pfrom.fDisconnect = true;
2115+
peer.GetId(), static_cast<uint8_t>(filter_type));
2116+
peer.fDisconnect = true;
21172117
return false;
21182118
}
21192119

@@ -2124,8 +2124,8 @@ static bool PrepareBlockFilterRequest(CNode& pfrom, const CChainParams& chain_pa
21242124
// Check that the stop block exists and the peer would be allowed to fetch it.
21252125
if (!stop_index || !BlockRequestAllowed(stop_index, chain_params.GetConsensus())) {
21262126
LogPrint(BCLog::NET, "peer %d requested invalid block hash: %s\n",
2127-
pfrom.GetId(), stop_hash.ToString());
2128-
pfrom.fDisconnect = true;
2127+
peer.GetId(), stop_hash.ToString());
2128+
peer.fDisconnect = true;
21292129
return false;
21302130
}
21312131
}
@@ -2134,14 +2134,14 @@ static bool PrepareBlockFilterRequest(CNode& pfrom, const CChainParams& chain_pa
21342134
if (start_height > stop_height) {
21352135
LogPrint(BCLog::NET, "peer %d sent invalid getcfilters/getcfheaders with " /* Continued */
21362136
"start height %d and stop height %d\n",
2137-
pfrom.GetId(), start_height, stop_height);
2138-
pfrom.fDisconnect = true;
2137+
peer.GetId(), start_height, stop_height);
2138+
peer.fDisconnect = true;
21392139
return false;
21402140
}
21412141
if (stop_height - start_height >= max_height_diff) {
21422142
LogPrint(BCLog::NET, "peer %d requested too many cfilters/cfheaders: %d / %d\n",
2143-
pfrom.GetId(), stop_height - start_height + 1, max_height_diff);
2144-
pfrom.fDisconnect = true;
2143+
peer.GetId(), stop_height - start_height + 1, max_height_diff);
2144+
peer.fDisconnect = true;
21452145
return false;
21462146
}
21472147

@@ -2159,12 +2159,12 @@ static bool PrepareBlockFilterRequest(CNode& pfrom, const CChainParams& chain_pa
21592159
*
21602160
* May disconnect from the peer in the case of a bad request.
21612161
*
2162-
* @param[in] pfrom The peer that we received the request from
2162+
* @param[in] peer The peer that we received the request from
21632163
* @param[in] vRecv The raw message received
21642164
* @param[in] chain_params Chain parameters
21652165
* @param[in] connman Pointer to the connection manager
21662166
*/
2167-
static void ProcessGetCFilters(CNode& pfrom, CDataStream& vRecv, const CChainParams& chain_params,
2167+
static void ProcessGetCFilters(CNode& peer, CDataStream& vRecv, const CChainParams& chain_params,
21682168
CConnman& connman)
21692169
{
21702170
uint8_t filter_type_ser;
@@ -2177,23 +2177,22 @@ static void ProcessGetCFilters(CNode& pfrom, CDataStream& vRecv, const CChainPar
21772177

21782178
const CBlockIndex* stop_index;
21792179
BlockFilterIndex* filter_index;
2180-
if (!PrepareBlockFilterRequest(pfrom, chain_params, filter_type, start_height, stop_hash,
2180+
if (!PrepareBlockFilterRequest(peer, chain_params, filter_type, start_height, stop_hash,
21812181
MAX_GETCFILTERS_SIZE, stop_index, filter_index)) {
21822182
return;
21832183
}
21842184

21852185
std::vector<BlockFilter> filters;
2186-
21872186
if (!filter_index->LookupFilterRange(start_height, stop_index, filters)) {
21882187
LogPrint(BCLog::NET, "Failed to find block filter in index: filter_type=%s, start_height=%d, stop_hash=%s\n",
21892188
BlockFilterTypeName(filter_type), start_height, stop_hash.ToString());
21902189
return;
21912190
}
21922191

21932192
for (const auto& filter : filters) {
2194-
CSerializedNetMsg msg = CNetMsgMaker(pfrom.GetSendVersion())
2193+
CSerializedNetMsg msg = CNetMsgMaker(peer.GetSendVersion())
21952194
.Make(NetMsgType::CFILTER, filter);
2196-
connman.PushMessage(&pfrom, std::move(msg));
2195+
connman.PushMessage(&peer, std::move(msg));
21972196
}
21982197
}
21992198

@@ -2202,12 +2201,12 @@ static void ProcessGetCFilters(CNode& pfrom, CDataStream& vRecv, const CChainPar
22022201
*
22032202
* May disconnect from the peer in the case of a bad request.
22042203
*
2205-
* @param[in] pfrom The peer that we received the request from
2204+
* @param[in] peer The peer that we received the request from
22062205
* @param[in] vRecv The raw message received
22072206
* @param[in] chain_params Chain parameters
22082207
* @param[in] connman Pointer to the connection manager
22092208
*/
2210-
static void ProcessGetCFHeaders(CNode& pfrom, CDataStream& vRecv, const CChainParams& chain_params,
2209+
static void ProcessGetCFHeaders(CNode& peer, CDataStream& vRecv, const CChainParams& chain_params,
22112210
CConnman& connman)
22122211
{
22132212
uint8_t filter_type_ser;
@@ -2220,7 +2219,7 @@ static void ProcessGetCFHeaders(CNode& pfrom, CDataStream& vRecv, const CChainPa
22202219

22212220
const CBlockIndex* stop_index;
22222221
BlockFilterIndex* filter_index;
2223-
if (!PrepareBlockFilterRequest(pfrom, chain_params, filter_type, start_height, stop_hash,
2222+
if (!PrepareBlockFilterRequest(peer, chain_params, filter_type, start_height, stop_hash,
22242223
MAX_GETCFHEADERS_SIZE, stop_index, filter_index)) {
22252224
return;
22262225
}
@@ -2243,26 +2242,26 @@ static void ProcessGetCFHeaders(CNode& pfrom, CDataStream& vRecv, const CChainPa
22432242
return;
22442243
}
22452244

2246-
CSerializedNetMsg msg = CNetMsgMaker(pfrom.GetSendVersion())
2245+
CSerializedNetMsg msg = CNetMsgMaker(peer.GetSendVersion())
22472246
.Make(NetMsgType::CFHEADERS,
22482247
filter_type_ser,
22492248
stop_index->GetBlockHash(),
22502249
prev_header,
22512250
filter_hashes);
2252-
connman.PushMessage(&pfrom, std::move(msg));
2251+
connman.PushMessage(&peer, std::move(msg));
22532252
}
22542253

22552254
/**
22562255
* Handle a getcfcheckpt request.
22572256
*
22582257
* May disconnect from the peer in the case of a bad request.
22592258
*
2260-
* @param[in] pfrom The peer that we received the request from
2259+
* @param[in] peer The peer that we received the request from
22612260
* @param[in] vRecv The raw message received
22622261
* @param[in] chain_params Chain parameters
22632262
* @param[in] connman Pointer to the connection manager
22642263
*/
2265-
static void ProcessGetCFCheckPt(CNode& pfrom, CDataStream& vRecv, const CChainParams& chain_params,
2264+
static void ProcessGetCFCheckPt(CNode& peer, CDataStream& vRecv, const CChainParams& chain_params,
22662265
CConnman& connman)
22672266
{
22682267
uint8_t filter_type_ser;
@@ -2274,7 +2273,7 @@ static void ProcessGetCFCheckPt(CNode& pfrom, CDataStream& vRecv, const CChainPa
22742273

22752274
const CBlockIndex* stop_index;
22762275
BlockFilterIndex* filter_index;
2277-
if (!PrepareBlockFilterRequest(pfrom, chain_params, filter_type, /*start_height=*/0, stop_hash,
2276+
if (!PrepareBlockFilterRequest(peer, chain_params, filter_type, /*start_height=*/0, stop_hash,
22782277
/*max_height_diff=*/std::numeric_limits<uint32_t>::max(),
22792278
stop_index, filter_index)) {
22802279
return;
@@ -2295,12 +2294,12 @@ static void ProcessGetCFCheckPt(CNode& pfrom, CDataStream& vRecv, const CChainPa
22952294
}
22962295
}
22972296

2298-
CSerializedNetMsg msg = CNetMsgMaker(pfrom.GetSendVersion())
2297+
CSerializedNetMsg msg = CNetMsgMaker(peer.GetSendVersion())
22992298
.Make(NetMsgType::CFCHECKPT,
23002299
filter_type_ser,
23012300
stop_index->GetBlockHash(),
23022301
headers);
2303-
connman.PushMessage(&pfrom, std::move(msg));
2302+
connman.PushMessage(&peer, std::move(msg));
23042303
}
23052304

23062305
void ProcessMessage(

src/protocol.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ static std::string serviceFlagToStr(size_t bit)
217217
case NODE_GETUTXO: return "GETUTXO";
218218
case NODE_BLOOM: return "BLOOM";
219219
case NODE_WITNESS: return "WITNESS";
220+
case NODE_COMPACT_FILTERS: return "COMPACT_FILTERS";
220221
case NODE_NETWORK_LIMITED: return "NETWORK_LIMITED";
221222
// Not using default, so we get warned when a case is missing
222223
}

src/protocol.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,9 @@ enum ServiceFlags : uint64_t {
272272
// NODE_WITNESS indicates that a node can be asked for blocks and transactions including
273273
// witness data.
274274
NODE_WITNESS = (1 << 3),
275+
// NODE_COMPACT_FILTERS means the node will service basic block filter requests.
276+
// See BIP157 and BIP158 for details on how this is implemented.
277+
NODE_COMPACT_FILTERS = (1 << 6),
275278
// NODE_NETWORK_LIMITED means the same as NODE_NETWORK with the limitation of only
276279
// serving the last 288 (2 day) blocks
277280
// See BIP159 for details on how this is implemented.

test/functional/p2p_blockfilters.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@
44
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
55
"""Tests NODE_COMPACT_FILTERS (BIP 157/158).
66
7-
Tests that a node configured with -blockfilterindex and -peerblockfilters can serve
8-
cfheaders and cfcheckpts.
7+
Tests that a node configured with -blockfilterindex and -peerblockfilters signals
8+
NODE_COMPACT_FILTERS and can serve cfilters, cfheaders and cfcheckpts.
99
"""
1010

1111
from test_framework.messages import (
1212
FILTER_TYPE_BASIC,
13+
NODE_COMPACT_FILTERS,
1314
hash256,
1415
msg_getcfcheckpt,
1516
msg_getcfheaders,
@@ -70,6 +71,14 @@ def run_test(self):
7071
self.nodes[1].generate(1001)
7172
wait_until(lambda: self.nodes[1].getblockcount() == 2000)
7273

74+
# Check that nodes have signalled NODE_COMPACT_FILTERS correctly.
75+
assert node0.nServices & NODE_COMPACT_FILTERS != 0
76+
assert node1.nServices & NODE_COMPACT_FILTERS == 0
77+
78+
# Check that the localservices is as expected.
79+
assert int(self.nodes[0].getnetworkinfo()['localservices'], 16) & NODE_COMPACT_FILTERS != 0
80+
assert int(self.nodes[1].getnetworkinfo()['localservices'], 16) & NODE_COMPACT_FILTERS == 0
81+
7382
self.log.info("get cfcheckpt on chain to be re-orged out.")
7483
request = msg_getcfcheckpt(
7584
filter_type=FILTER_TYPE_BASIC,

test/functional/test_framework/messages.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
NODE_GETUTXO = (1 << 1)
5454
NODE_BLOOM = (1 << 2)
5555
NODE_WITNESS = (1 << 3)
56+
NODE_COMPACT_FILTERS = (1 << 6)
5657
NODE_NETWORK_LIMITED = (1 << 10)
5758

5859
MSG_TX = 1

0 commit comments

Comments
 (0)