2016-12-20 14:26:45 +01:00
|
|
|
// Copyright (c) 2014-2017 The Dash Core developers
|
2016-11-13 18:52:34 +01:00
|
|
|
// Distributed under the MIT/X11 software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
|
|
|
#include "governance-votedb.h"
|
|
|
|
|
|
|
|
CGovernanceObjectVoteFile::CGovernanceObjectVoteFile()
|
|
|
|
: nMemoryVotes(0),
|
|
|
|
listVotes(),
|
|
|
|
mapVoteIndex()
|
|
|
|
{}
|
|
|
|
|
|
|
|
CGovernanceObjectVoteFile::CGovernanceObjectVoteFile(const CGovernanceObjectVoteFile& other)
|
|
|
|
: nMemoryVotes(other.nMemoryVotes),
|
|
|
|
listVotes(other.listVotes),
|
|
|
|
mapVoteIndex()
|
|
|
|
{
|
|
|
|
RebuildIndex();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CGovernanceObjectVoteFile::AddVote(const CGovernanceVote& vote)
|
|
|
|
{
|
2018-03-19 14:08:32 +01:00
|
|
|
uint256 nHash = vote.GetHash();
|
|
|
|
// make sure to never add/update already known votes
|
|
|
|
if (HasVote(nHash))
|
|
|
|
return;
|
2016-11-13 18:52:34 +01:00
|
|
|
listVotes.push_front(vote);
|
2018-03-19 14:08:32 +01:00
|
|
|
mapVoteIndex.emplace(nHash, listVotes.begin());
|
2016-11-13 18:52:34 +01:00
|
|
|
++nMemoryVotes;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CGovernanceObjectVoteFile::HasVote(const uint256& nHash) const
|
|
|
|
{
|
|
|
|
vote_m_cit it = mapVoteIndex.find(nHash);
|
|
|
|
if(it == mapVoteIndex.end()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-02-21 17:35:37 +01:00
|
|
|
bool CGovernanceObjectVoteFile::SerializeVoteToStream(const uint256& nHash, CDataStream& ss) const
|
2016-11-13 18:52:34 +01:00
|
|
|
{
|
|
|
|
vote_m_cit it = mapVoteIndex.find(nHash);
|
|
|
|
if(it == mapVoteIndex.end()) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-02-21 17:35:37 +01:00
|
|
|
ss << *(it->second);
|
2016-11-13 18:52:34 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<CGovernanceVote> CGovernanceObjectVoteFile::GetVotes() const
|
|
|
|
{
|
|
|
|
std::vector<CGovernanceVote> vecResult;
|
|
|
|
for(vote_l_cit it = listVotes.begin(); it != listVotes.end(); ++it) {
|
|
|
|
vecResult.push_back(*it);
|
|
|
|
}
|
|
|
|
return vecResult;
|
|
|
|
}
|
|
|
|
|
2017-09-11 16:13:48 +02:00
|
|
|
void CGovernanceObjectVoteFile::RemoveVotesFromMasternode(const COutPoint& outpointMasternode)
|
2016-11-13 18:52:34 +01:00
|
|
|
{
|
|
|
|
vote_l_it it = listVotes.begin();
|
|
|
|
while(it != listVotes.end()) {
|
2017-09-11 16:13:48 +02:00
|
|
|
if(it->GetMasternodeOutpoint() == outpointMasternode) {
|
2017-04-03 22:06:33 +02:00
|
|
|
--nMemoryVotes;
|
2017-01-21 06:26:35 +01:00
|
|
|
mapVoteIndex.erase(it->GetHash());
|
2016-11-13 18:52:34 +01:00
|
|
|
listVotes.erase(it++);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CGovernanceObjectVoteFile::RebuildIndex()
|
|
|
|
{
|
|
|
|
mapVoteIndex.clear();
|
2017-04-03 22:06:33 +02:00
|
|
|
nMemoryVotes = 0;
|
|
|
|
vote_l_it it = listVotes.begin();
|
|
|
|
while(it != listVotes.end()) {
|
2016-11-13 18:52:34 +01:00
|
|
|
CGovernanceVote& vote = *it;
|
2017-04-03 22:06:33 +02:00
|
|
|
uint256 nHash = vote.GetHash();
|
|
|
|
if(mapVoteIndex.find(nHash) == mapVoteIndex.end()) {
|
|
|
|
mapVoteIndex[nHash] = it;
|
|
|
|
++nMemoryVotes;
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
listVotes.erase(it++);
|
|
|
|
}
|
2016-11-13 18:52:34 +01:00
|
|
|
}
|
|
|
|
}
|