From 67fcc64802385a6224bb3e9b2069272b9994bf9d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C5=91rinc?= Date: Fri, 24 Jan 2025 15:12:28 +0100 Subject: [PATCH] log: unify error messages for (read/write)[undo]block Co-authored-by: maflcko <6399679+maflcko@users.noreply.github.com> --- src/node/blockstorage.cpp | 39 ++++++++++++++++----------------- src/streams.cpp | 2 +- src/test/blockmanager_tests.cpp | 4 ++-- 3 files changed, 22 insertions(+), 23 deletions(-) diff --git a/src/node/blockstorage.cpp b/src/node/blockstorage.cpp index d6d0274c681..085a2460a6f 100644 --- a/src/node/blockstorage.cpp +++ b/src/node/blockstorage.cpp @@ -662,7 +662,7 @@ bool BlockManager::ReadBlockUndo(CBlockUndo& blockundo, const CBlockIndex& index // Open history file to read AutoFile filein{OpenUndoFile(pos, true)}; if (filein.IsNull()) { - LogError("OpenUndoFile failed for %s", pos.ToString()); + LogError("OpenUndoFile failed for %s while reading block undo", pos.ToString()); return false; } @@ -678,11 +678,11 @@ bool BlockManager::ReadBlockUndo(CBlockUndo& blockundo, const CBlockIndex& index // Verify checksum 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; } } 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; } @@ -935,7 +935,7 @@ bool BlockManager::WriteBlockUndo(const CBlockUndo& blockundo, BlockValidationSt FlatFilePos pos; const unsigned int blockundo_size{static_cast(GetSerializeSize(blockundo))}; 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; } @@ -943,7 +943,7 @@ bool BlockManager::WriteBlockUndo(const CBlockUndo& blockundo, BlockValidationSt // Open history file to append AutoFile fileout{OpenUndoFile(pos)}; 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.")); } @@ -998,7 +998,7 @@ bool BlockManager::ReadBlock(CBlock& block, const FlatFilePos& pos) const // Open history file to read AutoFile filein{OpenBlockFile(pos, true)}; 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; } @@ -1006,19 +1006,19 @@ bool BlockManager::ReadBlock(CBlock& block, const FlatFilePos& pos) const // Read block filein >> TX_WITH_WITNESS(block); } 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; } // Check the header 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; } // Signet only: check block solution 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; } @@ -1033,7 +1033,7 @@ bool BlockManager::ReadBlock(CBlock& block, const CBlockIndex& index) const return false; } 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 true; @@ -1045,13 +1045,13 @@ bool BlockManager::ReadRawBlock(std::vector& block, const FlatFilePos& // 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 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; } hpos.nPos -= 8; // Seek back 8 bytes for meta header AutoFile filein{OpenBlockFile(hpos, true)}; 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; } @@ -1062,22 +1062,21 @@ bool BlockManager::ReadRawBlock(std::vector& block, const FlatFilePos& filein >> blk_start >> blk_size; if (blk_start != GetParams().MessageStart()) { - LogError("%s: Block magic mismatch for %s: %s versus expected %s\n", __func__, pos.ToString(), - HexStr(blk_start), - HexStr(GetParams().MessageStart())); + LogError("Block magic mismatch for %s: %s versus expected %s while reading raw block", + pos.ToString(), HexStr(blk_start), HexStr(GetParams().MessageStart())); return false; } if (blk_size > MAX_SIZE) { - LogError("%s: Block data is larger than maximum deserialization size for %s: %s versus %s\n", __func__, pos.ToString(), - blk_size, MAX_SIZE); + LogError("Block data is larger than maximum deserialization size for %s: %s versus %s while reading raw block", + pos.ToString(), blk_size, MAX_SIZE); return false; } block.resize(blk_size); // Zeroing of memory is intentional here filein.read(MakeWritableByteSpan(block)); } 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; } @@ -1089,12 +1088,12 @@ FlatFilePos BlockManager::WriteBlock(const CBlock& block, int nHeight) const unsigned int block_size{static_cast(GetSerializeSize(TX_WITH_WITNESS(block)))}; FlatFilePos pos{FindNextBlockPos(block_size + STORAGE_HEADER_BYTES, nHeight, block.GetBlockTime())}; if (pos.IsNull()) { - LogError("FindNextBlockPos failed"); + LogError("FindNextBlockPos failed for %s while writing block", pos.ToString()); return FlatFilePos(); } AutoFile fileout{OpenBlockFile(pos)}; if (fileout.IsNull()) { - LogError("OpenBlockFile failed"); + LogError("OpenBlockFile failed for %s while writing block", pos.ToString()); m_opts.notifications.fatalError(_("Failed to write block.")); return FlatFilePos(); } diff --git a/src/streams.cpp b/src/streams.cpp index d82824ee583..d26f526edb9 100644 --- a/src/streams.cpp +++ b/src/streams.cpp @@ -94,7 +94,7 @@ void AutoFile::write(std::span src) std::copy(src.begin(), src.begin() + buf_now.size(), buf_now.begin()); util::Xor(buf_now, m_xor, *m_position); 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()); *m_position += buf_now.size(); diff --git a/src/test/blockmanager_tests.cpp b/src/test/blockmanager_tests.cpp index 26850d0305b..49e49b2d536 100644 --- a/src/test/blockmanager_tests.cpp +++ b/src/test/blockmanager_tests.cpp @@ -179,12 +179,12 @@ BOOST_AUTO_TEST_CASE(blockmanager_flush_block_file) CBlock read_block; 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_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_EQUAL(read_block.nVersion, 2); }