fix(rpc): reject unavailable snapshot heights

(cherry picked from commit 0b4b2b797dfba5d15d9f3afff85894716e50a3fc)
This commit is contained in:
Ethan Clay
2026-07-12 03:33:07 -04:00
parent 9032359fdc
commit c0dc0573e4
2 changed files with 12 additions and 9 deletions
+6 -2
View File
@@ -1384,12 +1384,16 @@ CBlockIndex* FindBlockByHeight(int nHeight)
pblockindex = pindexGenesisBlock; pblockindex = pindexGenesisBlock;
else else
pblockindex = pindexBest; pblockindex = pindexBest;
if (!pblockindex)
return nullptr;
if (pblockindexFBBHLast && abs(nHeight - pblockindex->nHeight) > abs(nHeight - pblockindexFBBHLast->nHeight)) if (pblockindexFBBHLast && abs(nHeight - pblockindex->nHeight) > abs(nHeight - pblockindexFBBHLast->nHeight))
pblockindex = pblockindexFBBHLast; pblockindex = pblockindexFBBHLast;
while (pblockindex->nHeight > nHeight) while (pblockindex && pblockindex->nHeight > nHeight)
pblockindex = pblockindex->pprev; pblockindex = pblockindex->pprev;
while (pblockindex->nHeight < nHeight) while (pblockindex && pblockindex->nHeight < nHeight)
pblockindex = pblockindex->pnext; pblockindex = pblockindex->pnext;
if (!pblockindex || pblockindex->nHeight != nHeight)
return nullptr;
pblockindexFBBHLast = pblockindex; pblockindexFBBHLast = pblockindex;
return pblockindex; return pblockindex;
} }
+6 -7
View File
@@ -250,6 +250,8 @@ Value getblockhash(const Array& params, bool fHelp)
throw runtime_error("Block number out of range."); throw runtime_error("Block number out of range.");
CBlockIndex* pblockindex = FindBlockByHeight(nHeight); CBlockIndex* pblockindex = FindBlockByHeight(nHeight);
if (!pblockindex || !pblockindex->phashBlock)
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block height not available in local block index");
return pblockindex->phashBlock->GetHex(); return pblockindex->phashBlock->GetHex();
} }
@@ -286,14 +288,11 @@ Value getblockbynumber(const Array& params, bool fHelp)
if (nHeight < 0 || nHeight > nBestHeight) if (nHeight < 0 || nHeight > nBestHeight)
throw runtime_error("Block number out of range."); throw runtime_error("Block number out of range.");
CBlockIndex* pblockindex = FindBlockByHeight(nHeight);
if (!pblockindex || !pblockindex->phashBlock)
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block height not available in local block index");
CBlock block; CBlock block;
CBlockIndex* pblockindex = mapBlockIndex[hashBestChain];
while (pblockindex->nHeight > nHeight)
pblockindex = pblockindex->pprev;
uint256 hash = *pblockindex->phashBlock;
pblockindex = mapBlockIndex[hash];
block.ReadFromDisk(pblockindex, true); block.ReadFromDisk(pblockindex, true);
return blockToJSON(block, pblockindex, params.size() > 1 ? params[1].get_bool() : false); return blockToJSON(block, pblockindex, params.size() > 1 ? params[1].get_bool() : false);