Skip to content

Commit

Permalink
Remove flushError, fatalError from kernel notifications
Browse files Browse the repository at this point in the history
Also add using declarations where now possible.
  • Loading branch information
TheCharlatan committed Mar 19, 2024
1 parent ec2766c commit f251761
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 144 deletions.
9 changes: 0 additions & 9 deletions src/bitcoin-chainstate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,6 @@ int main(int argc, char* argv[])
{
std::cout << "Warning: " << warning.original << std::endl;
}
void flushError(const std::string& debug_message) override
{
std::cerr << "Error flushing block data to disk: " << debug_message << std::endl;
}
void fatalError(const std::string& debug_message, const bilingual_str& user_message) override
{
std::cerr << "Error: " << debug_message << std::endl;
std::cerr << (user_message.empty() ? "A fatal internal error occurred." : user_message.original) << std::endl;
}
};
auto notifications = std::make_unique<KernelNotifications>();

Expand Down
17 changes: 0 additions & 17 deletions src/kernel/notifications_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,23 +41,6 @@ class Notifications
virtual void headerTip(SynchronizationState state, int64_t height, int64_t timestamp, bool presync) {}
virtual void progress(const bilingual_str& title, int progress_percent, bool resume_possible) {}
virtual void warning(const bilingual_str& warning) {}

//! The flush error notification is sent to notify the user that an error
//! occurred while flushing block data to disk. Kernel code may ignore flush
//! errors that don't affect the immediate operation it is trying to
//! perform. Applications can choose to handle the flush error notification
//! by logging the error, or notifying the user, or triggering an early
//! shutdown as a precaution against causing more errors.
virtual void flushError(const std::string& debug_message) {}

//! The fatal error notification is sent to notify the user when an error
//! occurs in kernel code that can't be recovered from. After this
//! notification is sent, whatever function triggered the error should also
//! return an error code or raise an exception. Applications can choose to
//! handle the fatal error notification by logging the error, or notifying
//! the user, or triggering an early shutdown as a precaution against
//! causing more errors.
virtual void fatalError(const std::string& debug_message, const bilingual_str& user_message = {}) {}
};
} // namespace kernel

Expand Down
46 changes: 24 additions & 22 deletions src/node/blockstorage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
#include <map>
#include <unordered_map>

using kernel::FatalError;

namespace kernel {
static constexpr uint8_t DB_BLOCK_FILES{'f'};
static constexpr uint8_t DB_BLOCK_INDEX{'b'};
Expand Down Expand Up @@ -395,7 +397,7 @@ CBlockIndex* BlockManager::InsertBlockIndex(const uint256& hash)
return pindex;
}

util::Result<bool, kernel::FatalError> BlockManager::LoadBlockIndex(const std::optional<uint256>& snapshot_blockhash)
util::Result<bool, FatalError> BlockManager::LoadBlockIndex(const std::optional<uint256>& snapshot_blockhash)
{
if (!m_block_tree_db->LoadBlockIndexGuts(
GetConsensus(), [this](const uint256& hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main) { return this->InsertBlockIndex(hash); }, m_interrupt)) {
Expand All @@ -405,7 +407,7 @@ util::Result<bool, kernel::FatalError> BlockManager::LoadBlockIndex(const std::o
if (snapshot_blockhash) {
const std::optional<AssumeutxoData> maybe_au_data = GetParams().AssumeutxoForBlockhash(*snapshot_blockhash);
if (!maybe_au_data) {
return {util::Error{Untranslated(strprintf("Assumeutxo data not found for the given blockhash '%s'.", snapshot_blockhash->ToString()))}, kernel::FatalError::AssumeUtxoDataNotFound};
return {util::Error{Untranslated(strprintf("Assumeutxo data not found for the given blockhash '%s'.", snapshot_blockhash->ToString()))}, FatalError::AssumeUtxoDataNotFound};
}
const AssumeutxoData& au_data = *Assert(maybe_au_data);
m_snapshot_height = au_data.height;
Expand Down Expand Up @@ -495,7 +497,7 @@ bool BlockManager::WriteBlockIndexDB()
return true;
}

util::Result<bool, kernel::FatalError> BlockManager::LoadBlockIndexDB(const std::optional<uint256>& snapshot_blockhash)
util::Result<bool, FatalError> BlockManager::LoadBlockIndexDB(const std::optional<uint256>& snapshot_blockhash)
{
auto result{LoadBlockIndex(snapshot_blockhash)};
if (!result || !result.value()) {
Expand Down Expand Up @@ -739,18 +741,18 @@ bool BlockManager::UndoReadFromDisk(CBlockUndo& blockundo, const CBlockIndex& in
return true;
}

util::Result<bool, kernel::FatalError> BlockManager::FlushUndoFile(int block_file, bool finalize)
util::Result<bool, FatalError> BlockManager::FlushUndoFile(int block_file, bool finalize)
{
FlatFilePos undo_pos_old(block_file, m_blockfile_info[block_file].nUndoSize);
if (!UndoFileSeq().Flush(undo_pos_old, finalize)) {
return {util::Error{Untranslated("Flushing undo file to disk failed. This is likely the result of an I/O error.")}, kernel::FatalError::FlushUndoFileFailed};
return {util::Error{Untranslated("Flushing undo file to disk failed. This is likely the result of an I/O error.")}, FatalError::FlushUndoFileFailed};
}
return true;
}

util::Result<bool, kernel::FatalError> BlockManager::FlushBlockFile(int blockfile_num, bool fFinalize, bool finalize_undo)
util::Result<bool, FatalError> BlockManager::FlushBlockFile(int blockfile_num, bool fFinalize, bool finalize_undo)
{
util::Result<bool, kernel::FatalError> result{true};
util::Result<bool, FatalError> result{true};
LOCK(cs_LastBlockFile);

if (m_blockfile_info.size() < 1) {
Expand All @@ -765,7 +767,7 @@ util::Result<bool, kernel::FatalError> BlockManager::FlushBlockFile(int blockfil
FlatFilePos block_pos_old(blockfile_num, m_blockfile_info[blockfile_num].nSize);
if (!BlockFileSeq().Flush(block_pos_old, fFinalize)) {
LogError("%s: Failed to flush block file\n", __func__);
result.Set({util::Error{Untranslated("Flushing block file to disk failed. This is likely the result of an I/O error.")}, kernel::FatalError::FlushBlockFileFailed});
result.Set({util::Error{Untranslated("Flushing block file to disk failed. This is likely the result of an I/O error.")}, FatalError::FlushBlockFileFailed});
}
// we do not always flush the undo file, as the chain tip may be lagging behind the incoming blocks,
// e.g. during IBD or a sync after a node going offline
Expand All @@ -788,7 +790,7 @@ BlockfileType BlockManager::BlockfileTypeForHeight(int height)
return (height >= *m_snapshot_height) ? BlockfileType::ASSUMED : BlockfileType::NORMAL;
}

util::Result<bool, kernel::FatalError> BlockManager::FlushChainstateBlockFile(int tip_height)
util::Result<bool, FatalError> BlockManager::FlushChainstateBlockFile(int tip_height)
{
LOCK(cs_LastBlockFile);
auto& cursor = m_blockfile_cursors[BlockfileTypeForHeight(tip_height)];
Expand Down Expand Up @@ -852,11 +854,11 @@ fs::path BlockManager::GetBlockPosFilename(const FlatFilePos& pos) const
return BlockFileSeq().FileName(pos);
}

util::Result<bool, kernel::FatalError> BlockManager::FindBlockPos(FlatFilePos& pos, unsigned int nAddSize, unsigned int nHeight, uint64_t nTime, bool fKnown)
util::Result<bool, FatalError> BlockManager::FindBlockPos(FlatFilePos& pos, unsigned int nAddSize, unsigned int nHeight, uint64_t nTime, bool fKnown)
{
LOCK(cs_LastBlockFile);

util::Result<bool, kernel::FatalError> result{true};
util::Result<bool, FatalError> result{true};

const BlockfileType chain_type = BlockfileTypeForHeight(nHeight);

Expand Down Expand Up @@ -943,7 +945,7 @@ util::Result<bool, kernel::FatalError> BlockManager::FindBlockPos(FlatFilePos& p
bool out_of_space;
size_t bytes_allocated = BlockFileSeq().Allocate(pos, nAddSize, out_of_space);
if (out_of_space) {
result.Set({util::Error{Untranslated("Disk space is too low!")}, kernel::FatalError::DiskSpaceTooLow});
result.Set({util::Error{Untranslated("Disk space is too low!")}, FatalError::DiskSpaceTooLow});
return result;
}
if (bytes_allocated != 0 && IsPruneMode()) {
Expand All @@ -955,7 +957,7 @@ util::Result<bool, kernel::FatalError> BlockManager::FindBlockPos(FlatFilePos& p
return result;
}

util::Result<bool, kernel::FatalError> BlockManager::FindUndoPos(BlockValidationState& state, int nFile, FlatFilePos& pos, unsigned int nAddSize)
util::Result<bool, FatalError> BlockManager::FindUndoPos(BlockValidationState& state, int nFile, FlatFilePos& pos, unsigned int nAddSize)
{
pos.nFile = nFile;

Expand All @@ -968,7 +970,7 @@ util::Result<bool, kernel::FatalError> BlockManager::FindUndoPos(BlockValidation
bool out_of_space;
size_t bytes_allocated = UndoFileSeq().Allocate(pos, nAddSize, out_of_space);
if (out_of_space) {
return ValidationFatalError(state, "Disk space is too low!", kernel::FatalError::DiskSpaceTooLow);
return ValidationFatalError(state, "Disk space is too low!", FatalError::DiskSpaceTooLow);
}
if (bytes_allocated != 0 && IsPruneMode()) {
m_check_for_pruning = true;
Expand Down Expand Up @@ -1002,12 +1004,12 @@ bool BlockManager::WriteBlockToDisk(const CBlock& block, FlatFilePos& pos) const
return true;
}

util::Result<bool, kernel::FatalError> BlockManager::WriteUndoDataForBlock(const CBlockUndo& blockundo, BlockValidationState& state, CBlockIndex& block)
util::Result<bool, FatalError> BlockManager::WriteUndoDataForBlock(const CBlockUndo& blockundo, BlockValidationState& state, CBlockIndex& block)
{
AssertLockHeld(::cs_main);
const BlockfileType type = BlockfileTypeForHeight(block.nHeight);
auto& cursor = *Assert(WITH_LOCK(cs_LastBlockFile, return m_blockfile_cursors[type]));
util::Result<bool, kernel::FatalError> result{true};
util::Result<bool, FatalError> result{true};

// Write undo information to disk
if (block.GetUndoPos().IsNull()) {
Expand All @@ -1018,7 +1020,7 @@ util::Result<bool, kernel::FatalError> BlockManager::WriteUndoDataForBlock(const
return result;
}
if (!UndoWriteToDisk(blockundo, _pos, block.pprev->GetBlockHash())) {
return ValidationFatalError(state, "Failed to write undo data", kernel::FatalError::WriteUndoDataFailed);
return ValidationFatalError(state, "Failed to write undo data", FatalError::WriteUndoDataFailed);
}
// rev files are written in block height order, whereas blk files are written as blocks come in (often out of order)
// we want to flush the rev (undo) file once we've written the last block, which is indicated by the last height
Expand Down Expand Up @@ -1142,10 +1144,10 @@ bool BlockManager::ReadRawBlockFromDisk(std::vector<uint8_t>& block, const FlatF
return true;
}

util::Result<FlatFilePos, kernel::FatalError> BlockManager::SaveBlockToDisk(const CBlock& block, int nHeight, const FlatFilePos* dbp)
util::Result<FlatFilePos, FatalError> BlockManager::SaveBlockToDisk(const CBlock& block, int nHeight, const FlatFilePos* dbp)
{
unsigned int nBlockSize = ::GetSerializeSize(TX_WITH_WITNESS(block));
util::Result<FlatFilePos, kernel::FatalError> result{FlatFilePos{}};
util::Result<FlatFilePos, FatalError> result{FlatFilePos{}};
FlatFilePos blockPos;
const auto position_known {dbp != nullptr};
if (position_known) {
Expand All @@ -1165,7 +1167,7 @@ util::Result<FlatFilePos, kernel::FatalError> BlockManager::SaveBlockToDisk(cons
}
if (!position_known) {
if (!WriteBlockToDisk(block, blockPos)) {
result.Set({util::Error{Untranslated("Failed to write block")}, kernel::FatalError::BlockWriteFailed});
result.Set({util::Error{Untranslated("Failed to write block")}, FatalError::BlockWriteFailed});
return result;
}
}
Expand All @@ -1190,10 +1192,10 @@ class ImportingNow
}
};

util::Result<void, kernel::FatalError> ImportBlocks(ChainstateManager& chainman, std::vector<fs::path> vImportFiles)
util::Result<void, FatalError> ImportBlocks(ChainstateManager& chainman, std::vector<fs::path> vImportFiles)
{
ScheduleBatchPriority();
util::Result<void, kernel::FatalError> result{};
util::Result<void, FatalError> result{};

{
ImportingNow imp{chainman.m_blockman.m_importing};
Expand Down
11 changes: 0 additions & 11 deletions src/node/kernel_notifications.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,17 +84,6 @@ void KernelNotifications::warning(const bilingual_str& warning)
DoWarning(warning);
}

void KernelNotifications::flushError(const std::string& debug_message)
{
AbortNode(&m_shutdown, m_exit_status, debug_message);
}

void KernelNotifications::fatalError(const std::string& debug_message, const bilingual_str& user_message)
{
node::AbortNode(m_shutdown_on_fatal_error ? &m_shutdown : nullptr,
m_exit_status, debug_message, user_message);
}

void ReadNotificationArgs(const ArgsManager& args, KernelNotifications& notifications)
{
if (auto value{args.GetIntArg("-stopatheight")}) notifications.m_stop_at_height = *value;
Expand Down
4 changes: 0 additions & 4 deletions src/node/kernel_notifications.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,6 @@ class KernelNotifications : public kernel::Notifications

void warning(const bilingual_str& warning) override;

void flushError(const std::string& debug_message) override;

void fatalError(const std::string& debug_message, const bilingual_str& user_message = {}) override;

//! Block height after which blockTip notification will return Interrupted{}, if >0.
int m_stop_at_height{DEFAULT_STOPATHEIGHT};
//! Useful for tests, can be set to false to avoid shutdown on fatal error.
Expand Down

0 comments on commit f251761

Please sign in to comment.