log: unify error messages for (read/write)[undo]block

Co-authored-by: maflcko <6399679+maflcko@users.noreply.github.com>
This commit is contained in:
Lőrinc 2025-01-24 15:12:28 +01:00
parent a4de160492
commit 67fcc64802
3 changed files with 22 additions and 23 deletions

View file

@ -662,7 +662,7 @@ bool BlockManager::ReadBlockUndo(CBlockUndo& blockundo, const CBlockIndex& index
// Open history file to read // Open history file to read
AutoFile filein{OpenUndoFile(pos, true)}; AutoFile filein{OpenUndoFile(pos, true)};
if (filein.IsNull()) { if (filein.IsNull()) {
LogError("OpenUndoFile failed for %s", pos.ToString()); LogError("OpenUndoFile failed for %s while reading block undo", pos.ToString());
return false; return false;
} }
@ -678,11 +678,11 @@ bool BlockManager::ReadBlockUndo(CBlockUndo& blockundo, const CBlockIndex& index
// Verify checksum // Verify checksum
if (hashChecksum != verifier.GetHash()) { if (hashChecksum != verifier.GetHash()) {
LogError("%s: Checksum mismatch at %s\n", __func__, pos.ToString()); LogError("Checksum mismatch at %s while reading block undo", pos.ToString());
return false; return false;
} }
} catch (const std::exception& e) { } catch (const std::exception& e) {
LogError("%s: Deserialize or I/O error - %s at %s\n", __func__, e.what(), pos.ToString()); LogError("Deserialize or I/O error - %s at %s while reading block undo", e.what(), pos.ToString());
return false; return false;
} }
@ -935,7 +935,7 @@ bool BlockManager::WriteBlockUndo(const CBlockUndo& blockundo, BlockValidationSt
FlatFilePos pos; FlatFilePos pos;
const unsigned int blockundo_size{static_cast<unsigned int>(GetSerializeSize(blockundo))}; const unsigned int blockundo_size{static_cast<unsigned int>(GetSerializeSize(blockundo))};
if (!FindUndoPos(state, block.nFile, pos, blockundo_size + UNDO_DATA_DISK_OVERHEAD)) { if (!FindUndoPos(state, block.nFile, pos, blockundo_size + UNDO_DATA_DISK_OVERHEAD)) {
LogError("FindUndoPos failed"); LogError("FindUndoPos failed for %s while writing block undo", pos.ToString());
return false; return false;
} }
@ -943,7 +943,7 @@ bool BlockManager::WriteBlockUndo(const CBlockUndo& blockundo, BlockValidationSt
// Open history file to append // Open history file to append
AutoFile fileout{OpenUndoFile(pos)}; AutoFile fileout{OpenUndoFile(pos)};
if (fileout.IsNull()) { if (fileout.IsNull()) {
LogError("OpenUndoFile failed"); LogError("OpenUndoFile failed for %s while writing block undo", pos.ToString());
return FatalError(m_opts.notifications, state, _("Failed to write undo data.")); return FatalError(m_opts.notifications, state, _("Failed to write undo data."));
} }
@ -998,7 +998,7 @@ bool BlockManager::ReadBlock(CBlock& block, const FlatFilePos& pos) const
// Open history file to read // Open history file to read
AutoFile filein{OpenBlockFile(pos, true)}; AutoFile filein{OpenBlockFile(pos, true)};
if (filein.IsNull()) { if (filein.IsNull()) {
LogError("%s: OpenBlockFile failed for %s\n", __func__, pos.ToString()); LogError("OpenBlockFile failed for %s while reading block", pos.ToString());
return false; return false;
} }
@ -1006,19 +1006,19 @@ bool BlockManager::ReadBlock(CBlock& block, const FlatFilePos& pos) const
// Read block // Read block
filein >> TX_WITH_WITNESS(block); filein >> TX_WITH_WITNESS(block);
} catch (const std::exception& e) { } catch (const std::exception& e) {
LogError("%s: Deserialize or I/O error - %s at %s\n", __func__, e.what(), pos.ToString()); LogError("Deserialize or I/O error - %s at %s while reading block", e.what(), pos.ToString());
return false; return false;
} }
// Check the header // Check the header
if (!CheckProofOfWork(block.GetHash(), block.nBits, GetConsensus())) { if (!CheckProofOfWork(block.GetHash(), block.nBits, GetConsensus())) {
LogError("%s: Errors in block header at %s\n", __func__, pos.ToString()); LogError("Errors in block header at %s while reading block", pos.ToString());
return false; return false;
} }
// Signet only: check block solution // Signet only: check block solution
if (GetConsensus().signet_blocks && !CheckSignetBlockSolution(block, GetConsensus())) { if (GetConsensus().signet_blocks && !CheckSignetBlockSolution(block, GetConsensus())) {
LogError("%s: Errors in block solution at %s\n", __func__, pos.ToString()); LogError("Errors in block solution at %s while reading block", pos.ToString());
return false; return false;
} }
@ -1033,7 +1033,7 @@ bool BlockManager::ReadBlock(CBlock& block, const CBlockIndex& index) const
return false; return false;
} }
if (block.GetHash() != index.GetBlockHash()) { if (block.GetHash() != index.GetBlockHash()) {
LogError("%s: GetHash() doesn't match index for %s at %s\n", __func__, index.ToString(), block_pos.ToString()); LogError("GetHash() doesn't match index for %s at %s while reading block", index.ToString(), block_pos.ToString());
return false; return false;
} }
return true; return true;
@ -1045,13 +1045,13 @@ bool BlockManager::ReadRawBlock(std::vector<uint8_t>& block, const FlatFilePos&
// If nPos is less than 8 the pos is null and we don't have the block data // If nPos is less than 8 the pos is null and we don't have the block data
// Return early to prevent undefined behavior of unsigned int underflow // Return early to prevent undefined behavior of unsigned int underflow
if (hpos.nPos < 8) { if (hpos.nPos < 8) {
LogError("%s: OpenBlockFile failed for %s\n", __func__, pos.ToString()); LogError("Failed for %s while reading raw block", pos.ToString());
return false; return false;
} }
hpos.nPos -= 8; // Seek back 8 bytes for meta header hpos.nPos -= 8; // Seek back 8 bytes for meta header
AutoFile filein{OpenBlockFile(hpos, true)}; AutoFile filein{OpenBlockFile(hpos, true)};
if (filein.IsNull()) { if (filein.IsNull()) {
LogError("%s: OpenBlockFile failed for %s\n", __func__, pos.ToString()); LogError("OpenBlockFile failed for %s while reading raw block", pos.ToString());
return false; return false;
} }
@ -1062,22 +1062,21 @@ bool BlockManager::ReadRawBlock(std::vector<uint8_t>& block, const FlatFilePos&
filein >> blk_start >> blk_size; filein >> blk_start >> blk_size;
if (blk_start != GetParams().MessageStart()) { if (blk_start != GetParams().MessageStart()) {
LogError("%s: Block magic mismatch for %s: %s versus expected %s\n", __func__, pos.ToString(), LogError("Block magic mismatch for %s: %s versus expected %s while reading raw block",
HexStr(blk_start), pos.ToString(), HexStr(blk_start), HexStr(GetParams().MessageStart()));
HexStr(GetParams().MessageStart()));
return false; return false;
} }
if (blk_size > MAX_SIZE) { if (blk_size > MAX_SIZE) {
LogError("%s: Block data is larger than maximum deserialization size for %s: %s versus %s\n", __func__, pos.ToString(), LogError("Block data is larger than maximum deserialization size for %s: %s versus %s while reading raw block",
blk_size, MAX_SIZE); pos.ToString(), blk_size, MAX_SIZE);
return false; return false;
} }
block.resize(blk_size); // Zeroing of memory is intentional here block.resize(blk_size); // Zeroing of memory is intentional here
filein.read(MakeWritableByteSpan(block)); filein.read(MakeWritableByteSpan(block));
} catch (const std::exception& e) { } catch (const std::exception& e) {
LogError("%s: Read from block file failed: %s for %s\n", __func__, e.what(), pos.ToString()); LogError("Read from block file failed: %s for %s while reading raw block", e.what(), pos.ToString());
return false; return false;
} }
@ -1089,12 +1088,12 @@ FlatFilePos BlockManager::WriteBlock(const CBlock& block, int nHeight)
const unsigned int block_size{static_cast<unsigned int>(GetSerializeSize(TX_WITH_WITNESS(block)))}; const unsigned int block_size{static_cast<unsigned int>(GetSerializeSize(TX_WITH_WITNESS(block)))};
FlatFilePos pos{FindNextBlockPos(block_size + STORAGE_HEADER_BYTES, nHeight, block.GetBlockTime())}; FlatFilePos pos{FindNextBlockPos(block_size + STORAGE_HEADER_BYTES, nHeight, block.GetBlockTime())};
if (pos.IsNull()) { if (pos.IsNull()) {
LogError("FindNextBlockPos failed"); LogError("FindNextBlockPos failed for %s while writing block", pos.ToString());
return FlatFilePos(); return FlatFilePos();
} }
AutoFile fileout{OpenBlockFile(pos)}; AutoFile fileout{OpenBlockFile(pos)};
if (fileout.IsNull()) { if (fileout.IsNull()) {
LogError("OpenBlockFile failed"); LogError("OpenBlockFile failed for %s while writing block", pos.ToString());
m_opts.notifications.fatalError(_("Failed to write block.")); m_opts.notifications.fatalError(_("Failed to write block."));
return FlatFilePos(); return FlatFilePos();
} }

View file

@ -94,7 +94,7 @@ void AutoFile::write(std::span<const std::byte> src)
std::copy(src.begin(), src.begin() + buf_now.size(), buf_now.begin()); std::copy(src.begin(), src.begin() + buf_now.size(), buf_now.begin());
util::Xor(buf_now, m_xor, *m_position); util::Xor(buf_now, m_xor, *m_position);
if (std::fwrite(buf_now.data(), 1, buf_now.size(), m_file) != buf_now.size()) { if (std::fwrite(buf_now.data(), 1, buf_now.size(), m_file) != buf_now.size()) {
throw std::ios_base::failure{"XorFile::write: failed"}; throw std::ios_base::failure{"AutoFile::write: failed"};
} }
src = src.subspan(buf_now.size()); src = src.subspan(buf_now.size());
*m_position += buf_now.size(); *m_position += buf_now.size();

View file

@ -179,12 +179,12 @@ BOOST_AUTO_TEST_CASE(blockmanager_flush_block_file)
CBlock read_block; CBlock read_block;
BOOST_CHECK_EQUAL(read_block.nVersion, 0); BOOST_CHECK_EQUAL(read_block.nVersion, 0);
{ {
ASSERT_DEBUG_LOG("ReadBlock: Errors in block header"); ASSERT_DEBUG_LOG("Errors in block header");
BOOST_CHECK(!blockman.ReadBlock(read_block, pos1)); BOOST_CHECK(!blockman.ReadBlock(read_block, pos1));
BOOST_CHECK_EQUAL(read_block.nVersion, 1); BOOST_CHECK_EQUAL(read_block.nVersion, 1);
} }
{ {
ASSERT_DEBUG_LOG("ReadBlock: Errors in block header"); ASSERT_DEBUG_LOG("Errors in block header");
BOOST_CHECK(!blockman.ReadBlock(read_block, pos2)); BOOST_CHECK(!blockman.ReadBlock(read_block, pos2));
BOOST_CHECK_EQUAL(read_block.nVersion, 2); BOOST_CHECK_EQUAL(read_block.nVersion, 2);
} }