Repository: bitcoin/bitcoin
File: src/init.cpp (C++)

Code before:
    CTxMemPool::Options mempool_opts{
        .estimator = node.fee_estimator.get(),
        .check_ratio = chainparams.DefaultConsistencyChecks() ? 1 : 0,
    };
    if (const auto err{ApplyArgsManOptions(args, chainparams, mempool_opts)}) {
        return InitError(*err);
    }
    mempool_opts.check_ratio = std::clamp<int>(mempool_opts.check_ratio, 0, 1'000'000);

    int64_t descendant_limit_bytes = mempool_opts.limits.descendant_size_vbytes * 40;
    if (mempool_opts.max_size_bytes < 0 || mempool_opts.max_size_bytes < descendant_limit_bytes) {
        return InitError(strprintf(_("-maxmempool must be at least %d MB"), std::ceil(descendant_limit_bytes / 1'000'000.0)));
    }
    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), mempool_opts.max_size_bytes * (1.0 / 1024 / 1024));

    for (bool fLoaded = false; !fLoaded && !ShutdownRequested();) {
        node.mempool = std::make_unique<CTxMemPool>(mempool_opts);

        ChainstateManager::Options chainman_opts{
            .chainparams = chainparams,
            .adjusted_time_callback = GetAdjustedTime,
            .datadir = args.GetDataDirNet(),
        };
        node::ReadDatabaseArgs(args, chainman_opts.block_tree_db);
        node::ReadDatabaseArgs(args, chainman_opts.coins_db);
        node::ReadCoinsViewArgs(args, chainman_opts.coins_view);
        node.chainman = std::make_unique<ChainstateManager>(chainman_opts);
        ChainstateManager& chainman = *node.chainman;

        node::ChainstateLoadOptions options;
        options.mempool = Assert(node.mempool.get());
        options.reindex = node::fReindex;
        options.reindex_chainstate = fReindexChainState;
        options.prune = node::fPruneMode;
        options.check_blocks = args.GetIntArg("-checkblocks", DEFAULT_CHECKBLOCKS);
        options.check_level = args.GetIntArg("-checklevel", DEFAULT_CHECKLEVEL);
        options.check_interrupt = ShutdownRequested;
        options.coins_error_cb = [] {
            uiInterface.ThreadSafeMessageBox(
                _("Error reading from database, shutting down."),
                "", CClientUIInterface::MSG_ERROR);
        };

        uiInterface.InitMessage(_("Loading block index…").translated);
        const int64_t load_block_index_start_time = GetTimeMillis();
        auto catch_exceptions = [](auto&& f) {
            try {
                return f();
            } catch (const std::exception& e) {
                LogPrintf("%s\n", e.what());
                return std::make_tuple(node::ChainstateLoadStatus::FAILURE, _("Error opening block database"));

Review comment (suggestion):
I'd say we should remove LOC in init.cpp, not add them. What about moving this into a new function called `ApplyArgsManOptions`?

Also, no need to re-parse this in the loop? Could move outside like fae180c7fd85ef8a1aeb489691779c215cb9d5a2

Code after:

    const bool onlynet_used_with_onion{args.IsArgSet("-onlynet") && IsReachable(NET_ONION)};

    // -onion can be used to set only a proxy for .onion, or override normal proxy for .onion addresses
    // -noonion (or -onion=0) disables connecting to .onion entirely
    // An empty string is used to not override the onion proxy (in which case it defaults to -proxy set above, or none)
    std::string onionArg = args.GetArg("-onion", "");
    if (onionArg != "") {
        if (onionArg == "0") { // Handle -noonion/-onion=0
            onion_proxy = Proxy{};
            if (onlynet_used_with_onion) {
                return InitError(
                    _("Outbound connections restricted to Tor (-onlynet=onion) but the proxy for "
                      "reaching the Tor network is explicitly forbidden: -onion=0"));
            }
        } else {
            CService addr;
            if (!Lookup(onionArg, addr, 9050, fNameLookup) || !addr.IsValid()) {
                return InitError(strprintf(_("Invalid -onion address or hostname: '%s'"), onionArg));
            }
            onion_proxy = Proxy{addr, proxyRandomize};
        }
    }

    if (onion_proxy.IsValid()) {
        SetProxy(NET_ONION, onion_proxy);
    } else {
        // If -listenonion is set, then we will (try to) connect to the Tor control port
        // later from the torcontrol thread and may retrieve the onion proxy from there.
        const bool listenonion_disabled{!args.GetBoolArg("-listenonion", DEFAULT_LISTEN_ONION)};
        if (onlynet_used_with_onion && listenonion_disabled) {
            return InitError(
                _("Outbound connections restricted to Tor (-onlynet=onion) but the proxy for "
                  "reaching the Tor network is not provided: none of -proxy, -onion or "
                  "-listenonion is given"));
        }
        SetReachable(NET_ONION, false);
    }

    for (const std::string& strAddr : args.GetArgs("-externalip")) {
        CService addrLocal;
        if (Lookup(strAddr, addrLocal, GetListenPort(), fNameLookup) && addrLocal.IsValid())
            AddLocal(addrLocal, LOCAL_MANUAL);
        else
            return InitError(ResolveErrMsg("externalip", strAddr));
    }

#if ENABLE_ZMQ
    g_zmq_notification_interface = CZMQNotificationInterface::Create();

    if (g_zmq_notification_interface) {