Dash Core Source Documentation (0.16.0.1)

Find detailed information regarding the Dash Core source code.

governance-votedb.cpp
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 
6 
8  nMemoryVotes(0),
9  listVotes(),
10  mapVoteIndex()
11 {
12 }
13 
15  nMemoryVotes(other.nMemoryVotes),
16  listVotes(other.listVotes),
17  mapVoteIndex()
18 {
19  RebuildIndex();
20 }
21 
23 {
24  uint256 nHash = vote.GetHash();
25  // make sure to never add/update already known votes
26  if (HasVote(nHash))
27  return;
28  listVotes.push_front(vote);
29  mapVoteIndex.emplace(nHash, listVotes.begin());
30  ++nMemoryVotes;
31  RemoveOldVotes(vote);
32 }
33 
35 {
36  return mapVoteIndex.find(nHash) != mapVoteIndex.end();
37 }
38 
40 {
41  vote_m_cit it = mapVoteIndex.find(nHash);
42  if (it == mapVoteIndex.end()) {
43  return false;
44  }
45  ss << *(it->second);
46  return true;
47 }
48 
49 std::vector<CGovernanceVote> CGovernanceObjectVoteFile::GetVotes() const
50 {
51  std::vector<CGovernanceVote> vecResult;
52  for (vote_l_cit it = listVotes.begin(); it != listVotes.end(); ++it) {
53  vecResult.push_back(*it);
54  }
55  return vecResult;
56 }
57 
59 {
60  vote_l_it it = listVotes.begin();
61  while (it != listVotes.end()) {
62  if (it->GetMasternodeOutpoint() == outpointMasternode) {
63  --nMemoryVotes;
64  mapVoteIndex.erase(it->GetHash());
65  listVotes.erase(it++);
66  } else {
67  ++it;
68  }
69  }
70 }
71 
72 std::set<uint256> CGovernanceObjectVoteFile::RemoveInvalidVotes(const COutPoint& outpointMasternode, bool fProposal)
73 {
74  std::set<uint256> removedVotes;
75 
76  vote_l_it it = listVotes.begin();
77  while (it != listVotes.end()) {
78  if (it->GetMasternodeOutpoint() == outpointMasternode) {
79  bool useVotingKey = fProposal && (it->GetSignal() == VOTE_SIGNAL_FUNDING);
80  if (!it->IsValid(useVotingKey)) {
81  removedVotes.emplace(it->GetHash());
82  --nMemoryVotes;
83  mapVoteIndex.erase(it->GetHash());
84  listVotes.erase(it++);
85  continue;
86  }
87  }
88  ++it;
89  }
90 
91  return removedVotes;
92 }
93 
95 {
96  vote_l_it it = listVotes.begin();
97  while (it != listVotes.end()) {
98  if (it->GetMasternodeOutpoint() == vote.GetMasternodeOutpoint() // same masternode
99  && it->GetParentHash() == vote.GetParentHash() // same governance object (e.g. same proposal)
100  && it->GetSignal() == vote.GetSignal() // same signal (e.g. "funding", "delete", etc.)
101  && it->GetTimestamp() < vote.GetTimestamp()) // older than new vote
102  {
103  --nMemoryVotes;
104  mapVoteIndex.erase(it->GetHash());
105  listVotes.erase(it++);
106  } else {
107  ++it;
108  }
109  }
110 }
111 
113 {
114  mapVoteIndex.clear();
115  nMemoryVotes = 0;
116  vote_l_it it = listVotes.begin();
117  while (it != listVotes.end()) {
118  CGovernanceVote& vote = *it;
119  uint256 nHash = vote.GetHash();
120  if (mapVoteIndex.find(nHash) == mapVoteIndex.end()) {
121  mapVoteIndex[nHash] = it;
122  ++nMemoryVotes;
123  ++it;
124  } else {
125  listVotes.erase(it++);
126  }
127  }
128 }
bool HasVote(const uint256 &nHash) const
Return true if the vote with this hash is currently cached in memory.
const uint256 & GetParentHash() const
void RemoveOldVotes(const CGovernanceVote &vote)
uint256 GetHash() const
GetHash()
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:103
const COutPoint & GetMasternodeOutpoint() const
vote_l_t::const_iterator vote_l_cit
void RemoveVotesFromMasternode(const COutPoint &outpointMasternode)
int64_t GetTimestamp() const
vote_signal_enum_t GetSignal() const
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:26
256-bit opaque blob.
Definition: uint256.h:123
vote_l_t::iterator vote_l_it
void AddVote(const CGovernanceVote &vote)
Add a vote to the file.
std::vector< CGovernanceVote > GetVotes() const
Represents the collection of votes associated with a given CGovernanceObject Recently received votes ...
std::set< uint256 > RemoveInvalidVotes(const COutPoint &outpointMasternode, bool fProposal)
bool SerializeVoteToStream(const uint256 &nHash, CDataStream &ss) const
Retrieve a vote cached in memory.
vote_m_t::const_iterator vote_m_cit
Released under the MIT license