Dash Core Source Documentation (0.16.0.1)

Find detailed information regarding the Dash Core source code.

governance.h
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 
5 #ifndef GOVERNANCE_H
6 #define GOVERNANCE_H
7 
8 #include <bloom.h>
9 #include <cachemap.h>
10 #include <cachemultimap.h>
11 #include <chain.h>
15 #include <net.h>
16 #include <sync.h>
17 #include <timedata.h>
18 #include <util.h>
19 
20 #include <evo/deterministicmns.h>
21 
22 #include <univalue.h>
23 
24 class CGovernanceManager;
26 class CGovernanceObject;
27 class CGovernanceVote;
28 
30 
32  ExpirationInfo(int64_t _nExpirationTime, int _idFrom) :
33  nExpirationTime(_nExpirationTime), idFrom(_idFrom) {}
34 
35  int64_t nExpirationTime;
37 };
38 
39 typedef std::pair<CGovernanceObject, ExpirationInfo> object_info_pair_t;
40 
41 static const int RATE_BUFFER_SIZE = 5;
42 
44 {
45 private:
46  std::vector<int64_t> vecTimestamps;
47 
49 
50  int nDataEnd;
51 
53 
54 public:
57  nDataStart(0),
58  nDataEnd(0),
60  {
61  }
62 
63  void AddTimestamp(int64_t nTimestamp)
64  {
65  if ((nDataEnd == nDataStart) && !fBufferEmpty) {
66  // Buffer full, discard 1st element
68  }
69  vecTimestamps[nDataEnd] = nTimestamp;
71  fBufferEmpty = false;
72  }
73 
74  int64_t GetMinTimestamp()
75  {
76  int nIndex = nDataStart;
77  int64_t nMin = std::numeric_limits<int64_t>::max();
78  if (fBufferEmpty) {
79  return nMin;
80  }
81  do {
82  if (vecTimestamps[nIndex] < nMin) {
83  nMin = vecTimestamps[nIndex];
84  }
85  nIndex = (nIndex + 1) % RATE_BUFFER_SIZE;
86  } while (nIndex != nDataEnd);
87  return nMin;
88  }
89 
90  int64_t GetMaxTimestamp()
91  {
92  int nIndex = nDataStart;
93  int64_t nMax = 0;
94  if (fBufferEmpty) {
95  return nMax;
96  }
97  do {
98  if (vecTimestamps[nIndex] > nMax) {
99  nMax = vecTimestamps[nIndex];
100  }
101  nIndex = (nIndex + 1) % RATE_BUFFER_SIZE;
102  } while (nIndex != nDataEnd);
103  return nMax;
104  }
105 
106  int GetCount()
107  {
108  int nCount = 0;
109  if (fBufferEmpty) {
110  return 0;
111  }
112  if (nDataEnd > nDataStart) {
113  nCount = nDataEnd - nDataStart;
114  } else {
115  nCount = RATE_BUFFER_SIZE - nDataStart + nDataEnd;
116  }
117 
118  return nCount;
119  }
120 
121  double GetRate()
122  {
123  int nCount = GetCount();
124  if (nCount < RATE_BUFFER_SIZE) {
125  return 0.0;
126  }
127  int64_t nMin = GetMinTimestamp();
128  int64_t nMax = GetMaxTimestamp();
129  if (nMin == nMax) {
130  // multiple objects with the same timestamp => infinite rate
131  return 1.0e10;
132  }
133  return double(nCount) / double(nMax - nMin);
134  }
135 
137 
138  template <typename Stream, typename Operation>
139  inline void SerializationOp(Stream& s, Operation ser_action)
140  {
145  }
146 };
147 
148 //
149 // Governance Manager : Contains all proposals for the budget
150 //
152 {
153  friend class CGovernanceObject;
154 
155 public: // Types
157  last_object_rec(bool fStatusOKIn = true) :
158  triggerBuffer(),
159  fStatusOK(fStatusOKIn)
160  {
161  }
162 
164 
165  template <typename Stream, typename Operation>
166  inline void SerializationOp(Stream& s, Operation ser_action)
167  {
170  }
171 
173  bool fStatusOK;
174  };
175 
176 
177  typedef std::map<uint256, CGovernanceObject> object_m_t;
178 
179  typedef object_m_t::iterator object_m_it;
180 
181  typedef object_m_t::const_iterator object_m_cit;
182 
184 
185  typedef std::map<uint256, CGovernanceVote> vote_m_t;
186 
187  typedef vote_m_t::iterator vote_m_it;
188 
189  typedef vote_m_t::const_iterator vote_m_cit;
190 
192 
194 
195  typedef object_m_t::size_type size_type;
196 
197  typedef std::map<COutPoint, last_object_rec> txout_m_t;
198 
199  typedef txout_m_t::iterator txout_m_it;
200 
201  typedef std::set<uint256> hash_s_t;
202 
203  typedef hash_s_t::iterator hash_s_it;
204 
205  typedef hash_s_t::const_iterator hash_s_cit;
206 
207  typedef std::map<uint256, object_info_pair_t> object_info_m_t;
208 
209  typedef object_info_m_t::iterator object_info_m_it;
210 
211  typedef std::map<uint256, int64_t> hash_time_m_t;
212 
213  typedef hash_time_m_t::iterator hash_time_m_it;
214 
215 private:
216  static const int MAX_CACHE_SIZE = 1000000;
217 
218  static const std::string SERIALIZATION_VERSION_STRING;
219 
220  static const int MAX_TIME_FUTURE_DEVIATION;
221  static const int RELIABLE_PROPAGATION_TIME;
222 
223  int64_t nTimeLastDiff;
224 
225  // keep track of current block height
227 
228  // keep track of the scanning errors
230 
231  // mapErasedGovernanceObjects contains key-value pairs, where
232  // key - governance object's hash
233  // value - expiration time for deleted objects
235 
238 
240 
242 
244 
246 
248 
250 
252 
253  // used to check for changed voting keys
255 
257  {
258  bool& ref;
260 
261  public:
262  ScopedLockBool(CCriticalSection& _cs, bool& _ref, bool _value) :
263  ref(_ref)
264  {
265  AssertLockHeld(_cs);
266  fPrevValue = ref;
267  ref = _value;
268  }
269 
271  {
272  ref = fPrevValue;
273  }
274  };
275 
276 public:
277  // critical section to protect the inner data structures
279 
281 
282  virtual ~CGovernanceManager() {}
283 
289  bool ConfirmInventoryRequest(const CInv& inv);
290 
291  void SyncSingleObjVotes(CNode* pnode, const uint256& nProp, const CBloomFilter& filter, CConnman& connman);
292  void SyncObjects(CNode* pnode, CConnman& connman) const;
293 
294  void ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStream& vRecv, CConnman& connman);
295 
296  void DoMaintenance(CConnman& connman);
297 
299 
300  // These commands are only used in RPC
301  std::vector<CGovernanceVote> GetCurrentVotes(const uint256& nParentHash, const COutPoint& mnCollateralOutpointFilter) const;
302  std::vector<const CGovernanceObject*> GetAllNewerThan(int64_t nMoreThanTime) const;
303 
304  void AddGovernanceObject(CGovernanceObject& govobj, CConnman& connman, CNode* pfrom = nullptr);
305 
306  void UpdateCachesAndClean();
307 
309 
310  void Clear()
311  {
312  LOCK(cs);
313 
314  LogPrint(BCLog::GOBJECT, "Governance object manager was cleared\n");
315  mapObjects.clear();
320  mapLastMasternodeObject.clear();
321  }
322 
323  std::string ToString() const;
324  UniValue ToJson() const;
325 
327 
328  template <typename Stream, typename Operation>
329  inline void SerializationOp(Stream& s, Operation ser_action)
330  {
331  LOCK(cs);
332  std::string strVersion;
333  if (ser_action.ForRead()) {
334  Clear();
335  READWRITE(strVersion);
336  if (strVersion != SERIALIZATION_VERSION_STRING) {
337  return;
338  }
339  } else {
340  strVersion = SERIALIZATION_VERSION_STRING;
341  READWRITE(strVersion);
342  }
343 
350  }
351 
352  void UpdatedBlockTip(const CBlockIndex* pindex, CConnman& connman);
353  int64_t GetLastDiffTime() const { return nTimeLastDiff; }
354  void UpdateLastDiffTime(int64_t nTimeIn) { nTimeLastDiff = nTimeIn; }
355 
356  int GetCachedBlockHeight() const { return nCachedBlockHeight; }
357 
358  // Accessors for thread-safe access to maps
359  bool HaveObjectForHash(const uint256& nHash) const;
360 
361  bool HaveVoteForHash(const uint256& nHash) const;
362 
363  int GetVoteCount() const;
364 
365  bool SerializeObjectForHash(const uint256& nHash, CDataStream& ss) const;
366 
367  bool SerializeVoteForHash(const uint256& nHash, CDataStream& ss) const;
368 
370  {
371  LOCK(cs);
372  mapPostponedObjects.insert(std::make_pair(govobj.GetHash(), govobj));
373  }
374 
375  void MasternodeRateUpdate(const CGovernanceObject& govobj);
376 
377  bool MasternodeRateCheck(const CGovernanceObject& govobj, bool fUpdateFailStatus = false);
378 
379  bool MasternodeRateCheck(const CGovernanceObject& govobj, bool fUpdateFailStatus, bool fForce, bool& fRateCheckBypassed);
380 
381  bool ProcessVoteAndRelay(const CGovernanceVote& vote, CGovernanceException& exception, CConnman& connman)
382  {
383  bool fOK = ProcessVote(nullptr, vote, exception, connman);
384  if (fOK) {
385  vote.Relay(connman);
386  }
387  return fOK;
388  }
389 
390  void CheckPostponedObjects(CConnman& connman);
391 
392  bool AreRateChecksEnabled() const
393  {
394  LOCK(cs);
395  return fRateChecksEnabled;
396  }
397 
398  void InitOnLoad();
399 
400  int RequestGovernanceObjectVotes(CNode* pnode, CConnman& connman);
401  int RequestGovernanceObjectVotes(const std::vector<CNode*>& vNodesCopy, CConnman& connman);
402 
403 private:
404  void RequestGovernanceObject(CNode* pfrom, const uint256& nHash, CConnman& connman, bool fUseFilter = false);
405 
406  void AddInvalidVote(const CGovernanceVote& vote)
407  {
408  cmapInvalidVotes.Insert(vote.GetHash(), vote);
409  }
410 
411  bool ProcessVote(CNode* pfrom, const CGovernanceVote& vote, CGovernanceException& exception, CConnman& connman);
412 
414  bool AcceptObjectMessage(const uint256& nHash);
415 
417  bool AcceptVoteMessage(const uint256& nHash);
418 
419  static bool AcceptMessage(const uint256& nHash, hash_s_t& setHash);
420 
421  void CheckOrphanVotes(CGovernanceObject& govobj, CGovernanceException& exception, CConnman& connman);
422 
423  void RebuildIndexes();
424 
425  void AddCachedTriggers();
426 
427  void RequestOrphanObjects(CConnman& connman);
428 
429  void CleanOrphanObjects();
430 
431  void RemoveInvalidVotes();
432 
433 };
434 
435 #endif
object_m_t mapPostponedObjects
Definition: governance.h:236
static const int RATE_BUFFER_SIZE
Definition: governance.h:41
txout_m_t::iterator txout_m_it
Definition: governance.h:199
uint256 GetHash() const
GetHash()
Governance Object.
void SyncSingleObjVotes(CNode *pnode, const uint256 &nProp, const CBloomFilter &filter, CConnman &connman)
Definition: governance.cpp:615
#define READWRITE(obj)
Definition: serialize.h:165
CacheMultiMap< uint256, vote_time_pair_t > vote_cmm_t
Definition: governance.h:193
std::map< COutPoint, last_object_rec > txout_m_t
Definition: governance.h:197
void MasternodeRateUpdate(const CGovernanceObject &govobj)
Definition: governance.cpp:711
bool MasternodeRateCheck(const CGovernanceObject &govobj, bool fUpdateFailStatus=false)
Definition: governance.cpp:733
bool HaveVoteForHash(const uint256 &nHash) const
Definition: governance.cpp:66
void AddTimestamp(int64_t nTimestamp)
Definition: governance.h:63
void UpdateCachesAndClean()
Definition: governance.cpp:351
inv message data
Definition: protocol.h:429
int64_t nExpirationTime
Definition: governance.h:35
uint256 GetHash() const
CDeterministicMNList lastMNListForVotingKeys
Definition: governance.h:254
vote_m_t::iterator vote_m_it
Definition: governance.h:187
static bool AcceptMessage(const uint256 &nHash, hash_s_t &setHash)
BloomFilter is a probabilistic filter which SPV clients provide so that we can filter the transaction...
Definition: bloom.h:46
std::pair< CGovernanceObject, ExpirationInfo > object_info_pair_t
Definition: governance.h:39
bool ProcessVoteAndRelay(const CGovernanceVote &vote, CGovernanceException &exception, CConnman &connman)
Definition: governance.h:381
false true true true
Definition: bls_dkg.cpp:176
bool HaveObjectForHash(const uint256 &nHash) const
Definition: governance.cpp:47
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:103
bool ProcessVote(CNode *pfrom, const CGovernanceVote &vote, CGovernanceException &exception, CConnman &connman)
Definition: governance.cpp:803
std::vector< int64_t > vecTimestamps
Definition: governance.h:46
CacheMap< uint256, CGovernanceObject * > object_ref_cm_t
Definition: governance.h:183
hash_s_t::const_iterator hash_s_cit
Definition: governance.h:205
void SerializationOp(Stream &s, Operation ser_action)
Definition: governance.h:329
std::map< uint256, CGovernanceObject > object_m_t
Definition: governance.h:177
void AddPostponedObject(const CGovernanceObject &govobj)
Definition: governance.h:369
CGovernanceObject * FindGovernanceObject(const uint256 &nHash)
Definition: governance.cpp:463
ExpirationInfo(int64_t _nExpirationTime, int _idFrom)
Definition: governance.h:32
void Clear()
Definition: cachemap.h:87
std::map< uint256, object_info_pair_t > object_info_m_t
Definition: governance.h:207
static const int MAX_CACHE_SIZE
Definition: governance.h:216
object_m_t::const_iterator object_m_cit
Definition: governance.h:181
CGovernanceManager governance
Definition: governance.cpp:23
int GetCachedBlockHeight() const
Definition: governance.h:356
void AddGovernanceObject(CGovernanceObject &govobj, CConnman &connman, CNode *pfrom=nullptr)
Definition: governance.cpp:291
void RequestGovernanceObject(CNode *pfrom, const uint256 &nHash, CConnman &connman, bool fUseFilter=false)
Definition: governance.cpp:922
object_m_t::size_type size_type
Definition: governance.h:195
#define LOCK(cs)
Definition: sync.h:178
int64_t GetMaxTimestamp()
Definition: governance.h:90
void CheckPostponedObjects(CConnman &connman)
Definition: governance.cpp:857
static const std::string SERIALIZATION_VERSION_STRING
Definition: governance.h:218
int RequestGovernanceObjectVotes(CNode *pnode, CConnman &connman)
Definition: governance.cpp:959
object_m_t::iterator object_m_it
Definition: governance.h:179
void RequestOrphanObjects(CConnman &connman)
txout_m_t mapLastMasternodeObject
Definition: governance.h:245
std::map< uint256, CGovernanceVote > vote_m_t
Definition: governance.h:185
void ProcessMessage(CNode *pfrom, const std::string &strCommand, CDataStream &vRecv, CConnman &connman)
Definition: governance.cpp:88
object_info_m_t::iterator object_info_m_it
Definition: governance.h:209
int GetVoteCount() const
Definition: governance.cpp:74
std::string ToString() const
static const int RELIABLE_PROPAGATION_TIME
Definition: governance.h:221
int64_t NodeId
Definition: net.h:109
A class which encapsulates information about a governance exception condition.
Definition: net.h:136
std::vector< const CGovernanceObject * > GetAllNewerThan(int64_t nMoreThanTime) const
Definition: governance.cpp:516
bool SerializeObjectForHash(const uint256 &nHash, CDataStream &ss) const
Definition: governance.cpp:53
void CheckOrphanVotes(CGovernanceObject &govobj, CGovernanceException &exception, CConnman &connman)
Definition: governance.cpp:266
bool ConfirmInventoryRequest(const CInv &inv)
This is called by AlreadyHave in net_processing.cpp as part of the inventory retrieval process...
Definition: governance.cpp:562
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:26
bool AcceptVoteMessage(const uint256 &nHash)
Called to indicate a requested vote has been received.
std::vector< CGovernanceVote > GetCurrentVotes(const uint256 &nParentHash, const COutPoint &mnCollateralOutpointFilter) const
Definition: governance.cpp:472
static const int MAX_TIME_FUTURE_DEVIATION
Definition: governance.h:220
void UpdatedBlockTip(const CBlockIndex *pindex, CConnman &connman)
bool SerializeVoteForHash(const uint256 &nHash, CDataStream &ss) const
Definition: governance.cpp:80
void SerializationOp(Stream &s, Operation ser_action)
Definition: governance.h:139
object_ref_cm_t cmapVoteToObject
Definition: governance.h:239
#define LogPrint(category,...)
Definition: util.h:214
ScopedLockBool(CCriticalSection &_cs, bool &_ref, bool _value)
Definition: governance.h:262
hash_s_t setAdditionalRelayObjects
Definition: governance.h:237
256-bit opaque blob.
Definition: uint256.h:123
hash_s_t setRequestedObjects
Definition: governance.h:247
std::set< uint256 > hash_s_t
Definition: governance.h:201
vote_cmm_t cmmapOrphanVotes
Definition: governance.h:243
void SyncObjects(CNode *pnode, CConnman &connman) const
Definition: governance.cpp:664
virtual ~CGovernanceManager()
Definition: governance.h:282
The block chain is a tree shaped structure starting with the genesis block at the root...
Definition: chain.h:170
double GetRate()
Definition: governance.h:121
CacheMap< uint256, CGovernanceVote > vote_cm_t
Definition: governance.h:191
void Relay(CConnman &connman) const
hash_s_t setRequestedVotes
Definition: governance.h:249
int64_t GetMinTimestamp()
Definition: governance.h:74
hash_s_t::iterator hash_s_it
Definition: governance.h:203
hash_time_m_t::iterator hash_time_m_it
Definition: governance.h:213
hash_time_m_t mapErasedGovernanceObjects
Definition: governance.h:234
last_object_rec(bool fStatusOKIn=true)
Definition: governance.h:157
bool Insert(const K &key, const V &value)
Definition: cachemap.h:106
int64_t nTimeLastDiff
Definition: governance.h:223
CCriticalSection cs
Definition: governance.h:278
vote_m_t::const_iterator vote_m_cit
Definition: governance.h:189
UniValue ToJson() const
void SerializationOp(Stream &s, Operation ser_action)
Definition: governance.h:166
Information about a peer.
Definition: net.h:800
bool AreRateChecksEnabled() const
Definition: governance.h:392
vote_cm_t cmapInvalidVotes
Definition: governance.h:241
int64_t GetLastDiffTime() const
Definition: governance.h:353
AssertLockHeld(g_cs_orphans)
std::map< uint256, int64_t > hash_time_m_t
Definition: governance.h:211
NodeId idFrom
Definition: governance.h:36
void AddInvalidVote(const CGovernanceVote &vote)
Definition: governance.h:406
Wrapped mutex: supports recursive locking, but no waiting TODO: We should move away from using the re...
Definition: sync.h:94
void UpdateLastDiffTime(int64_t nTimeIn)
Definition: governance.h:354
object_m_t mapObjects
Definition: governance.h:229
bool AcceptObjectMessage(const uint256 &nHash)
Called to indicate a requested object has been received.
void DoMaintenance(CConnman &connman)
Definition: governance.cpp:547
Released under the MIT license