Dash Core Source Documentation (0.16.0.1)

Find detailed information regarding the Dash Core source code.

masternode-meta.cpp
Go to the documentation of this file.
1 // Copyright (c) 2014-2019 The Dash Core developers
2 // Distributed under the MIT/X11 software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
6 
7 #include <timedata.h>
8 
10 
11 const std::string CMasternodeMetaMan::SERIALIZATION_VERSION_STRING = "CMasternodeMetaMan-Version-2";
12 
14 {
16 
17  auto now = GetAdjustedTime();
18 
19  ret.push_back(Pair("lastDSQ", nLastDsq));
20  ret.push_back(Pair("mixingTxCount", nMixingTxCount));
21  ret.push_back(Pair("lastOutboundAttempt", lastOutboundAttempt));
22  ret.push_back(Pair("lastOutboundAttemptElapsed", now - lastOutboundAttempt));
23  ret.push_back(Pair("lastOutboundSuccess", lastOutboundSuccess));
24  ret.push_back(Pair("lastOutboundSuccessElapsed", now - lastOutboundSuccess));
25 
26  return ret;
27 }
28 
29 void CMasternodeMetaInfo::AddGovernanceVote(const uint256& nGovernanceObjectHash)
30 {
31  LOCK(cs);
32  // Insert a zero value, or not. Then increment the value regardless. This
33  // ensures the value is in the map.
34  const auto& pair = mapGovernanceObjectsVotedOn.emplace(nGovernanceObjectHash, 0);
35  pair.first->second++;
36 }
37 
38 void CMasternodeMetaInfo::RemoveGovernanceObject(const uint256& nGovernanceObjectHash)
39 {
40  LOCK(cs);
41  // Whether or not the govobj hash exists in the map first is irrelevant.
42  mapGovernanceObjectsVotedOn.erase(nGovernanceObjectHash);
43 }
44 
46 {
47  LOCK(cs);
48  auto it = metaInfos.find(proTxHash);
49  if (it != metaInfos.end()) {
50  return it->second;
51  }
52  if (!fCreate) {
53  return nullptr;
54  }
55  it = metaInfos.emplace(proTxHash, std::make_shared<CMasternodeMetaInfo>(proTxHash)).first;
56  return it->second;
57 }
58 
59 // We keep track of dsq (mixing queues) count to avoid using same masternodes for mixing too often.
60 // This threshold is calculated as the last dsq count this specific masternode was used in a mixing
61 // session plus a margin of 20% of masternode count. In other words we expect at least 20% of unique
62 // masternodes before we ever see a masternode that we know already mixed someone's funds ealier.
63 int64_t CMasternodeMetaMan::GetDsqThreshold(const uint256& proTxHash, int nMnCount)
64 {
65  LOCK(cs);
66  auto metaInfo = GetMetaInfo(proTxHash);
67  if (metaInfo == nullptr) {
68  // return a threshold which is slightly above nDsqCount i.e. a no-go
69  return nDsqCount + 1;
70  }
71  return metaInfo->GetLastDsq() + nMnCount / 5;
72 }
73 
75 {
76  LOCK(cs);
77  auto mm = GetMetaInfo(proTxHash);
78  nDsqCount++;
79  LOCK(mm->cs);
80  mm->nLastDsq = nDsqCount;
81  mm->nMixingTxCount = 0;
82 }
83 
85 {
86  LOCK(cs);
87  auto mm = GetMetaInfo(proTxHash);
88 
89  LOCK(mm->cs);
90  mm->nMixingTxCount++;
91 }
92 
93 bool CMasternodeMetaMan::AddGovernanceVote(const uint256& proTxHash, const uint256& nGovernanceObjectHash)
94 {
95  LOCK(cs);
96  auto mm = GetMetaInfo(proTxHash);
97  mm->AddGovernanceVote(nGovernanceObjectHash);
98  return true;
99 }
100 
101 void CMasternodeMetaMan::RemoveGovernanceObject(const uint256& nGovernanceObjectHash)
102 {
103  LOCK(cs);
104  for(auto& p : metaInfos) {
105  p.second->RemoveGovernanceObject(nGovernanceObjectHash);
106  }
107 }
108 
110 {
111  LOCK(cs);
112  std::vector<uint256> vecTmp = std::move(vecDirtyGovernanceObjectHashes);
114  return vecTmp;
115 }
116 
118 {
119  LOCK(cs);
120  metaInfos.clear();
122 }
123 
125 {
126 
127 }
128 
129 std::string CMasternodeMetaMan::ToString() const
130 {
131  std::ostringstream info;
132 
133  info << "Masternodes: meta infos object count: " << (int)metaInfos.size() <<
134  ", nDsqCount: " << (int)nDsqCount;
135  return info.str();
136 }
void DisallowMixing(const uint256 &proTxHash)
int64_t GetDsqThreshold(const uint256 &proTxHash, int nMnCount)
UniValue ToJson() const
static const std::string SERIALIZATION_VERSION_STRING
std::map< uint256, int > mapGovernanceObjectsVotedOn
void RemoveGovernanceObject(const uint256 &nGovernanceObjectHash)
CMasternodeMetaMan mmetaman
std::vector< uint256 > GetAndClearDirtyGovernanceObjectHashes()
CCriticalSection cs
bool push_back(const UniValue &val)
Definition: univalue.cpp:110
void AddGovernanceVote(const uint256 &nGovernanceObjectHash)
#define LOCK(cs)
Definition: sync.h:178
std::shared_ptr< CMasternodeMetaInfo > CMasternodeMetaInfoPtr
static std::pair< std::string, UniValue > Pair(const char *cKey, const char *cVal)
Definition: univalue.h:185
std::map< uint256, CMasternodeMetaInfoPtr > metaInfos
256-bit opaque blob.
Definition: uint256.h:123
void AllowMixing(const uint256 &proTxHash)
std::string ToString() const
int64_t GetAdjustedTime()
Definition: timedata.cpp:35
CCriticalSection cs
std::vector< uint256 > vecDirtyGovernanceObjectHashes
bool AddGovernanceVote(const uint256 &proTxHash, const uint256 &nGovernanceObjectHash)
CMasternodeMetaInfoPtr GetMetaInfo(const uint256 &proTxHash, bool fCreate=true)
void RemoveGovernanceObject(const uint256 &nGovernanceObjectHash)
Released under the MIT license