Skip to content

Commit e4f459d

Browse files
committed
perf: open the masternode connection thread on demand
Masternodes, quorum watchers and wallet mixing with CoinJoin creates masternode connections. Every node started the "mncon" thread though it should be spawn only when needed. Plain nodes with wallets disabled no longer carry the thread and its 0.9 MB of thread-local storage.
1 parent 756fc4f commit e4f459d

3 files changed

Lines changed: 13 additions & 4 deletions

File tree

‎src/init.cpp‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2562,6 +2562,9 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
25622562
connOptions.m_peer_connect_timeout = peer_connect_timeout;
25632563
connOptions.socketEventsMode = ::g_socket_events_mode;
25642564
connOptions.m_active_masternode = node.active_ctx != nullptr;
2565+
// wallets may mix with CoinJoin, which connects to the mixing masternode
2566+
connOptions.m_masternode_connections = node.active_ctx != nullptr || quorums_watch ||
2567+
node.wallet_loader != nullptr;
25652568

25662569
// Port to bind to if `-bind=addr` is provided without a `:port` suffix.
25672570
const uint16_t default_bind_port =

‎src/net.cpp‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4093,10 +4093,11 @@ bool CConnman::Start(CDeterministicMNManager& dmnman, CMasternodeMetaMan& mn_met
40934093
[this, connect = connOptions.m_specified_outgoing, &dmnman] { ThreadOpenConnections(connect, dmnman); });
40944094
}
40954095

4096-
// Initiate masternode connections
4097-
threadOpenMasternodeConnections = std::thread(&util::TraceThread, "mncon", [this, &dmnman, &mn_metaman, &mn_sync] {
4098-
ThreadOpenMasternodeConnections(dmnman, mn_metaman, mn_sync);
4099-
});
4096+
if (m_masternode_connections) {
4097+
threadOpenMasternodeConnections = std::thread(&util::TraceThread, "mncon", [this, &dmnman, &mn_metaman, &mn_sync] {
4098+
ThreadOpenMasternodeConnections(dmnman, mn_metaman, mn_sync);
4099+
});
4100+
}
41004101

41014102
// Process messages
41024103
threadMessageHandler = std::thread(&util::TraceThread, "msghand", [this] { ThreadMessageHandler(); });

‎src/net.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1245,6 +1245,9 @@ friend class CNode;
12451245
SocketEventsMode socketEventsMode = SocketEventsMode::Select;
12461246
bool m_i2p_accept_incoming;
12471247
bool m_active_masternode = false;
1248+
//! Run the thread that opens connections to masternodes; only masternodes, quorum watchers and
1249+
//! CoinJoin mixing ever request them.
1250+
bool m_masternode_connections = true;
12481251
};
12491252

12501253
void Init(const Options& connOptions) EXCLUSIVE_LOCKS_REQUIRED(!m_added_nodes_mutex, !m_total_bytes_sent_mutex)
@@ -1283,6 +1286,7 @@ friend class CNode;
12831286
socketEventsMode = connOptions.socketEventsMode;
12841287
m_onion_binds = connOptions.onion_binds;
12851288
m_active_masternode = connOptions.m_active_masternode;
1289+
m_masternode_connections = connOptions.m_masternode_connections;
12861290
}
12871291

12881292
CConnman(uint64_t seed0, uint64_t seed1, AddrMan& addrman, const NetGroupManager& netgroupman,
@@ -1928,6 +1932,7 @@ friend class CNode;
19281932

19291933
/** Flag for activating masternode mode */
19301934
bool m_active_masternode{false};
1935+
bool m_masternode_connections{true};
19311936

19321937
SocketEventsMode socketEventsMode;
19331938
std::unique_ptr<EdgeTriggeredEvents> m_edge_trig_events{nullptr};

0 commit comments

Comments
 (0)