neobytes/src/governance-votedb.cpp

98 lines
2.5 KiB
C++
Raw Normal View History

2016-12-20 14:26:45 +01:00
// Copyright (c) 2014-2017 The Dash Core developers
2024-02-05 23:00:22 +01:00
// Copyright (c) 2021-2024 The NeoBytes Core developers
// 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)
{
listVotes.push_front(vote);
mapVoteIndex[vote.GetHash()] = listVotes.begin();
++nMemoryVotes;
}
bool CGovernanceObjectVoteFile::HasVote(const uint256& nHash) const
{
vote_m_cit it = mapVoteIndex.find(nHash);
if(it == mapVoteIndex.end()) {
return false;
}
return true;
}
bool CGovernanceObjectVoteFile::GetVote(const uint256& nHash, CGovernanceVote& vote) const
{
vote_m_cit it = mapVoteIndex.find(nHash);
if(it == mapVoteIndex.end()) {
return false;
}
vote = *(it->second);
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;
}
void CGovernanceObjectVoteFile::RemoveVotesFromMasternode(const CTxIn& vinMasternode)
{
vote_l_it it = listVotes.begin();
while(it != listVotes.end()) {
if(it->GetVinMasternode() == vinMasternode) {
2024-02-05 23:00:22 +01:00
--nMemoryVotes;
mapVoteIndex.erase(it->GetHash());
listVotes.erase(it++);
}
else {
++it;
}
}
}
CGovernanceObjectVoteFile& CGovernanceObjectVoteFile::operator=(const CGovernanceObjectVoteFile& other)
{
nMemoryVotes = other.nMemoryVotes;
listVotes = other.listVotes;
RebuildIndex();
return *this;
}
void CGovernanceObjectVoteFile::RebuildIndex()
{
mapVoteIndex.clear();
2024-02-05 23:00:22 +01:00
nMemoryVotes = 0;
vote_l_it it = listVotes.begin();
while(it != listVotes.end()) {
CGovernanceVote& vote = *it;
2024-02-05 23:00:22 +01:00
uint256 nHash = vote.GetHash();
if(mapVoteIndex.find(nHash) == mapVoteIndex.end()) {
mapVoteIndex[nHash] = it;
++nMemoryVotes;
++it;
}
else {
listVotes.erase(it++);
}
}
}