LCOV - code coverage report
Current view: top level - src - init.cpp (source / functions) Hit Total Coverage
Test: total_coverage.info Lines: 1125 1309 85.9 %
Date: 2026-06-25 07:23:43 Functions: 58 69 84.1 %

          Line data    Source code
       1             : // Copyright (c) 2009-2010 Satoshi Nakamoto
       2             : // Copyright (c) 2009-2021 The Bitcoin Core developers
       3             : // Copyright (c) 2014-2026 The Dash Core developers
       4             : // Distributed under the MIT software license, see the accompanying
       5             : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
       6             : 
       7             : #if defined(HAVE_CONFIG_H)
       8             : #include <config/bitcoin-config.h>
       9             : #endif
      10             : 
      11             : #include <init.h>
      12             : 
      13             : #include <addrman.h>
      14             : #include <banman.h>
      15             : #include <base58.h>
      16             : #include <blockfilter.h>
      17             : #include <chain.h>
      18             : #include <chainparams.h>
      19             : #include <context.h>
      20             : #include <consensus/amount.h>
      21             : #include <deploymentstatus.h>
      22             : #include <fs.h>
      23             : #include <hash.h>
      24             : #include <httpserver.h>
      25             : #include <httprpc.h>
      26             : #include <chainlock/chainlock.h>
      27             : #include <chainlock/handler.h>
      28             : #include <init/common.h>
      29             : #include <interfaces/chain.h>
      30             : #include <index/addressindex.h>
      31             : #include <index/blockfilterindex.h>
      32             : #include <index/coinstatsindex.h>
      33             : #include <index/spentindex.h>
      34             : #include <index/timestampindex.h>
      35             : #include <index/txindex.h>
      36             : #include <interfaces/init.h>
      37             : #include <interfaces/node.h>
      38             : #include <interfaces/wallet.h>
      39             : #include <kernel/coinstats.h>
      40             : #include <mapport.h>
      41             : #include <node/miner.h>
      42             : #include <net.h>
      43             : #include <net_permissions.h>
      44             : #include <net_processing.h>
      45             : #include <netbase.h>
      46             : #include <netgroup.h>
      47             : #include <node/blockstorage.h>
      48             : #include <node/caches.h>
      49             : #include <node/chainstate.h>
      50             : #include <node/context.h>
      51             : #include <node/interface_ui.h>
      52             : #include <node/sync_manager.h>
      53             : #include <node/txreconciliation.h>
      54             : #include <policy/feerate.h>
      55             : #include <policy/fees.h>
      56             : #include <policy/policy.h>
      57             : #include <policy/settings.h>
      58             : #include <rpc/blockchain.h>
      59             : #include <rpc/register.h>
      60             : #include <rpc/server.h>
      61             : #include <rpc/util.h>
      62             : #include <scheduler.h>
      63             : #include <script/sigcache.h>
      64             : #include <script/standard.h>
      65             : #include <shutdown.h>
      66             : #include <sync.h>
      67             : #include <timedata.h>
      68             : #include <torcontrol.h>
      69             : #include <txdb.h>
      70             : #include <txmempool.h>
      71             : #include <util/asmap.h>
      72             : #include <util/error.h>
      73             : #include <util/moneystr.h>
      74             : #include <util/strencodings.h>
      75             : #include <util/string.h>
      76             : #include <util/syserror.h>
      77             : #include <util/system.h>
      78             : #include <util/thread.h>
      79             : #include <util/threadnames.h>
      80             : #include <util/translation.h>
      81             : #include <validation.h>
      82             : #include <validationinterface.h>
      83             : #include <walletinitinterface.h>
      84             : 
      85             : #include <active/context.h>
      86             : #include <active/masternode.h>
      87             : #include <bls/bls.h>
      88             : #include <coinjoin/coinjoin.h>
      89             : #include <coinjoin/server.h>
      90             : #include <coinjoin/walletman.h>
      91             : #include <dsnotificationinterface.h>
      92             : #include <evo/chainhelper.h>
      93             : #include <evo/deterministicmns.h>
      94             : #include <evo/evodb.h>
      95             : #include <evo/specialtxman.h>
      96             : #include <flat-database.h>
      97             : #include <governance/governance.h>
      98             : #include <governance/net_governance.h>
      99             : #include <instantsend/instantsend.h>
     100             : #include <instantsend/net_instantsend.h>
     101             : #include <llmq/context.h>
     102             : #include <llmq/dkgsessionmgr.h>
     103             : #include <llmq/signing.h>
     104             : #include <llmq/net_dkg.h>
     105             : #include <llmq/net_quorum.h>
     106             : #include <llmq/net_signing.h>
     107             : #include <llmq/observer.h>
     108             : #include <llmq/options.h>
     109             : #include <masternode/meta.h>
     110             : #include <masternode/sync.h>
     111             : #include <masternode/utils.h>
     112             : #include <messagesigner.h>
     113             : #include <netfulfilledman.h>
     114             : #include <spork.h>
     115             : #include <stats/client.h>
     116             : 
     117             : #ifdef ENABLE_WALLET
     118             : #include <coinjoin/client.h>
     119             : #include <coinjoin/options.h>
     120             : #endif // ENABLE_WALLET
     121             : 
     122             : #include <algorithm>
     123             : #include <condition_variable>
     124             : #include <cstdint>
     125             : #include <cstdio>
     126             : #include <fstream>
     127             : #include <functional>
     128             : #include <set>
     129             : #include <memory>
     130             : #include <optional>
     131             : #include <string>
     132             : #include <thread>
     133             : #include <vector>
     134             : 
     135             : #ifndef WIN32
     136             : #include <cerrno>
     137             : #include <signal.h>
     138             : #include <sys/stat.h>
     139             : #endif
     140             : 
     141             : #include <boost/signals2/signal.hpp>
     142             : 
     143             : #if ENABLE_ZMQ
     144             : #include <zmq/zmqabstractnotifier.h>
     145             : #include <zmq/zmqnotificationinterface.h>
     146             : #include <zmq/zmqrpc.h>
     147             : #endif
     148             : 
     149             : using kernel::CoinStatsHashType;
     150             : 
     151             : using node::CacheSizes;
     152             : using node::CalculateCacheSizes;
     153             : using node::ChainstateLoadingError;
     154             : using node::ChainstateLoadVerifyError;
     155             : using node::DashChainstateSetupClose;
     156             : using node::DEFAULT_PRINTPRIORITY;
     157             : using node::DEFAULT_STOPAFTERBLOCKIMPORT;
     158             : using node::LoadChainstate;
     159             : using node::NodeContext;
     160             : using node::ThreadImport;
     161             : using node::VerifyLoadedChainstate;
     162             : using node::fPruneMode;
     163             : using node::fReindex;
     164             : using node::nPruneTarget;
     165             : #ifdef ENABLE_WALLET
     166             : using wallet::DEFAULT_DISABLE_WALLET;
     167             : #endif // ENABLE_WALLET
     168             : 
     169             : static constexpr bool DEFAULT_PROXYRANDOMIZE{true};
     170             : static constexpr bool DEFAULT_REST_ENABLE{false};
     171             : static constexpr bool DEFAULT_I2P_ACCEPT_INCOMING{true};
     172             : 
     173             : #ifdef WIN32
     174             : // Win32 LevelDB doesn't use filedescriptors, and the ones used for
     175             : // accessing block files don't count towards the fd_set size limit
     176             : // anyway.
     177             : #define MIN_CORE_FILEDESCRIPTORS 0
     178             : #else
     179             : #define MIN_CORE_FILEDESCRIPTORS 150
     180             : #endif
     181             : 
     182             : static const char* DEFAULT_ASMAP_FILENAME="ip_asn.map";
     183             : /**
     184             :  * The PID file facilities.
     185             :  */
     186             : static const char* BITCOIN_PID_FILENAME = "dashd.pid";
     187             : 
     188        6057 : static fs::path GetPidFile(const ArgsManager& args)
     189             : {
     190        6057 :     return AbsPathForConfigVal(args.GetPathArg("-pid", BITCOIN_PID_FILENAME));
     191           0 : }
     192             : 
     193        3027 : [[nodiscard]] static bool CreatePidFile(const ArgsManager& args)
     194             : {
     195        3027 :     std::ofstream file{GetPidFile(args)};
     196        3027 :     if (file) {
     197             : #ifdef WIN32
     198             :         tfm::format(file, "%d\n", GetCurrentProcessId());
     199             : #else
     200        3027 :         tfm::format(file, "%d\n", getpid());
     201             : #endif
     202        3027 :         return true;
     203             :     } else {
     204           0 :         return InitError(strprintf(_("Unable to create the PID file '%s': %s"), fs::PathToString(GetPidFile(args)), SysErrorString(errno)));
     205             :     }
     206        3027 : }
     207             : 
     208             : //////////////////////////////////////////////////////////////////////////////
     209             : //
     210             : // Shutdown
     211             : //
     212             : 
     213             : //
     214             : // Thread management and startup/shutdown:
     215             : //
     216             : // The network-processing threads are all part of a thread group
     217             : // created by AppInit() or the Qt main() function.
     218             : //
     219             : // A clean exit happens when StartShutdown() or the SIGTERM
     220             : // signal handler sets ShutdownRequested(), which makes main thread's
     221             : // WaitForShutdown() interrupts the thread group.
     222             : // And then, WaitForShutdown() makes all other on-going threads
     223             : // in the thread group join the main thread.
     224             : // Shutdown() is then called to clean up database connections, and stop other
     225             : // threads that should only be stopped after the main network-processing
     226             : // threads have exited.
     227             : //
     228             : // Shutdown for Qt is very similar, only it uses a QTimer to detect
     229             : // ShutdownRequested() getting set, and then does the normal Qt
     230             : // shutdown thing.
     231             : //
     232             : 
     233             : #if HAVE_SYSTEM
     234        3030 : static void ShutdownNotify(const ArgsManager& args)
     235             : {
     236        3030 :     std::vector<std::thread> threads;
     237        3034 :     for (const auto& cmd : args.GetArgs("-shutdownnotify")) {
     238           4 :         threads.emplace_back(runCommand, cmd);
     239             :     }
     240        3034 :     for (auto& t : threads) {
     241           4 :         t.join();
     242             :     }
     243        3030 : }
     244             : #endif
     245             : 
     246        3030 : void Interrupt(NodeContext& node)
     247             : {
     248             : #if HAVE_SYSTEM
     249        3030 :     ShutdownNotify(*node.args);
     250             : #endif
     251        3030 :     InterruptHTTPServer();
     252        3030 :     InterruptHTTPRPC();
     253        3030 :     InterruptRPC();
     254        3030 :     InterruptREST();
     255        3030 :     InterruptTorControl();
     256        3030 :     if (node.peerman) {
     257        2857 :         node.peerman->InterruptHandlers();
     258        2857 :     }
     259        3030 :     InterruptMapPort();
     260        3030 :     if (node.connman)
     261        2935 :         node.connman->Interrupt();
     262        3030 :     if (g_txindex) {
     263        2807 :         g_txindex->Interrupt();
     264        2807 :     }
     265        3030 :     if (g_addressindex) {
     266          10 :         g_addressindex->Interrupt();
     267          10 :     }
     268        3030 :     if (g_timestampindex) {
     269           6 :         g_timestampindex->Interrupt();
     270           6 :     }
     271        3030 :     if (g_spentindex) {
     272          10 :         g_spentindex->Interrupt();
     273          10 :     }
     274        3720 :     ForEachBlockFilterIndex([](BlockFilterIndex& index) { index.Interrupt(); });
     275        3030 :     if (g_coin_stats_index) {
     276          30 :         g_coin_stats_index->Interrupt();
     277          30 :     }
     278        3030 : }
     279             : 
     280             : /** Preparing steps before shutting down or restarting the wallet */
     281        3030 : void PrepareShutdown(NodeContext& node)
     282             : {
     283        3030 :     static Mutex g_shutdown_mutex;
     284        3030 :     TRY_LOCK(g_shutdown_mutex, lock_shutdown);
     285        3030 :     if (!lock_shutdown) return;
     286        3030 :     LogPrintf("%s: In progress...\n", __func__);
     287        3030 :     Assert(node.args);
     288             : 
     289             :     /// Note: Shutdown() must be able to handle cases in which initialization failed part of the way,
     290             :     /// for example if the data directory was found to be locked.
     291             :     /// Be sure that anything that writes files or flushes caches only does this if the respective
     292             :     /// module was initialized.
     293        3030 :     util::ThreadRename("shutoff");
     294        3030 :     if (node.mempool) node.mempool->AddTransactionsUpdated(1);
     295             : 
     296        3030 :     StopHTTPRPC();
     297        3030 :     StopREST();
     298        3030 :     StopRPC();
     299        3030 :     StopHTTPServer();
     300             : 
     301        3030 :     if (node.active_ctx) node.active_ctx->Stop();
     302        3030 :     if (node.clhandler) node.clhandler->Stop();
     303        3030 :     if (node.peerman) node.peerman->StopHandlers();
     304             : 
     305             : 
     306        4466 :     for (const auto& client : node.chain_clients) {
     307        1436 :         client->flush();
     308             :     }
     309        3030 :     StopMapPort();
     310             : 
     311             :     // Because these depend on each-other, we make sure that neither can be
     312             :     // using the other before destroying them.
     313        3030 :     if (node.clhandler) UnregisterValidationInterface(node.clhandler.get());
     314        3030 :     if (node.peerman) UnregisterValidationInterface(node.peerman.get());
     315        3030 :     if (node.connman) node.connman->Stop();
     316             : 
     317        3030 :     StopTorControl();
     318             : 
     319             :     // After everything has been shut down, but before things get flushed, stop the
     320             :     // CScheduler/checkqueue, threadGroup and load block thread.
     321        3030 :     if (node.scheduler) node.scheduler->stop();
     322        3030 :     if (node.chainman && node.chainman->m_load_block.joinable()) node.chainman->m_load_block.join();
     323        3030 :     StopScriptCheckWorkerThreads();
     324             : 
     325             :     // After there are no more peers/RPC left to give us new data which may generate
     326             :     // CValidationInterface callbacks, flush them...
     327        3030 :     GetMainSignals().FlushBackgroundCallbacks();
     328             : 
     329             :     // After the threads that potentially access these pointers have been stopped,
     330             :     // destruct and reset all to nullptr.
     331        3030 :     node.peerman.reset();
     332        3030 :     node.connman.reset();
     333        3030 :     node.banman.reset();
     334        3030 :     node.addrman.reset();
     335        3030 :     node.netgroupman.reset();
     336        3030 :     ::g_stats_client.reset();
     337             : 
     338        5855 :     if (node.mempool && node.mempool->IsLoaded() && node.args->GetBoolArg("-persistmempool", DEFAULT_PERSIST_MEMPOOL)) {
     339        2771 :         DumpMempool(*node.mempool);
     340        2771 :     }
     341             : 
     342             :     // Drop transactions we were still watching, and record fee estimations.
     343        3030 :     if (node.fee_estimator) node.fee_estimator->Flush();
     344             : 
     345             :     // FlushStateToDisk generates a ChainStateFlushed callback, which we should avoid missing
     346        3030 :     if (node.chainman) {
     347        2885 :         LOCK(cs_main);
     348        5770 :         for (CChainState* chainstate : node.chainman->GetAll()) {
     349        2885 :             if (chainstate->CanFlushToDisk()) {
     350        2871 :                 chainstate->ForceFlushStateToDisk();
     351        2871 :             }
     352             :         }
     353        2885 :     }
     354             : 
     355             :     // After there are no more peers/RPC left to give us new data which may generate
     356             :     // CValidationInterface callbacks, flush them...
     357        3030 :     GetMainSignals().FlushBackgroundCallbacks();
     358             : 
     359        3030 :     if (node.observer_ctx) {
     360           6 :         UnregisterValidationInterface(node.observer_ctx.get());
     361           6 :     }
     362             : 
     363        3030 :     if (node.active_ctx) {
     364         660 :         UnregisterValidationInterface(node.active_ctx.get());
     365         660 :     }
     366             : 
     367        3030 :     if (node.cj_walletman) {
     368        2197 :         UnregisterValidationInterface(node.cj_walletman.get());
     369        2197 :     }
     370             : 
     371        3030 :     if (g_ds_notification_interface) {
     372        2857 :         UnregisterValidationInterface(g_ds_notification_interface.get());
     373        2857 :         g_ds_notification_interface.reset();
     374        2857 :     }
     375             : 
     376             :     // After all scheduled tasks have been flushed, destroy pointers
     377             :     // and reset all to nullptr.
     378        3030 :     node.observer_ctx.reset();
     379        3030 :     node.active_ctx.reset();
     380        3030 :     node.govman.reset();
     381        3030 :     node.mn_sync.reset();
     382        3030 :     node.sporkman.reset();
     383        3030 :     node.netfulfilledman.reset();
     384        3030 :     node.mn_metaman.reset();
     385             : 
     386             :     // Stop and delete all indexes only after flushing background callbacks.
     387        3030 :     if (g_txindex) {
     388        2807 :         g_txindex->Stop();
     389        2807 :         g_txindex.reset();
     390        2807 :     }
     391        3030 :     if (g_addressindex) {
     392          10 :         g_addressindex->Stop();
     393          10 :         g_addressindex.reset();
     394          10 :     }
     395        3030 :     if (g_timestampindex) {
     396           6 :         g_timestampindex->Stop();
     397           6 :         g_timestampindex.reset();
     398           6 :     }
     399        3030 :     if (g_spentindex) {
     400          10 :         g_spentindex->Stop();
     401          10 :         g_spentindex.reset();
     402          10 :     }
     403        3030 :     if (g_coin_stats_index) {
     404          30 :         g_coin_stats_index->Stop();
     405          30 :         g_coin_stats_index.reset();
     406          30 :     }
     407        3720 :     ForEachBlockFilterIndex([](BlockFilterIndex& index) { index.Stop(); });
     408        3030 :     DestroyAllBlockFilterIndexes();
     409             : 
     410             :     // Any future callbacks will be dropped. This should absolutely be safe - if
     411             :     // missing a callback results in an unrecoverable situation, unclean shutdown
     412             :     // would too. The only reason to do the above flushes is to let the wallet catch
     413             :     // up with our current chain to avoid any strange pruning edge cases and make
     414             :     // next startup faster by avoiding rescan.
     415             : 
     416        3030 :     if (node.chainman) {
     417        2885 :         LOCK(cs_main);
     418        5770 :         for (CChainState* chainstate : node.chainman->GetAll()) {
     419        2885 :             if (chainstate->CanFlushToDisk()) {
     420        2871 :                 chainstate->ForceFlushStateToDisk();
     421        2871 :                 chainstate->ResetCoinsViews();
     422        2871 :             }
     423             :         }
     424        2885 :         DashChainstateSetupClose(node.chain_helper, node.dmnman, node.llmq_ctx,
     425        2885 :                                  Assert(node.mempool.get()));
     426        2885 :         node.evodb.reset();
     427        2885 :     }
     428        4466 :     for (const auto& client : node.chain_clients) {
     429        1436 :         client->stop();
     430             :     }
     431             : 
     432             : #if ENABLE_ZMQ
     433             :     if (g_zmq_notification_interface) {
     434             :         UnregisterValidationInterface(g_zmq_notification_interface.get());
     435             :         g_zmq_notification_interface.reset();
     436             :     }
     437             : #endif
     438             : 
     439        3030 :     node.chain_clients.clear();
     440             : 
     441             :     // After all wallets are removed, destroy all CoinJoin objects
     442             :     // and reset them to nullptr
     443        3030 :     node.cj_walletman.reset();
     444        3030 :     node.dstxman.reset();
     445             : 
     446        3030 :     UnregisterAllValidationInterfaces();
     447        3030 :     GetMainSignals().UnregisterBackgroundSignalScheduler();
     448             : 
     449             :     // We need to manually release our directory locks if we are expected to restart
     450             :     // because the replacement instance will start before this instance stops and the
     451             :     // global context won't be torn down in time to release the locks automatically.
     452        3030 :     if (RestartRequested()) {
     453           0 :         ReleaseDirectoryLocks();
     454           0 :     }
     455        3030 : }
     456             : 
     457             : /**
     458             : * Shutdown is split into 2 parts:
     459             : * Part 1: shut down everything but the main wallet instance (done in PrepareShutdown() )
     460             : * Part 2: delete wallet instance
     461             : *
     462             : * In case of a restart PrepareShutdown() was already called before, but this method here gets
     463             : * called implicitly when the parent object is deleted. In this case we have to skip the
     464             : * PrepareShutdown() part because it was already executed and just delete the wallet instance.
     465             : */
     466        3030 : void Shutdown(NodeContext& node)
     467             : {
     468             :     // Shutdown part 1: prepare shutdown
     469        3030 :     if(!RestartRequested()) {
     470        3030 :         PrepareShutdown(node);
     471        3030 :     }
     472             :     // Shutdown part 2: delete wallet instance
     473        3030 :     init::UnsetGlobals();
     474        3030 :     node.mempool.reset();
     475        3030 :     node.fee_estimator.reset();
     476        3030 :     node.chainman.reset();
     477        3030 :     node.scheduler.reset();
     478             : 
     479             :     try {
     480        3030 :         if (!fs::remove(GetPidFile(*node.args))) {
     481           3 :             LogPrintf("%s: Unable to remove PID file: File does not exist\n", __func__);
     482           3 :         }
     483        3030 :     } catch (const fs::filesystem_error& e) {
     484           0 :         LogPrintf("%s: Unable to remove PID file: %s\n", __func__, fsbridge::get_filesystem_error_message(e));
     485           0 :     }
     486             : 
     487        3030 :     LogPrintf("%s: done\n", __func__);
     488        3030 : }
     489             : 
     490             : /**
     491             :  * Signal handlers are very limited in what they are allowed to do.
     492             :  * The execution context the handler is invoked in is not guaranteed,
     493             :  * so we restrict handler operations to just touching variables:
     494             :  */
     495             : #ifndef WIN32
     496          42 : static void HandleSIGTERM(int)
     497             : {
     498          42 :     StartShutdown();
     499          42 : }
     500             : 
     501           0 : static void HandleSIGHUP(int)
     502             : {
     503           0 :     LogInstance().m_reopen_file = true;
     504           0 : }
     505             : #else
     506             : static BOOL WINAPI consoleCtrlHandler(DWORD dwCtrlType)
     507             : {
     508             :     StartShutdown();
     509             :     Sleep(INFINITE);
     510             :     return true;
     511             : }
     512             : #endif
     513             : 
     514             : #ifndef WIN32
     515        9324 : static void registerSignalHandler(int signal, void(*handler)(int))
     516             : {
     517             :     struct sigaction sa;
     518        9324 :     sa.sa_handler = handler;
     519        9324 :     sigemptyset(&sa.sa_mask);
     520        9324 :     sa.sa_flags = 0;
     521        9324 :     sigaction(signal, &sa, nullptr);
     522        9324 : }
     523             : #endif
     524             : 
     525        3308 : static boost::signals2::connection rpc_notify_block_change_connection;
     526        3019 : static void OnRPCStarted()
     527             : {
     528        3019 :     rpc_notify_block_change_connection = uiInterface.NotifyBlockTip_connect(std::bind(RPCNotifyBlockChange, std::placeholders::_2));
     529        3019 : }
     530             : 
     531        3019 : static void OnRPCStopped()
     532             : {
     533        3019 :     rpc_notify_block_change_connection.disconnect();
     534        3019 :     RPCNotifyBlockChange(nullptr);
     535        3019 :     g_best_block_cv.notify_all();
     536        3019 :     LogPrint(BCLog::RPC, "RPC stopped.\n");
     537        3019 : }
     538             : 
     539        3789 : void SetupServerArgs(ArgsManager& argsman)
     540             : {
     541        3789 :     SetupHelpOptions(argsman);
     542        3789 :     argsman.AddArg("-help-debug", "Print help message with debugging options and exit", ArgsManager::ALLOW_ANY, OptionsCategory::DEBUG_TEST);
     543             : 
     544        3789 :     init::AddLoggingArgs(argsman);
     545             : 
     546        3789 :     const auto defaultBaseParams = CreateBaseChainParams(CBaseChainParams::MAIN);
     547        3789 :     const auto testnetBaseParams = CreateBaseChainParams(CBaseChainParams::TESTNET);
     548        3789 :     const auto devnetBaseParams = CreateBaseChainParams(CBaseChainParams::DEVNET);
     549        3789 :     const auto regtestBaseParams = CreateBaseChainParams(CBaseChainParams::REGTEST);
     550        3789 :     const auto defaultChainParams = CreateChainParams(argsman, CBaseChainParams::MAIN);
     551        3789 :     const auto testnetChainParams = CreateChainParams(argsman, CBaseChainParams::TESTNET);
     552        3789 :     const auto devnetChainParams = CreateChainParams(argsman, CBaseChainParams::DEVNET);
     553        3789 :     const auto regtestChainParams = CreateChainParams(argsman, CBaseChainParams::REGTEST);
     554             : 
     555             :     // Hidden Options
     556        3789 :     std::vector<std::string> hidden_args = {"-dbcrashratio", "-forcecompactdb", "-printcrashinfo",
     557             :         // GUI args. These will be overwritten by SetupUIArgs for the GUI
     558        3789 :         "-choosedatadir", "-lang=<lang>", "-min", "-resetguisettings", "-splash", "-uiplatform"};
     559             : 
     560             : 
     561             :     // Set all of the args and their help
     562             :     // When adding new options to the categories, please keep and ensure alphabetical ordering.
     563             : #if HAVE_SYSTEM
     564        3789 :     argsman.AddArg("-alertnotify=<cmd>", "Execute command when an alert is raised (%s in cmd is replaced by message)", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
     565             : #endif
     566        3789 :     argsman.AddArg("-assumevalid=<hex>", strprintf("If this block is in the chain assume that it and its ancestors are valid and potentially skip their script verification (0 to verify all, default: %s, testnet: %s, devnet: %s)", defaultChainParams->GetConsensus().defaultAssumeValid.GetHex(), testnetChainParams->GetConsensus().defaultAssumeValid.GetHex(), devnetChainParams->GetConsensus().defaultAssumeValid.GetHex()), ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
     567        3789 :     argsman.AddArg("-blocksdir=<dir>", "Specify directory to hold blocks subdirectory for *.dat files (default: <datadir>)", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
     568        3789 :     argsman.AddArg("-fastprune", "Use smaller block files and lower minimum prune height for testing purposes", ArgsManager::ALLOW_ANY | ArgsManager::DEBUG_ONLY, OptionsCategory::DEBUG_TEST);
     569        3789 :     argsman.AddArg("-tinyblk", "Use smaller block files for testing purposes", ArgsManager::ALLOW_ANY | ArgsManager::DEBUG_ONLY, OptionsCategory::DEBUG_TEST);
     570             : #if HAVE_SYSTEM
     571        3789 :     argsman.AddArg("-blocknotify=<cmd>", "Execute command when the best block changes (%s in cmd is replaced by block hash)", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
     572             : #endif
     573        3789 :     argsman.AddArg("-blockreconstructionextratxn=<n>", strprintf("Extra transactions to keep in memory for compact block reconstructions (default: %u)", DEFAULT_BLOCK_RECONSTRUCTION_EXTRA_TXN), ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
     574        3789 :     argsman.AddArg("-blocksonly", strprintf("Whether to reject transactions from network peers. Automatic broadcast and rebroadcast of any transactions from inbound peers is disabled, unless the peer has the 'forcerelay' permission. RPC transactions are not affected. (default: %u)", DEFAULT_BLOCKSONLY), ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
     575             : #if HAVE_SYSTEM
     576        3789 :     argsman.AddArg("-chainlocknotify=<cmd>", "Execute command when the best chainlock changes (%s in cmd is replaced by chainlocked block hash)", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
     577             : #endif
     578        3789 :     argsman.AddArg("-coinstatsindex", strprintf("Maintain coinstats index used by the gettxoutsetinfo RPC (default: %u)", DEFAULT_COINSTATSINDEX), ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
     579        3789 :     argsman.AddArg("-conf=<file>", strprintf("Specify path to read-only configuration file. Relative paths will be prefixed by datadir location (only useable from command line, not configuration file) (default: %s)", BITCOIN_CONF_FILENAME), ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
     580        3789 :     argsman.AddArg("-datadir=<dir>", "Specify data directory", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
     581        3789 :     argsman.AddArg("-dbbatchsize", strprintf("Maximum database write batch size in bytes (default: %u)", nDefaultDbBatchSize), ArgsManager::ALLOW_ANY | ArgsManager::DEBUG_ONLY, OptionsCategory::OPTIONS);
     582        3789 :     argsman.AddArg("-dbcache=<n>", strprintf("Maximum database cache size <n> MiB (%d to %d, default: %d). In addition, unused mempool memory is shared for this cache (see -maxmempool).", nMinDbCache, nMaxDbCache, nDefaultDbCache), ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
     583        3789 :     argsman.AddArg("-includeconf=<file>", "Specify additional configuration file, relative to the -datadir path (only useable from configuration file, not command line)", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
     584        3789 :     argsman.AddArg("-loadblock=<file>", "Imports blocks from external file on startup", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
     585        3789 :     argsman.AddArg("-maxmempool=<n>", strprintf("Keep the transaction memory pool below <n> megabytes (default: %u)", DEFAULT_MAX_MEMPOOL_SIZE), ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
     586        3789 :     argsman.AddArg("-maxorphantxsize=<n>", strprintf("Maximum total size of all orphan transactions in megabytes (default: %u)", DEFAULT_MAX_ORPHAN_TRANSACTIONS_SIZE), ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
     587        3789 :     argsman.AddArg("-maxrecsigsage=<n>", strprintf("Number of seconds to keep LLMQ recovery sigs (default: %u)", llmq::DEFAULT_MAX_RECOVERED_SIGS_AGE), ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
     588        3789 :     argsman.AddArg("-mempoolexpiry=<n>", strprintf("Do not keep transactions in the mempool longer than <n> hours (default: %u)", DEFAULT_MEMPOOL_EXPIRY), ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
     589        3789 :     argsman.AddArg("-minimumchainwork=<hex>", strprintf("Minimum work assumed to exist on a valid chain in hex (default: %s, testnet: %s, devnet: %s)", defaultChainParams->GetConsensus().nMinimumChainWork.GetHex(), testnetChainParams->GetConsensus().nMinimumChainWork.GetHex(), devnetChainParams->GetConsensus().nMinimumChainWork.GetHex()), ArgsManager::ALLOW_ANY | ArgsManager::DEBUG_ONLY, OptionsCategory::OPTIONS);
     590        7578 :     argsman.AddArg("-par=<n>", strprintf("Set the number of script verification threads (%u to %d, 0 = auto, <0 = leave that many cores free, default: %d)",
     591        3789 :         -GetNumCores(), MAX_SCRIPTCHECK_THREADS, DEFAULT_SCRIPTCHECK_THREADS), ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
     592        7578 :     argsman.AddArg("-parbls=<n>", strprintf("Set the number of BLS verification threads (%u to %d, 0 = auto, <0 = leave that many cores free, default: %d)",
     593        3789 :         -GetNumCores(), llmq::MAX_BLSCHECK_THREADS, llmq::DEFAULT_BLSCHECK_THREADS), ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
     594        3789 :     argsman.AddArg("-persistmempool", strprintf("Whether to save the mempool on shutdown and load on restart (default: %u)", DEFAULT_PERSIST_MEMPOOL), ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
     595        3789 :     argsman.AddArg("-pid=<file>", strprintf("Specify pid file. Relative paths will be prefixed by a net-specific datadir location. (default: %s)", BITCOIN_PID_FILENAME), ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
     596        3789 :     argsman.AddArg("-prune=<n>", strprintf("Reduce storage requirements by enabling pruning (deleting) of old blocks. This allows the pruneblockchain RPC to be called to delete specific blocks, and enables automatic pruning of old blocks if a target size in MiB is provided. This mode is incompatible with -txindex, -addressindex, -spentindex, -rescan and -disablegovernance=false. "
     597             :             "Warning: Reverting this setting requires re-downloading the entire blockchain. "
     598        3789 :             "(default: 0 = disable pruning blocks, 1 = allow manual pruning via RPC, >%u = automatically prune block files to stay under the specified target size in MiB)", MIN_DISK_SPACE_FOR_BLOCK_FILES / 1024 / 1024), ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
     599        3789 :     argsman.AddArg("-settings=<file>", strprintf("Specify path to dynamic settings data file. Can be disabled with -nosettings. File is written at runtime and not meant to be edited by users (use %s instead for custom settings). Relative paths will be prefixed by datadir location. (default: %s)", BITCOIN_CONF_FILENAME, BITCOIN_SETTINGS_FILENAME), ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
     600        3789 :     argsman.AddArg("-syncmempool", strprintf("Sync mempool from other nodes on start (default: %u)", DEFAULT_SYNC_MEMPOOL), ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
     601             : #if HAVE_SYSTEM
     602        3789 :     argsman.AddArg("-startupnotify=<cmd>", "Execute command on startup.", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
     603        3789 :     argsman.AddArg("-shutdownnotify=<cmd>", "Execute command immediately before beginning shutdown. The need for shutdown may be urgent, so be careful not to delay it long (if the command doesn't require interaction with the server, consider having it fork into the background).", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
     604             : #endif
     605        3789 :     argsman.AddArg("-version", "Print version and exit", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
     606             : 
     607        3789 :     argsman.AddArg("-addressindex", strprintf("Maintain a full address index, used to query for the balance, txids and unspent outputs for addresses (default: %u)", DEFAULT_ADDRESSINDEX), ArgsManager::ALLOW_ANY, OptionsCategory::INDEXING);
     608        3789 :     argsman.AddArg("-reindex", "Rebuild chain state and block index from the blk*.dat files on disk. This will also rebuild active optional indexes.", ArgsManager::ALLOW_ANY, OptionsCategory::INDEXING);
     609        3789 :     argsman.AddArg("-reindex-chainstate", "Rebuild chain state from the currently indexed blocks. When in pruning mode or if blocks on disk might be corrupted, use full -reindex instead. Deactivate all optional indexes before running this.", ArgsManager::ALLOW_ANY, OptionsCategory::INDEXING);
     610        3789 :     argsman.AddArg("-spentindex", strprintf("Maintain a full spent index, used to query the spending txid and input index for an outpoint (default: %u)", DEFAULT_SPENTINDEX), ArgsManager::ALLOW_ANY, OptionsCategory::INDEXING);
     611        3789 :     argsman.AddArg("-timestampindex", strprintf("Maintain a timestamp index for block hashes, used to query blocks hashes by a range of timestamps (default: %u)", DEFAULT_TIMESTAMPINDEX), ArgsManager::ALLOW_ANY, OptionsCategory::INDEXING);
     612        3789 :     argsman.AddArg("-txindex", strprintf("Maintain a full transaction index, used by the getrawtransaction rpc call (default: %u)", DEFAULT_TXINDEX), ArgsManager::ALLOW_ANY, OptionsCategory::INDEXING);
     613        7578 :     argsman.AddArg("-blockfilterindex=<type>",
     614        3789 :                  strprintf("Maintain an index of compact filters by block (default: %s, values: %s).", DEFAULT_BLOCKFILTERINDEX, ListBlockFilterTypes()) +
     615        3789 :                  " If <type> is not supplied or if <type> = 1, indexes for all known types are enabled." +
     616             :                  " Automatically enabled for masternodes with value 'basic'.",
     617        3789 :                  ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
     618             : 
     619        3789 :     argsman.AddArg("-asmap=<file>", strprintf("Specify asn mapping used for bucketing of the peers (default: %s). Relative paths will be prefixed by the net-specific datadir location.", DEFAULT_ASMAP_FILENAME), ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION);
     620        3789 :     argsman.AddArg("-addnode=<ip>", strprintf("Add a node to connect to and attempt to keep the connection open (see the addnode RPC help for more info). This option can be specified multiple times to add multiple nodes; connections are limited to %u at a time and are counted separately from the -maxconnections limit.", MAX_ADDNODE_CONNECTIONS), ArgsManager::ALLOW_ANY | ArgsManager::NETWORK_ONLY, OptionsCategory::CONNECTION);
     621        3789 :     argsman.AddArg("-allowprivatenet", strprintf("Allow RFC1918 addresses to be relayed and connected to (default: %u)", DEFAULT_ALLOWPRIVATENET), ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION);
     622        3789 :     argsman.AddArg("-bantime=<n>", strprintf("Default duration (in seconds) of manually configured bans (default: %u)", DEFAULT_MISBEHAVING_BANTIME), ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION);
     623        3789 :     argsman.AddArg("-bind=<addr>[:<port>][=onion]", strprintf("Bind to given address and always listen on it (default: 0.0.0.0). Use [host]:port notation for IPv6. Append =onion to tag any incoming connections to that address and port as incoming Tor connections (default: 127.0.0.1:%u=onion, testnet: 127.0.0.1:%u=onion, devnet: 127.0.0.1:%u=onion, regtest: 127.0.0.1:%u=onion)", defaultBaseParams->OnionServiceTargetPort(), testnetBaseParams->OnionServiceTargetPort(), devnetBaseParams->OnionServiceTargetPort(), regtestBaseParams->OnionServiceTargetPort()), ArgsManager::ALLOW_ANY | ArgsManager::NETWORK_ONLY, OptionsCategory::CONNECTION);
     624        3789 :     argsman.AddArg("-cjdnsreachable", "If set, then this host is configured for CJDNS (connecting to fc00::/8 addresses would lead us to the CJDNS network, see doc/cjdns.md) (default: 0)", ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION);
     625        3789 :     argsman.AddArg("-connect=<ip>", "Connect only to the specified node; -noconnect disables automatic connections (the rules for this peer are the same as for -addnode). This option can be specified multiple times to connect to multiple nodes.", ArgsManager::ALLOW_ANY | ArgsManager::NETWORK_ONLY, OptionsCategory::CONNECTION);
     626        3789 :     argsman.AddArg("-discover", "Discover own IP addresses (default: 1 when listening and no -externalip or -proxy)", ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION);
     627        3789 :     argsman.AddArg("-dns", strprintf("Allow DNS lookups for -addnode, -seednode and -connect (default: %u)", DEFAULT_NAME_LOOKUP), ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION);
     628        3789 :     argsman.AddArg("-dnsseed", strprintf("Query for peer addresses via DNS lookup, if low on addresses (default: %u unless -connect used or -maxconnections=0)", DEFAULT_DNSSEED), ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION);
     629        3789 :     argsman.AddArg("-externalip=<ip>", "Specify your own public address", ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION);
     630        3789 :     argsman.AddArg("-fixedseeds", strprintf("Allow fixed seeds if DNS seeds don't provide peers (default: %u)", DEFAULT_FIXEDSEEDS), ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION);
     631        3789 :     argsman.AddArg("-forcednsseed", strprintf("Always query for peer addresses via DNS lookup (default: %u)", DEFAULT_FORCEDNSSEED), ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION);
     632        3789 :     argsman.AddArg("-listen", strprintf("Accept connections from outside (default: %u if no -proxy, -connect or -maxconnections=0)", DEFAULT_LISTEN), ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION);
     633        3789 :     argsman.AddArg("-listenonion", strprintf("Automatically create Tor onion service (default: %d)", DEFAULT_LISTEN_ONION), ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION);
     634        3789 :     argsman.AddArg("-maxconnections=<n>", strprintf("Maintain at most <n> connections to peers (temporary service connections excluded) (default: %u). This limit does not apply to connections manually added via -addnode or the addnode RPC, which have a separate limit of %u.", DEFAULT_MAX_PEER_CONNECTIONS, MAX_ADDNODE_CONNECTIONS), ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION);
     635        3789 :     argsman.AddArg("-maxreceivebuffer=<n>", strprintf("Maximum per-connection receive buffer, <n>*1000 bytes (default: %u)", DEFAULT_MAXRECEIVEBUFFER), ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION);
     636        3789 :     argsman.AddArg("-maxsendbuffer=<n>", strprintf("Maximum per-connection memory usage for the send buffer, <n>*1000 bytes (default: %u)", DEFAULT_MAXSENDBUFFER), ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION);
     637        3789 :     argsman.AddArg("-maxtimeadjustment", strprintf("Maximum allowed median peer time offset adjustment. Local perspective of time may be influenced by outbound peers forward or backward by this amount (default: %u seconds).", DEFAULT_MAX_TIME_ADJUSTMENT), ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION);
     638        3789 :     argsman.AddArg("-maxuploadtarget=<n>", strprintf("Tries to keep outbound traffic under the given target per 24h. Limit does not apply to peers with 'download' permission or blocks created within past week. 0 = no limit (default: %s). Optional suffix units [k|K|m|M|g|G|t|T] (default: M). Lowercase is 1000 base while uppercase is 1024 base", DEFAULT_MAX_UPLOAD_TARGET), ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION);
     639             : #if HAVE_SOCKADDR_UN
     640        3789 :     argsman.AddArg("-onion=<ip:port|path>", "Use separate SOCKS5 proxy to reach peers via Tor onion services, set -noonion to disable (default: -proxy). May be a local file path prefixed with 'unix:'.", ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION);
     641             : #else
     642             :     argsman.AddArg("-onion=<ip:port>", "Use separate SOCKS5 proxy to reach peers via Tor onion services, set -noonion to disable (default: -proxy)", ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION);
     643             : #endif
     644        3789 :     argsman.AddArg("-i2psam=<ip:port>", "I2P SAM proxy to reach I2P peers and accept I2P connections (default: none)", ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION);
     645        3789 :     argsman.AddArg("-i2pacceptincoming", strprintf("Whether to accept inbound I2P connections (default: %i). Ignored if -i2psam is not set. Listening for inbound I2P connections is done through the SAM proxy, not by binding to a local address and port.", DEFAULT_I2P_ACCEPT_INCOMING), ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION);
     646        3789 :     argsman.AddArg("-onlynet=<net>", "Make automatic outbound connections only to network <net> (" + Join(GetNetworkNames(), ", ") + "). Inbound and manual connections are not affected by this option. It can be specified multiple times to allow multiple networks.", ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION);
     647        3789 :     argsman.AddArg("-v2transport", strprintf("Support v2 transport (default: %u)", DEFAULT_V2_TRANSPORT), ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION);
     648        3789 :     argsman.AddArg("-peerblockfilters", strprintf("Serve compact block filters to peers per BIP 157 (default: %u, automatically enabled for masternodes)", DEFAULT_PEERBLOCKFILTERS), ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION);
     649        3789 :     argsman.AddArg("-peerbloomfilters", strprintf("Support filtering of blocks and transaction with bloom filters (default: %u)", DEFAULT_PEERBLOOMFILTERS), ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION);
     650        3789 :     argsman.AddArg("-txreconciliation", strprintf("Enable transaction reconciliations per BIP 330 (default: %d)", DEFAULT_TXRECONCILIATION_ENABLE), ArgsManager::ALLOW_ANY | ArgsManager::DEBUG_ONLY, OptionsCategory::CONNECTION);
     651        3789 :     argsman.AddArg("-peertimeout=<n>", strprintf("Specify a p2p connection timeout delay in seconds. After connecting to a peer, wait this amount of time before considering disconnection based on inactivity (minimum: 1, default: %d)", DEFAULT_PEER_CONNECT_TIMEOUT), ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION);
     652             :     // TODO: remove the sentence "Nodes not using ... incoming connections." once the changes from
     653             :     // https://github.com/bitcoin/bitcoin/pull/23542 have become widespread.
     654        3789 :     argsman.AddArg("-port=<port>", strprintf("Listen for connections on <port>. Nodes not using the default ports (default: %u, testnet: %u, devnet: %u, regtest: %u) are unlikely to get incoming connections. Not relevant for I2P (see doc/i2p.md).", defaultChainParams->GetDefaultPort(), testnetChainParams->GetDefaultPort(), devnetChainParams->GetDefaultPort(), regtestChainParams->GetDefaultPort()), ArgsManager::ALLOW_ANY | ArgsManager::NETWORK_ONLY, OptionsCategory::CONNECTION);
     655             : #if HAVE_SOCKADDR_UN
     656        3789 :     argsman.AddArg("-proxy=<ip:port|path>", "Connect through SOCKS5 proxy, set -noproxy to disable (default: disabled). May be a local file path prefixed with 'unix:' if the proxy supports it.", ArgsManager::ALLOW_ANY | ArgsManager::DISALLOW_ELISION, OptionsCategory::CONNECTION);
     657             : #else
     658             :     argsman.AddArg("-proxy=<ip:port>", "Connect through SOCKS5 proxy, set -noproxy to disable (default: disabled)", ArgsManager::ALLOW_ANY | ArgsManager::DISALLOW_ELISION, OptionsCategory::CONNECTION);
     659             : #endif
     660        3789 :     argsman.AddArg("-proxyrandomize", strprintf("Randomize credentials for every proxy connection. This enables Tor stream isolation (default: %u)", DEFAULT_PROXYRANDOMIZE), ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION);
     661        3789 :     argsman.AddArg("-seednode=<ip>", "Connect to a node to retrieve peer addresses, and disconnect. This option can be specified multiple times to connect to multiple nodes.", ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION);
     662        3789 :     argsman.AddArg("-socketevents=<mode>", "Socket events mode, which must be one of 'select', 'poll', 'epoll' or 'kqueue', depending on your system (default: Linux - 'epoll', FreeBSD/Apple - 'kqueue', Windows - 'select')", ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION);
     663        3789 :     argsman.AddArg("-networkactive", "Enable all P2P network activity (default: 1). Can be changed by the setnetworkactive RPC command", ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION);
     664        3789 :     argsman.AddArg("-timeout=<n>", strprintf("Specify socket connection timeout in milliseconds. If an initial attempt to connect is unsuccessful after this amount of time, drop it (minimum: 1, default: %d)", DEFAULT_CONNECT_TIMEOUT), ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION);
     665        3789 :     argsman.AddArg("-torcontrol=<ip>:<port>", strprintf("Tor control host and port to use if onion listening enabled (default: %s). If no port is specified, the default port of %i will be used.", DEFAULT_TOR_CONTROL, DEFAULT_TOR_CONTROL_PORT), ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION);
     666        3789 :     argsman.AddArg("-torpassword=<pass>", "Tor control port password (default: empty)", ArgsManager::ALLOW_ANY | ArgsManager::SENSITIVE, OptionsCategory::CONNECTION);
     667             : #ifdef USE_UPNP
     668             :     argsman.AddArg("-upnp", strprintf("Use UPnP to map the listening port (default: %u)", DEFAULT_UPNP), ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION);
     669             : #else
     670        3789 :     hidden_args.emplace_back("-upnp");
     671             : #endif
     672             : #ifdef USE_NATPMP
     673             :     argsman.AddArg("-natpmp", strprintf("Use NAT-PMP to map the listening port (default: %u)", DEFAULT_NATPMP), ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION);
     674             : #else
     675        3789 :     hidden_args.emplace_back("-natpmp");
     676             : #endif // USE_NATPMP
     677        7578 :     argsman.AddArg("-whitebind=<[permissions@]addr>", "Bind to the given address and add permission flags to the peers connecting to it. "
     678        3789 :         "Use [host]:port notation for IPv6. Allowed permissions: " + Join(NET_PERMISSIONS_DOC, ", ") + ". "
     679        3789 :         "Specify multiple permissions separated by commas (default: download,noban,mempool,relay). Can be specified multiple times.", ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION);
     680             : 
     681        3789 :     argsman.AddArg("-whitelist=<[permissions@]IP address or network>", "Add permission flags to the peers connecting from the given IP address (e.g. 1.2.3.4) or "
     682             :         "CIDR-notated network (e.g. 1.2.3.0/24). Uses the same permissions as "
     683        3789 :         "-whitebind. Can be specified multiple times." , ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION);
     684             : 
     685        3789 :     g_wallet_init_interface.AddWalletOptions(argsman);
     686             : 
     687             : #if ENABLE_ZMQ
     688             :     argsman.AddArg("-zmqpubhashblock=<address>", "Enable publish hash block in <address>", ArgsManager::ALLOW_ANY, OptionsCategory::ZMQ);
     689             :     argsman.AddArg("-zmqpubhashchainlock=<address>", "Enable publish hash block (locked via ChainLocks) in <address>", ArgsManager::ALLOW_ANY, OptionsCategory::ZMQ);
     690             :     argsman.AddArg("-zmqpubhashgovernanceobject=<address>", "Enable publish hash of governance objects (like proposals) in <address>", ArgsManager::ALLOW_ANY, OptionsCategory::ZMQ);
     691             :     argsman.AddArg("-zmqpubhashgovernancevote=<address>", "Enable publish hash of governance votes in <address>", ArgsManager::ALLOW_ANY, OptionsCategory::ZMQ);
     692             :     argsman.AddArg("-zmqpubhashinstantsenddoublespend=<address>", "Enable publish transaction hashes of attempted InstantSend double spend in <address>", ArgsManager::ALLOW_ANY, OptionsCategory::ZMQ);
     693             :     argsman.AddArg("-zmqpubhashrecoveredsig=<address>", "Enable publish message hash of recovered signatures (recovered by LLMQs) in <address>", ArgsManager::ALLOW_ANY, OptionsCategory::ZMQ);
     694             :     argsman.AddArg("-zmqpubhashtx=<address>", "Enable publish hash transaction in <address>", ArgsManager::ALLOW_ANY, OptionsCategory::ZMQ);
     695             :     argsman.AddArg("-zmqpubhashtxlock=<address>", "Enable publish hash transaction (locked via InstantSend) in <address>", ArgsManager::ALLOW_ANY, OptionsCategory::ZMQ);
     696             :     argsman.AddArg("-zmqpubrawblock=<address>", "Enable publish raw block in <address>", ArgsManager::ALLOW_ANY, OptionsCategory::ZMQ);
     697             :     argsman.AddArg("-zmqpubrawchainlock=<address>", "Enable publish raw block (locked via ChainLocks) in <address>", ArgsManager::ALLOW_ANY, OptionsCategory::ZMQ);
     698             :     argsman.AddArg("-zmqpubrawchainlocksig=<address>", "Enable publish raw block (locked via ChainLocks) and CLSIG message in <address>", ArgsManager::ALLOW_ANY, OptionsCategory::ZMQ);
     699             :     argsman.AddArg("-zmqpubrawgovernancevote=<address>", "Enable publish raw governance objects (like proposals) in <address>", ArgsManager::ALLOW_ANY, OptionsCategory::ZMQ);
     700             :     argsman.AddArg("-zmqpubrawgovernanceobject=<address>", "Enable publish raw governance votes in <address>", ArgsManager::ALLOW_ANY, OptionsCategory::ZMQ);
     701             :     argsman.AddArg("-zmqpubrawinstantsenddoublespend=<address>", "Enable publish raw transactions of attempted InstantSend double spend in <address>", ArgsManager::ALLOW_ANY, OptionsCategory::ZMQ);
     702             :     argsman.AddArg("-zmqpubrawrecoveredsig=<address>", "Enable publish raw recovered signatures (recovered by LLMQs) in <address>", ArgsManager::ALLOW_ANY, OptionsCategory::ZMQ);
     703             :     argsman.AddArg("-zmqpubrawtx=<address>", "Enable publish raw transaction in <address>", ArgsManager::ALLOW_ANY, OptionsCategory::ZMQ);
     704             :     argsman.AddArg("-zmqpubrawtxlock=<address>", "Enable publish raw transaction (locked via InstantSend) in <address>", ArgsManager::ALLOW_ANY, OptionsCategory::ZMQ);
     705             :     argsman.AddArg("-zmqpubrawtxlocksig=<address>", "Enable publish raw transaction (locked via InstantSend) and ISLOCK in <address>", ArgsManager::ALLOW_ANY, OptionsCategory::ZMQ);
     706             :     argsman.AddArg("-zmqpubsequence=<address>", "Enable publish hash block and tx sequence in <address>", ArgsManager::ALLOW_ANY, OptionsCategory::ZMQ);
     707             :     argsman.AddArg("-zmqpubhashblockhwm=<n>", strprintf("Set publish hash block outbound message high water mark (default: %d)", CZMQAbstractNotifier::DEFAULT_ZMQ_SNDHWM), ArgsManager::ALLOW_ANY, OptionsCategory::ZMQ);
     708             :     argsman.AddArg("-zmqpubhashchainlockhwm=<n>", strprintf("Set publish hash chain lock outbound message high water mark (default: %d)", CZMQAbstractNotifier::DEFAULT_ZMQ_SNDHWM), ArgsManager::ALLOW_ANY, OptionsCategory::ZMQ);
     709             :     argsman.AddArg("-zmqpubhashgovernanceobjecthwm=<n>", strprintf("Set publish hash governance object outbound message high water mark (default: %d)", CZMQAbstractNotifier::DEFAULT_ZMQ_SNDHWM), ArgsManager::ALLOW_ANY, OptionsCategory::ZMQ);
     710             :     argsman.AddArg("-zmqpubhashgovernancevotehwm=<n>", strprintf("Set publish hash governance vote outbound message high water mark (default: %d)", CZMQAbstractNotifier::DEFAULT_ZMQ_SNDHWM), ArgsManager::ALLOW_ANY, OptionsCategory::ZMQ);
     711             :     argsman.AddArg("-zmqpubhashinstantsenddoublespendhwm=<n>", strprintf("Set publish hash InstantSend double spend outbound message high water mark (default: %d)", CZMQAbstractNotifier::DEFAULT_ZMQ_SNDHWM), ArgsManager::ALLOW_ANY, OptionsCategory::ZMQ);
     712             :     argsman.AddArg("-zmqpubhashrecoveredsighwm=<n>", strprintf("Set publish hash recovered signature outbound message high water mark (default: %d)", CZMQAbstractNotifier::DEFAULT_ZMQ_SNDHWM), ArgsManager::ALLOW_ANY, OptionsCategory::ZMQ);
     713             :     argsman.AddArg("-zmqpubhashtxhwm=<n>", strprintf("Set publish hash transaction outbound message high water mark (default: %d)", CZMQAbstractNotifier::DEFAULT_ZMQ_SNDHWM), ArgsManager::ALLOW_ANY, OptionsCategory::ZMQ);
     714             :     argsman.AddArg("-zmqpubhashtxlockhwm=<n>", strprintf("Set publish hash transaction lock outbound message high water mark (default: %d)", CZMQAbstractNotifier::DEFAULT_ZMQ_SNDHWM), ArgsManager::ALLOW_ANY, OptionsCategory::ZMQ);
     715             :     argsman.AddArg("-zmqpubrawblockhwm=<n>", strprintf("Set publish raw block outbound message high water mark (default: %d)", CZMQAbstractNotifier::DEFAULT_ZMQ_SNDHWM), ArgsManager::ALLOW_ANY, OptionsCategory::ZMQ);
     716             :     argsman.AddArg("-zmqpubrawchainlockhwm=<n>", strprintf("Set publish raw chain lock outbound message high water mark (default: %d)", CZMQAbstractNotifier::DEFAULT_ZMQ_SNDHWM), ArgsManager::ALLOW_ANY, OptionsCategory::ZMQ);
     717             :     argsman.AddArg("-zmqpubrawchainlocksighwm=<n>", strprintf("Set publish raw chain lock signature outbound message high water mark (default: %d)", CZMQAbstractNotifier::DEFAULT_ZMQ_SNDHWM), ArgsManager::ALLOW_ANY, OptionsCategory::ZMQ);
     718             :     argsman.AddArg("-zmqpubrawgovernanceobjecthwm=<n>", strprintf("Set publish raw governance object outbound message high water mark (default: %d)", CZMQAbstractNotifier::DEFAULT_ZMQ_SNDHWM), ArgsManager::ALLOW_ANY, OptionsCategory::ZMQ);
     719             :     argsman.AddArg("-zmqpubrawgovernancevotehwm=<n>", strprintf("Set publish raw governance vote outbound message high water mark (default: %d)", CZMQAbstractNotifier::DEFAULT_ZMQ_SNDHWM), ArgsManager::ALLOW_ANY, OptionsCategory::ZMQ);
     720             :     argsman.AddArg("-zmqpubrawinstantsenddoublespendhwm=<n>", strprintf("Set publish raw InstantSend double spend outbound message high water mark (default: %d)", CZMQAbstractNotifier::DEFAULT_ZMQ_SNDHWM), ArgsManager::ALLOW_ANY, OptionsCategory::ZMQ);
     721             :     argsman.AddArg("-zmqpubrawrecoveredsighwm=<n>", strprintf("Set publish raw recovered signature outbound message high water mark (default: %d)", CZMQAbstractNotifier::DEFAULT_ZMQ_SNDHWM), ArgsManager::ALLOW_ANY, OptionsCategory::ZMQ);
     722             :     argsman.AddArg("-zmqpubrawtxhwm=<n>", strprintf("Set publish raw transaction outbound message high water mark (default: %d)", CZMQAbstractNotifier::DEFAULT_ZMQ_SNDHWM), ArgsManager::ALLOW_ANY, OptionsCategory::ZMQ);
     723             :     argsman.AddArg("-zmqpubrawtxlockhwm=<n>", strprintf("Set publish raw transaction lock outbound message high water mark (default: %d)", CZMQAbstractNotifier::DEFAULT_ZMQ_SNDHWM), ArgsManager::ALLOW_ANY, OptionsCategory::ZMQ);
     724             :     argsman.AddArg("-zmqpubrawtxlocksighwm=<n>", strprintf("Set publish raw transaction lock signature outbound message high water mark (default: %d)", CZMQAbstractNotifier::DEFAULT_ZMQ_SNDHWM), ArgsManager::ALLOW_ANY, OptionsCategory::ZMQ);
     725             :     argsman.AddArg("-zmqpubsequencehwm=<n>", strprintf("Set publish hash sequence message high water mark (default: %d)", CZMQAbstractNotifier::DEFAULT_ZMQ_SNDHWM), ArgsManager::ALLOW_ANY, OptionsCategory::ZMQ);
     726             : #else
     727        3789 :     hidden_args.emplace_back("-zmqpubhashblock=<address>");
     728        3789 :     hidden_args.emplace_back("-zmqpubhashchainlock=<address>");
     729        3789 :     hidden_args.emplace_back("-zmqpubhashgovernanceobject=<address>");
     730        3789 :     hidden_args.emplace_back("-zmqpubhashgovernancevote=<address>");
     731        3789 :     hidden_args.emplace_back("-zmqpubhashinstantsenddoublespend=<address>");
     732        3789 :     hidden_args.emplace_back("-zmqpubhashrecoveredsig=<address>");
     733        3789 :     hidden_args.emplace_back("-zmqpubhashtx=<address>");
     734        3789 :     hidden_args.emplace_back("-zmqpubhashtxlock=<address>");
     735        3789 :     hidden_args.emplace_back("-zmqpubrawblock=<address>");
     736        3789 :     hidden_args.emplace_back("-zmqpubrawchainlock=<address>");
     737        3789 :     hidden_args.emplace_back("-zmqpubrawchainlocksig=<address>");
     738        3789 :     hidden_args.emplace_back("-zmqpubrawgovernancevote=<address>");
     739        3789 :     hidden_args.emplace_back("-zmqpubrawgovernanceobject=<address>");
     740        3789 :     hidden_args.emplace_back("-zmqpubrawinstantsenddoublespend=<address>");
     741        3789 :     hidden_args.emplace_back("-zmqpubrawrecoveredsig=<address>");
     742        3789 :     hidden_args.emplace_back("-zmqpubrawtx=<address>");
     743        3789 :     hidden_args.emplace_back("-zmqpubrawtxlock=<address>");
     744        3789 :     hidden_args.emplace_back("-zmqpubrawtxlocksig=<address>");
     745        3789 :     hidden_args.emplace_back("-zmqpubsequence=<n>");
     746        3789 :     hidden_args.emplace_back("-zmqpubhashblockhwm=<n>");
     747        3789 :     hidden_args.emplace_back("-zmqpubhashchainlockhwm=<n>");
     748        3789 :     hidden_args.emplace_back("-zmqpubhashgovernanceobjecthwm=<n>");
     749        3789 :     hidden_args.emplace_back("-zmqpubhashgovernancevotehwm=<n>");
     750        3789 :     hidden_args.emplace_back("-zmqpubhashinstantsenddoublespendhwm=<n>");
     751        3789 :     hidden_args.emplace_back("-zmqpubhashrecoveredsighwm=<n>");
     752        3789 :     hidden_args.emplace_back("-zmqpubhashtxhwm=<n>");
     753        3789 :     hidden_args.emplace_back("-zmqpubhashtxlockhwm=<n>");
     754        3789 :     hidden_args.emplace_back("-zmqpubrawblockhwm=<n>");
     755        3789 :     hidden_args.emplace_back("-zmqpubrawchainlockhwm=<n>");
     756        3789 :     hidden_args.emplace_back("-zmqpubrawchainlocksighwm=<n>");
     757        3789 :     hidden_args.emplace_back("-zmqpubrawgovernanceobjecthwm=<n>");
     758        3789 :     hidden_args.emplace_back("-zmqpubrawgovernancevotehwm=<n>");
     759        3789 :     hidden_args.emplace_back("-zmqpubrawinstantsenddoublespendhwm=<n>");
     760        3789 :     hidden_args.emplace_back("-zmqpubrawrecoveredsighwm=<n>");
     761        3789 :     hidden_args.emplace_back("-zmqpubrawtxhwm=<n>");
     762        3789 :     hidden_args.emplace_back("-zmqpubrawtxlockhwm=<n>");
     763        3789 :     hidden_args.emplace_back("-zmqpubrawtxlocksighwm=<n>");
     764        3789 :     hidden_args.emplace_back("-zmqpubsequencehwm=<n>");
     765             : #endif
     766             : 
     767        3789 :     argsman.AddArg("-checkblockindex", strprintf("Do a consistency check for the block tree, and  occasionally. (default: %u, regtest: %u)", defaultChainParams->DefaultConsistencyChecks(), regtestChainParams->DefaultConsistencyChecks()), ArgsManager::ALLOW_ANY | ArgsManager::DEBUG_ONLY, OptionsCategory::DEBUG_TEST);
     768        3789 :     argsman.AddArg("-checkblocks=<n>", strprintf("How many blocks to check at startup (default: %u, 0 = all)", DEFAULT_CHECKBLOCKS), ArgsManager::ALLOW_ANY | ArgsManager::DEBUG_ONLY, OptionsCategory::DEBUG_TEST);
     769        3789 :     argsman.AddArg("-checklevel=<n>", strprintf("How thorough the block verification of -checkblocks is: %s (0-4, default: %u)", Join(CHECKLEVEL_DOC, ", "), DEFAULT_CHECKLEVEL), ArgsManager::ALLOW_ANY | ArgsManager::DEBUG_ONLY, OptionsCategory::DEBUG_TEST);
     770        3789 :     argsman.AddArg("-checkaddrman=<n>", strprintf("Run addrman consistency checks every <n> operations. Use 0 to disable. (default: %u)", DEFAULT_ADDRMAN_CONSISTENCY_CHECKS), ArgsManager::ALLOW_ANY | ArgsManager::DEBUG_ONLY, OptionsCategory::DEBUG_TEST);
     771        3789 :     argsman.AddArg("-checkmempool=<n>", strprintf("Run mempool consistency checks every <n> transactions. Use 0 to disable. (default: %u, regtest: %u)", defaultChainParams->DefaultConsistencyChecks(), regtestChainParams->DefaultConsistencyChecks()), ArgsManager::ALLOW_ANY | ArgsManager::DEBUG_ONLY, OptionsCategory::DEBUG_TEST);
     772        3789 :     argsman.AddArg("-checkpoints", strprintf("Enable rejection of any forks from the known historical chain until block %s (default: %u)", defaultChainParams->Checkpoints().GetHeight(), DEFAULT_CHECKPOINTS_ENABLED), ArgsManager::ALLOW_ANY | ArgsManager::DEBUG_ONLY, OptionsCategory::DEBUG_TEST);
     773        3789 :     argsman.AddArg("-deprecatedrpc=<method>", "Allows deprecated RPC method(s) to be used", ArgsManager::ALLOW_ANY | ArgsManager::DEBUG_ONLY, OptionsCategory::DEBUG_TEST);
     774        3789 :     argsman.AddArg("-forceevodbrepair", "Force evodb masternode list diff verification and repair on startup, even if already repaired (default: 0)", ArgsManager::ALLOW_ANY | ArgsManager::DEBUG_ONLY, OptionsCategory::DEBUG_TEST);
     775        3789 :     argsman.AddArg("-limitancestorcount=<n>", strprintf("Do not accept transactions if number of in-mempool ancestors is <n> or more (default: %u)", DEFAULT_ANCESTOR_LIMIT), ArgsManager::ALLOW_ANY | ArgsManager::DEBUG_ONLY, OptionsCategory::DEBUG_TEST);
     776        3789 :     argsman.AddArg("-limitancestorsize=<n>", strprintf("Do not accept transactions whose size with all in-mempool ancestors exceeds <n> kilobytes (default: %u)", DEFAULT_ANCESTOR_SIZE_LIMIT), ArgsManager::ALLOW_ANY | ArgsManager::DEBUG_ONLY, OptionsCategory::DEBUG_TEST);
     777        3789 :     argsman.AddArg("-limitdescendantcount=<n>", strprintf("Do not accept transactions if any ancestor would have <n> or more in-mempool descendants (default: %u)", DEFAULT_DESCENDANT_LIMIT), ArgsManager::ALLOW_ANY | ArgsManager::DEBUG_ONLY, OptionsCategory::DEBUG_TEST);
     778        3789 :     argsman.AddArg("-limitdescendantsize=<n>", strprintf("Do not accept transactions if any ancestor would have more than <n> kilobytes of in-mempool descendants (default: %u).", DEFAULT_DESCENDANT_SIZE_LIMIT), ArgsManager::ALLOW_ANY | ArgsManager::DEBUG_ONLY, OptionsCategory::DEBUG_TEST);
     779        3789 :     argsman.AddArg("-stopafterblockimport", strprintf("Stop running after importing blocks from disk (default: %u)", DEFAULT_STOPAFTERBLOCKIMPORT), ArgsManager::ALLOW_ANY | ArgsManager::DEBUG_ONLY, OptionsCategory::DEBUG_TEST);
     780        3789 :     argsman.AddArg("-stopatheight", strprintf("Stop running after reaching the given height in the main chain (default: %u)", DEFAULT_STOPATHEIGHT), ArgsManager::ALLOW_ANY | ArgsManager::DEBUG_ONLY, OptionsCategory::DEBUG_TEST);
     781        3789 :     argsman.AddArg("-watchquorums=<n>", strprintf("Watch and validate quorum communication (default: %u)", llmq::DEFAULT_WATCH_QUORUMS), ArgsManager::ALLOW_ANY | ArgsManager::DEBUG_ONLY, OptionsCategory::DEBUG_TEST);
     782        3789 :     argsman.AddArg("-capturemessages", "Capture all P2P messages to disk", ArgsManager::ALLOW_ANY | ArgsManager::DEBUG_ONLY, OptionsCategory::DEBUG_TEST);
     783        3789 :     argsman.AddArg("-disablegovernance", strprintf("Disable governance validation (0-1, default: %u)", 0), ArgsManager::ALLOW_ANY, OptionsCategory::DEBUG_TEST);
     784        3789 :     argsman.AddArg("-maxsigcachesize=<n>", strprintf("Limit sum of signature cache and script execution cache sizes to <n> MiB (default: %u)", DEFAULT_MAX_SIG_CACHE_SIZE), ArgsManager::ALLOW_ANY | ArgsManager::DEBUG_ONLY, OptionsCategory::DEBUG_TEST);
     785        3789 :     argsman.AddArg("-maxtipage=<n>", strprintf("Maximum tip age in seconds to consider node in initial block download (default: %u)", DEFAULT_MAX_TIP_AGE), ArgsManager::ALLOW_ANY | ArgsManager::DEBUG_ONLY, OptionsCategory::DEBUG_TEST);
     786        3789 :     argsman.AddArg("-mocktime=<n>", "Replace actual time with " + UNIX_EPOCH_TIME + " (default: 0)", ArgsManager::ALLOW_ANY | ArgsManager::DEBUG_ONLY, OptionsCategory::DEBUG_TEST);
     787        3789 :     argsman.AddArg("-minsporkkeys=<n>", "Overrides minimum spork signers to change spork value. Only useful for regtest and devnet. Using this on mainnet or testnet will ban you.", ArgsManager::ALLOW_ANY, OptionsCategory::DEBUG_TEST);
     788        3789 :     argsman.AddArg("-printpriority", strprintf("Log transaction fee per kB when mining blocks (default: %u)", DEFAULT_PRINTPRIORITY), ArgsManager::ALLOW_ANY | ArgsManager::DEBUG_ONLY, OptionsCategory::DEBUG_TEST);
     789        3789 :     argsman.AddArg("-pushversion", "Protocol version to report to other nodes", ArgsManager::ALLOW_ANY, OptionsCategory::DEBUG_TEST);
     790        3789 :     argsman.AddArg("-sporkaddr=<dashaddress>", "Override spork address. Only useful for regtest and devnet. Using this on mainnet or testnet will ban you.", ArgsManager::ALLOW_ANY, OptionsCategory::DEBUG_TEST);
     791        3789 :     argsman.AddArg("-sporkkey=<privatekey>", "Set the private key to be used for signing spork messages.", ArgsManager::ALLOW_ANY | ArgsManager::SENSITIVE, OptionsCategory::DEBUG_TEST);
     792        3789 :     argsman.AddArg("-uacomment=<cmt>", "Append comment to the user agent string", ArgsManager::ALLOW_ANY, OptionsCategory::DEBUG_TEST);
     793             : 
     794        3789 :     SetupChainParamsOptions(argsman);
     795             : 
     796        3789 :     argsman.AddArg("-llmq-data-recovery=<n>", strprintf("Enable automated quorum data recovery (default: %u)", llmq::DEFAULT_ENABLE_QUORUM_DATA_RECOVERY), ArgsManager::ALLOW_ANY, OptionsCategory::MASTERNODE);
     797        3789 :     argsman.AddArg("-llmq-qvvec-sync=<quorum_name>:<mode>", strprintf("Defines from which LLMQ type the masternode should sync quorum verification vectors. Can be used multiple times with different LLMQ types. <mode>: %d (sync always from all quorums of the type defined by <quorum_name>), %d (sync from all quorums of the type defined by <quorum_name> if a member of any of the quorums)", (int32_t)llmq::QvvecSyncMode::Always, (int32_t)llmq::QvvecSyncMode::OnlyIfTypeMember), ArgsManager::ALLOW_ANY, OptionsCategory::MASTERNODE);
     798        3789 :     argsman.AddArg("-masternodeblsprivkey=<hex>", "Set the masternode BLS private key and enable the client to act as a masternode", ArgsManager::ALLOW_ANY | ArgsManager::SENSITIVE, OptionsCategory::MASTERNODE);
     799        3789 :     argsman.AddArg("-deprecated-platform-user=<user>", "Set the username for the \"platform user\", a restricted user intended to be used by Dash Platform, to the specified username.", ArgsManager::ALLOW_ANY, OptionsCategory::MASTERNODE);
     800             : 
     801        3789 :     argsman.AddArg("-acceptnonstdtxn", strprintf("Relay and mine \"non-standard\" transactions (%sdefault: %u)", "testnet/regtest only; ", !testnetChainParams->RequireStandard()), ArgsManager::ALLOW_ANY | ArgsManager::DEBUG_ONLY, OptionsCategory::NODE_RELAY);
     802        3789 :     argsman.AddArg("-dustrelayfee=<amt>", strprintf("Fee rate (in %s/kB) used to define dust, the value of an output such that it will cost more than its value in fees at this fee rate to spend it. (default: %s)", CURRENCY_UNIT, FormatMoney(DUST_RELAY_TX_FEE)), ArgsManager::ALLOW_ANY | ArgsManager::DEBUG_ONLY, OptionsCategory::NODE_RELAY);
     803        3789 :     argsman.AddArg("-incrementalrelayfee=<amt>", strprintf("Fee rate (in %s/kB) used to define cost of relay, used for mempool limiting. (default: %s)", CURRENCY_UNIT, FormatMoney(DEFAULT_INCREMENTAL_RELAY_FEE)), ArgsManager::ALLOW_ANY | ArgsManager::DEBUG_ONLY, OptionsCategory::NODE_RELAY);
     804        3789 :     argsman.AddArg("-bytespersigop", strprintf("Equivalent bytes per sigop in transactions for relay and mining (default: %u)", DEFAULT_BYTES_PER_SIGOP), ArgsManager::ALLOW_ANY, OptionsCategory::NODE_RELAY);
     805        3789 :     argsman.AddArg("-datacarrier", strprintf("Relay and mine data carrier transactions (default: %u)", DEFAULT_ACCEPT_DATACARRIER), ArgsManager::ALLOW_ANY, OptionsCategory::NODE_RELAY);
     806        7578 :     argsman.AddArg("-datacarriersize",
     807        3789 :                    strprintf("Relay and mine transactions whose data-carrying raw scriptPubKey "
     808             :                              "is of this size or less (default: %u)",
     809             :                              MAX_OP_RETURN_RELAY),
     810        3789 :                    ArgsManager::ALLOW_ANY, OptionsCategory::NODE_RELAY);
     811        3789 :     argsman.AddArg("-permitbaremultisig", strprintf("Relay non-P2SH multisig (default: %u)", DEFAULT_PERMIT_BAREMULTISIG), ArgsManager::ALLOW_ANY,
     812        3789 :                    OptionsCategory::NODE_RELAY);
     813        7578 :     argsman.AddArg("-minrelaytxfee=<amt>", strprintf("Fees (in %s/kB) smaller than this are considered zero fee for relaying, mining and transaction creation (default: %s)",
     814        7578 :         CURRENCY_UNIT, FormatMoney(DEFAULT_MIN_RELAY_TX_FEE)), ArgsManager::ALLOW_ANY, OptionsCategory::NODE_RELAY);
     815        3789 :     argsman.AddArg("-whitelistforcerelay", strprintf("Add 'forcerelay' permission to whitelisted inbound peers with default permissions. This will relay transactions even if the transactions were already in the mempool. (default: %d)", DEFAULT_WHITELISTFORCERELAY), ArgsManager::ALLOW_ANY, OptionsCategory::NODE_RELAY);
     816        3789 :     argsman.AddArg("-whitelistrelay", strprintf("Add 'relay' permission to whitelisted inbound peers with default permissions. This will accept relayed transactions even when not relaying transactions (default: %d)", DEFAULT_WHITELISTRELAY), ArgsManager::ALLOW_ANY, OptionsCategory::NODE_RELAY);
     817             : 
     818        3789 :     argsman.AddArg("-blockmaxsize=<n>", strprintf("Set maximum block size in bytes (default: %d)", DEFAULT_BLOCK_MAX_SIZE), ArgsManager::ALLOW_ANY, OptionsCategory::BLOCK_CREATION);
     819        3789 :     argsman.AddArg("-blockmintxfee=<amt>", strprintf("Set lowest fee rate (in %s/kB) for transactions to be included in block creation. (default: %s)", CURRENCY_UNIT, FormatMoney(DEFAULT_BLOCK_MIN_TX_FEE)), ArgsManager::ALLOW_ANY, OptionsCategory::BLOCK_CREATION);
     820        3789 :     argsman.AddArg("-blockversion=<n>", "Override block version to test forking scenarios", ArgsManager::ALLOW_ANY | ArgsManager::DEBUG_ONLY, OptionsCategory::BLOCK_CREATION);
     821             : 
     822        3789 :     argsman.AddArg("-rest", strprintf("Accept public REST requests (default: %u)", DEFAULT_REST_ENABLE), ArgsManager::ALLOW_ANY, OptionsCategory::RPC);
     823        3789 :     argsman.AddArg("-rpcallowip=<ip>", "Allow JSON-RPC connections from specified source. Valid values for <ip> are a single IP (e.g. 1.2.3.4), a network/netmask (e.g. 1.2.3.4/255.255.255.0), a network/CIDR (e.g. 1.2.3.4/24), all ipv4 (0.0.0.0/0), or all ipv6 (::/0). This option can be specified multiple times", ArgsManager::ALLOW_ANY, OptionsCategory::RPC);
     824        3789 :     argsman.AddArg("-rpcauth=<userpw>", "Username and HMAC-SHA-256 hashed password for JSON-RPC connections. The field <userpw> comes in the format: <USERNAME>:<SALT>$<HASH>. A canonical python script is included in share/rpcuser. The client then connects normally using the rpcuser=<USERNAME>/rpcpassword=<PASSWORD> pair of arguments. This option can be specified multiple times", ArgsManager::ALLOW_ANY | ArgsManager::SENSITIVE, OptionsCategory::RPC);
     825        3789 :     argsman.AddArg("-rpcbind=<addr>[:port]", "Bind to given address to listen for JSON-RPC connections. Do not expose the RPC server to untrusted networks such as the public internet! This option is ignored unless -rpcallowip is also passed. Port is optional and overrides -rpcport. Use [host]:port notation for IPv6. This option can be specified multiple times (default: 127.0.0.1 and ::1 i.e., localhost, or if -rpcallowip has been specified, 0.0.0.0 and :: i.e., all addresses)", ArgsManager::ALLOW_ANY | ArgsManager::NETWORK_ONLY, OptionsCategory::RPC);
     826        3789 :     argsman.AddArg("-rpcdoccheck", strprintf("Throw a non-fatal error at runtime if the documentation for an RPC is incorrect (default: %u)", DEFAULT_RPC_DOC_CHECK), ArgsManager::ALLOW_ANY | ArgsManager::DEBUG_ONLY, OptionsCategory::RPC);
     827        3789 :     argsman.AddArg("-rpccookiefile=<loc>", "Location of the auth cookie. Relative paths will be prefixed by a net-specific datadir location. (default: data dir)", ArgsManager::ALLOW_ANY, OptionsCategory::RPC);
     828        3789 :     argsman.AddArg("-rpcexternaluser=<users>", "List of comma-separated usernames for JSON-RPC external connections", ArgsManager::ALLOW_ANY | ArgsManager::SENSITIVE, OptionsCategory::RPC);
     829        3789 :     argsman.AddArg("-rpcexternalworkqueue=<n>", strprintf("Set the depth of the work queue to service external RPC calls (default: %d)", DEFAULT_HTTP_WORKQUEUE), ArgsManager::ALLOW_ANY | ArgsManager::DEBUG_ONLY, OptionsCategory::RPC);
     830        3789 :     argsman.AddArg("-rpcpassword=<pw>", "Password for JSON-RPC connections", ArgsManager::ALLOW_ANY | ArgsManager::SENSITIVE, OptionsCategory::RPC);
     831        3789 :     argsman.AddArg("-rpcport=<port>", strprintf("Listen for JSON-RPC connections on <port> (default: %u, testnet: %u, devnet: %u, regtest: %u)", defaultBaseParams->RPCPort(), testnetBaseParams->RPCPort(), devnetBaseParams->RPCPort(), regtestBaseParams->RPCPort()), ArgsManager::ALLOW_ANY | ArgsManager::NETWORK_ONLY, OptionsCategory::RPC);
     832        3789 :     argsman.AddArg("-rpcservertimeout=<n>", strprintf("Timeout during HTTP requests (default: %d)", DEFAULT_HTTP_SERVER_TIMEOUT), ArgsManager::ALLOW_ANY | ArgsManager::DEBUG_ONLY, OptionsCategory::RPC);
     833        3789 :     argsman.AddArg("-rpcthreads=<n>", strprintf("Set the number of threads to service RPC calls (default: %d)", DEFAULT_HTTP_THREADS), ArgsManager::ALLOW_ANY, OptionsCategory::RPC);
     834        3789 :     argsman.AddArg("-rpcuser=<user>", "Username for JSON-RPC connections", ArgsManager::ALLOW_ANY | ArgsManager::SENSITIVE, OptionsCategory::RPC);
     835        3789 :     argsman.AddArg("-rpcwhitelist=<whitelist>", "Set a whitelist to filter incoming RPC calls for a specific user. The field <whitelist> comes in the format: <USERNAME>:<rpc 1>,<rpc 2>,...,<rpc n>. If multiple whitelists are set for a given user, they are set-intersected. See -rpcwhitelistdefault documentation for information on default whitelist behavior.", ArgsManager::ALLOW_ANY, OptionsCategory::RPC);
     836        3789 :     argsman.AddArg("-rpcwhitelistdefault", "Sets default behavior for rpc whitelisting. Unless rpcwhitelistdefault is set to 0, if any -rpcwhitelist is set, the rpc server acts as if all rpc users are subject to empty-unless-otherwise-specified whitelists. If rpcwhitelistdefault is set to 1 and no -rpcwhitelist is set, rpc server acts as if all rpc users are subject to empty whitelists.", ArgsManager::ALLOW_ANY, OptionsCategory::RPC);
     837        3789 :     argsman.AddArg("-rpcworkqueue=<n>", strprintf("Set the depth of the work queue to service RPC calls (default: %d)", DEFAULT_HTTP_WORKQUEUE), ArgsManager::ALLOW_ANY | ArgsManager::DEBUG_ONLY, OptionsCategory::RPC);
     838        3789 :     argsman.AddArg("-server", "Accept command line and JSON-RPC commands", ArgsManager::ALLOW_ANY, OptionsCategory::RPC);
     839             : 
     840        3789 :     argsman.AddArg("-statsbatchsize=<bytes>", strprintf("Specify the size of each batch of stats messages (default: %d)", DEFAULT_STATSD_BATCH_SIZE), ArgsManager::ALLOW_ANY, OptionsCategory::STATSD);
     841        3789 :     argsman.AddArg("-statsduration=<ms>", strprintf("Specify the number of milliseconds between stats messages (default: %d)", DEFAULT_STATSD_DURATION), ArgsManager::ALLOW_ANY, OptionsCategory::STATSD);
     842        3789 :     argsman.AddArg("-statshost=<ip>", strprintf("Specify statsd host (default: %s)", DEFAULT_STATSD_HOST), ArgsManager::ALLOW_ANY, OptionsCategory::STATSD);
     843        3789 :     hidden_args.emplace_back("-statsport");
     844        3789 :     argsman.AddArg("-statsperiod=<seconds>", strprintf("Specify the number of seconds between periodic measurements (default: %d)", DEFAULT_STATSD_PERIOD), ArgsManager::ALLOW_ANY, OptionsCategory::STATSD);
     845        3789 :     argsman.AddArg("-statsprefix=<string>", strprintf("Specify an optional string prepended to every stats key (default: %s)", DEFAULT_STATSD_PREFIX), ArgsManager::ALLOW_ANY, OptionsCategory::STATSD);
     846        3789 :     argsman.AddArg("-statssuffix=<string>", strprintf("Specify an optional string appended to every stats key (default: %s)", DEFAULT_STATSD_SUFFIX), ArgsManager::ALLOW_ANY, OptionsCategory::STATSD);
     847             : #if HAVE_DECL_FORK
     848        3789 :     argsman.AddArg("-daemon", strprintf("Run in the background as a daemon and accept commands (default: %d)", DEFAULT_DAEMON), ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
     849        3789 :     argsman.AddArg("-daemonwait", strprintf("Wait for initialization to be finished before exiting. This implies -daemon (default: %d)", DEFAULT_DAEMONWAIT), ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
     850             : #else
     851             :     hidden_args.emplace_back("-daemon");
     852             :     hidden_args.emplace_back("-daemonwait");
     853             : #endif
     854             : 
     855             :     // Add the hidden options
     856        3789 :     argsman.AddHiddenArgs(hidden_args);
     857        3789 : }
     858             : 
     859             : static bool fHaveGenesis = false;
     860             : static GlobalMutex g_genesis_wait_mutex;
     861             : static std::condition_variable g_genesis_wait_cv;
     862             : 
     863         895 : static void BlockNotifyGenesisWait(const CBlockIndex* pBlockIndex)
     864             : {
     865         895 :     if (pBlockIndex != nullptr) {
     866             :         {
     867         895 :             LOCK(g_genesis_wait_mutex);
     868         895 :             fHaveGenesis = true;
     869         895 :         }
     870         895 :         g_genesis_wait_cv.notify_all();
     871         895 :     }
     872         895 : }
     873             : 
     874             : #if HAVE_SYSTEM
     875        2821 : static void StartupNotify(const ArgsManager& args)
     876             : {
     877        2821 :     std::string cmd = args.GetArg("-startupnotify", "");
     878        2821 :     if (!cmd.empty()) {
     879           2 :         std::thread t(runCommand, cmd);
     880           2 :         t.detach(); // thread runs free
     881           2 :     }
     882        2821 : }
     883             : #endif
     884             : 
     885           0 : static void PeriodicStats(NodeContext& node)
     886             : {
     887           0 :     assert(::g_stats_client->active());
     888           0 :     const ArgsManager& args = *Assert(node.args);
     889           0 :     ChainstateManager& chainman = *Assert(node.chainman);
     890           0 :     const CTxMemPool& mempool = *Assert(node.mempool);
     891           0 :     const llmq::CInstantSendManager& isman = *Assert(node.llmq_ctx->isman);
     892           0 :     chainman.ActiveChainstate().ForceFlushStateToDisk();
     893           0 :     const auto maybe_stats = WITH_LOCK(::cs_main, return GetUTXOStats(&chainman.ActiveChainstate().CoinsDB(), chainman.m_blockman, /*hash_type=*/CoinStatsHashType::NONE, node.rpc_interruption_point, chainman.ActiveChain().Tip(), /*index_requested=*/true));
     894           0 :     if (maybe_stats.has_value()) {
     895           0 :         ::g_stats_client->gauge("utxoset.tx", maybe_stats->nTransactions, 1.0f);
     896           0 :         ::g_stats_client->gauge("utxoset.txOutputs", maybe_stats->nTransactionOutputs, 1.0f);
     897           0 :         ::g_stats_client->gauge("utxoset.dbSizeBytes", maybe_stats->nDiskSize, 1.0f);
     898           0 :         ::g_stats_client->gauge("utxoset.blockHeight", maybe_stats->nHeight, 1.0f);
     899           0 :         if (maybe_stats->total_amount.has_value()) {
     900           0 :             ::g_stats_client->gauge("utxoset.totalAmount", (double)maybe_stats->total_amount.value() / (double)COIN, 1.0f);
     901           0 :         }
     902           0 :     } else {
     903             :         // something went wrong
     904           0 :         LogPrintf("%s: GetUTXOStats failed\n", __func__);
     905             :     }
     906             : 
     907           0 :     CBlockIndex *tip = chainman.ActiveChain().Tip();
     908           0 :     double nNetworkHashPS = [&]() {
     909             :         // Short version of GetNetworkHashPS(120, -1);
     910           0 :         CBlockIndex *pindex = tip;
     911           0 :         int64_t minTime = pindex->GetBlockTime();
     912           0 :         int64_t maxTime = minTime;
     913           0 :         for (int i = 0; i < 120 && pindex->pprev != nullptr; i++) {
     914           0 :             pindex = pindex->pprev;
     915           0 :             int64_t time = pindex->GetBlockTime();
     916           0 :             minTime = std::min(time, minTime);
     917           0 :             maxTime = std::max(time, maxTime);
     918           0 :         }
     919           0 :         if (minTime == maxTime) return 0.0;
     920           0 :         arith_uint256 workDiff = tip->nChainWork - pindex->nChainWork;
     921           0 :         int64_t timeDiff = maxTime - minTime;
     922           0 :         return workDiff.getdouble() / timeDiff;
     923           0 :     }();
     924             : 
     925           0 :     if (nNetworkHashPS > 0.0) {
     926           0 :         ::g_stats_client->gaugeDouble("network.hashesPerSecond", nNetworkHashPS);
     927           0 :         ::g_stats_client->gaugeDouble("network.terahashesPerSecond", nNetworkHashPS / 1e12);
     928           0 :         ::g_stats_client->gaugeDouble("network.petahashesPerSecond", nNetworkHashPS / 1e15);
     929           0 :         ::g_stats_client->gaugeDouble("network.exahashesPerSecond", nNetworkHashPS / 1e18);
     930           0 :     }
     931             :     // No need for cs_main, we never use null tip here
     932           0 :     ::g_stats_client->gaugeDouble("network.difficulty", (double)GetDifficulty(tip));
     933             : 
     934           0 :     ::g_stats_client->gauge("transactions.txCacheSize", WITH_LOCK(cs_main, return chainman.ActiveChainstate().CoinsTip().GetCacheSize()), 1.0f);
     935           0 :     ::g_stats_client->gauge("transactions.totalTransactions", tip->nChainTx, 1.0f);
     936             : 
     937             :     {
     938           0 :         LOCK(mempool.cs);
     939           0 :         ::g_stats_client->gauge("transactions.mempool.totalTransactions", mempool.size(), 1.0f);
     940           0 :         ::g_stats_client->gauge("transactions.mempool.totalTxBytes", (int64_t) mempool.GetTotalTxSize(), 1.0f);
     941           0 :         ::g_stats_client->gauge("transactions.mempool.memoryUsageBytes", (int64_t) mempool.DynamicMemoryUsage(), 1.0f);
     942           0 :         ::g_stats_client->gauge("transactions.mempool.minFeePerKb", mempool.GetMinFee(args.GetIntArg("-maxmempool", DEFAULT_MAX_MEMPOOL_SIZE) * 1000000).GetFeePerK(), 1.0f);
     943           0 :     }
     944           0 :     ::g_stats_client->gauge("transactions.mempool.lockedTransactions", isman.GetInstantSendLockCount(), 1.0f);
     945           0 : }
     946             : 
     947        3019 : static bool AppInitServers(NodeContext& node)
     948             : {
     949        3019 :     const ArgsManager& args = *Assert(node.args);
     950        3019 :     RPCServer::OnStarted(&OnRPCStarted);
     951        3019 :     RPCServer::OnStopped(&OnRPCStopped);
     952        3019 :     if (!InitHTTPServer())
     953           0 :         return false;
     954        3019 :     StartRPC();
     955        3019 :     node.rpc_interruption_point = RpcInterruptionPoint;
     956        3019 :     if (!StartHTTPRPC(node))
     957          12 :         return false;
     958        3007 :     if (args.GetBoolArg("-rest", DEFAULT_REST_ENABLE)) StartREST(node);
     959        3007 :     StartHTTPServer();
     960        3007 :     return true;
     961        3019 : }
     962             : 
     963             : // Parameter interaction based on rules
     964        3108 : void InitParameterInteraction(ArgsManager& args)
     965             : {
     966             :     // when specifying an explicit binding address, you want to listen on it
     967             :     // even when -connect or -proxy is specified
     968        3108 :     if (args.IsArgSet("-bind")) {
     969        3088 :         if (args.SoftSetBoolArg("-listen", true))
     970        3067 :             LogPrintf("%s: parameter interaction: -bind set -> setting -listen=1\n", __func__);
     971        3088 :     }
     972        3108 :     if (args.IsArgSet("-whitebind")) {
     973           6 :         if (args.SoftSetBoolArg("-listen", true))
     974           2 :             LogPrintf("%s: parameter interaction: -whitebind set -> setting -listen=1\n", __func__);
     975           6 :     }
     976             : 
     977        3108 :     if (args.IsArgSet("-connect") || args.GetIntArg("-maxconnections", DEFAULT_MAX_PEER_CONNECTIONS) <= 0) {
     978             :         // when only connecting to trusted nodes, do not seed via DNS, or listen by default
     979        2215 :         if (args.SoftSetBoolArg("-dnsseed", false))
     980           0 :             LogPrintf("%s: parameter interaction: -connect or -maxconnections=0 set -> setting -dnsseed=0\n", __func__);
     981        2215 :         if (args.SoftSetBoolArg("-listen", false))
     982          14 :             LogPrintf("%s: parameter interaction: -connect or -maxconnections=0 set -> setting -listen=0\n", __func__);
     983        2215 :     }
     984             : 
     985        3108 :     std::string proxy_arg = args.GetArg("-proxy", "");
     986        3108 :     if (proxy_arg != "" && proxy_arg != "0") {
     987             :         // to protect privacy, do not listen by default if a default proxy server is specified
     988          24 :         if (args.SoftSetBoolArg("-listen", false))
     989           0 :             LogPrintf("%s: parameter interaction: -proxy set -> setting -listen=0\n", __func__);
     990             :         // to protect privacy, do not map ports when a proxy is set. The user may still specify -listen=1
     991             :         // to listen locally, so don't rely on this happening through -listen below.
     992          24 :         if (args.SoftSetBoolArg("-upnp", false))
     993           0 :             LogPrintf("%s: parameter interaction: -proxy set -> setting -upnp=0\n", __func__);
     994          24 :         if (args.SoftSetBoolArg("-natpmp", false))
     995           0 :             LogPrintf("%s: parameter interaction: -proxy set -> setting -natpmp=0\n", __func__);
     996             :         // to protect privacy, do not discover addresses by default
     997          24 :         if (args.SoftSetBoolArg("-discover", false))
     998           0 :             LogPrintf("%s: parameter interaction: -proxy set -> setting -discover=0\n", __func__);
     999          24 :     }
    1000             : 
    1001        3108 :     if (!args.GetBoolArg("-listen", DEFAULT_LISTEN)) {
    1002             :         // do not map ports or try to retrieve public IP when not listening (pointless)
    1003          23 :         if (args.SoftSetBoolArg("-upnp", false))
    1004           0 :             LogPrintf("%s: parameter interaction: -listen=0 -> setting -upnp=0\n", __func__);
    1005          23 :         if (args.SoftSetBoolArg("-natpmp", false))
    1006           0 :             LogPrintf("%s: parameter interaction: -listen=0 -> setting -natpmp=0\n", __func__);
    1007          23 :         if (args.SoftSetBoolArg("-discover", false))
    1008           0 :             LogPrintf("%s: parameter interaction: -listen=0 -> setting -discover=0\n", __func__);
    1009          23 :         if (args.SoftSetBoolArg("-listenonion", false))
    1010           0 :             LogPrintf("%s: parameter interaction: -listen=0 -> setting -listenonion=0\n", __func__);
    1011          23 :         if (args.SoftSetBoolArg("-i2pacceptincoming", false)) {
    1012          23 :             LogPrintf("%s: parameter interaction: -listen=0 -> setting -i2pacceptincoming=0\n", __func__);
    1013          23 :         }
    1014          23 :     }
    1015             : 
    1016        3108 :     if (args.IsArgSet("-externalip")) {
    1017             :         // if an explicit public IP is specified, do not try to find others
    1018           0 :         if (args.SoftSetBoolArg("-discover", false))
    1019           0 :             LogPrintf("%s: parameter interaction: -externalip set -> setting -discover=0\n", __func__);
    1020           0 :     }
    1021             : 
    1022             :     // disable whitelistrelay in blocksonly mode
    1023        3108 :     if (args.GetBoolArg("-blocksonly", DEFAULT_BLOCKSONLY)) {
    1024          12 :         if (args.SoftSetBoolArg("-whitelistrelay", false))
    1025          12 :             LogPrintf("%s: parameter interaction: -blocksonly=1 -> setting -whitelistrelay=0\n", __func__);
    1026          12 :     }
    1027             : 
    1028             :     // Forcing relay from whitelisted hosts implies we will accept relays from them in the first place.
    1029        3108 :     if (args.GetBoolArg("-whitelistforcerelay", DEFAULT_WHITELISTFORCERELAY)) {
    1030           6 :         if (args.SoftSetBoolArg("-whitelistrelay", true))
    1031           6 :             LogPrintf("%s: parameter interaction: -whitelistforcerelay=1 -> setting -whitelistrelay=1\n", __func__);
    1032           6 :     }
    1033             : 
    1034        3108 :     if (args.IsArgSet("-onlynet")) {
    1035          16 :         const auto onlynets = args.GetArgs("-onlynet");
    1036          32 :         bool clearnet_reachable = std::any_of(onlynets.begin(), onlynets.end(), [](const auto& net) {
    1037          16 :             const auto n = ParseNetwork(net);
    1038          16 :             return n == NET_IPV4 || n == NET_IPV6;
    1039             :         });
    1040          32 :         if (!clearnet_reachable && args.SoftSetBoolArg("-dnsseed", false)) {
    1041           0 :             LogPrintf("%s: parameter interaction: -onlynet excludes IPv4 and IPv6 -> setting -dnsseed=0\n", __func__);
    1042           0 :         }
    1043          16 :     }
    1044             : 
    1045        3108 :     int64_t nPruneArg = args.GetIntArg("-prune", 0);
    1046        3108 :     if (nPruneArg > 0) {
    1047          26 :         if (args.SoftSetBoolArg("-disablegovernance", true)) {
    1048          26 :             LogPrintf("%s: parameter interaction: -prune=%d -> setting -disablegovernance=true\n", __func__, nPruneArg);
    1049          26 :         }
    1050          26 :         if (args.SoftSetBoolArg("-txindex", false)) {
    1051          18 :             LogPrintf("%s: parameter interaction: -prune=%d -> setting -txindex=false\n", __func__, nPruneArg);
    1052          18 :         }
    1053          26 :     }
    1054             : 
    1055        3108 :     if (args.IsArgSet("-masternodeblsprivkey")) {
    1056         660 :         if (args.SoftSetBoolArg("-disablewallet", true)) {
    1057         654 :             LogPrintf("%s: parameter interaction: -masternodeblsprivkey set -> setting -disablewallet=1\n", __func__);
    1058         654 :         }
    1059             :         // Enable block filters for masternodes to improve network services
    1060         660 :         if (args.SoftSetBoolArg("-peerblockfilters", true)) {
    1061         656 :             LogPrintf("%s: parameter interaction: -masternodeblsprivkey set -> setting -peerblockfilters=1\n", __func__);
    1062         656 :         }
    1063         660 :         if (args.SoftSetArg("-blockfilterindex", "basic")) {
    1064         656 :             LogPrintf("%s: parameter interaction: -masternodeblsprivkey set -> setting -blockfilterindex=basic\n", __func__);
    1065         656 :         }
    1066         660 :     }
    1067        3108 : }
    1068             : 
    1069             : /**
    1070             :  * Initialize global loggers.
    1071             :  *
    1072             :  * Note that this is called very early in the process lifetime, so you should be
    1073             :  * careful about what global state you rely on here.
    1074             :  */
    1075        3735 : void InitLogging(const ArgsManager& args)
    1076             : {
    1077        3735 :     init::SetLoggingOptions(args);
    1078        3735 :     init::LogPackageVersion();
    1079        3735 : }
    1080             : 
    1081             : namespace { // Variables internal to initialization process only
    1082             : 
    1083             : int nMaxConnections;
    1084             : int nUserMaxConnections;
    1085             : int nFD;
    1086             : ServiceFlags nLocalServices = ServiceFlags(NODE_NETWORK_LIMITED | NODE_HEADERS_COMPRESSED);
    1087             : int64_t peer_connect_timeout;
    1088        3308 : std::set<BlockFilterType> g_enabled_filter_types;
    1089             : 
    1090             : } // namespace
    1091             : 
    1092           0 : [[noreturn]] static void new_handler_terminate()
    1093             : {
    1094             :     // Rather than throwing std::bad-alloc if allocation fails, terminate
    1095             :     // immediately to (try to) avoid chain corruption.
    1096             :     // Since LogPrintf may itself allocate memory, set the handler directly
    1097             :     // to terminate first.
    1098           0 :     std::set_new_handler(std::terminate);
    1099           0 :     LogPrintf("Error: Out of memory. Terminating.\n");
    1100             : 
    1101             :     // The log was successful, terminate now.
    1102           0 :     std::terminate();
    1103           0 : };
    1104             : 
    1105        3108 : bool AppInitBasicSetup(const ArgsManager& args)
    1106             : {
    1107             :     // ********************************************************* Step 1: setup
    1108             : #ifdef _MSC_VER
    1109             :     // Turn off Microsoft heap dump noise
    1110             :     _CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE);
    1111             :     _CrtSetReportFile(_CRT_WARN, CreateFileA("NUL", GENERIC_WRITE, 0, nullptr, OPEN_EXISTING, 0, 0));
    1112             :     // Disable confusing "helpful" text message on abort, Ctrl-C
    1113             :     _set_abort_behavior(0, _WRITE_ABORT_MSG | _CALL_REPORTFAULT);
    1114             : #endif
    1115             : #ifdef WIN32
    1116             :     // Enable heap terminate-on-corruption
    1117             :     HeapSetInformation(nullptr, HeapEnableTerminationOnCorruption, nullptr, 0);
    1118             : #endif
    1119        3108 :     if (!InitShutdownState()) {
    1120           0 :         return InitError(Untranslated("Initializing wait-for-shutdown state failed."));
    1121             :     }
    1122             : 
    1123        3108 :     if (!SetupNetworking()) {
    1124           0 :         return InitError(Untranslated("Initializing networking failed."));
    1125             :     }
    1126             : 
    1127             : #ifndef WIN32
    1128             :     // Clean shutdown on SIGTERM
    1129        3108 :     registerSignalHandler(SIGTERM, HandleSIGTERM);
    1130        3108 :     registerSignalHandler(SIGINT, HandleSIGTERM);
    1131             : 
    1132             :     // Reopen debug.log on SIGHUP
    1133        3108 :     registerSignalHandler(SIGHUP, HandleSIGHUP);
    1134             : 
    1135             :     // Ignore SIGPIPE, otherwise it will bring the daemon down if the client closes unexpectedly
    1136        3108 :     signal(SIGPIPE, SIG_IGN);
    1137             : #else
    1138             :     SetConsoleCtrlHandler(consoleCtrlHandler, true);
    1139             : #endif
    1140             : 
    1141        3108 :     std::set_new_handler(new_handler_terminate);
    1142             : 
    1143        3108 :     return true;
    1144        3108 : }
    1145             : 
    1146        3735 : bool AppInitParameterInteraction(const ArgsManager& args)
    1147             : {
    1148        3735 :     const CChainParams& chainparams = Params();
    1149             :     // ********************************************************* Step 2: parameter interactions
    1150             : 
    1151             :     // also see: InitParameterInteraction()
    1152             : 
    1153             :     // Error if network-specific options (-addnode, -connect, etc) are
    1154             :     // specified in default section of config file, but not overridden
    1155             :     // on the command line or in this network's section of the config file.
    1156        3735 :     std::string network = args.GetChainName();
    1157        3735 :     bilingual_str errors;
    1158        3737 :     for (const auto& arg : args.GetUnsuitableSectionOnlyArgs()) {
    1159           2 :         errors += strprintf(_("Config setting for %s only applied on %s network when in [%s] section.") + Untranslated("\n"), arg, network, network);
    1160             :     }
    1161             : 
    1162        3735 :     if (!errors.empty()) {
    1163           2 :         return InitError(errors);
    1164             :     }
    1165             : 
    1166             :     // Warn if unrecognized section name are present in the config file.
    1167        3733 :     bilingual_str warnings;
    1168        3737 :     for (const auto& section : args.GetUnrecognizedSections()) {
    1169           4 :         warnings += strprintf(Untranslated("%s:%i ") + _("Section [%s] is not recognized.") + Untranslated("\n"), section.m_file, section.m_line, section.m_name);
    1170             :     }
    1171             : 
    1172        3733 :     if (!warnings.empty()) {
    1173           2 :         InitWarning(warnings);
    1174           2 :     }
    1175             : 
    1176        3733 :     if (!fs::is_directory(gArgs.GetBlocksDirPath())) {
    1177           2 :         return InitError(strprintf(_("Specified blocks directory \"%s\" does not exist."), args.GetArg("-blocksdir", "")));
    1178             :     }
    1179             : 
    1180             :     // parse and validate enabled filter types
    1181        3731 :     std::string blockfilterindex_value = args.GetArg("-blockfilterindex", DEFAULT_BLOCKFILTERINDEX);
    1182        3731 :     if (blockfilterindex_value == "" || blockfilterindex_value == "1") {
    1183          70 :         g_enabled_filter_types = AllBlockFilterTypes();
    1184        3731 :     } else if (blockfilterindex_value != "0") {
    1185         658 :         const std::vector<std::string> names = args.GetArgs("-blockfilterindex");
    1186        1314 :         for (const auto& name : names) {
    1187             :             BlockFilterType filter_type;
    1188         658 :             if (!BlockFilterTypeByName(name, filter_type)) {
    1189           2 :                 return InitError(strprintf(_("Unknown -blockfilterindex value %s."), name));
    1190             :             }
    1191         656 :             g_enabled_filter_types.insert(filter_type);
    1192             :         }
    1193         658 :     }
    1194             : 
    1195             :     // Signal NODE_P2P_V2 if BIP324 v2 transport is enabled.
    1196        3729 :     if (args.GetBoolArg("-v2transport", DEFAULT_V2_TRANSPORT)) {
    1197         731 :         nLocalServices = ServiceFlags(nLocalServices | NODE_P2P_V2);
    1198         731 :     }
    1199             : 
    1200             :     // Signal NODE_COMPACT_FILTERS if peerblockfilters and basic filters index are both enabled.
    1201        3729 :     if (args.GetBoolArg("-peerblockfilters", DEFAULT_PEERBLOCKFILTERS)) {
    1202         662 :         if (g_enabled_filter_types.count(BlockFilterType::BASIC_FILTER) != 1) {
    1203           2 :             return InitError(_("Cannot set -peerblockfilters without -blockfilterindex."));
    1204             :         }
    1205             : 
    1206         660 :         nLocalServices = ServiceFlags(nLocalServices | NODE_COMPACT_FILTERS);
    1207         660 :     }
    1208             : 
    1209        3727 :     if (args.GetIntArg("-prune", 0)) {
    1210          26 :         if (args.GetBoolArg("-txindex", DEFAULT_TXINDEX))
    1211           0 :             return InitError(_("Prune mode is incompatible with -txindex."));
    1212          26 :         if (args.GetBoolArg("-addressindex", DEFAULT_ADDRESSINDEX))
    1213           0 :             return InitError(_("Prune mode is incompatible with -addressindex."));
    1214          26 :         if (args.GetBoolArg("-spentindex", DEFAULT_SPENTINDEX))
    1215           0 :             return InitError(_("Prune mode is incompatible with -spentindex."));
    1216          26 :         if (args.GetBoolArg("-reindex-chainstate", false)) {
    1217           0 :             return InitError(_("Prune mode is incompatible with -reindex-chainstate. Use full -reindex instead."));
    1218             :         }
    1219          26 :         if (!args.GetBoolArg("-disablegovernance", !DEFAULT_GOVERNANCE_ENABLE)) {
    1220           0 :             return InitError(_("Prune mode is incompatible with -disablegovernance=false."));
    1221             :         }
    1222          26 :     }
    1223             : 
    1224        3727 :     if (args.IsArgSet("-devnet")) {
    1225             :         // Require setting of ports when running devnet
    1226           4 :         if (args.GetBoolArg("-listen", DEFAULT_LISTEN) && !args.IsArgSet("-port")) {
    1227           0 :             return InitError(_("-port must be specified when -devnet and -listen are specified"));
    1228             :         }
    1229           4 :         if (args.GetBoolArg("-server", false) && !args.IsArgSet("-rpcport")) {
    1230           0 :             return InitError(_("-rpcport must be specified when -devnet and -server are specified"));
    1231             :         }
    1232           4 :         if (args.GetArgs("-devnet").size() > 1) {
    1233           0 :             return InitError(_("-devnet can only be specified once"));
    1234             :         }
    1235           4 :     }
    1236             : 
    1237        3727 :     fAllowPrivateNet = args.GetBoolArg("-allowprivatenet", DEFAULT_ALLOWPRIVATENET);
    1238             : 
    1239             :     // If -forcednsseed is set to true, ensure -dnsseed has not been set to false
    1240        3727 :     if (args.GetBoolArg("-forcednsseed", DEFAULT_FORCEDNSSEED) && !args.GetBoolArg("-dnsseed", DEFAULT_DNSSEED)){
    1241           4 :         return InitError(_("Cannot set -forcednsseed to true when setting -dnsseed to false."));
    1242             :     }
    1243             : 
    1244             :     // -bind and -whitebind can't be set when not listening
    1245        3723 :     size_t nUserBind = args.GetArgs("-bind").size() + args.GetArgs("-whitebind").size();
    1246        3723 :     if (nUserBind != 0 && !args.GetBoolArg("-listen", DEFAULT_LISTEN)) {
    1247           2 :         return InitError(Untranslated("Cannot set -bind or -whitebind together with -listen=0"));
    1248             :     }
    1249             : 
    1250             :     // if listen=0, then disallow listenonion=1
    1251        3740 :     if (!args.GetBoolArg("-listen", DEFAULT_LISTEN) && args.GetBoolArg("-listenonion", DEFAULT_LISTEN_ONION)) {
    1252           0 :         return InitError(Untranslated("Cannot set -listen=0 together with -listenonion=1"));
    1253             :     }
    1254             : 
    1255             :     // Make sure enough file descriptors are available
    1256        3721 :     int nBind = std::max(nUserBind, size_t(1));
    1257        3721 :     nUserMaxConnections = args.GetIntArg("-maxconnections", DEFAULT_MAX_PEER_CONNECTIONS);
    1258        3721 :     nMaxConnections = std::max(nUserMaxConnections, 0);
    1259             : 
    1260        3721 :     nFD = RaiseFileDescriptorLimit(nMaxConnections + MIN_CORE_FILEDESCRIPTORS + MAX_ADDNODE_CONNECTIONS + nBind + NUM_FDS_MESSAGE_CAPTURE);
    1261             : 
    1262             : #ifdef USE_POLL
    1263             :     int fd_max = nFD;
    1264             : #else
    1265        3721 :     int fd_max = FD_SETSIZE;
    1266             : #endif
    1267             :     // Trim requested connection counts, to fit into system limitations
    1268             :     // <int> in std::min<int>(...) to work around FreeBSD compilation issue described in #2695
    1269        3721 :     nMaxConnections = std::max(std::min<int>(nMaxConnections, fd_max - nBind - MIN_CORE_FILEDESCRIPTORS - MAX_ADDNODE_CONNECTIONS - NUM_FDS_MESSAGE_CAPTURE), 0);
    1270        3721 :     if (nFD < MIN_CORE_FILEDESCRIPTORS)
    1271           0 :         return InitError(_("Not enough file descriptors available."));
    1272        3721 :     nMaxConnections = std::min(nFD - MIN_CORE_FILEDESCRIPTORS - MAX_ADDNODE_CONNECTIONS - NUM_FDS_MESSAGE_CAPTURE, nMaxConnections);
    1273             : 
    1274        3721 :     if (nMaxConnections < nUserMaxConnections)
    1275           0 :         InitWarning(strprintf(_("Reducing -maxconnections from %d to %d, because of system limitations."), nUserMaxConnections, nMaxConnections));
    1276             : 
    1277             :     // ********************************************************* Step 3: parameter-to-internal-flags
    1278        3721 :     init::SetLoggingCategories(args);
    1279        3721 :     init::SetLoggingLevel(args);
    1280             : 
    1281        3721 :     fCheckBlockIndex = args.GetBoolArg("-checkblockindex", chainparams.DefaultConsistencyChecks());
    1282        3721 :     fCheckpointsEnabled = args.GetBoolArg("-checkpoints", DEFAULT_CHECKPOINTS_ENABLED);
    1283             : 
    1284        3721 :     hashAssumeValid = uint256S(args.GetArg("-assumevalid", chainparams.GetConsensus().defaultAssumeValid.GetHex()));
    1285        3721 :     if (!hashAssumeValid.IsNull())
    1286         542 :         LogPrintf("Assuming ancestors of block %s have valid signatures.\n", hashAssumeValid.GetHex());
    1287             :     else
    1288        3179 :         LogPrintf("Validating signatures for all blocks.\n");
    1289             : 
    1290        3721 :     if (args.IsArgSet("-minimumchainwork")) {
    1291           8 :         const std::string minChainWorkStr = args.GetArg("-minimumchainwork", "");
    1292           8 :         if (!IsHexNumber(minChainWorkStr)) {
    1293           2 :             return InitError(strprintf(Untranslated("Invalid non-hex (%s) minimum chain work value specified"), minChainWorkStr));
    1294             :         }
    1295           6 :         nMinimumChainWork = UintToArith256(uint256S(minChainWorkStr));
    1296           8 :     } else {
    1297        3713 :         nMinimumChainWork = UintToArith256(chainparams.GetConsensus().nMinimumChainWork);
    1298             :     }
    1299        3719 :     LogPrintf("Setting nMinimumChainWork=%s\n", nMinimumChainWork.GetHex());
    1300        3719 :     if (nMinimumChainWork < UintToArith256(chainparams.GetConsensus().nMinimumChainWork)) {
    1301           0 :         LogPrintf("Warning: nMinimumChainWork set below default value of %s\n", chainparams.GetConsensus().nMinimumChainWork.GetHex());
    1302           0 :     }
    1303             : 
    1304             :     // mempool limits
    1305        3719 :     int64_t nMempoolSizeMax = args.GetIntArg("-maxmempool", DEFAULT_MAX_MEMPOOL_SIZE) * 1000000;
    1306        3719 :     int64_t nMempoolSizeMin = args.GetIntArg("-limitdescendantsize", DEFAULT_DESCENDANT_SIZE_LIMIT) * 1000 * 40;
    1307        3719 :     if (nMempoolSizeMax < 0 || nMempoolSizeMax < nMempoolSizeMin)
    1308           2 :         return InitError(strprintf(_("-maxmempool must be at least %d MB"), std::ceil(nMempoolSizeMin / 1000000.0)));
    1309             :     // incremental relay fee sets the amount the mempool min fee increases above the feerate of txs evicted due to mempool limiting.
    1310        3717 :     if (args.IsArgSet("-incrementalrelayfee")) {
    1311           0 :         if (std::optional<CAmount> inc_relay_fee = ParseMoney(args.GetArg("-incrementalrelayfee", ""))) {
    1312           0 :             ::incrementalRelayFee = CFeeRate{inc_relay_fee.value()};
    1313           0 :         } else {
    1314           0 :             return InitError(AmountErrMsg("incrementalrelayfee", args.GetArg("-incrementalrelayfee", "")));
    1315             :         }
    1316           0 :     }
    1317             : 
    1318             :     // block pruning; get the amount of disk space (in MiB) to allot for block & undo files
    1319        3717 :     int64_t nPruneArg = args.GetIntArg("-prune", 0);
    1320        3717 :     if (nPruneArg < 0) {
    1321           0 :         return InitError(_("Prune cannot be configured with a negative value."));
    1322             :     }
    1323        3717 :     nPruneTarget = (uint64_t) nPruneArg * 1024 * 1024;
    1324        3717 :     if (nPruneArg == 1) {  // manual pruning: -prune=1
    1325           4 :         LogPrintf("Block pruning enabled.  Use RPC call pruneblockchain(height) to manually prune block and undo files.\n");
    1326           4 :         nPruneTarget = std::numeric_limits<uint64_t>::max();
    1327           4 :         fPruneMode = true;
    1328        3717 :     } else if (nPruneTarget) {
    1329          22 :         if (args.GetBoolArg("-regtest", false)) {
    1330             :             // we use 1MB blocks to test this on regtest
    1331          12 :             if (nPruneTarget < 550 * 1024 * 1024) {
    1332           0 :                 return InitError(strprintf(_("Prune configured below the minimum of %d MiB.  Please use a higher number."), 550));
    1333             :             }
    1334          12 :         } else {
    1335          10 :             if (nPruneTarget < MIN_DISK_SPACE_FOR_BLOCK_FILES) {
    1336           0 :                 return InitError(strprintf(_("Prune configured below the minimum of %d MiB.  Please use a higher number."), MIN_DISK_SPACE_FOR_BLOCK_FILES / 1024 / 1024));
    1337             :             }
    1338             :         }
    1339          22 :         LogPrintf("Prune configured to target %u MiB on disk for block and undo files.\n", nPruneTarget / 1024 / 1024);
    1340          22 :         fPruneMode = true;
    1341          22 :     }
    1342             : 
    1343        3717 :     nConnectTimeout = args.GetIntArg("-timeout", DEFAULT_CONNECT_TIMEOUT);
    1344        3717 :     if (nConnectTimeout <= 0) {
    1345           0 :         nConnectTimeout = DEFAULT_CONNECT_TIMEOUT;
    1346           0 :     }
    1347             : 
    1348        3717 :     peer_connect_timeout = args.GetIntArg("-peertimeout", DEFAULT_PEER_CONNECT_TIMEOUT);
    1349        3717 :     if (peer_connect_timeout <= 0) {
    1350           6 :         return InitError(Untranslated("peertimeout must be a positive integer."));
    1351             :     }
    1352             : 
    1353        3711 :     if (args.IsArgSet("-minrelaytxfee")) {
    1354          38 :         if (std::optional<CAmount> min_relay_fee = ParseMoney(args.GetArg("-minrelaytxfee", ""))) {
    1355             :             // High fee check is done afterward in CWallet::Create()
    1356          38 :             ::minRelayTxFee = CFeeRate{min_relay_fee.value()};
    1357          38 :         } else {
    1358           0 :             return InitError(AmountErrMsg("minrelaytxfee", args.GetArg("-minrelaytxfee", "")));
    1359             :         }
    1360        3711 :     } else if (incrementalRelayFee > ::minRelayTxFee) {
    1361             :         // Allow only setting incrementalRelayFee to control both
    1362           0 :         ::minRelayTxFee = incrementalRelayFee;
    1363           0 :         LogPrintf("Increasing minrelaytxfee to %s to match incrementalrelayfee\n",::minRelayTxFee.ToString());
    1364           0 :     }
    1365             : 
    1366             :     // Sanity check argument for min fee for including tx in block
    1367             :     // TODO: Harmonize which arguments need sanity checking and where that happens
    1368        3711 :     if (args.IsArgSet("-blockmintxfee")) {
    1369           0 :         if (!ParseMoney(args.GetArg("-blockmintxfee", ""))) {
    1370           0 :             return InitError(AmountErrMsg("blockmintxfee", args.GetArg("-blockmintxfee", "")));
    1371             :         }
    1372           0 :     }
    1373             : 
    1374             :     // Feerate used to define dust.  Shouldn't be changed lightly as old
    1375             :     // implementations may inadvertently create non-standard transactions
    1376        3711 :     if (args.IsArgSet("-dustrelayfee")) {
    1377          92 :         if (std::optional<CAmount> parsed = ParseMoney(args.GetArg("-dustrelayfee", ""))) {
    1378          92 :             dustRelayFee = CFeeRate{parsed.value()};
    1379          92 :         } else {
    1380           0 :             return InitError(AmountErrMsg("dustrelayfee", args.GetArg("-dustrelayfee", "")));
    1381             :         }
    1382          92 :     }
    1383             : 
    1384        3711 :     fRequireStandard = !args.GetBoolArg("-acceptnonstdtxn", !chainparams.RequireStandard());
    1385        3711 :     if (!chainparams.IsTestChain() && !fRequireStandard) {
    1386           2 :         return InitError(strprintf(Untranslated("acceptnonstdtxn is not currently supported for %s chain"), chainparams.NetworkIDString()));
    1387             :     }
    1388        3709 :     nBytesPerSigOp = args.GetIntArg("-bytespersigop", nBytesPerSigOp);
    1389             : 
    1390        3709 :     if (!g_wallet_init_interface.ParameterInteraction()) return false;
    1391             : 
    1392        3705 :     fIsBareMultisigStd = args.GetBoolArg("-permitbaremultisig", DEFAULT_PERMIT_BAREMULTISIG);
    1393        3705 :     fAcceptDatacarrier = args.GetBoolArg("-datacarrier", DEFAULT_ACCEPT_DATACARRIER);
    1394        3705 :     nMaxDatacarrierBytes = args.GetIntArg("-datacarriersize", nMaxDatacarrierBytes);
    1395             : 
    1396             :     // Option to startup with mocktime set (used for regression testing):
    1397        3705 :     SetMockTime(args.GetIntArg("-mocktime", 0)); // SetMockTime(0) is a no-op
    1398             : 
    1399        3705 :     if (args.GetBoolArg("-peerbloomfilters", DEFAULT_PEERBLOOMFILTERS))
    1400        3703 :         nLocalServices = ServiceFlags(nLocalServices | NODE_BLOOM);
    1401             : 
    1402        3705 :     nMaxTipAge = args.GetIntArg("-maxtipage", DEFAULT_MAX_TIP_AGE);
    1403             : 
    1404        3705 :     if (args.GetBoolArg("-reindex-chainstate", false)) {
    1405             :         // indexes that must be deactivated to prevent index corruption, see #24630
    1406          12 :         if (args.GetBoolArg("-coinstatsindex", DEFAULT_COINSTATSINDEX)) {
    1407           2 :             return InitError(_("-reindex-chainstate option is not compatible with -coinstatsindex. Please temporarily disable coinstatsindex while using -reindex-chainstate, or replace -reindex-chainstate with -reindex to fully rebuild all indexes."));
    1408             :         }
    1409          10 :         if (g_enabled_filter_types.count(BlockFilterType::BASIC_FILTER)) {
    1410           2 :             return InitError(_("-reindex-chainstate option is not compatible with -blockfilterindex. Please temporarily disable blockfilterindex while using -reindex-chainstate, or replace -reindex-chainstate with -reindex to fully rebuild all indexes."));
    1411             :         }
    1412           8 :         if (args.GetBoolArg("-txindex", DEFAULT_TXINDEX)) {
    1413           0 :             return InitError(_("-reindex-chainstate option is not compatible with -txindex. Please temporarily disable txindex while using -reindex-chainstate, or replace -reindex-chainstate with -reindex to fully rebuild all indexes."));
    1414             :         }
    1415           8 :     }
    1416             : 
    1417             :     try {
    1418        3701 :         const bool fQuorumVvecRequestsEnabled{llmq::GetEnabledQuorumVvecSyncEntries(args).size() > 0};
    1419        3659 :         if (!args.GetBoolArg("-llmq-data-recovery", llmq::DEFAULT_ENABLE_QUORUM_DATA_RECOVERY) && fQuorumVvecRequestsEnabled) {
    1420           0 :             InitWarning(Untranslated("-llmq-qvvec-sync set but recovery is disabled due to -llmq-data-recovery=0"));
    1421           0 :         }
    1422        3701 :     } catch (const std::invalid_argument& e) {
    1423          42 :         return InitError(Untranslated(e.what()));
    1424          42 :     }
    1425             : 
    1426        3659 :     if (args.IsArgSet("-masternodeblsprivkey")) {
    1427         667 :         if (!args.GetBoolArg("-listen", DEFAULT_LISTEN) && Params().RequireRoutableExternalIP()) {
    1428           0 :             return InitError(Untranslated("Masternode must accept connections from outside, set -listen=1"));
    1429             :         }
    1430         660 :         if (!args.GetBoolArg("-txindex", DEFAULT_TXINDEX)) {
    1431           0 :             return InitError(Untranslated("Masternode must have transaction index enabled, set -txindex=1"));
    1432             :         }
    1433         660 :         if (!args.GetBoolArg("-peerbloomfilters", DEFAULT_PEERBLOOMFILTERS)) {
    1434           0 :             return InitError(Untranslated("Masternode must have bloom filters enabled, set -peerbloomfilters=1"));
    1435             :         }
    1436         660 :         if (args.GetIntArg("-prune", 0) > 0) {
    1437           0 :             return InitError(Untranslated("Masternode must have no pruning enabled, set -prune=0"));
    1438             :         }
    1439         660 :         if (args.GetIntArg("-maxconnections", DEFAULT_MAX_PEER_CONNECTIONS) < DEFAULT_MAX_PEER_CONNECTIONS) {
    1440           0 :             return InitError(strprintf(Untranslated("Masternode must be able to handle at least %d connections, set -maxconnections=%d"), DEFAULT_MAX_PEER_CONNECTIONS, DEFAULT_MAX_PEER_CONNECTIONS));
    1441             :         }
    1442         660 :         if (args.GetBoolArg("-disablegovernance", !DEFAULT_GOVERNANCE_ENABLE)) {
    1443           0 :             return InitError(_("You can not disable governance validation on a masternode."));
    1444             :         }
    1445         660 :     }
    1446             : 
    1447        3659 :     if (args.GetBoolArg("-disablegovernance", !DEFAULT_GOVERNANCE_ENABLE)) {
    1448          52 :         InitWarning(_("You are starting with governance validation disabled.") +
    1449          52 :             (fPruneMode ?
    1450          26 :                 Untranslated(" ") + _("This is expected because you are running a pruned node.") :
    1451           0 :                 Untranslated("")));
    1452          26 :     }
    1453             : 
    1454        3659 :     return true;
    1455        3777 : }
    1456             : 
    1457        6062 : static bool LockDataDirectory(bool probeOnly)
    1458             : {
    1459             :     // Make sure only a single Dash Core process is using the data directory.
    1460        6062 :     const fs::path& datadir = gArgs.GetDataDirNet();
    1461        6062 :     if (!DirIsWritable(datadir)) {
    1462           0 :         return InitError(strprintf(_("Cannot write to data directory '%s'; check permissions."), fs::PathToString(datadir)));
    1463             :     }
    1464        6062 :     if (!LockDirectory(datadir, ".lock", probeOnly)) {
    1465           2 :         return InitError(strprintf(_("Cannot obtain a lock on data directory %s. %s is probably already running."), fs::PathToString(datadir), PACKAGE_NAME));
    1466             :     }
    1467        6060 :     return true;
    1468        6062 : }
    1469             : 
    1470        3032 : bool AppInitSanityChecks()
    1471             : {
    1472             :     // ********************************************************* Step 4: sanity checks
    1473             : 
    1474        3032 :     init::SetGlobals();
    1475             : 
    1476        3032 :     if (!init::SanityChecks()) {
    1477           0 :         return InitError(strprintf(_("Initialization sanity check failed. %s is shutting down."), PACKAGE_NAME));
    1478             :     }
    1479             : 
    1480             :     // Probe the data directory lock to give an early error message, if possible
    1481             :     // We cannot hold the data directory lock here, as the forking for daemon() hasn't yet happened,
    1482             :     // and a fork will cause weird behavior to it.
    1483        3032 :     return LockDataDirectory(true);
    1484        3032 : }
    1485             : 
    1486        3030 : bool AppInitLockDataDirectory()
    1487             : {
    1488             :     // After daemonization get the data directory lock again and hold on to it until exit
    1489             :     // This creates a slight window for a race condition to happen, however this condition is harmless: it
    1490             :     // will at most make us exit without printing a message to console.
    1491        3030 :     if (!LockDataDirectory(false)) {
    1492             :         // Detailed error printed inside LockDataDirectory
    1493           0 :         return false;
    1494             :     }
    1495        3030 :     return true;
    1496        3030 : }
    1497             : 
    1498        3030 : bool AppInitInterfaces(NodeContext& node)
    1499             : {
    1500        3030 :     node.chain = node.init->makeChain();
    1501        3030 :     return true;
    1502             : }
    1503             : 
    1504        3030 : bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
    1505             : {
    1506        3030 :     const ArgsManager& args = *Assert(node.args);
    1507        3030 :     const CChainParams& chainparams = Params();
    1508             : 
    1509        3030 :     auto opt_max_upload = ParseByteUnits(args.GetArg("-maxuploadtarget", DEFAULT_MAX_UPLOAD_TARGET), ByteUnit::M);
    1510        3030 :     if (!opt_max_upload) {
    1511           3 :         return InitError(strprintf(_("Unable to parse -maxuploadtarget: '%s'"), args.GetArg("-maxuploadtarget", "")));
    1512             :     }
    1513             : 
    1514             :     // ********************************************************* Step 4a: application initialization
    1515        3027 :     if (!CreatePidFile(args)) {
    1516             :         // Detailed error printed inside CreatePidFile().
    1517           0 :         return false;
    1518             :     }
    1519        3027 :     if (!init::StartLogging(args)) {
    1520             :         // Detailed error printed inside StartLogging().
    1521           4 :         return false;
    1522             :     }
    1523             : 
    1524        3023 :     LogPrintf("Using at most %i automatic connections (%i file descriptors available)\n", nMaxConnections, nFD);
    1525             : 
    1526             :     // Warn about relative -datadir path.
    1527        3023 :     if (args.IsArgSet("-datadir") && !args.GetPathArg("-datadir").is_absolute()) {
    1528           0 :         LogPrintf("Warning: relative datadir option '%s' specified, which will be interpreted relative to the " /* Continued */
    1529             :                   "current working directory '%s'. This is fragile, because if Dash Core is started in the future "
    1530             :                   "from a different location, it will be unable to locate the current data files. There could "
    1531             :                   "also be data loss if Dash Core is started while in a temporary directory.\n",
    1532             :                   args.GetArg("-datadir", ""), fs::PathToString(fs::current_path()));
    1533           0 :     }
    1534             : 
    1535        3023 :     InitSignatureCache();
    1536        3023 :     InitScriptExecutionCache();
    1537             : 
    1538        3023 :     int script_threads = args.GetIntArg("-par", DEFAULT_SCRIPTCHECK_THREADS);
    1539        3023 :     if (script_threads <= 0) {
    1540             :         // -par=0 means autodetect (number of cores - 1 script threads)
    1541             :         // -par=-n means "leave n cores free" (number of cores - n - 1 script threads)
    1542        3015 :         script_threads += GetNumCores();
    1543        3015 :     }
    1544             : 
    1545             :     // Subtract 1 because the main thread counts towards the par threads
    1546        3023 :     script_threads = std::max(script_threads - 1, 0);
    1547             : 
    1548             :     // Number of script-checking threads <= MAX_SCRIPTCHECK_THREADS
    1549        3023 :     script_threads = std::min(script_threads, MAX_SCRIPTCHECK_THREADS);
    1550             : 
    1551        3023 :     LogPrintf("Script verification uses %d additional threads\n", script_threads);
    1552        3023 :     if (script_threads >= 1) {
    1553        3015 :         g_parallel_script_checks = true;
    1554        3015 :         StartScriptCheckWorkerThreads(script_threads);
    1555        3015 :     }
    1556             : 
    1557        3023 :     assert(!node.scheduler);
    1558        3023 :     node.scheduler = std::make_unique<CScheduler>();
    1559             : 
    1560             :     // Start the lightweight task scheduler thread
    1561        6046 :     node.scheduler->m_service_thread = std::thread(util::TraceThread, "scheduler", [&] { node.scheduler->serviceQueue(); });
    1562             : 
    1563             :     // Gather some entropy once per minute.
    1564       11429 :     node.scheduler->scheduleEvery([]{
    1565        5383 :         RandAddPeriodic();
    1566        8406 :     }, std::chrono::minutes{1});
    1567             : 
    1568        3023 :     GetMainSignals().RegisterBackgroundSignalScheduler(*node.scheduler);
    1569             : 
    1570             :     // Create client interfaces for wallets that are supposed to be loaded
    1571             :     // according to -wallet and -disablewallet options. This only constructs
    1572             :     // the interfaces, it doesn't load wallet data. Wallets actually get loaded
    1573             :     // when load() and start() interface methods are called below.
    1574        3023 :     g_wallet_init_interface.Construct(node);
    1575        3023 :     uiInterface.InitWallet();
    1576             : 
    1577             :     /* Register RPC commands regardless of -server setting so they will be
    1578             :      * available in the GUI RPC console even if external calls are disabled.
    1579             :      */
    1580        3023 :     RegisterAllCoreRPCCommands(tableRPC);
    1581        4459 :     for (const auto& client : node.chain_clients) {
    1582        1436 :         client->registerRpcs();
    1583             :     }
    1584             : #ifdef ENABLE_WALLET
    1585             :     // Register non-core wallet-only RPC commands. These are commands that
    1586             :     // aren't a part of the wallet library but heavily rely on wallet logic.
    1587             :     // TODO: Move them to chain client interfaces so they can be called
    1588             :     //       with registerRpcs()
    1589        3023 :     if (!args.GetBoolArg("-disablewallet", DEFAULT_DISABLE_WALLET)) {
    1590       11488 :         for (const auto& commands : {
    1591        1436 :             GetWalletCoinJoinRPCCommands(),
    1592        1436 :             GetWalletEvoRPCCommands(),
    1593        1436 :             GetWalletGovernanceRPCCommands(),
    1594        1436 :             GetWalletMasternodeRPCCommands(),
    1595             :         }) {
    1596        5744 :             node.wallet_loader->registerOtherRpcs(commands);
    1597             :         }
    1598        1436 :     }
    1599             : #endif // ENABLE_WALLET
    1600             : 
    1601             : #if ENABLE_ZMQ
    1602             :     RegisterZMQRPCCommands(tableRPC);
    1603             : #endif
    1604             : 
    1605             :     /* Start the RPC server already.  It will be started in "warmup" mode
    1606             :      * and not really process calls already (but it will signify connections
    1607             :      * that the server is there and will be ready later).  Warmup mode will
    1608             :      * be disabled when initialisation is finished.
    1609             :      */
    1610        3023 :     if (args.GetBoolArg("-server", false)) {
    1611        3019 :         uiInterface.InitMessage_connect(SetRPCWarmupStatus);
    1612        3019 :         if (!AppInitServers(node))
    1613          12 :             return InitError(_("Unable to start HTTP server. See debug log for details."));
    1614        3007 :     }
    1615             : 
    1616             :     // ********************************************************* Step 5: verify wallet database integrity
    1617             : 
    1618        3011 :     g_wallet_init_interface.InitAutoBackup();
    1619        4401 :     for (const auto& client : node.chain_clients) {
    1620        1436 :         if (!client->verify()) {
    1621          46 :             return false;
    1622             :         }
    1623             :     }
    1624             : 
    1625             :     // ********************************************************* Step 6: network initialization
    1626             :     // Note that we absolutely cannot open any actual connections
    1627             :     // until the very end ("start node") as the UTXO/block state
    1628             :     // is not yet setup and may end up being set up twice if we
    1629             :     // need to reindex later.
    1630             : 
    1631        2965 :     fListen = args.GetBoolArg("-listen", DEFAULT_LISTEN);
    1632        2965 :     fDiscover = args.GetBoolArg("-discover", true);
    1633        2965 :     const bool ignores_incoming_txs{args.GetBoolArg("-blocksonly", DEFAULT_BLOCKSONLY)};
    1634             : 
    1635             :     {
    1636             : 
    1637             :         // Read asmap file if configured
    1638        2965 :         std::vector<bool> asmap;
    1639        2965 :         if (args.IsArgSet("-asmap")) {
    1640          18 :             fs::path asmap_path = args.GetPathArg("-asmap", DEFAULT_ASMAP_FILENAME);
    1641          18 :             if (!asmap_path.is_absolute()) {
    1642          16 :                 asmap_path = gArgs.GetDataDirNet() / asmap_path;
    1643          16 :             }
    1644          18 :             if (!fs::exists(asmap_path)) {
    1645           2 :                 InitError(strprintf(_("Could not find asmap file %s"), fs::quoted(fs::PathToString(asmap_path))));
    1646           2 :                 return false;
    1647             :             }
    1648          16 :             asmap = DecodeAsmap(asmap_path);
    1649          16 :             if (asmap.size() == 0) {
    1650           2 :                 InitError(strprintf(_("Could not parse asmap file %s"), fs::quoted(fs::PathToString(asmap_path))));
    1651           2 :                 return false;
    1652             :             }
    1653          14 :             const uint256 asmap_version = (HashWriter{} << asmap).GetHash();
    1654          14 :             LogPrintf("Using asmap version %s for IP bucketing\n", asmap_version.ToString());
    1655          18 :         } else {
    1656        2947 :             LogPrintf("Using /16 prefix for IP bucketing\n");
    1657             :         }
    1658             : 
    1659             :         // Initialize netgroup manager
    1660        2961 :         assert(!node.netgroupman);
    1661        2961 :         node.netgroupman = std::make_unique<NetGroupManager>(std::move(asmap));
    1662             : 
    1663             :         // Initialize addrman
    1664        2961 :         assert(!node.addrman);
    1665        2961 :         uiInterface.InitMessage(_("Loading P2P addresses…").translated);
    1666        2961 :         auto addrman{LoadAddrman(*node.netgroupman, args)};
    1667        2961 :         if (!addrman) return InitError(util::ErrorString(addrman));
    1668        2945 :         node.addrman = std::move(*addrman);
    1669        2965 :     }
    1670             : 
    1671        2945 :     std::string sem_str = args.GetArg("-socketevents", DEFAULT_SOCKETEVENTS);
    1672        2945 :     ::g_socket_events_mode = SEMFromString(sem_str);
    1673        2945 :     if (::g_socket_events_mode == SocketEventsMode::Unknown) {
    1674           0 :         return InitError(strprintf(_("Invalid -socketevents ('%s') specified. Only these modes are supported: %s"), sem_str, GetSupportedSocketEventsStr()));
    1675             :     }
    1676             : 
    1677             :     // We need to initialize g_stats_client early as currently, g_stats_client is called
    1678             :     // regardless of whether transmitting stats are desirable or not and if
    1679             :     // g_stats_client isn't present when that attempt is made, the client will crash.
    1680             :     {
    1681        2945 :         auto stats_client = StatsdClient::make(args);
    1682        2945 :         if (!stats_client) {
    1683          10 :             return InitError(_("Cannot init Statsd client") + Untranslated(" (") + util::ErrorString(stats_client) + Untranslated(")"));
    1684             :         }
    1685        2935 :         ::g_stats_client = std::move(*stats_client);
    1686        2945 :     }
    1687             : 
    1688        2935 :     assert(!node.banman);
    1689        2935 :     node.banman = std::make_unique<BanMan>(gArgs.GetDataDirNet() / "banlist", &uiInterface, args.GetIntArg("-bantime", DEFAULT_MISBEHAVING_BANTIME));
    1690        2935 :     assert(!node.connman);
    1691        5870 :     node.connman = std::make_unique<CConnman>(GetRand<uint64_t>(),
    1692        2935 :                                               GetRand<uint64_t>(),
    1693        2935 :                                               *node.addrman, *node.netgroupman, args.GetBoolArg("-networkactive", true));
    1694             : 
    1695        2935 :     assert(!node.fee_estimator);
    1696             :     // Don't initialize fee estimation with old data if we don't relay transactions,
    1697             :     // as they would never get updated.
    1698        2935 :     if (!ignores_incoming_txs) node.fee_estimator = std::make_unique<CBlockPolicyEstimator>();
    1699             : 
    1700        2935 :     assert(!node.mn_metaman);
    1701        2935 :     node.mn_metaman = std::make_unique<CMasternodeMetaMan>();
    1702             : 
    1703        2935 :     assert(!node.netfulfilledman);
    1704        2935 :     node.netfulfilledman = std::make_unique<CNetFulfilledRequestManager>();
    1705             : 
    1706        2935 :     const bool is_governance_enabled{!args.GetBoolArg("-disablegovernance", !DEFAULT_GOVERNANCE_ENABLE)};
    1707             : 
    1708        2935 :     assert(!node.sporkman);
    1709        2935 :     node.sporkman = std::make_unique<CSporkManager>();
    1710        2935 :     node.chainlocks = std::make_unique<chainlock::Chainlocks>(*node.sporkman);
    1711             : 
    1712        2935 :     std::vector<std::string> vSporkAddresses;
    1713        2935 :     if (args.IsArgSet("-sporkaddr")) {
    1714          27 :         vSporkAddresses = args.GetArgs("-sporkaddr");
    1715          27 :     } else {
    1716        2908 :         vSporkAddresses = Params().SporkAddresses();
    1717             :     }
    1718        5978 :     for (const auto& address: vSporkAddresses) {
    1719        3043 :         if (!node.sporkman->SetSporkAddress(address)) {
    1720           0 :             return InitError(_("Invalid spork address specified with -sporkaddr"));
    1721             :         }
    1722             :     }
    1723             : 
    1724        2935 :     int minsporkkeys = args.GetIntArg("-minsporkkeys", Params().MinSporkKeys());
    1725        2935 :     if (!node.sporkman->SetMinSporkKeys(minsporkkeys)) {
    1726           0 :         return InitError(_("Invalid minimum number of spork signers specified with -minsporkkeys"));
    1727             :     }
    1728             : 
    1729             : 
    1730        2935 :     if (args.IsArgSet("-sporkkey")) { // spork priv key
    1731         291 :         if (!node.sporkman->SetPrivKey(args.GetArg("-sporkkey", ""))) {
    1732           0 :             return InitError(_("Unable to sign spork message, wrong key?"));
    1733             :         }
    1734         291 :     }
    1735             : 
    1736             :     // Check port numbers
    1737        8805 :     for (const std::string port_option : {
    1738             :         "-port",
    1739             :         "-rpcport",
    1740             :     }) {
    1741        5870 :         if (args.IsArgSet(port_option)) {
    1742        5870 :             const std::string port = args.GetArg(port_option, "");
    1743             :             uint16_t n;
    1744        5870 :             if (!ParseUInt16(port, &n) || n == 0) {
    1745           0 :                 return InitError(InvalidPortErrMsg(port_option, port));
    1746             :             }
    1747        5870 :         }
    1748        5870 :     }
    1749             : 
    1750       76188 :     for ([[maybe_unused]] const auto& [arg, unix] : std::vector<std::pair<std::string, bool>>{
    1751             :         // arg name                 UNIX socket support
    1752        2935 :         {"-i2psam",                             false},
    1753        2935 :         {"-onion",                               true},
    1754        2935 :         {"-proxy",                               true},
    1755        2935 :         {"-rpcbind",                            false},
    1756        2935 :         {"-torcontrol",                         false},
    1757        2935 :         {"-whitebind",                          false},
    1758        2935 :         {"-zmqpubhashblock",                     true},
    1759        2935 :         {"-zmqpubhashchainlock",                 true},
    1760        2935 :         {"-zmqpubhashgovernanceobject",          true},
    1761        2935 :         {"-zmqpubhashgovernancevote",            true},
    1762        2935 :         {"-zmqpubhashinstantsenddoublespend",    true},
    1763        2935 :         {"-zmqpubhashrecoveredsig",              true},
    1764        2935 :         {"-zmqpubhashtx",                        true},
    1765        2935 :         {"-zmqpubhashtxlock",                    true},
    1766        2935 :         {"-zmqpubrawblock",                      true},
    1767        2935 :         {"-zmqpubrawchainlock",                  true},
    1768        2935 :         {"-zmqpubrawchainlocksig",               true},
    1769        2935 :         {"-zmqpubrawgovernancevote",             true},
    1770        2935 :         {"-zmqpubrawgovernanceobject",           true},
    1771        2935 :         {"-zmqpubrawinstantsenddoublespend",     true},
    1772        2935 :         {"-zmqpubrawrecoveredsig",               true},
    1773        2935 :         {"-zmqpubrawtx",                         true},
    1774        2935 :         {"-zmqpubrawtxlock",                     true},
    1775        2935 :         {"-zmqpubrawtxlocksig",                  true},
    1776        2935 :         {"-zmqpubsequence",                      true},
    1777             :     }) {
    1778      146538 :         for (const std::string& socket_addr : args.GetArgs(arg)) {
    1779          64 :             std::string host_out;
    1780          64 :             uint16_t port_out{0};
    1781          64 :             if (!SplitHostPort(socket_addr, port_out, host_out)) {
    1782             : #if HAVE_SOCKADDR_UN
    1783             :                 // Allow unix domain sockets for some options e.g. unix:/some/file/path
    1784          12 :                 if (!unix || socket_addr.find(ADDR_PREFIX_UNIX) != 0) {
    1785           6 :                     return InitError(InvalidPortErrMsg(arg, socket_addr));
    1786             :                 }
    1787             : #else
    1788             :                 return InitError(InvalidPortErrMsg(arg, socket_addr));
    1789             : #endif
    1790           6 :             }
    1791          64 :         }
    1792             :     }
    1793             : 
    1794        5839 :     for (const std::string& socket_addr : args.GetArgs("-bind")) {
    1795        2910 :         std::string host_out;
    1796        2910 :         uint16_t port_out{0};
    1797        2910 :         std::string bind_socket_addr = socket_addr.substr(0, socket_addr.rfind('='));
    1798        2910 :         if (!SplitHostPort(bind_socket_addr, port_out, host_out)) {
    1799           0 :             return InitError(InvalidPortErrMsg("-bind", socket_addr));
    1800             :         }
    1801        2910 :     }
    1802             : 
    1803             :     // sanitize comments per BIP-0014, format user agent and check total size
    1804        2929 :     std::vector<std::string> uacomments;
    1805             : 
    1806        2929 :     if (chainparams.NetworkIDString() == CBaseChainParams::DEVNET) {
    1807             :         // Add devnet name to user agent. This allows to disconnect nodes immediately if they don't belong to our own devnet
    1808           4 :         uacomments.push_back(strprintf("devnet.%s", args.GetDevNetName()));
    1809           4 :     }
    1810             : 
    1811        5888 :     for (const std::string& cmt : args.GetArgs("-uacomment")) {
    1812        2959 :         if (cmt != SanitizeString(cmt, SAFE_CHARS_UA_COMMENT))
    1813          12 :             return InitError(strprintf(_("User Agent comment (%s) contains unsafe characters."), cmt));
    1814        2947 :         uacomments.push_back(cmt);
    1815             :     }
    1816        2917 :     strSubVersion = FormatSubVersion(CLIENT_NAME, CLIENT_VERSION, uacomments);
    1817        2917 :     if (strSubVersion.size() > MAX_SUBVERSION_LENGTH) {
    1818           2 :         return InitError(strprintf(_("Total length of network version string (%i) exceeds maximum length (%i). Reduce the number or size of uacomments."),
    1819           2 :             strSubVersion.size(), MAX_SUBVERSION_LENGTH));
    1820             :     }
    1821             : 
    1822        2915 :     if (args.IsArgSet("-onlynet")) {
    1823          16 :         g_reachable_nets.RemoveAll();
    1824          32 :         for (const std::string& snet : args.GetArgs("-onlynet")) {
    1825          16 :             enum Network net = ParseNetwork(snet);
    1826          16 :             if (net == NET_UNROUTABLE)
    1827           2 :                 return InitError(strprintf(_("Unknown network specified in -onlynet: '%s'"), snet));
    1828          14 :             g_reachable_nets.Add(net);
    1829             :         }
    1830          14 :     }
    1831             : 
    1832        2913 :     if (!args.IsArgSet("-cjdnsreachable")) {
    1833        2925 :         if (args.IsArgSet("-onlynet") && g_reachable_nets.Contains(NET_CJDNS)) {
    1834           2 :             return InitError(
    1835           2 :                 _("Outbound connections restricted to CJDNS (-onlynet=cjdns) but "
    1836             :                   "-cjdnsreachable is not provided"));
    1837             :         }
    1838        2909 :         g_reachable_nets.Remove(NET_CJDNS);
    1839        2909 :     }
    1840             :     // Now g_reachable_nets.Contains(NET_CJDNS) is true if:
    1841             :     // 1. -cjdnsreachable is given and
    1842             :     // 2.1. -onlynet is not given or
    1843             :     // 2.2. -onlynet=cjdns is given
    1844             : 
    1845             :     // Requesting DNS seeds entails connecting to IPv4/IPv6, which -onlynet options may prohibit:
    1846             :     // If -dnsseed=1 is explicitly specified, abort. If it's left unspecified by the user, we skip
    1847             :     // the DNS seeds by adjusting -dnsseed in InitParameterInteraction.
    1848        2911 :     if (args.GetBoolArg("-dnsseed", DEFAULT_DNSSEED) == true && !g_reachable_nets.Contains(NET_IPV4) && !g_reachable_nets.Contains(NET_IPV6)) {
    1849           2 :         return InitError(strprintf(_("Incompatible options: -dnsseed=1 was explicitly specified, but -onlynet forbids connections to IPv4/IPv6")));
    1850             :     };
    1851             : 
    1852             :     // Check for host lookup allowed before parsing any network related parameters
    1853        2909 :     fNameLookup = args.GetBoolArg("-dns", DEFAULT_NAME_LOOKUP);
    1854             : 
    1855        2909 :     Proxy onion_proxy;
    1856             : 
    1857        2909 :     bool proxyRandomize = args.GetBoolArg("-proxyrandomize", DEFAULT_PROXYRANDOMIZE);
    1858             :     // -proxy sets a proxy for all outgoing network traffic
    1859             :     // -noproxy (or -proxy=0) as well as the empty string can be used to not set a proxy, this is the default
    1860        2909 :     std::string proxyArg = args.GetArg("-proxy", "");
    1861        2909 :     if (proxyArg != "" && proxyArg != "0") {
    1862          22 :         Proxy addrProxy;
    1863          22 :         if (IsUnixSocketPath(proxyArg)) {
    1864           2 :             addrProxy = Proxy(proxyArg, proxyRandomize);
    1865           2 :         } else {
    1866          20 :             const std::optional<CService> proxyAddr{Lookup(proxyArg, 9050, fNameLookup)};
    1867          20 :             if (!proxyAddr.has_value()) {
    1868           4 :                 return InitError(strprintf(_("Invalid -proxy address or hostname: '%s'"), proxyArg));
    1869             :             }
    1870             : 
    1871          16 :             addrProxy = Proxy(proxyAddr.value(), proxyRandomize);
    1872          20 :         }
    1873             : 
    1874          18 :         if (!addrProxy.IsValid())
    1875           0 :             return InitError(strprintf(_("Invalid -proxy address or hostname: '%s'"), proxyArg));
    1876             : 
    1877          18 :         SetProxy(NET_IPV4, addrProxy);
    1878          18 :         SetProxy(NET_IPV6, addrProxy);
    1879          18 :         SetProxy(NET_CJDNS, addrProxy);
    1880          18 :         SetNameProxy(addrProxy);
    1881          18 :         onion_proxy = addrProxy;
    1882          22 :     }
    1883             : 
    1884        2915 :     const bool onlynet_used_with_onion{args.IsArgSet("-onlynet") && g_reachable_nets.Contains(NET_ONION)};
    1885             : 
    1886             :     // -onion can be used to set only a proxy for .onion, or override normal proxy for .onion addresses
    1887             :     // -noonion (or -onion=0) disables connecting to .onion entirely
    1888             :     // An empty string is used to not override the onion proxy (in which case it defaults to -proxy set above, or none)
    1889        2905 :     std::string onionArg = args.GetArg("-onion", "");
    1890        2905 :     if (onionArg != "") {
    1891          14 :         if (onionArg == "0") { // Handle -noonion/-onion=0
    1892           4 :             onion_proxy = Proxy{};
    1893           4 :             if (onlynet_used_with_onion) {
    1894           4 :                 return InitError(
    1895           4 :                     _("Outbound connections restricted to Tor (-onlynet=onion) but the proxy for "
    1896             :                       "reaching the Tor network is explicitly forbidden: -onion=0"));
    1897             :             }
    1898           0 :         } else {
    1899          10 :             if (IsUnixSocketPath(onionArg)) {
    1900           2 :                 onion_proxy = Proxy(onionArg, proxyRandomize);
    1901           2 :             } else {
    1902           8 :                 const std::optional<CService> addr{Lookup(onionArg, 9050, fNameLookup)};
    1903           8 :                 if (!addr.has_value() || !addr->IsValid()) {
    1904           2 :                     return InitError(strprintf(_("Invalid -onion address or hostname: '%s'"), onionArg));
    1905             :                 }
    1906             : 
    1907           6 :                 onion_proxy = Proxy(addr.value(), proxyRandomize);
    1908           8 :             }
    1909             :         }
    1910           8 :     }
    1911             : 
    1912        2899 :     if (onion_proxy.IsValid()) {
    1913          24 :         SetProxy(NET_ONION, onion_proxy);
    1914          24 :     } else {
    1915             :         // If -listenonion is set, then we will (try to) connect to the Tor control port
    1916             :         // later from the torcontrol thread and may retrieve the onion proxy from there.
    1917        2875 :         const bool listenonion_disabled{!args.GetBoolArg("-listenonion", DEFAULT_LISTEN_ONION)};
    1918        2875 :         if (onlynet_used_with_onion && listenonion_disabled) {
    1919           2 :             return InitError(
    1920           2 :                 _("Outbound connections restricted to Tor (-onlynet=onion) but the proxy for "
    1921             :                   "reaching the Tor network is not provided: none of -proxy, -onion or "
    1922             :                   "-listenonion is given"));
    1923             :         }
    1924        2873 :         g_reachable_nets.Remove(NET_ONION);
    1925             :     }
    1926             : 
    1927        2897 :     for (const std::string& strAddr : args.GetArgs("-externalip")) {
    1928           0 :         const std::optional<CService> addrLocal{Lookup(strAddr, GetListenPort(), fNameLookup)};
    1929           0 :         if (addrLocal.has_value() && addrLocal->IsValid())
    1930           0 :             AddLocal(addrLocal.value(), LOCAL_MANUAL);
    1931             :         else
    1932           0 :             return InitError(ResolveErrMsg("externalip", strAddr));
    1933           0 :     }
    1934             : 
    1935             : #if ENABLE_ZMQ
    1936             :     g_zmq_notification_interface = CZMQNotificationInterface::Create();
    1937             : 
    1938             :     if (g_zmq_notification_interface) {
    1939             :         RegisterValidationInterface(g_zmq_notification_interface.get());
    1940             :     }
    1941             : #endif
    1942             : 
    1943             :     // ********************************************************* Step 7a: Load sporks
    1944             : 
    1945        2897 :     if (!node.sporkman->LoadCache()) {
    1946           0 :         auto file_path = fs::PathToString(gArgs.GetDataDirNet() / "sporks.dat");
    1947           0 :         return InitError(strprintf(_("Failed to load sporks cache from %s"), file_path));
    1948           0 :     }
    1949             : 
    1950             :     // ********************************************************* Step 7b: load block chain
    1951             : 
    1952        2897 :     fReindex = args.GetBoolArg("-reindex", false);
    1953        2897 :     bool fReindexChainState = args.GetBoolArg("-reindex-chainstate", false);
    1954             : 
    1955             :     // cache size calculations
    1956        2897 :     CacheSizes cache_sizes = CalculateCacheSizes(args, g_enabled_filter_types.size());
    1957             : 
    1958        2897 :     int64_t nMempoolSizeMax = args.GetIntArg("-maxmempool", DEFAULT_MAX_MEMPOOL_SIZE) * 1000000;
    1959        2897 :     LogPrintf("Cache configuration:\n");
    1960        2897 :     LogPrintf("* Using %.1f MiB for block index database\n", cache_sizes.block_tree_db * (1.0 / 1024 / 1024));
    1961        2897 :     if (args.GetBoolArg("-txindex", DEFAULT_TXINDEX)) {
    1962        2847 :         LogPrintf("* Using %.1f MiB for transaction index database\n", cache_sizes.tx_index * (1.0 / 1024 / 1024));
    1963        2847 :     }
    1964        2897 :     if (args.GetBoolArg("-addressindex", DEFAULT_ADDRESSINDEX)) {
    1965          10 :         LogPrintf("* Using %.1f MiB for address index database\n", cache_sizes.address_index * (1.0 / 1024 / 1024));
    1966          10 :     }
    1967        2897 :     if (args.GetBoolArg("-timestampindex", DEFAULT_TIMESTAMPINDEX)) {
    1968           6 :         LogPrintf("* Using %.1f MiB for timestamp index database\n", cache_sizes.timestamp_index * (1.0 / 1024 / 1024));
    1969           6 :     }
    1970        2897 :     if (args.GetBoolArg("-spentindex", DEFAULT_SPENTINDEX)) {
    1971          10 :         LogPrintf("* Using %.1f MiB for spent index database\n", cache_sizes.spent_index * (1.0 / 1024 / 1024));
    1972          10 :     }
    1973        3621 :     for (BlockFilterType filter_type : g_enabled_filter_types) {
    1974         724 :         LogPrintf("* Using %.1f MiB for %s block filter index database\n",
    1975             :                   cache_sizes.filter_index * (1.0 / 1024 / 1024), BlockFilterTypeName(filter_type));
    1976             :     }
    1977        2897 :     LogPrintf("* Using %.1f MiB for chain state database\n", cache_sizes.coins_db * (1.0 / 1024 / 1024));
    1978        2897 :     LogPrintf("* Using %.1f MiB for in-memory UTXO set (plus up to %.1f MiB of unused mempool space)\n", cache_sizes.coins * (1.0 / 1024 / 1024), nMempoolSizeMax * (1.0 / 1024 / 1024));
    1979             : 
    1980        2897 :     assert(!node.mempool);
    1981        2897 :     assert(!node.chainman);
    1982        2897 :     assert(!node.mn_sync);
    1983        2897 :     const int mempool_check_ratio = std::clamp<int>(args.GetIntArg("-checkmempool", chainparams.DefaultConsistencyChecks() ? 1 : 0), 0, 1000000);
    1984             : 
    1985        5764 :     for (bool fLoaded = false; !fLoaded && !ShutdownRequested();) {
    1986        2885 :         node.mempool = std::make_unique<CTxMemPool>(node.fee_estimator.get(), mempool_check_ratio);
    1987             : 
    1988        2885 :         node.chainman = std::make_unique<ChainstateManager>(chainparams);
    1989        2885 :         ChainstateManager& chainman = *node.chainman;
    1990             : 
    1991             :         /**
    1992             :          * The manager needs to be constructed regardless of whether governance
    1993             :          * validation is needed or not.
    1994             :          *
    1995             :          * Instead, we decide whether to initialize its database based on whether we
    1996             :          * need it or not further down and then query if the database is initialized
    1997             :          * to check if validation is enabled.
    1998             :          */
    1999        2885 :         node.mn_sync = std::make_unique<CMasternodeSync>(std::make_unique<NodeSyncNotifierImpl>(*node.connman, *node.netfulfilledman));
    2000             : 
    2001        2885 :         const bool fReset = fReindex;
    2002        2885 :         bilingual_str strLoadError;
    2003             : 
    2004        2885 :         uiInterface.InitMessage(_("Loading block index…").translated);
    2005        2885 :         const auto load_block_index_start_time{SteadyClock::now()};
    2006        2885 :         std::optional<ChainstateLoadingError> maybe_load_error;
    2007             :         try {
    2008        5770 :             maybe_load_error = LoadChainstate(fReset,
    2009        2885 :                                               chainman,
    2010        2885 :                                               *node.mn_metaman,
    2011        2885 :                                               *node.sporkman,
    2012        2885 :                                               *node.chainlocks,
    2013        2885 :                                               *node.mn_sync,
    2014        2885 :                                               node.chain_helper,
    2015        2885 :                                               node.dmnman,
    2016        2885 :                                               node.evodb,
    2017        2885 :                                               node.llmq_ctx,
    2018        2885 :                                               Assert(node.mempool.get()),
    2019        2885 :                                               args.GetDataDirNet(),
    2020        2885 :                                               fPruneMode,
    2021        2885 :                                               chainparams.GetConsensus(),
    2022        2885 :                                               fReindexChainState,
    2023        2885 :                                               cache_sizes.block_tree_db,
    2024        2885 :                                               cache_sizes.coins_db,
    2025        2885 :                                               cache_sizes.coins,
    2026             :                                               /*block_tree_db_in_memory=*/false,
    2027             :                                               /*coins_db_in_memory=*/false,
    2028             :                                               /*dash_dbs_in_memory=*/false,
    2029        5770 :                                               /*bls_threads=*/[&args]() -> int8_t {
    2030        2885 :                                                   int8_t threads = args.GetIntArg("-parbls", llmq::DEFAULT_BLSCHECK_THREADS);
    2031        2885 :                                                   if (threads <= 0) {
    2032             :                                                       // -parbls=0 means autodetect (number of cores - 1 validator threads)
    2033             :                                                       // -parbls=-n means "leave n cores free" (number of cores - n - 1 validator threads)
    2034        2885 :                                                       threads += GetNumCores();
    2035        2885 :                                                   }
    2036             :                                                   // Subtract 1 because the main thread counts towards the par threads
    2037        2885 :                                                   return std::clamp<int8_t>(threads - 1, 0, llmq::MAX_BLSCHECK_THREADS);
    2038           0 :                                               }(),
    2039        2885 :                                               llmq::DEFAULT_WORKER_COUNT,
    2040        2885 :                                               args.GetIntArg("-maxrecsigsage", llmq::DEFAULT_MAX_RECOVERED_SIGS_AGE),
    2041        2885 :                                               /*shutdown_requested=*/ShutdownRequested,
    2042        2885 :                                               /*coins_error_cb=*/[]() {
    2043           0 :                                                   uiInterface.ThreadSafeMessageBox(
    2044           0 :                                                       _("Error reading from database, shutting down."),
    2045           0 :                                                       "", CClientUIInterface::MSG_ERROR);
    2046           0 :                                               });
    2047        2885 :         } catch (const std::exception& e) {
    2048           4 :             LogPrintf("%s\n", e.what());
    2049           4 :             maybe_load_error = ChainstateLoadingError::ERROR_GENERIC_BLOCKDB_OPEN_FAILED;
    2050           4 :         }
    2051        2885 :         if (maybe_load_error.has_value()) {
    2052          14 :             switch (maybe_load_error.value()) {
    2053             :             case ChainstateLoadingError::ERROR_LOADING_BLOCK_DB:
    2054           4 :                 strLoadError = _("Error loading block database");
    2055           4 :                 break;
    2056             :             case ChainstateLoadingError::ERROR_BAD_GENESIS_BLOCK:
    2057             :                 // If the loaded chain has a wrong genesis, bail out immediately
    2058             :                 // (we're likely using a testnet datadir, or the other way around).
    2059           0 :                 return InitError(_("Incorrect or no genesis block found. Wrong datadir for network?"));
    2060             :             case ChainstateLoadingError::ERROR_BAD_DEVNET_GENESIS_BLOCK:
    2061           0 :                 return InitError(_("Incorrect or no devnet genesis block found. Wrong datadir for devnet specified?"));
    2062             :             case ChainstateLoadingError::ERROR_PRUNED_NEEDS_REINDEX:
    2063           0 :                 strLoadError = _("You need to rebuild the database using -reindex to go back to unpruned mode.  This will redownload the entire blockchain");
    2064           0 :                 break;
    2065             :             case ChainstateLoadingError::ERROR_LOAD_GENESIS_BLOCK_FAILED:
    2066           0 :                 strLoadError = _("Error initializing block database");
    2067           0 :                 break;
    2068             :             case ChainstateLoadingError::ERROR_CHAINSTATE_UPGRADE_FAILED:
    2069           0 :                 return InitError(_("Unsupported chainstate database format found. "
    2070             :                                    "Please restart with -reindex-chainstate. This will "
    2071             :                                    "rebuild the chainstate database."));
    2072             :             case ChainstateLoadingError::ERROR_REPLAYBLOCKS_FAILED:
    2073           0 :                 strLoadError = _("Unable to replay blocks. You will need to rebuild the database using -reindex-chainstate.");
    2074           0 :                 break;
    2075             :             case ChainstateLoadingError::ERROR_LOADCHAINTIP_FAILED:
    2076           0 :                 strLoadError = _("Error initializing block database");
    2077           0 :                 break;
    2078             :             case ChainstateLoadingError::ERROR_GENERIC_BLOCKDB_OPEN_FAILED:
    2079           4 :                 strLoadError = _("Error opening block database");
    2080           4 :                 break;
    2081             :             case ChainstateLoadingError::ERROR_COMMITING_EVO_DB:
    2082           0 :                 strLoadError = _("Failed to commit Evo database");
    2083           0 :                 break;
    2084             :             case ChainstateLoadingError::ERROR_UPGRADING_EVO_DB:
    2085           0 :                 strLoadError = _("Failed to upgrade Evo database");
    2086           0 :                 break;
    2087             :             case ChainstateLoadingError::ERROR_UPGRADING_SIGNALS_DB:
    2088           0 :                 strLoadError = _("Error upgrading evo database for EHF");
    2089           0 :                 break;
    2090             :             case ChainstateLoadingError::SHUTDOWN_PROBED:
    2091           6 :                 break;
    2092             :             }
    2093          14 :         } else {
    2094        2871 :             std::optional<ChainstateLoadVerifyError> maybe_verify_error;
    2095             :             try {
    2096        2871 :                 uiInterface.InitMessage(_("Verifying blocks…").translated);
    2097        2871 :                 auto check_blocks = args.GetIntArg("-checkblocks", DEFAULT_CHECKBLOCKS);
    2098        2871 :                 if (chainman.m_blockman.m_have_pruned && check_blocks > MIN_BLOCKS_TO_KEEP) {
    2099           0 :                     LogWarning("pruned datadir may not have more than %d blocks; only checking available blocks\n",
    2100             :                                       MIN_BLOCKS_TO_KEEP);
    2101           0 :                 }
    2102        5742 :                 maybe_verify_error = VerifyLoadedChainstate(chainman,
    2103        2871 :                                                             *Assert(node.evodb.get()),
    2104        2871 :                                                             fReset,
    2105        2871 :                                                             fReindexChainState,
    2106        2871 :                                                             chainparams.GetConsensus(),
    2107        2871 :                                                             check_blocks,
    2108        2871 :                                                             args.GetIntArg("-checklevel", DEFAULT_CHECKLEVEL),
    2109        2871 :                                                             /*get_unix_time_seconds=*/static_cast<int64_t(*)()>(GetTime),
    2110        4821 :                                                             [](bool bls_state) {
    2111        1950 :                                                                 LogPrintf("%s: bls_legacy_scheme=%d\n", __func__, bls_state);
    2112        1950 :                                                             });
    2113        2871 :             } catch (const std::exception& e) {
    2114           2 :                 LogPrintf("%s\n", e.what());
    2115           2 :                 maybe_verify_error = ChainstateLoadVerifyError::ERROR_GENERIC_FAILURE;
    2116           2 :             }
    2117        2871 :             if (maybe_verify_error.has_value()) {
    2118          10 :                 switch (maybe_verify_error.value()) {
    2119             :                 case ChainstateLoadVerifyError::ERROR_BLOCK_FROM_FUTURE:
    2120           4 :                     strLoadError = _("The block database contains a block which appears to be from the future. "
    2121             :                                      "This may be due to your computer's date and time being set incorrectly. "
    2122             :                                      "Only rebuild the block database if you are sure that your computer's date and time are correct");
    2123           4 :                     break;
    2124             :                 case ChainstateLoadVerifyError::ERROR_CORRUPTED_BLOCK_DB:
    2125           4 :                     strLoadError = _("Corrupted block database detected");
    2126           4 :                     break;
    2127             :                 case ChainstateLoadVerifyError::ERROR_EVO_DB_SANITY_FAILED:
    2128           0 :                     strLoadError = _("Error initializing block database");
    2129           0 :                     break;
    2130             :                 case ChainstateLoadVerifyError::ERROR_GENERIC_FAILURE:
    2131           2 :                     strLoadError = _("Error opening block database");
    2132           2 :                     break;
    2133             :                 }
    2134          10 :             } else {
    2135        2861 :                 fLoaded = true;
    2136        2861 :                 LogPrintf(" block index %15dms\n", Ticks<std::chrono::milliseconds>(SteadyClock::now() - load_block_index_start_time));
    2137             :             }
    2138             :         }
    2139             : 
    2140        2885 :         if (!fLoaded && !ShutdownRequested()) {
    2141             :             // first suggest a reindex
    2142          18 :             if (!fReset) {
    2143          18 :                 bool fRet = uiInterface.ThreadSafeQuestion(
    2144          18 :                     strLoadError + Untranslated(".\n\n") + _("Do you want to rebuild the block database now?"),
    2145          18 :                     strLoadError.original + ".\nPlease restart with -reindex or -reindex-chainstate to recover.",
    2146          18 :                     "", CClientUIInterface::MSG_ERROR | CClientUIInterface::BTN_ABORT);
    2147          18 :                 if (fRet) {
    2148           0 :                     fReindex = true;
    2149           0 :                     AbortShutdown();
    2150           0 :                 } else {
    2151          18 :                     LogPrintf("Aborted block database rebuild. Exiting.\n");
    2152          18 :                     return false;
    2153             :                 }
    2154           0 :             } else {
    2155           0 :                 return InitError(strLoadError);
    2156             :             }
    2157           0 :         }
    2158        2885 :     }
    2159             : 
    2160             :     // As LoadBlockIndex can take several minutes, it's possible the user
    2161             :     // requested to kill the GUI during the last operation. If so, exit.
    2162             :     // As the program has not fully started yet, Shutdown() is possibly overkill.
    2163        2879 :     if (ShutdownRequested()) {
    2164          22 :         LogPrintf("Shutdown requested. Exiting.\n");
    2165          22 :         return false;
    2166             :     }
    2167             : 
    2168        2857 :     ChainstateManager& chainman = *Assert(node.chainman);
    2169             : 
    2170        2857 :     assert(!node.dstxman);
    2171        2857 :     node.dstxman = std::make_unique<CDSTXManager>(*node.chainlocks);
    2172             : 
    2173        2857 :     node.clhandler = std::make_unique<chainlock::ChainlockHandler>(*node.chainlocks, chainman, *node.mempool, *node.mn_sync);
    2174        2857 :     RegisterValidationInterface(node.clhandler.get());
    2175             : 
    2176        2857 :     assert(!node.govman);
    2177        2857 :     node.govman = std::make_unique<CGovernanceManager>(*node.mn_metaman, *node.chainman, *node.chain_helper->superblocks, *node.dmnman, *node.mn_sync);
    2178             : 
    2179             :     // ********************************************************* Step 7c: Setup masternode mode or watch-only mode
    2180        2857 :     assert(!node.active_ctx);
    2181        2857 :     assert(!node.observer_ctx);
    2182             : 
    2183        2857 :     const bool quorums_recovery = args.GetBoolArg("-llmq-data-recovery", llmq::DEFAULT_ENABLE_QUORUM_DATA_RECOVERY);
    2184        2857 :     const bool quorums_watch = args.GetBoolArg("-watchquorums", llmq::DEFAULT_WATCH_QUORUMS);
    2185        2857 :     const llmq::QvvecSyncModeMap sync_map{llmq::GetEnabledQuorumVvecSyncEntries(args)};
    2186        2857 :     const util::DbWrapperParams dash_db_params{.path = args.GetDataDirNet(), .memory = false, .wipe = (fReindex || fReindexChainState)};
    2187             :     if (const auto operator_sk_str = args.GetArg("-masternodeblsprivkey", ""); !operator_sk_str.empty()) {
    2188             :         const CBLSSecretKey operator_sk{ParseHex(operator_sk_str)};
    2189             :         if (!operator_sk.IsValid()) {
    2190             :             return InitError(_("Invalid masternodeblsprivkey. Please see documentation."));
    2191             :         }
    2192             :         // Will init later in ThreadImport
    2193             :         node.active_ctx = std::make_unique<ActiveContext>(*node.llmq_ctx->bls_worker, chainman, *node.connman, *node.dmnman,
    2194             :                                                           *node.govman, *node.chain_helper->superblocks,
    2195             :                                                           *node.sporkman, *node.chainlocks, *node.mempool, *node.clhandler, *node.llmq_ctx->isman,
    2196             :                                                           *node.llmq_ctx->qman, *node.llmq_ctx->qsnapman, *node.llmq_ctx->sigman,
    2197             :                                                           *node.mn_sync, operator_sk, dash_db_params, quorums_watch);
    2198             :         RegisterValidationInterface(node.active_ctx.get());
    2199             :     } else if (quorums_watch) {
    2200             :         node.observer_ctx = std::make_unique<llmq::ObserverContext>(*node.dmnman, *node.llmq_ctx->qman, *node.llmq_ctx->qsnapman,
    2201             :                                                                     chainman, *node.sporkman, dash_db_params);
    2202             :         RegisterValidationInterface(node.observer_ctx.get());
    2203             :     }
    2204             : 
    2205             :     assert(!node.peerman);
    2206             :     node.peerman = PeerManager::make(chainparams, *node.connman, *node.addrman, node.banman.get(), *node.dstxman,
    2207             :                                      chainman, *node.mempool, *node.mn_metaman, *node.mn_sync,
    2208             :                                      *node.sporkman, *node.chainlocks, *node.clhandler,
    2209             :                                      node.active_ctx ? node.active_ctx->nodeman.get() : nullptr,
    2210             :                                      node.dmnman, node.cj_walletman, node.llmq_ctx, ignores_incoming_txs);
    2211             :     RegisterValidationInterface(node.peerman.get());
    2212             : 
    2213             :     g_ds_notification_interface = std::make_unique<CDSNotificationInterface>(
    2214             :         *node.connman, *node.dstxman, *node.mn_sync, *node.govman, chainman, node.dmnman // todo: replace unique_ptr for dmnman to reference
    2215             :     );
    2216             :     RegisterValidationInterface(g_ds_notification_interface.get());
    2217             : 
    2218             :     // ********************************************************* Step 7d: Setup other Dash services
    2219             : 
    2220             :     node.peerman->AddExtraHandler(std::make_unique<NetInstantSend>(node.peerman.get(), *node.llmq_ctx->isman, node.active_ctx ? node.active_ctx->is_signer.get() : nullptr, *node.llmq_ctx->sigman, *node.llmq_ctx->qman, *node.chainlocks, chainman.ActiveChainstate(), *node.mempool, *node.mn_sync));
    2221             :     node.peerman->AddExtraHandler(std::make_unique<llmq::NetSigning>(node.peerman.get(), *node.llmq_ctx->sigman, node.active_ctx ? node.active_ctx->shareman.get() : nullptr, *node.sporkman));
    2222             : 
    2223             :     {
    2224             :         llmq::QuorumRole* quorum_role = node.active_ctx ? static_cast<llmq::QuorumRole*>(node.active_ctx.get())
    2225             :                                                         : static_cast<llmq::QuorumRole*>(node.observer_ctx.get());
    2226             :         auto net_quorum = std::make_unique<llmq::NetQuorum>(
    2227             :             node.peerman.get(), *node.llmq_ctx->bls_worker, *node.connman, *node.dmnman,
    2228             :             *node.llmq_ctx->qman, *node.llmq_ctx->qsnapman, chainman,
    2229             :             *node.mn_sync, *node.sporkman, quorum_role,
    2230             :             node.active_ctx ? node.active_ctx->nodeman.get() : nullptr,
    2231             :             llmq::DEFAULT_WORKER_COUNT, sync_map, quorums_recovery);
    2232             :         node.peerman->AddExtraHandler(std::move(net_quorum));
    2233             :     }
    2234             : 
    2235             :     if (node.active_ctx) {
    2236             :         node.peerman->AddExtraHandler(std::make_unique<llmq::NetDKG>(
    2237             :             node.peerman.get(), *node.sporkman, *node.active_ctx->qdkgsman, chainman, quorums_watch,
    2238             :             *node.llmq_ctx->qman, *node.active_ctx,
    2239             :             *node.llmq_ctx->bls_worker, *node.dmnman, *node.mn_metaman,
    2240             :             *node.active_ctx->dkgdbgman, *node.llmq_ctx->quorum_block_processor, *node.llmq_ctx->qsnapman,
    2241             :             *node.active_ctx->nodeman, *node.connman));
    2242             :     } else if (node.observer_ctx) {
    2243             :         node.peerman->AddExtraHandler(std::make_unique<llmq::NetDKG>(
    2244             :             node.peerman.get(), *node.sporkman, *node.observer_ctx->qdkgsman, chainman,
    2245             :             *node.llmq_ctx->qman, *node.observer_ctx));
    2246             :     } else {
    2247             :         node.peerman->AddExtraHandler(std::make_unique<llmq::NetDKGStub>(node.peerman.get()));
    2248             :     }
    2249             : 
    2250             :     if (node.active_ctx) {
    2251             :         auto cj_server = std::make_unique<CCoinJoinServer>(node.peerman.get(), chainman, *node.connman, *node.dmnman, *node.dstxman, *node.mn_metaman,
    2252             :                                                            *node.mempool, *node.active_ctx->nodeman, *node.mn_sync, *node.llmq_ctx->isman);
    2253             :         node.active_ctx->SetCJServer(cj_server.get());
    2254             :         node.peerman->AddExtraHandler(std::move(cj_server));
    2255             :     } else {
    2256             :         assert(!node.cj_walletman);
    2257             :         // Can return nullptr if built without wallet support, must check before use
    2258             :         node.cj_walletman = CJWalletManager::make(chainman, *node.dmnman, *node.mn_metaman, *node.mempool, *node.mn_sync,
    2259             :                                                   *node.llmq_ctx->isman, !ignores_incoming_txs);
    2260             :     }
    2261             : 
    2262             :     if (node.cj_walletman) {
    2263             :         RegisterValidationInterface(node.cj_walletman.get());
    2264             :     }
    2265             : 
    2266             :     bool fLoadCacheFiles = !(fReindex || fReindexChainState) && (chainman.ActiveChain().Tip() != nullptr);
    2267             : 
    2268             :     if (!node.netfulfilledman->LoadCache(fLoadCacheFiles)) {
    2269             :         auto file_path = fs::PathToString(gArgs.GetDataDirNet() / "netfulfilled.dat");
    2270             :         if (fLoadCacheFiles) {
    2271             :             return InitError(strprintf(_("Failed to load fulfilled requests cache from %s"), file_path));
    2272             :         }
    2273             :         return InitError(strprintf(_("Failed to clear fulfilled requests cache at %s"), file_path));
    2274             :     }
    2275             : 
    2276             :     if (!node.mn_metaman->LoadCache(fLoadCacheFiles)) {
    2277             :         auto file_path = fs::PathToString(gArgs.GetDataDirNet() / "mncache.dat");
    2278             :         if (fLoadCacheFiles) {
    2279             :             return InitError(strprintf(_("Failed to load masternode cache from %s"), file_path));
    2280             :         }
    2281             :         return InitError(strprintf(_("Failed to clear masternode cache at %s"), file_path));
    2282             :     }
    2283             : 
    2284             :     if (is_governance_enabled) {
    2285             :         if (!node.govman->LoadCache(fLoadCacheFiles)) {
    2286             :             auto file_path = fs::PathToString(gArgs.GetDataDirNet() / "governance.dat");
    2287             :             if (fLoadCacheFiles) {
    2288             :                 return InitError(strprintf(_("Failed to load governance cache from %s"), file_path));
    2289             :             }
    2290             :             return InitError(strprintf(_("Failed to clear governance cache at %s"), file_path));
    2291             :         }
    2292             :     }
    2293             :     // Always register NetGovernance so it can suppress governance inv items in AlreadyHave()
    2294             :     // even when -disablegovernance is set. The handler's ProcessMessage/Schedule paths
    2295             :     // early-return on !IsValid(), and AlreadyHave() short-circuits to true so we don't grow
    2296             :     // m_requested_hash_time without a cleanup task.
    2297             :     node.peerman->AddExtraHandler(std::make_unique<NetGovernance>(node.peerman.get(), *node.govman, *node.mn_sync, *node.netfulfilledman, *node.connman));
    2298             :     node.peerman->AddExtraHandler(std::make_unique<SyncManager>(node.peerman.get(), *node.govman, *node.mn_sync, *node.connman, *node.netfulfilledman));
    2299             : 
    2300             :     // ********************************************************* Step 8: start indexers
    2301             :     if (args.GetBoolArg("-txindex", DEFAULT_TXINDEX)) {
    2302             :         g_txindex = std::make_unique<TxIndex>(cache_sizes.tx_index, false, fReindex);
    2303             :         if (!g_txindex->Start(chainman.ActiveChainstate())) {
    2304             :             return false;
    2305             :         }
    2306             :     }
    2307             : 
    2308             :     if (args.GetBoolArg("-addressindex", DEFAULT_ADDRESSINDEX)) {
    2309             :         g_addressindex = std::make_unique<AddressIndex>(cache_sizes.address_index, false, fReindex);
    2310             :         if (!g_addressindex->Start(chainman.ActiveChainstate())) {
    2311             :             return false;
    2312             :         }
    2313             :     }
    2314             : 
    2315             :     if (args.GetBoolArg("-timestampindex", DEFAULT_TIMESTAMPINDEX)) {
    2316             :         g_timestampindex = std::make_unique<TimestampIndex>(cache_sizes.timestamp_index, false, fReindex);
    2317             :         if (!g_timestampindex->Start(chainman.ActiveChainstate())) {
    2318             :             return false;
    2319             :         }
    2320             :     }
    2321             : 
    2322             :     if (args.GetBoolArg("-spentindex", DEFAULT_SPENTINDEX)) {
    2323             :         g_spentindex = std::make_unique<SpentIndex>(cache_sizes.spent_index, false, fReindex);
    2324             :         if (!g_spentindex->Start(chainman.ActiveChainstate())) {
    2325             :             return false;
    2326             :         }
    2327             :     }
    2328             : 
    2329             :     for (const auto& filter_type : g_enabled_filter_types) {
    2330             :         InitBlockFilterIndex(filter_type, cache_sizes.filter_index, false, fReindex);
    2331             :         if (!GetBlockFilterIndex(filter_type)->Start(chainman.ActiveChainstate())) {
    2332             :             return false;
    2333             :         }
    2334             :     }
    2335             : 
    2336             :     if (args.GetBoolArg("-coinstatsindex", DEFAULT_COINSTATSINDEX)) {
    2337             :         g_coin_stats_index = std::make_unique<CoinStatsIndex>(/* cache size */ 0, false, fReindex);
    2338             :         if (!g_coin_stats_index->Start(chainman.ActiveChainstate())) {
    2339             :             return false;
    2340             :         }
    2341             :     }
    2342             : 
    2343             :     // ********************************************************* Step 9: load wallet
    2344             :     for (const auto& client : node.chain_clients) {
    2345             :         if (!client->load()) {
    2346             :             return false;
    2347             :         }
    2348             :     }
    2349             : 
    2350             :     // As InitLoadWallet can take several minutes, it's possible the user
    2351             :     // requested to kill the GUI during the last operation. If so, exit.
    2352             :     if (ShutdownRequested())
    2353             :     {
    2354             :         LogPrintf("Shutdown requested. Exiting.\n");
    2355             :         return false;
    2356             :     }
    2357             :     // ********************************************************* Step 10: data directory maintenance
    2358             : 
    2359             :     // if pruning, perform the initial blockstore prune
    2360             :     // after any wallet rescanning has taken place.
    2361             :     if (fPruneMode) {
    2362             :         if (!fReindex) {
    2363             :             LOCK(cs_main);
    2364             :             for (CChainState* chainstate : chainman.GetAll()) {
    2365             :                 uiInterface.InitMessage(_("Pruning blockstore…").translated);
    2366             :                 chainstate->PruneAndFlush();
    2367             :             }
    2368             :         }
    2369             :     } else {
    2370             :         LogPrintf("Setting NODE_NETWORK on non-prune mode\n");
    2371             :         nLocalServices = ServiceFlags(nLocalServices | NODE_NETWORK);
    2372             :     }
    2373             : 
    2374             :     // As PruneAndFlush can take several minutes, it's possible the user
    2375             :     // requested to kill the GUI during the last operation. If so, exit.
    2376             :     if (ShutdownRequested())
    2377             :     {
    2378             :         LogPrintf("Shutdown requested. Exiting.\n");
    2379             :         return false;
    2380             :     }
    2381             : 
    2382             :     // ********************************************************* Step 10a: schedule Dash-specific tasks
    2383             : 
    2384             :     node.peerman->StartHandlers();
    2385             :     node.clhandler->Start();
    2386             : 
    2387             :     node.scheduler->scheduleEvery(std::bind(&CNetFulfilledRequestManager::DoMaintenance, std::ref(*node.netfulfilledman)), std::chrono::minutes{1});
    2388             :     node.scheduler->scheduleEvery(std::bind(&CMasternodeUtils::DoMaintenance, std::ref(*node.connman), std::ref(*node.dmnman), std::ref(*node.mn_sync), node.cj_walletman.get()), std::chrono::minutes{1});
    2389             :     node.scheduler->scheduleEvery(std::bind(&CDeterministicMNManager::DoMaintenance, std::ref(*node.dmnman)), std::chrono::seconds{10});
    2390             :     node.peerman->ScheduleHandlers(*node.scheduler);
    2391             : 
    2392             :     if (node.active_ctx) {
    2393             :         node.active_ctx->Start();
    2394             :         node.scheduler->scheduleEvery(std::bind(&llmq::CDKGSessionManager::CleanupOldContributions, std::ref(*node.active_ctx->qdkgsman)), std::chrono::hours{1});
    2395             :     }
    2396             : 
    2397             :     if (node.cj_walletman) {
    2398             :         node.cj_walletman->Schedule(*node.connman, *node.scheduler);
    2399             :     }
    2400             : 
    2401             :     if (::g_stats_client->active()) {
    2402             :         int nStatsPeriod = std::min(std::max((int)args.GetIntArg("-statsperiod", DEFAULT_STATSD_PERIOD), MIN_STATSD_PERIOD), MAX_STATSD_PERIOD);
    2403             :         node.scheduler->scheduleEvery(std::bind(&PeriodicStats, std::ref(node)), std::chrono::seconds{nStatsPeriod});
    2404             :     }
    2405             : 
    2406             :     // ********************************************************* Step 11: import blocks
    2407             : 
    2408             :     if (!CheckDiskSpace(gArgs.GetDataDirNet())) {
    2409             :         InitError(strprintf(_("Error: Disk space is low for %s"), fs::quoted(fs::PathToString(gArgs.GetDataDirNet()))));
    2410             :         return false;
    2411             :     }
    2412             :     if (!CheckDiskSpace(gArgs.GetBlocksDirPath())) {
    2413             :         InitError(strprintf(_("Error: Disk space is low for %s"), fs::quoted(fs::PathToString(gArgs.GetBlocksDirPath()))));
    2414             :         return false;
    2415             :     }
    2416             : 
    2417        2831 :     int chain_active_height = WITH_LOCK(cs_main, return chainman.ActiveChain().Height());
    2418             : 
    2419             :     // On first startup, warn on low block storage space
    2420             :     if (!fReindex && !fReindexChainState && chain_active_height <= 1) {
    2421             :         uint64_t additional_bytes_needed = fPruneMode ? nPruneTarget
    2422             :             : chainparams.AssumedBlockchainSize() * 1024 * 1024 * 1024;
    2423             : 
    2424             :         if (!CheckDiskSpace(args.GetBlocksDirPath(), additional_bytes_needed)) {
    2425             :             InitWarning(strprintf(_(
    2426             :                     "Disk space for %s may not accommodate the block files. " \
    2427             :                     "Approximately %u GB of data will be stored in this directory."
    2428             :                 ),
    2429             :                 fs::quoted(fs::PathToString(args.GetBlocksDirPath())),
    2430             :                 chainparams.AssumedBlockchainSize()
    2431             :             ));
    2432             :         }
    2433             :     }
    2434             : 
    2435             :     // Either install a handler to notify us when genesis activates, or set fHaveGenesis directly.
    2436             :     // No locking, as this happens before any background thread is started.
    2437             :     boost::signals2::connection block_notify_genesis_wait_connection;
    2438             :     if (chainman.ActiveChain().Tip() == nullptr) {
    2439             :         block_notify_genesis_wait_connection = uiInterface.NotifyBlockTip_connect(std::bind(BlockNotifyGenesisWait, std::placeholders::_2));
    2440             :     } else {
    2441             :         fHaveGenesis = true;
    2442             :     }
    2443             : 
    2444             : #if HAVE_SYSTEM
    2445             :     const std::string block_notify = args.GetArg("-blocknotify", "");
    2446             :     if (!block_notify.empty()) {
    2447         476 :         uiInterface.NotifyBlockTip_connect([block_notify](SynchronizationState sync_state, const CBlockIndex* pBlockIndex) {
    2448         476 :             if (sync_state != SynchronizationState::POST_INIT || !pBlockIndex) return;
    2449         474 :             std::string command = block_notify;
    2450         474 :             ReplaceAll(command, "%s", pBlockIndex->GetBlockHash().GetHex());
    2451         474 :             std::thread t(runCommand, command);
    2452         474 :             t.detach(); // thread runs free
    2453         476 :         });
    2454             :     }
    2455             :     const std::string chainlock_notify = args.GetArg("-chainlocknotify", "");
    2456             :     if (!chainlock_notify.empty()) {
    2457           2 :         uiInterface.NotifyChainLock_connect([chainlock_notify](const std::string& bestChainLockHash, int bestChainLockHeight) {
    2458           2 :             std::string command = chainlock_notify;
    2459           2 :             ReplaceAll(command, "%s", bestChainLockHash);
    2460           2 :             std::thread t(runCommand, command);
    2461           2 :             t.detach(); // thread runs free
    2462           2 :         });
    2463             :     }
    2464             : #endif
    2465             : 
    2466             :     std::vector<fs::path> vImportFiles;
    2467             :     for (const std::string& strFile : args.GetArgs("-loadblock")) {
    2468             :         vImportFiles.push_back(fs::PathFromString(strFile));
    2469             :     }
    2470             : 
    2471        2831 :     chainman.m_load_block = std::thread(&util::TraceThread, "loadblk", [=, &args, &chainman, &node] {
    2472             :         // ThreadImport can switch fReindex from true to false, fetch its original state here to use later
    2473        2831 :         bool skip_evodb_repair_on_reindex = fReindex || fReindexChainState;
    2474        2831 :         ThreadImport(chainman, vImportFiles, args);
    2475             : 
    2476             :         {
    2477        5662 :             const CBlockIndex* tip = WITH_LOCK(::cs_main, return chainman.ActiveTip());
    2478        2831 :             const bool ibd = chainman.ActiveChainstate().IsInitialBlockDownload();
    2479        2831 :             if (node.active_ctx) {
    2480             :                 // On masternodes, defer the full broadcast until after
    2481             :                 // nodeman->Init() so that GetProTxHash() is available
    2482             :                 // for quorum connection setup and skShare derivation.
    2483             :                 // Only kick CDSNotificationInterface here (cached block
    2484             :                 // height for DS/MN payments/budgets).
    2485         660 :                 g_ds_notification_interface->InitializeCurrentBlockTip(tip, ibd);
    2486         660 :             } else {
    2487             :                 // Non-masternode nodes (including observer-only): broadcast
    2488             :                 // to all subscribers now; no proTxHash dependency.
    2489        2171 :                 GetMainSignals().InitializeCurrentBlockTip(tip, ibd);
    2490             :             }
    2491             :         }
    2492             : 
    2493             :         // Seed InstantSend tip-height cache; NetInstantSend receives future
    2494             :         // updates via CValidationInterface but misses InitializeCurrentBlockTip.
    2495             :         // TODO: move cache updates from NetInstantSend to g_ds_notification due to specific of Tip's processing
    2496        2831 :         node.llmq_ctx->isman->CacheTipHeight(chainman.ActiveChain().Tip());
    2497             : 
    2498             :         {
    2499             :             // Get all UTXOs for each MN collateral in one go so that we can fill coin cache early
    2500             :             // and reduce further locking overhead for cs_main in other parts of code including GUI
    2501        2831 :             LogPrintf("Filling coin cache with masternode UTXOs...\n");
    2502        2831 :             LOCK(cs_main);
    2503        2831 :             const auto start{SteadyClock::now()};
    2504        2831 :             const auto mnList{node.dmnman->GetListAtChainTip()};
    2505        6855 :             mnList.ForEachMN(/*onlyValid=*/false, [&](const auto& dmn) {
    2506        4024 :                 Coin coin;
    2507        4024 :                 GetUTXOCoin(chainman.ActiveChainstate(), dmn.collateralOutpoint, coin);
    2508        4024 :             });
    2509        2831 :             LogPrintf("Filling coin cache with masternode UTXOs: done in %dms\n", Ticks<std::chrono::milliseconds>(SteadyClock::now() - start));
    2510        2831 :         }
    2511             : 
    2512        2831 :         if (skip_evodb_repair_on_reindex) {
    2513          73 :             LogPrintf("Skipping evodb repair during reindex\n");
    2514          73 :             node.dmnman->CompleteRepair();  // Mark as repaired since we're rebuilding fresh
    2515        2831 :         } else if (node.dmnman->IsRepaired() && !args.GetBoolArg("-forceevodbrepair", false)) {
    2516         313 :             LogPrintf("Masternode list diffs are already repaired\n");
    2517         313 :         } else {
    2518             :             const CBlockIndex* start_index;
    2519             :             const CBlockIndex* stop_index;
    2520             :             {
    2521        2445 :                 LOCK(cs_main);
    2522        2445 :                 const auto& consensus_params = Params().GetConsensus();
    2523        2445 :                 start_index = chainman.ActiveChain()[consensus_params.DIP0003Height];
    2524        2445 :                 stop_index = chainman.ActiveChain().Tip();
    2525        2445 :             }
    2526             : 
    2527        2445 :             if (start_index && stop_index && start_index->nHeight < stop_index->nHeight) {
    2528         415 :                 LogPrintf("Verifying and repairing masternode list diffs...\n");
    2529         415 :                 const auto start{SteadyClock::now()};
    2530             :                 // Create a callback that wraps CSpecialTxProcessor::BuildNewListFromBlock
    2531         415 :                 auto build_list_func = [&node](const CBlock& block, const CBlockIndex* const pindexPrev,
    2532             :                                                        const CDeterministicMNList& prevList, const CCoinsViewCache& view,
    2533             :                                                        bool debugLogs, BlockValidationState& state,
    2534             :                                                        CDeterministicMNList& mnListRet) -> bool {
    2535           0 :                     return node.chain_helper->special_tx->RebuildListFromBlock(block, pindexPrev, prevList, view, debugLogs, state, mnListRet);
    2536             :                 };
    2537         415 :                 auto result = node.dmnman->RecalculateAndRepairDiffs(start_index, stop_index, chainman, build_list_func, true);
    2538             : 
    2539         415 :                 if (!result.verification_errors.empty()) {
    2540         408 :                     LogPrintf("WARNING: Verification errors:\n%s\n", Join(result.verification_errors, "\n"));
    2541         408 :                 }
    2542             : 
    2543         415 :                 if (!result.repair_errors.empty()) {
    2544             :                     // Critical errors occurred - reindex required
    2545           0 :                     LogPrintf("Failed to repair masternode list diffs. Database corruption detected. " /* Continued */
    2546             :                               "Please restart with -reindex to rebuild the database.\n"
    2547             :                               "Errors:\n%s\n",
    2548             :                               Join(result.repair_errors, "\n"));
    2549           0 :                     StartShutdown();
    2550           0 :                     return;
    2551             :                 }
    2552         415 :                 node.dmnman->CompleteRepair();
    2553         415 :                 LogPrintf("Successfully repaired %d masternode list diffs, verified %d snapshots in %ds\n",
    2554             :                           result.diffs_recalculated, result.snapshots_verified,
    2555             :                           Ticks<std::chrono::seconds>(SteadyClock::now() - start));
    2556         415 :             }
    2557             :         }
    2558             : 
    2559        2831 :         if (node.active_ctx) {
    2560         660 :             node.active_ctx->nodeman->Init(chainman.ActiveTip());
    2561             :             // Now that nodeman->Init has set proTxHash, fan out the
    2562             :             // startup tip to all CValidationInterface subscribers.
    2563             :             // The earlier call only kicked CDSNotificationInterface
    2564             :             // directly because the full broadcast (NetQuorum,
    2565             :             // ActiveContext) depends on a valid proTxHash.
    2566        1320 :             const CBlockIndex* tip = WITH_LOCK(::cs_main, return chainman.ActiveTip());
    2567         660 :             const bool ibd = chainman.ActiveChainstate().IsInitialBlockDownload();
    2568         660 :             GetMainSignals().InitializeCurrentBlockTip(tip, ibd);
    2569         660 :         }
    2570        2831 :     });
    2571             : #ifdef ENABLE_WALLET
    2572             :     if (!args.GetBoolArg("-disablewallet", DEFAULT_DISABLE_WALLET)) {
    2573             :         g_wallet_init_interface.AutoLockMasternodeCollaterals(*node.wallet_loader);
    2574             :     }
    2575             : #endif // ENABLE_WALLET
    2576             : 
    2577             :     // Wait for genesis block to be processed
    2578             :     {
    2579             :         WAIT_LOCK(g_genesis_wait_mutex, lock);
    2580             :         // We previously could hang here if StartShutdown() is called prior to
    2581             :         // ThreadImport getting started, so instead we just wait on a timer to
    2582             :         // check ShutdownRequested() regularly.
    2583             :         while (!fHaveGenesis && !ShutdownRequested()) {
    2584             :             g_genesis_wait_cv.wait_for(lock, std::chrono::milliseconds(500));
    2585             :         }
    2586             :         block_notify_genesis_wait_connection.disconnect();
    2587             :     }
    2588             : 
    2589             :     // As importing blocks can take several minutes, it's possible the user
    2590             :     // requested to kill the GUI during one of the last operations. If so, exit.
    2591             :     if (ShutdownRequested()) {
    2592             :         LogPrintf("Shutdown requested. Exiting.\n");
    2593             :         return false;
    2594             :     }
    2595             : 
    2596             :     // ********************************************************* Step 12: start node
    2597             : 
    2598             :     //// debug print
    2599             :     {
    2600             :         LOCK(cs_main);
    2601             :         LogPrintf("block tree size = %u\n", chainman.BlockIndex().size());
    2602             :         chain_active_height = chainman.ActiveChain().Height();
    2603             :         if (tip_info) {
    2604             :             tip_info->block_height = chain_active_height;
    2605             :             tip_info->block_time = chainman.ActiveChain().Tip() ? chainman.ActiveChain().Tip()->GetBlockTime() : Params().GenesisBlock().GetBlockTime();
    2606             :             tip_info->block_hash = chainman.ActiveChain().Tip() ? chainman.ActiveChain().Tip()->GetBlockHash() : Params().GenesisBlock().GetHash();
    2607             :             tip_info->verification_progress = GuessVerificationProgress(Params().TxData(), chainman.ActiveChain().Tip());
    2608             :         }
    2609             :         if (tip_info && chainman.m_best_header) {
    2610             :             tip_info->header_height = chainman.m_best_header->nHeight;
    2611             :             tip_info->header_time = chainman.m_best_header->GetBlockTime();
    2612             :         }
    2613             :     }
    2614             :     LogPrintf("nBestHeight = %d\n", chain_active_height);
    2615             :     if (node.peerman) node.peerman->SetBestHeight(chain_active_height);
    2616             : 
    2617             :     // Map ports with UPnP or NAT-PMP.
    2618             :     StartMapPort(args.GetBoolArg("-upnp", DEFAULT_UPNP), args.GetBoolArg("-natpmp", DEFAULT_NATPMP));
    2619             : 
    2620             :     CConnman::Options connOptions;
    2621             :     connOptions.nLocalServices = nLocalServices;
    2622             :     connOptions.nMaxConnections = nMaxConnections;
    2623             :     connOptions.m_max_outbound_full_relay = std::min(MAX_OUTBOUND_FULL_RELAY_CONNECTIONS, connOptions.nMaxConnections);
    2624             :     connOptions.m_max_outbound_block_relay = std::min(MAX_BLOCK_RELAY_ONLY_CONNECTIONS, connOptions.nMaxConnections-connOptions.m_max_outbound_full_relay);
    2625             :     connOptions.m_max_outbound_onion = std::min(MAX_DESIRED_ONION_CONNECTIONS, connOptions.nMaxConnections / 2);
    2626             :     connOptions.nMaxAddnode = MAX_ADDNODE_CONNECTIONS;
    2627             :     connOptions.nMaxFeeler = MAX_FEELER_CONNECTIONS;
    2628             :     connOptions.uiInterface = &uiInterface;
    2629             :     connOptions.m_banman = node.banman.get();
    2630             :     connOptions.m_msgproc = node.peerman.get();
    2631             :     connOptions.nSendBufferMaxSize = 1000 * args.GetIntArg("-maxsendbuffer", DEFAULT_MAXSENDBUFFER);
    2632             :     connOptions.nReceiveFloodSize = 1000 * args.GetIntArg("-maxreceivebuffer", DEFAULT_MAXRECEIVEBUFFER);
    2633             :     connOptions.m_added_nodes = args.GetArgs("-addnode");
    2634             :     connOptions.nMaxOutboundLimit = *opt_max_upload;
    2635             :     connOptions.m_peer_connect_timeout = peer_connect_timeout;
    2636             :     connOptions.socketEventsMode = ::g_socket_events_mode;
    2637             :     connOptions.m_active_masternode = node.active_ctx != nullptr;
    2638             : 
    2639             :     // Port to bind to if `-bind=addr` is provided without a `:port` suffix.
    2640             :     const uint16_t default_bind_port =
    2641             :         static_cast<uint16_t>(args.GetIntArg("-port", Params().GetDefaultPort()));
    2642             : 
    2643           0 :     const auto BadPortWarning = [](const char* prefix, uint16_t port) {
    2644           0 :         return strprintf(_("%s request to listen on port %u. This port is considered \"bad\" and "
    2645             :                            "thus it is unlikely that any peer will connect to it. See "
    2646             :                            "doc/p2p-bad-ports.md for details and a full list."),
    2647             :                          prefix,
    2648             :                          port);
    2649           0 :     };
    2650             : 
    2651             :     for (const std::string& bind_arg : args.GetArgs("-bind")) {
    2652             :         std::optional<CService> bind_addr;
    2653             :         const size_t index = bind_arg.rfind('=');
    2654             :         if (index == std::string::npos) {
    2655             :             bind_addr = Lookup(bind_arg, default_bind_port, /*fAllowLookup=*/false);
    2656             :             if (bind_addr.has_value()) {
    2657             :                 connOptions.vBinds.push_back(bind_addr.value());
    2658             :                 if (IsBadPort(bind_addr.value().GetPort())) {
    2659             :                     InitWarning(BadPortWarning("-bind", bind_addr.value().GetPort()));
    2660             :                 }
    2661             :                 continue;
    2662             :             }
    2663             :         } else {
    2664             :             const std::string network_type = bind_arg.substr(index + 1);
    2665             :             if (network_type == "onion") {
    2666             :                 const std::string truncated_bind_arg = bind_arg.substr(0, index);
    2667             :                 bind_addr = Lookup(truncated_bind_arg, BaseParams().OnionServiceTargetPort(), false);
    2668             :                 if (bind_addr.has_value()) {
    2669             :                     connOptions.onion_binds.push_back(bind_addr.value());
    2670             :                     continue;
    2671             :                 }
    2672             :             }
    2673             :         }
    2674             :         return InitError(ResolveErrMsg("bind", bind_arg));
    2675             :     }
    2676             : 
    2677             :     for (const std::string& strBind : args.GetArgs("-whitebind")) {
    2678             :         NetWhitebindPermissions whitebind;
    2679             :         bilingual_str error;
    2680             :         if (!NetWhitebindPermissions::TryParse(strBind, whitebind, error)) return InitError(error);
    2681             :         connOptions.vWhiteBinds.push_back(whitebind);
    2682             :     }
    2683             : 
    2684             :     // If the user did not specify -bind= or -whitebind= then we bind
    2685             :     // on any address - 0.0.0.0 (IPv4) and :: (IPv6).
    2686             :     connOptions.bind_on_any = args.GetArgs("-bind").empty() && args.GetArgs("-whitebind").empty();
    2687             : 
    2688             :     // Emit a warning if a bad port is given to -port= but only if -bind and -whitebind are not
    2689             :     // given, because if they are, then -port= is ignored.
    2690             :     if (connOptions.bind_on_any && args.IsArgSet("-port")) {
    2691             :         const uint16_t port_arg = args.GetIntArg("-port", 0);
    2692             :         if (IsBadPort(port_arg)) {
    2693             :             InitWarning(BadPortWarning("-port", port_arg));
    2694             :         }
    2695             :     }
    2696             : 
    2697             :     CService onion_service_target;
    2698             :     if (!connOptions.onion_binds.empty()) {
    2699             :         onion_service_target = connOptions.onion_binds.front();
    2700             :     } else {
    2701             :         onion_service_target = DefaultOnionServiceTarget();
    2702             :         connOptions.onion_binds.push_back(onion_service_target);
    2703             :     }
    2704             : 
    2705             :     if (args.GetBoolArg("-listenonion", DEFAULT_LISTEN_ONION)) {
    2706             :         if (connOptions.onion_binds.size() > 1) {
    2707             :             InitWarning(strprintf(_("More than one onion bind address is provided. Using %s "
    2708             :                                     "for the automatically created Tor onion service."),
    2709             :                                   onion_service_target.ToStringAddrPort()));
    2710             :         }
    2711             :         StartTorControl(onion_service_target);
    2712             :     }
    2713             : 
    2714             :     if (connOptions.bind_on_any) {
    2715             :         // Only add all IP addresses of the machine if we would be listening on
    2716             :         // any address - 0.0.0.0 (IPv4) and :: (IPv6).
    2717             :         Discover();
    2718             :     }
    2719             : 
    2720             :     for (const auto& net : args.GetArgs("-whitelist")) {
    2721             :         NetWhitelistPermissions subnet;
    2722             :         bilingual_str error;
    2723             :         if (!NetWhitelistPermissions::TryParse(net, subnet, error)) return InitError(error);
    2724             :         connOptions.vWhitelistedRange.push_back(subnet);
    2725             :     }
    2726             : 
    2727             :     connOptions.vSeedNodes = args.GetArgs("-seednode");
    2728             : 
    2729             :     // Initiate outbound connections unless connect=0
    2730             :     connOptions.m_use_addrman_outgoing = !args.IsArgSet("-connect");
    2731             :     if (!connOptions.m_use_addrman_outgoing) {
    2732             :         const auto connect = args.GetArgs("-connect");
    2733             :         if (connect.size() != 1 || connect[0] != "0") {
    2734             :             connOptions.m_specified_outgoing = connect;
    2735             :         }
    2736             :         if (!connOptions.m_specified_outgoing.empty() && !connOptions.vSeedNodes.empty()) {
    2737             :             LogPrintf("-seednode is ignored when -connect is used\n");
    2738             :         }
    2739             : 
    2740             :         if (args.IsArgSet("-dnsseed") && args.GetBoolArg("-dnsseed", DEFAULT_DNSSEED) && args.IsArgSet("-proxy")) {
    2741             :             LogPrintf("-dnsseed is ignored when -connect is used and -proxy is specified\n");
    2742             :         }
    2743             :     }
    2744             : 
    2745             :     const std::string& i2psam_arg = args.GetArg("-i2psam", "");
    2746             :     if (!i2psam_arg.empty()) {
    2747             :         const std::optional<CService> addr{Lookup(i2psam_arg, 7656, fNameLookup)};
    2748             :         if (!addr.has_value() || !addr->IsValid()) {
    2749             :             return InitError(strprintf(_("Invalid -i2psam address or hostname: '%s'"), i2psam_arg));
    2750             :         }
    2751             :         SetProxy(NET_I2P, Proxy{addr.value()});
    2752             :     } else {
    2753             :         if (args.IsArgSet("-onlynet") && g_reachable_nets.Contains(NET_I2P)) {
    2754             :             return InitError(
    2755             :                 _("Outbound connections restricted to i2p (-onlynet=i2p) but "
    2756             :                   "-i2psam is not provided"));
    2757             :         }
    2758             :         g_reachable_nets.Remove(NET_I2P);
    2759             :     }
    2760             : 
    2761             :     connOptions.m_i2p_accept_incoming = args.GetBoolArg("-i2pacceptincoming", DEFAULT_I2P_ACCEPT_INCOMING);
    2762             : 
    2763             :     if (!node.connman->Start(*node.dmnman, *node.mn_metaman, *node.mn_sync, *node.scheduler, connOptions)) {
    2764             :         return false;
    2765             :     }
    2766             : 
    2767             :     // ********************************************************* Step 13: finished
    2768             : 
    2769             :     // At this point, the RPC is "started", but still in warmup, which means it
    2770             :     // cannot yet be called. Before we make it callable, we need to make sure
    2771             :     // that the RPC's view of the best block is valid and consistent with
    2772             :     // ChainstateManager's ActiveTip.
    2773             :     //
    2774             :     // If we do not do this, RPC's view of the best block will be height=0 and
    2775             :     // hash=0x0. This will lead to erroroneous responses for things like
    2776             :     // waitforblockheight.
    2777             :     RPCNotifyBlockChange(chainman.ActiveTip());
    2778             :     SetRPCWarmupFinished();
    2779             : 
    2780             :     uiInterface.InitMessage(_("Done loading").translated);
    2781             : 
    2782             :     for (const auto& client : node.chain_clients) {
    2783             :         client->start(*node.scheduler);
    2784             :     }
    2785             : 
    2786             :     BanMan* banman = node.banman.get();
    2787        1254 :     node.scheduler->scheduleEvery([banman]{
    2788        1254 :         banman->DumpBanlist();
    2789        1254 :     }, DUMP_BANS_INTERVAL);
    2790             : 
    2791             :     if (node.peerman) node.peerman->StartScheduledTasks(*node.scheduler);
    2792             : 
    2793             : #if HAVE_SYSTEM
    2794             :     StartupNotify(args);
    2795             : #endif
    2796             : 
    2797             :     return true;
    2798           6 : }

Generated by: LCOV version 1.16