Line data Source code
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 : 31 : class CBudgetProposal 32 : { 33 : private: 34 : friend class CBudgetManager; 35 : CAmount nAllotted; 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; 51 : int nBlockStart; 52 : int nBlockEnd; 53 : CScript address; 54 : CAmount nAmount; 55 : uint256 nFeeTXHash; 56 : 57 : public: 58 : // Set in CBudgetManager::AddProposal via CheckCollateral 59 : int64_t nTime; 60 : 61 : CBudgetProposal(); 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 8143 : bool IsValid() const { return fValid; } 76 184 : void SetStrInvalid(const std::string& _strInvalid) { strInvalid = _strInvalid; } 77 0 : std::string IsInvalidReason() const { return strInvalid; } 78 0 : 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 14776 : std::string GetName() const { return strProposalName; } 85 1014 : std::string GetURL() const { return strURL; } 86 710 : int GetBlockStart() const { return nBlockStart; } 87 1046 : int GetBlockEnd() const { return nBlockEnd; } 88 581 : 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 2144 : const uint256& GetFeeTXHash() const { return nFeeTXHash; } 95 : double GetRatio() const; 96 : int GetVoteCount(CBudgetVote::VoteDirection vd) const; 97 0 : std::map<COutPoint, CBudgetVote> GetVotes() const { return mapVotes; } 98 10539 : int GetYeas() const { return GetVoteCount(CBudgetVote::VOTE_YES); } 99 10539 : int GetNays() const { return GetVoteCount(CBudgetVote::VOTE_NO); } 100 507 : int GetAbstains() const { return GetVoteCount(CBudgetVote::VOTE_ABSTAIN); } 101 1127 : CAmount GetAmount() const { return nAmount; } 102 43 : void SetAllotted(CAmount nAllottedIn) { nAllotted = nAllottedIn; } 103 531 : CAmount GetAllotted() const { return nAllotted; } 104 : void SetFeeTxHash(const uint256& txid) { nFeeTXHash = txid; } 105 : 106 7856 : uint256 GetHash() const 107 : { 108 7856 : CHashWriter ss(SER_GETHASH, PROTOCOL_VERSION); 109 7856 : ss << strProposalName; 110 7856 : ss << strURL; 111 7856 : ss << nBlockStart; 112 7856 : ss << nBlockEnd; 113 7856 : ss << nAmount; 114 31424 : ss << std::vector<unsigned char>(address.begin(), address.end()); 115 15712 : return ss.GetHash(); 116 : } 117 : 118 : // Serialization for local DB 119 240 : SERIALIZE_METHODS(CBudgetProposal, obj) 120 : { 121 120 : READWRITE(LIMITED_STRING(obj.strProposalName, PROP_NAME_MAX_SIZE)); 122 120 : READWRITE(LIMITED_STRING(obj.strURL, PROP_URL_MAX_SIZE)); 123 120 : READWRITE(obj.nBlockStart); 124 120 : READWRITE(obj.nBlockEnd); 125 120 : READWRITE(obj.nAmount); 126 120 : READWRITE(obj.address); 127 120 : READWRITE(obj.nFeeTXHash); 128 120 : READWRITE(obj.nTime); 129 120 : READWRITE(obj.mapVotes); 130 120 : } 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 4069 : static inline bool PtrHigherYes(CBudgetProposal* a, CBudgetProposal* b) 150 : { 151 4069 : const int netYes_a = a->GetYeas() - a->GetNays(); 152 4069 : const int netYes_b = b->GetYeas() - b->GetNays(); 153 7538 : if (netYes_a == netYes_b) return UintToArith256(a->GetFeeTXHash()) > UintToArith256(b->GetFeeTXHash()); 154 600 : return netYes_a > netYes_b; 155 : } 156 : 157 : }; 158 : 159 : #endif // PIVX_BUDGET_BUDGETPROPOSAL_H