Skip to content

Commit 6abf7f8

Browse files
committed
refactor: stop using ::Chain{state}Active() in GetUTXO*
1 parent f6f7df3 commit 6abf7f8

12 files changed

+40
-36
lines changed

src/coinjoin/client.cpp

+8-7
Original file line numberDiff line numberDiff line change
@@ -781,7 +781,7 @@ bool CCoinJoinClientManager::CheckAutomaticBackup()
781781
//
782782
// Passively run mixing in the background to mix funds based on the given configuration.
783783
//
784-
bool CCoinJoinClientSession::DoAutomaticDenominating(CConnman& connman, CTxMemPool& mempool, bool fDryRun)
784+
bool CCoinJoinClientSession::DoAutomaticDenominating(CChainState& active_chainstate, CConnman& connman, CTxMemPool& mempool, bool fDryRun)
785785
{
786786
if (m_is_masternode) return false; // no client-side mixing on masternodes
787787
if (nState != POOL_STATE_IDLE) return false;
@@ -934,7 +934,7 @@ bool CCoinJoinClientSession::DoAutomaticDenominating(CConnman& connman, CTxMemPo
934934
return false;
935935
}
936936
} else {
937-
if (!CoinJoin::IsCollateralValid(mempool, CTransaction(txMyCollateral))) {
937+
if (!CoinJoin::IsCollateralValid(active_chainstate, mempool, CTransaction(txMyCollateral))) {
938938
WalletCJLogPrint(m_wallet, "CCoinJoinClientSession::DoAutomaticDenominating -- invalid collateral, recreating...\n");
939939
if (!CreateCollateralTransaction(txMyCollateral, strReason)) {
940940
WalletCJLogPrint(m_wallet, "CCoinJoinClientSession::DoAutomaticDenominating -- create collateral error: %s\n", strReason);
@@ -961,7 +961,7 @@ bool CCoinJoinClientSession::DoAutomaticDenominating(CConnman& connman, CTxMemPo
961961
return false;
962962
}
963963

964-
bool CCoinJoinClientManager::DoAutomaticDenominating(CConnman& connman, CTxMemPool& mempool, bool fDryRun)
964+
bool CCoinJoinClientManager::DoAutomaticDenominating(CChainState& active_chainstate, CConnman& connman, CTxMemPool& mempool, bool fDryRun)
965965
{
966966
if (m_is_masternode) return false; // no client-side mixing on masternodes
967967
if (!CCoinJoinClientOptions::IsEnabled() || !IsMixing()) return false;
@@ -1003,7 +1003,7 @@ bool CCoinJoinClientManager::DoAutomaticDenominating(CConnman& connman, CTxMemPo
10031003
return false;
10041004
}
10051005

1006-
fResult &= session.DoAutomaticDenominating(connman, mempool, fDryRun);
1006+
fResult &= session.DoAutomaticDenominating(active_chainstate, connman, mempool, fDryRun);
10071007
}
10081008

10091009
return fResult;
@@ -1840,7 +1840,7 @@ void CCoinJoinClientQueueManager::DoMaintenance()
18401840
CheckQueue();
18411841
}
18421842

1843-
void CCoinJoinClientManager::DoMaintenance(CConnman& connman, CTxMemPool& mempool)
1843+
void CCoinJoinClientManager::DoMaintenance(CChainState& active_chainstate, CConnman& connman, CTxMemPool& mempool)
18441844
{
18451845
if (!CCoinJoinClientOptions::IsEnabled()) return;
18461846
if (m_is_masternode) return; // no client-side mixing on masternodes
@@ -1854,7 +1854,7 @@ void CCoinJoinClientManager::DoMaintenance(CConnman& connman, CTxMemPool& mempoo
18541854
CheckTimeout();
18551855
ProcessPendingDsaRequest(connman);
18561856
if (nDoAutoNextRun == nTick) {
1857-
DoAutomaticDenominating(connman, mempool);
1857+
DoAutomaticDenominating(active_chainstate, connman, mempool);
18581858
nDoAutoNextRun = nTick + COINJOIN_AUTO_TIMEOUT_MIN + GetRandInt(COINJOIN_AUTO_TIMEOUT_MAX - COINJOIN_AUTO_TIMEOUT_MIN);
18591859
}
18601860
}
@@ -1901,9 +1901,10 @@ void CoinJoinWalletManager::Add(CWallet& wallet) {
19011901

19021902
void CoinJoinWalletManager::DoMaintenance() {
19031903
for (auto& [wallet_str, walletman] : m_wallet_manager_map) {
1904-
walletman->DoMaintenance(m_connman, m_mempool);
1904+
walletman->DoMaintenance(m_chainstate, m_connman, m_mempool);
19051905
}
19061906
}
1907+
19071908
void CoinJoinWalletManager::Remove(const std::string& name) {
19081909
m_wallet_manager_map.erase(name);
19091910
g_wallet_init_interface.InitCoinJoinSettings(*this);

src/coinjoin/client.h

+7-6
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,10 @@ class CoinJoinWalletManager {
7373
using wallet_name_cjman_map = std::map<const std::string, std::unique_ptr<CCoinJoinClientManager>>;
7474

7575
public:
76-
CoinJoinWalletManager(CConnman& connman, CDeterministicMNManager& dmnman, CMasternodeMetaMan& mn_metaman, CTxMemPool& mempool,
76+
CoinJoinWalletManager(CChainState& chainstate, CConnman& connman, CDeterministicMNManager& dmnman, CMasternodeMetaMan& mn_metaman, CTxMemPool& mempool,
7777
const CMasternodeSync& mn_sync, const std::unique_ptr<CCoinJoinClientQueueManager>& queueman, bool is_masternode)
78-
: m_connman(connman), m_dmnman(dmnman), m_mn_metaman(mn_metaman), m_mempool(mempool), m_mn_sync(mn_sync), m_queueman(queueman),
79-
m_is_masternode{is_masternode}
78+
: m_chainstate(chainstate), m_connman(connman), m_dmnman(dmnman), m_mn_metaman(mn_metaman), m_mempool(mempool), m_mn_sync(mn_sync),
79+
m_queueman(queueman), m_is_masternode{is_masternode}
8080
{}
8181

8282
~CoinJoinWalletManager() {
@@ -96,6 +96,7 @@ class CoinJoinWalletManager {
9696
const wallet_name_cjman_map& raw() const { return m_wallet_manager_map; }
9797

9898
private:
99+
CChainState& m_chainstate;
99100
CConnman& m_connman;
100101
CDeterministicMNManager& m_dmnman;
101102
CMasternodeMetaMan& m_mn_metaman;
@@ -181,7 +182,7 @@ class CCoinJoinClientSession : public CCoinJoinBaseSession
181182
bool GetMixingMasternodeInfo(CDeterministicMNCPtr& ret) const;
182183

183184
/// Passively run mixing in the background according to the configuration in settings
184-
bool DoAutomaticDenominating(CConnman& connman, CTxMemPool& mempool, bool fDryRun = false) EXCLUSIVE_LOCKS_REQUIRED(!cs_coinjoin);
185+
bool DoAutomaticDenominating(CChainState& active_chainstate, CConnman& connman, CTxMemPool& mempool, bool fDryRun = false) EXCLUSIVE_LOCKS_REQUIRED(!cs_coinjoin);
185186

186187
/// As a client, submit part of a future mixing transaction to a Masternode to start the process
187188
bool SubmitDenominate(CConnman& connman);
@@ -280,7 +281,7 @@ class CCoinJoinClientManager
280281
bool GetMixingMasternodesInfo(std::vector<CDeterministicMNCPtr>& vecDmnsRet) const EXCLUSIVE_LOCKS_REQUIRED(!cs_deqsessions);
281282

282283
/// Passively run mixing in the background according to the configuration in settings
283-
bool DoAutomaticDenominating(CConnman& connman, CTxMemPool& mempool, bool fDryRun = false) EXCLUSIVE_LOCKS_REQUIRED(!cs_deqsessions);
284+
bool DoAutomaticDenominating(CChainState& active_chainstate, CConnman& connman, CTxMemPool& mempool, bool fDryRun = false) EXCLUSIVE_LOCKS_REQUIRED(!cs_deqsessions);
284285

285286
bool TrySubmitDenominate(const CService& mnAddr, CConnman& connman) EXCLUSIVE_LOCKS_REQUIRED(!cs_deqsessions);
286287
bool MarkAlreadyJoinedQueueAsTried(CCoinJoinQueue& dsq) const EXCLUSIVE_LOCKS_REQUIRED(!cs_deqsessions);
@@ -296,7 +297,7 @@ class CCoinJoinClientManager
296297

297298
void UpdatedBlockTip(const CBlockIndex* pindex);
298299

299-
void DoMaintenance(CConnman& connman, CTxMemPool& mempool) EXCLUSIVE_LOCKS_REQUIRED(!cs_deqsessions);
300+
void DoMaintenance(CChainState& active_chainstate, CConnman& connman, CTxMemPool& mempool) EXCLUSIVE_LOCKS_REQUIRED(!cs_deqsessions);
300301

301302
void GetJsonInfo(UniValue& obj) const EXCLUSIVE_LOCKS_REQUIRED(!cs_deqsessions);
302303
};

src/coinjoin/coinjoin.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ bool ATMPIfSaneFee(CChainState& active_chainstate, CTxMemPool& pool, const CTran
322322
}
323323

324324
// check to make sure the collateral provided by the client is valid
325-
bool CoinJoin::IsCollateralValid(CTxMemPool& mempool, const CTransaction& txCollateral)
325+
bool CoinJoin::IsCollateralValid(CChainState& active_chainstate, CTxMemPool& mempool, const CTransaction& txCollateral)
326326
{
327327
if (txCollateral.vout.empty()) return false;
328328
if (txCollateral.nLockTime != 0) return false;
@@ -348,7 +348,7 @@ bool CoinJoin::IsCollateralValid(CTxMemPool& mempool, const CTransaction& txColl
348348
return false;
349349
}
350350
nValueIn += mempoolTx->vout[txin.prevout.n].nValue;
351-
} else if (GetUTXOCoin(txin.prevout, coin)) {
351+
} else if (GetUTXOCoin(active_chainstate, txin.prevout, coin)) {
352352
nValueIn += coin.out.nValue;
353353
} else {
354354
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
366366

367367
{
368368
LOCK(cs_main);
369-
if (!ATMPIfSaneFee(::ChainstateActive(), mempool, MakeTransactionRef(txCollateral), /*test_accept=*/true)) {
369+
if (!ATMPIfSaneFee(active_chainstate, mempool, MakeTransactionRef(txCollateral), /*test_accept=*/true)) {
370370
LogPrint(BCLog::COINJOIN, "CoinJoin::IsCollateralValid -- didn't pass AcceptToMemoryPool()\n");
371371
return false;
372372
}

src/coinjoin/coinjoin.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ namespace CoinJoin
354354
constexpr CAmount GetMaxPoolAmount() { return COINJOIN_ENTRY_MAX_SIZE * vecStandardDenominations.front(); }
355355

356356
/// If the collateral is valid given by a client
357-
bool IsCollateralValid(CTxMemPool& mempool, const CTransaction& txCollateral);
357+
bool IsCollateralValid(CChainState& active_chainstate, CTxMemPool& mempool, const CTransaction& txCollateral);
358358

359359
}
360360

src/coinjoin/context.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ CJContext::CJContext(CChainState& chainstate, CConnman& connman, CDeterministicM
1414
const std::unique_ptr<PeerManager>& peerman, bool relay_txes) :
1515
dstxman{std::make_unique<CDSTXManager>()},
1616
#ifdef ENABLE_WALLET
17-
walletman{std::make_unique<CoinJoinWalletManager>(connman, dmnman, mn_metaman, mempool, mn_sync, queueman, /* is_masternode = */ mn_activeman != nullptr)},
17+
walletman{std::make_unique<CoinJoinWalletManager>(chainstate, connman, dmnman, mn_metaman, mempool, mn_sync, queueman, /* is_masternode = */ mn_activeman != nullptr)},
1818
queueman {relay_txes ? std::make_unique<CCoinJoinClientQueueManager>(connman, *walletman, dmnman, mn_metaman, mn_sync, /* is_masternode = */ mn_activeman != nullptr) : nullptr},
1919
#endif // ENABLE_WALLET
2020
server{std::make_unique<CCoinJoinServer>(chainstate, connman, dmnman, *dstxman, mn_metaman, mempool, mn_activeman, mn_sync, peerman)}

src/coinjoin/server.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -574,7 +574,7 @@ bool CCoinJoinServer::AddEntry(const CCoinJoinEntry& entry, PoolMessage& nMessag
574574
return false;
575575
}
576576

577-
if (!CoinJoin::IsCollateralValid(mempool, *entry.txCollateral)) {
577+
if (!CoinJoin::IsCollateralValid(m_chainstate, mempool, *entry.txCollateral)) {
578578
LogPrint(BCLog::COINJOIN, "CCoinJoinServer::%s -- ERROR: collateral not valid!\n", __func__);
579579
nMessageIDRet = ERR_INVALID_COLLATERAL;
580580
return false;
@@ -685,7 +685,7 @@ bool CCoinJoinServer::IsAcceptableDSA(const CCoinJoinAccept& dsa, PoolMessage& n
685685
}
686686

687687
// check collateral
688-
if (!fUnitTest && !CoinJoin::IsCollateralValid(mempool, CTransaction(dsa.txCollateral))) {
688+
if (!fUnitTest && !CoinJoin::IsCollateralValid(m_chainstate, mempool, CTransaction(dsa.txCollateral))) {
689689
LogPrint(BCLog::COINJOIN, "CCoinJoinServer::%s -- collateral not valid!\n", __func__);
690690
nMessageIDRet = ERR_INVALID_COLLATERAL;
691691
return false;

src/node/blockstorage.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ void ThreadImport(ChainstateManager& chainman, CDeterministicMNManager& dmnman,
204204
auto mnList = dmnman.GetListAtChainTip();
205205
mnList.ForEachMN(false, [&](auto& dmn) {
206206
Coin coin;
207-
GetUTXOCoin(dmn.collateralOutpoint, coin);
207+
GetUTXOCoin(chainman.ActiveChainstate(), dmn.collateralOutpoint, coin);
208208
});
209209
LogPrintf("Filling coin cache with masternode UTXOs: done in %dms\n", GetTimeMillis() - nStart);
210210
}

src/rpc/coinjoin.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ static RPCHelpMan coinjoin()
4040
if (!wallet) return NullUniValue;
4141

4242
const NodeContext& node = EnsureAnyNodeContext(request.context);
43+
4344
if (node.mn_activeman) {
4445
throw JSONRPCError(RPC_INTERNAL_ERROR, "Client-side mixing is not supported on masternodes");
4546
}
@@ -69,9 +70,10 @@ static RPCHelpMan coinjoin()
6970
throw JSONRPCError(RPC_INTERNAL_ERROR, "Mixing has been started already.");
7071
}
7172

73+
ChainstateManager& chainman = EnsureChainman(node);
7274
CTxMemPool& mempool = EnsureMemPool(node);
7375
CConnman& connman = EnsureConnman(node);
74-
bool result = cj_clientman->DoAutomaticDenominating(connman, mempool);
76+
bool result = cj_clientman->DoAutomaticDenominating(chainman.ActiveChainstate(), connman, mempool);
7577
return "Mixing " + (result ? "started successfully" : ("start failed: " + cj_clientman->GetStatuses().original + ", will retry"));
7678
}
7779

src/rpc/evo.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -756,7 +756,7 @@ static UniValue protx_register_common_wrapper(const JSONRPCRequest& request,
756756
FundSpecialTx(wallet.get(), tx, ptx, fundDest);
757757
UpdateSpecialTxInputsHash(tx, ptx);
758758
Coin coin;
759-
if (!GetUTXOCoin(ptx.collateralOutpoint, coin)) {
759+
if (!GetUTXOCoin(chainman.ActiveChainstate(), ptx.collateralOutpoint, coin)) {
760760
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, strprintf("collateral not found: %s", ptx.collateralOutpoint.ToStringShort()));
761761
}
762762
CTxDestination txDest;
@@ -1286,7 +1286,7 @@ static UniValue BuildDMNListEntry(const CWallet* const pwallet, const CDetermini
12861286
UniValue o = dmn.ToJson();
12871287

12881288
CTransactionRef collateralTx{nullptr};
1289-
int confirmations = GetUTXOConfirmations(dmn.collateralOutpoint);
1289+
int confirmations = GetUTXOConfirmations(chainman.ActiveChainstate(), dmn.collateralOutpoint);
12901290

12911291
if (pindex != nullptr) {
12921292
if (confirmations > -1) {
@@ -1307,7 +1307,7 @@ static UniValue BuildDMNListEntry(const CWallet* const pwallet, const CDetermini
13071307
bool hasVotingKey = CheckWalletOwnsKey(pwallet, dmn.pdmnState->keyIDVoting);
13081308

13091309
bool ownsCollateral = false;
1310-
if (Coin coin; GetUTXOCoin(dmn.collateralOutpoint, coin)) {
1310+
if (Coin coin; GetUTXOCoin(chainman.ActiveChainstate(), dmn.collateralOutpoint, coin)) {
13111311
ownsCollateral = CheckWalletOwnsScript(pwallet, coin.out.scriptPubKey);
13121312
} else if (collateralTx != nullptr) {
13131313
ownsCollateral = CheckWalletOwnsScript(pwallet, collateralTx->vout[dmn.collateralOutpoint.n].scriptPubKey);

src/rpc/masternode.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -595,7 +595,7 @@ static RPCHelpMan masternodelist_helper(bool is_composite)
595595
std::string strOutpoint = dmn.collateralOutpoint.ToStringShort();
596596
Coin coin;
597597
std::string collateralAddressStr = "UNKNOWN";
598-
if (GetUTXOCoin(dmn.collateralOutpoint, coin)) {
598+
if (GetUTXOCoin(chainman.ActiveChainstate(), dmn.collateralOutpoint, coin)) {
599599
CTxDestination collateralDest;
600600
if (ExtractDestination(coin.out.scriptPubKey, collateralDest)) {
601601
collateralAddressStr = EncodeDestination(collateralDest);

src/validation.cpp

+7-7
Original file line numberDiff line numberDiff line change
@@ -344,29 +344,29 @@ bool CheckSequenceLocks(CBlockIndex* tip,
344344
return EvaluateSequenceLocks(index, lockPair);
345345
}
346346

347-
bool GetUTXOCoin(const COutPoint& outpoint, Coin& coin)
347+
bool GetUTXOCoin(CChainState& active_chainstate, const COutPoint& outpoint, Coin& coin)
348348
{
349349
LOCK(cs_main);
350-
if (!::ChainstateActive().CoinsTip().GetCoin(outpoint, coin))
350+
if (!active_chainstate.CoinsTip().GetCoin(outpoint, coin))
351351
return false;
352352
if (coin.IsSpent())
353353
return false;
354354
return true;
355355
}
356356

357-
int GetUTXOHeight(const COutPoint& outpoint)
357+
int GetUTXOHeight(CChainState& active_chainstate, const COutPoint& outpoint)
358358
{
359359
// -1 means UTXO is yet unknown or already spent
360360
Coin coin;
361-
return GetUTXOCoin(outpoint, coin) ? coin.nHeight : -1;
361+
return GetUTXOCoin(active_chainstate, outpoint, coin) ? coin.nHeight : -1;
362362
}
363363

364-
int GetUTXOConfirmations(const COutPoint& outpoint)
364+
int GetUTXOConfirmations(CChainState& active_chainstate, const COutPoint& outpoint)
365365
{
366366
// -1 means UTXO is yet unknown or already spent
367367
LOCK(cs_main);
368-
int nPrevoutHeight = GetUTXOHeight(outpoint);
369-
return (nPrevoutHeight > -1 && ::ChainActive().Tip()) ? ::ChainActive().Height() - nPrevoutHeight + 1 : -1;
368+
int nPrevoutHeight = GetUTXOHeight(active_chainstate, outpoint);
369+
return (nPrevoutHeight > -1 && active_chainstate.m_chain.Tip()) ? active_chainstate.m_chain.Height() - nPrevoutHeight + 1 : -1;
370370
}
371371

372372
static bool ContextualCheckTransaction(const CTransaction& tx, TxValidationState& state, const Consensus::Params& consensusParams, const CBlockIndex* pindexPrev)

src/validation.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -294,9 +294,9 @@ PackageMempoolAcceptResult ProcessNewPackage(CChainState& active_chainstate, CTx
294294
const Package& txns, bool test_accept)
295295
EXCLUSIVE_LOCKS_REQUIRED(cs_main);
296296

297-
bool GetUTXOCoin(const COutPoint& outpoint, Coin& coin);
298-
int GetUTXOHeight(const COutPoint& outpoint);
299-
int GetUTXOConfirmations(const COutPoint& outpoint);
297+
bool GetUTXOCoin(CChainState& active_chainstate, const COutPoint& outpoint, Coin& coin);
298+
int GetUTXOHeight(CChainState& active_chainstate, const COutPoint& outpoint);
299+
int GetUTXOConfirmations(CChainState& active_chainstate, const COutPoint& outpoint);
300300

301301
/** Apply the effects of this transaction on the UTXO set represented by view */
302302
void UpdateCoins(const CTransaction& tx, CCoinsViewCache& inputs, int nHeight);

0 commit comments

Comments
 (0)