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"
|
|
|
|
#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-14 20:53:46 +02:00
|
|
|
class CNode;
|
2016-08-17 09:08:25 +02:00
|
|
|
class CBudgetVote;
|
2016-04-14 20:53:46 +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-09-05 01:44:10 +02:00
|
|
|
CGovernanceManager::CGovernanceManager()
|
|
|
|
: mapCollateral(),
|
|
|
|
pCurrentBlockIndex(NULL),
|
|
|
|
nTimeLastDiff(0),
|
|
|
|
nCachedBlockHeight(0),
|
|
|
|
mapObjects(),
|
|
|
|
mapSeenGovernanceObjects(),
|
|
|
|
mapSeenVotes(),
|
|
|
|
mapOrphanVotes(),
|
|
|
|
mapLastMasternodeTrigger(),
|
|
|
|
cs()
|
|
|
|
{}
|
2016-04-09 21:57:53 +02:00
|
|
|
|
2016-08-17 09:08:25 +02:00
|
|
|
// Accessors for thread-safe access to maps
|
|
|
|
bool CGovernanceManager::HaveObjectForHash(uint256 nHash) {
|
|
|
|
LOCK(cs);
|
|
|
|
return (mapObjects.count(nHash) == 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CGovernanceManager::HaveVoteForHash(uint256 nHash) {
|
|
|
|
LOCK(cs);
|
|
|
|
return (mapVotesByHash.count(nHash) == 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// int CGovernanceManager::GetVoteCountByHash(uint256 nHash) {
|
|
|
|
// int count = 0;
|
|
|
|
// LOCK(cs);
|
|
|
|
// count_m_cit it = mapVotesByHash.find(nHash);
|
|
|
|
// if (it != mapVotesByHash.end()) {
|
|
|
|
// count = it->second;
|
|
|
|
// }
|
|
|
|
// return count;
|
|
|
|
// }
|
|
|
|
|
|
|
|
bool CGovernanceManager::SerializeObjectForHash(uint256 nHash, CDataStream& ss) {
|
|
|
|
LOCK(cs);
|
|
|
|
object_m_it it = mapObjects.find(nHash);
|
|
|
|
if (it == mapObjects.end()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
ss << it->second;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CGovernanceManager::SerializeVoteForHash(uint256 nHash, CDataStream& ss) {
|
|
|
|
LOCK(cs);
|
|
|
|
vote_m_it it = mapVotesByHash.find(nHash);
|
|
|
|
if (it == mapVotesByHash.end()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
ss << it->second;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CGovernanceManager::AddSeenGovernanceObject(uint256 nHash, int status)
|
|
|
|
{
|
|
|
|
LOCK(cs);
|
|
|
|
mapSeenGovernanceObjects[nHash] = status;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CGovernanceManager::AddSeenVote(uint256 nHash, int status)
|
|
|
|
{
|
|
|
|
LOCK(cs);
|
|
|
|
mapSeenVotes[nHash] = status;
|
|
|
|
}
|
|
|
|
|
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-08-05 18:25:03 +02:00
|
|
|
//
|
|
|
|
// REMOVE AFTER MIGRATION TO 12.1
|
|
|
|
//
|
|
|
|
if(pfrom->nVersion < 70201) return;
|
|
|
|
//
|
|
|
|
// END REMOVE
|
|
|
|
//
|
|
|
|
|
2016-09-08 13:40:19 +02:00
|
|
|
LOCK(governance.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-08-05 18:25:03 +02:00
|
|
|
// ignore such request until we are fully synced
|
|
|
|
if (!masternodeSync.IsSynced()) return;
|
|
|
|
|
2016-06-08 08:57:16 +02:00
|
|
|
uint256 nProp;
|
|
|
|
vRecv >> nProp;
|
|
|
|
|
2016-08-17 09:08:25 +02:00
|
|
|
// IF THE NETWORK IS MAIN, MAKE SURE THEY HAVEN'T ASKED US BEFORE
|
|
|
|
|
2016-06-08 08:57:16 +02:00
|
|
|
if(Params().NetworkIDString() == CBaseChainParams::MAIN){
|
|
|
|
if(nProp == uint256()) {
|
|
|
|
if(pfrom->HasFulfilledRequest(NetMsgType::MNGOVERNANCESYNC)) {
|
2016-08-17 09:08:25 +02:00
|
|
|
LogPrint("gobject", "peer already asked me for the list\n");
|
|
|
|
// BAD PEER! BAD!
|
2016-06-08 08:57:16 +02:00
|
|
|
Misbehaving(pfrom->GetId(), 20);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
pfrom->FulfilledRequest(NetMsgType::MNGOVERNANCESYNC);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-17 09:08:25 +02:00
|
|
|
// ask for a specific govobj and it's votes
|
2016-06-08 08:57:16 +02:00
|
|
|
Sync(pfrom, nProp);
|
2016-08-17 09:08:25 +02:00
|
|
|
LogPrint("gobject", "syncing governance objects to our peer at %s\n", pfrom->addr.ToString());
|
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-08-17 09:08:25 +02:00
|
|
|
LOCK(cs);
|
2016-06-10 07:16:32 +02:00
|
|
|
// MAKE SURE WE HAVE A VALID REFERENCE TO THE TIP BEFORE CONTINUING
|
|
|
|
|
|
|
|
if(!pCurrentBlockIndex) return;
|
2016-06-08 08:57:16 +02:00
|
|
|
|
|
|
|
CGovernanceObject govobj;
|
|
|
|
vRecv >> govobj;
|
|
|
|
|
|
|
|
if(mapSeenGovernanceObjects.count(govobj.GetHash())){
|
|
|
|
// TODO - print error code? what if it's GOVOBJ_ERROR_IMMATURE?
|
|
|
|
masternodeSync.AddedBudgetItem(govobj.GetHash());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-08-17 09:08:25 +02:00
|
|
|
// IS THE COLLATERAL TRANSACTION ASSOCIATED WITH THIS GOVERNANCE OBJECT MATURE/VALID?
|
|
|
|
|
2016-06-08 08:57:16 +02:00
|
|
|
std::string strError = "";
|
2016-09-05 01:44:10 +02:00
|
|
|
if(!govobj.IsCollateralValid(strError)) {
|
2016-08-17 09:08:25 +02:00
|
|
|
LogPrintf("Governance object collateral tx is not valid - %s - %s\n", govobj.nCollateralHash.ToString(), strError);
|
2016-06-08 08:57:16 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-08-17 09:08:25 +02:00
|
|
|
// CHECK OBJECT AGAINST LOCAL BLOCKCHAIN
|
|
|
|
|
|
|
|
if(!govobj.IsValidLocally(pCurrentBlockIndex, strError, true)) {
|
2016-06-08 08:57:16 +02:00
|
|
|
mapSeenGovernanceObjects.insert(make_pair(govobj.GetHash(), SEEN_OBJECT_ERROR_INVALID));
|
|
|
|
LogPrintf("Governance object is invalid - %s\n", strError);
|
|
|
|
return;
|
|
|
|
}
|
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-08-17 09:08:25 +02:00
|
|
|
{
|
|
|
|
govobj.UpdateSentinelVariables(pCurrentBlockIndex); //this sets local vars in object
|
|
|
|
|
|
|
|
if(AddGovernanceObject(govobj))
|
|
|
|
{
|
|
|
|
govobj.Relay();
|
|
|
|
}
|
|
|
|
}
|
2016-06-08 08:57:16 +02:00
|
|
|
|
2016-08-17 09:08:25 +02:00
|
|
|
// UPDATE THAT WE'VE SEEN THIS OBJECT
|
2016-06-08 08:57:16 +02:00
|
|
|
mapSeenGovernanceObjects.insert(make_pair(govobj.GetHash(), SEEN_OBJECT_IS_VALID));
|
|
|
|
masternodeSync.AddedBudgetItem(govobj.GetHash());
|
|
|
|
|
2016-08-05 18:25:03 +02:00
|
|
|
LogPrintf("MNGOVERNANCEOBJECT -- %s new\n", govobj.GetHash().ToString());
|
2016-08-17 09:08:25 +02:00
|
|
|
|
|
|
|
// WE MIGHT HAVE PENDING/ORPHAN VOTES FOR THIS OBJECT
|
|
|
|
|
2016-06-08 08:57:16 +02:00
|
|
|
CheckOrphanVotes();
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
if(!masternodeSync.IsMasternodeListSynced()) return;
|
|
|
|
|
2016-06-08 08:57:16 +02:00
|
|
|
CGovernanceVote vote;
|
|
|
|
vRecv >> vote;
|
2016-09-08 13:40:19 +02:00
|
|
|
//vote.fValid = true;
|
2016-06-08 08:57:16 +02:00
|
|
|
|
2016-08-17 09:08:25 +02:00
|
|
|
// IF WE'VE SEEN THIS OBJECT THEN SKIP
|
|
|
|
|
2016-06-08 08:57:16 +02:00
|
|
|
if(mapSeenVotes.count(vote.GetHash())){
|
|
|
|
masternodeSync.AddedBudgetItem(vote.GetHash());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-08-17 09:08:25 +02:00
|
|
|
// FIND THE MASTERNODE OF THE VOTER
|
|
|
|
|
2016-09-08 13:40:19 +02:00
|
|
|
CMasternode* pmn = mnodeman.Find(vote.GetVinMasternode());
|
2016-06-08 08:57:16 +02:00
|
|
|
if(pmn == NULL) {
|
2016-09-08 13:40:19 +02:00
|
|
|
LogPrint("gobject", "gobject - unknown masternode - vin: %s\n", vote.GetVinMasternode().ToString());
|
|
|
|
mnodeman.AskForMN(pfrom, vote.GetVinMasternode());
|
2016-06-08 08:57:16 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-08-17 09:08:25 +02:00
|
|
|
// CHECK LOCAL VALIDITY AGAINST BLOCKCHAIN, TIME DATA, ETC
|
|
|
|
|
2016-06-08 08:57:16 +02:00
|
|
|
if(!vote.IsValid(true)){
|
2016-08-17 09:08:25 +02:00
|
|
|
LogPrintf("gobject - signature invalid\n");
|
2016-06-08 08:57:16 +02:00
|
|
|
if(masternodeSync.IsSynced()) Misbehaving(pfrom->GetId(), 20);
|
|
|
|
// it could just be a non-synced masternode
|
2016-09-08 13:40:19 +02:00
|
|
|
mnodeman.AskForMN(pfrom, vote.GetVinMasternode());
|
2016-06-08 08:57:16 +02:00
|
|
|
mapSeenVotes.insert(make_pair(vote.GetHash(), SEEN_OBJECT_ERROR_INVALID));
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
mapSeenVotes.insert(make_pair(vote.GetHash(), SEEN_OBJECT_IS_VALID));
|
|
|
|
}
|
|
|
|
|
2016-08-17 09:08:25 +02:00
|
|
|
// IF EVERYTHING CHECKS OUT, UPDATE THE GOVERNANCE MANAGER
|
|
|
|
|
2016-06-08 08:57:16 +02:00
|
|
|
std::string strError = "";
|
2016-08-17 09:08:25 +02:00
|
|
|
if(AddOrUpdateVote(vote, pfrom, strError)) {
|
2016-06-08 08:57:16 +02:00
|
|
|
vote.Relay();
|
|
|
|
masternodeSync.AddedBudgetItem(vote.GetHash());
|
2016-08-17 09:08:25 +02:00
|
|
|
pmn->AddGovernanceVote(vote.GetParentHash());
|
2016-06-08 08:57:16 +02:00
|
|
|
}
|
|
|
|
|
2016-08-17 09:08:25 +02:00
|
|
|
LogPrint("gobject", "NEW governance vote: %s\n", vote.GetHash().ToString());
|
2016-06-08 08:57:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-04-09 22:55:52 +02:00
|
|
|
void CGovernanceManager::CheckOrphanVotes()
|
2016-04-09 21:57:53 +02:00
|
|
|
{
|
|
|
|
LOCK(cs);
|
|
|
|
|
|
|
|
std::string strError = "";
|
2016-05-23 19:58:39 +02:00
|
|
|
std::map<uint256, CGovernanceVote>::iterator it1 = mapOrphanVotes.begin();
|
|
|
|
while(it1 != mapOrphanVotes.end()){
|
2016-08-17 09:08:25 +02:00
|
|
|
if(AddOrUpdateVote(((*it1).second), NULL, strError)){
|
2016-05-28 12:31:44 +02:00
|
|
|
LogPrintf("CGovernanceManager::CheckOrphanVotes - Governance object is known, activating and removing orphan vote\n");
|
2016-05-23 19:58:39 +02:00
|
|
|
mapOrphanVotes.erase(it1++);
|
2016-04-09 21:57:53 +02:00
|
|
|
} else {
|
|
|
|
++it1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-05-28 12:31:44 +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-05-23 19:58:39 +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
|
|
|
|
{
|
|
|
|
mapObjects.insert(make_pair(govobj.GetHash(), govobj));
|
|
|
|
}
|
|
|
|
|
|
|
|
// SHOULD WE ADD THIS OBJECT TO ANY OTHER MANANGERS?
|
|
|
|
|
|
|
|
DBG( cout << "CGovernanceManager::AddGovernanceObject Before trigger block, strData = "
|
|
|
|
<< govobj.GetDataAsString()
|
|
|
|
<< ", nObjectType = " << govobj.nObjectType
|
|
|
|
<< endl; );
|
|
|
|
|
2016-09-05 01:44:10 +02:00
|
|
|
if(govobj.nObjectType == GOVERNANCE_OBJECT_TRIGGER) {
|
|
|
|
mapLastMasternodeTrigger[govobj.pubkeyMasternode.GetHash()] = 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; );
|
|
|
|
}
|
|
|
|
|
|
|
|
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-08-17 09:08:25 +02:00
|
|
|
LogPrintf("CGovernanceManager::UpdateCachesAndClean \n");
|
|
|
|
|
|
|
|
LOCK(cs);
|
2016-04-09 21:57:53 +02:00
|
|
|
|
2016-06-10 07:16:32 +02:00
|
|
|
// DOUBLE CHECK THAT WE HAVE A VALID POINTER TO TIP
|
|
|
|
|
|
|
|
if(!pCurrentBlockIndex) return;
|
|
|
|
|
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
|
|
|
|
|
|
|
std::map<uint256, CGovernanceObject>::iterator it = mapObjects.begin();
|
2016-08-17 09:08:25 +02:00
|
|
|
|
|
|
|
std::map<uint256, int> mapDirtyObjects;
|
|
|
|
|
|
|
|
// Clean up any expired or invalid triggers
|
|
|
|
triggerman.CleanAndRemove();
|
|
|
|
|
2016-05-24 20:11:59 +02:00
|
|
|
while(it != mapObjects.end())
|
2016-06-10 07:16:32 +02:00
|
|
|
{
|
2016-05-24 20:11:59 +02:00
|
|
|
CGovernanceObject* pObj = &((*it).second);
|
|
|
|
|
2016-08-17 09:08:25 +02:00
|
|
|
if(!pObj) {
|
|
|
|
++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?
|
|
|
|
if(pObj->fDirtyCache) {
|
|
|
|
mapDirtyObjects.insert(make_pair((*it).first, 1));
|
|
|
|
|
|
|
|
// UPDATE LOCAL VALIDITY AGAINST CRYPTO DATA
|
|
|
|
|
|
|
|
pObj->UpdateLocalValidity(pCurrentBlockIndex);
|
|
|
|
|
|
|
|
// 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-08-17 09:08:25 +02:00
|
|
|
if(pObj->fCachedDelete || pObj->fExpired)
|
|
|
|
{
|
|
|
|
LogPrintf("UpdateCachesAndClean --- erase obj %s\n", (*it).first.ToString());
|
|
|
|
mapObjects.erase(it++);
|
|
|
|
} else {
|
|
|
|
++it;
|
|
|
|
}
|
2016-05-23 23:39:10 +02:00
|
|
|
}
|
2016-06-10 07:16:32 +02:00
|
|
|
|
2016-08-17 09:08:25 +02:00
|
|
|
// CHECK EACH GOVERNANCE OBJECTS VALIDITY (CPU HEAVY)
|
|
|
|
|
|
|
|
// 12.1 todo - compile issues
|
|
|
|
|
|
|
|
// std::map<uint256, CBudgetVote>::iterator it = mapVotesByHash.begin();
|
|
|
|
// while(it != mapVotes.end()) {
|
|
|
|
|
|
|
|
// // ONLY UPDATE THE DIRTY OBJECTS!
|
|
|
|
|
|
|
|
// if(mapDirtyObjects.count((*it).first))
|
|
|
|
// {
|
|
|
|
// (*it).second.fValid = (*it).second.IsValid(true);
|
|
|
|
// ++it;
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
2016-04-09 21:57:53 +02:00
|
|
|
}
|
|
|
|
|
2016-05-24 20:11:59 +02:00
|
|
|
CGovernanceObject *CGovernanceManager::FindGovernanceObject(const std::string &strName)
|
2016-04-09 21:57:53 +02:00
|
|
|
{
|
2016-06-08 08:57:16 +02:00
|
|
|
// find the prop with the highest yes count
|
2016-04-09 21:57:53 +02:00
|
|
|
|
|
|
|
int nYesCount = -99999;
|
2016-05-28 12:31:44 +02:00
|
|
|
CGovernanceObject* pGovObj = NULL;
|
2016-04-09 21:57:53 +02:00
|
|
|
|
2016-05-13 18:03:01 +02:00
|
|
|
std::map<uint256, CGovernanceObject>::iterator it = mapObjects.begin();
|
|
|
|
while(it != mapObjects.end()){
|
2016-06-08 08:57:16 +02:00
|
|
|
int nGovObjYesCount = pGovObj->GetYesCount(VOTE_SIGNAL_FUNDING);
|
|
|
|
if((*it).second.strName == strName && nGovObjYesCount > nYesCount){
|
|
|
|
nYesCount = nGovObjYesCount;
|
2016-08-17 09:08:25 +02:00
|
|
|
pGovObj = &((*it).second);
|
2016-04-09 21:57:53 +02:00
|
|
|
}
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(nYesCount == -99999) return NULL;
|
|
|
|
|
2016-05-28 12:31:44 +02:00
|
|
|
return pGovObj;
|
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-08-17 09:08:25 +02:00
|
|
|
std::vector<CGovernanceVote*> CGovernanceManager::GetMatchingVotes(const uint256& nParentHash)
|
|
|
|
{
|
|
|
|
std::vector<CGovernanceVote*> vecResult;
|
|
|
|
|
|
|
|
// LOOP THROUGH ALL VOTES AND FIND THOSE MATCHING USER HASH
|
|
|
|
|
|
|
|
std::map<uint256, CGovernanceVote>::iterator it2 = mapVotesByHash.begin();
|
|
|
|
while(it2 != mapVotesByHash.end()){
|
|
|
|
if((*it2).second.GetParentHash() == nParentHash)
|
|
|
|
{
|
|
|
|
vecResult.push_back(&(*it2).second);
|
|
|
|
}
|
|
|
|
*it2++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return vecResult;
|
|
|
|
}
|
|
|
|
|
|
|
|
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-05-13 18:03:01 +02:00
|
|
|
std::map<uint256, CGovernanceObject>::iterator it = mapObjects.begin();
|
|
|
|
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
|
|
|
|
|
|
|
|
if((*it).second.nTime < nMoreThanTime) {
|
|
|
|
++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-04-09 21:57:53 +02:00
|
|
|
if( left.second != right.second)
|
|
|
|
return (left.second > right.second);
|
2016-08-17 09:08:25 +02:00
|
|
|
return (UintToArith256(left.first->nCollateralHash) > UintToArith256(right.first->nCollateralHash));
|
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();
|
|
|
|
while(it != mapAskedForGovernanceObject.end()){
|
2016-04-09 21:57:53 +02:00
|
|
|
if((*it).second > GetTime() - (60*60*24)){
|
|
|
|
++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-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
|
|
|
{
|
|
|
|
LOCK(cs);
|
|
|
|
std::map<uint256, CGovernanceObject>::iterator it1 = mapObjects.begin();
|
|
|
|
while(it1 != mapObjects.end()) {
|
|
|
|
uint256 h = (*it1).first;
|
|
|
|
|
|
|
|
if((*it1).second.fCachedValid && ((nProp == uint256() || (h == nProp)))){
|
|
|
|
// Push the inventory budget proposal message over to the other client
|
|
|
|
pfrom->PushInventory(CInv(MSG_GOVERNANCE_OBJECT, h));
|
|
|
|
nInvCount++;
|
|
|
|
}
|
|
|
|
++it1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// SYNC OUR GOVERNANCE OBJECT VOTES WITH THEIR GOVERNANCE OBJECT VOTES
|
|
|
|
|
|
|
|
std::map<uint256, CGovernanceVote>::iterator it2 = mapVotesByHash.begin();
|
|
|
|
while(it2 != mapVotesByHash.end()) {
|
|
|
|
pfrom->PushInventory(CInv(MSG_GOVERNANCE_OBJECT_VOTE, (*it2).first));
|
|
|
|
nInvCount++;
|
|
|
|
++it2;
|
|
|
|
}
|
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-04-09 22:55:52 +02:00
|
|
|
LogPrintf("CGovernanceManager::Sync - sent %d items\n", nInvCount);
|
2016-04-09 21:57:53 +02:00
|
|
|
}
|
|
|
|
|
2016-06-08 08:57:16 +02:00
|
|
|
void CGovernanceManager::SyncParentObjectByVote(CNode* pfrom, const CGovernanceVote& vote)
|
|
|
|
{
|
2016-09-08 13:40:19 +02:00
|
|
|
if(!mapAskedForGovernanceObject.count(vote.GetParentHash())){
|
|
|
|
pfrom->PushMessage(NetMsgType::MNGOVERNANCESYNC, vote.GetParentHash());
|
|
|
|
mapAskedForGovernanceObject[vote.GetParentHash()] = GetTime();
|
2016-06-08 08:57:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-17 09:08:25 +02:00
|
|
|
bool CGovernanceManager::AddOrUpdateVote(const CGovernanceVote& vote, CNode* pfrom, std::string& strError)
|
2016-04-09 21:57:53 +02:00
|
|
|
{
|
2016-08-17 09:08:25 +02:00
|
|
|
// MAKE SURE WE HAVE THE PARENT OBJECT THE VOTE IS FOR
|
2016-04-09 21:57:53 +02:00
|
|
|
|
2016-08-17 09:08:25 +02:00
|
|
|
bool syncparent = false;
|
|
|
|
uint256 votehash;
|
|
|
|
{
|
|
|
|
LOCK(cs);
|
2016-09-08 13:40:19 +02:00
|
|
|
if(!mapObjects.count(vote.GetParentHash())) {
|
2016-08-17 09:08:25 +02:00
|
|
|
if(pfrom) {
|
|
|
|
// only ask for missing items after our syncing process is complete --
|
|
|
|
// otherwise we'll think a full sync succeeded when they return a result
|
|
|
|
if(!masternodeSync.IsSynced()) return false;
|
|
|
|
|
|
|
|
// ADD THE VOTE AS AN ORPHAN, TO BE USED UPON RECEIVAL OF THE PARENT OBJECT
|
|
|
|
|
2016-09-08 13:40:19 +02:00
|
|
|
LogPrintf("CGovernanceManager::AddOrUpdateVote - Unknown object %d, asking for source\n", vote.GetParentHash().ToString());
|
|
|
|
mapOrphanVotes[vote.GetParentHash()] = vote;
|
2016-08-17 09:08:25 +02:00
|
|
|
|
|
|
|
// ASK FOR THIS VOTES PARENT SPECIFICALLY FROM THIS USER (THEY SHOULD HAVE IT, NO?)
|
|
|
|
|
2016-09-08 13:40:19 +02:00
|
|
|
if(!mapAskedForGovernanceObject.count(vote.GetParentHash())){
|
2016-08-17 09:08:25 +02:00
|
|
|
syncparent = true;
|
2016-09-08 13:40:19 +02:00
|
|
|
votehash = vote.GetParentHash();
|
|
|
|
mapAskedForGovernanceObject[vote.GetParentHash()] = GetTime();
|
2016-08-17 09:08:25 +02:00
|
|
|
}
|
|
|
|
}
|
2016-04-09 21:57:53 +02:00
|
|
|
|
2016-08-17 09:08:25 +02:00
|
|
|
strError = "Governance object not found!";
|
|
|
|
return false;
|
2016-04-09 21:57:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-17 09:08:25 +02:00
|
|
|
// Need to keep this out of the locked section
|
|
|
|
if(syncparent) {
|
|
|
|
pfrom->PushMessage(NetMsgType::MNGOVERNANCESYNC, votehash);
|
|
|
|
}
|
2016-04-09 21:57:53 +02:00
|
|
|
|
2016-08-17 09:08:25 +02:00
|
|
|
// Reestablish lock
|
2016-04-14 00:41:40 +02:00
|
|
|
LOCK(cs);
|
2016-06-08 08:57:16 +02:00
|
|
|
// GET DETERMINISTIC HASH WHICH COLLIDES ON MASTERNODE-VIN/GOVOBJ-HASH/VOTE-SIGNAL
|
|
|
|
|
|
|
|
uint256 nTypeHash = vote.GetTypeHash();
|
|
|
|
uint256 nHash = vote.GetHash();
|
2016-04-14 03:52:26 +02:00
|
|
|
|
2016-06-08 08:57:16 +02:00
|
|
|
// LOOK FOR PREVIOUS VOTES BY THIS SPECIFIC MASTERNODE FOR THIS SPECIFIC SIGNAL
|
|
|
|
|
2016-09-08 13:40:19 +02:00
|
|
|
vote_m_it it = mapVotesByType.find(nTypeHash);
|
|
|
|
if(it != mapVotesByType.end()) {
|
|
|
|
if(it->second.GetTimestamp() > vote.GetTimestamp()) {
|
2016-06-08 08:57:16 +02:00
|
|
|
strError = strprintf("new vote older than existing vote - %s", nTypeHash.ToString());
|
2016-08-17 09:08:25 +02:00
|
|
|
LogPrint("gobject", "CGovernanceObject::AddOrUpdateVote - %s\n", strError);
|
2016-05-19 23:08:57 +02:00
|
|
|
return false;
|
|
|
|
}
|
2016-09-08 13:40:19 +02:00
|
|
|
if(vote.GetTimestamp() - it->second.GetTimestamp() < GOVERNANCE_UPDATE_MIN) {
|
|
|
|
strError = strprintf("time between votes is too soon - %s - %lli", nTypeHash.ToString(), vote.GetTimestamp() - it->second.GetTimestamp());
|
2016-08-17 09:08:25 +02:00
|
|
|
LogPrint("gobject", "CGovernanceObject::AddOrUpdateVote - %s\n", strError);
|
2016-05-19 23:08:57 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2016-04-14 00:41:40 +02:00
|
|
|
|
2016-06-08 08:57:16 +02:00
|
|
|
// UPDATE TO NEWEST VOTE
|
2016-08-17 09:08:25 +02:00
|
|
|
{
|
|
|
|
mapVotesByHash[nHash] = vote;
|
|
|
|
mapVotesByType[nTypeHash] = vote;
|
|
|
|
}
|
2016-06-08 08:57:16 +02:00
|
|
|
|
2016-08-17 09:08:25 +02:00
|
|
|
// SET CACHE AS DIRTY / WILL BE UPDATED NEXT BLOCK
|
2016-06-10 07:16:32 +02:00
|
|
|
|
2016-08-17 09:08:25 +02:00
|
|
|
CGovernanceObject* pGovObj = FindGovernanceObject(vote.GetParentHash());
|
|
|
|
if(pGovObj)
|
|
|
|
{
|
|
|
|
pGovObj->fDirtyCache = true;
|
|
|
|
UpdateCachesAndClean();
|
|
|
|
} else {
|
|
|
|
LogPrintf("Governance object not found! Can't update fDirtyCache - %s\n", vote.GetParentHash().ToString());
|
|
|
|
}
|
2016-06-10 07:16:32 +02:00
|
|
|
|
2016-04-14 00:41:40 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-09-05 01:44:10 +02:00
|
|
|
bool CGovernanceManager::MasternodeRateCheck(const CPubKey& pubkey)
|
|
|
|
{
|
|
|
|
LOCK(cs);
|
|
|
|
count_m_it it = mapLastMasternodeTrigger.find(pubkey.GetHash());
|
|
|
|
if(it == mapLastMasternodeTrigger.end()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// Allow 1 trigger per mn per cycle, with a small fudge factor
|
|
|
|
int mindiff = Params().GetConsensus().nSuperblockCycle - Params().GetConsensus().nSuperblockCycle / 10;
|
|
|
|
if((nCachedBlockHeight - it->second) > mindiff) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-04-16 19:19:17 +02:00
|
|
|
CGovernanceObject::CGovernanceObject()
|
2016-04-09 21:57:53 +02:00
|
|
|
{
|
2016-08-17 09:08:25 +02:00
|
|
|
// MAIN OBJECT DATA
|
|
|
|
|
2016-04-15 18:39:33 +02:00
|
|
|
strName = "unknown";
|
2016-04-09 21:57:53 +02:00
|
|
|
nTime = 0;
|
2016-08-17 09:08:25 +02:00
|
|
|
nObjectType = GOVERNANCE_OBJECT_UNKNOWN;
|
2016-04-26 06:08:36 +02:00
|
|
|
|
2016-04-26 13:42:34 +02:00
|
|
|
nHashParent = uint256(); //parent object, 0 is root
|
2016-04-26 06:08:36 +02:00
|
|
|
nRevision = 0; //object revision in the system
|
2016-08-17 09:08:25 +02:00
|
|
|
nCollateralHash = uint256(); //fee-tx
|
2016-04-26 06:08:36 +02:00
|
|
|
|
2016-08-17 09:08:25 +02:00
|
|
|
// CACHING FOR VARIOUS FLAGS
|
|
|
|
|
2016-05-13 18:03:01 +02:00
|
|
|
fCachedFunding = false;
|
2016-05-19 20:34:43 +02:00
|
|
|
fCachedValid = true;
|
2016-05-13 18:03:01 +02:00
|
|
|
fCachedDelete = false;
|
|
|
|
fCachedEndorsed = false;
|
2016-08-17 09:08:25 +02:00
|
|
|
fDirtyCache = true;
|
|
|
|
fUnparsable = false;
|
|
|
|
fExpired = false;
|
|
|
|
|
|
|
|
// PARSE JSON DATA STORAGE (STRDATA)
|
|
|
|
LoadData();
|
2016-04-09 21:57:53 +02:00
|
|
|
}
|
|
|
|
|
2016-08-17 09:08:25 +02:00
|
|
|
CGovernanceObject::CGovernanceObject(uint256 nHashParentIn, int nRevisionIn, std::string strNameIn, int64_t nTimeIn, uint256 nCollateralHashIn, std::string strDataIn)
|
2016-04-09 21:57:53 +02:00
|
|
|
{
|
2016-08-17 09:08:25 +02:00
|
|
|
// MAIN OBJECT DATA
|
|
|
|
|
2016-04-26 06:08:36 +02:00
|
|
|
nHashParent = nHashParentIn; //parent object, 0 is root
|
|
|
|
nRevision = nRevisionIn; //object revision in the system
|
2016-05-27 00:03:37 +02:00
|
|
|
strName = strNameIn;
|
|
|
|
nTime = nTimeIn;
|
2016-08-17 09:08:25 +02:00
|
|
|
nCollateralHash = nCollateralHashIn; //fee-tx
|
|
|
|
nObjectType = GOVERNANCE_OBJECT_UNKNOWN; // Avoid having an uninitialized variable
|
|
|
|
strData = strDataIn;
|
|
|
|
|
|
|
|
// CACHING FOR VARIOUS FLAGS
|
2016-06-10 07:16:32 +02:00
|
|
|
|
|
|
|
fCachedFunding = false;
|
|
|
|
fCachedValid = true;
|
|
|
|
fCachedDelete = false;
|
|
|
|
fCachedEndorsed = false;
|
2016-08-17 09:08:25 +02:00
|
|
|
fDirtyCache = true;
|
|
|
|
fUnparsable = false;
|
|
|
|
fExpired = false;
|
|
|
|
|
|
|
|
// 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-05-27 00:03:37 +02:00
|
|
|
// COPY OTHER OBJECT'S DATA INTO THIS OBJECT
|
|
|
|
|
|
|
|
nHashParent = other.nHashParent;
|
|
|
|
nRevision = other.nRevision;
|
2016-04-26 13:42:34 +02:00
|
|
|
strName = other.strName;
|
|
|
|
nTime = other.nTime;
|
2016-08-17 09:08:25 +02:00
|
|
|
nCollateralHash = other.nCollateralHash;
|
2016-05-13 18:03:01 +02:00
|
|
|
strData = other.strData;
|
2016-08-17 09:08:25 +02:00
|
|
|
nObjectType = other.nObjectType;
|
|
|
|
fUnparsable = true;
|
2016-06-10 07:16:32 +02:00
|
|
|
|
2016-09-05 01:44:10 +02:00
|
|
|
vinMasternode = other.vinMasternode;
|
|
|
|
vchSig = other.vchSig;
|
|
|
|
|
2016-06-10 07:16:32 +02:00
|
|
|
// caching
|
|
|
|
fCachedFunding = other.fCachedFunding;
|
|
|
|
fCachedValid = other.fCachedValid;
|
|
|
|
fCachedDelete = other.fCachedDelete;
|
|
|
|
fCachedEndorsed = other.fCachedEndorsed;
|
2016-08-17 09:08:25 +02:00
|
|
|
fDirtyCache = other.fDirtyCache;
|
|
|
|
fExpired = other.fExpired;
|
2016-04-26 13:42:34 +02:00
|
|
|
}
|
|
|
|
|
2016-09-05 01:44:10 +02:00
|
|
|
void CGovernanceObject::SetMasternodeInfo(const CTxIn& vin, const CPubKey& pubkey)
|
|
|
|
{
|
|
|
|
vinMasternode = vin;
|
|
|
|
pubkeyMasternode = pubkey;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CGovernanceObject::Sign(CKey& keyMasternode)
|
|
|
|
{
|
|
|
|
LOCK(cs);
|
|
|
|
CPubKey pubKeyCollateralAddress;
|
|
|
|
CKey keyCollateralAddress;
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!darkSendSigner.VerifyMessage(pubkeyMasternode, vchSig, strMessage, strError)) {
|
|
|
|
LogPrintf("CGovernanceObject::Sign -- VerifyMessage() failed, error: %s\n", strError);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CGovernanceObject::CheckSignature()
|
|
|
|
{
|
|
|
|
LOCK(cs);
|
|
|
|
std::string strError;
|
|
|
|
uint256 nHash = GetHash();
|
|
|
|
std::string strMessage = nHash.ToString();
|
|
|
|
|
|
|
|
if(!darkSendSigner.VerifyMessage(pubkeyMasternode, vchSig, strMessage, strError)) {
|
|
|
|
LogPrintf("CGovernance::CheckSignature -- VerifyMessage() failed, error: %s\n", strError);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-08-17 09:08:25 +02:00
|
|
|
int CGovernanceObject::GetObjectType()
|
2016-04-09 21:57:53 +02:00
|
|
|
{
|
2016-08-17 09:08:25 +02:00
|
|
|
return nObjectType;
|
|
|
|
}
|
|
|
|
|
|
|
|
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 << strName;
|
|
|
|
ss << nTime;
|
|
|
|
ss << strData;
|
|
|
|
// fee_tx is left out on purpose
|
|
|
|
uint256 h1 = ss.GetHash();
|
|
|
|
|
|
|
|
DBG( printf("CGovernanceObject::GetHash %i %s %li %s\n", nRevision, strName.c_str(), nTime, strData.c_str()); );
|
|
|
|
|
|
|
|
return h1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Return the actual object from the strData JSON structure.
|
|
|
|
|
|
|
|
Returns an empty object on error.
|
|
|
|
*/
|
|
|
|
UniValue CGovernanceObject::GetJSONObject()
|
|
|
|
{
|
|
|
|
UniValue obj(UniValue::VOBJ);
|
|
|
|
if(strData.empty()) {
|
|
|
|
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
|
|
|
|
* --------------------------------------------------------
|
|
|
|
*
|
|
|
|
* Attempt to load data from strData
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
void CGovernanceObject::LoadData()
|
|
|
|
{
|
|
|
|
// todo : 12.1 - resolved
|
|
|
|
//return;
|
|
|
|
|
|
|
|
if(strData.empty()) {
|
|
|
|
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);
|
|
|
|
|
|
|
|
DBG( cout << "CGovernanceObject::LoadData strData = "
|
|
|
|
<< GetDataAsString()
|
|
|
|
<< endl; );
|
|
|
|
|
2016-08-17 09:08:25 +02:00
|
|
|
UniValue obj = GetJSONObject();
|
|
|
|
nObjectType = obj["type"].get_int();
|
|
|
|
}
|
2016-08-29 00:08:27 +02:00
|
|
|
catch(std::exception& e) {
|
|
|
|
std::ostringstream ostr;
|
|
|
|
ostr << "CGovernanceObject::LoadData Error parsing JSON"
|
|
|
|
<< ", e.what() = " << e.what();
|
|
|
|
DBG( cout << ostr.str() << endl; );
|
|
|
|
LogPrintf( ostr.str().c_str() );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
catch(...) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* SetData - Example usage:
|
|
|
|
* --------------------------------------------------------
|
|
|
|
*
|
|
|
|
* Data must be stored as an encoded hex/json string.
|
|
|
|
* Other than the above requirement gov objects are data-agnostic.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
bool CGovernanceObject::SetData(std::string& strError, std::string strDataIn)
|
|
|
|
{
|
|
|
|
// SET DATA FIELD TO INPUT
|
|
|
|
|
|
|
|
if(strDataIn.size() > 512*4)
|
|
|
|
{
|
|
|
|
// (assumption) this is equal to pythons len(strData) > 512*4, I think
|
|
|
|
strError = "Too big.";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
strData = strDataIn;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* GetData - Example usage:
|
|
|
|
* --------------------------------------------------------
|
|
|
|
*
|
|
|
|
* Decode governance object data into UniValue(VOBJ)
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
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
|
|
|
|
* --------------------------------------------------------
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
std::string CGovernanceObject::GetDataAsHex()
|
|
|
|
{
|
|
|
|
return strData;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string CGovernanceObject::GetDataAsString()
|
|
|
|
{
|
|
|
|
vector<unsigned char> v = ParseHex(strData);
|
|
|
|
string s(v.begin(), v.end());
|
|
|
|
|
|
|
|
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-04-09 21:57:53 +02:00
|
|
|
if(!pindex) {
|
|
|
|
strError = "Tip is NULL";
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-04-15 18:39:33 +02:00
|
|
|
if(strName.size() > 20) {
|
2016-06-08 08:57:16 +02:00
|
|
|
strError = "Invalid object name, limit of 20 characters.";
|
2016-04-09 21:57:53 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-04-15 18:39:33 +02:00
|
|
|
if(strName != SanitizeString(strName)) {
|
2016-06-08 08:57:16 +02:00
|
|
|
strError = "Invalid object name, unsafe characters found.";
|
2016-04-09 21:57:53 +02:00
|
|
|
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-05 01:44:10 +02:00
|
|
|
if(GetAbsoluteNoCount(VOTE_SIGNAL_VALID) > mnodeman.CountEnabled(MSG_GOVERNANCE_PEER_PROTO_VERSION)/10) {
|
|
|
|
strError = "Automated removal";
|
|
|
|
return false;
|
2016-08-17 09:08:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// CHECK COLLATERAL IF REQUIRED (HIGH CPU USAGE)
|
|
|
|
|
2016-09-05 01:44:10 +02:00
|
|
|
if(fCheckCollateral) {
|
|
|
|
if(nObjectType == GOVERNANCE_OBJECT_TRIGGER) {
|
|
|
|
CMasternode mn;
|
2016-09-09 17:52:10 +02:00
|
|
|
if(!mnodeman.Get(pubkeyMasternode, mn)) {
|
2016-09-05 01:44:10 +02:00
|
|
|
strError = "Masternode not found";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if(!mn.IsEnabled()) {
|
|
|
|
strError = "Masternode not enabled";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that we have a valid MN signature
|
|
|
|
if(!CheckSignature()) {
|
|
|
|
strError = "Invalid masternode signature";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!governance.MasternodeRateCheck(pubkeyMasternode)) {
|
|
|
|
strError = "Masternode attempting to create too many objects";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
|
|
|
{
|
|
|
|
CAmount nMinFee = 0;
|
|
|
|
// Only 1 type has a fee for the moment but switch statement allows for future object types
|
|
|
|
switch(nObjectType) {
|
|
|
|
case GOVERNANCE_OBJECT_PROPOSAL:
|
|
|
|
nMinFee = GOVERNANCE_PROPOSAL_FEE_TX;
|
|
|
|
break;
|
|
|
|
case GOVERNANCE_OBJECT_TRIGGER:
|
|
|
|
nMinFee = 0;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
std::ostringstream ostr;
|
|
|
|
ostr << "CGovernanceObject::GetMinCollateralFee ERROR: Unknown governance object type: " << nObjectType;
|
|
|
|
throw std::runtime_error(ostr.str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nMinFee;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CGovernanceObject::IsCollateralValid(std::string& strError)
|
|
|
|
{
|
|
|
|
CAmount nMinFee = 0;
|
|
|
|
try {
|
|
|
|
nMinFee = GetMinCollateralFee();
|
|
|
|
}
|
|
|
|
catch(std::exception& e) {
|
|
|
|
strError = e.what();
|
|
|
|
LogPrintf("CGovernanceObject::IsCollateralValid ERROR An exception occurred - %s\n", e.what());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint256 nExpectedHash = GetHash();
|
|
|
|
|
|
|
|
CTransaction txCollateral;
|
|
|
|
uint256 nBlockHash;
|
|
|
|
|
|
|
|
// RETRIEVE TRANSACTION IN QUESTION
|
|
|
|
|
|
|
|
if(!GetTransaction(nCollateralHash, txCollateral, Params().GetConsensus(), nBlockHash, true)){
|
|
|
|
strError = strprintf("Can't find collateral tx %s", txCollateral.ToString());
|
|
|
|
LogPrintf("CGovernanceObject::IsCollateralValid - %s\n", strError);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(txCollateral.vout.size() < 1) {
|
|
|
|
strError = strprintf("tx vout size less than 1 | %d", txCollateral.vout.size());
|
|
|
|
LogPrintf ("CGovernanceObject::IsCollateralValid - %s\n", strError);
|
|
|
|
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) {
|
|
|
|
DBG( cout << "IsCollateralValid txout : " << o.ToString()
|
|
|
|
<< ", 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());
|
|
|
|
LogPrintf ("CGovernanceObject::IsCollateralValid - %s\n", strError);
|
|
|
|
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());
|
|
|
|
LogPrintf ("CGovernanceObject::IsCollateralValid - %s\n", strError);
|
|
|
|
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);
|
|
|
|
LogPrintf ("CGovernanceObject::IsCollateralValid - %s - %d confirmations\n", strError, nConfirmationsIn);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-04-26 06:08:36 +02:00
|
|
|
/**
|
|
|
|
* Get specific vote counts for each outcome (funding, validity, etc)
|
|
|
|
*/
|
|
|
|
|
2016-09-08 13:40:19 +02:00
|
|
|
int CGovernanceObject::GetAbsoluteYesCount(vote_signal_enum_t eVoteSignalIn)
|
2016-04-09 21:57:53 +02:00
|
|
|
{
|
2016-09-08 13:40:19 +02:00
|
|
|
return governance.CountMatchingVotes((*this), eVoteSignalIn, VOTE_OUTCOME_YES) - governance.CountMatchingVotes((*this), eVoteSignalIn, VOTE_OUTCOME_NO);
|
2016-04-09 21:57:53 +02:00
|
|
|
}
|
|
|
|
|
2016-09-08 13:40:19 +02:00
|
|
|
int CGovernanceObject::GetAbsoluteNoCount(vote_signal_enum_t eVoteSignalIn)
|
2016-06-08 08:57:16 +02:00
|
|
|
{
|
2016-09-08 13:40:19 +02:00
|
|
|
return governance.CountMatchingVotes((*this), eVoteSignalIn, VOTE_OUTCOME_NO) - governance.CountMatchingVotes((*this), eVoteSignalIn, VOTE_OUTCOME_YES);
|
2016-06-08 08:57:16 +02:00
|
|
|
}
|
|
|
|
|
2016-09-08 13:40:19 +02:00
|
|
|
int CGovernanceObject::GetYesCount(vote_signal_enum_t eVoteSignalIn)
|
2016-04-09 21:57:53 +02:00
|
|
|
{
|
2016-09-08 13:40:19 +02:00
|
|
|
return governance.CountMatchingVotes((*this), eVoteSignalIn, VOTE_OUTCOME_YES);
|
2016-04-09 21:57:53 +02:00
|
|
|
}
|
|
|
|
|
2016-09-08 13:40:19 +02:00
|
|
|
int CGovernanceObject::GetNoCount(vote_signal_enum_t eVoteSignalIn)
|
2016-04-09 21:57:53 +02:00
|
|
|
{
|
2016-09-08 13:40:19 +02:00
|
|
|
return governance.CountMatchingVotes((*this), eVoteSignalIn, VOTE_OUTCOME_NO);
|
2016-04-09 21:57:53 +02:00
|
|
|
}
|
|
|
|
|
2016-09-08 13:40:19 +02:00
|
|
|
int CGovernanceObject::GetAbstainCount(vote_signal_enum_t eVoteSignalIn)
|
2016-04-09 21:57:53 +02:00
|
|
|
{
|
2016-09-08 13:40:19 +02:00
|
|
|
return governance.CountMatchingVotes((*this), eVoteSignalIn, VOTE_OUTCOME_ABSTAIN);
|
2016-04-09 21:57:53 +02:00
|
|
|
}
|
|
|
|
|
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());
|
|
|
|
RelayInv(inv, MSG_GOVERNANCE_PEER_PROTO_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-05-23 19:53:05 +02:00
|
|
|
", Seen Budgets: " << (int)mapSeenGovernanceObjects.size() <<
|
|
|
|
", Seen Budget Votes: " << (int)mapSeenVotes.size() <<
|
2016-06-08 08:57:16 +02:00
|
|
|
", VoteByHash Count: " << (int)mapVotesByHash.size() <<
|
|
|
|
", VoteByType Count: " << (int)mapVotesByType.size();
|
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
|
|
|
|
// On the other hand it should be safe for us to access pindex without holding a lock
|
|
|
|
// on cs_main because the CBlockIndex objects are dynamically allocated and
|
|
|
|
// presumably never deleted.
|
|
|
|
LOCK(cs);
|
2016-04-09 21:57:53 +02:00
|
|
|
pCurrentBlockIndex = pindex;
|
2016-08-17 09:08:25 +02:00
|
|
|
nCachedBlockHeight = pCurrentBlockIndex->nHeight;
|
|
|
|
LogPrint("gobject", "pCurrentBlockIndex->nHeight: %d\n", pCurrentBlockIndex->nHeight);
|
|
|
|
|
|
|
|
// 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-09-08 13:40:19 +02:00
|
|
|
int CGovernanceManager::CountMatchingVotes(CGovernanceObject& govobj, vote_signal_enum_t eVoteSignalIn, vote_outcome_enum_t eVoteOutcomeIn)
|
2016-04-14 22:01:15 +02:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
*
|
|
|
|
* Count matching votes and return
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2016-08-17 09:08:25 +02:00
|
|
|
LOCK(cs);
|
2016-05-13 18:03:01 +02:00
|
|
|
int nCount = 0;
|
2016-04-14 22:01:15 +02:00
|
|
|
|
2016-09-08 13:40:19 +02:00
|
|
|
std::map<uint256, CGovernanceVote>::iterator it = mapVotesByHash.begin();
|
|
|
|
while(it != mapVotesByHash.end()) {
|
|
|
|
if(it->second.IsValid() &&
|
|
|
|
it->second.GetSignal() == eVoteSignalIn &&
|
|
|
|
it->second.GetOutcome() == eVoteOutcomeIn &&
|
|
|
|
it->second.GetParentHash() == govobj.GetHash()) {
|
|
|
|
++nCount;
|
2016-04-14 22:01:15 +02:00
|
|
|
}
|
2016-09-08 13:40:19 +02:00
|
|
|
++it;
|
2016-04-14 22:01:15 +02:00
|
|
|
}
|
|
|
|
|
2016-05-13 18:03:01 +02:00
|
|
|
return nCount;
|
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-08-28 21:15:48 +02:00
|
|
|
int nAbsVoteReq = 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;
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
if(GetAbsoluteYesCount(VOTE_SIGNAL_DELETE) >= nAbsVoteReq) fCachedDelete = true;
|
|
|
|
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.strName, second.strName);
|
|
|
|
swap(first.nHashParent, second.nHashParent);
|
|
|
|
swap(first.nRevision, second.nRevision);
|
|
|
|
swap(first.nTime, second.nTime);
|
|
|
|
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);
|
|
|
|
}
|