From 462390c85f1174b3cf83dfb0f74922049e5cb8af Mon Sep 17 00:00:00 2001
From: TheCharlatan <seb.kung@gmail.com>
Date: Sat, 8 Jul 2023 09:20:59 +0200
Subject: refactor: Move stopafterblockimport handling out of blockstorage

This has the benefit of moving the StartShutdown call out of the
blockstorage file and thus out of the kernel's responsibility. The user
can now decide if he wants to start shutdown / interrupt after a block
import or not.
---
 src/init.cpp                   | 8 +++++++-
 src/kernel/blockmanager_opts.h | 3 ---
 src/node/blockmanager_args.cpp | 1 -
 src/node/blockstorage.cpp      | 6 ------
 src/node/blockstorage.h        | 2 --
 5 files changed, 7 insertions(+), 13 deletions(-)

(limited to 'src')

diff --git a/src/init.cpp b/src/init.cpp
index e96485b3aa..2f4eec060b 100644
--- a/src/init.cpp
+++ b/src/init.cpp
@@ -114,7 +114,6 @@
 #include <zmq/zmqrpc.h>
 #endif
 
-using kernel::DEFAULT_STOPAFTERBLOCKIMPORT;
 using kernel::DumpMempool;
 using kernel::ValidationCacheSizes;
 
@@ -136,6 +135,7 @@ using node::VerifyLoadedChainstate;
 static constexpr bool DEFAULT_PROXYRANDOMIZE{true};
 static constexpr bool DEFAULT_REST_ENABLE{false};
 static constexpr bool DEFAULT_I2P_ACCEPT_INCOMING{true};
+static constexpr bool DEFAULT_STOPAFTERBLOCKIMPORT{false};
 
 #ifdef WIN32
 // Win32 LevelDB doesn't use filedescriptors, and the ones used for
@@ -1653,6 +1653,12 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
     chainman.m_thread_load = std::thread(&util::TraceThread, "initload", [=, &chainman, &args, &node] {
         // Import blocks
         ImportBlocks(chainman, vImportFiles);
+        if (args.GetBoolArg("-stopafterblockimport", DEFAULT_STOPAFTERBLOCKIMPORT)) {
+            LogPrintf("Stopping after block import\n");
+            StartShutdown();
+            return;
+        }
+
         // Start indexes initial sync
         if (!StartIndexBackgroundSync(node)) {
             bilingual_str err_str = _("Failed to start indexes, shutting down..");
diff --git a/src/kernel/blockmanager_opts.h b/src/kernel/blockmanager_opts.h
index 0251bbb10a..deeba7e318 100644
--- a/src/kernel/blockmanager_opts.h
+++ b/src/kernel/blockmanager_opts.h
@@ -14,8 +14,6 @@ class CChainParams;
 
 namespace kernel {
 
-static constexpr bool DEFAULT_STOPAFTERBLOCKIMPORT{false};
-
 /**
  * An options struct for `BlockManager`, more ergonomically referred to as
  * `BlockManager::Options` due to the using-declaration in `BlockManager`.
@@ -24,7 +22,6 @@ struct BlockManagerOpts {
     const CChainParams& chainparams;
     uint64_t prune_target{0};
     bool fast_prune{false};
-    bool stop_after_block_import{DEFAULT_STOPAFTERBLOCKIMPORT};
     const fs::path blocks_dir;
     Notifications& notifications;
 };
diff --git a/src/node/blockmanager_args.cpp b/src/node/blockmanager_args.cpp
index 4b296db1b0..fa76566652 100644
--- a/src/node/blockmanager_args.cpp
+++ b/src/node/blockmanager_args.cpp
@@ -32,7 +32,6 @@ util::Result<void> ApplyArgsManOptions(const ArgsManager& args, BlockManager::Op
     opts.prune_target = nPruneTarget;
 
     if (auto value{args.GetBoolArg("-fastprune")}) opts.fast_prune = *value;
-    if (auto value{args.GetBoolArg("-stopafterblockimport")}) opts.stop_after_block_import = *value;
 
     return {};
 }
diff --git a/src/node/blockstorage.cpp b/src/node/blockstorage.cpp
index b3ef29a445..78416ec576 100644
--- a/src/node/blockstorage.cpp
+++ b/src/node/blockstorage.cpp
@@ -946,12 +946,6 @@ void ImportBlocks(ChainstateManager& chainman, std::vector<fs::path> vImportFile
                 return;
             }
         }
-
-        if (chainman.m_blockman.StopAfterBlockImport()) {
-            LogPrintf("Stopping after block import\n");
-            StartShutdown();
-            return;
-        }
     } // End scope of ImportingNow
 }
 } // namespace node
diff --git a/src/node/blockstorage.h b/src/node/blockstorage.h
index 0b8b6de6ad..c2e903e470 100644
--- a/src/node/blockstorage.h
+++ b/src/node/blockstorage.h
@@ -213,8 +213,6 @@ public:
 
     [[nodiscard]] bool LoadingBlocks() const { return m_importing || fReindex; }
 
-    [[nodiscard]] bool StopAfterBlockImport() const { return m_opts.stop_after_block_import; }
-
     /** Calculate the amount of disk space the block & undo files currently use */
     uint64_t CalculateCurrentUsage();
 
-- 
cgit v1.2.3