Line data Source code
1 : // Copyright (c) 2024-2025 The Dash Core developers 2 : // Distributed under the MIT software license, see the accompanying 3 : // file COPYING or http://www.opensource.org/licenses/mit-license.php. 4 : 5 : #include <interfaces/coinjoin.h> 6 : 7 : #include <coinjoin/client.h> 8 : #include <coinjoin/options.h> 9 : #include <coinjoin/walletman.h> 10 : #include <node/context.h> 11 : #include <util/check.h> 12 : #include <walletinitinterface.h> 13 : 14 : #include <memory> 15 : #include <string> 16 : 17 : using node::NodeContext; 18 : using wallet::CWallet; 19 : 20 : namespace coinjoin { 21 : namespace { 22 : 23 : class CoinJoinClientImpl : public interfaces::CoinJoin::Client 24 : { 25 : CCoinJoinClientManager& m_clientman; 26 : 27 : public: 28 25528 : explicit CoinJoinClientImpl(CCoinJoinClientManager& clientman) 29 25528 : : m_clientman(clientman) {} 30 : 31 0 : void resetCachedBlocks() override 32 : { 33 0 : m_clientman.nCachedNumBlocks = std::numeric_limits<int>::max(); 34 0 : } 35 2 : void resetPool() override 36 : { 37 2 : m_clientman.ResetPool(); 38 2 : } 39 0 : void disableAutobackups() override 40 : { 41 0 : m_clientman.fCreateAutoBackups = false; 42 0 : } 43 0 : int getCachedBlocks() override 44 : { 45 0 : return m_clientman.nCachedNumBlocks; 46 : } 47 22 : void getJsonInfo(UniValue& obj) override 48 : { 49 22 : return m_clientman.GetJsonInfo(obj); 50 : } 51 0 : std::string getSessionDenoms() override 52 : { 53 0 : return m_clientman.GetSessionDenoms(); 54 : } 55 2 : std::vector<std::string> getSessionStatuses() override 56 : { 57 2 : return m_clientman.GetStatuses(); 58 : } 59 0 : void setCachedBlocks(int nCachedBlocks) override 60 : { 61 0 : m_clientman.nCachedNumBlocks = nCachedBlocks; 62 0 : } 63 22 : bool isMixing() override 64 : { 65 22 : return m_clientman.IsMixing(); 66 : } 67 6 : bool startMixing() override 68 : { 69 6 : return m_clientman.StartMixing(); 70 : } 71 783 : void stopMixing() override 72 : { 73 783 : m_clientman.StopMixing(); 74 783 : } 75 : }; 76 : 77 : class CoinJoinLoaderImpl : public interfaces::CoinJoin::Loader 78 : { 79 : private: 80 21689 : CJWalletManager& manager() 81 : { 82 21689 : return *Assert(m_node.cj_walletman); 83 : } 84 : 85 6563 : interfaces::WalletLoader& wallet_loader() 86 : { 87 6563 : return *Assert(m_node.wallet_loader); 88 : } 89 : 90 : public: 91 3270 : explicit CoinJoinLoaderImpl(NodeContext& node) : 92 1635 : m_node(node) 93 3270 : { 94 : // Enablement will be re-evaluated when a wallet is added or removed 95 1635 : CCoinJoinClientOptions::SetEnabled(false); 96 3270 : } 97 : 98 4373 : void AddWallet(const std::shared_ptr<CWallet>& wallet) override 99 : { 100 4373 : manager().addWallet(wallet); 101 4373 : g_wallet_init_interface.InitCoinJoinSettings(*this, wallet_loader()); 102 4373 : } 103 2190 : void RemoveWallet(const std::string& name) override 104 : { 105 2190 : manager().removeWallet(name); 106 2190 : g_wallet_init_interface.InitCoinJoinSettings(*this, wallet_loader()); 107 2190 : } 108 1791 : void FlushWallet(const std::string& name) override 109 : { 110 1791 : manager().flushWallet(name); 111 1791 : } 112 13335 : std::unique_ptr<interfaces::CoinJoin::Client> GetClient(const std::string& name) override 113 : { 114 13335 : auto clientman = manager().getClient(name); 115 13335 : return clientman ? std::make_unique<CoinJoinClientImpl>(*clientman) : nullptr; 116 : } 117 : 118 : NodeContext& m_node; 119 : }; 120 : 121 : } // namespace 122 : } // namespace coinjoin 123 : 124 : namespace interfaces { 125 1635 : std::unique_ptr<CoinJoin::Loader> MakeCoinJoinLoader(NodeContext& node) { return std::make_unique<coinjoin::CoinJoinLoaderImpl>(node); } 126 : } // namespace interfaces