2016-04-09 21:57:53 +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.
|
|
|
|
|
|
|
|
#include "core_io.h"
|
|
|
|
#include "main.h"
|
|
|
|
#include "init.h"
|
|
|
|
|
2016-04-10 08:31:32 +02:00
|
|
|
#include "flat-database.h"
|
2016-04-09 22:55:52 +02:00
|
|
|
#include "governance.h"
|
2016-04-14 00:41:40 +02:00
|
|
|
#include "governance-vote.h"
|
2016-08-17 09:08:25 +02:00
|
|
|
#include "governance-classes.h"
|
2016-04-09 21:57:53 +02:00
|
|
|
#include "masternode.h"
|
2016-04-15 04:54:11 +02:00
|
|
|
#include "governance.h"
|
2016-04-09 21:57:53 +02:00
|
|
|
#include "darksend.h"
|
|
|
|
#include "masternodeman.h"
|
|
|
|
#include "masternode-sync.h"
|
2016-09-27 09:50:04 +02:00
|
|
|
#include "netfulfilledman.h"
|
2016-04-09 21:57:53 +02:00
|
|
|
#include "util.h"
|
|
|
|
#include "addrman.h"
|
|
|
|
#include <boost/lexical_cast.hpp>
|
2016-08-17 09:08:25 +02:00
|
|
|
#include <univalue.h>
|
2016-04-09 21:57:53 +02:00
|
|
|
|
2016-04-10 16:46:19 +02:00
|
|
|
CGovernanceManager governance;
|
2016-04-09 21:57:53 +02:00
|
|
|
|
2016-05-23 20:10:20 +02:00
|
|
|
std::map<uint256, int64_t> mapAskedForGovernanceObject;
|
2016-04-09 21:57:53 +02:00
|
|
|
|
|
|
|
int nSubmittedFinalBudget;
|
|
|
|
|
2016-11-13 18:52:34 +01:00
|
|
|
const std::string CGovernanceManager::SERIALIZATION_VERSION_STRING = "CGovernanceManager-Version-1";
|
|
|
|
|
2016-09-05 01:44:10 +02:00
|
|
|
CGovernanceManager::CGovernanceManager()
|
2016-11-13 18:52:34 +01:00
|
|
|
: pCurrentBlockIndex(NULL),
|
2016-09-05 01:44:10 +02:00
|
|
|
nTimeLastDiff(0),
|
|
|
|
nCachedBlockHeight(0),
|
|
|
|
mapObjects(),
|
|
|
|
mapSeenGovernanceObjects(),
|
2016-11-12 02:51:45 +01:00
|
|
|
mapMasternodeOrphanObjects(),
|
2016-11-13 18:52:34 +01:00
|
|
|
mapVoteToObject(MAX_CACHE_SIZE),
|
|
|
|
mapInvalidVotes(MAX_CACHE_SIZE),
|
|
|
|
mapOrphanVotes(MAX_CACHE_SIZE),
|
2016-09-05 01:44:10 +02:00
|
|
|
mapLastMasternodeTrigger(),
|
2016-11-13 18:52:34 +01:00
|
|
|
setRequestedObjects(),
|
2016-11-18 15:17:22 +01:00
|
|
|
fRateChecksEnabled(true),
|
2016-09-05 01:44:10 +02:00
|
|
|
cs()
|
|
|
|
{}
|
2016-04-09 21:57:53 +02:00
|
|
|
|
2016-08-17 09:08:25 +02:00
|
|
|
// Accessors for thread-safe access to maps
|
2016-09-12 09:40:00 +02:00
|
|
|
bool CGovernanceManager::HaveObjectForHash(uint256 nHash) {
|
2016-08-17 09:08:25 +02:00
|
|
|
LOCK(cs);
|
|
|
|
return (mapObjects.count(nHash) == 1);
|
|
|
|
}
|
|
|
|
|
2016-11-13 18:52:34 +01:00
|
|
|
bool CGovernanceManager::SerializeObjectForHash(uint256 nHash, CDataStream& ss)
|
|
|
|
{
|
2016-08-17 09:08:25 +02:00
|
|
|
LOCK(cs);
|
|
|
|
object_m_it it = mapObjects.find(nHash);
|
2016-09-12 09:40:00 +02:00
|
|
|
if (it == mapObjects.end()) {
|
2016-08-17 09:08:25 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
ss << it->second;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-11-13 18:52:34 +01:00
|
|
|
bool CGovernanceManager::HaveVoteForHash(uint256 nHash)
|
|
|
|
{
|
2016-08-17 09:08:25 +02:00
|
|
|
LOCK(cs);
|
2016-11-13 18:52:34 +01:00
|
|
|
|
|
|
|
CGovernanceObject* pGovobj = NULL;
|
|
|
|
if(!mapVoteToObject.Get(nHash,pGovobj)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!pGovobj->GetVoteFile().HasVote(nHash)) {
|
2016-08-17 09:08:25 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-11-13 18:52:34 +01:00
|
|
|
bool CGovernanceManager::SerializeVoteForHash(uint256 nHash, CDataStream& ss)
|
2016-08-17 09:08:25 +02:00
|
|
|
{
|
|
|
|
LOCK(cs);
|
2016-11-13 18:52:34 +01:00
|
|
|
|
|
|
|
CGovernanceObject* pGovobj = NULL;
|
|
|
|
if(!mapVoteToObject.Get(nHash,pGovobj)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
CGovernanceVote vote;
|
|
|
|
if(!pGovobj->GetVoteFile().GetVote(nHash, vote)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
ss << vote;
|
|
|
|
return true;
|
2016-08-17 09:08:25 +02:00
|
|
|
}
|
|
|
|
|
2016-11-13 18:52:34 +01:00
|
|
|
void CGovernanceManager::AddSeenGovernanceObject(uint256 nHash, int status)
|
2016-08-17 09:08:25 +02:00
|
|
|
{
|
|
|
|
LOCK(cs);
|
2016-11-13 18:52:34 +01:00
|
|
|
mapSeenGovernanceObjects[nHash] = status;
|
2016-08-17 09:08:25 +02:00
|
|
|
}
|
|
|
|
|
2016-06-08 08:57:16 +02:00
|
|
|
void CGovernanceManager::ProcessMessage(CNode* pfrom, std::string& strCommand, CDataStream& vRecv)
|
|
|
|
{
|
|
|
|
// lite mode is not supported
|
|
|
|
if(fLiteMode) return;
|
|
|
|
if(!masternodeSync.IsBlockchainSynced()) return;
|
|
|
|
|
2016-09-28 22:03:54 +02:00
|
|
|
if(pfrom->nVersion < MIN_GOVERNANCE_PEER_PROTO_VERSION) return;
|
2016-08-05 18:25:03 +02:00
|
|
|
|
2016-11-13 18:52:34 +01:00
|
|
|
LOCK(cs);
|
2016-06-08 08:57:16 +02:00
|
|
|
|
2016-08-17 09:08:25 +02:00
|
|
|
// ANOTHER USER IS ASKING US TO HELP THEM SYNC GOVERNANCE OBJECT DATA
|
2016-06-08 08:57:16 +02:00
|
|
|
if (strCommand == NetMsgType::MNGOVERNANCESYNC)
|
|
|
|
{
|
|
|
|
|
2016-09-12 09:40:00 +02:00
|
|
|
// Ignore such requests until we are fully synced.
|
|
|
|
// We could start processing this after masternode list is synced
|
|
|
|
// but this is a heavy one so it's better to finish sync first.
|
2016-08-05 18:25:03 +02:00
|
|
|
if (!masternodeSync.IsSynced()) return;
|
|
|
|
|
2016-06-08 08:57:16 +02:00
|
|
|
uint256 nProp;
|
|
|
|
vRecv >> nProp;
|
|
|
|
|
2016-09-27 09:50:04 +02:00
|
|
|
if(nProp == uint256()) {
|
|
|
|
if(netfulfilledman.HasFulfilledRequest(pfrom->addr, NetMsgType::MNGOVERNANCESYNC)) {
|
|
|
|
// Asking for the whole list multiple times in a short period of time is no good
|
2016-10-22 18:52:14 +02:00
|
|
|
LogPrint("gobject", "MNGOVERNANCESYNC -- peer already asked me for the list\n");
|
2016-09-27 09:50:04 +02:00
|
|
|
Misbehaving(pfrom->GetId(), 20);
|
|
|
|
return;
|
2016-06-08 08:57:16 +02:00
|
|
|
}
|
2016-09-27 09:50:04 +02:00
|
|
|
netfulfilledman.AddFulfilledRequest(pfrom->addr, NetMsgType::MNGOVERNANCESYNC);
|
2016-06-08 08:57:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Sync(pfrom, nProp);
|
2016-10-22 18:52:14 +02:00
|
|
|
LogPrint("gobject", "MNGOVERNANCESYNC -- syncing governance objects to our peer at %s\n", pfrom->addr.ToString());
|
2016-09-12 09:40:00 +02:00
|
|
|
|
2016-06-08 08:57:16 +02:00
|
|
|
}
|
|
|
|
|
2016-08-17 09:08:25 +02:00
|
|
|
// A NEW GOVERNANCE OBJECT HAS ARRIVED
|
2016-06-08 08:57:16 +02:00
|
|
|
else if (strCommand == NetMsgType::MNGOVERNANCEOBJECT)
|
|
|
|
|
|
|
|
{
|
2016-06-10 07:16:32 +02:00
|
|
|
// MAKE SURE WE HAVE A VALID REFERENCE TO THE TIP BEFORE CONTINUING
|
|
|
|
|
2016-11-12 12:14:50 +01:00
|
|
|
if(!pCurrentBlockIndex) {
|
|
|
|
LogPrintf("CGovernanceManager::ProcessMessage MNGOVERNANCEOBJECT -- pCurrentBlockIndex is NULL\n");
|
|
|
|
return;
|
|
|
|
}
|
2016-06-08 08:57:16 +02:00
|
|
|
|
|
|
|
CGovernanceObject govobj;
|
|
|
|
vRecv >> govobj;
|
|
|
|
|
2016-11-13 18:52:34 +01:00
|
|
|
uint256 nHash = govobj.GetHash();
|
|
|
|
std::string strHash = nHash.ToString();
|
|
|
|
|
|
|
|
LogPrint("gobject", "CGovernanceManager -- Received object: %s\n", strHash);
|
|
|
|
|
|
|
|
if(!AcceptObjectMessage(nHash)) {
|
|
|
|
LogPrintf("CGovernanceManager -- Received unrequested object: %s\n", strHash);
|
|
|
|
Misbehaving(pfrom->GetId(), 20);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(mapSeenGovernanceObjects.count(nHash)) {
|
2016-06-08 08:57:16 +02:00
|
|
|
// TODO - print error code? what if it's GOVOBJ_ERROR_IMMATURE?
|
2016-11-13 18:52:34 +01:00
|
|
|
LogPrint("gobject", "CGovernanceManager -- Received already seen object: %s\n", strHash);
|
2016-06-08 08:57:16 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string strError = "";
|
2016-09-12 09:40:00 +02:00
|
|
|
// CHECK OBJECT AGAINST LOCAL BLOCKCHAIN
|
2016-08-17 09:08:25 +02:00
|
|
|
|
2016-11-12 02:51:45 +01:00
|
|
|
bool fMasternodeMissing = false;
|
|
|
|
bool fIsValid = govobj.IsValidLocally(pCurrentBlockIndex, strError, fMasternodeMissing, true);
|
|
|
|
|
|
|
|
if(fMasternodeMissing) {
|
|
|
|
mapMasternodeOrphanObjects.insert(std::make_pair(govobj.GetHash(), govobj));
|
|
|
|
LogPrint("gobject", "CGovernanceManager -- Missing masternode for: %s\n", strHash);
|
|
|
|
// fIsValid must also be false here so we will return early in the next if block
|
|
|
|
}
|
|
|
|
if(!fIsValid) {
|
2016-11-13 18:52:34 +01:00
|
|
|
mapSeenGovernanceObjects.insert(std::make_pair(nHash, SEEN_OBJECT_ERROR_INVALID));
|
2016-10-22 18:52:14 +02:00
|
|
|
LogPrintf("MNGOVERNANCEOBJECT -- Governance object is invalid - %s\n", strError);
|
2016-06-08 08:57:16 +02:00
|
|
|
return;
|
|
|
|
}
|
2016-09-12 09:40:00 +02:00
|
|
|
|
2016-06-10 07:16:32 +02:00
|
|
|
// UPDATE CACHED VARIABLES FOR THIS OBJECT AND ADD IT TO OUR MANANGED DATA
|
2016-06-08 08:57:16 +02:00
|
|
|
|
2016-10-22 18:52:14 +02:00
|
|
|
govobj.UpdateSentinelVariables(pCurrentBlockIndex); //this sets local vars in object
|
2016-08-17 09:08:25 +02:00
|
|
|
|
2016-10-22 18:52:14 +02:00
|
|
|
if(AddGovernanceObject(govobj))
|
|
|
|
{
|
2016-11-13 18:52:34 +01:00
|
|
|
LogPrintf("MNGOVERNANCEOBJECT -- %s new\n", strHash);
|
2016-10-22 18:52:14 +02:00
|
|
|
govobj.Relay();
|
2016-08-17 09:08:25 +02:00
|
|
|
}
|
2016-06-08 08:57:16 +02:00
|
|
|
|
2016-08-17 09:08:25 +02:00
|
|
|
// UPDATE THAT WE'VE SEEN THIS OBJECT
|
2016-10-22 18:52:14 +02:00
|
|
|
mapSeenGovernanceObjects.insert(std::make_pair(govobj.GetHash(), SEEN_OBJECT_IS_VALID));
|
2016-06-08 08:57:16 +02:00
|
|
|
masternodeSync.AddedBudgetItem(govobj.GetHash());
|
|
|
|
|
2016-10-22 18:52:14 +02:00
|
|
|
|
2016-08-17 09:08:25 +02:00
|
|
|
// WE MIGHT HAVE PENDING/ORPHAN VOTES FOR THIS OBJECT
|
|
|
|
|
2016-11-13 18:52:34 +01:00
|
|
|
CGovernanceException exception;
|
|
|
|
CheckOrphanVotes(pfrom, govobj, exception);
|
2016-06-08 08:57:16 +02:00
|
|
|
}
|
|
|
|
|
2016-08-17 09:08:25 +02:00
|
|
|
// A NEW GOVERNANCE OBJECT VOTE HAS ARRIVED
|
2016-08-05 18:25:03 +02:00
|
|
|
else if (strCommand == NetMsgType::MNGOVERNANCEOBJECTVOTE)
|
2016-06-08 08:57:16 +02:00
|
|
|
{
|
2016-08-29 21:11:34 +02:00
|
|
|
// Ignore such messages until masternode list is synced
|
2016-11-12 12:14:50 +01:00
|
|
|
if(!masternodeSync.IsMasternodeListSynced()) {
|
|
|
|
LogPrint("gobject", "CGovernanceManager::ProcessMessage MNGOVERNANCEOBJECTVOTE -- masternode list not synced\n");
|
|
|
|
return;
|
|
|
|
}
|
2016-08-29 21:11:34 +02:00
|
|
|
|
2016-06-08 08:57:16 +02:00
|
|
|
CGovernanceVote vote;
|
|
|
|
vRecv >> vote;
|
|
|
|
|
2016-11-13 18:52:34 +01:00
|
|
|
LogPrint("gobject", "CGovernanceManager -- Received vote: %s\n", vote.ToString());
|
2016-08-17 09:08:25 +02:00
|
|
|
|
2016-11-13 18:52:34 +01:00
|
|
|
if(!AcceptVoteMessage(vote.GetHash())) {
|
|
|
|
LogPrintf("CGovernanceManager -- Received unrequested vote object: %s, hash: %s, peer = %d\n",
|
|
|
|
vote.ToString(),
|
|
|
|
vote.GetHash().ToString(),
|
|
|
|
pfrom->GetId());
|
|
|
|
//Misbehaving(pfrom->GetId(), 20);
|
2016-06-08 08:57:16 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-11-13 18:52:34 +01:00
|
|
|
CGovernanceException exception;
|
|
|
|
if(ProcessVote(pfrom, vote, exception)) {
|
|
|
|
LogPrint("gobject", "CGovernanceManager -- Accepted vote\n");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
LogPrint("gobject", "CGovernanceManager -- Rejected vote, error = %s\n", exception.what());
|
|
|
|
if((exception.GetNodePenalty() != 0) && masternodeSync.IsSynced()) {
|
|
|
|
Misbehaving(pfrom->GetId(), exception.GetNodePenalty());
|
|
|
|
}
|
|
|
|
return;
|
2016-06-08 08:57:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-13 18:52:34 +01:00
|
|
|
void CGovernanceManager::CheckOrphanVotes(CNode* pfrom, CGovernanceObject& govobj, CGovernanceException& exception)
|
2016-04-09 21:57:53 +02:00
|
|
|
{
|
2016-11-13 18:52:34 +01:00
|
|
|
uint256 nHash = govobj.GetHash();
|
|
|
|
std::vector<CGovernanceVote> vecVotes;
|
|
|
|
mapOrphanVotes.GetAll(nHash, vecVotes);
|
|
|
|
|
|
|
|
for(size_t i = 0; i < vecVotes.size(); ++i) {
|
|
|
|
CGovernanceVote& vote = vecVotes[i];
|
|
|
|
CGovernanceException exception;
|
|
|
|
if(govobj.ProcessVote(pfrom, vote, exception)) {
|
|
|
|
vecVotes[i].Relay();
|
|
|
|
mapOrphanVotes.Erase(nHash, vote);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if((exception.GetNodePenalty() != 0) && masternodeSync.IsSynced()) {
|
|
|
|
Misbehaving(pfrom->GetId(), exception.GetNodePenalty());
|
|
|
|
}
|
2016-04-09 21:57:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-28 12:31:44 +02:00
|
|
|
bool CGovernanceManager::AddGovernanceObject(CGovernanceObject& govobj)
|
2016-04-09 21:57:53 +02:00
|
|
|
{
|
|
|
|
LOCK(cs);
|
|
|
|
std::string strError = "";
|
2016-08-17 09:08:25 +02:00
|
|
|
|
|
|
|
DBG( cout << "CGovernanceManager::AddGovernanceObject START" << endl; );
|
|
|
|
|
|
|
|
// MAKE SURE THIS OBJECT IS OK
|
|
|
|
|
|
|
|
if(!govobj.IsValidLocally(pCurrentBlockIndex, strError, true)) {
|
2016-10-22 18:52:14 +02:00
|
|
|
LogPrintf("CGovernanceManager::AddGovernanceObject -- invalid governance object - %s - (pCurrentBlockIndex nHeight %d) \n", strError, pCurrentBlockIndex->nHeight);
|
2016-04-09 21:57:53 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-08-17 09:08:25 +02:00
|
|
|
// IF WE HAVE THIS OBJECT ALREADY, WE DON'T WANT ANOTHER COPY
|
|
|
|
|
2016-05-28 12:31:44 +02:00
|
|
|
if(mapObjects.count(govobj.GetHash())) {
|
2016-10-22 18:52:14 +02:00
|
|
|
LogPrintf("CGovernanceManager::AddGovernanceObject -- already have governance object - %s\n", strError);
|
2016-04-09 21:57:53 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-08-17 09:08:25 +02:00
|
|
|
// INSERT INTO OUR GOVERNANCE OBJECT MEMORY
|
2016-09-12 09:40:00 +02:00
|
|
|
mapObjects.insert(std::make_pair(govobj.GetHash(), govobj));
|
2016-08-17 09:08:25 +02:00
|
|
|
|
|
|
|
// SHOULD WE ADD THIS OBJECT TO ANY OTHER MANANGERS?
|
|
|
|
|
|
|
|
DBG( cout << "CGovernanceManager::AddGovernanceObject Before trigger block, strData = "
|
|
|
|
<< govobj.GetDataAsString()
|
|
|
|
<< ", nObjectType = " << govobj.nObjectType
|
|
|
|
<< endl; );
|
|
|
|
|
2016-11-13 18:52:34 +01:00
|
|
|
if(govobj.GetObjectType() == GOVERNANCE_OBJECT_TRIGGER) {
|
|
|
|
mapLastMasternodeTrigger[govobj.GetMasternodeVin().prevout] = nCachedBlockHeight;
|
|
|
|
}
|
|
|
|
|
2016-10-17 20:54:28 +02:00
|
|
|
switch(govobj.nObjectType) {
|
|
|
|
case GOVERNANCE_OBJECT_TRIGGER:
|
2016-09-15 08:49:24 +02:00
|
|
|
mapLastMasternodeTrigger[govobj.vinMasternode.prevout] = nCachedBlockHeight;
|
2016-08-17 09:08:25 +02:00
|
|
|
DBG( cout << "CGovernanceManager::AddGovernanceObject Before AddNewTrigger" << endl; );
|
|
|
|
triggerman.AddNewTrigger(govobj.GetHash());
|
|
|
|
DBG( cout << "CGovernanceManager::AddGovernanceObject After AddNewTrigger" << endl; );
|
2016-10-17 20:54:28 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2016-08-17 09:08:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
DBG( cout << "CGovernanceManager::AddGovernanceObject END" << endl; );
|
|
|
|
|
2016-04-09 21:57:53 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-08-17 09:08:25 +02:00
|
|
|
void CGovernanceManager::UpdateCachesAndClean()
|
2016-04-09 21:57:53 +02:00
|
|
|
{
|
2016-10-22 18:52:14 +02:00
|
|
|
LogPrint("gobject", "CGovernanceManager::UpdateCachesAndClean\n");
|
2016-08-17 09:08:25 +02:00
|
|
|
|
2016-10-17 20:54:28 +02:00
|
|
|
std::vector<uint256> vecDirtyHashes = mnodeman.GetAndClearDirtyGovernanceObjectHashes();
|
|
|
|
|
2016-08-17 09:08:25 +02:00
|
|
|
LOCK(cs);
|
2016-04-09 21:57:53 +02:00
|
|
|
|
2016-10-17 20:54:28 +02:00
|
|
|
for(size_t i = 0; i < vecDirtyHashes.size(); ++i) {
|
|
|
|
object_m_it it = mapObjects.find(vecDirtyHashes[i]);
|
|
|
|
if(it == mapObjects.end()) {
|
|
|
|
continue;
|
|
|
|
}
|
2016-11-13 18:52:34 +01:00
|
|
|
it->second.ClearMasternodeVotes();
|
2016-10-17 20:54:28 +02:00
|
|
|
it->second.fDirtyCache = true;
|
|
|
|
}
|
|
|
|
|
2016-06-10 07:16:32 +02:00
|
|
|
// DOUBLE CHECK THAT WE HAVE A VALID POINTER TO TIP
|
|
|
|
|
|
|
|
if(!pCurrentBlockIndex) return;
|
|
|
|
|
2016-11-05 17:13:30 +01:00
|
|
|
LogPrint("gobject", "CGovernanceManager::UpdateCachesAndClean -- After pCurrentBlockIndex (not NULL)\n");
|
|
|
|
|
2016-08-17 09:08:25 +02:00
|
|
|
// UPDATE CACHE FOR EACH OBJECT THAT IS FLAGGED DIRTYCACHE=TRUE
|
2016-05-24 20:11:59 +02:00
|
|
|
|
2016-09-12 09:40:00 +02:00
|
|
|
object_m_it it = mapObjects.begin();
|
2016-08-17 09:08:25 +02:00
|
|
|
|
|
|
|
// Clean up any expired or invalid triggers
|
|
|
|
triggerman.CleanAndRemove();
|
|
|
|
|
2016-05-24 20:11:59 +02:00
|
|
|
while(it != mapObjects.end())
|
2016-09-12 09:40:00 +02:00
|
|
|
{
|
2016-05-24 20:11:59 +02:00
|
|
|
CGovernanceObject* pObj = &((*it).second);
|
|
|
|
|
2016-09-12 09:40:00 +02:00
|
|
|
if(!pObj) {
|
2016-08-17 09:08:25 +02:00
|
|
|
++it;
|
|
|
|
continue;
|
|
|
|
}
|
2016-05-24 20:11:59 +02:00
|
|
|
|
2016-08-17 09:08:25 +02:00
|
|
|
// IF CACHE IS NOT DIRTY, WHY DO THIS?
|
2016-11-13 18:52:34 +01:00
|
|
|
if(pObj->IsSetDirtyCache()) {
|
2016-08-17 09:08:25 +02:00
|
|
|
// UPDATE LOCAL VALIDITY AGAINST CRYPTO DATA
|
|
|
|
pObj->UpdateLocalValidity(pCurrentBlockIndex);
|
2016-09-12 09:40:00 +02:00
|
|
|
|
2016-08-17 09:08:25 +02:00
|
|
|
// UPDATE SENTINEL SIGNALING VARIABLES
|
|
|
|
pObj->UpdateSentinelVariables(pCurrentBlockIndex);
|
|
|
|
}
|
2016-04-09 22:55:52 +02:00
|
|
|
|
2016-08-17 09:08:25 +02:00
|
|
|
// IF DELETE=TRUE, THEN CLEAN THE MESS UP!
|
2016-05-24 20:11:59 +02:00
|
|
|
|
2016-11-18 15:17:00 +01:00
|
|
|
int64_t nTimeSinceDeletion = GetAdjustedTime() - pObj->GetDeletionTime();
|
|
|
|
|
|
|
|
if((pObj->IsSetCachedDelete() || pObj->IsSetExpired()) &&
|
|
|
|
(nTimeSinceDeletion >= GOVERNANCE_DELETION_DELAY)) {
|
2016-11-05 17:13:30 +01:00
|
|
|
LogPrintf("CGovernanceManager::UpdateCachesAndClean -- erase obj %s\n", (*it).first.ToString());
|
2016-10-17 20:54:28 +02:00
|
|
|
mnodeman.RemoveGovernanceObject(pObj->GetHash());
|
2016-11-13 18:52:34 +01:00
|
|
|
|
|
|
|
// Remove vote references
|
|
|
|
const object_ref_cache_t::list_t& listItems = mapVoteToObject.GetItemList();
|
|
|
|
object_ref_cache_t::list_cit lit = listItems.begin();
|
|
|
|
while(lit != listItems.end()) {
|
|
|
|
if(lit->value == pObj) {
|
|
|
|
uint256 nKey = lit->key;
|
|
|
|
++lit;
|
|
|
|
mapVoteToObject.Erase(nKey);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
++lit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-17 09:08:25 +02:00
|
|
|
mapObjects.erase(it++);
|
|
|
|
} else {
|
|
|
|
++it;
|
|
|
|
}
|
2016-05-23 23:39:10 +02:00
|
|
|
}
|
2016-04-09 21:57:53 +02:00
|
|
|
}
|
|
|
|
|
2016-06-08 08:57:16 +02:00
|
|
|
CGovernanceObject *CGovernanceManager::FindGovernanceObject(const uint256& nHash)
|
2016-04-09 21:57:53 +02:00
|
|
|
{
|
|
|
|
LOCK(cs);
|
|
|
|
|
2016-05-13 18:03:01 +02:00
|
|
|
if(mapObjects.count(nHash))
|
|
|
|
return &mapObjects[nHash];
|
2016-04-09 21:57:53 +02:00
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2016-11-13 18:52:34 +01:00
|
|
|
std::vector<CGovernanceVote> CGovernanceManager::GetMatchingVotes(const uint256& nParentHash)
|
2016-08-17 09:08:25 +02:00
|
|
|
{
|
2016-11-13 18:52:34 +01:00
|
|
|
LOCK(cs);
|
|
|
|
std::vector<CGovernanceVote> vecResult;
|
2016-08-17 09:08:25 +02:00
|
|
|
|
2016-11-13 18:52:34 +01:00
|
|
|
object_m_it it = mapObjects.find(nParentHash);
|
|
|
|
if(it == mapObjects.end()) {
|
|
|
|
return vecResult;
|
2016-08-17 09:08:25 +02:00
|
|
|
}
|
2016-11-13 18:52:34 +01:00
|
|
|
CGovernanceObject& govobj = it->second;
|
2016-08-17 09:08:25 +02:00
|
|
|
|
2016-11-13 18:52:34 +01:00
|
|
|
return govobj.GetVoteFile().GetVotes();
|
2016-08-17 09:08:25 +02:00
|
|
|
}
|
|
|
|
|
2016-11-22 20:26:36 +01:00
|
|
|
std::vector<CGovernanceVote> CGovernanceManager::GetCurrentVotes(const uint256& nParentHash, const CTxIn& mnCollateralOutpointFilter)
|
|
|
|
{
|
|
|
|
LOCK(cs);
|
|
|
|
std::vector<CGovernanceVote> vecResult;
|
|
|
|
|
|
|
|
// Find the governance object or short-circuit.
|
|
|
|
object_m_it it = mapObjects.find(nParentHash);
|
|
|
|
if(it == mapObjects.end()) return vecResult;
|
|
|
|
CGovernanceObject& govobj = it->second;
|
|
|
|
|
|
|
|
// Compile a list of Masternode collateral outpoints for which to get votes
|
|
|
|
std::vector<CTxIn> vecMNTxIn;
|
|
|
|
if (mnCollateralOutpointFilter == CTxIn()) {
|
|
|
|
std::vector<CMasternode> mnlist = mnodeman.GetFullMasternodeVector();
|
|
|
|
for (std::vector<CMasternode>::iterator it = mnlist.begin(); it != mnlist.end(); ++it)
|
|
|
|
{
|
|
|
|
vecMNTxIn.push_back(it->vin);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
vecMNTxIn.push_back(mnCollateralOutpointFilter);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Loop thru each MN collateral outpoint and get the votes for the `nParentHash` governance object
|
|
|
|
for (std::vector<CTxIn>::iterator it = vecMNTxIn.begin(); it != vecMNTxIn.end(); ++it)
|
|
|
|
{
|
|
|
|
CTxIn &mnCollateralOutpoint = *it;
|
|
|
|
|
|
|
|
// get a vote_rec_t from the govobj
|
|
|
|
vote_rec_t voteRecord;
|
|
|
|
if (!govobj.GetCurrentMNVotes(mnCollateralOutpoint, voteRecord)) continue;
|
|
|
|
|
|
|
|
for (vote_instance_m_it it3 = voteRecord.mapInstances.begin(); it3 != voteRecord.mapInstances.end(); ++it3) {
|
|
|
|
int signal = (it3->first);
|
|
|
|
int outcome = ((it3->second).eOutcome);
|
|
|
|
int64_t nTime = ((it3->second).nTime);
|
|
|
|
|
|
|
|
CGovernanceVote vote = CGovernanceVote(mnCollateralOutpoint, nParentHash, (vote_signal_enum_t)signal, (vote_outcome_enum_t)outcome);
|
|
|
|
vote.SetTime(nTime);
|
|
|
|
|
|
|
|
vecResult.push_back(vote);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return vecResult;
|
|
|
|
}
|
|
|
|
|
2016-08-17 09:08:25 +02:00
|
|
|
std::vector<CGovernanceObject*> CGovernanceManager::GetAllNewerThan(int64_t nMoreThanTime)
|
2016-04-09 21:57:53 +02:00
|
|
|
{
|
|
|
|
LOCK(cs);
|
|
|
|
|
2016-05-28 12:31:44 +02:00
|
|
|
std::vector<CGovernanceObject*> vGovObjs;
|
2016-04-09 21:57:53 +02:00
|
|
|
|
2016-09-12 09:40:00 +02:00
|
|
|
object_m_it it = mapObjects.begin();
|
2016-05-13 18:03:01 +02:00
|
|
|
while(it != mapObjects.end())
|
2016-04-09 21:57:53 +02:00
|
|
|
{
|
2016-08-17 09:08:25 +02:00
|
|
|
// IF THIS OBJECT IS OLDER THAN TIME, CONTINUE
|
|
|
|
|
2016-11-13 18:52:34 +01:00
|
|
|
if((*it).second.GetCreationTime() < nMoreThanTime) {
|
2016-08-17 09:08:25 +02:00
|
|
|
++it;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ADD GOVERNANCE OBJECT TO LIST
|
2016-04-09 21:57:53 +02:00
|
|
|
|
2016-05-28 12:31:44 +02:00
|
|
|
CGovernanceObject* pGovObj = &((*it).second);
|
|
|
|
vGovObjs.push_back(pGovObj);
|
2016-04-09 21:57:53 +02:00
|
|
|
|
2016-08-17 09:08:25 +02:00
|
|
|
// NEXT
|
|
|
|
|
2016-04-09 21:57:53 +02:00
|
|
|
++it;
|
|
|
|
}
|
|
|
|
|
2016-05-28 12:31:44 +02:00
|
|
|
return vGovObjs;
|
2016-04-09 21:57:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Sort by votes, if there's a tie sort by their feeHash TX
|
|
|
|
//
|
|
|
|
struct sortProposalsByVotes {
|
2016-04-16 19:19:17 +02:00
|
|
|
bool operator()(const std::pair<CGovernanceObject*, int> &left, const std::pair<CGovernanceObject*, int> &right) {
|
2016-09-12 09:40:00 +02:00
|
|
|
if (left.second != right.second)
|
|
|
|
return (left.second > right.second);
|
2016-11-13 18:52:34 +01:00
|
|
|
return (UintToArith256(left.first->GetCollateralHash()) > UintToArith256(right.first->GetCollateralHash()));
|
2016-04-09 21:57:53 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-04-09 22:55:52 +02:00
|
|
|
void CGovernanceManager::NewBlock()
|
2016-04-09 21:57:53 +02:00
|
|
|
{
|
2016-08-17 09:08:25 +02:00
|
|
|
// IF WE'RE NOT SYNCED, EXIT
|
|
|
|
if(!masternodeSync.IsSynced()) return;
|
2016-04-09 21:57:53 +02:00
|
|
|
|
2016-08-29 21:11:34 +02:00
|
|
|
TRY_LOCK(cs, fBudgetNewBlock);
|
|
|
|
if(!fBudgetNewBlock || !pCurrentBlockIndex) return;
|
|
|
|
|
2016-08-17 09:08:25 +02:00
|
|
|
// CHECK OBJECTS WE'VE ASKED FOR, REMOVE OLD ENTRIES
|
2016-04-09 21:57:53 +02:00
|
|
|
|
2016-05-23 20:10:20 +02:00
|
|
|
std::map<uint256, int64_t>::iterator it = mapAskedForGovernanceObject.begin();
|
2016-11-13 18:52:34 +01:00
|
|
|
while(it != mapAskedForGovernanceObject.end()) {
|
|
|
|
if((*it).second > GetTime() - (60*60*24)) {
|
2016-04-09 21:57:53 +02:00
|
|
|
++it;
|
|
|
|
} else {
|
2016-05-23 20:10:20 +02:00
|
|
|
mapAskedForGovernanceObject.erase(it++);
|
2016-04-09 21:57:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-17 09:08:25 +02:00
|
|
|
// CHECK AND REMOVE - REPROCESS GOVERNANCE OBJECTS
|
2016-04-09 21:57:53 +02:00
|
|
|
|
2016-08-17 09:08:25 +02:00
|
|
|
UpdateCachesAndClean();
|
2016-04-09 21:57:53 +02:00
|
|
|
}
|
|
|
|
|
2016-11-13 18:52:34 +01:00
|
|
|
bool CGovernanceManager::ConfirmInventoryRequest(const CInv& inv)
|
|
|
|
{
|
|
|
|
LOCK(cs);
|
|
|
|
|
|
|
|
LogPrint("gobject", "CGovernanceManager::ConfirmInventoryRequest inv = %s\n", inv.ToString());
|
|
|
|
|
|
|
|
// First check if we've already recorded this object
|
|
|
|
switch(inv.type) {
|
|
|
|
case MSG_GOVERNANCE_OBJECT:
|
|
|
|
{
|
|
|
|
object_m_it it = mapObjects.find(inv.hash);
|
|
|
|
if(it != mapObjects.end()) {
|
|
|
|
LogPrint("gobject", "CGovernanceManager::ConfirmInventoryRequest already have governance object, returning false\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case MSG_GOVERNANCE_OBJECT_VOTE:
|
|
|
|
{
|
|
|
|
if(mapVoteToObject.HasKey(inv.hash)) {
|
|
|
|
LogPrint("gobject", "CGovernanceManager::ConfirmInventoryRequest already have governance vote, returning false\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
LogPrint("gobject", "CGovernanceManager::ConfirmInventoryRequest unknown type, returning false\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
hash_s_t* setHash = NULL;
|
|
|
|
switch(inv.type) {
|
|
|
|
case MSG_GOVERNANCE_OBJECT:
|
|
|
|
setHash = &setRequestedObjects;
|
|
|
|
break;
|
|
|
|
case MSG_GOVERNANCE_OBJECT_VOTE:
|
|
|
|
setHash = &setRequestedVotes;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
hash_s_cit it = setHash->find(inv.hash);
|
|
|
|
if(it == setHash->end()) {
|
|
|
|
setHash->insert(inv.hash);
|
|
|
|
LogPrint("gobject", "CGovernanceManager::ConfirmInventoryRequest added inv to requested set\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
LogPrint("gobject", "CGovernanceManager::ConfirmInventoryRequest reached end, returning true\n");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-04-14 00:41:40 +02:00
|
|
|
void CGovernanceManager::Sync(CNode* pfrom, uint256 nProp)
|
2016-04-09 21:57:53 +02:00
|
|
|
{
|
|
|
|
|
|
|
|
/*
|
|
|
|
This code checks each of the hash maps for all known budget proposals and finalized budget proposals, then checks them against the
|
|
|
|
budget object to see if they're OK. If all checks pass, we'll send it to the peer.
|
|
|
|
*/
|
|
|
|
|
|
|
|
int nInvCount = 0;
|
|
|
|
|
2016-05-24 20:11:59 +02:00
|
|
|
// SYNC GOVERNANCE OBJECTS WITH OTHER CLIENT
|
|
|
|
|
2016-08-17 09:08:25 +02:00
|
|
|
{
|
2016-11-13 18:52:34 +01:00
|
|
|
LOCK(cs);
|
|
|
|
for(object_m_it it = mapObjects.begin(); it != mapObjects.end(); ++it) {
|
|
|
|
uint256 h = it->first;
|
|
|
|
|
|
|
|
CGovernanceObject& govobj = it->second;
|
|
|
|
|
2016-11-15 03:27:05 +01:00
|
|
|
std::string strError;
|
|
|
|
if(govobj.IsSetCachedValid() &&
|
|
|
|
(nProp == uint256() || h == nProp) &&
|
|
|
|
govobj.IsValidLocally(pCurrentBlockIndex, strError, true)) {
|
2016-11-13 18:52:34 +01:00
|
|
|
// Push the inventory budget proposal message over to the other client
|
|
|
|
pfrom->PushInventory(CInv(MSG_GOVERNANCE_OBJECT, h));
|
|
|
|
++nInvCount;
|
|
|
|
|
|
|
|
std::vector<CGovernanceVote> vecVotes = govobj.GetVoteFile().GetVotes();
|
|
|
|
for(size_t i = 0; i < vecVotes.size(); ++i) {
|
|
|
|
if(!vecVotes[i].IsValid(true)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
pfrom->PushInventory(CInv(MSG_GOVERNANCE_OBJECT_VOTE, vecVotes[i].GetHash()));
|
|
|
|
++nInvCount;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-04-14 00:41:40 +02:00
|
|
|
}
|
|
|
|
|
2016-06-08 08:57:16 +02:00
|
|
|
pfrom->PushMessage(NetMsgType::SYNCSTATUSCOUNT, MASTERNODE_SYNC_GOVOBJ, nInvCount);
|
2016-11-15 14:23:49 +01:00
|
|
|
LogPrintf("CGovernanceManager::Sync -- sent %d items, peer=%d\n", nInvCount, pfrom->id);
|
2016-04-09 21:57:53 +02:00
|
|
|
}
|
|
|
|
|
2016-10-17 20:54:28 +02:00
|
|
|
bool CGovernanceManager::MasternodeRateCheck(const CTxIn& vin, int nObjectType)
|
2016-09-05 01:44:10 +02:00
|
|
|
{
|
|
|
|
LOCK(cs);
|
2016-10-17 20:54:28 +02:00
|
|
|
|
2016-11-18 15:17:22 +01:00
|
|
|
if(!fRateChecksEnabled) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-10-17 20:54:28 +02:00
|
|
|
int mindiff = 0;
|
|
|
|
switch(nObjectType) {
|
|
|
|
case GOVERNANCE_OBJECT_TRIGGER:
|
|
|
|
mindiff = Params().GetConsensus().nSuperblockCycle - Params().GetConsensus().nSuperblockCycle / 10;
|
|
|
|
break;
|
|
|
|
case GOVERNANCE_OBJECT_WATCHDOG:
|
|
|
|
mindiff = 1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-09-15 08:49:24 +02:00
|
|
|
txout_m_it it = mapLastMasternodeTrigger.find(vin.prevout);
|
2016-09-12 09:40:00 +02:00
|
|
|
if(it == mapLastMasternodeTrigger.end()) {
|
2016-09-05 01:44:10 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// Allow 1 trigger per mn per cycle, with a small fudge factor
|
2016-09-12 09:40:00 +02:00
|
|
|
if((nCachedBlockHeight - it->second) > mindiff) {
|
2016-09-05 01:44:10 +02:00
|
|
|
return true;
|
|
|
|
}
|
2016-09-15 08:49:24 +02:00
|
|
|
|
2016-10-22 18:52:14 +02:00
|
|
|
LogPrintf("CGovernanceManager::MasternodeRateCheck -- Rate too high: vin = %s, current height = %d, last MN height = %d, minimum difference = %d\n",
|
2016-09-15 08:49:24 +02:00
|
|
|
vin.prevout.ToStringShort(), nCachedBlockHeight, it->second, mindiff);
|
2016-09-05 01:44:10 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-11-13 18:52:34 +01:00
|
|
|
bool CGovernanceManager::ProcessVote(CNode* pfrom, const CGovernanceVote& vote, CGovernanceException& exception)
|
|
|
|
{
|
|
|
|
LOCK(cs);
|
|
|
|
uint256 nHashVote = vote.GetHash();
|
|
|
|
if(mapInvalidVotes.HasKey(nHashVote)) {
|
|
|
|
std::ostringstream ostr;
|
|
|
|
ostr << "CGovernanceManager::ProcessVote -- Old invalid vote "
|
|
|
|
<< ", MN outpoint = " << vote.GetVinMasternode().prevout.ToStringShort()
|
|
|
|
<< ", governance object hash = " << vote.GetParentHash().ToString() << "\n";
|
|
|
|
LogPrintf(ostr.str().c_str());
|
|
|
|
exception = CGovernanceException(ostr.str(), GOVERNANCE_EXCEPTION_PERMANENT_ERROR, 20);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint256 nHashGovobj = vote.GetParentHash();
|
|
|
|
object_m_it it = mapObjects.find(nHashGovobj);
|
|
|
|
if(it == mapObjects.end()) {
|
2016-11-14 21:26:53 +01:00
|
|
|
std::ostringstream ostr;
|
|
|
|
ostr << "CGovernanceManager::ProcessVote -- Unknown parent object "
|
|
|
|
<< ", MN outpoint = " << vote.GetVinMasternode().prevout.ToStringShort()
|
|
|
|
<< ", governance object hash = " << vote.GetParentHash().ToString() << "\n";
|
|
|
|
exception = CGovernanceException(ostr.str(), GOVERNANCE_EXCEPTION_WARNING);
|
2016-11-14 19:22:50 +01:00
|
|
|
if(mapOrphanVotes.Insert(nHashGovobj, vote)) {
|
|
|
|
RequestGovernanceObject(pfrom, nHashGovobj);
|
|
|
|
LogPrintf(ostr.str().c_str());
|
2016-11-14 21:26:53 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
LogPrint("gobject", ostr.str().c_str());
|
2016-11-14 19:22:50 +01:00
|
|
|
}
|
2016-11-13 18:52:34 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
CGovernanceObject& govobj = it->second;
|
|
|
|
bool fOk = govobj.ProcessVote(pfrom, vote, exception);
|
|
|
|
if(fOk) {
|
|
|
|
mapVoteToObject.Insert(vote.GetHash(), &govobj);
|
|
|
|
|
|
|
|
if(govobj.GetObjectType() == GOVERNANCE_OBJECT_WATCHDOG) {
|
|
|
|
mnodeman.UpdateWatchdogVoteTime(vote.GetVinMasternode());
|
|
|
|
}
|
2016-11-14 19:07:11 +01:00
|
|
|
|
|
|
|
vote.Relay();
|
2016-11-13 18:52:34 +01:00
|
|
|
}
|
|
|
|
return fOk;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CGovernanceManager::CheckMasternodeOrphanVotes()
|
|
|
|
{
|
|
|
|
LOCK(cs);
|
2016-11-18 15:17:22 +01:00
|
|
|
fRateChecksEnabled = false;
|
2016-11-13 18:52:34 +01:00
|
|
|
for(object_m_it it = mapObjects.begin(); it != mapObjects.end(); ++it) {
|
|
|
|
it->second.CheckOrphanVotes();
|
|
|
|
}
|
2016-11-18 15:17:22 +01:00
|
|
|
fRateChecksEnabled = true;
|
2016-11-13 18:52:34 +01:00
|
|
|
}
|
|
|
|
|
2016-11-12 02:51:45 +01:00
|
|
|
void CGovernanceManager::CheckMasternodeOrphanObjects()
|
|
|
|
{
|
|
|
|
LOCK(cs);
|
2016-11-18 15:17:22 +01:00
|
|
|
fRateChecksEnabled = false;
|
2016-11-12 02:51:45 +01:00
|
|
|
object_m_it it = mapMasternodeOrphanObjects.begin();
|
|
|
|
while(it != mapMasternodeOrphanObjects.end()) {
|
|
|
|
CGovernanceObject& govobj = it->second;
|
|
|
|
|
|
|
|
string strError;
|
|
|
|
bool fMasternodeMissing = false;
|
|
|
|
bool fIsValid = govobj.IsValidLocally(pCurrentBlockIndex, strError, fMasternodeMissing, true);
|
|
|
|
if(!fIsValid) {
|
|
|
|
if(!fMasternodeMissing) {
|
|
|
|
mapMasternodeOrphanObjects.erase(it++);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
++it;
|
|
|
|
}
|
2016-11-16 23:28:10 +01:00
|
|
|
continue;
|
2016-11-12 02:51:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if(AddGovernanceObject(govobj)) {
|
|
|
|
LogPrintf("CGovernanceManager::CheckMasternodeOrphanObjects -- %s new\n", govobj.GetHash().ToString());
|
|
|
|
govobj.Relay();
|
|
|
|
mapMasternodeOrphanObjects.erase(it++);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
}
|
2016-11-18 15:17:22 +01:00
|
|
|
fRateChecksEnabled = true;
|
2016-11-12 02:51:45 +01:00
|
|
|
}
|
|
|
|
|
2016-11-13 18:52:34 +01:00
|
|
|
void CGovernanceManager::RequestGovernanceObject(CNode* pfrom, const uint256& nHash)
|
|
|
|
{
|
|
|
|
if(!pfrom) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
pfrom->PushMessage(NetMsgType::MNGOVERNANCESYNC, nHash);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CGovernanceManager::AcceptObjectMessage(const uint256& nHash)
|
|
|
|
{
|
|
|
|
LOCK(cs);
|
|
|
|
return AcceptMessage(nHash, setRequestedObjects);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CGovernanceManager::AcceptVoteMessage(const uint256& nHash)
|
|
|
|
{
|
|
|
|
LOCK(cs);
|
|
|
|
return AcceptMessage(nHash, setRequestedVotes);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CGovernanceManager::AcceptMessage(const uint256& nHash, hash_s_t& setHash)
|
|
|
|
{
|
|
|
|
hash_s_it it = setHash.find(nHash);
|
|
|
|
if(it == setHash.end()) {
|
|
|
|
// We never requested this
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// Only accept one response
|
|
|
|
setHash.erase(it);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CGovernanceManager::RebuildIndexes()
|
|
|
|
{
|
|
|
|
mapVoteToObject.Clear();
|
|
|
|
for(object_m_it it = mapObjects.begin(); it != mapObjects.end(); ++it) {
|
|
|
|
CGovernanceObject& govobj = it->second;
|
|
|
|
std::vector<CGovernanceVote> vecVotes = govobj.GetVoteFile().GetVotes();
|
|
|
|
for(size_t i = 0; i < vecVotes.size(); ++i) {
|
|
|
|
mapVoteToObject.Insert(vecVotes[i].GetHash(), &govobj);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int CGovernanceManager::GetMasternodeIndex(const CTxIn& masternodeVin)
|
|
|
|
{
|
|
|
|
LOCK(cs);
|
|
|
|
bool fIndexRebuilt = false;
|
|
|
|
int nMNIndex = mnodeman.GetMasternodeIndex(masternodeVin, fIndexRebuilt);
|
|
|
|
while(fIndexRebuilt) {
|
|
|
|
RebuildVoteMaps();
|
|
|
|
nMNIndex = mnodeman.GetMasternodeIndex(masternodeVin, fIndexRebuilt);
|
|
|
|
}
|
|
|
|
return nMNIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CGovernanceManager::RebuildVoteMaps()
|
|
|
|
{
|
|
|
|
for(object_m_it it = mapObjects.begin(); it != mapObjects.end(); ++it) {
|
|
|
|
it->second.RebuildVoteMap();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-05 17:13:30 +01:00
|
|
|
void CGovernanceManager::AddCachedTriggers()
|
|
|
|
{
|
|
|
|
LOCK(cs);
|
|
|
|
|
|
|
|
for(object_m_it it = mapObjects.begin(); it != mapObjects.end(); ++it) {
|
|
|
|
CGovernanceObject& govobj = it->second;
|
|
|
|
|
|
|
|
if(govobj.nObjectType != GOVERNANCE_OBJECT_TRIGGER) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
triggerman.AddNewTrigger(govobj.GetHash());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-16 19:19:17 +02:00
|
|
|
CGovernanceObject::CGovernanceObject()
|
2016-11-13 18:52:34 +01:00
|
|
|
: cs(),
|
|
|
|
nObjectType(GOVERNANCE_OBJECT_UNKNOWN),
|
|
|
|
nHashParent(),
|
|
|
|
nRevision(0),
|
|
|
|
nTime(0),
|
2016-11-18 15:17:00 +01:00
|
|
|
nDeletionTime(0),
|
2016-11-13 18:52:34 +01:00
|
|
|
nCollateralHash(),
|
|
|
|
strData(),
|
|
|
|
vinMasternode(),
|
|
|
|
vchSig(),
|
|
|
|
fCachedLocalValidity(false),
|
|
|
|
strLocalValidityError(),
|
|
|
|
fCachedFunding(false),
|
|
|
|
fCachedValid(true),
|
|
|
|
fCachedDelete(false),
|
|
|
|
fCachedEndorsed(false),
|
|
|
|
fDirtyCache(true),
|
|
|
|
fExpired(false),
|
|
|
|
fUnparsable(false),
|
|
|
|
mapCurrentMNVotes(),
|
|
|
|
mapOrphanVotes(),
|
|
|
|
fileVotes()
|
2016-04-09 21:57:53 +02:00
|
|
|
{
|
2016-08-17 09:08:25 +02:00
|
|
|
// PARSE JSON DATA STORAGE (STRDATA)
|
|
|
|
LoadData();
|
2016-04-09 21:57:53 +02:00
|
|
|
}
|
|
|
|
|
2016-09-17 21:37:48 +02:00
|
|
|
CGovernanceObject::CGovernanceObject(uint256 nHashParentIn, int nRevisionIn, int64_t nTimeIn, uint256 nCollateralHashIn, std::string strDataIn)
|
2016-11-13 18:52:34 +01:00
|
|
|
: cs(),
|
|
|
|
nObjectType(GOVERNANCE_OBJECT_UNKNOWN),
|
|
|
|
nHashParent(nHashParentIn),
|
|
|
|
nRevision(nRevisionIn),
|
|
|
|
nTime(nTimeIn),
|
2016-11-18 15:17:00 +01:00
|
|
|
nDeletionTime(0),
|
2016-11-13 18:52:34 +01:00
|
|
|
nCollateralHash(nCollateralHashIn),
|
|
|
|
strData(strDataIn),
|
|
|
|
vinMasternode(),
|
|
|
|
vchSig(),
|
|
|
|
fCachedLocalValidity(false),
|
|
|
|
strLocalValidityError(),
|
|
|
|
fCachedFunding(false),
|
|
|
|
fCachedValid(true),
|
|
|
|
fCachedDelete(false),
|
|
|
|
fCachedEndorsed(false),
|
|
|
|
fDirtyCache(true),
|
|
|
|
fExpired(false),
|
|
|
|
fUnparsable(false),
|
|
|
|
mapCurrentMNVotes(),
|
|
|
|
mapOrphanVotes(),
|
|
|
|
fileVotes()
|
2016-04-09 21:57:53 +02:00
|
|
|
{
|
2016-08-17 09:08:25 +02:00
|
|
|
// PARSE JSON DATA STORAGE (STRDATA)
|
|
|
|
LoadData();
|
2016-04-09 21:57:53 +02:00
|
|
|
}
|
|
|
|
|
2016-04-26 13:42:34 +02:00
|
|
|
CGovernanceObject::CGovernanceObject(const CGovernanceObject& other)
|
2016-11-13 18:52:34 +01:00
|
|
|
: cs(),
|
|
|
|
nObjectType(other.nObjectType),
|
|
|
|
nHashParent(other.nHashParent),
|
|
|
|
nRevision(other.nRevision),
|
|
|
|
nTime(other.nTime),
|
2016-11-18 15:17:00 +01:00
|
|
|
nDeletionTime(other.nDeletionTime),
|
2016-11-13 18:52:34 +01:00
|
|
|
nCollateralHash(other.nCollateralHash),
|
|
|
|
strData(other.strData),
|
|
|
|
vinMasternode(other.vinMasternode),
|
|
|
|
vchSig(other.vchSig),
|
|
|
|
fCachedLocalValidity(other.fCachedLocalValidity),
|
|
|
|
strLocalValidityError(other.strLocalValidityError),
|
|
|
|
fCachedFunding(other.fCachedFunding),
|
|
|
|
fCachedValid(other.fCachedValid),
|
|
|
|
fCachedDelete(other.fCachedDelete),
|
|
|
|
fCachedEndorsed(other.fCachedEndorsed),
|
|
|
|
fDirtyCache(other.fDirtyCache),
|
|
|
|
fExpired(other.fExpired),
|
|
|
|
fUnparsable(other.fUnparsable),
|
|
|
|
mapCurrentMNVotes(other.mapCurrentMNVotes),
|
|
|
|
mapOrphanVotes(other.mapOrphanVotes),
|
|
|
|
fileVotes(other.fileVotes)
|
2016-10-17 20:54:28 +02:00
|
|
|
{}
|
2016-04-26 13:42:34 +02:00
|
|
|
|
2016-11-13 18:52:34 +01:00
|
|
|
bool CGovernanceObject::ProcessVote(CNode* pfrom,
|
|
|
|
const CGovernanceVote& vote,
|
|
|
|
CGovernanceException& exception)
|
|
|
|
{
|
|
|
|
int nMNIndex = governance.GetMasternodeIndex(vote.GetVinMasternode());
|
|
|
|
if(nMNIndex < 0) {
|
2016-11-14 21:26:53 +01:00
|
|
|
std::ostringstream ostr;
|
2016-11-14 21:28:46 +01:00
|
|
|
ostr << "CGovernanceObject::ProcessVote -- Masternode index not found\n";
|
2016-11-14 21:26:53 +01:00
|
|
|
exception = CGovernanceException(ostr.str(), GOVERNANCE_EXCEPTION_WARNING);
|
2016-11-14 19:22:50 +01:00
|
|
|
if(mapOrphanVotes.Insert(vote.GetVinMasternode(), vote)) {
|
|
|
|
if(pfrom) {
|
|
|
|
mnodeman.AskForMN(pfrom, vote.GetVinMasternode());
|
|
|
|
}
|
|
|
|
LogPrintf(ostr.str().c_str());
|
2016-11-14 21:26:53 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
LogPrint("gobject", ostr.str().c_str());
|
2016-11-13 18:52:34 +01:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
vote_m_it it = mapCurrentMNVotes.find(nMNIndex);
|
|
|
|
if(it == mapCurrentMNVotes.end()) {
|
|
|
|
it = mapCurrentMNVotes.insert(vote_m_t::value_type(nMNIndex,vote_rec_t())).first;
|
|
|
|
}
|
|
|
|
vote_rec_t& recVote = it->second;
|
|
|
|
vote_signal_enum_t eSignal = vote.GetSignal();
|
|
|
|
if(eSignal == VOTE_SIGNAL_NONE) {
|
|
|
|
std::ostringstream ostr;
|
2016-11-14 21:28:46 +01:00
|
|
|
ostr << "CGovernanceObject::ProcessVote -- Vote signal: none" << "\n";
|
2016-11-13 18:52:34 +01:00
|
|
|
LogPrint("gobject", ostr.str().c_str());
|
|
|
|
exception = CGovernanceException(ostr.str(), GOVERNANCE_EXCEPTION_WARNING);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if(eSignal > MAX_SUPPORTED_VOTE_SIGNAL) {
|
|
|
|
std::ostringstream ostr;
|
2016-11-14 21:28:46 +01:00
|
|
|
ostr << "CGovernanceObject::ProcessVote -- Unsupported vote signal:" << CGovernanceVoting::ConvertSignalToString(vote.GetSignal()) << "\n";
|
2016-11-13 18:52:34 +01:00
|
|
|
LogPrintf(ostr.str().c_str());
|
|
|
|
exception = CGovernanceException(ostr.str(), GOVERNANCE_EXCEPTION_PERMANENT_ERROR, 20);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
vote_instance_m_it it2 = recVote.mapInstances.find(int(eSignal));
|
|
|
|
if(it2 == recVote.mapInstances.end()) {
|
|
|
|
it2 = recVote.mapInstances.insert(vote_instance_m_t::value_type(int(eSignal), vote_instance_t())).first;
|
|
|
|
}
|
|
|
|
vote_instance_t& voteInstance = it2->second;
|
|
|
|
int64_t nNow = GetTime();
|
2016-11-18 15:17:22 +01:00
|
|
|
if(governance.AreRateChecksEnabled()) {
|
|
|
|
int64_t nTimeDelta = nNow - voteInstance.nTime;
|
|
|
|
if(nTimeDelta < GOVERNANCE_UPDATE_MIN) {
|
|
|
|
std::ostringstream ostr;
|
|
|
|
ostr << "CGovernanceObject::ProcessVote -- Masternode voting too often "
|
|
|
|
<< ", MN outpoint = " << vote.GetVinMasternode().prevout.ToStringShort()
|
|
|
|
<< ", governance object hash = " << GetHash().ToString()
|
|
|
|
<< ", time delta = " << nTimeDelta << "\n";
|
|
|
|
LogPrint("gobject", ostr.str().c_str());
|
|
|
|
exception = CGovernanceException(ostr.str(), GOVERNANCE_EXCEPTION_TEMPORARY_ERROR);
|
|
|
|
return false;
|
|
|
|
}
|
2016-11-13 18:52:34 +01:00
|
|
|
}
|
|
|
|
// Finally check that the vote is actually valid (done last because of cost of signature verification)
|
|
|
|
if(!vote.IsValid(true)) {
|
|
|
|
std::ostringstream ostr;
|
2016-11-14 21:28:46 +01:00
|
|
|
ostr << "CGovernanceObject::ProcessVote -- Invalid vote "
|
2016-11-13 18:52:34 +01:00
|
|
|
<< ", MN outpoint = " << vote.GetVinMasternode().prevout.ToStringShort()
|
|
|
|
<< ", governance object hash = " << GetHash().ToString()
|
|
|
|
<< ", vote hash = " << vote.GetHash().ToString() << "\n";
|
|
|
|
LogPrintf(ostr.str().c_str());
|
|
|
|
exception = CGovernanceException(ostr.str(), GOVERNANCE_EXCEPTION_PERMANENT_ERROR, 20);
|
|
|
|
governance.AddInvalidVote(vote);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
voteInstance = vote_instance_t(vote.GetOutcome(), nNow);
|
|
|
|
fileVotes.AddVote(vote);
|
|
|
|
fDirtyCache = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CGovernanceObject::RebuildVoteMap()
|
|
|
|
{
|
|
|
|
vote_m_t mapMNVotesNew;
|
|
|
|
for(vote_m_it it = mapCurrentMNVotes.begin(); it != mapCurrentMNVotes.end(); ++it) {
|
|
|
|
CTxIn vinMasternode;
|
|
|
|
if(mnodeman.GetMasternodeVinForIndexOld(it->first, vinMasternode)) {
|
|
|
|
int nNewIndex = mnodeman.GetMasternodeIndex(vinMasternode);
|
|
|
|
if((nNewIndex >= 0)) {
|
|
|
|
mapMNVotesNew[nNewIndex] = it->second;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mapCurrentMNVotes = mapMNVotesNew;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CGovernanceObject::ClearMasternodeVotes()
|
|
|
|
{
|
|
|
|
vote_m_it it = mapCurrentMNVotes.begin();
|
|
|
|
while(it != mapCurrentMNVotes.end()) {
|
|
|
|
bool fIndexRebuilt = false;
|
|
|
|
CTxIn vinMasternode;
|
|
|
|
bool fRemove = true;
|
|
|
|
if(mnodeman.Get(it->first, vinMasternode, fIndexRebuilt)) {
|
|
|
|
if(mnodeman.Has(vinMasternode)) {
|
|
|
|
fRemove = false;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
fileVotes.RemoveVotesFromMasternode(vinMasternode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(fRemove) {
|
|
|
|
mapCurrentMNVotes.erase(it++);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-15 08:49:24 +02:00
|
|
|
void CGovernanceObject::SetMasternodeInfo(const CTxIn& vin)
|
2016-09-05 01:44:10 +02:00
|
|
|
{
|
|
|
|
vinMasternode = vin;
|
|
|
|
}
|
|
|
|
|
2016-10-22 18:52:14 +02:00
|
|
|
bool CGovernanceObject::Sign(CKey& keyMasternode, CPubKey& pubKeyMasternode)
|
2016-09-05 01:44:10 +02:00
|
|
|
{
|
|
|
|
LOCK(cs);
|
|
|
|
|
|
|
|
std::string strError;
|
|
|
|
uint256 nHash = GetHash();
|
|
|
|
std::string strMessage = nHash.ToString();
|
|
|
|
|
|
|
|
if(!darkSendSigner.SignMessage(strMessage, vchSig, keyMasternode)) {
|
|
|
|
LogPrintf("CGovernanceObject::Sign -- SignMessage() failed\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-10-22 18:52:14 +02:00
|
|
|
if(!darkSendSigner.VerifyMessage(pubKeyMasternode, vchSig, strMessage, strError)) {
|
2016-09-05 01:44:10 +02:00
|
|
|
LogPrintf("CGovernanceObject::Sign -- VerifyMessage() failed, error: %s\n", strError);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-10-22 18:52:14 +02:00
|
|
|
LogPrint("gobject", "CGovernanceObject::Sign -- pubkey id = %s, vin = %s\n",
|
|
|
|
pubKeyMasternode.GetID().ToString(), vinMasternode.prevout.ToStringShort());
|
2016-09-15 08:49:24 +02:00
|
|
|
|
|
|
|
|
2016-09-05 01:44:10 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-10-22 18:52:14 +02:00
|
|
|
bool CGovernanceObject::CheckSignature(CPubKey& pubKeyMasternode)
|
2016-09-05 01:44:10 +02:00
|
|
|
{
|
|
|
|
LOCK(cs);
|
|
|
|
std::string strError;
|
|
|
|
uint256 nHash = GetHash();
|
|
|
|
std::string strMessage = nHash.ToString();
|
|
|
|
|
2016-10-22 18:52:14 +02:00
|
|
|
if(!darkSendSigner.VerifyMessage(pubKeyMasternode, vchSig, strMessage, strError)) {
|
2016-09-05 01:44:10 +02:00
|
|
|
LogPrintf("CGovernance::CheckSignature -- VerifyMessage() failed, error: %s\n", strError);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-08-17 09:08:25 +02:00
|
|
|
int CGovernanceObject::GetObjectSubtype()
|
|
|
|
{
|
|
|
|
// todo - 12.1
|
|
|
|
// - detect subtype from strData json, obj["subtype"]
|
|
|
|
|
|
|
|
if(nObjectType == GOVERNANCE_OBJECT_TRIGGER) return TRIGGER_SUPERBLOCK;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint256 CGovernanceObject::GetHash()
|
|
|
|
{
|
|
|
|
// CREATE HASH OF ALL IMPORTANT PIECES OF DATA
|
|
|
|
|
|
|
|
CHashWriter ss(SER_GETHASH, PROTOCOL_VERSION);
|
|
|
|
ss << nHashParent;
|
|
|
|
ss << nRevision;
|
|
|
|
ss << nTime;
|
|
|
|
ss << strData;
|
|
|
|
// fee_tx is left out on purpose
|
|
|
|
uint256 h1 = ss.GetHash();
|
|
|
|
|
2016-09-17 21:37:48 +02:00
|
|
|
DBG( printf("CGovernanceObject::GetHash %i %li %s\n", nRevision, nTime, strData.c_str()); );
|
2016-08-17 09:08:25 +02:00
|
|
|
|
|
|
|
return h1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Return the actual object from the strData JSON structure.
|
|
|
|
|
|
|
|
Returns an empty object on error.
|
|
|
|
*/
|
|
|
|
UniValue CGovernanceObject::GetJSONObject()
|
|
|
|
{
|
|
|
|
UniValue obj(UniValue::VOBJ);
|
2016-09-12 09:40:00 +02:00
|
|
|
if(strData.empty()) {
|
2016-08-17 09:08:25 +02:00
|
|
|
return obj;
|
2016-04-09 21:57:53 +02:00
|
|
|
}
|
|
|
|
|
2016-08-17 09:08:25 +02:00
|
|
|
UniValue objResult(UniValue::VOBJ);
|
2016-09-08 13:40:19 +02:00
|
|
|
GetData(objResult);
|
|
|
|
|
|
|
|
std::vector<UniValue> arr1 = objResult.getValues();
|
|
|
|
std::vector<UniValue> arr2 = arr1.at( 0 ).getValues();
|
|
|
|
obj = arr2.at( 1 );
|
2016-04-09 21:57:53 +02:00
|
|
|
|
2016-08-17 09:08:25 +02:00
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* LoadData
|
|
|
|
* --------------------------------------------------------
|
2016-09-12 09:40:00 +02:00
|
|
|
*
|
|
|
|
* Attempt to load data from strData
|
|
|
|
*
|
2016-08-17 09:08:25 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
void CGovernanceObject::LoadData()
|
|
|
|
{
|
|
|
|
// todo : 12.1 - resolved
|
|
|
|
//return;
|
|
|
|
|
2016-09-12 09:40:00 +02:00
|
|
|
if(strData.empty()) {
|
2016-08-17 09:08:25 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-08-29 00:08:27 +02:00
|
|
|
try {
|
2016-09-08 13:40:19 +02:00
|
|
|
// ATTEMPT TO LOAD JSON STRING FROM STRDATA
|
|
|
|
UniValue objResult(UniValue::VOBJ);
|
|
|
|
GetData(objResult);
|
2016-09-12 09:40:00 +02:00
|
|
|
|
2016-09-08 13:40:19 +02:00
|
|
|
DBG( cout << "CGovernanceObject::LoadData strData = "
|
|
|
|
<< GetDataAsString()
|
|
|
|
<< endl; );
|
2016-09-12 09:40:00 +02:00
|
|
|
|
2016-08-17 09:08:25 +02:00
|
|
|
UniValue obj = GetJSONObject();
|
|
|
|
nObjectType = obj["type"].get_int();
|
|
|
|
}
|
2016-09-12 09:40:00 +02:00
|
|
|
catch(std::exception& e) {
|
2016-10-17 20:54:28 +02:00
|
|
|
fUnparsable = true;
|
2016-08-29 00:08:27 +02:00
|
|
|
std::ostringstream ostr;
|
|
|
|
ostr << "CGovernanceObject::LoadData Error parsing JSON"
|
|
|
|
<< ", e.what() = " << e.what();
|
|
|
|
DBG( cout << ostr.str() << endl; );
|
|
|
|
LogPrintf( ostr.str().c_str() );
|
|
|
|
return;
|
|
|
|
}
|
2016-09-12 09:40:00 +02:00
|
|
|
catch(...) {
|
2016-10-17 20:54:28 +02:00
|
|
|
fUnparsable = true;
|
2016-08-29 00:08:27 +02:00
|
|
|
std::ostringstream ostr;
|
|
|
|
ostr << "CGovernanceObject::LoadData Unknown Error parsing JSON";
|
|
|
|
DBG( cout << ostr.str() << endl; );
|
|
|
|
LogPrintf( ostr.str().c_str() );
|
2016-08-17 09:08:25 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* GetData - Example usage:
|
|
|
|
* --------------------------------------------------------
|
2016-09-12 09:40:00 +02:00
|
|
|
*
|
2016-08-17 09:08:25 +02:00
|
|
|
* Decode governance object data into UniValue(VOBJ)
|
2016-09-12 09:40:00 +02:00
|
|
|
*
|
2016-08-17 09:08:25 +02:00
|
|
|
*/
|
|
|
|
|
2016-09-08 13:40:19 +02:00
|
|
|
void CGovernanceObject::GetData(UniValue& objResult)
|
2016-08-17 09:08:25 +02:00
|
|
|
{
|
2016-09-08 13:40:19 +02:00
|
|
|
UniValue o(UniValue::VOBJ);
|
|
|
|
std::string s = GetDataAsString();
|
|
|
|
o.read(s);
|
|
|
|
objResult = o;
|
2016-08-17 09:08:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* GetData - As
|
|
|
|
* --------------------------------------------------------
|
2016-09-12 09:40:00 +02:00
|
|
|
*
|
2016-08-17 09:08:25 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
std::string CGovernanceObject::GetDataAsHex()
|
|
|
|
{
|
|
|
|
return strData;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string CGovernanceObject::GetDataAsString()
|
|
|
|
{
|
2016-09-12 09:40:00 +02:00
|
|
|
std::vector<unsigned char> v = ParseHex(strData);
|
|
|
|
std::string s(v.begin(), v.end());
|
2016-08-17 09:08:25 +02:00
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CGovernanceObject::UpdateLocalValidity(const CBlockIndex *pCurrentBlockIndex)
|
|
|
|
{
|
|
|
|
// THIS DOES NOT CHECK COLLATERAL, THIS IS CHECKED UPON ORIGINAL ARRIVAL
|
|
|
|
fCachedLocalValidity = IsValidLocally(pCurrentBlockIndex, strLocalValidityError, false);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
bool CGovernanceObject::IsValidLocally(const CBlockIndex* pindex, std::string& strError, bool fCheckCollateral)
|
|
|
|
{
|
2016-11-11 21:47:04 +01:00
|
|
|
bool fMissingMasternode = false;
|
|
|
|
|
|
|
|
return IsValidLocally(pindex, strError, fMissingMasternode, fCheckCollateral);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CGovernanceObject::IsValidLocally(const CBlockIndex* pindex, std::string& strError, bool& fMissingMasternode, bool fCheckCollateral)
|
|
|
|
{
|
|
|
|
fMissingMasternode = false;
|
2016-04-09 21:57:53 +02:00
|
|
|
if(!pindex) {
|
|
|
|
strError = "Tip is NULL";
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-10-17 20:54:28 +02:00
|
|
|
if(fUnparsable) {
|
2016-11-11 22:07:28 +01:00
|
|
|
strError = "Object data unparseable";
|
2016-10-17 20:54:28 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-09-29 22:29:29 +02:00
|
|
|
switch(nObjectType) {
|
|
|
|
case GOVERNANCE_OBJECT_PROPOSAL:
|
|
|
|
case GOVERNANCE_OBJECT_TRIGGER:
|
2016-10-17 20:54:28 +02:00
|
|
|
case GOVERNANCE_OBJECT_WATCHDOG:
|
2016-09-29 22:29:29 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
strError = strprintf("Invalid object type %d", nObjectType);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-08-17 09:08:25 +02:00
|
|
|
// IF ABSOLUTE NO COUNT (NO-YES VALID VOTES) IS MORE THAN 10% OF THE NETWORK MASTERNODES, OBJ IS INVALID
|
|
|
|
|
2016-09-28 22:03:54 +02:00
|
|
|
if(GetAbsoluteNoCount(VOTE_SIGNAL_VALID) > mnodeman.CountEnabled(MIN_GOVERNANCE_PEER_PROTO_VERSION)/10) {
|
2016-09-05 01:44:10 +02:00
|
|
|
strError = "Automated removal";
|
|
|
|
return false;
|
2016-08-17 09:08:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// CHECK COLLATERAL IF REQUIRED (HIGH CPU USAGE)
|
|
|
|
|
2016-10-17 20:54:28 +02:00
|
|
|
if(fCheckCollateral) {
|
|
|
|
if((nObjectType == GOVERNANCE_OBJECT_TRIGGER) || (nObjectType == GOVERNANCE_OBJECT_WATCHDOG)) {
|
2016-10-22 18:52:14 +02:00
|
|
|
std::string strOutpoint = vinMasternode.prevout.ToStringShort();
|
2016-10-17 20:54:28 +02:00
|
|
|
masternode_info_t infoMn = mnodeman.GetMasternodeInfo(vinMasternode);
|
|
|
|
if(!infoMn.fInfoValid) {
|
2016-11-11 21:47:04 +01:00
|
|
|
fMissingMasternode = true;
|
2016-10-22 18:52:14 +02:00
|
|
|
strError = "Masternode not found: " + strOutpoint;
|
2016-09-05 01:44:10 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that we have a valid MN signature
|
2016-10-17 20:54:28 +02:00
|
|
|
if(!CheckSignature(infoMn.pubKeyMasternode)) {
|
2016-10-22 18:52:14 +02:00
|
|
|
strError = "Invalid masternode signature for: " + strOutpoint + ", pubkey id = " + infoMn.pubKeyMasternode.GetID().ToString();
|
2016-09-05 01:44:10 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-09-15 08:49:24 +02:00
|
|
|
// Only perform rate check if we are synced because during syncing it is expected
|
|
|
|
// that objects will be seen in rapid succession
|
|
|
|
if(masternodeSync.IsSynced()) {
|
2016-10-17 20:54:28 +02:00
|
|
|
if(!governance.MasternodeRateCheck(vinMasternode, nObjectType)) {
|
2016-10-22 18:52:14 +02:00
|
|
|
strError = "Masternode attempting to create too many objects: " + strOutpoint;
|
2016-09-15 08:49:24 +02:00
|
|
|
return false;
|
|
|
|
}
|
2016-09-05 01:44:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!IsCollateralValid(strError)) {
|
2016-05-13 18:03:01 +02:00
|
|
|
// strError set in IsCollateralValid
|
2016-08-17 09:08:25 +02:00
|
|
|
if(strError == "") strError = "Collateral is invalid";
|
2016-04-09 21:57:53 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2016-08-17 09:08:25 +02:00
|
|
|
TODO
|
|
|
|
|
|
|
|
- There might be an issue with multisig in the coinbase on mainnet, we will add support for it in a future release.
|
|
|
|
- Post 12.2+ (test multisig coinbase transaction)
|
2016-04-09 21:57:53 +02:00
|
|
|
*/
|
|
|
|
|
2016-08-17 09:08:25 +02:00
|
|
|
// 12.1 - todo - compile error
|
|
|
|
// if(address.IsPayToScriptHash()) {
|
|
|
|
// strError = "Governance system - multisig is not currently supported";
|
2016-04-14 23:28:33 +02:00
|
|
|
// return false;
|
|
|
|
// }
|
2016-04-09 21:57:53 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-09-05 01:44:10 +02:00
|
|
|
CAmount CGovernanceObject::GetMinCollateralFee()
|
|
|
|
{
|
|
|
|
// Only 1 type has a fee for the moment but switch statement allows for future object types
|
2016-09-12 09:40:00 +02:00
|
|
|
switch(nObjectType) {
|
2016-09-29 22:29:29 +02:00
|
|
|
case GOVERNANCE_OBJECT_PROPOSAL: return GOVERNANCE_PROPOSAL_FEE_TX;
|
|
|
|
case GOVERNANCE_OBJECT_TRIGGER: return 0;
|
2016-10-17 20:54:28 +02:00
|
|
|
case GOVERNANCE_OBJECT_WATCHDOG: return 0;
|
2016-09-29 22:29:29 +02:00
|
|
|
default: return MAX_MONEY;
|
2016-09-05 01:44:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CGovernanceObject::IsCollateralValid(std::string& strError)
|
|
|
|
{
|
2016-09-29 22:29:29 +02:00
|
|
|
strError = "";
|
|
|
|
CAmount nMinFee = GetMinCollateralFee();
|
2016-09-05 01:44:10 +02:00
|
|
|
uint256 nExpectedHash = GetHash();
|
|
|
|
|
|
|
|
CTransaction txCollateral;
|
|
|
|
uint256 nBlockHash;
|
|
|
|
|
2016-09-12 09:40:00 +02:00
|
|
|
// RETRIEVE TRANSACTION IN QUESTION
|
2016-09-05 01:44:10 +02:00
|
|
|
|
|
|
|
if(!GetTransaction(nCollateralHash, txCollateral, Params().GetConsensus(), nBlockHash, true)){
|
|
|
|
strError = strprintf("Can't find collateral tx %s", txCollateral.ToString());
|
2016-10-22 18:52:14 +02:00
|
|
|
LogPrintf("CGovernanceObject::IsCollateralValid -- %s\n", strError);
|
2016-09-05 01:44:10 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(txCollateral.vout.size() < 1) {
|
|
|
|
strError = strprintf("tx vout size less than 1 | %d", txCollateral.vout.size());
|
2016-10-22 18:52:14 +02:00
|
|
|
LogPrintf("CGovernanceObject::IsCollateralValid -- %s\n", strError);
|
2016-09-05 01:44:10 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// LOOK FOR SPECIALIZED GOVERNANCE SCRIPT (PROOF OF BURN)
|
|
|
|
|
|
|
|
CScript findScript;
|
|
|
|
findScript << OP_RETURN << ToByteVector(nExpectedHash);
|
|
|
|
|
|
|
|
DBG( cout << "IsCollateralValid txCollateral.vout.size() = " << txCollateral.vout.size() << endl; );
|
|
|
|
|
|
|
|
DBG( cout << "IsCollateralValid: findScript = " << ScriptToAsmStr( findScript, false ) << endl; );
|
|
|
|
|
|
|
|
DBG( cout << "IsCollateralValid: nMinFee = " << nMinFee << endl; );
|
|
|
|
|
|
|
|
|
|
|
|
bool foundOpReturn = false;
|
|
|
|
BOOST_FOREACH(const CTxOut o, txCollateral.vout) {
|
2016-09-12 09:40:00 +02:00
|
|
|
DBG( cout << "IsCollateralValid txout : " << o.ToString()
|
2016-09-05 01:44:10 +02:00
|
|
|
<< ", o.nValue = " << o.nValue
|
|
|
|
<< ", o.scriptPubKey = " << ScriptToAsmStr( o.scriptPubKey, false )
|
|
|
|
<< endl; );
|
|
|
|
if(!o.scriptPubKey.IsNormalPaymentScript() && !o.scriptPubKey.IsUnspendable()){
|
|
|
|
strError = strprintf("Invalid Script %s", txCollateral.ToString());
|
2016-10-22 18:52:14 +02:00
|
|
|
LogPrintf ("CGovernanceObject::IsCollateralValid -- %s\n", strError);
|
2016-09-05 01:44:10 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if(o.scriptPubKey == findScript && o.nValue >= nMinFee) {
|
|
|
|
DBG( cout << "IsCollateralValid foundOpReturn = true" << endl; );
|
|
|
|
foundOpReturn = true;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
DBG( cout << "IsCollateralValid No match, continuing" << endl; );
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!foundOpReturn){
|
|
|
|
strError = strprintf("Couldn't find opReturn %s in %s", nExpectedHash.ToString(), txCollateral.ToString());
|
2016-10-22 18:52:14 +02:00
|
|
|
LogPrintf ("CGovernanceObject::IsCollateralValid -- %s\n", strError);
|
2016-09-05 01:44:10 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// GET CONFIRMATIONS FOR TRANSACTION
|
|
|
|
|
|
|
|
LOCK(cs_main);
|
|
|
|
int nConfirmationsIn = GetIXConfirmations(nCollateralHash);
|
|
|
|
if (nBlockHash != uint256()) {
|
|
|
|
BlockMap::iterator mi = mapBlockIndex.find(nBlockHash);
|
|
|
|
if (mi != mapBlockIndex.end() && (*mi).second) {
|
|
|
|
CBlockIndex* pindex = (*mi).second;
|
|
|
|
if (chainActive.Contains(pindex)) {
|
|
|
|
nConfirmationsIn += chainActive.Height() - pindex->nHeight + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(nConfirmationsIn >= GOVERNANCE_FEE_CONFIRMATIONS) {
|
|
|
|
strError = "valid";
|
|
|
|
} else {
|
|
|
|
strError = strprintf("Collateral requires at least %d confirmations - %d confirmations", GOVERNANCE_FEE_CONFIRMATIONS, nConfirmationsIn);
|
2016-10-22 18:52:14 +02:00
|
|
|
LogPrintf ("CGovernanceObject::IsCollateralValid -- %s - %d confirmations\n", strError, nConfirmationsIn);
|
2016-09-05 01:44:10 +02:00
|
|
|
return false;
|
|
|
|
}
|
2016-09-12 09:40:00 +02:00
|
|
|
|
2016-09-05 01:44:10 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-11-13 18:52:34 +01:00
|
|
|
int CGovernanceObject::CountMatchingVotes(vote_signal_enum_t eVoteSignalIn, vote_outcome_enum_t eVoteOutcomeIn) const
|
|
|
|
{
|
|
|
|
int nCount = 0;
|
|
|
|
for(vote_m_cit it = mapCurrentMNVotes.begin(); it != mapCurrentMNVotes.end(); ++it) {
|
|
|
|
const vote_rec_t& recVote = it->second;
|
|
|
|
vote_instance_m_cit it2 = recVote.mapInstances.find(eVoteSignalIn);
|
|
|
|
if(it2 == recVote.mapInstances.end()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
const vote_instance_t& voteInstance = it2->second;
|
|
|
|
if(voteInstance.eOutcome == eVoteOutcomeIn) {
|
|
|
|
++nCount;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nCount;
|
|
|
|
}
|
|
|
|
|
2016-04-26 06:08:36 +02:00
|
|
|
/**
|
|
|
|
* Get specific vote counts for each outcome (funding, validity, etc)
|
|
|
|
*/
|
|
|
|
|
2016-11-13 18:52:34 +01:00
|
|
|
int CGovernanceObject::GetAbsoluteYesCount(vote_signal_enum_t eVoteSignalIn) const
|
2016-04-09 21:57:53 +02:00
|
|
|
{
|
2016-09-16 17:30:33 +02:00
|
|
|
return GetYesCount(eVoteSignalIn) - GetNoCount(eVoteSignalIn);
|
2016-04-09 21:57:53 +02:00
|
|
|
}
|
|
|
|
|
2016-11-13 18:52:34 +01:00
|
|
|
int CGovernanceObject::GetAbsoluteNoCount(vote_signal_enum_t eVoteSignalIn) const
|
2016-06-08 08:57:16 +02:00
|
|
|
{
|
2016-09-16 17:30:33 +02:00
|
|
|
return GetNoCount(eVoteSignalIn) - GetYesCount(eVoteSignalIn);
|
2016-06-08 08:57:16 +02:00
|
|
|
}
|
|
|
|
|
2016-11-13 18:52:34 +01:00
|
|
|
int CGovernanceObject::GetYesCount(vote_signal_enum_t eVoteSignalIn) const
|
2016-04-09 21:57:53 +02:00
|
|
|
{
|
2016-11-13 18:52:34 +01:00
|
|
|
return CountMatchingVotes(eVoteSignalIn, VOTE_OUTCOME_YES);
|
2016-04-09 21:57:53 +02:00
|
|
|
}
|
|
|
|
|
2016-11-13 18:52:34 +01:00
|
|
|
int CGovernanceObject::GetNoCount(vote_signal_enum_t eVoteSignalIn) const
|
2016-04-09 21:57:53 +02:00
|
|
|
{
|
2016-11-13 18:52:34 +01:00
|
|
|
return CountMatchingVotes(eVoteSignalIn, VOTE_OUTCOME_NO);
|
2016-04-09 21:57:53 +02:00
|
|
|
}
|
|
|
|
|
2016-11-13 18:52:34 +01:00
|
|
|
int CGovernanceObject::GetAbstainCount(vote_signal_enum_t eVoteSignalIn) const
|
2016-04-09 21:57:53 +02:00
|
|
|
{
|
2016-11-13 18:52:34 +01:00
|
|
|
return CountMatchingVotes(eVoteSignalIn, VOTE_OUTCOME_ABSTAIN);
|
2016-04-09 21:57:53 +02:00
|
|
|
}
|
|
|
|
|
2016-11-22 20:26:36 +01:00
|
|
|
bool CGovernanceObject::GetCurrentMNVotes(const CTxIn& mnCollateralOutpoint, vote_rec_t& voteRecord)
|
|
|
|
{
|
|
|
|
int nMNIndex = governance.GetMasternodeIndex(mnCollateralOutpoint);
|
|
|
|
vote_m_it it = mapCurrentMNVotes.find(nMNIndex);
|
|
|
|
if (it == mapCurrentMNVotes.end()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
voteRecord = it->second;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-04-16 19:19:17 +02:00
|
|
|
void CGovernanceObject::Relay()
|
2016-04-09 21:57:53 +02:00
|
|
|
{
|
2016-06-08 08:57:16 +02:00
|
|
|
CInv inv(MSG_GOVERNANCE_OBJECT, GetHash());
|
2016-09-28 22:03:54 +02:00
|
|
|
RelayInv(inv, PROTOCOL_VERSION);
|
2016-04-09 21:57:53 +02:00
|
|
|
}
|
|
|
|
|
2016-04-09 22:55:52 +02:00
|
|
|
std::string CGovernanceManager::ToString() const
|
2016-04-09 21:57:53 +02:00
|
|
|
{
|
|
|
|
std::ostringstream info;
|
|
|
|
|
2016-05-19 23:08:57 +02:00
|
|
|
info << "Governance Objects: " << (int)mapObjects.size() <<
|
2016-11-13 18:52:34 +01:00
|
|
|
", Seen Budgets : " << (int)mapSeenGovernanceObjects.size() <<
|
|
|
|
", Vote Count : " << (int)mapVoteToObject.GetSize();
|
2016-04-09 21:57:53 +02:00
|
|
|
|
|
|
|
return info.str();
|
|
|
|
}
|
|
|
|
|
2016-04-09 22:55:52 +02:00
|
|
|
void CGovernanceManager::UpdatedBlockTip(const CBlockIndex *pindex)
|
2016-04-09 21:57:53 +02:00
|
|
|
{
|
2016-08-17 09:08:25 +02:00
|
|
|
// Note this gets called from ActivateBestChain without cs_main being held
|
|
|
|
// so it should be safe to lock our mutex here without risking a deadlock
|
2016-09-12 09:40:00 +02:00
|
|
|
// On the other hand it should be safe for us to access pindex without holding a lock
|
2016-08-17 09:08:25 +02:00
|
|
|
// on cs_main because the CBlockIndex objects are dynamically allocated and
|
|
|
|
// presumably never deleted.
|
2016-11-12 12:14:50 +01:00
|
|
|
if(!pindex) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-08-17 09:08:25 +02:00
|
|
|
LOCK(cs);
|
2016-04-09 21:57:53 +02:00
|
|
|
pCurrentBlockIndex = pindex;
|
2016-08-17 09:08:25 +02:00
|
|
|
nCachedBlockHeight = pCurrentBlockIndex->nHeight;
|
2016-11-05 17:13:30 +01:00
|
|
|
LogPrint("gobject", "CGovernanceManager::UpdatedBlockTip pCurrentBlockIndex->nHeight: %d\n", pCurrentBlockIndex->nHeight);
|
2016-08-17 09:08:25 +02:00
|
|
|
|
|
|
|
// TO REPROCESS OBJECTS WE SHOULD BE SYNCED
|
2016-04-09 21:57:53 +02:00
|
|
|
|
2016-08-29 21:11:34 +02:00
|
|
|
if(!fLiteMode && masternodeSync.IsSynced())
|
2016-04-09 21:57:53 +02:00
|
|
|
NewBlock();
|
|
|
|
}
|
2016-04-14 20:07:59 +02:00
|
|
|
|
2016-08-17 09:08:25 +02:00
|
|
|
void CGovernanceObject::UpdateSentinelVariables(const CBlockIndex *pCurrentBlockIndex)
|
|
|
|
{
|
|
|
|
// CALCULATE MINIMUM SUPPORT LEVELS REQUIRED
|
|
|
|
|
|
|
|
int nMnCount = mnodeman.CountEnabled();
|
|
|
|
if(nMnCount == 0) return;
|
|
|
|
|
|
|
|
// CALCULATE THE MINUMUM VOTE COUNT REQUIRED FOR FULL SIGNAL
|
|
|
|
|
|
|
|
// todo - 12.1 - should be set to `10` after governance vote compression is implemented
|
2016-09-12 09:40:00 +02:00
|
|
|
int nAbsVoteReq = std::max(Params().GetConsensus().nGovernanceMinQuorum, nMnCount / 10);
|
2016-08-17 09:08:25 +02:00
|
|
|
// todo - 12.1 - Temporarily set to 1 for testing - reverted
|
|
|
|
//nAbsVoteReq = 1;
|
|
|
|
|
|
|
|
// SET SENTINEL FLAGS TO FALSE
|
|
|
|
|
|
|
|
fCachedFunding = false;
|
|
|
|
fCachedValid = true; //default to valid
|
|
|
|
fCachedDelete = false;
|
|
|
|
fCachedEndorsed = false;
|
|
|
|
fDirtyCache = false;
|
2016-09-12 09:40:00 +02:00
|
|
|
|
2016-08-17 09:08:25 +02:00
|
|
|
// SET SENTINEL FLAGS TO TRUE IF MIMIMUM SUPPORT LEVELS ARE REACHED
|
|
|
|
// ARE ANY OF THESE FLAGS CURRENTLY ACTIVATED?
|
|
|
|
|
|
|
|
if(GetAbsoluteYesCount(VOTE_SIGNAL_FUNDING) >= nAbsVoteReq) fCachedFunding = true;
|
|
|
|
if(GetAbsoluteYesCount(VOTE_SIGNAL_VALID) >= nAbsVoteReq) fCachedValid = true;
|
2016-11-18 15:17:00 +01:00
|
|
|
if(GetAbsoluteYesCount(VOTE_SIGNAL_DELETE) >= nAbsVoteReq) {
|
|
|
|
fCachedDelete = true;
|
|
|
|
nDeletionTime = GetAdjustedTime();
|
|
|
|
}
|
2016-08-17 09:08:25 +02:00
|
|
|
if(GetAbsoluteYesCount(VOTE_SIGNAL_ENDORSED) >= nAbsVoteReq) fCachedEndorsed = true;
|
|
|
|
|
|
|
|
// ARE ANY OF THE VOTING FLAGS NEGATIVELY SET BY THE NETWORK?
|
|
|
|
// THIS CAN BE CACHED, THE VOTES BEING HOT-LOADED AS NEEDED TO RECALCULATE
|
|
|
|
|
|
|
|
if(GetAbsoluteNoCount(VOTE_SIGNAL_FUNDING) >= nAbsVoteReq) fCachedFunding = false;
|
|
|
|
if(GetAbsoluteNoCount(VOTE_SIGNAL_VALID) >= nAbsVoteReq) fCachedValid = false;
|
|
|
|
if(GetAbsoluteNoCount(VOTE_SIGNAL_DELETE) >= nAbsVoteReq) fCachedDelete = false;
|
|
|
|
if(GetAbsoluteNoCount(VOTE_SIGNAL_ENDORSED) >= nAbsVoteReq) fCachedEndorsed = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CGovernanceObject::swap(CGovernanceObject& first, CGovernanceObject& second) // nothrow
|
|
|
|
{
|
|
|
|
// enable ADL (not necessary in our case, but good practice)
|
|
|
|
using std::swap;
|
|
|
|
|
|
|
|
// by swapping the members of two classes,
|
|
|
|
// the two classes are effectively swapped
|
|
|
|
swap(first.nHashParent, second.nHashParent);
|
|
|
|
swap(first.nRevision, second.nRevision);
|
|
|
|
swap(first.nTime, second.nTime);
|
2016-11-18 15:17:00 +01:00
|
|
|
swap(first.nDeletionTime, second.nDeletionTime);
|
2016-08-17 09:08:25 +02:00
|
|
|
swap(first.nCollateralHash, second.nCollateralHash);
|
|
|
|
swap(first.strData, second.strData);
|
|
|
|
swap(first.nObjectType, second.nObjectType);
|
|
|
|
|
|
|
|
// swap all cached valid flags
|
|
|
|
swap(first.fCachedFunding, second.fCachedFunding);
|
|
|
|
swap(first.fCachedValid, second.fCachedValid);
|
|
|
|
swap(first.fCachedDelete, second.fCachedDelete);
|
|
|
|
swap(first.fCachedEndorsed, second.fCachedEndorsed);
|
|
|
|
swap(first.fDirtyCache, second.fDirtyCache);
|
|
|
|
swap(first.fExpired, second.fExpired);
|
|
|
|
}
|
2016-11-13 18:52:34 +01:00
|
|
|
|
|
|
|
void CGovernanceObject::CheckOrphanVotes()
|
|
|
|
{
|
|
|
|
const vote_mcache_t::list_t& listVotes = mapOrphanVotes.GetItemList();
|
|
|
|
for(vote_mcache_t::list_cit it = listVotes.begin(); it != listVotes.end(); ++it) {
|
|
|
|
const CGovernanceVote& vote = it->value;
|
|
|
|
if(!mnodeman.Has(vote.GetVinMasternode())) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
CGovernanceException exception;
|
|
|
|
if(!ProcessVote(NULL, vote, exception)) {
|
|
|
|
LogPrintf("CGovernanceObject::CheckOrphanVotes -- Failed to add orphan vote: %s\n", exception.what());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|