2021-04-20 21:33:02 +02:00
|
|
|
// Copyright (c) 2014-2020 The Dash Core developers
|
2019-01-03 21:08:34 +01:00
|
|
|
// Distributed under the MIT/X11 software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
2021-10-01 21:19:08 +02:00
|
|
|
#include <masternode/meta.h>
|
2019-01-03 21:08:34 +01:00
|
|
|
|
2020-03-26 16:47:48 +01:00
|
|
|
#include <timedata.h>
|
2020-03-19 15:23:00 +01:00
|
|
|
|
2019-01-03 21:08:34 +01:00
|
|
|
CMasternodeMetaMan mmetaman;
|
|
|
|
|
2020-03-19 13:51:25 +01:00
|
|
|
const std::string CMasternodeMetaMan::SERIALIZATION_VERSION_STRING = "CMasternodeMetaMan-Version-2";
|
2019-01-03 21:08:34 +01:00
|
|
|
|
2020-03-19 15:23:00 +01:00
|
|
|
UniValue CMasternodeMetaInfo::ToJson() const
|
|
|
|
{
|
|
|
|
UniValue ret(UniValue::VOBJ);
|
|
|
|
|
|
|
|
auto now = GetAdjustedTime();
|
|
|
|
|
2020-06-18 11:17:23 +02:00
|
|
|
ret.pushKV("lastDSQ", nLastDsq);
|
|
|
|
ret.pushKV("mixingTxCount", nMixingTxCount);
|
|
|
|
ret.pushKV("lastOutboundAttempt", lastOutboundAttempt);
|
|
|
|
ret.pushKV("lastOutboundAttemptElapsed", now - lastOutboundAttempt);
|
|
|
|
ret.pushKV("lastOutboundSuccess", lastOutboundSuccess);
|
|
|
|
ret.pushKV("lastOutboundSuccessElapsed", now - lastOutboundSuccess);
|
2020-03-19 15:23:00 +01:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-01-03 21:08:34 +01:00
|
|
|
void CMasternodeMetaInfo::AddGovernanceVote(const uint256& nGovernanceObjectHash)
|
|
|
|
{
|
|
|
|
LOCK(cs);
|
|
|
|
// Insert a zero value, or not. Then increment the value regardless. This
|
|
|
|
// ensures the value is in the map.
|
|
|
|
const auto& pair = mapGovernanceObjectsVotedOn.emplace(nGovernanceObjectHash, 0);
|
|
|
|
pair.first->second++;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CMasternodeMetaInfo::RemoveGovernanceObject(const uint256& nGovernanceObjectHash)
|
|
|
|
{
|
|
|
|
LOCK(cs);
|
|
|
|
// Whether or not the govobj hash exists in the map first is irrelevant.
|
|
|
|
mapGovernanceObjectsVotedOn.erase(nGovernanceObjectHash);
|
|
|
|
}
|
|
|
|
|
|
|
|
CMasternodeMetaInfoPtr CMasternodeMetaMan::GetMetaInfo(const uint256& proTxHash, bool fCreate)
|
|
|
|
{
|
|
|
|
LOCK(cs);
|
|
|
|
auto it = metaInfos.find(proTxHash);
|
|
|
|
if (it != metaInfos.end()) {
|
|
|
|
return it->second;
|
|
|
|
}
|
|
|
|
if (!fCreate) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
it = metaInfos.emplace(proTxHash, std::make_shared<CMasternodeMetaInfo>(proTxHash)).first;
|
|
|
|
return it->second;
|
|
|
|
}
|
|
|
|
|
2020-05-02 17:50:03 +02:00
|
|
|
// We keep track of dsq (mixing queues) count to avoid using same masternodes for mixing too often.
|
|
|
|
// This threshold is calculated as the last dsq count this specific masternode was used in a mixing
|
|
|
|
// session plus a margin of 20% of masternode count. In other words we expect at least 20% of unique
|
2020-08-09 23:35:42 +02:00
|
|
|
// masternodes before we ever see a masternode that we know already mixed someone's funds earlier.
|
2020-05-02 17:50:03 +02:00
|
|
|
int64_t CMasternodeMetaMan::GetDsqThreshold(const uint256& proTxHash, int nMnCount)
|
|
|
|
{
|
|
|
|
auto metaInfo = GetMetaInfo(proTxHash);
|
|
|
|
if (metaInfo == nullptr) {
|
|
|
|
// return a threshold which is slightly above nDsqCount i.e. a no-go
|
|
|
|
return nDsqCount + 1;
|
|
|
|
}
|
|
|
|
return metaInfo->GetLastDsq() + nMnCount / 5;
|
|
|
|
}
|
|
|
|
|
2019-01-03 21:08:34 +01:00
|
|
|
void CMasternodeMetaMan::AllowMixing(const uint256& proTxHash)
|
|
|
|
{
|
|
|
|
auto mm = GetMetaInfo(proTxHash);
|
|
|
|
nDsqCount++;
|
2021-09-30 23:00:52 +02:00
|
|
|
mm->nLastDsq = nDsqCount.load();
|
2019-01-03 21:08:34 +01:00
|
|
|
mm->nMixingTxCount = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CMasternodeMetaMan::DisallowMixing(const uint256& proTxHash)
|
|
|
|
{
|
|
|
|
auto mm = GetMetaInfo(proTxHash);
|
|
|
|
mm->nMixingTxCount++;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CMasternodeMetaMan::AddGovernanceVote(const uint256& proTxHash, const uint256& nGovernanceObjectHash)
|
|
|
|
{
|
|
|
|
auto mm = GetMetaInfo(proTxHash);
|
|
|
|
mm->AddGovernanceVote(nGovernanceObjectHash);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CMasternodeMetaMan::RemoveGovernanceObject(const uint256& nGovernanceObjectHash)
|
|
|
|
{
|
|
|
|
LOCK(cs);
|
2021-09-30 20:59:42 +02:00
|
|
|
for(const auto& p : metaInfos) {
|
2019-01-03 21:08:34 +01:00
|
|
|
p.second->RemoveGovernanceObject(nGovernanceObjectHash);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<uint256> CMasternodeMetaMan::GetAndClearDirtyGovernanceObjectHashes()
|
|
|
|
{
|
|
|
|
LOCK(cs);
|
|
|
|
std::vector<uint256> vecTmp = std::move(vecDirtyGovernanceObjectHashes);
|
|
|
|
vecDirtyGovernanceObjectHashes.clear();
|
|
|
|
return vecTmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CMasternodeMetaMan::Clear()
|
|
|
|
{
|
|
|
|
LOCK(cs);
|
|
|
|
metaInfos.clear();
|
|
|
|
vecDirtyGovernanceObjectHashes.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string CMasternodeMetaMan::ToString() const
|
|
|
|
{
|
|
|
|
std::ostringstream info;
|
2021-09-30 23:00:52 +02:00
|
|
|
LOCK(cs);
|
2019-01-03 21:08:34 +01:00
|
|
|
info << "Masternodes: meta infos object count: " << (int)metaInfos.size() <<
|
|
|
|
", nDsqCount: " << (int)nDsqCount;
|
|
|
|
return info.str();
|
|
|
|
}
|