diff --git a/src/coinjoin/client.cpp b/src/coinjoin/client.cpp index 1675a6408e831b..aa51ccb64a9cf1 100644 --- a/src/coinjoin/client.cpp +++ b/src/coinjoin/client.cpp @@ -1895,6 +1895,8 @@ void CCoinJoinClientManager::GetJsonInfo(UniValue& obj) const } void CoinJoinWalletManager::Add(CWallet& wallet) { + AssertLockHeld(cs_wallet_manager_map); + m_wallet_manager_map.try_emplace( wallet.GetName(), std::make_unique(wallet, *this, m_dmnman, m_mn_metaman, m_mn_sync, m_queueman, m_is_masternode) @@ -1903,12 +1905,17 @@ void CoinJoinWalletManager::Add(CWallet& wallet) { } void CoinJoinWalletManager::DoMaintenance() { + AssertLockNotHeld(cs_wallet_manager_map); + LOCK(cs_wallet_manager_map); + for (auto& [wallet_str, walletman] : m_wallet_manager_map) { walletman->DoMaintenance(m_chainstate, m_connman, m_mempool); } } void CoinJoinWalletManager::Remove(const std::string& name) { + AssertLockHeld(cs_wallet_manager_map); + m_wallet_manager_map.erase(name); g_wallet_init_interface.InitCoinJoinSettings(*this); } diff --git a/src/coinjoin/client.h b/src/coinjoin/client.h index ba77e565cf2645..15cd24489218bc 100644 --- a/src/coinjoin/client.h +++ b/src/coinjoin/client.h @@ -72,6 +72,8 @@ class CoinJoinWalletManager { public: using wallet_name_cjman_map = std::map>; + Mutex cs_wallet_manager_map; + public: CoinJoinWalletManager(CChainState& chainstate, CConnman& connman, CDeterministicMNManager& dmnman, CMasternodeMetaMan& mn_metaman, CTxMemPool& mempool, const CMasternodeSync& mn_sync, const std::unique_ptr& queueman, bool is_masternode) @@ -85,11 +87,11 @@ class CoinJoinWalletManager { } } - void Add(CWallet& wallet); - void DoMaintenance(); + void Add(CWallet& wallet) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet_manager_map); + void DoMaintenance() EXCLUSIVE_LOCKS_REQUIRED(!cs_wallet_manager_map); - void Remove(const std::string& name); - void Flush(const std::string& name); + void Remove(const std::string& name) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet_manager_map); + void Flush(const std::string& name) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet_manager_map); CCoinJoinClientManager* Get(const std::string& name) const; @@ -105,7 +107,7 @@ class CoinJoinWalletManager { const std::unique_ptr& m_queueman; const bool m_is_masternode; - wallet_name_cjman_map m_wallet_manager_map; + wallet_name_cjman_map m_wallet_manager_map GUARDED_BY(cs_wallet_manager_map); }; class CCoinJoinClientSession : public CCoinJoinBaseSession diff --git a/src/coinjoin/interfaces.cpp b/src/coinjoin/interfaces.cpp index 78a97194319ba6..5d019c4a25b9f5 100644 --- a/src/coinjoin/interfaces.cpp +++ b/src/coinjoin/interfaces.cpp @@ -69,15 +69,15 @@ class CoinJoinLoaderImpl : public interfaces::CoinJoin::Loader void AddWallet(CWallet& wallet) override { - m_walletman.Add(wallet); + WITH_LOCK(m_walletman.cs_wallet_manager_map, m_walletman.Add(wallet)); } void RemoveWallet(const std::string& name) override { - m_walletman.Remove(name); + WITH_LOCK(m_walletman.cs_wallet_manager_map, m_walletman.Remove(name)); } void FlushWallet(const std::string& name) override { - m_walletman.Flush(name); + WITH_LOCK(m_walletman.cs_wallet_manager_map, m_walletman.Flush(name)); } std::unique_ptr GetClient(const std::string& name) override {