refactor: create a new global object dstxManager

Its class CDSTXManager has a lot of ex-static functions and data that
are actually meant to be an object
This commit is contained in:
Konstantin Akimov 2023-12-13 01:57:41 +07:00 committed by pasta
parent 89496e117b
commit e23b07bfa4
No known key found for this signature in database
GPG Key ID: 52527BEDABE87984
8 changed files with 53 additions and 31 deletions

View File

@ -307,10 +307,6 @@ bool CCoinJoinBaseSession::IsValidInOuts(const CTxMemPool& mempool, const std::v
return true;
}
// Definitions for static data members
Mutex CoinJoin::cs_mapdstx;
std::map<uint256, CCoinJoinBroadcastTx> CoinJoin::mapDSTX GUARDED_BY(CoinJoin::cs_mapdstx);
// check to make sure the collateral provided by the client is valid
bool CoinJoin::IsCollateralValid(CTxMemPool& mempool, const CTransaction& txCollateral)
{
@ -418,14 +414,17 @@ bilingual_str CoinJoin::GetMessageByID(PoolMessage nMessageID)
}
}
void CoinJoin::AddDSTX(const CCoinJoinBroadcastTx& dstx)
// Definitions for static data members
std::unique_ptr<CDSTXManager> dstxManager;
void CDSTXManager::AddDSTX(const CCoinJoinBroadcastTx& dstx)
{
AssertLockNotHeld(cs_mapdstx);
LOCK(cs_mapdstx);
mapDSTX.insert(std::make_pair(dstx.tx->GetHash(), dstx));
}
CCoinJoinBroadcastTx CoinJoin::GetDSTX(const uint256& hash)
CCoinJoinBroadcastTx CDSTXManager::GetDSTX(const uint256& hash)
{
AssertLockNotHeld(cs_mapdstx);
LOCK(cs_mapdstx);
@ -433,7 +432,7 @@ CCoinJoinBroadcastTx CoinJoin::GetDSTX(const uint256& hash)
return (it == mapDSTX.end()) ? CCoinJoinBroadcastTx() : it->second;
}
void CoinJoin::CheckDSTXes(const CBlockIndex* pindex, const llmq::CChainLocksHandler& clhandler)
void CDSTXManager::CheckDSTXes(const CBlockIndex* pindex, const llmq::CChainLocksHandler& clhandler)
{
AssertLockNotHeld(cs_mapdstx);
LOCK(cs_mapdstx);
@ -448,21 +447,21 @@ void CoinJoin::CheckDSTXes(const CBlockIndex* pindex, const llmq::CChainLocksHan
LogPrint(BCLog::COINJOIN, "CoinJoin::CheckDSTXes -- mapDSTX.size()=%llu\n", mapDSTX.size());
}
void CoinJoin::UpdatedBlockTip(const CBlockIndex* pindex, const llmq::CChainLocksHandler& clhandler, const CMasternodeSync& mn_sync)
void CDSTXManager::UpdatedBlockTip(const CBlockIndex* pindex, const llmq::CChainLocksHandler& clhandler, const CMasternodeSync& mn_sync)
{
if (pindex && mn_sync.IsBlockchainSynced()) {
CheckDSTXes(pindex, clhandler);
}
}
void CoinJoin::NotifyChainLock(const CBlockIndex* pindex, const llmq::CChainLocksHandler& clhandler, const CMasternodeSync& mn_sync)
void CDSTXManager::NotifyChainLock(const CBlockIndex* pindex, const llmq::CChainLocksHandler& clhandler, const CMasternodeSync& mn_sync)
{
if (pindex && mn_sync.IsBlockchainSynced()) {
CheckDSTXes(pindex, clhandler);
}
}
void CoinJoin::UpdateDSTXConfirmedHeight(const CTransactionRef& tx, std::optional<int> nHeight)
void CDSTXManager::UpdateDSTXConfirmedHeight(const CTransactionRef& tx, std::optional<int> nHeight)
{
AssertLockHeld(cs_mapdstx);
@ -472,17 +471,17 @@ void CoinJoin::UpdateDSTXConfirmedHeight(const CTransactionRef& tx, std::optiona
}
it->second.SetConfirmedHeight(nHeight);
LogPrint(BCLog::COINJOIN, "CoinJoin::%s -- txid=%s, nHeight=%d\n", __func__, tx->GetHash().ToString(), nHeight.value_or(-1));
LogPrint(BCLog::COINJOIN, "CDSTXManager::%s -- txid=%s, nHeight=%d\n", __func__, tx->GetHash().ToString(), nHeight.value_or(-1));
}
void CoinJoin::TransactionAddedToMempool(const CTransactionRef& tx)
void CDSTXManager::TransactionAddedToMempool(const CTransactionRef& tx)
{
AssertLockNotHeld(cs_mapdstx);
LOCK(cs_mapdstx);
UpdateDSTXConfirmedHeight(tx, std::nullopt);
}
void CoinJoin::BlockConnected(const std::shared_ptr<const CBlock>& pblock, const CBlockIndex* pindex)
void CDSTXManager::BlockConnected(const std::shared_ptr<const CBlock>& pblock, const CBlockIndex* pindex)
{
AssertLockNotHeld(cs_mapdstx);
LOCK(cs_mapdstx);
@ -492,7 +491,7 @@ void CoinJoin::BlockConnected(const std::shared_ptr<const CBlock>& pblock, const
}
}
void CoinJoin::BlockDisconnected(const std::shared_ptr<const CBlock>& pblock, const CBlockIndex*)
void CDSTXManager::BlockDisconnected(const std::shared_ptr<const CBlock>& pblock, const CBlockIndex*)
{
AssertLockNotHeld(cs_mapdstx);
LOCK(cs_mapdstx);

View File

@ -18,6 +18,7 @@
#include <version.h>
#include <atomic>
#include <map>
#include <optional>
#include <utility>
@ -340,8 +341,6 @@ public:
// Various helpers and dstx manager implementation
namespace CoinJoin
{
extern Mutex cs_mapdstx;
bilingual_str GetMessageByID(PoolMessage nMessageID);
/// Get the minimum/maximum number of participants for the pool
@ -353,6 +352,15 @@ namespace CoinJoin
/// If the collateral is valid given by a client
bool IsCollateralValid(CTxMemPool& mempool, const CTransaction& txCollateral);
}
class CDSTXManager
{
Mutex cs_mapdstx;
std::map<uint256, CCoinJoinBroadcastTx> mapDSTX GUARDED_BY(cs_mapdstx);
public:
CDSTXManager() = default;
void AddDSTX(const CCoinJoinBroadcastTx& dstx) LOCKS_EXCLUDED(cs_mapdstx);
CCoinJoinBroadcastTx GetDSTX(const uint256& hash) LOCKS_EXCLUDED(cs_mapdstx);
@ -362,6 +370,14 @@ namespace CoinJoin
void TransactionAddedToMempool(const CTransactionRef& tx) LOCKS_EXCLUDED(cs_mapdstx);
void BlockConnected(const std::shared_ptr<const CBlock>& pblock, const CBlockIndex* pindex) LOCKS_EXCLUDED(cs_mapdstx);
void BlockDisconnected(const std::shared_ptr<const CBlock>& pblock, const CBlockIndex*) LOCKS_EXCLUDED(cs_mapdstx);
private:
void CheckDSTXes(const CBlockIndex* pindex, const llmq::CChainLocksHandler& clhandler);
void UpdateDSTXConfirmedHeight(const CTransactionRef& tx, std::optional<int> nHeight);
};
extern std::unique_ptr<CDSTXManager> dstxManager;
#endif // BITCOIN_COINJOIN_COINJOIN_H

View File

@ -333,13 +333,13 @@ void CCoinJoinServer::CommitFinalTransaction()
LogPrint(BCLog::COINJOIN, "CCoinJoinServer::CommitFinalTransaction -- CREATING DSTX\n");
// create and sign masternode dstx transaction
if (!CoinJoin::GetDSTX(hashTx)) {
if (!::dstxManager->GetDSTX(hashTx)) {
CCoinJoinBroadcastTx dstxNew(finalTransaction,
WITH_LOCK(activeMasternodeInfoCs, return activeMasternodeInfo.outpoint),
WITH_LOCK(activeMasternodeInfoCs, return activeMasternodeInfo.proTxHash),
GetAdjustedTime());
dstxNew.Sign();
CoinJoin::AddDSTX(dstxNew);
::dstxManager->AddDSTX(dstxNew);
}
LogPrint(BCLog::COINJOIN, "CCoinJoinServer::CommitFinalTransaction -- TRANSMITTING DSTX\n");

View File

@ -67,7 +67,7 @@ void CDSNotificationInterface::UpdatedBlockTip(const CBlockIndex *pindexNew, con
if (fInitialDownload)
return;
CoinJoin::UpdatedBlockTip(pindexNew, *llmq_ctx->clhandler, m_mn_sync);
::dstxManager->UpdatedBlockTip(pindexNew, *llmq_ctx->clhandler, m_mn_sync);
#ifdef ENABLE_WALLET
for (auto& pair : cj_ctx->clientman->raw()) {
pair.second->UpdatedBlockTip(pindexNew);
@ -88,7 +88,7 @@ void CDSNotificationInterface::TransactionAddedToMempool(const CTransactionRef&
{
llmq_ctx->isman->TransactionAddedToMempool(ptx);
llmq_ctx->clhandler->TransactionAddedToMempool(ptx, nAcceptTime);
CoinJoin::TransactionAddedToMempool(ptx);
::dstxManager->TransactionAddedToMempool(ptx);
}
void CDSNotificationInterface::TransactionRemovedFromMempool(const CTransactionRef& ptx, MemPoolRemovalReason reason)
@ -100,14 +100,14 @@ void CDSNotificationInterface::BlockConnected(const std::shared_ptr<const CBlock
{
llmq_ctx->isman->BlockConnected(pblock, pindex);
llmq_ctx->clhandler->BlockConnected(pblock, pindex);
CoinJoin::BlockConnected(pblock, pindex);
::dstxManager->BlockConnected(pblock, pindex);
}
void CDSNotificationInterface::BlockDisconnected(const std::shared_ptr<const CBlock>& pblock, const CBlockIndex* pindexDisconnected)
{
llmq_ctx->isman->BlockDisconnected(pblock, pindexDisconnected);
llmq_ctx->clhandler->BlockDisconnected(pblock, pindexDisconnected);
CoinJoin::BlockDisconnected(pblock, pindexDisconnected);
::dstxManager->BlockDisconnected(pblock, pindexDisconnected);
}
void CDSNotificationInterface::NotifyMasternodeListChanged(bool undo, const CDeterministicMNList& oldMNList, const CDeterministicMNListDiff& diff)
@ -119,5 +119,5 @@ void CDSNotificationInterface::NotifyMasternodeListChanged(bool undo, const CDet
void CDSNotificationInterface::NotifyChainLock(const CBlockIndex* pindex, const std::shared_ptr<const llmq::CChainLockSig>& clsig)
{
llmq_ctx->isman->NotifyChainLock(pindex);
CoinJoin::NotifyChainLock(pindex, *llmq_ctx->clhandler, m_mn_sync);
::dstxManager->NotifyChainLock(pindex, *llmq_ctx->clhandler, m_mn_sync);
}

View File

@ -68,6 +68,7 @@
#include <validationinterface.h>
#include <masternode/node.h>
#include <coinjoin/coinjoin.h>
#include <coinjoin/context.h>
#ifdef ENABLE_WALLET
#include <coinjoin/client.h>
@ -306,6 +307,7 @@ void PrepareShutdown(NodeContext& node)
::masternodeSync.reset();
::netfulfilledman.reset();
::mmetaman.reset();
::dstxManager.reset();
// Stop and delete all indexes only after flushing background callbacks.
if (g_txindex) {
@ -2216,6 +2218,9 @@ bool AppInitMain(const CoreContext& context, NodeContext& node, interfaces::Bloc
}
}
assert(!::dstxManager);
::dstxManager = std::make_unique<CDSTXManager>();
assert(!::mmetaman);
::mmetaman = std::make_unique<CMasternodeMetaMan>(fLoadCacheFiles);
if (!::mmetaman->IsValid()) {

View File

@ -3758,7 +3758,7 @@ void CConnman::RelayTransaction(const CTransaction& tx)
{
uint256 hash = tx.GetHash();
int nInv = MSG_TX;
if (CoinJoin::GetDSTX(hash)) {
if (::dstxManager->GetDSTX(hash)) {
nInv = MSG_DSTX;
}
CInv inv(nInv, hash);

View File

@ -1834,7 +1834,7 @@ bool PeerManagerImpl::AlreadyHave(const CInv& inv)
m_llmq_ctx->isman->IsLocked(inv.hash);
return (!fIgnoreRecentRejects && m_recent_rejects.contains(inv.hash)) ||
(inv.type == MSG_DSTX && static_cast<bool>(CoinJoin::GetDSTX(inv.hash))) ||
(inv.type == MSG_DSTX && static_cast<bool>(::dstxManager->GetDSTX(inv.hash))) ||
m_mempool.exists(inv.hash) ||
(g_txindex != nullptr && g_txindex->HasTx(inv.hash));
}
@ -1882,7 +1882,7 @@ bool PeerManagerImpl::AlreadyHave(const CInv& inv)
void PeerManagerImpl::RelayTransaction(const uint256& txid)
{
CInv inv(CoinJoin::GetDSTX(txid) ? MSG_DSTX : MSG_TX, txid);
CInv inv(::dstxManager->GetDSTX(txid) ? MSG_DSTX : MSG_TX, txid);
m_connman.ForEachNode([&inv](CNode* pnode)
{
pnode->PushInventory(inv);
@ -2149,7 +2149,7 @@ void PeerManagerImpl::ProcessGetData(CNode& pfrom, Peer& peer, const std::atomic
if (tx) {
CCoinJoinBroadcastTx dstx;
if (inv.type == MSG_DSTX) {
dstx = CoinJoin::GetDSTX(inv.hash);
dstx = ::dstxManager->GetDSTX(inv.hash);
}
if (dstx) {
m_connman.PushMessage(&pfrom, msgMaker.Make(NetMsgType::DSTX, dstx));
@ -2731,7 +2731,7 @@ std::pair<bool /*ret*/, bool /*do_return*/> static ValidateDSTX(CTxMemPool& memp
LogPrint(BCLog::COINJOIN, "DSTX -- Invalid DSTX structure: %s\n", hashTx.ToString());
return {false, true};
}
if (CoinJoin::GetDSTX(hashTx)) {
if (::dstxManager->GetDSTX(hashTx)) {
LogPrint(BCLog::COINJOIN, "DSTX -- Already have %s, skipping...\n", hashTx.ToString());
return {true, true}; // not an error
}
@ -3589,7 +3589,7 @@ void PeerManagerImpl::ProcessMessage(
if (nInvType == MSG_DSTX) {
LogPrint(BCLog::COINJOIN, "DSTX -- Masternode transaction accepted, txid=%s, peer=%d\n",
tx.GetHash().ToString(), pfrom.GetId());
CoinJoin::AddDSTX(dstx);
::dstxManager->AddDSTX(dstx);
}
m_mempool.check(m_chainman.ActiveChainstate());
@ -4992,7 +4992,7 @@ bool PeerManagerImpl::SendMessages(CNode* pto)
pto->m_tx_relay->setInventoryTxToSend.erase(hash);
if (pto->m_tx_relay->pfilter && !pto->m_tx_relay->pfilter->IsRelevantAndUpdate(*txinfo.tx)) continue;
int nInvType = CoinJoin::GetDSTX(hash) ? MSG_DSTX : MSG_TX;
int nInvType = ::dstxManager->GetDSTX(hash) ? MSG_DSTX : MSG_TX;
queueAndMaybePushInv(CInv(nInvType, hash));
const auto islock = m_llmq_ctx->isman->GetInstantSendLockByTxid(hash);
@ -5061,7 +5061,7 @@ bool PeerManagerImpl::SendMessages(CNode* pto)
vRelayExpiration.push_back(std::make_pair(nNow + std::chrono::microseconds{RELAY_TX_CACHE_TIME}.count(), ret.first));
}
}
int nInvType = CoinJoin::GetDSTX(hash) ? MSG_DSTX : MSG_TX;
int nInvType = ::dstxManager->GetDSTX(hash) ? MSG_DSTX : MSG_TX;
queueAndMaybePushInv(CInv(nInvType, hash));
}
}

View File

@ -214,6 +214,7 @@ ChainTestingSetup::ChainTestingSetup(const std::string& chainName, const std::ve
::sporkManager = std::make_unique<CSporkManager>();
::governance = std::make_unique<CGovernanceManager>();
::masternodeSync = std::make_unique<CMasternodeSync>(*m_node.connman, *::governance);
::dstxManager = std::make_unique<CDSTXManager>();
::mmetaman = std::make_unique<CMasternodeMetaMan>(/* load_cache */ false);
::netfulfilledman = std::make_unique<CNetFulfilledRequestManager>(/* load_cache */ false);
@ -237,6 +238,7 @@ ChainTestingSetup::~ChainTestingSetup()
GetMainSignals().UnregisterBackgroundSignalScheduler();
::netfulfilledman.reset();
::mmetaman.reset();
::dstxManager.reset();
::masternodeSync.reset();
::governance.reset();
::sporkManager.reset();