Skip to content

Commit 16abbed

Browse files
committed
refactor: replace direct usages of GetMetaInfo while calculating dsq threshold
1 parent 6cb2344 commit 16abbed

File tree

4 files changed

+25
-33
lines changed

4 files changed

+25
-33
lines changed

src/coinjoin/client.cpp

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -124,17 +124,11 @@ MessageProcessingResult CCoinJoinClientQueueManager::ProcessMessage(NodeId from,
124124
LogPrint(BCLog::COINJOIN, "DSQUEUE -- CoinJoin queue is ready, masternode=%s, queue=%s\n", dmn->proTxHash.ToString(), dsq.ToString());
125125
return ret;
126126
} else {
127-
int64_t nLastDsq = m_mn_metaman.GetMetaInfo(dmn->proTxHash)->GetLastDsq();
128-
int64_t nDsqThreshold = m_mn_metaman.GetDsqThreshold(dmn->proTxHash, tip_mn_list.GetValidMNsCount());
129-
LogPrint(BCLog::COINJOIN, "DSQUEUE -- nLastDsq: %d nDsqThreshold: %d nDsqCount: %d\n", nLastDsq,
130-
nDsqThreshold, m_mn_metaman.GetDsqCount());
131-
// don't allow a few nodes to dominate the queuing process
132-
if (nLastDsq != 0 && nDsqThreshold > m_mn_metaman.GetDsqCount()) {
127+
if (m_mn_metaman.IsDsqOver(dmn->proTxHash, tip_mn_list.GetValidMNsCount())) {
133128
LogPrint(BCLog::COINJOIN, "DSQUEUE -- Masternode %s is sending too many dsq messages\n",
134129
dmn->proTxHash.ToString());
135130
return ret;
136131
}
137-
138132
m_mn_metaman.AllowMixing(dmn->proTxHash);
139133

140134
LogPrint(BCLog::COINJOIN, "DSQUEUE -- new CoinJoin queue, masternode=%s, queue=%s\n", dmn->proTxHash.ToString(), dsq.ToString());
@@ -1180,13 +1174,10 @@ bool CCoinJoinClientSession::StartNewQueue(CAmount nBalanceNeedsAnonymized, CCon
11801174
continue;
11811175
}
11821176

1183-
int64_t nLastDsq = m_mn_metaman.GetMetaInfo(dmn->proTxHash)->GetLastDsq();
1184-
int64_t nDsqThreshold = m_mn_metaman.GetDsqThreshold(dmn->proTxHash, nMnCount);
1185-
if (nLastDsq != 0 && nDsqThreshold > m_mn_metaman.GetDsqCount()) {
1177+
if (m_mn_metaman.IsDsqOver(dmn->proTxHash, nMnCount)) {
11861178
WalletCJLogPrint(m_wallet, /* Continued */
1187-
"CCoinJoinClientSession::StartNewQueue -- too early to mix with node," /* Continued */
1188-
" masternode=%s, nLastDsq=%d, nDsqThreshold=%d, nDsqCount=%d\n",
1189-
dmn->proTxHash.ToString(), nLastDsq, nDsqThreshold, m_mn_metaman.GetDsqCount());
1179+
"CCoinJoinClientSession::StartNewQueue -- too early to mix with node masternode=%s\n",
1180+
dmn->proTxHash.ToString());
11901181
nTries++;
11911182
continue;
11921183
}

src/coinjoin/server.cpp

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,7 @@ void CCoinJoinServer::ProcessDSACCEPT(CNode& peer, CDataStream& vRecv)
9999
}
100100
}
101101

102-
int64_t nLastDsq = m_mn_metaman.GetMetaInfo(dmn->proTxHash)->GetLastDsq();
103-
int64_t nDsqThreshold = m_mn_metaman.GetDsqThreshold(dmn->proTxHash, mnList.GetValidMNsCount());
104-
if (nLastDsq != 0 && nDsqThreshold > m_mn_metaman.GetDsqCount()) {
102+
if (m_mn_metaman.IsDsqOver(dmn->proTxHash, mnList.GetValidMNsCount())) {
105103
if (fLogIPs) {
106104
LogPrint(BCLog::COINJOIN, "DSACCEPT -- last dsq too recent, must wait: peer=%d, addr=%s\n",
107105
peer.GetId(), peer.addr.ToStringAddrPort());
@@ -194,11 +192,8 @@ MessageProcessingResult CCoinJoinServer::ProcessDSQUEUE(NodeId from, CDataStream
194192
}
195193

196194
if (!dsq.fReady) {
197-
int64_t nLastDsq = m_mn_metaman.GetMetaInfo(dmn->proTxHash)->GetLastDsq();
198-
int64_t nDsqThreshold = m_mn_metaman.GetDsqThreshold(dmn->proTxHash, tip_mn_list.GetValidMNsCount());
199-
LogPrint(BCLog::COINJOIN, "DSQUEUE -- nLastDsq: %d nDsqThreshold: %d nDsqCount: %d\n", nLastDsq, nDsqThreshold, m_mn_metaman.GetDsqCount());
200195
//don't allow a few nodes to dominate the queuing process
201-
if (nLastDsq != 0 && nDsqThreshold > m_mn_metaman.GetDsqCount()) {
196+
if (m_mn_metaman.IsDsqOver(dmn->proTxHash, tip_mn_list.GetValidMNsCount())) {
202197
LogPrint(BCLog::COINJOIN, "DSQUEUE -- node sending too many dsq messages, masternode=%s\n", dmn->proTxHash.ToString());
203198
return ret;
204199
}

src/masternode/meta.cpp

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -88,18 +88,21 @@ CMasternodeMetaInfoPtr CMasternodeMetaMan::GetMetaInfo(const uint256& proTxHash,
8888
return it->second;
8989
}
9090

91-
// We keep track of dsq (mixing queues) count to avoid using same masternodes for mixing too often.
92-
// This threshold is calculated as the last dsq count this specific masternode was used in a mixing
93-
// session plus a margin of 20% of masternode count. In other words we expect at least 20% of unique
94-
// masternodes before we ever see a masternode that we know already mixed someone's funds earlier.
95-
int64_t CMasternodeMetaMan::GetDsqThreshold(const uint256& proTxHash, int nMnCount)
96-
{
97-
auto metaInfo = GetMetaInfo(proTxHash);
98-
if (metaInfo == nullptr) {
99-
// return a threshold which is slightly above nDsqCount i.e. a no-go
100-
return nDsqCount + 1;
91+
bool CMasternodeMetaMan::IsDsqOver(const uint256& protx_hash, int mn_count) const
92+
{
93+
LOCK(cs);
94+
auto it = metaInfos.find(protx_hash);
95+
if (it == metaInfos.end()) {
96+
LogPrint(BCLog::COINJOIN, "DSQUEUE -- node %s is logged\n", protx_hash.ToString());
97+
return false;
10198
}
102-
return metaInfo->GetLastDsq() + nMnCount / 5;
99+
const auto& meta_info = *it->second;
100+
int64_t last_dsq = meta_info.GetLastDsq();
101+
int64_t threshold = last_dsq + mn_count / 5;
102+
103+
LogPrint(BCLog::COINJOIN, "DSQUEUE -- mn: %s last_dsq: %d dsq_threshold: %d nDsqCount: %d\n",
104+
protx_hash.ToString(), last_dsq, threshold, nDsqCount);
105+
return last_dsq != 0 && threshold > nDsqCount;
103106
}
104107

105108
void CMasternodeMetaMan::AllowMixing(const uint256& proTxHash)

src/masternode/meta.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,8 +262,11 @@ class CMasternodeMetaMan : public MasternodeMetaStore
262262
CMasternodeMetaInfo GetInfo(const uint256& proTxHash) EXCLUSIVE_LOCKS_REQUIRED(!cs);
263263
CMasternodeMetaInfoPtr GetMetaInfo(const uint256& proTxHash, bool fCreate = true) EXCLUSIVE_LOCKS_REQUIRED(!cs);
264264

265-
int64_t GetDsqCount() const { return nDsqCount; }
266-
int64_t GetDsqThreshold(const uint256& proTxHash, int nMnCount);
265+
// We keep track of dsq (mixing queues) count to avoid using same masternodes for mixing too often.
266+
// MN's threshold is calculated as the last dsq count this specific masternode was used in a mixing
267+
// session plus a margin of 20% of masternode count. In other words we expect at least 20% of unique
268+
// masternodes before we ever see a masternode that we know already mixed someone's funds earlier.
269+
bool IsDsqOver(const uint256& protx_hash, int mn_count) const EXCLUSIVE_LOCKS_REQUIRED(!cs);
267270

268271
void AllowMixing(const uint256& proTxHash);
269272
void DisallowMixing(const uint256& proTxHash);

0 commit comments

Comments
 (0)