Dash Core Source Documentation (0.16.0.1)

Find detailed information regarding the Dash Core source code.

governance-object.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_OBJECT_H
6 #define GOVERNANCE_OBJECT_H
7 
8 #include <cachemultimap.h>
12 #include <key.h>
13 #include <net.h>
14 #include <sync.h>
15 #include <util.h>
16 #include <utilstrencodings.h>
17 #include <bls/bls.h>
18 
19 #include <univalue.h>
20 
21 class CGovernanceManager;
23 class CGovernanceObject;
24 class CGovernanceVote;
25 
26 static const int MIN_GOVERNANCE_PEER_PROTO_VERSION = 70213;
27 static const int GOVERNANCE_FILTER_PROTO_VERSION = 70206;
28 static const int GOVERNANCE_POSE_BANNED_VOTES_VERSION = 70215;
29 
30 static const double GOVERNANCE_FILTER_FP_RATE = 0.001;
31 
32 static const int GOVERNANCE_OBJECT_UNKNOWN = 0;
33 static const int GOVERNANCE_OBJECT_PROPOSAL = 1;
34 static const int GOVERNANCE_OBJECT_TRIGGER = 2;
35 
36 static const CAmount GOVERNANCE_PROPOSAL_FEE_TX = (5.0 * COIN);
37 
38 static const int64_t GOVERNANCE_FEE_CONFIRMATIONS = 6;
39 static const int64_t GOVERNANCE_MIN_RELAY_FEE_CONFIRMATIONS = 1;
40 static const int64_t GOVERNANCE_UPDATE_MIN = 60 * 60;
41 static const int64_t GOVERNANCE_DELETION_DELAY = 10 * 60;
42 static const int64_t GOVERNANCE_ORPHAN_EXPIRATION_TIME = 10 * 60;
43 
44 // FOR SEEN MAP ARRAYS - GOVERNANCE OBJECTS AND VOTES
45 static const int SEEN_OBJECT_IS_VALID = 0;
46 static const int SEEN_OBJECT_ERROR_INVALID = 1;
47 static const int SEEN_OBJECT_EXECUTED = 3; //used for triggers
48 static const int SEEN_OBJECT_UNKNOWN = 4; // the default
49 
50 typedef std::pair<CGovernanceVote, int64_t> vote_time_pair_t;
51 
52 inline bool operator<(const vote_time_pair_t& p1, const vote_time_pair_t& p2)
53 {
54  return (p1.first < p2.first);
55 }
56 
59  int64_t nTime;
60  int64_t nCreationTime;
61 
62  vote_instance_t(vote_outcome_enum_t eOutcomeIn = VOTE_OUTCOME_NONE, int64_t nTimeIn = 0, int64_t nCreationTimeIn = 0) :
63  eOutcome(eOutcomeIn),
64  nTime(nTimeIn),
65  nCreationTime(nCreationTimeIn)
66  {
67  }
68 
70 
71  template <typename Stream, typename Operation>
72  inline void SerializationOp(Stream& s, Operation ser_action)
73  {
74  int nOutcome = int(eOutcome);
75  READWRITE(nOutcome);
78  if (ser_action.ForRead()) {
79  eOutcome = vote_outcome_enum_t(nOutcome);
80  }
81  }
82 };
83 
84 typedef std::map<int, vote_instance_t> vote_instance_m_t;
85 
86 typedef vote_instance_m_t::iterator vote_instance_m_it;
87 
88 typedef vote_instance_m_t::const_iterator vote_instance_m_cit;
89 
90 struct vote_rec_t {
92 
94 
95  template <typename Stream, typename Operation>
96  inline void SerializationOp(Stream& s, Operation ser_action)
97  {
99  }
100 };
101 
108 {
109 public: // Types
110  typedef std::map<COutPoint, vote_rec_t> vote_m_t;
111 
112  typedef vote_m_t::iterator vote_m_it;
113 
114  typedef vote_m_t::const_iterator vote_m_cit;
115 
117 
118 private:
121 
124 
127 
130 
132  int64_t nTime;
133 
135  int64_t nDeletionTime;
136 
139 
141  std::vector<unsigned char> vchData;
142 
145  std::vector<unsigned char> vchSig;
146 
150 
151  // VARIOUS FLAGS FOR OBJECT / SET VIA MASTERNODE VOTING
152 
155 
158 
161 
166 
169 
171  bool fExpired;
172 
175 
177 
179 
180 public:
182 
183  CGovernanceObject(const uint256& nHashParentIn, int nRevisionIn, int64_t nTime, const uint256& nCollateralHashIn, const std::string& strDataHexIn);
184 
185  CGovernanceObject(const CGovernanceObject& other);
186 
187  // Public Getter methods
188 
189  int64_t GetCreationTime() const
190  {
191  return nTime;
192  }
193 
194  int64_t GetDeletionTime() const
195  {
196  return nDeletionTime;
197  }
198 
199  int GetObjectType() const
200  {
201  return nObjectType;
202  }
203 
204  const uint256& GetCollateralHash() const
205  {
206  return nCollateralHash;
207  }
208 
210  {
211  return masternodeOutpoint;
212  }
213 
214  bool IsSetCachedFunding() const
215  {
216  return fCachedFunding;
217  }
218 
219  bool IsSetCachedValid() const
220  {
221  return fCachedValid;
222  }
223 
224  bool IsSetCachedDelete() const
225  {
226  return fCachedDelete;
227  }
228 
229  bool IsSetCachedEndorsed() const
230  {
231  return fCachedEndorsed;
232  }
233 
234  bool IsSetDirtyCache() const
235  {
236  return fDirtyCache;
237  }
238 
239  bool IsSetExpired() const
240  {
241  return fExpired;
242  }
243 
244  void SetExpired()
245  {
246  fExpired = true;
247  }
248 
250  {
251  return fileVotes;
252  }
253 
254  // Signature related functions
255 
256  void SetMasternodeOutpoint(const COutPoint& outpoint);
257  bool Sign(const CBLSSecretKey& key);
258  bool CheckSignature(const CBLSPublicKey& pubKey) const;
259 
260  uint256 GetSignatureHash() const;
261 
262  // CORE OBJECT FUNCTIONS
263 
264  bool IsValidLocally(std::string& strError, bool fCheckCollateral) const;
265 
266  bool IsValidLocally(std::string& strError, bool& fMissingConfirmations, bool fCheckCollateral) const;
267 
269  bool IsCollateralValid(std::string& strError, bool& fMissingConfirmations) const;
270 
271  void UpdateLocalValidity();
272 
274 
275  void PrepareDeletion(int64_t nDeletionTime_)
276  {
277  fCachedDelete = true;
278  if (nDeletionTime == 0) {
279  nDeletionTime = nDeletionTime_;
280  }
281  }
282 
284 
286 
287  void Relay(CConnman& connman);
288 
289  uint256 GetHash() const;
290 
291  // GET VOTE COUNT FOR SIGNAL
292 
293  int CountMatchingVotes(vote_signal_enum_t eVoteSignalIn, vote_outcome_enum_t eVoteOutcomeIn) const;
294 
295  int GetAbsoluteYesCount(vote_signal_enum_t eVoteSignalIn) const;
296  int GetAbsoluteNoCount(vote_signal_enum_t eVoteSignalIn) const;
297  int GetYesCount(vote_signal_enum_t eVoteSignalIn) const;
298  int GetNoCount(vote_signal_enum_t eVoteSignalIn) const;
299  int GetAbstainCount(vote_signal_enum_t eVoteSignalIn) const;
300 
301  bool GetCurrentMNVotes(const COutPoint& mnCollateralOutpoint, vote_rec_t& voteRecord) const;
302 
303  // FUNCTIONS FOR DEALING WITH DATA STRING
304 
305  std::string GetDataAsHexString() const;
306  std::string GetDataAsPlainString() const;
307 
308  // SERIALIZER
309 
311 
312  template <typename Stream, typename Operation>
313  inline void SerializationOp(Stream& s, Operation ser_action)
314  {
315  // SERIALIZE DATA FOR SAVING/LOADING OR NETWORK FUNCTIONS
318  READWRITE(nTime);
323  if (!(s.GetType() & SER_GETHASH)) {
324  READWRITE(vchSig);
325  }
326  if (s.GetType() & SER_DISK) {
327  // Only include these for the disk file format
328  LogPrint(BCLog::GOBJECT, "CGovernanceObject::SerializationOp Reading/writing votes from/to disk\n");
333  LogPrint(BCLog::GOBJECT, "CGovernanceObject::SerializationOp hash = %s, vote count = %d\n", GetHash().ToString(), fileVotes.GetVoteCount());
334  }
335 
336  // AFTER DESERIALIZATION OCCURS, CACHED VARIABLES MUST BE CALCULATED MANUALLY
337  }
338 
339  // FUNCTIONS FOR DEALING WITH DATA STRING
340  void LoadData();
341  void GetData(UniValue& objResult);
342 
343  bool ProcessVote(CNode* pfrom,
344  const CGovernanceVote& vote,
345  CGovernanceException& exception,
346  CConnman& connman);
347 
349  void ClearMasternodeVotes();
350 
351  // Revalidate all votes from this MN and delete them if validation fails.
352  // This is the case for DIP3 MNs that changed voting or operator keys and
353  // also for MNs that were removed from the list completely.
354  // Returns deleted vote hashes.
355  std::set<uint256> RemoveInvalidVotes(const COutPoint& mnOutpoint);
356 };
357 
358 
359 #endif
bool IsCollateralValid(std::string &strError, bool &fMissingConfirmations) const
Check the collateral transaction for the budget proposal/finalized budget.
Map like container that keeps the N most recently added items.
Definition: cachemultimap.h:21
std::string GetDataAsHexString() const
GetData - As
Governance Object.
void LoadData()
LoadData
static const int64_t GOVERNANCE_FEE_CONFIRMATIONS
static const int64_t GOVERNANCE_ORPHAN_EXPIRATION_TIME
#define READWRITE(obj)
Definition: serialize.h:165
bool IsSetDirtyCache() const
void SerializationOp(Stream &s, Operation ser_action)
std::pair< CGovernanceVote, int64_t > vote_time_pair_t
vote_instance_m_t mapInstances
int GetAbstainCount(vote_signal_enum_t eVoteSignalIn) const
const CGovernanceObjectVoteFile & GetVoteFile() const
uint256 GetHash() const
static const int MIN_GOVERNANCE_PEER_PROTO_VERSION
static const CAmount COIN
Definition: amount.h:14
const COutPoint & GetMasternodeOutpoint() const
bool CheckSignature(const CBLSPublicKey &pubKey) const
static const CAmount GOVERNANCE_PROPOSAL_FEE_TX
std::string strLocalValidityError
void SetMasternodeOutpoint(const COutPoint &outpoint)
const uint256 & GetCollateralHash() const
static const int64_t GOVERNANCE_MIN_RELAY_FEE_CONFIRMATIONS
static const int GOVERNANCE_OBJECT_PROPOSAL
void PrepareDeletion(int64_t nDeletionTime_)
bool IsSetCachedFunding() const
void GetData(UniValue &objResult)
GetData - Example usage:
static const int SEEN_OBJECT_ERROR_INVALID
bool fCachedLocalValidity
is valid by blockchain
uint256 nCollateralHash
fee-tx
vote_outcome_enum_t eOutcome
bool IsSetCachedEndorsed() const
static const double GOVERNANCE_FILTER_FP_RATE
void SerializationOp(Stream &s, Operation ser_action)
bool fCachedFunding
true == minimum network support has been reached for this object to be funded (doesn&#39;t mean it will f...
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
UniValue GetJSONObject()
Return the actual object from the vchData JSON structure.
bool ProcessVote(CNode *pfrom, const CGovernanceVote &vote, CGovernanceException &exception, CConnman &connman)
COutPoint masternodeOutpoint
Masternode info for signed objects.
int GetNoCount(vote_signal_enum_t eVoteSignalIn) const
int64_t nTime
time this object was created
static const int GOVERNANCE_POSE_BANNED_VOTES_VERSION
int GetObjectType() const
std::set< uint256 > RemoveInvalidVotes(const COutPoint &mnOutpoint)
std::vector< unsigned char > vchSig
int nRevision
object revision in the system
bool Sign(const CBLSSecretKey &key)
bool fCachedValid
true == minimum network has been reached flagging this object as a valid and understood governance ob...
CGovernanceObjectVoteFile fileVotes
uint256 nHashParent
parent object, 0 is root
void Relay(CConnman &connman)
std::map< COutPoint, vote_rec_t > vote_m_t
bool IsSetCachedDelete() const
static const int GOVERNANCE_OBJECT_TRIGGER
int GetAbsoluteNoCount(vote_signal_enum_t eVoteSignalIn) const
std::map< int, vote_instance_t > vote_instance_m_t
A class which encapsulates information about a governance exception condition.
Definition: net.h:136
static const int SEEN_OBJECT_IS_VALID
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:26
static const int GOVERNANCE_FILTER_PROTO_VERSION
uint256 GetSignatureHash() const
static const int SEEN_OBJECT_EXECUTED
static const int SEEN_OBJECT_UNKNOWN
int64_t GetCreationTime() const
std::vector< unsigned char > vchData
Data field - can be used for anything.
#define LogPrint(category,...)
Definition: util.h:214
bool IsValidLocally(std::string &strError, bool fCheckCollateral) const
256-bit opaque blob.
Definition: uint256.h:123
int nObjectType
Object typecode.
std::string GetDataAsPlainString() const
vote_instance_t(vote_outcome_enum_t eOutcomeIn=VOTE_OUTCOME_NONE, int64_t nTimeIn=0, int64_t nCreationTimeIn=0)
int64_t nDeletionTime
time this object was marked for deletion
void ClearMasternodeVotes()
Called when MN&#39;s which have voted on this object have been removed.
int GetAbsoluteYesCount(vote_signal_enum_t eVoteSignalIn) const
Get specific vote counts for each outcome (funding, validity, etc)
static const int64_t GOVERNANCE_UPDATE_MIN
bool fUnparsable
Failed to parse object data.
vote_outcome_enum_t
vote_m_t::iterator vote_m_it
CAmount GetMinCollateralFee() const
bool IsSetExpired() const
void SerializationOp(Stream &s, Operation ser_action)
int GetYesCount(vote_signal_enum_t eVoteSignalIn) const
vote_signal_enum_t
bool fExpired
Object is no longer of interest.
static const int GOVERNANCE_OBJECT_UNKNOWN
CacheMultiMap< COutPoint, vote_time_pair_t > vote_cmm_t
int CountMatchingVotes(vote_signal_enum_t eVoteSignalIn, vote_outcome_enum_t eVoteOutcomeIn) const
bool IsSetCachedValid() const
Information about a peer.
Definition: net.h:800
Represents the collection of votes associated with a given CGovernanceObject Recently received votes ...
static const int64_t GOVERNANCE_DELETION_DELAY
bool fCachedEndorsed
true == minimum network support has been reached flagging this object as endorsed by an elected repre...
vote_m_t::const_iterator vote_m_cit
bool fDirtyCache
object was updated and cached values should be updated soon
bool operator<(const vote_time_pair_t &p1, const vote_time_pair_t &p2)
bool fCachedDelete
true == minimum network support has been reached saying this object should be deleted from the system...
Wrapped mutex: supports recursive locking, but no waiting TODO: We should move away from using the re...
Definition: sync.h:94
CCriticalSection cs
critical section to protect the inner data structures
bool GetCurrentMNVotes(const COutPoint &mnCollateralOutpoint, vote_rec_t &voteRecord) const
vote_instance_m_t::const_iterator vote_instance_m_cit
vote_instance_m_t::iterator vote_instance_m_it
int64_t GetDeletionTime() const
Released under the MIT license