Skip to content

Commit 9d099b0

Browse files
committed
Merge bitcoin/bitcoin#23836: rest: Expose block filters follow-ups
4523d28 [test] compare filter and header with the result of the getblockfilter RPC (Niklas Gögge) 3a2464f [rest] drop superfluous rpc serializations flags for block filters (Niklas Gögge) 064abd1 [rest] add a more verbose error message for invalid header counts (Niklas Gögge) 83b8f3a [refactor] various style fix-ups (Niklas Gögge) Pull request description: This PR addresses unresolved review comments from [#17631](bitcoin/bitcoin#17631). This includes: * various style fix-ups * returning a more verbose error message for invalid header counts * removing superfluous rpc serializations flags for block filters * improving the test to include comparing the block filters returned from the rest with the ones returned from the `getblockfilter` RPC. ACKs for top commit: jnewbery: ACK 4523d28 brunoerg: tACK 4523d28 Tree-SHA512: 634e6b2ae3e1d5f31675a50cfe11a5e74bf5a51b9e7b512d9e18879bf2ed424fc0ac6ec633023f192e3ad12cf0c73b0b51de79dd7ec00844dba3e1493d823b8c
2 parents 6535772 + 4523d28 commit 9d099b0

File tree

2 files changed

+21
-16
lines changed

2 files changed

+21
-16
lines changed

src/rest.cpp

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ static bool rest_headers(const std::any& context,
195195

196196
const auto parsed_count{ToIntegral<size_t>(path[0])};
197197
if (!parsed_count.has_value() || *parsed_count < 1 || *parsed_count > MAX_REST_HEADERS_RESULTS) {
198-
return RESTERR(req, HTTP_BAD_REQUEST, strprintf("Header count out of acceptable range (1-%u): %s", MAX_REST_HEADERS_RESULTS, path[0]));
198+
return RESTERR(req, HTTP_BAD_REQUEST, strprintf("Header count is invalid or out of acceptable range (1-%u): %s", MAX_REST_HEADERS_RESULTS, path[0]));
199199
}
200200

201201
std::string hashStr = path[1];
@@ -341,11 +341,10 @@ static bool rest_block_notxdetails(const std::any& context, HTTPRequest* req, co
341341
return rest_block(context, req, strURIPart, TxVerbosity::SHOW_TXID);
342342
}
343343

344-
345344
static bool rest_filter_header(const std::any& context, HTTPRequest* req, const std::string& strURIPart)
346345
{
347-
if (!CheckWarmup(req))
348-
return false;
346+
if (!CheckWarmup(req)) return false;
347+
349348
std::string param;
350349
const RetFormat rf = ParseDataFormat(param, strURIPart);
351350

@@ -372,10 +371,10 @@ static bool rest_filter_header(const std::any& context, HTTPRequest* req, const
372371

373372
const auto parsed_count{ToIntegral<size_t>(uri_parts[1])};
374373
if (!parsed_count.has_value() || *parsed_count < 1 || *parsed_count > MAX_REST_HEADERS_RESULTS) {
375-
return RESTERR(req, HTTP_BAD_REQUEST, strprintf("Header count out of acceptable range (1-%u): %s", MAX_REST_HEADERS_RESULTS, uri_parts[1]));
374+
return RESTERR(req, HTTP_BAD_REQUEST, strprintf("Header count is invalid or out of acceptable range (1-%u): %s", MAX_REST_HEADERS_RESULTS, uri_parts[1]));
376375
}
377376

378-
std::vector<const CBlockIndex *> headers;
377+
std::vector<const CBlockIndex*> headers;
379378
headers.reserve(*parsed_count);
380379
{
381380
ChainstateManager* maybe_chainman = GetChainman(context, req);
@@ -396,7 +395,7 @@ static bool rest_filter_header(const std::any& context, HTTPRequest* req, const
396395

397396
std::vector<uint256> filter_headers;
398397
filter_headers.reserve(*parsed_count);
399-
for (const CBlockIndex *pindex : headers) {
398+
for (const CBlockIndex* pindex : headers) {
400399
uint256 filter_header;
401400
if (!index->LookupFilterHeader(pindex, filter_header)) {
402401
std::string errmsg = "Filter not found.";
@@ -414,7 +413,7 @@ static bool rest_filter_header(const std::any& context, HTTPRequest* req, const
414413

415414
switch (rf) {
416415
case RetFormat::BINARY: {
417-
CDataStream ssHeader(SER_NETWORK, PROTOCOL_VERSION | RPCSerializationFlags());
416+
CDataStream ssHeader{SER_NETWORK, PROTOCOL_VERSION};
418417
for (const uint256& header : filter_headers) {
419418
ssHeader << header;
420419
}
@@ -425,7 +424,7 @@ static bool rest_filter_header(const std::any& context, HTTPRequest* req, const
425424
return true;
426425
}
427426
case RetFormat::HEX: {
428-
CDataStream ssHeader(SER_NETWORK, PROTOCOL_VERSION | RPCSerializationFlags());
427+
CDataStream ssHeader{SER_NETWORK, PROTOCOL_VERSION};
429428
for (const uint256& header : filter_headers) {
430429
ssHeader << header;
431430
}
@@ -454,12 +453,12 @@ static bool rest_filter_header(const std::any& context, HTTPRequest* req, const
454453

455454
static bool rest_block_filter(const std::any& context, HTTPRequest* req, const std::string& strURIPart)
456455
{
457-
if (!CheckWarmup(req))
458-
return false;
456+
if (!CheckWarmup(req)) return false;
457+
459458
std::string param;
460459
const RetFormat rf = ParseDataFormat(param, strURIPart);
461460

462-
//request is sent over URI scheme /rest/blockfilter/filtertype/blockhash
461+
// request is sent over URI scheme /rest/blockfilter/filtertype/blockhash
463462
std::vector<std::string> uri_parts;
464463
boost::split(uri_parts, param, boost::is_any_of("/"));
465464
if (uri_parts.size() != 2) {
@@ -514,7 +513,7 @@ static bool rest_block_filter(const std::any& context, HTTPRequest* req, const s
514513

515514
switch (rf) {
516515
case RetFormat::BINARY: {
517-
CDataStream ssResp(SER_NETWORK, PROTOCOL_VERSION | RPCSerializationFlags());
516+
CDataStream ssResp{SER_NETWORK, PROTOCOL_VERSION};
518517
ssResp << filter;
519518

520519
std::string binaryResp = ssResp.str();
@@ -523,7 +522,7 @@ static bool rest_block_filter(const std::any& context, HTTPRequest* req, const s
523522
return true;
524523
}
525524
case RetFormat::HEX: {
526-
CDataStream ssResp(SER_NETWORK, PROTOCOL_VERSION | RPCSerializationFlags());
525+
CDataStream ssResp{SER_NETWORK, PROTOCOL_VERSION};
527526
ssResp << filter;
528527

529528
std::string strHex = HexStr(ssResp) + "\n";

test/functional/interface_rest.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -273,13 +273,19 @@ def run_test(self):
273273
json_obj = self.test_rest_request(f"/headers/5/{bb_hash}")
274274
assert_equal(len(json_obj), 5) # now we should have 5 header objects
275275
json_obj = self.test_rest_request(f"/blockfilterheaders/basic/5/{bb_hash}")
276+
first_filter_header = json_obj[0]
276277
assert_equal(len(json_obj), 5) # now we should have 5 filter header objects
277-
self.test_rest_request(f"/blockfilter/basic/{bb_hash}", req_type=ReqType.BIN, ret_type=RetType.OBJ)
278+
json_obj = self.test_rest_request(f"/blockfilter/basic/{bb_hash}")
279+
280+
# Compare with normal RPC blockfilter response
281+
rpc_blockfilter = self.nodes[0].getblockfilter(bb_hash)
282+
assert_equal(first_filter_header, rpc_blockfilter['header'])
283+
assert_equal(json_obj['filter'], rpc_blockfilter['filter'])
278284

279285
# Test number parsing
280286
for num in ['5a', '-5', '0', '2001', '99999999999999999999999999999999999']:
281287
assert_equal(
282-
bytes(f'Header count out of acceptable range (1-2000): {num}\r\n', 'ascii'),
288+
bytes(f'Header count is invalid or out of acceptable range (1-2000): {num}\r\n', 'ascii'),
283289
self.test_rest_request(f"/headers/{num}/{bb_hash}", ret_type=RetType.BYTES, status=400),
284290
)
285291

0 commit comments

Comments
 (0)