torcontrol: Move tor controller into node context

Co-authored-by: sedited <seb.kung@gmail.com>
This commit is contained in:
Fabian Jahr
2026-03-13 15:19:48 +01:00
parent eae193e750
commit b1869e9a2d
5 changed files with 11 additions and 36 deletions

View File

@@ -276,7 +276,9 @@ void Interrupt(NodeContext& node)
InterruptHTTPRPC();
InterruptRPC();
InterruptREST();
InterruptTorControl();
if (node.tor_controller) {
node.tor_controller->Interrupt();
}
InterruptMapPort();
if (node.connman)
node.connman->Interrupt();
@@ -319,7 +321,10 @@ void Shutdown(NodeContext& node)
if (node.peerman && node.validation_signals) node.validation_signals->UnregisterValidationInterface(node.peerman.get());
if (node.connman) node.connman->Stop();
StopTorControl();
if (node.tor_controller) {
node.tor_controller->Join();
node.tor_controller.reset();
}
if (node.background_init_thread.joinable()) node.background_init_thread.join();
// After everything has been shut down, but before things get flushed, stop the
@@ -2187,7 +2192,7 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
"for the automatically created Tor onion service."),
onion_service_target.ToStringAddrPort()));
}
StartTorControl(onion_service_target);
node.tor_controller = std::make_unique<TorController>(gArgs.GetArg("-torcontrol", DEFAULT_TOR_CONTROL), onion_service_target);
}
if (connOptions.bind_on_any) {

View File

@@ -17,6 +17,7 @@
#include <node/warnings.h>
#include <policy/fees/block_policy_estimator.h>
#include <scheduler.h>
#include <torcontrol.h>
#include <txmempool.h>
#include <validation.h>
#include <validationinterface.h>

View File

@@ -25,6 +25,7 @@ class ChainstateManager;
class ECC_Context;
class NetGroupManager;
class PeerManager;
class TorController;
namespace interfaces {
class Chain;
class ChainClient;
@@ -69,6 +70,7 @@ struct NodeContext {
std::unique_ptr<const NetGroupManager> netgroupman;
std::unique_ptr<CBlockPolicyEstimator> fee_estimator;
std::unique_ptr<PeerManager> peerman;
std::unique_ptr<TorController> tor_controller;
std::unique_ptr<ChainstateManager> chainman;
std::unique_ptr<BanMan> banman;
ArgsManager* args{nullptr}; // Currently a raw pointer because the memory is not managed by this struct

View File

@@ -735,35 +735,6 @@ fs::path TorController::GetPrivateKeyFile()
return gArgs.GetDataDirNet() / "onion_v3_private_key";
}
/****** Thread ********/
/**
* TODO: TBD if introducing a global is the preferred approach here since we
* usually try to avoid them. We could let init manage the lifecycle or make
* this a part of NodeContext maybe instead.
*/
static std::unique_ptr<TorController> g_tor_controller;
void StartTorControl(CService onion_service_target)
{
assert(!g_tor_controller);
g_tor_controller = std::make_unique<TorController>(gArgs.GetArg("-torcontrol", DEFAULT_TOR_CONTROL), onion_service_target);
}
void InterruptTorControl()
{
if (!g_tor_controller) return;
LogInfo("tor: Thread interrupt");
g_tor_controller->Interrupt();
}
void StopTorControl()
{
if (!g_tor_controller) return;
g_tor_controller->Join();
g_tor_controller.reset();
}
CService DefaultOnionServiceTarget(uint16_t port)
{
struct in_addr onion_service_target;

View File

@@ -31,10 +31,6 @@ constexpr int TOR_REPLY_OK{250};
constexpr int TOR_REPLY_UNRECOGNIZED{510};
constexpr int TOR_REPLY_SYNTAX_ERROR{512}; //!< Syntax error in command argument
void StartTorControl(CService onion_service_target);
void InterruptTorControl();
void StopTorControl();
CService DefaultOnionServiceTarget(uint16_t port);
/** Reply from Tor, can be single or multi-line */