Skip to content

Commit

Permalink
Improve handling of database read errors.
Browse files Browse the repository at this point in the history
Signed-off-by: Daira Hopwood <daira@jacaranda.org>
  • Loading branch information
daira committed Nov 21, 2022
1 parent aee8d5c commit 8c958e1
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 13 deletions.
5 changes: 4 additions & 1 deletion src/chain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 5 additions & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
8 changes: 6 additions & 2 deletions src/rest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}
}
Expand Down
19 changes: 11 additions & 8 deletions src/rpc/blockchain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 6 additions & 1 deletion src/txdb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 8c958e1

Please sign in to comment.