mirror of
https://github.com/bitcoin/bitcoin.git
synced 2026-01-20 23:29:12 +01:00
blockstorage: return an error code from ReadRawBlock()
It will enable different error handling flows for different error types. Also, `ReadRawBlockBench` performance has decreased due to no longer reusing a vector with an unchanging capacity - mirroring our production code behavior. Co-authored-by: Hodlinator <172445034+hodlinator@users.noreply.github.com> Co-authored-by: Lőrinc <pap.lorinc@gmail.com>
This commit is contained in:
15
src/rest.cpp
15
src/rest.cpp
@@ -416,20 +416,23 @@ static bool rest_block(const std::any& context,
|
||||
pos = pblockindex->GetBlockPos();
|
||||
}
|
||||
|
||||
std::vector<std::byte> block_data{};
|
||||
if (!chainman.m_blockman.ReadRawBlock(block_data, pos)) {
|
||||
return RESTERR(req, HTTP_NOT_FOUND, hashStr + " not found");
|
||||
const auto block_data{chainman.m_blockman.ReadRawBlock(pos)};
|
||||
if (!block_data) {
|
||||
switch (block_data.error()) {
|
||||
case node::ReadRawError::IO: return RESTERR(req, HTTP_INTERNAL_SERVER_ERROR, "I/O error reading " + hashStr);
|
||||
}
|
||||
assert(false);
|
||||
}
|
||||
|
||||
switch (rf) {
|
||||
case RESTResponseFormat::BINARY: {
|
||||
req->WriteHeader("Content-Type", "application/octet-stream");
|
||||
req->WriteReply(HTTP_OK, block_data);
|
||||
req->WriteReply(HTTP_OK, *block_data);
|
||||
return true;
|
||||
}
|
||||
|
||||
case RESTResponseFormat::HEX: {
|
||||
const std::string strHex{HexStr(block_data) + "\n"};
|
||||
const std::string strHex{HexStr(*block_data) + "\n"};
|
||||
req->WriteHeader("Content-Type", "text/plain");
|
||||
req->WriteReply(HTTP_OK, strHex);
|
||||
return true;
|
||||
@@ -437,7 +440,7 @@ static bool rest_block(const std::any& context,
|
||||
|
||||
case RESTResponseFormat::JSON: {
|
||||
CBlock block{};
|
||||
DataStream block_stream{block_data};
|
||||
DataStream block_stream{*block_data};
|
||||
block_stream >> TX_WITH_WITNESS(block);
|
||||
UniValue objBlock = blockToJSON(chainman.m_blockman, block, *tip, *pblockindex, tx_verbosity, chainman.GetConsensus().powLimit);
|
||||
std::string strJSON = objBlock.write() + "\n";
|
||||
|
||||
Reference in New Issue
Block a user