Skip to content

Commit

Permalink
refactor, blockstorage: Return fatal error from ImportBlocks
Browse files Browse the repository at this point in the history
Return fatal error ImportBlocks function and add nodiscard annotation.
  • Loading branch information
ryanofsky committed Nov 2, 2024
1 parent a2c85be commit c11669e
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1773,7 +1773,7 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
node.background_init_thread = std::thread(&util::TraceThread, "initload", [=, &chainman, &args, &node] {
ScheduleBatchPriority();
// Import blocks
ImportBlocks(chainman, vImportFiles);
(void)ImportBlocks(chainman, vImportFiles);
if (args.GetBoolArg("-stopafterblockimport", DEFAULT_STOPAFTERBLOCKIMPORT)) {
LogPrintf("Stopping after block import\n");
if (!(Assert(node.shutdown_request))()) {
Expand Down
17 changes: 11 additions & 6 deletions src/node/blockstorage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1241,9 +1241,9 @@ class ImportingNow
}
};

void ImportBlocks(ChainstateManager& chainman, std::span<const fs::path> import_paths)
FlushResult<InterruptResult, AbortFailure> ImportBlocks(ChainstateManager& chainman, std::span<const fs::path> import_paths)
{
FlushResult<InterruptResult, AbortFailure> result; // TODO Return this result!
FlushResult<InterruptResult, AbortFailure> result;
ImportingNow imp{chainman.m_blockman.m_importing};

// -reindex
Expand All @@ -1266,7 +1266,8 @@ void ImportBlocks(ChainstateManager& chainman, std::span<const fs::path> import_
chainman.LoadExternalBlockFile(file, &pos, &blocks_with_unknown_parent) >> result;
if (chainman.m_interrupt) {
LogPrintf("Interrupt requested. Exit %s\n", __func__);
return;
result.Update(Interrupted{});
return result;
}
nFile++;
}
Expand All @@ -1287,7 +1288,8 @@ void ImportBlocks(ChainstateManager& chainman, std::span<const fs::path> import_
chainman.LoadExternalBlockFile(file) >> result;
if (chainman.m_interrupt) {
LogPrintf("Interrupt requested. Exit %s\n", __func__);
return;
result.Update(Interrupted{});
return result;
}
} else {
LogPrintf("Warning: Could not open blocks file %s\n", fs::PathToString(path));
Expand All @@ -1302,11 +1304,14 @@ void ImportBlocks(ChainstateManager& chainman, std::span<const fs::path> import_
for (Chainstate* chainstate : WITH_LOCK(::cs_main, return chainman.GetAll())) {
BlockValidationState state;
if (!(chainstate->ActivateBestChain(state, nullptr) >> result)) {
chainman.GetNotifications().fatalError(strprintf(_("Failed to connect best block (%s)."), state.ToString()));
return;
auto error{strprintf(_("Failed to connect best block (%s)."), state.ToString())};
chainman.GetNotifications().fatalError(error);
result.Update({util::Error{std::move(error)}, AbortFailure{.fatal = true}});
return result;
}
}
// End scope of ImportingNow
return result;
}

std::ostream& operator<<(std::ostream& os, const BlockfileType& type) {
Expand Down
2 changes: 1 addition & 1 deletion src/node/blockstorage.h
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ class BlockManager
void CleanupBlockRevFiles() const;
};

void ImportBlocks(ChainstateManager& chainman, std::span<const fs::path> import_paths);
[[nodiscard]] kernel::FlushResult<kernel::InterruptResult, kernel::AbortFailure> ImportBlocks(ChainstateManager& chainman, std::span<const fs::path> import_paths);
} // namespace node

#endif // BITCOIN_NODE_BLOCKSTORAGE_H

0 comments on commit c11669e

Please sign in to comment.