Skip to content

Commit

Permalink
refactor: stop using ::Chain{state}Active() in GetUTXO*
Browse files Browse the repository at this point in the history
  • Loading branch information
kwvg committed Jun 26, 2024
1 parent f6f7df3 commit 6abf7f8
Show file tree
Hide file tree
Showing 12 changed files with 40 additions and 36 deletions.
15 changes: 8 additions & 7 deletions src/coinjoin/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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);
}
}
Expand Down Expand Up @@ -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);
Expand Down
13 changes: 7 additions & 6 deletions src/coinjoin/client.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,10 @@ class CoinJoinWalletManager {
using wallet_name_cjman_map = std::map<const std::string, std::unique_ptr<CCoinJoinClientManager>>;

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<CCoinJoinClientQueueManager>& 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() {
Expand All @@ -96,6 +96,7 @@ class CoinJoinWalletManager {
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;
Expand Down Expand Up @@ -181,7 +182,7 @@ class CCoinJoinClientSession : public CCoinJoinBaseSession
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);
Expand Down Expand Up @@ -280,7 +281,7 @@ class CCoinJoinClientManager
bool GetMixingMasternodesInfo(std::vector<CDeterministicMNCPtr>& 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);
Expand All @@ -296,7 +297,7 @@ class CCoinJoinClientManager

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);
};
Expand Down
6 changes: 3 additions & 3 deletions src/coinjoin/coinjoin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 */
Expand All @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion src/coinjoin/coinjoin.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

}

Expand Down
2 changes: 1 addition & 1 deletion src/coinjoin/context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ CJContext::CJContext(CChainState& chainstate, CConnman& connman, CDeterministicM
const std::unique_ptr<PeerManager>& peerman, bool relay_txes) :
dstxman{std::make_unique<CDSTXManager>()},
#ifdef ENABLE_WALLET
walletman{std::make_unique<CoinJoinWalletManager>(connman, dmnman, mn_metaman, mempool, mn_sync, queueman, /* is_masternode = */ mn_activeman != nullptr)},
walletman{std::make_unique<CoinJoinWalletManager>(chainstate, connman, dmnman, mn_metaman, mempool, mn_sync, queueman, /* is_masternode = */ mn_activeman != nullptr)},
queueman {relay_txes ? std::make_unique<CCoinJoinClientQueueManager>(connman, *walletman, dmnman, mn_metaman, mn_sync, /* is_masternode = */ mn_activeman != nullptr) : nullptr},
#endif // ENABLE_WALLET
server{std::make_unique<CCoinJoinServer>(chainstate, connman, dmnman, *dstxman, mn_metaman, mempool, mn_activeman, mn_sync, peerman)}
Expand Down
4 changes: 2 additions & 2 deletions src/coinjoin/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/node/blockstorage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
4 changes: 3 additions & 1 deletion src/rpc/coinjoin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand Down Expand Up @@ -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"));
}

Expand Down
6 changes: 3 additions & 3 deletions src/rpc/evo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/rpc/masternode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
14 changes: 7 additions & 7 deletions src/validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions src/validation.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 6abf7f8

Please sign in to comment.