Skip to content

Commit 67fcc64

Browse files
l0rincmaflcko
andcommitted
log: unify error messages for (read/write)[undo]block
Co-authored-by: maflcko <[email protected]>
1 parent a4de160 commit 67fcc64

File tree

3 files changed

+22
-23
lines changed

3 files changed

+22
-23
lines changed

src/node/blockstorage.cpp

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -662,7 +662,7 @@ bool BlockManager::ReadBlockUndo(CBlockUndo& blockundo, const CBlockIndex& index
662662
// Open history file to read
663663
AutoFile filein{OpenUndoFile(pos, true)};
664664
if (filein.IsNull()) {
665-
LogError("OpenUndoFile failed for %s", pos.ToString());
665+
LogError("OpenUndoFile failed for %s while reading block undo", pos.ToString());
666666
return false;
667667
}
668668

@@ -678,11 +678,11 @@ bool BlockManager::ReadBlockUndo(CBlockUndo& blockundo, const CBlockIndex& index
678678

679679
// Verify checksum
680680
if (hashChecksum != verifier.GetHash()) {
681-
LogError("%s: Checksum mismatch at %s\n", __func__, pos.ToString());
681+
LogError("Checksum mismatch at %s while reading block undo", pos.ToString());
682682
return false;
683683
}
684684
} catch (const std::exception& e) {
685-
LogError("%s: Deserialize or I/O error - %s at %s\n", __func__, e.what(), pos.ToString());
685+
LogError("Deserialize or I/O error - %s at %s while reading block undo", e.what(), pos.ToString());
686686
return false;
687687
}
688688

@@ -935,15 +935,15 @@ bool BlockManager::WriteBlockUndo(const CBlockUndo& blockundo, BlockValidationSt
935935
FlatFilePos pos;
936936
const unsigned int blockundo_size{static_cast<unsigned int>(GetSerializeSize(blockundo))};
937937
if (!FindUndoPos(state, block.nFile, pos, blockundo_size + UNDO_DATA_DISK_OVERHEAD)) {
938-
LogError("FindUndoPos failed");
938+
LogError("FindUndoPos failed for %s while writing block undo", pos.ToString());
939939
return false;
940940
}
941941

942942
{
943943
// Open history file to append
944944
AutoFile fileout{OpenUndoFile(pos)};
945945
if (fileout.IsNull()) {
946-
LogError("OpenUndoFile failed");
946+
LogError("OpenUndoFile failed for %s while writing block undo", pos.ToString());
947947
return FatalError(m_opts.notifications, state, _("Failed to write undo data."));
948948
}
949949

@@ -998,27 +998,27 @@ bool BlockManager::ReadBlock(CBlock& block, const FlatFilePos& pos) const
998998
// Open history file to read
999999
AutoFile filein{OpenBlockFile(pos, true)};
10001000
if (filein.IsNull()) {
1001-
LogError("%s: OpenBlockFile failed for %s\n", __func__, pos.ToString());
1001+
LogError("OpenBlockFile failed for %s while reading block", pos.ToString());
10021002
return false;
10031003
}
10041004

10051005
try {
10061006
// Read block
10071007
filein >> TX_WITH_WITNESS(block);
10081008
} catch (const std::exception& e) {
1009-
LogError("%s: Deserialize or I/O error - %s at %s\n", __func__, e.what(), pos.ToString());
1009+
LogError("Deserialize or I/O error - %s at %s while reading block", e.what(), pos.ToString());
10101010
return false;
10111011
}
10121012

10131013
// Check the header
10141014
if (!CheckProofOfWork(block.GetHash(), block.nBits, GetConsensus())) {
1015-
LogError("%s: Errors in block header at %s\n", __func__, pos.ToString());
1015+
LogError("Errors in block header at %s while reading block", pos.ToString());
10161016
return false;
10171017
}
10181018

10191019
// Signet only: check block solution
10201020
if (GetConsensus().signet_blocks && !CheckSignetBlockSolution(block, GetConsensus())) {
1021-
LogError("%s: Errors in block solution at %s\n", __func__, pos.ToString());
1021+
LogError("Errors in block solution at %s while reading block", pos.ToString());
10221022
return false;
10231023
}
10241024

@@ -1033,7 +1033,7 @@ bool BlockManager::ReadBlock(CBlock& block, const CBlockIndex& index) const
10331033
return false;
10341034
}
10351035
if (block.GetHash() != index.GetBlockHash()) {
1036-
LogError("%s: GetHash() doesn't match index for %s at %s\n", __func__, index.ToString(), block_pos.ToString());
1036+
LogError("GetHash() doesn't match index for %s at %s while reading block", index.ToString(), block_pos.ToString());
10371037
return false;
10381038
}
10391039
return true;
@@ -1045,13 +1045,13 @@ bool BlockManager::ReadRawBlock(std::vector<uint8_t>& block, const FlatFilePos&
10451045
// If nPos is less than 8 the pos is null and we don't have the block data
10461046
// Return early to prevent undefined behavior of unsigned int underflow
10471047
if (hpos.nPos < 8) {
1048-
LogError("%s: OpenBlockFile failed for %s\n", __func__, pos.ToString());
1048+
LogError("Failed for %s while reading raw block", pos.ToString());
10491049
return false;
10501050
}
10511051
hpos.nPos -= 8; // Seek back 8 bytes for meta header
10521052
AutoFile filein{OpenBlockFile(hpos, true)};
10531053
if (filein.IsNull()) {
1054-
LogError("%s: OpenBlockFile failed for %s\n", __func__, pos.ToString());
1054+
LogError("OpenBlockFile failed for %s while reading raw block", pos.ToString());
10551055
return false;
10561056
}
10571057

@@ -1062,22 +1062,21 @@ bool BlockManager::ReadRawBlock(std::vector<uint8_t>& block, const FlatFilePos&
10621062
filein >> blk_start >> blk_size;
10631063

10641064
if (blk_start != GetParams().MessageStart()) {
1065-
LogError("%s: Block magic mismatch for %s: %s versus expected %s\n", __func__, pos.ToString(),
1066-
HexStr(blk_start),
1067-
HexStr(GetParams().MessageStart()));
1065+
LogError("Block magic mismatch for %s: %s versus expected %s while reading raw block",
1066+
pos.ToString(), HexStr(blk_start), HexStr(GetParams().MessageStart()));
10681067
return false;
10691068
}
10701069

10711070
if (blk_size > MAX_SIZE) {
1072-
LogError("%s: Block data is larger than maximum deserialization size for %s: %s versus %s\n", __func__, pos.ToString(),
1073-
blk_size, MAX_SIZE);
1071+
LogError("Block data is larger than maximum deserialization size for %s: %s versus %s while reading raw block",
1072+
pos.ToString(), blk_size, MAX_SIZE);
10741073
return false;
10751074
}
10761075

10771076
block.resize(blk_size); // Zeroing of memory is intentional here
10781077
filein.read(MakeWritableByteSpan(block));
10791078
} catch (const std::exception& e) {
1080-
LogError("%s: Read from block file failed: %s for %s\n", __func__, e.what(), pos.ToString());
1079+
LogError("Read from block file failed: %s for %s while reading raw block", e.what(), pos.ToString());
10811080
return false;
10821081
}
10831082

@@ -1089,12 +1088,12 @@ FlatFilePos BlockManager::WriteBlock(const CBlock& block, int nHeight)
10891088
const unsigned int block_size{static_cast<unsigned int>(GetSerializeSize(TX_WITH_WITNESS(block)))};
10901089
FlatFilePos pos{FindNextBlockPos(block_size + STORAGE_HEADER_BYTES, nHeight, block.GetBlockTime())};
10911090
if (pos.IsNull()) {
1092-
LogError("FindNextBlockPos failed");
1091+
LogError("FindNextBlockPos failed for %s while writing block", pos.ToString());
10931092
return FlatFilePos();
10941093
}
10951094
AutoFile fileout{OpenBlockFile(pos)};
10961095
if (fileout.IsNull()) {
1097-
LogError("OpenBlockFile failed");
1096+
LogError("OpenBlockFile failed for %s while writing block", pos.ToString());
10981097
m_opts.notifications.fatalError(_("Failed to write block."));
10991098
return FlatFilePos();
11001099
}

src/streams.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ void AutoFile::write(std::span<const std::byte> src)
9494
std::copy(src.begin(), src.begin() + buf_now.size(), buf_now.begin());
9595
util::Xor(buf_now, m_xor, *m_position);
9696
if (std::fwrite(buf_now.data(), 1, buf_now.size(), m_file) != buf_now.size()) {
97-
throw std::ios_base::failure{"XorFile::write: failed"};
97+
throw std::ios_base::failure{"AutoFile::write: failed"};
9898
}
9999
src = src.subspan(buf_now.size());
100100
*m_position += buf_now.size();

src/test/blockmanager_tests.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,12 +179,12 @@ BOOST_AUTO_TEST_CASE(blockmanager_flush_block_file)
179179
CBlock read_block;
180180
BOOST_CHECK_EQUAL(read_block.nVersion, 0);
181181
{
182-
ASSERT_DEBUG_LOG("ReadBlock: Errors in block header");
182+
ASSERT_DEBUG_LOG("Errors in block header");
183183
BOOST_CHECK(!blockman.ReadBlock(read_block, pos1));
184184
BOOST_CHECK_EQUAL(read_block.nVersion, 1);
185185
}
186186
{
187-
ASSERT_DEBUG_LOG("ReadBlock: Errors in block header");
187+
ASSERT_DEBUG_LOG("Errors in block header");
188188
BOOST_CHECK(!blockman.ReadBlock(read_block, pos2));
189189
BOOST_CHECK_EQUAL(read_block.nVersion, 2);
190190
}

0 commit comments

Comments
 (0)