Skip to content

Commit 24bfe4e

Browse files
committed
RPC: introduce 'getblockfileinfo' RPC command
Simple command to retrieve information about a certain block file
1 parent 655dc71 commit 24bfe4e

File tree

4 files changed

+43
-1
lines changed

4 files changed

+43
-1
lines changed

src/node/blockstorage.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -644,7 +644,7 @@ void BlockManager::CleanupBlockRevFiles() const
644644
CBlockFileInfo* BlockManager::GetBlockFileInfo(size_t n)
645645
{
646646
LOCK(cs_LastBlockFile);
647-
647+
if (n > m_blockfile_info.size()-1) return nullptr;
648648
return &m_blockfile_info.at(n);
649649
}
650650

src/rpc/blockchain.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2869,6 +2869,45 @@ return RPCHelpMan{
28692869
};
28702870
}
28712871

2872+
static RPCHelpMan getblockfileinfo()
2873+
{
2874+
return RPCHelpMan{
2875+
"getblockfileinfo",
2876+
"Retrieves information about a certain block file.",
2877+
{
2878+
{"file_number", RPCArg::Type::NUM, RPCArg::Optional::NO, "block file number"},
2879+
},
2880+
RPCResult{
2881+
RPCResult::Type::OBJ, "", "",
2882+
{
2883+
{RPCResult::Type::NUM, "blocks_num", "the number of blocks stored in the file"},
2884+
{RPCResult::Type::NUM, "lowest_block", "the height of the lowest block inside the file"},
2885+
{RPCResult::Type::NUM, "highest_block", "the height of the highest block inside the file"},
2886+
{RPCResult::Type::NUM, "data_size", "the number of used bytes in the block file"},
2887+
{RPCResult::Type::NUM, "undo_size", "the number of used bytes in the undo file"},
2888+
}
2889+
},
2890+
RPCExamples{ HelpExampleCli("getblockfileinfo", "0") },
2891+
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue {
2892+
NodeContext& node = EnsureAnyNodeContext(request.context);
2893+
2894+
int block_num = request.params[0].getInt<int>();
2895+
if (block_num < 0) throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid block number");
2896+
2897+
CBlockFileInfo* info = node.chainman->m_blockman.GetBlockFileInfo(block_num);
2898+
if (!info) throw JSONRPCError(RPC_INVALID_PARAMETER, "block file not found");
2899+
2900+
UniValue result(UniValue::VOBJ);
2901+
result.pushKV("blocks_num", info->nBlocks);
2902+
result.pushKV("lowest_block", info->nHeightFirst);
2903+
result.pushKV("highest_block", info->nHeightLast);
2904+
result.pushKV("data_size", info->nSize);
2905+
result.pushKV("undo_size", info->nUndoSize);
2906+
2907+
return result;
2908+
}
2909+
};
2910+
}
28722911

28732912
void RegisterBlockchainRPCCommands(CRPCTable& t)
28742913
{
@@ -2896,6 +2935,7 @@ void RegisterBlockchainRPCCommands(CRPCTable& t)
28962935
{"blockchain", &dumptxoutset},
28972936
{"blockchain", &loadtxoutset},
28982937
{"blockchain", &getchainstates},
2938+
{"hidden", &getblockfileinfo},
28992939
{"hidden", &invalidateblock},
29002940
{"hidden", &reconsiderblock},
29012941
{"hidden", &waitfornewblock},

src/rpc/client.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ static const CRPCConvertParam vRPCConvertParams[] =
242242
{ "verifychain", 1, "nblocks" },
243243
{ "getblockstats", 0, "hash_or_height" },
244244
{ "getblockstats", 1, "stats" },
245+
{ "getblockfileinfo", 0, "file_number" },
245246
{ "pruneblockchain", 0, "height" },
246247
{ "keypoolrefill", 0, "newsize" },
247248
{ "getrawmempool", 0, "verbose" },

src/test/fuzz/rpc.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ const std::vector<std::string> RPC_COMMANDS_SAFE_FOR_FUZZING{
120120
"getblockfrompeer", // when no peers are connected, no p2p message is sent
121121
"getblockhash",
122122
"getblockheader",
123+
"getblockfileinfo",
123124
"getblockstats",
124125
"getblocktemplate",
125126
"getchaintips",

0 commit comments

Comments
 (0)