Dash Core  0.12.2.1
P2P Digital Currency
governance-object.h
Go to the documentation of this file.
1 // Copyright (c) 2014-2017 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 //#define ENABLE_DASH_DEBUG
9 
10 #include "cachemultimap.h"
11 #include "governance-exceptions.h"
12 #include "governance-vote.h"
13 #include "governance-votedb.h"
14 #include "key.h"
15 #include "net.h"
16 #include "sync.h"
17 #include "util.h"
18 
19 #include <univalue.h>
20 
21 class CGovernanceManager;
23 class CGovernanceObject;
24 class CGovernanceVote;
25 
26 static const int MAX_GOVERNANCE_OBJECT_DATA_SIZE = 16 * 1024;
27 static const int MIN_GOVERNANCE_PEER_PROTO_VERSION = 70206;
28 static const int GOVERNANCE_FILTER_PROTO_VERSION = 70206;
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 static const int GOVERNANCE_OBJECT_WATCHDOG = 3;
36 
38 
39 static const int64_t GOVERNANCE_FEE_CONFIRMATIONS = 6;
40 static const int64_t GOVERNANCE_MIN_RELAY_FEE_CONFIRMATIONS = 1;
41 static const int64_t GOVERNANCE_UPDATE_MIN = 60*60;
42 static const int64_t GOVERNANCE_DELETION_DELAY = 10*60;
43 static const int64_t GOVERNANCE_ORPHAN_EXPIRATION_TIME = 10*60;
44 static const int64_t GOVERNANCE_WATCHDOG_EXPIRATION_TIME = 2*60*60;
45 
46 static const int GOVERNANCE_TRIGGER_EXPIRATION_BLOCKS = 576;
47 
48 // FOR SEEN MAP ARRAYS - GOVERNANCE OBJECTS AND VOTES
49 static const int SEEN_OBJECT_IS_VALID = 0;
50 static const int SEEN_OBJECT_ERROR_INVALID = 1;
51 static const int SEEN_OBJECT_ERROR_IMMATURE = 2;
52 static const int SEEN_OBJECT_EXECUTED = 3; //used for triggers
53 static const int SEEN_OBJECT_UNKNOWN = 4; // the default
54 
55 typedef std::pair<CGovernanceVote, int64_t> vote_time_pair_t;
56 
57 inline bool operator<(const vote_time_pair_t& p1, const vote_time_pair_t& p2)
58 {
59  return (p1.first < p2.first);
60 }
61 
63 
65  int64_t nTime;
66  int64_t nCreationTime;
67 
68  vote_instance_t(vote_outcome_enum_t eOutcomeIn = VOTE_OUTCOME_NONE, int64_t nTimeIn = 0, int64_t nCreationTimeIn = 0)
69  : eOutcome(eOutcomeIn),
70  nTime(nTimeIn),
71  nCreationTime(nCreationTimeIn)
72  {}
73 
75 
76  template <typename Stream, typename Operation>
77  inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion)
78  {
79  int nOutcome = int(eOutcome);
80  READWRITE(nOutcome);
83  if(ser_action.ForRead()) {
84  eOutcome = vote_outcome_enum_t(nOutcome);
85  }
86  }
87 };
88 
89 typedef std::map<int,vote_instance_t> vote_instance_m_t;
90 
91 typedef vote_instance_m_t::iterator vote_instance_m_it;
92 
93 typedef vote_instance_m_t::const_iterator vote_instance_m_cit;
94 
95 struct vote_rec_t {
97 
99 
100  template <typename Stream, typename Operation>
101  inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion)
102  {
104  }
105 };
106 
112 class CGovernanceObject
113 {
114  friend class CGovernanceManager;
115 
117 
118 public: // Types
119  typedef std::map<COutPoint, vote_rec_t> vote_m_t;
120 
121  typedef vote_m_t::iterator vote_m_it;
122 
123  typedef vote_m_t::const_iterator vote_m_cit;
124 
126 
127 private:
130 
133 
136 
139 
141  int64_t nTime;
142 
144  int64_t nDeletionTime;
145 
148 
150  std::string strData;
151 
154  std::vector<unsigned char> vchSig;
155 
159 
160  // VARIOUS FLAGS FOR OBJECT / SET VIA MASTERNODE VOTING
161 
164 
167 
170 
175 
178 
180  bool fExpired;
181 
184 
186 
189 
191 
192 public:
194 
195  CGovernanceObject(uint256 nHashParentIn, int nRevisionIn, int64_t nTime, uint256 nCollateralHashIn, std::string strDataIn);
196 
197  CGovernanceObject(const CGovernanceObject& other);
198 
199  void swap(CGovernanceObject& first, CGovernanceObject& second); // nothrow
200 
201  // Public Getter methods
202 
203  int64_t GetCreationTime() const {
204  return nTime;
205  }
206 
207  int64_t GetDeletionTime() const {
208  return nDeletionTime;
209  }
210 
211  int GetObjectType() const {
212  return nObjectType;
213  }
214 
215  const uint256& GetCollateralHash() const {
216  return nCollateralHash;
217  }
218 
219  const CTxIn& GetMasternodeVin() const {
220  return vinMasternode;
221  }
222 
223  bool IsSetCachedFunding() const {
224  return fCachedFunding;
225  }
226 
227  bool IsSetCachedValid() const {
228  return fCachedValid;
229  }
230 
231  bool IsSetCachedDelete() const {
232  return fCachedDelete;
233  }
234 
235  bool IsSetCachedEndorsed() const {
236  return fCachedEndorsed;
237  }
238 
239  bool IsSetDirtyCache() const {
240  return fDirtyCache;
241  }
242 
243  bool IsSetExpired() const {
244  return fExpired;
245  }
246 
248  fDirtyCache = true;
249  }
250 
252  return fileVotes;
253  }
254 
255  // Signature related functions
256 
257  void SetMasternodeVin(const COutPoint& outpoint);
258  bool Sign(CKey& keyMasternode, CPubKey& pubKeyMasternode);
259  bool CheckSignature(CPubKey& pubKeyMasternode);
260 
261  std::string GetSignatureMessage() const;
262 
263  // CORE OBJECT FUNCTIONS
264 
265  bool IsValidLocally(std::string& strError, bool fCheckCollateral);
266 
267  bool IsValidLocally(std::string& strError, bool& fMissingMasternode, bool& fMissingConfirmations, bool fCheckCollateral);
268 
270  bool IsCollateralValid(std::string& strError, bool &fMissingConfirmations);
271 
272  void UpdateLocalValidity();
273 
275 
276  int GetObjectSubtype();
277 
279 
281 
282  void Relay(CConnman& connman);
283 
284  uint256 GetHash() const;
285 
286  // GET VOTE COUNT FOR SIGNAL
287 
288  int CountMatchingVotes(vote_signal_enum_t eVoteSignalIn, vote_outcome_enum_t eVoteOutcomeIn) const;
289 
290  int GetAbsoluteYesCount(vote_signal_enum_t eVoteSignalIn) const;
291  int GetAbsoluteNoCount(vote_signal_enum_t eVoteSignalIn) const;
292  int GetYesCount(vote_signal_enum_t eVoteSignalIn) const;
293  int GetNoCount(vote_signal_enum_t eVoteSignalIn) const;
294  int GetAbstainCount(vote_signal_enum_t eVoteSignalIn) const;
295 
296  bool GetCurrentMNVotes(const COutPoint& mnCollateralOutpoint, vote_rec_t& voteRecord);
297 
298  // FUNCTIONS FOR DEALING WITH DATA STRING
299 
300  std::string GetDataAsHex();
301  std::string GetDataAsString();
302 
303  // SERIALIZER
304 
306 
307  template <typename Stream, typename Operation>
308  inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion)
309  {
310  // SERIALIZE DATA FOR SAVING/LOADING OR NETWORK FUNCTIONS
311 
314  READWRITE(nTime);
319  READWRITE(vchSig);
320  if(nType & SER_DISK) {
321  // Only include these for the disk file format
322  LogPrint("gobject", "CGovernanceObject::SerializationOp Reading/writing votes from/to disk\n");
327  LogPrint("gobject", "CGovernanceObject::SerializationOp hash = %s, vote count = %d\n", GetHash().ToString(), fileVotes.GetVoteCount());
328  }
329 
330  // AFTER DESERIALIZATION OCCURS, CACHED VARIABLES MUST BE CALCULATED MANUALLY
331  }
332 
334  {
335  swap(*this, from);
336  return *this;
337  }
338 
339 private:
340  // FUNCTIONS FOR DEALING WITH DATA STRING
341  void LoadData();
342  void GetData(UniValue& objResult);
343 
344  bool ProcessVote(CNode* pfrom,
345  const CGovernanceVote& vote,
346  CGovernanceException& exception,
347  CConnman& connman);
348 
350  void ClearMasternodeVotes();
351 
352  void CheckOrphanVotes(CConnman& connman);
353 
354 };
355 
356 
357 #endif
bool GetCurrentMNVotes(const COutPoint &mnCollateralOutpoint, vote_rec_t &voteRecord)
static const int64_t GOVERNANCE_FEE_CONFIRMATIONS
static const int64_t GOVERNANCE_ORPHAN_EXPIRATION_TIME
#define READWRITE(obj)
Definition: serialize.h:175
vote_m_t::iterator vote_m_it
std::pair< CGovernanceVote, int64_t > vote_time_pair_t
static const int GOVERNANCE_TRIGGER_EXPIRATION_BLOCKS
bool fCachedValid
true == minimum network has been reached flagging this object as a valid and understood goverance obj...
vote_instance_m_t mapInstances
static const int MIN_GOVERNANCE_PEER_PROTO_VERSION
static const CAmount COIN
Definition: amount.h:16
CGovernanceObject & operator=(CGovernanceObject from)
static const CAmount GOVERNANCE_PROPOSAL_FEE_TX
bool fCachedLocalValidity
is valid by blockchain
const CTxIn & GetMasternodeVin() const
bool IsSetExpired() const
bool fUnparsable
Failed to parse object data.
std::string strLocalValidityError
Definition: net.h:108
static const int64_t GOVERNANCE_MIN_RELAY_FEE_CONFIRMATIONS
static const int GOVERNANCE_OBJECT_PROPOSAL
int CountMatchingVotes(vote_signal_enum_t eVoteSignalIn, vote_outcome_enum_t eVoteOutcomeIn) const
CGovernanceObjectVoteFile fileVotes
static const int SEEN_OBJECT_ERROR_INVALID
void ClearMasternodeVotes()
Called when MN&#39;s which have voted on this object have been removed.
bool fExpired
Object is no longer of interest.
vote_outcome_enum_t eOutcome
static const int64_t GOVERNANCE_WATCHDOG_EXPIRATION_TIME
void SerializationOp(Stream &s, Operation ser_action, int nType, int nVersion)
int nObjectType
Object typecode.
int64_t GetCreationTime() const
static const double GOVERNANCE_FILTER_FP_RATE
int64_t CAmount
Definition: amount.h:14
std::map< int, vote_instance_t > vote_instance_m_t
void SerializationOp(Stream &s, Operation ser_action, int nType, int nVersion)
std::string GetSignatureMessage() const
static const int GOVERNANCE_OBJECT_WATCHDOG
Definition: net.h:661
static int LogPrint(const char *category, const char *format)
Definition: util.h:126
void Relay(CConnman &connman)
bool fDirtyCache
object was updated and cached values should be updated soon
void SerializationOp(Stream &s, Operation ser_action, int nType, int nVersion)
CacheMultiMap< COutPoint, vote_time_pair_t > vote_mcache_t
int64_t nDeletionTime
time this object was marked for deletion
std::string strData
Data field - can be used for anything.
std::map< COutPoint, vote_rec_t > vote_m_t
vote_m_t::const_iterator vote_m_cit
int GetYesCount(vote_signal_enum_t eVoteSignalIn) const
std::string GetDataAsHex()
bool IsSetCachedEndorsed() const
int GetAbsoluteNoCount(vote_signal_enum_t eVoteSignalIn) const
static const int GOVERNANCE_OBJECT_TRIGGER
std::string GetDataAsString()
static const int SEEN_OBJECT_IS_VALID
bool IsSetCachedValid() const
static const int SEEN_OBJECT_ERROR_IMMATURE
bool IsCollateralValid(std::string &strError, bool &fMissingConfirmations)
Check the collateral transaction for the budget proposal/finalized budget.
int64_t GetDeletionTime() const
static const int GOVERNANCE_FILTER_PROTO_VERSION
static const int SEEN_OBJECT_EXECUTED
static const int SEEN_OBJECT_UNKNOWN
const uint256 & GetCollateralHash() const
vote_mcache_t mapOrphanVotes
Limited map of votes orphaned by MN.
bool CheckSignature(CPubKey &pubKeyMasternode)
bool IsSetCachedDelete() const
bool IsValidLocally(std::string &strError, bool fCheckCollateral)
void swap(CGovernanceObject &first, CGovernanceObject &second)
void SetMasternodeVin(const COutPoint &outpoint)
bool IsSetDirtyCache() const
void GetData(UniValue &objResult)
vote_instance_t(vote_outcome_enum_t eOutcomeIn=VOTE_OUTCOME_NONE, int64_t nTimeIn=0, int64_t nCreationTimeIn=0)
int GetAbstainCount(vote_signal_enum_t eVoteSignalIn) const
bool IsSetCachedFunding() const
CCriticalSection cs
critical section to protect the inner data structures
static const int64_t GOVERNANCE_UPDATE_MIN
bool Sign(CKey &keyMasternode, CPubKey &pubKeyMasternode)
vote_outcome_enum_t
int GetAbsoluteYesCount(vote_signal_enum_t eVoteSignalIn) const
vote_signal_enum_t
CTxIn vinMasternode
Masternode info for signed objects.
#define LIMITED_STRING(obj, n)
Definition: serialize.h:389
static const int MAX_GOVERNANCE_OBJECT_DATA_SIZE
uint256 nHashParent
parent object, 0 is root
static const int GOVERNANCE_OBJECT_UNKNOWN
uint256 nCollateralHash
fee-tx
bool fCachedFunding
true == minimum network support has been reached for this object to be funded (doesn&#39;t mean it will f...
Definition: pubkey.h:37
virtual uint256 GetHash()=0
int GetNoCount(vote_signal_enum_t eVoteSignalIn) const
int64_t nTime
time this object was created
static const int64_t GOVERNANCE_DELETION_DELAY
CGovernanceObjectVoteFile & GetVoteFile()
std::vector< unsigned char > vchSig
void CheckOrphanVotes(CConnman &connman)
Definition: key.h:35
bool operator<(const vote_time_pair_t &p1, const vote_time_pair_t &p2)
int GetObjectType() const
bool ProcessVote(CNode *pfrom, const CGovernanceVote &vote, CGovernanceException &exception, CConnman &connman)
vote_instance_m_t::const_iterator vote_instance_m_cit
vote_instance_m_t::iterator vote_instance_m_it
bool fCachedDelete
true == minimum network support has been reached saying this object should be deleted from the system...
int nRevision
object revision in the system