|
7 | 7 |
|
8 | 8 | #include <amount.h>
|
9 | 9 | #include <base58.h>
|
| 10 | +#include <blockfilter.h> |
10 | 11 | #include <chain.h>
|
11 | 12 | #include <chainparams.h>
|
12 | 13 | #include <checkpoints.h>
|
13 | 14 | #include <coins.h>
|
14 | 15 | #include <consensus/validation.h>
|
15 | 16 | #include <core_io.h>
|
16 | 17 | #include <hash.h>
|
| 18 | +#include <index/blockfilterindex.h> |
17 | 19 | #include <index/txindex.h>
|
18 | 20 | #include <key_io.h>
|
19 | 21 | #include <policy/feerate.h>
|
@@ -2296,6 +2298,85 @@ UniValue scantxoutset(const JSONRPCRequest& request)
|
2296 | 2298 | return result;
|
2297 | 2299 | }
|
2298 | 2300 |
|
| 2301 | +static UniValue getblockfilter(const JSONRPCRequest& request) |
| 2302 | +{ |
| 2303 | + if (request.fHelp || request.params.size() < 1 || request.params.size() > 2) { |
| 2304 | + throw std::runtime_error( |
| 2305 | + RPCHelpMan{"getblockfilter", |
| 2306 | + "\nRetrieve a BIP 157 content filter for a particular block.\n", |
| 2307 | + { |
| 2308 | + {"blockhash", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "The hash of the block"}, |
| 2309 | + {"filtertype", RPCArg::Type::STR, /*default*/ "basic", "The type name of the filter"}, |
| 2310 | + }, |
| 2311 | + RPCResult{ |
| 2312 | + "{\n" |
| 2313 | + " \"filter\" : (string) the hex-encoded filter data\n" |
| 2314 | + " \"header\" : (string) the hex-encoded filter header\n" |
| 2315 | + "}\n" |
| 2316 | + }, |
| 2317 | + RPCExamples{ |
| 2318 | + HelpExampleCli("getblockfilter", "\"00000000c937983704a73af28acdec37b049d214adbda81d7e2a3dd146f6ed09\" \"basic\"") |
| 2319 | + } |
| 2320 | + }.ToString() |
| 2321 | + ); |
| 2322 | + } |
| 2323 | + |
| 2324 | + uint256 block_hash = ParseHashV(request.params[0], "blockhash"); |
| 2325 | + std::string filtertype_name = "basic"; |
| 2326 | + if (!request.params[1].isNull()) { |
| 2327 | + filtertype_name = request.params[1].get_str(); |
| 2328 | + } |
| 2329 | + |
| 2330 | + BlockFilterType filtertype; |
| 2331 | + if (!BlockFilterTypeByName(filtertype_name, filtertype)) { |
| 2332 | + throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Unknown filtertype"); |
| 2333 | + } |
| 2334 | + |
| 2335 | + BlockFilterIndex* index = GetBlockFilterIndex(filtertype); |
| 2336 | + if (!index) { |
| 2337 | + throw JSONRPCError(RPC_MISC_ERROR, "Index is not enabled for filtertype " + filtertype_name); |
| 2338 | + } |
| 2339 | + |
| 2340 | + const CBlockIndex* block_index; |
| 2341 | + bool block_was_connected; |
| 2342 | + { |
| 2343 | + LOCK(cs_main); |
| 2344 | + block_index = LookupBlockIndex(block_hash); |
| 2345 | + if (!block_index) { |
| 2346 | + throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block not found"); |
| 2347 | + } |
| 2348 | + block_was_connected = block_index->IsValid(BLOCK_VALID_SCRIPTS); |
| 2349 | + } |
| 2350 | + |
| 2351 | + bool index_ready = index->BlockUntilSyncedToCurrentChain(); |
| 2352 | + |
| 2353 | + BlockFilter filter; |
| 2354 | + uint256 filter_header; |
| 2355 | + if (!index->LookupFilter(block_index, filter) || |
| 2356 | + !index->LookupFilterHeader(block_index, filter_header)) { |
| 2357 | + int err_code; |
| 2358 | + std::string errmsg = "Filter not found."; |
| 2359 | + |
| 2360 | + if (!block_was_connected) { |
| 2361 | + err_code = RPC_INVALID_ADDRESS_OR_KEY; |
| 2362 | + errmsg += " Block was not connected to active chain."; |
| 2363 | + } else if (!index_ready) { |
| 2364 | + err_code = RPC_MISC_ERROR; |
| 2365 | + errmsg += " Block filters are still in the process of being indexed."; |
| 2366 | + } else { |
| 2367 | + err_code = RPC_INTERNAL_ERROR; |
| 2368 | + errmsg += " This error is unexpected and indicates index corruption."; |
| 2369 | + } |
| 2370 | + |
| 2371 | + throw JSONRPCError(err_code, errmsg); |
| 2372 | + } |
| 2373 | + |
| 2374 | + UniValue ret(UniValue::VOBJ); |
| 2375 | + ret.pushKV("filter", HexStr(filter.GetEncodedFilter())); |
| 2376 | + ret.pushKV("header", filter_header.GetHex()); |
| 2377 | + return ret; |
| 2378 | +} |
| 2379 | + |
2299 | 2380 | // clang-format off
|
2300 | 2381 | static const CRPCCommand commands[] =
|
2301 | 2382 | { // category name actor (function) argNames
|
@@ -2323,6 +2404,7 @@ static const CRPCCommand commands[] =
|
2323 | 2404 |
|
2324 | 2405 | { "blockchain", "preciousblock", &preciousblock, {"blockhash"} },
|
2325 | 2406 | { "blockchain", "scantxoutset", &scantxoutset, {"action", "scanobjects"} },
|
| 2407 | + { "blockchain", "getblockfilter", &getblockfilter, {"blockhash", "filtertype"} }, |
2326 | 2408 |
|
2327 | 2409 | /* Not shown in help */
|
2328 | 2410 | { "hidden", "invalidateblock", &invalidateblock, {"blockhash"} },
|
|
0 commit comments