fix(rpc): reject unavailable snapshot heights
(cherry picked from commit 0b4b2b797dfba5d15d9f3afff85894716e50a3fc)
This commit is contained in:
+6
-2
@@ -1384,12 +1384,16 @@ CBlockIndex* FindBlockByHeight(int nHeight)
|
||||
pblockindex = pindexGenesisBlock;
|
||||
else
|
||||
pblockindex = pindexBest;
|
||||
if (!pblockindex)
|
||||
return nullptr;
|
||||
if (pblockindexFBBHLast && abs(nHeight - pblockindex->nHeight) > abs(nHeight - pblockindexFBBHLast->nHeight))
|
||||
pblockindex = pblockindexFBBHLast;
|
||||
while (pblockindex->nHeight > nHeight)
|
||||
while (pblockindex && pblockindex->nHeight > nHeight)
|
||||
pblockindex = pblockindex->pprev;
|
||||
while (pblockindex->nHeight < nHeight)
|
||||
while (pblockindex && pblockindex->nHeight < nHeight)
|
||||
pblockindex = pblockindex->pnext;
|
||||
if (!pblockindex || pblockindex->nHeight != nHeight)
|
||||
return nullptr;
|
||||
pblockindexFBBHLast = pblockindex;
|
||||
return pblockindex;
|
||||
}
|
||||
|
||||
@@ -250,6 +250,8 @@ Value getblockhash(const Array& params, bool fHelp)
|
||||
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");
|
||||
return pblockindex->phashBlock->GetHex();
|
||||
}
|
||||
|
||||
@@ -286,14 +288,11 @@ Value getblockbynumber(const Array& params, bool fHelp)
|
||||
if (nHeight < 0 || nHeight > nBestHeight)
|
||||
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;
|
||||
CBlockIndex* pblockindex = mapBlockIndex[hashBestChain];
|
||||
while (pblockindex->nHeight > nHeight)
|
||||
pblockindex = pblockindex->pprev;
|
||||
|
||||
uint256 hash = *pblockindex->phashBlock;
|
||||
|
||||
pblockindex = mapBlockIndex[hash];
|
||||
block.ReadFromDisk(pblockindex, true);
|
||||
|
||||
return blockToJSON(block, pblockindex, params.size() > 1 ? params[1].get_bool() : false);
|
||||
|
||||
Reference in New Issue
Block a user