neobytes/src/governance-vote.cpp

95 lines
2.7 KiB
C++
Raw Normal View History

2016-04-08 19:47:00 +02:00
// Copyright (c) 2014-2016 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.
2016-04-14 00:41:40 +02:00
#include "core_io.h"
#include "main.h"
#include "init.h"
#include "flat-database.h"
#include "governance.h"
#include "masternode.h"
2016-04-15 04:54:11 +02:00
#include "governance.h"
2016-04-14 00:41:40 +02:00
#include "darksend.h"
#include "masternodeman.h"
#include "masternode-sync.h"
#include "util.h"
#include "addrman.h"
#include <boost/lexical_cast.hpp>
CBudgetVote::CBudgetVote()
2016-04-08 19:47:00 +02:00
{
vin = CTxIn();
2016-04-14 00:41:40 +02:00
nProposalHash = uint256();
2016-04-08 19:47:00 +02:00
nVote = VOTE_ABSTAIN;
nTime = 0;
fValid = true;
fSynced = false;
}
2016-04-14 00:41:40 +02:00
CBudgetVote::CBudgetVote(CTxIn vinIn, uint256 nProposalHashIn, int nVoteIn)
2016-04-08 19:47:00 +02:00
{
vin = vinIn;
2016-04-14 00:41:40 +02:00
nProposalHash = nProposalHashIn;
2016-04-08 19:47:00 +02:00
nVote = nVoteIn;
nTime = GetAdjustedTime();
fValid = true;
fSynced = false;
}
2016-04-14 00:41:40 +02:00
void CBudgetVote::Relay()
2016-04-08 19:47:00 +02:00
{
2016-04-14 00:41:40 +02:00
CInv inv(MSG_BUDGET_VOTE, GetHash());
2016-04-08 19:47:00 +02:00
RelayInv(inv, MIN_BUDGET_PEER_PROTO_VERSION);
}
2016-04-14 00:41:40 +02:00
bool CBudgetVote::Sign(CKey& keyMasternode, CPubKey& pubKeyMasternode)
2016-04-08 19:47:00 +02:00
{
// Choose coins to use
CPubKey pubKeyCollateralAddress;
CKey keyCollateralAddress;
std::string errorMessage;
2016-04-14 00:41:40 +02:00
std::string strMessage = vin.prevout.ToStringShort() + nProposalHash.ToString() + boost::lexical_cast<std::string>(nVote) + boost::lexical_cast<std::string>(nTime);
2016-04-08 19:47:00 +02:00
if(!darkSendSigner.SignMessage(strMessage, errorMessage, vchSig, keyMasternode)) {
2016-04-14 00:41:40 +02:00
LogPrintf("CBudgetVote::Sign - Error upon calling SignMessage");
2016-04-08 19:47:00 +02:00
return false;
}
if(!darkSendSigner.VerifyMessage(pubKeyMasternode, vchSig, strMessage, errorMessage)) {
2016-04-14 00:41:40 +02:00
LogPrintf("CBudgetVote::Sign - Error upon calling VerifyMessage");
2016-04-08 19:47:00 +02:00
return false;
}
return true;
}
2016-04-14 00:41:40 +02:00
bool CBudgetVote::IsValid(bool fSignatureCheck)
2016-04-08 19:47:00 +02:00
{
if(nTime > GetTime() + (60*60)){
LogPrint("mngovernance", "CBudgetVote::IsValid() - vote is too far ahead of current time - %s - nTime %lli - Max Time %lli\n", GetHash().ToString(), nTime, GetTime() + (60*60));
2016-04-08 19:47:00 +02:00
return false;
}
CMasternode* pmn = mnodeman.Find(vin);
2016-04-14 00:41:40 +02:00
2016-04-08 19:47:00 +02:00
if(pmn == NULL)
{
LogPrint("mngovernance", "CBudgetVote::IsValid() - Unknown Masternode - %s\n", vin.ToString());
2016-04-08 19:47:00 +02:00
return false;
}
if(!fSignatureCheck) return true;
std::string errorMessage;
2016-04-14 00:41:40 +02:00
std::string strMessage = vin.prevout.ToStringShort() + nProposalHash.ToString() + boost::lexical_cast<std::string>(nVote) + boost::lexical_cast<std::string>(nTime);
2016-04-08 19:47:00 +02:00
if(!darkSendSigner.VerifyMessage(pmn->pubkey2, vchSig, strMessage, errorMessage)) {
2016-04-14 00:41:40 +02:00
LogPrintf("CBudgetVote::IsValid() - Verify message failed - Error: %s\n", errorMessage);
2016-04-08 19:47:00 +02:00
return false;
}
return true;
2016-04-14 00:41:40 +02:00
}