Skip to content

Commit 8426e01

Browse files
committed
Merge bitcoin/bitcoin#30428: log: LogError with FlatFilePos in UndoReadFromDisk
fa14e1d log: Fix __func__ in LogError in blockstorage module (MarcoFalke) fad59a2 log: LogError with FlatFilePos in UndoReadFromDisk (MarcoFalke) aaaa332 refactor: Mark IsBlockPruned const (MarcoFalke) Pull request description: These errors should never happen in normal operation. If they do, knowing the `FlatFilePos` may be useful to determine if data corruption happened. Also, handle the error `pos.IsNull()` as part of `OpenUndoFile`, because it may as well have happened due to data corruption. This mirrors the `LogError` behavior from `ReadBlockFromDisk`. Also, two other fixup commits in this module. ACKs for top commit: kevkevinpal: ACK [fa14e1d](bitcoin/bitcoin@fa14e1d) tdb3: cr and light test ACK fa14e1d ryanofsky: Code review ACK fa14e1d. This should make logging clearer and more consistent Tree-SHA512: abb492a919b4796698d1de0a7874c8eae355422b992aa80dcd6b59c2de1ee0d2949f62b3cf649cd62892976fee640358f7522867ed9d48a595d6f8f4e619df50
2 parents ff827a8 + fa14e1d commit 8426e01

File tree

2 files changed

+12
-18
lines changed

2 files changed

+12
-18
lines changed

src/node/blockstorage.cpp

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -588,7 +588,7 @@ const CBlockIndex* BlockManager::GetLastCheckpoint(const CCheckpointData& data)
588588
return nullptr;
589589
}
590590

591-
bool BlockManager::IsBlockPruned(const CBlockIndex& block)
591+
bool BlockManager::IsBlockPruned(const CBlockIndex& block) const
592592
{
593593
AssertLockHeld(::cs_main);
594594
return m_have_pruned && !(block.nStatus & BLOCK_HAVE_DATA) && (block.nTx > 0);
@@ -703,15 +703,10 @@ bool BlockManager::UndoReadFromDisk(CBlockUndo& blockundo, const CBlockIndex& in
703703
{
704704
const FlatFilePos pos{WITH_LOCK(::cs_main, return index.GetUndoPos())};
705705

706-
if (pos.IsNull()) {
707-
LogError("%s: no undo data available\n", __func__);
708-
return false;
709-
}
710-
711706
// Open history file to read
712707
AutoFile filein{OpenUndoFile(pos, true)};
713708
if (filein.IsNull()) {
714-
LogError("%s: OpenUndoFile failed\n", __func__);
709+
LogError("%s: OpenUndoFile failed for %s\n", __func__, pos.ToString());
715710
return false;
716711
}
717712

@@ -723,13 +718,13 @@ bool BlockManager::UndoReadFromDisk(CBlockUndo& blockundo, const CBlockIndex& in
723718
verifier >> blockundo;
724719
filein >> hashChecksum;
725720
} catch (const std::exception& e) {
726-
LogError("%s: Deserialize or I/O error - %s\n", __func__, e.what());
721+
LogError("%s: Deserialize or I/O error - %s at %s\n", __func__, e.what(), pos.ToString());
727722
return false;
728723
}
729724

730725
// Verify checksum
731726
if (hashChecksum != verifier.GetHash()) {
732-
LogError("%s: Checksum mismatch\n", __func__);
727+
LogError("%s: Checksum mismatch at %s\n", __func__, pos.ToString());
733728
return false;
734729
}
735730

@@ -986,7 +981,7 @@ bool BlockManager::WriteBlockToDisk(const CBlock& block, FlatFilePos& pos) const
986981
// Open history file to append
987982
AutoFile fileout{OpenBlockFile(pos)};
988983
if (fileout.IsNull()) {
989-
LogError("WriteBlockToDisk: OpenBlockFile failed\n");
984+
LogError("%s: OpenBlockFile failed\n", __func__);
990985
return false;
991986
}
992987

@@ -997,7 +992,7 @@ bool BlockManager::WriteBlockToDisk(const CBlock& block, FlatFilePos& pos) const
997992
// Write block
998993
long fileOutPos = ftell(fileout.Get());
999994
if (fileOutPos < 0) {
1000-
LogError("WriteBlockToDisk: ftell failed\n");
995+
LogError("%s: ftell failed\n", __func__);
1001996
return false;
1002997
}
1003998
pos.nPos = (unsigned int)fileOutPos;
@@ -1016,7 +1011,7 @@ bool BlockManager::WriteUndoDataForBlock(const CBlockUndo& blockundo, BlockValid
10161011
if (block.GetUndoPos().IsNull()) {
10171012
FlatFilePos _pos;
10181013
if (!FindUndoPos(state, block.nFile, _pos, ::GetSerializeSize(blockundo) + 40)) {
1019-
LogError("ConnectBlock(): FindUndoPos failed\n");
1014+
LogError("%s: FindUndoPos failed\n", __func__);
10201015
return false;
10211016
}
10221017
if (!UndoWriteToDisk(blockundo, _pos, block.pprev->GetBlockHash())) {
@@ -1055,7 +1050,7 @@ bool BlockManager::ReadBlockFromDisk(CBlock& block, const FlatFilePos& pos) cons
10551050
// Open history file to read
10561051
AutoFile filein{OpenBlockFile(pos, true)};
10571052
if (filein.IsNull()) {
1058-
LogError("ReadBlockFromDisk: OpenBlockFile failed for %s\n", pos.ToString());
1053+
LogError("%s: OpenBlockFile failed for %s\n", __func__, pos.ToString());
10591054
return false;
10601055
}
10611056

@@ -1069,13 +1064,13 @@ bool BlockManager::ReadBlockFromDisk(CBlock& block, const FlatFilePos& pos) cons
10691064

10701065
// Check the header
10711066
if (!CheckProofOfWork(block.GetHash(), block.nBits, GetConsensus())) {
1072-
LogError("ReadBlockFromDisk: Errors in block header at %s\n", pos.ToString());
1067+
LogError("%s: Errors in block header at %s\n", __func__, pos.ToString());
10731068
return false;
10741069
}
10751070

10761071
// Signet only: check block solution
10771072
if (GetConsensus().signet_blocks && !CheckSignetBlockSolution(block, GetConsensus())) {
1078-
LogError("ReadBlockFromDisk: Errors in block solution at %s\n", pos.ToString());
1073+
LogError("%s: Errors in block solution at %s\n", __func__, pos.ToString());
10791074
return false;
10801075
}
10811076

@@ -1090,8 +1085,7 @@ bool BlockManager::ReadBlockFromDisk(CBlock& block, const CBlockIndex& index) co
10901085
return false;
10911086
}
10921087
if (block.GetHash() != index.GetBlockHash()) {
1093-
LogError("ReadBlockFromDisk(CBlock&, CBlockIndex*): GetHash() doesn't match index for %s at %s\n",
1094-
index.ToString(), block_pos.ToString());
1088+
LogError("%s: GetHash() doesn't match index for %s at %s\n", __func__, index.ToString(), block_pos.ToString());
10951089
return false;
10961090
}
10971091
return true;

src/node/blockstorage.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ class BlockManager
404404
bool m_have_pruned = false;
405405

406406
//! Check whether the block associated with this index entry is pruned or not.
407-
bool IsBlockPruned(const CBlockIndex& block) EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
407+
bool IsBlockPruned(const CBlockIndex& block) const EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
408408

409409
//! Create or update a prune lock identified by its name
410410
void UpdatePruneLock(const std::string& name, const PruneLockInfo& lock_info) EXCLUSIVE_LOCKS_REQUIRED(::cs_main);

0 commit comments

Comments
 (0)