Skip to content

Commit 11106a4

Browse files
jimpojnewbery
authored andcommitted
[net processing] Message handling for getcfilters.
Handle getcfilters request if -peercfilter is configured.
1 parent e535670 commit 11106a4

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed

src/net_processing.cpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,8 @@ static constexpr unsigned int INVENTORY_BROADCAST_MAX = 7 * INVENTORY_BROADCAST_
129129
static constexpr unsigned int AVG_FEEFILTER_BROADCAST_INTERVAL = 10 * 60;
130130
/** Maximum feefilter broadcast delay after significant change. */
131131
static constexpr unsigned int MAX_FEEFILTER_CHANGE_DELAY = 5 * 60;
132+
/** Maximum number of compact filters that may be requested with one getcfilters. See BIP 157. */
133+
static constexpr uint32_t MAX_GETCFILTERS_SIZE = 1000;
132134
/** Maximum number of cf hashes that may be requested with one getcfheaders. See BIP 157. */
133135
static constexpr uint32_t MAX_GETCFHEADERS_SIZE = 2000;
134136

@@ -2051,6 +2053,49 @@ static bool PrepareBlockFilterRequest(CNode& pfrom, const CChainParams& chain_pa
20512053
return true;
20522054
}
20532055

2056+
/**
2057+
* Handle a cfilters request.
2058+
*
2059+
* May disconnect from the peer in the case of a bad request.
2060+
*
2061+
* @param[in] pfrom The peer that we received the request from
2062+
* @param[in] vRecv The raw message received
2063+
* @param[in] chain_params Chain parameters
2064+
* @param[in] connman Pointer to the connection manager
2065+
*/
2066+
static void ProcessGetCFilters(CNode& pfrom, CDataStream& vRecv, const CChainParams& chain_params,
2067+
CConnman& connman)
2068+
{
2069+
uint8_t filter_type_ser;
2070+
uint32_t start_height;
2071+
uint256 stop_hash;
2072+
2073+
vRecv >> filter_type_ser >> start_height >> stop_hash;
2074+
2075+
const BlockFilterType filter_type = static_cast<BlockFilterType>(filter_type_ser);
2076+
2077+
const CBlockIndex* stop_index;
2078+
BlockFilterIndex* filter_index;
2079+
if (!PrepareBlockFilterRequest(pfrom, chain_params, filter_type, start_height, stop_hash,
2080+
MAX_GETCFILTERS_SIZE, stop_index, filter_index)) {
2081+
return;
2082+
}
2083+
2084+
std::vector<BlockFilter> filters;
2085+
2086+
if (!filter_index->LookupFilterRange(start_height, stop_index, filters)) {
2087+
LogPrint(BCLog::NET, "Failed to find block filter in index: filter_type=%s, start_height=%d, stop_hash=%s\n",
2088+
BlockFilterTypeName(filter_type), start_height, stop_hash.ToString());
2089+
return;
2090+
}
2091+
2092+
for (const auto& filter : filters) {
2093+
CSerializedNetMsg msg = CNetMsgMaker(pfrom.GetSendVersion())
2094+
.Make(NetMsgType::CFILTER, filter);
2095+
connman.PushMessage(&pfrom, std::move(msg));
2096+
}
2097+
}
2098+
20542099
/**
20552100
* Handle a cfheaders request.
20562101
*
@@ -3466,6 +3511,11 @@ bool ProcessMessage(CNode* pfrom, const std::string& msg_type, CDataStream& vRec
34663511
return true;
34673512
}
34683513

3514+
if (msg_type == NetMsgType::GETCFILTERS) {
3515+
ProcessGetCFilters(*pfrom, vRecv, chainparams, *connman);
3516+
return true;
3517+
}
3518+
34693519
if (msg_type == NetMsgType::GETCFHEADERS) {
34703520
ProcessGetCFHeaders(*pfrom, vRecv, chainparams, *connman);
34713521
return true;

src/protocol.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ const char *SENDCMPCT="sendcmpct";
4040
const char *CMPCTBLOCK="cmpctblock";
4141
const char *GETBLOCKTXN="getblocktxn";
4242
const char *BLOCKTXN="blocktxn";
43+
const char *GETCFILTERS="getcfilters";
44+
const char *CFILTER="cfilter";
4345
const char *GETCFHEADERS="getcfheaders";
4446
const char *CFHEADERS="cfheaders";
4547
const char *GETCFCHECKPT="getcfcheckpt";
@@ -75,6 +77,8 @@ const static std::string allNetMessageTypes[] = {
7577
NetMsgType::CMPCTBLOCK,
7678
NetMsgType::GETBLOCKTXN,
7779
NetMsgType::BLOCKTXN,
80+
NetMsgType::GETCFILTERS,
81+
NetMsgType::CFILTER,
7882
NetMsgType::GETCFHEADERS,
7983
NetMsgType::CFHEADERS,
8084
NetMsgType::GETCFCHECKPT,

src/protocol.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,17 @@ extern const char* GETBLOCKTXN;
225225
* @since protocol version 70014 as described by BIP 152
226226
*/
227227
extern const char* BLOCKTXN;
228+
/**
229+
* getcfilters requests compact filters for a range of blocks.
230+
* Only available with service bit NODE_COMPACT_FILTERS as described by
231+
* BIP 157 & 158.
232+
*/
233+
extern const char* GETCFILTERS;
234+
/**
235+
* cfilter is a response to a getcfilters request containing a single compact
236+
* filter.
237+
*/
238+
extern const char* CFILTER;
228239
/**
229240
* getcfheaders requests a compact filter header and the filter hashes for a
230241
* range of blocks, which can then be used to reconstruct the filter headers

0 commit comments

Comments
 (0)