Skip to content

Commit 0b159b9

Browse files
committed
refactor: Pass InitError as callback to ChainstateManager
This commit is part of the libbitcoinkernel project and removes the shutdown's and, more generally, the kernel library's dependency on interface_ui with a callback function. By removing interface_ui from the kernel library, its dependency on boost is reduced to just boost::multi_index.
1 parent b271102 commit 0b159b9

16 files changed

+71
-43
lines changed

src/Makefile.am

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -934,7 +934,6 @@ libbitcoinkernel_la_SOURCES = \
934934
logging.cpp \
935935
node/blockstorage.cpp \
936936
node/chainstate.cpp \
937-
node/interface_ui.cpp \
938937
node/utxo_snapshot.cpp \
939938
policy/feerate.cpp \
940939
policy/fees.cpp \

src/bitcoin-chainstate.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ int main(int argc, char* argv[])
9191
.notify_header_tip = [](SynchronizationState, int64_t height, int64_t timestamp, bool presync) { std::cout << "Header tip changed: " << height << ", " << timestamp << ", " << presync << std::endl; },
9292
.show_progress = [](const std::string& title, int nProgress, bool resume_possible) { std::cout << "Progress: " << title << ", " << nProgress << ", " << resume_possible << std::endl; },
9393
.do_warning = [](const bilingual_str& warning) { std::cout << "Warning: " << warning.original << std::endl; },
94+
.init_error = [](const bilingual_str& user_message) { std::cout << "Init error: " << user_message.original << std::endl; },
9495
},
9596
};
9697
const node::BlockManager::Options blockman_opts{

src/init.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1455,7 +1455,9 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
14551455
BlockManager::Options blockman_opts{
14561456
.chainparams = chainman_opts.chainparams,
14571457
.blocks_dir = args.GetBlocksDirPath(),
1458-
};
1458+
.init_error_callback = [](const bilingual_str& user_message) {
1459+
InitError(user_message);
1460+
}};
14591461
Assert(!ApplyArgsManOptions(args, blockman_opts)); // no error can happen, already checked in AppInitParameterInteraction
14601462

14611463
// cache size calculations

src/kernel/blockmanager_opts.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#define BITCOIN_KERNEL_BLOCKMANAGER_OPTS_H
77

88
#include <util/fs.h>
9+
#include <util/translation.h>
910

1011
#include <cstdint>
1112

@@ -25,6 +26,7 @@ struct BlockManagerOpts {
2526
bool fast_prune{false};
2627
bool stop_after_block_import{DEFAULT_STOPAFTERBLOCKIMPORT};
2728
const fs::path blocks_dir;
29+
const std::function<void(const bilingual_str& str)> init_error_callback;
2830
};
2931

3032
} // namespace kernel

src/kernel/chainstatemanager_opts.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ struct ChainstateManagerNotificationCallbacks {
3535
const std::function<void(SynchronizationState state, int64_t height, int64_t timestamp, bool presync)> notify_header_tip;
3636
const std::function<void(const std::string& title, int nProgress, bool resume_possible)> show_progress;
3737
const std::function<void(const bilingual_str& warning)> do_warning;
38+
const std::function<void(const bilingual_str& user_message)> init_error;
3839
};
3940

4041
/**

src/node/blockstorage.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,7 @@ void BlockManager::FlushUndoFile(int block_file, bool finalize)
527527
{
528528
FlatFilePos undo_pos_old(block_file, m_blockfile_info[block_file].nUndoSize);
529529
if (!UndoFileSeq().Flush(undo_pos_old, finalize)) {
530-
AbortNode("Flushing undo file to disk failed. This is likely the result of an I/O error.");
530+
AbortNode("Flushing undo file to disk failed. This is likely the result of an I/O error.", m_opts.init_error_callback);
531531
}
532532
}
533533

@@ -546,7 +546,7 @@ void BlockManager::FlushBlockFile(bool fFinalize, bool finalize_undo)
546546

547547
FlatFilePos block_pos_old(m_last_blockfile, m_blockfile_info[m_last_blockfile].nSize);
548548
if (!BlockFileSeq().Flush(block_pos_old, fFinalize)) {
549-
AbortNode("Flushing block file to disk failed. This is likely the result of an I/O error.");
549+
AbortNode("Flushing block file to disk failed. This is likely the result of an I/O error.", m_opts.init_error_callback);
550550
}
551551
// we do not always flush the undo file, as the chain tip may be lagging behind the incoming blocks,
552552
// e.g. during IBD or a sync after a node going offline
@@ -658,7 +658,7 @@ bool BlockManager::FindBlockPos(FlatFilePos& pos, unsigned int nAddSize, unsigne
658658
bool out_of_space;
659659
size_t bytes_allocated = BlockFileSeq().Allocate(pos, nAddSize, out_of_space);
660660
if (out_of_space) {
661-
return AbortNode("Disk space is too low!", _("Disk space is too low!"));
661+
return AbortNode("Disk space is too low!", m_opts.init_error_callback, _("Disk space is too low!"));
662662
}
663663
if (bytes_allocated != 0 && IsPruneMode()) {
664664
m_check_for_pruning = true;
@@ -682,7 +682,7 @@ bool BlockManager::FindUndoPos(BlockValidationState& state, int nFile, FlatFileP
682682
bool out_of_space;
683683
size_t bytes_allocated = UndoFileSeq().Allocate(pos, nAddSize, out_of_space);
684684
if (out_of_space) {
685-
return AbortNode(state, "Disk space is too low!", _("Disk space is too low!"));
685+
return AbortNode(state, "Disk space is too low!", m_opts.init_error_callback, _("Disk space is too low!"));
686686
}
687687
if (bytes_allocated != 0 && IsPruneMode()) {
688688
m_check_for_pruning = true;
@@ -724,7 +724,7 @@ bool BlockManager::WriteUndoDataForBlock(const CBlockUndo& blockundo, BlockValid
724724
return error("ConnectBlock(): FindUndoPos failed");
725725
}
726726
if (!UndoWriteToDisk(blockundo, _pos, block.pprev->GetBlockHash(), GetParams().MessageStart())) {
727-
return AbortNode(state, "Failed to write undo data");
727+
return AbortNode(state, "Failed to write undo data", m_opts.init_error_callback);
728728
}
729729
// rev files are written in block height order, whereas blk files are written as blocks come in (often out of order)
730730
// we want to flush the rev (undo) file once we've written the last block, which is indicated by the last height
@@ -842,7 +842,7 @@ FlatFilePos BlockManager::SaveBlockToDisk(const CBlock& block, int nHeight, CCha
842842
}
843843
if (!position_known) {
844844
if (!WriteBlockToDisk(block, blockPos, GetParams().MessageStart())) {
845-
AbortNode("Failed to write block");
845+
AbortNode("Failed to write block", m_opts.init_error_callback);
846846
return FlatFilePos();
847847
}
848848
}

src/node/blockstorage.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,10 @@ class BlockManager
154154

155155
explicit BlockManager(Options opts)
156156
: m_prune_mode{opts.prune_target > 0},
157-
m_opts{std::move(opts)} {};
157+
m_opts{std::move(opts)}
158+
{
159+
assert(m_opts.init_error_callback);
160+
};
158161

159162
std::atomic<bool> m_importing{false};
160163

src/node/chainstate.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,14 +200,17 @@ ChainstateLoadResult LoadChainstate(ChainstateManager& chainman, const CacheSize
200200
// snapshot is actually validated? Because this entails unusual
201201
// filesystem operations to move leveldb data directories around, and that seems
202202
// too risky to do in the middle of normal runtime.
203-
auto snapshot_completion = chainman.MaybeCompleteSnapshotValidation();
203+
auto snapshot_completion = chainman.MaybeCompleteSnapshotValidation(
204+
[&init_error = chainman.GetInitErrorCb()](bilingual_str msg) {
205+
AbortNode(msg.original, init_error, msg);
206+
});
204207

205208
if (snapshot_completion == SnapshotCompletionResult::SKIPPED) {
206209
// do nothing; expected case
207210
} else if (snapshot_completion == SnapshotCompletionResult::SUCCESS) {
208211
LogPrintf("[snapshot] cleaning up unneeded background chainstate, then reinitializing\n");
209212
if (!chainman.ValidatedSnapshotCleanup()) {
210-
AbortNode("Background chainstate cleanup failed unexpectedly.");
213+
AbortNode("Background chainstate cleanup failed unexpectedly.", chainman.GetInitErrorCb());
211214
}
212215

213216
// Because ValidatedSnapshotCleanup() has torn down chainstates with

src/node/chainstatemanager_notifications.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ ChainstateManagerNotificationCallbacks DefaultChainstateManagerNotifications()
6565
.notify_header_tip = [](SynchronizationState state, int64_t height, int64_t timestamp, bool presync) { uiInterface.NotifyHeaderTip(state, height, timestamp, presync); },
6666
.show_progress = [](const std::string& title, int nProgress, bool resume_possible) { uiInterface.ShowProgress(title, nProgress, resume_possible); },
6767
.do_warning = [](const bilingual_str& warning) { DoWarning(warning); },
68+
.init_error = [](const bilingual_str& user_message) { InitError(user_message); },
6869
};
6970
}
7071
} // namespace node

src/shutdown.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
#endif
1111

1212
#include <logging.h>
13-
#include <node/interface_ui.h>
1413
#include <util/tokenpipe.h>
1514
#include <warnings.h>
1615

@@ -20,14 +19,14 @@
2019
#include <condition_variable>
2120
#endif
2221

23-
bool AbortNode(const std::string& strMessage, bilingual_str user_message)
22+
bool AbortNode(const std::string& strMessage, const std::function<void(const bilingual_str& user_message)>& init_error, bilingual_str user_message)
2423
{
2524
SetMiscWarning(Untranslated(strMessage));
2625
LogPrintf("*** %s\n", strMessage);
2726
if (user_message.empty()) {
2827
user_message = _("A fatal internal error occurred, see debug.log for details");
2928
}
30-
InitError(user_message);
29+
init_error(user_message);
3130
StartShutdown();
3231
return false;
3332
}

0 commit comments

Comments
 (0)