Repository: bitcoin/bitcoin
File: src/node/chainstatemanager_args.h (C/C++)

Code before:
// Copyright (c) 2022 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#ifndef BITCOIN_NODE_CHAINSTATEMANAGER_ARGS_H
#define BITCOIN_NODE_CHAINSTATEMANAGER_ARGS_H

#include <util/result.h>
#include <validation.h>

class ArgsManager;

/** Maximum number of dedicated script-checking threads allowed */
static constexpr int MAX_SCRIPTCHECK_THREADS{15};
/** -par default (number of script-checking threads, 0 = auto) */
static constexpr int DEFAULT_SCRIPTCHECK_THREADS{0};

namespace node {
[[nodiscard]] util::Result<void> ApplyArgsManOptions(const ArgsManager& args, ChainstateManager::Options& opts);
} // namespace node

#endif // BITCOIN_NODE_CHAINSTATEMANAGER_ARGS_H


Review comment (bug):
Related (but not required for this PR), the below patch removing default value duplication would ensure the default `work_threads_num` value doesn't depend on argsmanager being used either:

<details>
<summary>git diff on 8f85d36d68</summary>

```diff
diff --git a/src/init.cpp b/src/init.cpp
index 94a5a08463..61c6f060f1 100644
--- a/src/init.cpp
+++ b/src/init.cpp
@@ -32,6 +32,7 @@
 #include <interfaces/ipc.h>
 #include <interfaces/mining.h>
 #include <interfaces/node.h>
+#include <kernel/chainstatemanager_opts.h>
 #include <kernel/context.h>
 #include <key.h>
 #include <logging.h>
diff --git a/src/kernel/chainstatemanager_opts.h b/src/kernel/chainstatemanager_opts.h
index 1b605f3d55..95827c5d00 100644
--- a/src/kernel/chainstatemanager_opts.h
+++ b/src/kernel/chainstatemanager_opts.h
@@ -8,6 +8,7 @@
 #include <kernel/notifications_interface.h>
 
 #include <arith_uint256.h>
+#include <common/system.h>
 #include <dbwrapper.h>
 #include <script/sigcache.h>
 #include <txdb.h>
@@ -23,6 +24,8 @@ class ValidationSignals;
 
 static constexpr bool DEFAULT_CHECKPOINTS_ENABLED{true};
 static constexpr auto DEFAULT_MAX_TIP_AGE{24h};
+/** -par default (number of script-checking threads) */
+static const int DEFAULT_SCRIPTCHECK_THREADS{GetNumCores() - 1};
 
 namespace kernel {
 
@@ -48,7 +51,7 @@ struct ChainstateManagerOpts {
     Notifications& notifications;
     ValidationSignals* signals{nullptr};
     //! Number of script check worker threads. Zero means no parallel verification.
-    int worker_threads_num{0};
+    int worker_threads_num{DEFAULT_SCRIPTCHECK_THREADS};
     size_t script_execution_cache_bytes{DEFAULT_SCRIPT_EXECUTION_CACHE_BYTES};
     size_t signature_cache_bytes{DEFAULT_SIGNATURE_CACHE_BYTES};
 };
diff --git a/src/node/chainstatemanager_args.cpp b/src/node/chainstatemanager_args.cpp
index cdc8bdd43e..e4a2546b2b 100644
--- a/src/node/chainstatemanager_args.cpp
+++ b/src/node/chainstatemanager_args.cpp
@@ -53,14 +53,13 @@ util::Result<void> ApplyArgsManOptions(const ArgsManager& args, ChainstateManage
     ReadDatabaseArgs(args, opts.coins_db);
     ReadCoinsViewArgs(args, opts.coins_view);
 
-    int script_threads = args.GetIntArg("-par", DEFAULT_SCRIPTCHECK_THREADS);
-    if (script_threads <= 0) {
+    if (auto script_threads{args.GetIntArg("-par")}) {
         // -par=0 means autodetect (number of cores - 1 script threads)
         // -par=-n means "leave n cores free" (number of cores - n - 1 script threads)
-        script_threads += GetNumCores();
+        if (*script_threads <= 0) *script_threads += GetNumCores();
+        // Subtract 1 because the main thread counts towards the par threads.
+        opts.worker_threads_num = *script_threads - 1;
     }
-    // Subtract 1 because the main thread counts towards the par threads.
-    opts.worker_threads_num = script_threads - 1;
 
     if (auto max_size = args.GetIntArg("-maxsigcachesize")) {
         // 1. When supplied with a max_size of 0, both the signature cache and
diff --git a/src/node/chainstatemanager_args.h b/src/node/chainstatemanager_args.h
index af13aa8d3c..701515953e 100644
--- a/src/node/chainstatemanager_args.h
+++ b/src/node/chainstatemanager_args.h
@@ -10,9 +10,6 @@
 
 class ArgsManager;
 
-/** -par default (number of script-checking threads, 0 = auto) */
-static constexpr int DEFAULT_SCRIPTCHECK_THREADS{0};
-
 namespace node {
 [[nodiscard]] util::Result<void> ApplyArgsManOptions(const ArgsManager& args, ChainstateManager::Options& opts);
 } // namespace node

```
</details>



Code after:
// Copyright (c) 2022 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#ifndef BITCOIN_NODE_CHAINSTATEMANAGER_ARGS_H
#define BITCOIN_NODE_CHAINSTATEMANAGER_ARGS_H

#include <util/result.h>
#include <validation.h>

class ArgsManager;

/** -par default (number of script-checking threads, 0 = auto) */
static constexpr int DEFAULT_SCRIPTCHECK_THREADS{0};

namespace node {
[[nodiscard]] util::Result<void> ApplyArgsManOptions(const ArgsManager& args, ChainstateManager::Options& opts);
} // namespace node

#endif // BITCOIN_NODE_CHAINSTATEMANAGER_ARGS_H