Improve handling of database read errors.

Signed-off-by: Daira Hopwood <daira@jacaranda.org>
This commit is contained in:
Daira Hopwood 2022-11-08 23:34:14 +00:00
parent aee8d5c0ec
commit 8c958e13a5
5 changed files with 32 additions and 13 deletions

View File

@ -95,7 +95,10 @@ CBlockHeader CBlockIndex::GetBlockHeader() const
header.nSolution = nSolution;
} else {
CDiskBlockIndex dbindex;
assert(pblocktree->ReadDiskBlockIndex(GetBlockHash(), dbindex));
if (!pblocktree->ReadDiskBlockIndex(GetBlockHash(), dbindex)) {
LogPrintf("%s: Failed to read index entry", __func__);
throw std::runtime_error("Failed to read index entry");
}
header.nSolution = dbindex.GetSolution();
}
return header;

View File

@ -6134,7 +6134,11 @@ void static CheckBlockIndex(const Consensus::Params& consensusParams)
}
}
}
// assert(pindex->GetBlockHash() == pindex->GetBlockHeader().GetHash()); // Perhaps too slow
// try {
// assert(pindex->GetBlockHash() == pindex->GetBlockHeader().GetHash()); // Perhaps too slow
// } catch (const runtime_error&) {
// assert(!"Failed to read index entry");
// }
// End: actual consistency checks.
// Try descending into the first subnode.

View File

@ -154,8 +154,12 @@ static bool rest_headers(HTTPRequest* req,
}
if (rf == RF_BINARY || rf == RF_HEX) {
for (const CBlockIndex *pindex : headers) {
ssHeader << pindex->GetBlockHeader();
try {
for (const CBlockIndex *pindex : headers) {
ssHeader << pindex->GetBlockHeader();
}
} catch (const std::runtime_error&) {
return RESTERR(req, HTTP_INTERNAL_SERVER_ERROR, "Failed to read index entry");
}
}
}

View File

@ -684,15 +684,18 @@ UniValue getblockheader(const UniValue& params, bool fHelp)
CBlockIndex* pblockindex = mapBlockIndex[hash];
if (!fVerbose)
{
CDataStream ssBlock(SER_NETWORK, PROTOCOL_VERSION);
ssBlock << pblockindex->GetBlockHeader();
std::string strHex = HexStr(ssBlock.begin(), ssBlock.end());
return strHex;
try {
if (!fVerbose) {
CDataStream ssBlock(SER_NETWORK, PROTOCOL_VERSION);
ssBlock << pblockindex->GetBlockHeader();
std::string strHex = HexStr(ssBlock.begin(), ssBlock.end());
return strHex;
} else {
return blockheaderToJSON(pblockindex);
}
} catch (const runtime_error&) {
throw JSONRPCError(RPC_DATABASE_ERROR, "Failed to read index entry");
}
return blockheaderToJSON(pblockindex);
}
UniValue getblock(const UniValue& params, bool fHelp)

View File

@ -634,7 +634,12 @@ bool CBlockTreeDB::LoadBlockIndexGuts(
CBlockHeader header;
{
LOCK(cs_main);
header = pindexNew->GetBlockHeader();
try {
header = pindexNew->GetBlockHeader();
} catch (const runtime_error&) {
return error("LoadBlockIndex(): failed to read index entry: diskindex hash = %s",
diskindex.GetBlockHash().ToString());
}
}
if (header.GetHash() != diskindex.GetBlockHash())
return error("LoadBlockIndex(): inconsistent header vs diskindex hash: header hash = %s, diskindex hash = %s",