Line data Source code
1 : // Copyright (c) 2014-2024 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 BITCOIN_GOVERNANCE_VOTEDB_H 6 : #define BITCOIN_GOVERNANCE_VOTEDB_H 7 : 8 : #include <governance/vote.h> 9 : #include <serialize.h> 10 : #include <uint256.h> 11 : 12 : #include <list> 13 : #include <map> 14 : #include <set> 15 : #include <vector> 16 : 17 : class CDataStream; 18 : class CDeterministicMNList; 19 : 20 : /** 21 : * Represents the collection of votes associated with a given CGovernanceObject 22 : * Recently received votes are held in memory until a maximum size is reached after 23 : * which older votes a flushed to a disk file. 24 : * 25 : * Note: This is a stub implementation that doesn't limit the number of votes held 26 : * in memory and doesn't flush to disk. 27 : */ 28 : class CGovernanceObjectVoteFile 29 : { 30 : public: // Types 31 : using vote_l_t = std::list<CGovernanceVote>; 32 : 33 : using vote_m_t = std::map<uint256, vote_l_t::iterator>; 34 : 35 : private: 36 : int nMemoryVotes{0}; 37 : 38 : vote_l_t listVotes; 39 : 40 : vote_m_t mapVoteIndex; 41 : 42 : public: 43 : CGovernanceObjectVoteFile(); 44 : 45 : CGovernanceObjectVoteFile(const CGovernanceObjectVoteFile& other); 46 : 47 : /** 48 : * Add a vote to the file 49 : */ 50 : void AddVote(const CGovernanceVote& vote); 51 : 52 : /** 53 : * Return true if the vote with this hash is currently cached in memory 54 : */ 55 : bool HasVote(const uint256& nHash) const; 56 : 57 : /** 58 : * Retrieve a vote cached in memory 59 : */ 60 : bool SerializeVoteToStream(const uint256& nHash, CDataStream& ss) const; 61 : 62 : int GetVoteCount() const 63 : { 64 : return nMemoryVotes; 65 : } 66 : 67 : std::vector<CGovernanceVote> GetVotes() const; 68 : 69 : void RemoveVotesFromMasternode(const COutPoint& outpointMasternode); 70 : std::set<uint256> RemoveInvalidVotes(const CDeterministicMNList& tip_mn_list, const COutPoint& outpointMasternode, bool fProposal); 71 : 72 0 : SERIALIZE_METHODS(CGovernanceObjectVoteFile, obj) 73 : { 74 0 : READWRITE(obj.nMemoryVotes, obj.listVotes); 75 0 : SER_READ(obj, obj.RebuildIndex()); 76 0 : } 77 : 78 : private: 79 : // Drop older votes for the same gobject from the same masternode 80 : void RemoveOldVotes(const CGovernanceVote& vote); 81 : 82 : void RebuildIndex(); 83 : }; 84 : 85 : #endif // BITCOIN_GOVERNANCE_VOTEDB_H