PIVX Core  5.6.99
P2P Digital Currency
budgetproposal.h
Go to the documentation of this file.
1 // Copyright (c) 2014-2015 The Dash developers
2 // Copyright (c) 2015-2022 The PIVX Core developers
3 // Distributed under the MIT/X11 software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
6 #ifndef PIVX_BUDGET_BUDGETPROPOSAL_H
7 #define PIVX_BUDGET_BUDGETPROPOSAL_H
8 
9 #include "budget/budgetvote.h"
10 #include "net.h"
11 #include "streams.h"
12 
13 static const CAmount PROPOSAL_FEE_TX = (50 * COIN);
14 static const CAmount BUDGET_FEE_TX_OLD = (50 * COIN);
15 static const CAmount BUDGET_FEE_TX = (5 * COIN);
16 static const int64_t BUDGET_VOTE_UPDATE_MIN = 60 * 60;
17 
18 // Minimum value for a proposal to be considered valid
19 static const CAmount PROPOSAL_MIN_AMOUNT = 10 * COIN;
20 
21 // Net ser values
22 static const size_t PROP_URL_MAX_SIZE = 64;
23 static const size_t PROP_NAME_MAX_SIZE = 20;
24 
25 class CBudgetManager;
26 
27 //
28 // Budget Proposal : Contains the masternode votes for each budget
29 //
30 
32 {
33 private:
34  friend class CBudgetManager;
36  bool fValid;
37  std::string strInvalid;
38 
39  // Functions used inside UpdateValid()/IsWellFormed - setting strInvalid
40  bool IsHeavilyDownvoted(int mnCount);
41  bool updateExpired(int nCurrentHeight);
42  bool CheckStartEnd();
43  bool CheckAmount(const CAmount& nTotalBudget);
44  bool CheckAddress();
45  bool CheckStrings();
46 
47 protected:
48  std::map<COutPoint, CBudgetVote> mapVotes;
49  std::string strProposalName;
50  std::string strURL;
52  int nBlockEnd;
56 
57 public:
58  // Set in CBudgetManager::AddProposal via CheckCollateral
59  int64_t nTime;
60 
62  CBudgetProposal(const std::string& name, const std::string& url, int paycount, const CScript& payee, const CAmount& amount, int blockstart, const uint256& nfeetxhash);
63 
64  bool AddOrUpdateVote(const CBudgetVote& vote, std::string& strError);
65  UniValue GetVotesArray() const;
66  void SetSynced(bool synced); // sets fSynced on votes (true only if valid)
67 
68  // sync proposal votes with a node
69  void SyncVotes(CNode* pfrom, bool fPartial, int& nInvCount) const;
70 
71  // sets fValid and strInvalid, returns fValid
72  bool UpdateValid(int nHeight, int mnCount);
73  // Static checks that should be done only once - sets strInvalid
74  bool IsWellFormed(const CAmount& nTotalBudget);
75  bool IsValid() const { return fValid; }
76  void SetStrInvalid(const std::string& _strInvalid) { strInvalid = _strInvalid; }
77  std::string IsInvalidReason() const { return strInvalid; }
78  std::string IsInvalidLogStr() const { return strprintf("[%s]: %s", GetName(), IsInvalidReason()); }
79 
80  bool IsEstablished() const;
81  bool IsPassing(int nBlockStartBudget, int nBlockEndBudget, int mnCount) const;
82  bool IsExpired(int nCurrentHeight) const;
83 
84  std::string GetName() const { return strProposalName; }
85  std::string GetURL() const { return strURL; }
86  int GetBlockStart() const { return nBlockStart; }
87  int GetBlockEnd() const { return nBlockEnd; }
88  CScript GetPayee() const { return address; }
89  int GetTotalPaymentCount() const;
90  int GetRemainingPaymentCount(int nCurrentHeight) const;
91  int GetBlockStartCycle() const;
92  static int GetBlockCycle(int nCurrentHeight);
93  int GetBlockEndCycle() const;
94  const uint256& GetFeeTXHash() const { return nFeeTXHash; }
95  double GetRatio() const;
97  std::map<COutPoint, CBudgetVote> GetVotes() const { return mapVotes; }
98  int GetYeas() const { return GetVoteCount(CBudgetVote::VOTE_YES); }
99  int GetNays() const { return GetVoteCount(CBudgetVote::VOTE_NO); }
101  CAmount GetAmount() const { return nAmount; }
102  void SetAllotted(CAmount nAllottedIn) { nAllotted = nAllottedIn; }
103  CAmount GetAllotted() const { return nAllotted; }
104  void SetFeeTxHash(const uint256& txid) { nFeeTXHash = txid; }
105 
106  uint256 GetHash() const
107  {
108  CHashWriter ss(SER_GETHASH, PROTOCOL_VERSION);
109  ss << strProposalName;
110  ss << strURL;
111  ss << nBlockStart;
112  ss << nBlockEnd;
113  ss << nAmount;
114  ss << std::vector<unsigned char>(address.begin(), address.end());
115  return ss.GetHash();
116  }
117 
118  // Serialization for local DB
120  {
121  READWRITE(LIMITED_STRING(obj.strProposalName, PROP_NAME_MAX_SIZE));
122  READWRITE(LIMITED_STRING(obj.strURL, PROP_URL_MAX_SIZE));
123  READWRITE(obj.nBlockStart);
124  READWRITE(obj.nBlockEnd);
125  READWRITE(obj.nAmount);
126  READWRITE(obj.address);
127  READWRITE(obj.nFeeTXHash);
128  READWRITE(obj.nTime);
129  READWRITE(obj.mapVotes);
130  }
131 
132  // Serialization for network messages.
133  bool ParseBroadcast(CDataStream& broadcast);
134  CDataStream GetBroadcast() const;
135  void Relay();
136 
137  inline bool operator==(const CBudgetProposal& other) const
138  {
139  return GetHash() == other.GetHash();
140  }
141 
142  // compare proposals by proposal hash
143  inline bool operator>(const CBudgetProposal& other) const
144  {
145  return UintToArith256(GetHash()) > UintToArith256(other.GetHash());
146  }
147  //
148  // compare proposals pointers by net yes count (solve tie with feeHash)
149  static inline bool PtrHigherYes(CBudgetProposal* a, CBudgetProposal* b)
150  {
151  const int netYes_a = a->GetYeas() - a->GetNays();
152  const int netYes_b = b->GetYeas() - b->GetNays();
153  if (netYes_a == netYes_b) return UintToArith256(a->GetFeeTXHash()) > UintToArith256(b->GetFeeTXHash());
154  return netYes_a > netYes_b;
155  }
156 
157 };
158 
159 #endif // PIVX_BUDGET_BUDGETPROPOSAL_H
int64_t CAmount
Amount in PIV (Can be negative)
Definition: amount.h:13
arith_uint256 UintToArith256(const uint256 &a)
int GetYeas() const
double GetRatio() const
void SetAllotted(CAmount nAllottedIn)
std::map< COutPoint, CBudgetVote > mapVotes
int GetRemainingPaymentCount(int nCurrentHeight) const
bool operator==(const CBudgetProposal &other) const
bool IsExpired(int nCurrentHeight) const
SERIALIZE_METHODS(CBudgetProposal, obj)
int GetBlockStart() const
CAmount GetAllotted() const
CDataStream GetBroadcast() const
bool updateExpired(int nCurrentHeight)
bool CheckAmount(const CAmount &nTotalBudget)
std::map< COutPoint, CBudgetVote > GetVotes() const
std::string GetURL() const
std::string IsInvalidReason() const
void SetFeeTxHash(const uint256 &txid)
static int GetBlockCycle(int nCurrentHeight)
std::string GetName() const
int GetBlockEndCycle() const
int GetVoteCount(CBudgetVote::VoteDirection vd) const
const uint256 & GetFeeTXHash() const
CScript GetPayee() const
int GetAbstains() const
std::string strInvalid
bool IsValid() const
bool ParseBroadcast(CDataStream &broadcast)
bool operator>(const CBudgetProposal &other) const
void SetSynced(bool synced)
void SetStrInvalid(const std::string &_strInvalid)
bool AddOrUpdateVote(const CBudgetVote &vote, std::string &strError)
int GetBlockEnd() const
static bool PtrHigherYes(CBudgetProposal *a, CBudgetProposal *b)
std::string strProposalName
CAmount GetAmount() const
bool UpdateValid(int nHeight, int mnCount)
bool IsHeavilyDownvoted(int mnCount)
std::string strURL
bool IsWellFormed(const CAmount &nTotalBudget)
int GetNays() const
void SyncVotes(CNode *pfrom, bool fPartial, int &nInvCount) const
std::string IsInvalidLogStr() const
uint256 GetHash() const
int GetTotalPaymentCount() const
bool IsEstablished() const
UniValue GetVotesArray() const
bool IsPassing(int nBlockStartBudget, int nBlockEndBudget, int mnCount) const
int GetBlockStartCycle() const
A writer stream (for serialization) that computes a 256-bit hash.
Definition: hash.h:216
uint256 GetHash()
Definition: hash.h:236
Information about a peer.
Definition: net.h:669
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:381
iterator begin()
Definition: prevector.h:285
iterator end()
Definition: prevector.h:287
256-bit opaque blob.
Definition: uint256.h:138
const char * name
Definition: rest.cpp:37
const char * url
Definition: rpcconsole.cpp:51
@ SER_GETHASH
Definition: serialize.h:176
#define LIMITED_STRING(obj, n)
Definition: serialize.h:515
#define READWRITE(...)
Definition: serialize.h:183
#define strprintf
Definition: tinyformat.h:1056