mirror of
https://github.com/dashpay/dash.git
synced 2024-12-28 13:32:47 +01:00
3f8a7b2068
* Begin Merge 12193 Squashed 'src/univalue/' changes from 07947ff2da..51d3ab34ba 51d3ab34ba Merge #10: Add pushKV(key, boolean) function (replaces #5) 129bad96d5 [tests] test pushKV for boolean values b3c44c947f Pushing boolean value to univalue correctly git-subtree-dir: src/univalue git-subtree-split: 51d3ab34ba2857f0d03dc07250cb4a2b5e712e67 * scripted-diff: Use UniValue.pushKV instead of push_back(Pair()) (end #12193) -BEGIN VERIFY SCRIPT- git grep -l "push_back(Pair" | xargs sed -i "s/push_back(Pair(\(.*\)));/pushKV(\1);/g" -END VERIFY SCRIPT- Signed-off-by: pasta <pasta@dashboost.org> Co-authored-by: MarcoFalke <falke.marco@gmail.com>
137 lines
3.9 KiB
C++
137 lines
3.9 KiB
C++
// Copyright (c) 2014-2019 The Dash Core developers
|
|
// Distributed under the MIT/X11 software license, see the accompanying
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
#include <masternode/masternode-meta.h>
|
|
|
|
#include <timedata.h>
|
|
|
|
CMasternodeMetaMan mmetaman;
|
|
|
|
const std::string CMasternodeMetaMan::SERIALIZATION_VERSION_STRING = "CMasternodeMetaMan-Version-2";
|
|
|
|
UniValue CMasternodeMetaInfo::ToJson() const
|
|
{
|
|
UniValue ret(UniValue::VOBJ);
|
|
|
|
auto now = GetAdjustedTime();
|
|
|
|
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);
|
|
|
|
return ret;
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
// 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
|
|
// masternodes before we ever see a masternode that we know already mixed someone's funds ealier.
|
|
int64_t CMasternodeMetaMan::GetDsqThreshold(const uint256& proTxHash, int nMnCount)
|
|
{
|
|
LOCK(cs);
|
|
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;
|
|
}
|
|
|
|
void CMasternodeMetaMan::AllowMixing(const uint256& proTxHash)
|
|
{
|
|
LOCK(cs);
|
|
auto mm = GetMetaInfo(proTxHash);
|
|
nDsqCount++;
|
|
LOCK(mm->cs);
|
|
mm->nLastDsq = nDsqCount;
|
|
mm->nMixingTxCount = 0;
|
|
}
|
|
|
|
void CMasternodeMetaMan::DisallowMixing(const uint256& proTxHash)
|
|
{
|
|
LOCK(cs);
|
|
auto mm = GetMetaInfo(proTxHash);
|
|
|
|
LOCK(mm->cs);
|
|
mm->nMixingTxCount++;
|
|
}
|
|
|
|
bool CMasternodeMetaMan::AddGovernanceVote(const uint256& proTxHash, const uint256& nGovernanceObjectHash)
|
|
{
|
|
LOCK(cs);
|
|
auto mm = GetMetaInfo(proTxHash);
|
|
mm->AddGovernanceVote(nGovernanceObjectHash);
|
|
return true;
|
|
}
|
|
|
|
void CMasternodeMetaMan::RemoveGovernanceObject(const uint256& nGovernanceObjectHash)
|
|
{
|
|
LOCK(cs);
|
|
for(auto& p : metaInfos) {
|
|
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();
|
|
}
|
|
|
|
void CMasternodeMetaMan::CheckAndRemove()
|
|
{
|
|
|
|
}
|
|
|
|
std::string CMasternodeMetaMan::ToString() const
|
|
{
|
|
std::ostringstream info;
|
|
|
|
info << "Masternodes: meta infos object count: " << (int)metaInfos.size() <<
|
|
", nDsqCount: " << (int)nDsqCount;
|
|
return info.str();
|
|
}
|