Skip to content

Commit 667bc7a

Browse files
committed
rpc: Add getindexinfo RPC
1 parent a57af89 commit 667bc7a

File tree

3 files changed

+75
-0
lines changed

3 files changed

+75
-0
lines changed

src/index/base.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,3 +319,12 @@ void BaseIndex::Stop()
319319
m_thread_sync.join();
320320
}
321321
}
322+
323+
IndexSummary BaseIndex::GetSummary() const
324+
{
325+
IndexSummary summary{};
326+
summary.name = GetName();
327+
summary.synced = m_synced;
328+
summary.best_block_height = m_best_block_index.load()->nHeight;
329+
return summary;
330+
}

src/index/base.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@
1313

1414
class CBlockIndex;
1515

16+
struct IndexSummary {
17+
std::string name;
18+
bool synced{false};
19+
int best_block_height{0};
20+
};
21+
1622
/**
1723
* Base class for indices of blockchain data. This implements
1824
* CValidationInterface and ensures blocks are indexed sequentially according
@@ -106,6 +112,9 @@ class BaseIndex : public CValidationInterface
106112

107113
/// Stops the instance from staying in sync with blockchain updates.
108114
void Stop();
115+
116+
/// Get a summary of the index and its state.
117+
IndexSummary GetSummary() const;
109118
};
110119

111120
#endif // BITCOIN_INDEX_BASE_H

src/rpc/misc.cpp

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
55

66
#include <httpserver.h>
7+
#include <index/blockfilterindex.h>
8+
#include <index/txindex.h>
79
#include <interfaces/chain.h>
810
#include <key_io.h>
911
#include <node/context.h>
@@ -636,6 +638,60 @@ static RPCHelpMan echo(const std::string& name)
636638
static RPCHelpMan echo() { return echo("echo"); }
637639
static RPCHelpMan echojson() { return echo("echojson"); }
638640

641+
static UniValue SummaryToJSON(const IndexSummary&& summary, std::string index_name)
642+
{
643+
UniValue ret_summary(UniValue::VOBJ);
644+
if (!index_name.empty() && index_name != summary.name) return ret_summary;
645+
646+
UniValue entry(UniValue::VOBJ);
647+
entry.pushKV("synced", summary.synced);
648+
entry.pushKV("best_block_height", summary.best_block_height);
649+
ret_summary.pushKV(summary.name, entry);
650+
return ret_summary;
651+
}
652+
653+
static RPCHelpMan getindexinfo()
654+
{
655+
return RPCHelpMan{"getindexinfo",
656+
"\nReturns the status of one or all available indices currently running in the node.\n",
657+
{
658+
{"index_name", RPCArg::Type::STR, RPCArg::Optional::OMITTED_NAMED_ARG, "Filter results for an index with a specific name."},
659+
},
660+
RPCResult{
661+
RPCResult::Type::OBJ, "", "", {
662+
{
663+
RPCResult::Type::OBJ, "name", "The name of the index",
664+
{
665+
{RPCResult::Type::BOOL, "synced", "Whether the index is synced or not"},
666+
{RPCResult::Type::NUM, "best_block_height", "The block height to which the index is synced"},
667+
}
668+
},
669+
},
670+
},
671+
RPCExamples{
672+
HelpExampleCli("getindexinfo", "")
673+
+ HelpExampleRpc("getindexinfo", "")
674+
+ HelpExampleCli("getindexinfo", "txindex")
675+
+ HelpExampleRpc("getindexinfo", "txindex")
676+
},
677+
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
678+
{
679+
UniValue result(UniValue::VOBJ);
680+
const std::string index_name = request.params[0].isNull() ? "" : request.params[0].get_str();
681+
682+
if (g_txindex) {
683+
result.pushKVs(SummaryToJSON(g_txindex->GetSummary(), index_name));
684+
}
685+
686+
ForEachBlockFilterIndex([&result, &index_name](const BlockFilterIndex& index) {
687+
result.pushKVs(SummaryToJSON(index.GetSummary(), index_name));
688+
});
689+
690+
return result;
691+
},
692+
};
693+
}
694+
639695
void RegisterMiscRPCCommands(CRPCTable &t)
640696
{
641697
// clang-format off
@@ -650,6 +706,7 @@ static const CRPCCommand commands[] =
650706
{ "util", "getdescriptorinfo", &getdescriptorinfo, {"descriptor"} },
651707
{ "util", "verifymessage", &verifymessage, {"address","signature","message"} },
652708
{ "util", "signmessagewithprivkey", &signmessagewithprivkey, {"privkey","message"} },
709+
{ "util", "getindexinfo", &getindexinfo, {"index_name"} },
653710

654711
/* Not shown in help */
655712
{ "hidden", "setmocktime", &setmocktime, {"timestamp"}},

0 commit comments

Comments
 (0)