mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-01-25 10:43:19 -03:00
rpc: Add getindexinfo RPC
This commit is contained in:
parent
a57af897ec
commit
667bc7a7f7
3 changed files with 75 additions and 0 deletions
|
@ -319,3 +319,12 @@ void BaseIndex::Stop()
|
|||
m_thread_sync.join();
|
||||
}
|
||||
}
|
||||
|
||||
IndexSummary BaseIndex::GetSummary() const
|
||||
{
|
||||
IndexSummary summary{};
|
||||
summary.name = GetName();
|
||||
summary.synced = m_synced;
|
||||
summary.best_block_height = m_best_block_index.load()->nHeight;
|
||||
return summary;
|
||||
}
|
||||
|
|
|
@ -13,6 +13,12 @@
|
|||
|
||||
class CBlockIndex;
|
||||
|
||||
struct IndexSummary {
|
||||
std::string name;
|
||||
bool synced{false};
|
||||
int best_block_height{0};
|
||||
};
|
||||
|
||||
/**
|
||||
* Base class for indices of blockchain data. This implements
|
||||
* CValidationInterface and ensures blocks are indexed sequentially according
|
||||
|
@ -106,6 +112,9 @@ public:
|
|||
|
||||
/// Stops the instance from staying in sync with blockchain updates.
|
||||
void Stop();
|
||||
|
||||
/// Get a summary of the index and its state.
|
||||
IndexSummary GetSummary() const;
|
||||
};
|
||||
|
||||
#endif // BITCOIN_INDEX_BASE_H
|
||||
|
|
|
@ -4,6 +4,8 @@
|
|||
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||
|
||||
#include <httpserver.h>
|
||||
#include <index/blockfilterindex.h>
|
||||
#include <index/txindex.h>
|
||||
#include <interfaces/chain.h>
|
||||
#include <key_io.h>
|
||||
#include <node/context.h>
|
||||
|
@ -636,6 +638,60 @@ static RPCHelpMan echo(const std::string& name)
|
|||
static RPCHelpMan echo() { return echo("echo"); }
|
||||
static RPCHelpMan echojson() { return echo("echojson"); }
|
||||
|
||||
static UniValue SummaryToJSON(const IndexSummary&& summary, std::string index_name)
|
||||
{
|
||||
UniValue ret_summary(UniValue::VOBJ);
|
||||
if (!index_name.empty() && index_name != summary.name) return ret_summary;
|
||||
|
||||
UniValue entry(UniValue::VOBJ);
|
||||
entry.pushKV("synced", summary.synced);
|
||||
entry.pushKV("best_block_height", summary.best_block_height);
|
||||
ret_summary.pushKV(summary.name, entry);
|
||||
return ret_summary;
|
||||
}
|
||||
|
||||
static RPCHelpMan getindexinfo()
|
||||
{
|
||||
return RPCHelpMan{"getindexinfo",
|
||||
"\nReturns the status of one or all available indices currently running in the node.\n",
|
||||
{
|
||||
{"index_name", RPCArg::Type::STR, RPCArg::Optional::OMITTED_NAMED_ARG, "Filter results for an index with a specific name."},
|
||||
},
|
||||
RPCResult{
|
||||
RPCResult::Type::OBJ, "", "", {
|
||||
{
|
||||
RPCResult::Type::OBJ, "name", "The name of the index",
|
||||
{
|
||||
{RPCResult::Type::BOOL, "synced", "Whether the index is synced or not"},
|
||||
{RPCResult::Type::NUM, "best_block_height", "The block height to which the index is synced"},
|
||||
}
|
||||
},
|
||||
},
|
||||
},
|
||||
RPCExamples{
|
||||
HelpExampleCli("getindexinfo", "")
|
||||
+ HelpExampleRpc("getindexinfo", "")
|
||||
+ HelpExampleCli("getindexinfo", "txindex")
|
||||
+ HelpExampleRpc("getindexinfo", "txindex")
|
||||
},
|
||||
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
|
||||
{
|
||||
UniValue result(UniValue::VOBJ);
|
||||
const std::string index_name = request.params[0].isNull() ? "" : request.params[0].get_str();
|
||||
|
||||
if (g_txindex) {
|
||||
result.pushKVs(SummaryToJSON(g_txindex->GetSummary(), index_name));
|
||||
}
|
||||
|
||||
ForEachBlockFilterIndex([&result, &index_name](const BlockFilterIndex& index) {
|
||||
result.pushKVs(SummaryToJSON(index.GetSummary(), index_name));
|
||||
});
|
||||
|
||||
return result;
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
void RegisterMiscRPCCommands(CRPCTable &t)
|
||||
{
|
||||
// clang-format off
|
||||
|
@ -650,6 +706,7 @@ static const CRPCCommand commands[] =
|
|||
{ "util", "getdescriptorinfo", &getdescriptorinfo, {"descriptor"} },
|
||||
{ "util", "verifymessage", &verifymessage, {"address","signature","message"} },
|
||||
{ "util", "signmessagewithprivkey", &signmessagewithprivkey, {"privkey","message"} },
|
||||
{ "util", "getindexinfo", &getindexinfo, {"index_name"} },
|
||||
|
||||
/* Not shown in help */
|
||||
{ "hidden", "setmocktime", &setmocktime, {"timestamp"}},
|
||||
|
|
Loading…
Add table
Reference in a new issue