From 6abf7f8b6394295a152ac5df751cc59a81dbd1a8 Mon Sep 17 00:00:00 2001 From: Kittywhiskers Van Gogh <63189531+kwvg@users.noreply.github.com> Date: Tue, 25 Jun 2024 10:57:20 +0000 Subject: [PATCH] refactor: stop using `::Chain`{`state`}`Active()` in `GetUTXO*` --- src/coinjoin/client.cpp | 15 ++++++++------- src/coinjoin/client.h | 13 +++++++------ src/coinjoin/coinjoin.cpp | 6 +++--- src/coinjoin/coinjoin.h | 2 +- src/coinjoin/context.cpp | 2 +- src/coinjoin/server.cpp | 4 ++-- src/node/blockstorage.cpp | 2 +- src/rpc/coinjoin.cpp | 4 +++- src/rpc/evo.cpp | 6 +++--- src/rpc/masternode.cpp | 2 +- src/validation.cpp | 14 +++++++------- src/validation.h | 6 +++--- 12 files changed, 40 insertions(+), 36 deletions(-) diff --git a/src/coinjoin/client.cpp b/src/coinjoin/client.cpp index 4f35c0b8f0..f1b97baf21 100644 --- a/src/coinjoin/client.cpp +++ b/src/coinjoin/client.cpp @@ -781,7 +781,7 @@ bool CCoinJoinClientManager::CheckAutomaticBackup() // // Passively run mixing in the background to mix funds based on the given configuration. // -bool CCoinJoinClientSession::DoAutomaticDenominating(CConnman& connman, CTxMemPool& mempool, bool fDryRun) +bool CCoinJoinClientSession::DoAutomaticDenominating(CChainState& active_chainstate, CConnman& connman, CTxMemPool& mempool, bool fDryRun) { if (m_is_masternode) return false; // no client-side mixing on masternodes if (nState != POOL_STATE_IDLE) return false; @@ -934,7 +934,7 @@ bool CCoinJoinClientSession::DoAutomaticDenominating(CConnman& connman, CTxMemPo return false; } } else { - if (!CoinJoin::IsCollateralValid(mempool, CTransaction(txMyCollateral))) { + if (!CoinJoin::IsCollateralValid(active_chainstate, mempool, CTransaction(txMyCollateral))) { WalletCJLogPrint(m_wallet, "CCoinJoinClientSession::DoAutomaticDenominating -- invalid collateral, recreating...\n"); if (!CreateCollateralTransaction(txMyCollateral, strReason)) { WalletCJLogPrint(m_wallet, "CCoinJoinClientSession::DoAutomaticDenominating -- create collateral error: %s\n", strReason); @@ -961,7 +961,7 @@ bool CCoinJoinClientSession::DoAutomaticDenominating(CConnman& connman, CTxMemPo return false; } -bool CCoinJoinClientManager::DoAutomaticDenominating(CConnman& connman, CTxMemPool& mempool, bool fDryRun) +bool CCoinJoinClientManager::DoAutomaticDenominating(CChainState& active_chainstate, CConnman& connman, CTxMemPool& mempool, bool fDryRun) { if (m_is_masternode) return false; // no client-side mixing on masternodes if (!CCoinJoinClientOptions::IsEnabled() || !IsMixing()) return false; @@ -1003,7 +1003,7 @@ bool CCoinJoinClientManager::DoAutomaticDenominating(CConnman& connman, CTxMemPo return false; } - fResult &= session.DoAutomaticDenominating(connman, mempool, fDryRun); + fResult &= session.DoAutomaticDenominating(active_chainstate, connman, mempool, fDryRun); } return fResult; @@ -1840,7 +1840,7 @@ void CCoinJoinClientQueueManager::DoMaintenance() CheckQueue(); } -void CCoinJoinClientManager::DoMaintenance(CConnman& connman, CTxMemPool& mempool) +void CCoinJoinClientManager::DoMaintenance(CChainState& active_chainstate, CConnman& connman, CTxMemPool& mempool) { if (!CCoinJoinClientOptions::IsEnabled()) return; if (m_is_masternode) return; // no client-side mixing on masternodes @@ -1854,7 +1854,7 @@ void CCoinJoinClientManager::DoMaintenance(CConnman& connman, CTxMemPool& mempoo CheckTimeout(); ProcessPendingDsaRequest(connman); if (nDoAutoNextRun == nTick) { - DoAutomaticDenominating(connman, mempool); + DoAutomaticDenominating(active_chainstate, connman, mempool); nDoAutoNextRun = nTick + COINJOIN_AUTO_TIMEOUT_MIN + GetRandInt(COINJOIN_AUTO_TIMEOUT_MAX - COINJOIN_AUTO_TIMEOUT_MIN); } } @@ -1901,9 +1901,10 @@ void CoinJoinWalletManager::Add(CWallet& wallet) { void CoinJoinWalletManager::DoMaintenance() { for (auto& [wallet_str, walletman] : m_wallet_manager_map) { - walletman->DoMaintenance(m_connman, m_mempool); + walletman->DoMaintenance(m_chainstate, m_connman, m_mempool); } } + void CoinJoinWalletManager::Remove(const std::string& name) { 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 40a633fc48..e98e11d3d1 100644 --- a/src/coinjoin/client.h +++ b/src/coinjoin/client.h @@ -73,10 +73,10 @@ public: using wallet_name_cjman_map = std::map>; public: - CoinJoinWalletManager(CConnman& connman, CDeterministicMNManager& dmnman, CMasternodeMetaMan& mn_metaman, CTxMemPool& mempool, + CoinJoinWalletManager(CChainState& chainstate, CConnman& connman, CDeterministicMNManager& dmnman, CMasternodeMetaMan& mn_metaman, CTxMemPool& mempool, const CMasternodeSync& mn_sync, const std::unique_ptr& queueman, bool is_masternode) - : m_connman(connman), m_dmnman(dmnman), m_mn_metaman(mn_metaman), m_mempool(mempool), m_mn_sync(mn_sync), m_queueman(queueman), - m_is_masternode{is_masternode} + : m_chainstate(chainstate), m_connman(connman), m_dmnman(dmnman), m_mn_metaman(mn_metaman), m_mempool(mempool), m_mn_sync(mn_sync), + m_queueman(queueman), m_is_masternode{is_masternode} {} ~CoinJoinWalletManager() { @@ -96,6 +96,7 @@ public: const wallet_name_cjman_map& raw() const { return m_wallet_manager_map; } private: + CChainState& m_chainstate; CConnman& m_connman; CDeterministicMNManager& m_dmnman; CMasternodeMetaMan& m_mn_metaman; @@ -181,7 +182,7 @@ public: bool GetMixingMasternodeInfo(CDeterministicMNCPtr& ret) const; /// Passively run mixing in the background according to the configuration in settings - bool DoAutomaticDenominating(CConnman& connman, CTxMemPool& mempool, bool fDryRun = false) EXCLUSIVE_LOCKS_REQUIRED(!cs_coinjoin); + bool DoAutomaticDenominating(CChainState& active_chainstate, CConnman& connman, CTxMemPool& mempool, bool fDryRun = false) EXCLUSIVE_LOCKS_REQUIRED(!cs_coinjoin); /// As a client, submit part of a future mixing transaction to a Masternode to start the process bool SubmitDenominate(CConnman& connman); @@ -280,7 +281,7 @@ public: bool GetMixingMasternodesInfo(std::vector& vecDmnsRet) const EXCLUSIVE_LOCKS_REQUIRED(!cs_deqsessions); /// Passively run mixing in the background according to the configuration in settings - bool DoAutomaticDenominating(CConnman& connman, CTxMemPool& mempool, bool fDryRun = false) EXCLUSIVE_LOCKS_REQUIRED(!cs_deqsessions); + bool DoAutomaticDenominating(CChainState& active_chainstate, CConnman& connman, CTxMemPool& mempool, bool fDryRun = false) EXCLUSIVE_LOCKS_REQUIRED(!cs_deqsessions); bool TrySubmitDenominate(const CService& mnAddr, CConnman& connman) EXCLUSIVE_LOCKS_REQUIRED(!cs_deqsessions); bool MarkAlreadyJoinedQueueAsTried(CCoinJoinQueue& dsq) const EXCLUSIVE_LOCKS_REQUIRED(!cs_deqsessions); @@ -296,7 +297,7 @@ public: void UpdatedBlockTip(const CBlockIndex* pindex); - void DoMaintenance(CConnman& connman, CTxMemPool& mempool) EXCLUSIVE_LOCKS_REQUIRED(!cs_deqsessions); + void DoMaintenance(CChainState& active_chainstate, CConnman& connman, CTxMemPool& mempool) EXCLUSIVE_LOCKS_REQUIRED(!cs_deqsessions); void GetJsonInfo(UniValue& obj) const EXCLUSIVE_LOCKS_REQUIRED(!cs_deqsessions); }; diff --git a/src/coinjoin/coinjoin.cpp b/src/coinjoin/coinjoin.cpp index 8ab741e28f..8d26721efc 100644 --- a/src/coinjoin/coinjoin.cpp +++ b/src/coinjoin/coinjoin.cpp @@ -322,7 +322,7 @@ bool ATMPIfSaneFee(CChainState& active_chainstate, CTxMemPool& pool, const CTran } // check to make sure the collateral provided by the client is valid -bool CoinJoin::IsCollateralValid(CTxMemPool& mempool, const CTransaction& txCollateral) +bool CoinJoin::IsCollateralValid(CChainState& active_chainstate, CTxMemPool& mempool, const CTransaction& txCollateral) { if (txCollateral.vout.empty()) return false; if (txCollateral.nLockTime != 0) return false; @@ -348,7 +348,7 @@ bool CoinJoin::IsCollateralValid(CTxMemPool& mempool, const CTransaction& txColl return false; } nValueIn += mempoolTx->vout[txin.prevout.n].nValue; - } else if (GetUTXOCoin(txin.prevout, coin)) { + } else if (GetUTXOCoin(active_chainstate, txin.prevout, coin)) { nValueIn += coin.out.nValue; } else { LogPrint(BCLog::COINJOIN, "CoinJoin::IsCollateralValid -- Unknown inputs in collateral transaction, txCollateral=%s", txCollateral.ToString()); /* Continued */ @@ -366,7 +366,7 @@ bool CoinJoin::IsCollateralValid(CTxMemPool& mempool, const CTransaction& txColl { LOCK(cs_main); - if (!ATMPIfSaneFee(::ChainstateActive(), mempool, MakeTransactionRef(txCollateral), /*test_accept=*/true)) { + if (!ATMPIfSaneFee(active_chainstate, mempool, MakeTransactionRef(txCollateral), /*test_accept=*/true)) { LogPrint(BCLog::COINJOIN, "CoinJoin::IsCollateralValid -- didn't pass AcceptToMemoryPool()\n"); return false; } diff --git a/src/coinjoin/coinjoin.h b/src/coinjoin/coinjoin.h index 06dd934a83..0c97984726 100644 --- a/src/coinjoin/coinjoin.h +++ b/src/coinjoin/coinjoin.h @@ -354,7 +354,7 @@ namespace CoinJoin constexpr CAmount GetMaxPoolAmount() { return COINJOIN_ENTRY_MAX_SIZE * vecStandardDenominations.front(); } /// If the collateral is valid given by a client - bool IsCollateralValid(CTxMemPool& mempool, const CTransaction& txCollateral); + bool IsCollateralValid(CChainState& active_chainstate, CTxMemPool& mempool, const CTransaction& txCollateral); } diff --git a/src/coinjoin/context.cpp b/src/coinjoin/context.cpp index f8a47466cf..c6f24fe9cb 100644 --- a/src/coinjoin/context.cpp +++ b/src/coinjoin/context.cpp @@ -14,7 +14,7 @@ CJContext::CJContext(CChainState& chainstate, CConnman& connman, CDeterministicM const std::unique_ptr& peerman, bool relay_txes) : dstxman{std::make_unique()}, #ifdef ENABLE_WALLET - walletman{std::make_unique(connman, dmnman, mn_metaman, mempool, mn_sync, queueman, /* is_masternode = */ mn_activeman != nullptr)}, + walletman{std::make_unique(chainstate, connman, dmnman, mn_metaman, mempool, mn_sync, queueman, /* is_masternode = */ mn_activeman != nullptr)}, queueman {relay_txes ? std::make_unique(connman, *walletman, dmnman, mn_metaman, mn_sync, /* is_masternode = */ mn_activeman != nullptr) : nullptr}, #endif // ENABLE_WALLET server{std::make_unique(chainstate, connman, dmnman, *dstxman, mn_metaman, mempool, mn_activeman, mn_sync, peerman)} diff --git a/src/coinjoin/server.cpp b/src/coinjoin/server.cpp index a3b7a8c61e..801bb38686 100644 --- a/src/coinjoin/server.cpp +++ b/src/coinjoin/server.cpp @@ -574,7 +574,7 @@ bool CCoinJoinServer::AddEntry(const CCoinJoinEntry& entry, PoolMessage& nMessag return false; } - if (!CoinJoin::IsCollateralValid(mempool, *entry.txCollateral)) { + if (!CoinJoin::IsCollateralValid(m_chainstate, mempool, *entry.txCollateral)) { LogPrint(BCLog::COINJOIN, "CCoinJoinServer::%s -- ERROR: collateral not valid!\n", __func__); nMessageIDRet = ERR_INVALID_COLLATERAL; return false; @@ -685,7 +685,7 @@ bool CCoinJoinServer::IsAcceptableDSA(const CCoinJoinAccept& dsa, PoolMessage& n } // check collateral - if (!fUnitTest && !CoinJoin::IsCollateralValid(mempool, CTransaction(dsa.txCollateral))) { + if (!fUnitTest && !CoinJoin::IsCollateralValid(m_chainstate, mempool, CTransaction(dsa.txCollateral))) { LogPrint(BCLog::COINJOIN, "CCoinJoinServer::%s -- collateral not valid!\n", __func__); nMessageIDRet = ERR_INVALID_COLLATERAL; return false; diff --git a/src/node/blockstorage.cpp b/src/node/blockstorage.cpp index 9decd636f8..a0ea4ab33b 100644 --- a/src/node/blockstorage.cpp +++ b/src/node/blockstorage.cpp @@ -204,7 +204,7 @@ void ThreadImport(ChainstateManager& chainman, CDeterministicMNManager& dmnman, auto mnList = dmnman.GetListAtChainTip(); mnList.ForEachMN(false, [&](auto& dmn) { Coin coin; - GetUTXOCoin(dmn.collateralOutpoint, coin); + GetUTXOCoin(chainman.ActiveChainstate(), dmn.collateralOutpoint, coin); }); LogPrintf("Filling coin cache with masternode UTXOs: done in %dms\n", GetTimeMillis() - nStart); } diff --git a/src/rpc/coinjoin.cpp b/src/rpc/coinjoin.cpp index 0d5d0ad555..0c03b56b34 100644 --- a/src/rpc/coinjoin.cpp +++ b/src/rpc/coinjoin.cpp @@ -40,6 +40,7 @@ static RPCHelpMan coinjoin() if (!wallet) return NullUniValue; const NodeContext& node = EnsureAnyNodeContext(request.context); + if (node.mn_activeman) { throw JSONRPCError(RPC_INTERNAL_ERROR, "Client-side mixing is not supported on masternodes"); } @@ -69,9 +70,10 @@ static RPCHelpMan coinjoin() throw JSONRPCError(RPC_INTERNAL_ERROR, "Mixing has been started already."); } + ChainstateManager& chainman = EnsureChainman(node); CTxMemPool& mempool = EnsureMemPool(node); CConnman& connman = EnsureConnman(node); - bool result = cj_clientman->DoAutomaticDenominating(connman, mempool); + bool result = cj_clientman->DoAutomaticDenominating(chainman.ActiveChainstate(), connman, mempool); return "Mixing " + (result ? "started successfully" : ("start failed: " + cj_clientman->GetStatuses().original + ", will retry")); } diff --git a/src/rpc/evo.cpp b/src/rpc/evo.cpp index 8d7a8ca232..25c9c89d2a 100644 --- a/src/rpc/evo.cpp +++ b/src/rpc/evo.cpp @@ -756,7 +756,7 @@ static UniValue protx_register_common_wrapper(const JSONRPCRequest& request, FundSpecialTx(wallet.get(), tx, ptx, fundDest); UpdateSpecialTxInputsHash(tx, ptx); Coin coin; - if (!GetUTXOCoin(ptx.collateralOutpoint, coin)) { + if (!GetUTXOCoin(chainman.ActiveChainstate(), ptx.collateralOutpoint, coin)) { throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, strprintf("collateral not found: %s", ptx.collateralOutpoint.ToStringShort())); } CTxDestination txDest; @@ -1286,7 +1286,7 @@ static UniValue BuildDMNListEntry(const CWallet* const pwallet, const CDetermini UniValue o = dmn.ToJson(); CTransactionRef collateralTx{nullptr}; - int confirmations = GetUTXOConfirmations(dmn.collateralOutpoint); + int confirmations = GetUTXOConfirmations(chainman.ActiveChainstate(), dmn.collateralOutpoint); if (pindex != nullptr) { if (confirmations > -1) { @@ -1307,7 +1307,7 @@ static UniValue BuildDMNListEntry(const CWallet* const pwallet, const CDetermini bool hasVotingKey = CheckWalletOwnsKey(pwallet, dmn.pdmnState->keyIDVoting); bool ownsCollateral = false; - if (Coin coin; GetUTXOCoin(dmn.collateralOutpoint, coin)) { + if (Coin coin; GetUTXOCoin(chainman.ActiveChainstate(), dmn.collateralOutpoint, coin)) { ownsCollateral = CheckWalletOwnsScript(pwallet, coin.out.scriptPubKey); } else if (collateralTx != nullptr) { ownsCollateral = CheckWalletOwnsScript(pwallet, collateralTx->vout[dmn.collateralOutpoint.n].scriptPubKey); diff --git a/src/rpc/masternode.cpp b/src/rpc/masternode.cpp index 253a9c80fd..dd58dbfd4d 100644 --- a/src/rpc/masternode.cpp +++ b/src/rpc/masternode.cpp @@ -595,7 +595,7 @@ static RPCHelpMan masternodelist_helper(bool is_composite) std::string strOutpoint = dmn.collateralOutpoint.ToStringShort(); Coin coin; std::string collateralAddressStr = "UNKNOWN"; - if (GetUTXOCoin(dmn.collateralOutpoint, coin)) { + if (GetUTXOCoin(chainman.ActiveChainstate(), dmn.collateralOutpoint, coin)) { CTxDestination collateralDest; if (ExtractDestination(coin.out.scriptPubKey, collateralDest)) { collateralAddressStr = EncodeDestination(collateralDest); diff --git a/src/validation.cpp b/src/validation.cpp index f8e4a4acad..532bbfd1c1 100644 --- a/src/validation.cpp +++ b/src/validation.cpp @@ -344,29 +344,29 @@ bool CheckSequenceLocks(CBlockIndex* tip, return EvaluateSequenceLocks(index, lockPair); } -bool GetUTXOCoin(const COutPoint& outpoint, Coin& coin) +bool GetUTXOCoin(CChainState& active_chainstate, const COutPoint& outpoint, Coin& coin) { LOCK(cs_main); - if (!::ChainstateActive().CoinsTip().GetCoin(outpoint, coin)) + if (!active_chainstate.CoinsTip().GetCoin(outpoint, coin)) return false; if (coin.IsSpent()) return false; return true; } -int GetUTXOHeight(const COutPoint& outpoint) +int GetUTXOHeight(CChainState& active_chainstate, const COutPoint& outpoint) { // -1 means UTXO is yet unknown or already spent Coin coin; - return GetUTXOCoin(outpoint, coin) ? coin.nHeight : -1; + return GetUTXOCoin(active_chainstate, outpoint, coin) ? coin.nHeight : -1; } -int GetUTXOConfirmations(const COutPoint& outpoint) +int GetUTXOConfirmations(CChainState& active_chainstate, const COutPoint& outpoint) { // -1 means UTXO is yet unknown or already spent LOCK(cs_main); - int nPrevoutHeight = GetUTXOHeight(outpoint); - return (nPrevoutHeight > -1 && ::ChainActive().Tip()) ? ::ChainActive().Height() - nPrevoutHeight + 1 : -1; + int nPrevoutHeight = GetUTXOHeight(active_chainstate, outpoint); + return (nPrevoutHeight > -1 && active_chainstate.m_chain.Tip()) ? active_chainstate.m_chain.Height() - nPrevoutHeight + 1 : -1; } static bool ContextualCheckTransaction(const CTransaction& tx, TxValidationState& state, const Consensus::Params& consensusParams, const CBlockIndex* pindexPrev) diff --git a/src/validation.h b/src/validation.h index 293ba4191e..39ba3e2e1a 100644 --- a/src/validation.h +++ b/src/validation.h @@ -294,9 +294,9 @@ PackageMempoolAcceptResult ProcessNewPackage(CChainState& active_chainstate, CTx const Package& txns, bool test_accept) EXCLUSIVE_LOCKS_REQUIRED(cs_main); -bool GetUTXOCoin(const COutPoint& outpoint, Coin& coin); -int GetUTXOHeight(const COutPoint& outpoint); -int GetUTXOConfirmations(const COutPoint& outpoint); +bool GetUTXOCoin(CChainState& active_chainstate, const COutPoint& outpoint, Coin& coin); +int GetUTXOHeight(CChainState& active_chainstate, const COutPoint& outpoint); +int GetUTXOConfirmations(CChainState& active_chainstate, const COutPoint& outpoint); /** Apply the effects of this transaction on the UTXO set represented by view */ void UpdateCoins(const CTransaction& tx, CCoinsViewCache& inputs, int nHeight);