@@ -129,6 +129,8 @@ static constexpr unsigned int INVENTORY_BROADCAST_MAX = 7 * INVENTORY_BROADCAST_
129
129
static constexpr unsigned int AVG_FEEFILTER_BROADCAST_INTERVAL = 10 * 60 ;
130
130
/* * Maximum feefilter broadcast delay after significant change. */
131
131
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 ;
132
134
/* * Maximum number of cf hashes that may be requested with one getcfheaders. See BIP 157. */
133
135
static constexpr uint32_t MAX_GETCFHEADERS_SIZE = 2000 ;
134
136
@@ -1999,7 +2001,7 @@ void static ProcessOrphanTx(CConnman* connman, CTxMemPool& mempool, std::set<uin
1999
2001
* @param[out] filter_index The filter index, if the request can be serviced.
2000
2002
* @return True if the request can be serviced.
2001
2003
*/
2002
- static bool PrepareBlockFilterRequest (CNode* pfrom, const CChainParams& chain_params,
2004
+ static bool PrepareBlockFilterRequest (CNode& pfrom, const CChainParams& chain_params,
2003
2005
BlockFilterType filter_type, uint32_t start_height,
2004
2006
const uint256& stop_hash, uint32_t max_height_diff,
2005
2007
const CBlockIndex*& stop_index,
@@ -2010,8 +2012,8 @@ static bool PrepareBlockFilterRequest(CNode* pfrom, const CChainParams& chain_pa
2010
2012
gArgs .GetBoolArg (" -peerblockfilters" , DEFAULT_PEERBLOCKFILTERS));
2011
2013
if (!supported_filter_type) {
2012
2014
LogPrint (BCLog::NET, " peer %d requested unsupported block filter type: %d\n " ,
2013
- pfrom-> GetId (), static_cast <uint8_t >(filter_type));
2014
- pfrom-> fDisconnect = true ;
2015
+ pfrom. GetId (), static_cast <uint8_t >(filter_type));
2016
+ pfrom. fDisconnect = true ;
2015
2017
return false ;
2016
2018
}
2017
2019
@@ -2022,8 +2024,8 @@ static bool PrepareBlockFilterRequest(CNode* pfrom, const CChainParams& chain_pa
2022
2024
// Check that the stop block exists and the peer would be allowed to fetch it.
2023
2025
if (!stop_index || !BlockRequestAllowed (stop_index, chain_params.GetConsensus ())) {
2024
2026
LogPrint (BCLog::NET, " peer %d requested invalid block hash: %s\n " ,
2025
- pfrom-> GetId (), stop_hash.ToString ());
2026
- pfrom-> fDisconnect = true ;
2027
+ pfrom. GetId (), stop_hash.ToString ());
2028
+ pfrom. fDisconnect = true ;
2027
2029
return false ;
2028
2030
}
2029
2031
}
@@ -2032,14 +2034,14 @@ static bool PrepareBlockFilterRequest(CNode* pfrom, const CChainParams& chain_pa
2032
2034
if (start_height > stop_height) {
2033
2035
LogPrint (BCLog::NET, " peer %d sent invalid getcfilters/getcfheaders with " /* Continued */
2034
2036
" start height %d and stop height %d\n " ,
2035
- pfrom-> GetId (), start_height, stop_height);
2036
- pfrom-> fDisconnect = true ;
2037
+ pfrom. GetId (), start_height, stop_height);
2038
+ pfrom. fDisconnect = true ;
2037
2039
return false ;
2038
2040
}
2039
2041
if (stop_height - start_height >= max_height_diff) {
2040
2042
LogPrint (BCLog::NET, " peer %d requested too many cfilters/cfheaders: %d / %d\n " ,
2041
- pfrom-> GetId (), stop_height - start_height + 1 , max_height_diff);
2042
- pfrom-> fDisconnect = true ;
2043
+ pfrom. GetId (), stop_height - start_height + 1 , max_height_diff);
2044
+ pfrom. fDisconnect = true ;
2043
2045
return false ;
2044
2046
}
2045
2047
@@ -2052,6 +2054,49 @@ static bool PrepareBlockFilterRequest(CNode* pfrom, const CChainParams& chain_pa
2052
2054
return true ;
2053
2055
}
2054
2056
2057
+ /* *
2058
+ * Handle a cfilters request.
2059
+ *
2060
+ * May disconnect from the peer in the case of a bad request.
2061
+ *
2062
+ * @param[in] pfrom The peer that we received the request from
2063
+ * @param[in] vRecv The raw message received
2064
+ * @param[in] chain_params Chain parameters
2065
+ * @param[in] connman Pointer to the connection manager
2066
+ */
2067
+ static void ProcessGetCFilters (CNode& pfrom, CDataStream& vRecv, const CChainParams& chain_params,
2068
+ CConnman& connman)
2069
+ {
2070
+ uint8_t filter_type_ser;
2071
+ uint32_t start_height;
2072
+ uint256 stop_hash;
2073
+
2074
+ vRecv >> filter_type_ser >> start_height >> stop_hash;
2075
+
2076
+ const BlockFilterType filter_type = static_cast <BlockFilterType>(filter_type_ser);
2077
+
2078
+ const CBlockIndex* stop_index;
2079
+ BlockFilterIndex* filter_index;
2080
+ if (!PrepareBlockFilterRequest (pfrom, chain_params, filter_type, start_height, stop_hash,
2081
+ MAX_GETCFILTERS_SIZE, stop_index, filter_index)) {
2082
+ return ;
2083
+ }
2084
+
2085
+ std::vector<BlockFilter> filters;
2086
+
2087
+ if (!filter_index->LookupFilterRange (start_height, stop_index, filters)) {
2088
+ LogPrint (BCLog::NET, " Failed to find block filter in index: filter_type=%s, start_height=%d, stop_hash=%s\n " ,
2089
+ BlockFilterTypeName (filter_type), start_height, stop_hash.ToString ());
2090
+ return ;
2091
+ }
2092
+
2093
+ for (const auto & filter : filters) {
2094
+ CSerializedNetMsg msg = CNetMsgMaker (pfrom.GetSendVersion ())
2095
+ .Make (NetMsgType::CFILTER, filter);
2096
+ connman.PushMessage (&pfrom, std::move (msg));
2097
+ }
2098
+ }
2099
+
2055
2100
/* *
2056
2101
* Handle a cfheaders request.
2057
2102
*
@@ -2062,8 +2107,8 @@ static bool PrepareBlockFilterRequest(CNode* pfrom, const CChainParams& chain_pa
2062
2107
* @param[in] chain_params Chain parameters
2063
2108
* @param[in] connman Pointer to the connection manager
2064
2109
*/
2065
- static void ProcessGetCFHeaders (CNode* pfrom, CDataStream& vRecv, const CChainParams& chain_params,
2066
- CConnman* connman)
2110
+ static void ProcessGetCFHeaders (CNode& pfrom, CDataStream& vRecv, const CChainParams& chain_params,
2111
+ CConnman& connman)
2067
2112
{
2068
2113
uint8_t filter_type_ser;
2069
2114
uint32_t start_height;
@@ -2098,13 +2143,13 @@ static void ProcessGetCFHeaders(CNode* pfrom, CDataStream& vRecv, const CChainPa
2098
2143
return ;
2099
2144
}
2100
2145
2101
- CSerializedNetMsg msg = CNetMsgMaker (pfrom-> GetSendVersion ())
2146
+ CSerializedNetMsg msg = CNetMsgMaker (pfrom. GetSendVersion ())
2102
2147
.Make (NetMsgType::CFHEADERS,
2103
2148
filter_type_ser,
2104
2149
stop_index->GetBlockHash (),
2105
2150
prev_header,
2106
2151
filter_hashes);
2107
- connman-> PushMessage (pfrom, std::move (msg));
2152
+ connman. PushMessage (& pfrom, std::move (msg));
2108
2153
}
2109
2154
2110
2155
/* *
@@ -2117,8 +2162,8 @@ static void ProcessGetCFHeaders(CNode* pfrom, CDataStream& vRecv, const CChainPa
2117
2162
* @param[in] chain_params Chain parameters
2118
2163
* @param[in] connman Pointer to the connection manager
2119
2164
*/
2120
- static void ProcessGetCFCheckPt (CNode* pfrom, CDataStream& vRecv, const CChainParams& chain_params,
2121
- CConnman* connman)
2165
+ static void ProcessGetCFCheckPt (CNode& pfrom, CDataStream& vRecv, const CChainParams& chain_params,
2166
+ CConnman& connman)
2122
2167
{
2123
2168
uint8_t filter_type_ser;
2124
2169
uint256 stop_hash;
@@ -2150,12 +2195,12 @@ static void ProcessGetCFCheckPt(CNode* pfrom, CDataStream& vRecv, const CChainPa
2150
2195
}
2151
2196
}
2152
2197
2153
- CSerializedNetMsg msg = CNetMsgMaker (pfrom-> GetSendVersion ())
2198
+ CSerializedNetMsg msg = CNetMsgMaker (pfrom. GetSendVersion ())
2154
2199
.Make (NetMsgType::CFCHECKPT,
2155
2200
filter_type_ser,
2156
2201
stop_index->GetBlockHash (),
2157
2202
headers);
2158
- connman-> PushMessage (pfrom, std::move (msg));
2203
+ connman. PushMessage (& pfrom, std::move (msg));
2159
2204
}
2160
2205
2161
2206
bool ProcessMessage (CNode* pfrom, const std::string& msg_type, CDataStream& vRecv, int64_t nTimeReceived, const CChainParams& chainparams, ChainstateManager& chainman, CTxMemPool& mempool, CConnman* connman, BanMan* banman, const std::atomic<bool >& interruptMsgProc)
@@ -3467,13 +3512,18 @@ bool ProcessMessage(CNode* pfrom, const std::string& msg_type, CDataStream& vRec
3467
3512
return true ;
3468
3513
}
3469
3514
3515
+ if (msg_type == NetMsgType::GETCFILTERS) {
3516
+ ProcessGetCFilters (*pfrom, vRecv, chainparams, *connman);
3517
+ return true ;
3518
+ }
3519
+
3470
3520
if (msg_type == NetMsgType::GETCFHEADERS) {
3471
- ProcessGetCFHeaders (pfrom, vRecv, chainparams, connman);
3521
+ ProcessGetCFHeaders (* pfrom, vRecv, chainparams, * connman);
3472
3522
return true ;
3473
3523
}
3474
3524
3475
3525
if (msg_type == NetMsgType::GETCFCHECKPT) {
3476
- ProcessGetCFCheckPt (pfrom, vRecv, chainparams, connman);
3526
+ ProcessGetCFCheckPt (* pfrom, vRecv, chainparams, * connman);
3477
3527
return true ;
3478
3528
}
3479
3529
0 commit comments