-
Notifications
You must be signed in to change notification settings - Fork 1.2k
feat: share vecMasternodesUsed across all wallets, improve its handling
#6875
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
2c82532
13a28fa
f50d09a
eb3be09
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -248,7 +248,6 @@ void CCoinJoinClientSession::ResetPool() | |
| void CCoinJoinClientManager::ResetPool() | ||
| { | ||
| nCachedLastSuccessBlock = 0; | ||
| vecMasternodesUsed.clear(); | ||
| AssertLockNotHeld(cs_deqsessions); | ||
| LOCK(cs_deqsessions); | ||
| for (auto& session : deqSessions) { | ||
|
|
@@ -967,11 +966,15 @@ bool CCoinJoinClientManager::DoAutomaticDenominating(ChainstateManager& chainman | |
| // If we've used 90% of the Masternode list then drop the oldest first ~30% | ||
| int nThreshold_high = nMnCountEnabled * 0.9; | ||
| int nThreshold_low = nThreshold_high * 0.7; | ||
| WalletCJLogPrint(m_wallet, "Checking vecMasternodesUsed: size: %d, threshold: %d\n", (int)vecMasternodesUsed.size(), nThreshold_high); | ||
| size_t nUsedMasternodes{m_mn_metaman.GetUsedMasternodesCount()}; | ||
|
|
||
| if ((int)vecMasternodesUsed.size() > nThreshold_high) { | ||
| vecMasternodesUsed.erase(vecMasternodesUsed.begin(), vecMasternodesUsed.begin() + vecMasternodesUsed.size() - nThreshold_low); | ||
| WalletCJLogPrint(m_wallet, " vecMasternodesUsed: new size: %d, threshold: %d\n", (int)vecMasternodesUsed.size(), nThreshold_high); | ||
| WalletCJLogPrint(m_wallet, "Checking nUsedMasternodes: %d, threshold: %d\n", (int)nUsedMasternodes, nThreshold_high); | ||
|
|
||
| if ((int)nUsedMasternodes > nThreshold_high) { | ||
| size_t nToRemove{nUsedMasternodes - nThreshold_low}; | ||
| m_mn_metaman.RemoveUsedMasternodes(nToRemove); | ||
| WalletCJLogPrint(m_wallet, " new nUsedMasternodes: %d, threshold: %d\n", | ||
| (int)m_mn_metaman.GetUsedMasternodesCount(), nThreshold_high); | ||
|
UdjinM6 marked this conversation as resolved.
Outdated
|
||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
|
UdjinM6 marked this conversation as resolved.
|
||
| bool fResult = true; | ||
|
|
@@ -995,17 +998,17 @@ bool CCoinJoinClientManager::DoAutomaticDenominating(ChainstateManager& chainman | |
| return fResult; | ||
| } | ||
|
|
||
| void CCoinJoinClientManager::AddUsedMasternode(const COutPoint& outpointMn) | ||
| void CCoinJoinClientManager::AddUsedMasternode(const uint256& proTxHash) | ||
| { | ||
| vecMasternodesUsed.push_back(outpointMn); | ||
| m_mn_metaman.AddUsedMasternode(proTxHash); | ||
| } | ||
|
|
||
| CDeterministicMNCPtr CCoinJoinClientManager::GetRandomNotUsedMasternode() | ||
| { | ||
| auto mnList = m_dmnman.GetListAtChainTip(); | ||
|
|
||
| size_t nCountEnabled = mnList.GetValidMNsCount(); | ||
| size_t nCountNotExcluded = nCountEnabled - vecMasternodesUsed.size(); | ||
| size_t nCountNotExcluded{nCountEnabled - m_mn_metaman.GetUsedMasternodesCount()}; | ||
|
|
||
|
Comment on lines
+1010
to
1011
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix potential size_t underflow and printf specifiers. If used_count > enabled, subtraction underflows; also %d mismatches size_t. Clamp and cast. - size_t nCountNotExcluded{nCountEnabled - m_mn_metaman.GetUsedMasternodesCount()};
-
- WalletCJLogPrint(m_wallet, "CCoinJoinClientManager::%s -- %d enabled masternodes, %d masternodes to choose from\n", __func__, nCountEnabled, nCountNotExcluded);
+ const size_t used_count = m_mn_metaman.GetUsedMasternodesCount();
+ const size_t nCountNotExcluded = nCountEnabled > used_count ? (nCountEnabled - used_count) : 0;
+
+ WalletCJLogPrint(m_wallet, "CCoinJoinClientManager::%s -- %d enabled masternodes, %d masternodes to choose from\n",
+ __func__, static_cast<int>(nCountEnabled), static_cast<int>(nCountNotExcluded));
🤖 Prompt for AI Agents |
||
| WalletCJLogPrint(m_wallet, "CCoinJoinClientManager::%s -- %d enabled masternodes, %d masternodes to choose from\n", __func__, nCountEnabled, nCountNotExcluded); | ||
| if (nCountNotExcluded < 1) { | ||
|
|
@@ -1022,15 +1025,14 @@ CDeterministicMNCPtr CCoinJoinClientManager::GetRandomNotUsedMasternode() | |
| // shuffle pointers | ||
| Shuffle(vpMasternodesShuffled.begin(), vpMasternodesShuffled.end(), FastRandomContext()); | ||
|
|
||
| std::set<COutPoint> excludeSet(vecMasternodesUsed.begin(), vecMasternodesUsed.end()); | ||
|
|
||
| // loop through | ||
| // loop through - using direct O(1) lookup instead of creating a set copy | ||
| for (const auto& dmn : vpMasternodesShuffled) { | ||
| if (excludeSet.count(dmn->collateralOutpoint)) { | ||
| if (m_mn_metaman.IsUsedMasternode(dmn->proTxHash)) { | ||
| continue; | ||
| } | ||
|
|
||
| WalletCJLogPrint(m_wallet, "CCoinJoinClientManager::%s -- found, masternode=%s\n", __func__, dmn->collateralOutpoint.ToStringShort()); | ||
| WalletCJLogPrint(m_wallet, "CCoinJoinClientManager::%s -- found, masternode=%s\n", __func__, | ||
| dmn->proTxHash.ToString()); | ||
| return dmn; | ||
| } | ||
|
|
||
|
|
@@ -1083,7 +1085,7 @@ bool CCoinJoinClientSession::JoinExistingQueue(CAmount nBalanceNeedsAnonymized, | |
| continue; | ||
| } | ||
|
|
||
| m_clientman.AddUsedMasternode(dsq.masternodeOutpoint); | ||
| m_clientman.AddUsedMasternode(dmn->proTxHash); | ||
|
|
||
| if (connman.IsMasternodeOrDisconnectRequested(dmn->pdmnState->netInfo->GetPrimary())) { | ||
| WalletCJLogPrint(m_wallet, /* Continued */ | ||
|
|
@@ -1137,7 +1139,7 @@ bool CCoinJoinClientSession::StartNewQueue(CAmount nBalanceNeedsAnonymized, CCon | |
| return false; | ||
| } | ||
|
|
||
| m_clientman.AddUsedMasternode(dmn->collateralOutpoint); | ||
| m_clientman.AddUsedMasternode(dmn->proTxHash); | ||
|
|
||
| // skip next mn payments winners | ||
| if (dmn->pdmnState->nLastPaidHeight + nWeightedMnCount < mnList.GetHeight() + WinnersToSkip()) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -8,7 +8,7 @@ | |||||
| #include <univalue.h> | ||||||
| #include <util/time.h> | ||||||
|
|
||||||
| const std::string MasternodeMetaStore::SERIALIZATION_VERSION_STRING = "CMasternodeMetaMan-Version-4"; | ||||||
| const std::string MasternodeMetaStore::SERIALIZATION_VERSION_STRING = "CMasternodeMetaMan-Version-5"; | ||||||
|
|
||||||
| CMasternodeMetaMan::CMasternodeMetaMan() : | ||||||
| m_db{std::make_unique<db_type>("mncache.dat", "magicMasternodeCache")} | ||||||
|
|
@@ -153,10 +153,44 @@ void CMasternodeMetaMan::RememberPlatformBan(const uint256& inv_hash, PlatformBa | |||||
| m_seen_platform_bans.insert(inv_hash, std::move(msg)); | ||||||
| } | ||||||
|
|
||||||
| void CMasternodeMetaMan::AddUsedMasternode(const uint256& proTxHash) | ||||||
| { | ||||||
| LOCK(cs); | ||||||
| // Only add if not already present (prevents duplicates) | ||||||
| if (m_used_masternodes_set.insert(proTxHash).second) { | ||||||
| m_used_masternodes.push_back(proTxHash); | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| void CMasternodeMetaMan::RemoveUsedMasternodes(size_t nCount) | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: nCount -> count
Suggested change
|
||||||
| { | ||||||
| LOCK(cs); | ||||||
| size_t removed = 0; | ||||||
| while (removed < nCount && !m_used_masternodes.empty()) { | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| // Remove from both the set and the deque | ||||||
| m_used_masternodes_set.erase(m_used_masternodes.front()); | ||||||
| m_used_masternodes.pop_front(); | ||||||
| ++removed; | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| size_t CMasternodeMetaMan::GetUsedMasternodesCount() const | ||||||
| { | ||||||
| LOCK(cs); | ||||||
| return m_used_masternodes.size(); | ||||||
| } | ||||||
|
|
||||||
| bool CMasternodeMetaMan::IsUsedMasternode(const uint256& proTxHash) const | ||||||
| { | ||||||
| LOCK(cs); | ||||||
| return m_used_masternodes_set.find(proTxHash) != m_used_masternodes_set.end(); | ||||||
| } | ||||||
|
|
||||||
| std::string MasternodeMetaStore::ToString() const | ||||||
| { | ||||||
| LOCK(cs); | ||||||
| return strprintf("Masternodes: meta infos object count: %d, nDsqCount: %d", metaInfos.size(), nDsqCount); | ||||||
| return strprintf("Masternodes: meta infos object count: %d, nDsqCount: %d, used masternodes count: %d", | ||||||
| metaInfos.size(), nDsqCount, m_used_masternodes.size()); | ||||||
| } | ||||||
|
|
||||||
| uint256 PlatformBanMessage::GetHash() const { return ::SerializeHash(*this); } | ||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit(code-style):
nUsedMasternodesis not matched with our code style.prefix
nshould not be used.case of variable should be snake-case, such as
used_masternodesorused_masternodes_countor something similar.See doc/developer-notes.md:
I'd suggest to change log record to be a bit more human rather than use variable name in logs (even if it's debug):