dash/src/governance-votedb.cpp

93 lines
2.4 KiB
C++
Raw Normal View History

2016-12-20 14:26:45 +01:00
// Copyright (c) 2014-2017 The Dash 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)
{
A pack of small fixes (#1992) * Make sure gobject collateral was mined `CGovernanceObject::IsCollateralValid()` would still fail for non-mined collateral later trying to check confirmations * Fix powLimit for mainnet/testnet That's a legacy thing, slightly rising it to match the actual bit-shifted value has no effect because real values are already lower. Also clarify values for all networks in comments. * Check for script addresses in CSuperblock::ParsePaymentSchedule() Sentinel should already be downvoting such triggers if they would exist, no need to store/relay them. * Do not process already known valid vote twice in CGovernanceObject::ProcessVote() This should be handled by `CGovernanceManager::ProcessVote()` but imo it's better to have this at the `CGovernanceObject::ProcessVote()` level as well. * Make sure CGovernanceObjectVoteFile::AddVote() never adds/updates already known votes The way `CGovernanceObjectVoteFile::AddVote()` is used (i.e. wrapped in `CGovernanceObjectVoteFile::HasVote()` condition) it's already the case. Hoever nothing would guarantee consistency if it would be used elsewhere without such wrapper, so it's better to have similar check inside. * Do not even try mnb recovery when -connect is set `CConnman::ThreadOpenMasternodeConnections()` thread won't even start when `-connect` is set, so no need to ask for recovery, there is nothing that is going to be able to process such request. * No need for SIGHASH_ANYONECANPAY in PS collateral signature Collateral is just a normal tx created and signed by each participant individually, there is no need for special sig types. * Release semMasternodeOutbound in CConnman::Interrupt() Re-align Dash code with Bitcoin.
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;
listVotes.push_front(vote);
A pack of small fixes (#1992) * Make sure gobject collateral was mined `CGovernanceObject::IsCollateralValid()` would still fail for non-mined collateral later trying to check confirmations * Fix powLimit for mainnet/testnet That's a legacy thing, slightly rising it to match the actual bit-shifted value has no effect because real values are already lower. Also clarify values for all networks in comments. * Check for script addresses in CSuperblock::ParsePaymentSchedule() Sentinel should already be downvoting such triggers if they would exist, no need to store/relay them. * Do not process already known valid vote twice in CGovernanceObject::ProcessVote() This should be handled by `CGovernanceManager::ProcessVote()` but imo it's better to have this at the `CGovernanceObject::ProcessVote()` level as well. * Make sure CGovernanceObjectVoteFile::AddVote() never adds/updates already known votes The way `CGovernanceObjectVoteFile::AddVote()` is used (i.e. wrapped in `CGovernanceObjectVoteFile::HasVote()` condition) it's already the case. Hoever nothing would guarantee consistency if it would be used elsewhere without such wrapper, so it's better to have similar check inside. * Do not even try mnb recovery when -connect is set `CConnman::ThreadOpenMasternodeConnections()` thread won't even start when `-connect` is set, so no need to ask for recovery, there is nothing that is going to be able to process such request. * No need for SIGHASH_ANYONECANPAY in PS collateral signature Collateral is just a normal tx created and signed by each participant individually, there is no need for special sig types. * Release semMasternodeOutbound in CConnman::Interrupt() Re-align Dash code with Bitcoin.
2018-03-19 14:08:32 +01:00
mapVoteIndex.emplace(nHash, 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::SerializeVoteToStream(const uint256& nHash, CDataStream& ss) const
{
vote_m_cit it = mapVoteIndex.find(nHash);
if(it == mapVoteIndex.end()) {
return false;
}
ss << *(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 COutPoint& outpointMasternode)
{
vote_l_it it = listVotes.begin();
while(it != listVotes.end()) {
if(it->GetMasternodeOutpoint() == outpointMasternode) {
--nMemoryVotes;
mapVoteIndex.erase(it->GetHash());
listVotes.erase(it++);
}
else {
++it;
}
}
}
void CGovernanceObjectVoteFile::RebuildIndex()
{
mapVoteIndex.clear();
nMemoryVotes = 0;
vote_l_it it = listVotes.begin();
while(it != listVotes.end()) {
CGovernanceVote& vote = *it;
uint256 nHash = vote.GetHash();
if(mapVoteIndex.find(nHash) == mapVoteIndex.end()) {
mapVoteIndex[nHash] = it;
++nMemoryVotes;
++it;
}
else {
listVotes.erase(it++);
}
}
}