refactor: drop global coinJoinWalletManager

This commit is contained in:
UdjinM6 2024-01-02 18:28:46 +03:00 committed by pasta
parent 7fd30b5203
commit dc5152bd74
No known key found for this signature in database
GPG Key ID: 52527BEDABE87984
14 changed files with 30 additions and 46 deletions

View File

@ -29,8 +29,6 @@
#include <memory> #include <memory>
#include <univalue.h> #include <univalue.h>
std::unique_ptr<CoinJoinWalletManager> coinJoinWalletManager;
void CCoinJoinClientQueueManager::ProcessMessage(const CNode& peer, PeerManager& peerman, std::string_view msg_type, CDataStream& vRecv) void CCoinJoinClientQueueManager::ProcessMessage(const CNode& peer, PeerManager& peerman, std::string_view msg_type, CDataStream& vRecv)
{ {
if (fMasternodeMode) return; if (fMasternodeMode) return;

View File

@ -29,9 +29,6 @@ class UniValue;
using CDeterministicMNCPtr = std::shared_ptr<const CDeterministicMN>; using CDeterministicMNCPtr = std::shared_ptr<const CDeterministicMN>;
// The main object for accessing mixing
extern std::unique_ptr<CoinJoinWalletManager> coinJoinWalletManager;
class CPendingDsaRequest class CPendingDsaRequest
{ {
private: private:

View File

@ -5,8 +5,8 @@
#include <coinjoin/context.h> #include <coinjoin/context.h>
#include <net.h> #include <net.h>
#include <policy/fees.h>
#include <txmempool.h> #include <txmempool.h>
#include <validation.h>
#ifdef ENABLE_WALLET #ifdef ENABLE_WALLET
#include <coinjoin/client.h> #include <coinjoin/client.h>
@ -15,20 +15,10 @@
CJContext::CJContext(CChainState& chainstate, CConnman& connman, CTxMemPool& mempool, const CMasternodeSync& mn_sync, bool relay_txes) : CJContext::CJContext(CChainState& chainstate, CConnman& connman, CTxMemPool& mempool, const CMasternodeSync& mn_sync, bool relay_txes) :
#ifdef ENABLE_WALLET #ifdef ENABLE_WALLET
walletman { walletman{std::make_unique<CoinJoinWalletManager>(connman, mempool, mn_sync, queueman)},
[&]() -> CoinJoinWalletManager* const {
assert(::coinJoinWalletManager == nullptr);
::coinJoinWalletManager = std::make_unique<CoinJoinWalletManager>(connman, mempool, mn_sync, queueman);
return ::coinJoinWalletManager.get();
}()
},
queueman {relay_txes ? std::make_unique<CCoinJoinClientQueueManager>(connman, *walletman, mn_sync) : nullptr}, queueman {relay_txes ? std::make_unique<CCoinJoinClientQueueManager>(connman, *walletman, mn_sync) : nullptr},
#endif // ENABLE_WALLET #endif // ENABLE_WALLET
server{std::make_unique<CCoinJoinServer>(chainstate, connman, mempool, mn_sync)} server{std::make_unique<CCoinJoinServer>(chainstate, connman, mempool, mn_sync)}
{} {}
CJContext::~CJContext() { CJContext::~CJContext() {}
#ifdef ENABLE_WALLET
::coinJoinWalletManager.reset();
#endif // ENABLE_WALLET
}

View File

@ -30,7 +30,8 @@ struct CJContext {
~CJContext(); ~CJContext();
#ifdef ENABLE_WALLET #ifdef ENABLE_WALLET
CoinJoinWalletManager* const walletman; // The main object for accessing mixing
const std::unique_ptr<CoinJoinWalletManager> walletman;
const std::unique_ptr<CCoinJoinClientQueueManager> queueman; const std::unique_ptr<CCoinJoinClientQueueManager> queueman;
#endif // ENABLE_WALLET #endif // ENABLE_WALLET
const std::unique_ptr<CCoinJoinServer> server; const std::unique_ptr<CCoinJoinServer> server;

View File

@ -81,7 +81,12 @@ public:
} }
std::unique_ptr<interfaces::CoinJoin::Client> GetClient(const std::string& name) override std::unique_ptr<interfaces::CoinJoin::Client> GetClient(const std::string& name) override
{ {
return interfaces::MakeCoinJoinClient(m_walletman, name); auto clientman = m_walletman.Get(name);
return clientman ? std::make_unique<CoinJoinClientImpl>(*clientman) : nullptr;
}
CoinJoinWalletManager& walletman() override
{
return m_walletman;
} }
}; };
@ -89,10 +94,5 @@ public:
} // namespace coinjoin } // namespace coinjoin
namespace interfaces { namespace interfaces {
std::unique_ptr<CoinJoin::Client> MakeCoinJoinClient(const CoinJoinWalletManager& walletman, const std::string& name)
{
auto clientman = walletman.Get(name);
return clientman ? std::make_unique<coinjoin::CoinJoinClientImpl>(*clientman) : nullptr;
}
std::unique_ptr<CoinJoin::Loader> MakeCoinJoinLoader(CoinJoinWalletManager& walletman) { return std::make_unique<coinjoin::CoinJoinLoaderImpl>(walletman); } std::unique_ptr<CoinJoin::Loader> MakeCoinJoinLoader(CoinJoinWalletManager& walletman) { return std::make_unique<coinjoin::CoinJoinLoaderImpl>(walletman); }
} // namespace interfaces } // namespace interfaces

View File

@ -38,10 +38,10 @@ public:
virtual void RemoveWallet(const std::string&) = 0; virtual void RemoveWallet(const std::string&) = 0;
virtual void FlushWallet(const std::string&) = 0; virtual void FlushWallet(const std::string&) = 0;
virtual std::unique_ptr<CoinJoin::Client> GetClient(const std::string&) = 0; virtual std::unique_ptr<CoinJoin::Client> GetClient(const std::string&) = 0;
virtual CoinJoinWalletManager& walletman() = 0;
}; };
} // namespace CoinJoin } // namespace CoinJoin
std::unique_ptr<CoinJoin::Client> MakeCoinJoinClient(const CoinJoinWalletManager& walletman, const std::string& name);
std::unique_ptr<CoinJoin::Loader> MakeCoinJoinLoader(CoinJoinWalletManager& walletman); std::unique_ptr<CoinJoin::Loader> MakeCoinJoinLoader(CoinJoinWalletManager& walletman);
} // namespace interfaces } // namespace interfaces

View File

@ -303,7 +303,7 @@ public:
//! Return interface for accessing masternode related handler. //! Return interface for accessing masternode related handler.
virtual Masternode::Sync& masternodeSync() = 0; virtual Masternode::Sync& masternodeSync() = 0;
//! Return interface for accessing coinjoin related handler. //! Return interface for accessing coinjoin options related handler.
virtual CoinJoin::Options& coinJoinOptions() = 0; virtual CoinJoin::Options& coinJoinOptions() = 0;
//! Return interface for accessing coinjoin loader handler. //! Return interface for accessing coinjoin loader handler.

View File

@ -8,7 +8,6 @@
#include <amount.h> // For CAmount #include <amount.h> // For CAmount
#include <fs.h> // For fs::path #include <fs.h> // For fs::path
#include <interfaces/chain.h> // For ChainClient #include <interfaces/chain.h> // For ChainClient
#include <interfaces/coinjoin.h> // For CoinJoin::*
#include <pubkey.h> // For CKeyID and CScriptID (definitions needed in CTxDestination instantiation) #include <pubkey.h> // For CKeyID and CScriptID (definitions needed in CTxDestination instantiation)
#include <script/standard.h> // For CTxDestination #include <script/standard.h> // For CTxDestination
#include <support/allocators/secure.h> // For SecureString #include <support/allocators/secure.h> // For SecureString
@ -48,6 +47,9 @@ struct WalletBalances;
struct WalletTx; struct WalletTx;
struct WalletTxOut; struct WalletTxOut;
struct WalletTxStatus; struct WalletTxStatus;
namespace CoinJoin {
class Loader;
}
using WalletOrderForm = std::vector<std::pair<std::string, std::string>>; using WalletOrderForm = std::vector<std::pair<std::string, std::string>>;
using WalletValueMap = std::map<std::string, std::string>; using WalletValueMap = std::map<std::string, std::string>;
@ -278,8 +280,6 @@ public:
// Return whether private keys enabled. // Return whether private keys enabled.
virtual bool privateKeysDisabled() = 0; virtual bool privateKeysDisabled() = 0;
virtual CoinJoin::Client& coinJoin() = 0;
//! Get max tx fee. //! Get max tx fee.
virtual CAmount getDefaultMaxTxFee() = 0; virtual CAmount getDefaultMaxTxFee() = 0;

View File

@ -14,6 +14,7 @@
#ifdef ENABLE_WALLET #ifdef ENABLE_WALLET
#include <coinjoin/client.h> #include <coinjoin/client.h>
#include <coinjoin/options.h> #include <coinjoin/options.h>
#include <interfaces/coinjoin.h>
#include <wallet/rpcwallet.h> #include <wallet/rpcwallet.h>
#endif // ENABLE_WALLET #endif // ENABLE_WALLET
@ -51,7 +52,8 @@ static UniValue coinjoin(const JSONRPCRequest& request)
} }
} }
auto cj_clientman = ::coinJoinWalletManager->Get(wallet->GetName()); const NodeContext& node = EnsureAnyNodeContext(request.context);
auto cj_clientman = node.coinjoin_loader->walletman().Get(wallet->GetName());
CHECK_NONFATAL(cj_clientman != nullptr); CHECK_NONFATAL(cj_clientman != nullptr);
if (request.params[0].get_str() == "start") { if (request.params[0].get_str() == "start") {
@ -65,7 +67,6 @@ static UniValue coinjoin(const JSONRPCRequest& request)
throw JSONRPCError(RPC_INTERNAL_ERROR, "Mixing has been started already."); throw JSONRPCError(RPC_INTERNAL_ERROR, "Mixing has been started already.");
} }
const NodeContext& node = EnsureAnyNodeContext(request.context);
CTxMemPool& mempool = EnsureMemPool(node); CTxMemPool& mempool = EnsureMemPool(node);
CBlockPolicyEstimator& fee_estimator = EnsureFeeEstimator(node); CBlockPolicyEstimator& fee_estimator = EnsureFeeEstimator(node);
bool result = cj_clientman->DoAutomaticDenominating(*node.connman, fee_estimator, mempool); bool result = cj_clientman->DoAutomaticDenominating(*node.connman, fee_estimator, mempool);
@ -163,7 +164,7 @@ static UniValue getcoinjoininfo(const JSONRPCRequest& request)
return obj; return obj;
} }
auto manager = ::coinJoinWalletManager->Get(wallet->GetName()); auto manager = node.coinjoin_loader->walletman().Get(wallet->GetName());
CHECK_NONFATAL(manager != nullptr); CHECK_NONFATAL(manager != nullptr);
manager->GetJsonInfo(obj); manager->GetJsonInfo(obj);

View File

@ -110,7 +110,7 @@ void DashTestSetup(NodeContext& node)
node.cj_ctx = std::make_unique<CJContext>(chainstate, *node.connman, *node.mempool, *::masternodeSync, /* relay_txes */ true); node.cj_ctx = std::make_unique<CJContext>(chainstate, *node.connman, *node.mempool, *::masternodeSync, /* relay_txes */ true);
#ifdef ENABLE_WALLET #ifdef ENABLE_WALLET
node.coinjoin_loader = interfaces::MakeCoinJoinLoader(*::coinJoinWalletManager); node.coinjoin_loader = interfaces::MakeCoinJoinLoader(*node.cj_ctx->walletman);
#endif // ENABLE_WALLET #endif // ENABLE_WALLET
::deterministicMNManager = std::make_unique<CDeterministicMNManager>(chainstate, *node.connman, *node.evodb); ::deterministicMNManager = std::make_unique<CDeterministicMNManager>(chainstate, *node.connman, *node.evodb);
node.llmq_ctx = std::make_unique<LLMQContext>(chainstate, *node.connman, *node.evodb, *sporkManager, *node.mempool, node.peerman, true, false); node.llmq_ctx = std::make_unique<LLMQContext>(chainstate, *node.connman, *node.evodb, *sporkManager, *node.mempool, node.peerman, true, false);

View File

@ -124,7 +124,7 @@ WalletTxOut MakeWalletTxOut(const CWallet& wallet,
class WalletImpl : public Wallet class WalletImpl : public Wallet
{ {
public: public:
explicit WalletImpl(const std::shared_ptr<CWallet>& wallet, CoinJoinWalletManager& cjwalletman) : m_wallet(wallet), m_cjwalletman(cjwalletman) {} explicit WalletImpl(const std::shared_ptr<CWallet>& wallet) : m_wallet(wallet) {}
void markDirty() override void markDirty() override
{ {
@ -504,11 +504,6 @@ public:
bool hdEnabled() override { return m_wallet->IsHDEnabled(); } bool hdEnabled() override { return m_wallet->IsHDEnabled(); }
bool canGetAddresses() override { return m_wallet->CanGetAddresses(); } bool canGetAddresses() override { return m_wallet->CanGetAddresses(); }
bool privateKeysDisabled() override { return m_wallet->IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS); } bool privateKeysDisabled() override { return m_wallet->IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS); }
interfaces::CoinJoin::Client& coinJoin() override
{
if (m_coinjoin_client == nullptr) m_coinjoin_client = interfaces::MakeCoinJoinClient(m_cjwalletman, m_wallet->GetName());
return *m_coinjoin_client;
}
CAmount getDefaultMaxTxFee() override { return m_wallet->m_default_max_tx_fee; } CAmount getDefaultMaxTxFee() override { return m_wallet->m_default_max_tx_fee; }
void remove() override void remove() override
{ {
@ -558,7 +553,6 @@ public:
CWallet* wallet() override { return m_wallet.get(); } CWallet* wallet() override { return m_wallet.get(); }
std::shared_ptr<CWallet> m_wallet; std::shared_ptr<CWallet> m_wallet;
CoinJoinWalletManager& m_cjwalletman;
std::unique_ptr<interfaces::CoinJoin::Client> m_coinjoin_client; std::unique_ptr<interfaces::CoinJoin::Client> m_coinjoin_client;
}; };
@ -644,7 +638,7 @@ public:
} // namespace wallet } // namespace wallet
namespace interfaces { namespace interfaces {
std::unique_ptr<Wallet> MakeWallet(const std::shared_ptr<CWallet>& wallet) { return wallet ? std::make_unique<wallet::WalletImpl>(wallet, *::coinJoinWalletManager) : nullptr; } std::unique_ptr<Wallet> MakeWallet(const std::shared_ptr<CWallet>& wallet) { return wallet ? std::make_unique<wallet::WalletImpl>(wallet) : nullptr; }
std::unique_ptr<WalletLoader> MakeWalletLoader(Chain& chain, const std::unique_ptr<interfaces::CoinJoin::Loader>& coinjoin_loader, ArgsManager& args) { std::unique_ptr<WalletLoader> MakeWalletLoader(Chain& chain, const std::unique_ptr<interfaces::CoinJoin::Loader>& coinjoin_loader, ArgsManager& args) {
return std::make_unique<wallet::WalletLoaderImpl>(chain, coinjoin_loader, args); return std::make_unique<wallet::WalletLoaderImpl>(chain, coinjoin_loader, args);
} }

View File

@ -7,6 +7,7 @@
#include <amount.h> #include <amount.h>
#include <coinjoin/client.h> #include <coinjoin/client.h>
#include <coinjoin/coinjoin.h> #include <coinjoin/coinjoin.h>
#include <coinjoin/context.h>
#include <coinjoin/options.h> #include <coinjoin/options.h>
#include <coinjoin/util.h> #include <coinjoin/util.h>
#include <node/context.h> #include <node/context.h>
@ -207,8 +208,8 @@ public:
BOOST_FIXTURE_TEST_CASE(coinjoin_manager_start_stop_tests, CTransactionBuilderTestSetup) BOOST_FIXTURE_TEST_CASE(coinjoin_manager_start_stop_tests, CTransactionBuilderTestSetup)
{ {
BOOST_CHECK_EQUAL(::coinJoinWalletManager->raw().size(), 1); BOOST_CHECK_EQUAL(m_node.cj_ctx->walletman->raw().size(), 1);
auto& cj_man = ::coinJoinWalletManager->raw().begin()->second; auto& cj_man = m_node.cj_ctx->walletman->raw().begin()->second;
BOOST_CHECK_EQUAL(cj_man->IsMixing(), false); BOOST_CHECK_EQUAL(cj_man->IsMixing(), false);
BOOST_CHECK_EQUAL(cj_man->StartMixing(), true); BOOST_CHECK_EQUAL(cj_man->StartMixing(), true);
BOOST_CHECK_EQUAL(cj_man->IsMixing(), true); BOOST_CHECK_EQUAL(cj_man->IsMixing(), true);

View File

@ -6,6 +6,7 @@
#define BITCOIN_WALLET_TEST_INIT_TEST_FIXTURE_H #define BITCOIN_WALLET_TEST_INIT_TEST_FIXTURE_H
#include <interfaces/chain.h> #include <interfaces/chain.h>
#include <interfaces/coinjoin.h>
#include <interfaces/wallet.h> #include <interfaces/wallet.h>
#include <node/context.h> #include <node/context.h>
#include <test/util/setup_common.h> #include <test/util/setup_common.h>

View File

@ -11,7 +11,9 @@
#include <vector> #include <vector>
#include <coinjoin/client.h> #include <coinjoin/client.h>
#include <coinjoin/context.h>
#include <interfaces/chain.h> #include <interfaces/chain.h>
#include <interfaces/coinjoin.h>
#include <key_io.h> #include <key_io.h>
#include <node/context.h> #include <node/context.h>
#include <policy/policy.h> #include <policy/policy.h>
@ -1335,8 +1337,7 @@ static size_t CalculateNestedKeyhashInputSize(bool use_max_sig)
node.fee_estimator = std::make_unique<CBlockPolicyEstimator>(); node.fee_estimator = std::make_unique<CBlockPolicyEstimator>();
node.mempool = std::make_unique<CTxMemPool>(node.fee_estimator.get()); node.mempool = std::make_unique<CTxMemPool>(node.fee_estimator.get());
auto chain = interfaces::MakeChain(node); auto chain = interfaces::MakeChain(node);
auto coinjoin_loader = interfaces::MakeCoinJoinLoader(*::coinJoinWalletManager); CWallet wallet(chain.get(), /*coinjoin_loader=*/ nullptr, "", CreateDummyWalletDatabase());
CWallet wallet(chain.get(), coinjoin_loader, "", CreateDummyWalletDatabase());
AddKey(wallet, key); AddKey(wallet, key);
auto spk_man = wallet.GetLegacyScriptPubKeyMan(); auto spk_man = wallet.GetLegacyScriptPubKeyMan();
spk_man->AddCScript(inner_script); spk_man->AddCScript(inner_script);