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.
|
2016-09-12 09:40:00 +02:00
|
|
|
|
2016-11-13 18:52:34 +01:00
|
|
|
#ifndef GOVERNANCE_H
|
|
|
|
#define GOVERNANCE_H
|
2016-04-09 21:57:53 +02:00
|
|
|
|
2016-08-17 09:08:25 +02:00
|
|
|
//#define ENABLE_DASH_DEBUG
|
|
|
|
|
|
|
|
#include "util.h"
|
2016-04-09 21:57:53 +02:00
|
|
|
#include "main.h"
|
|
|
|
#include "sync.h"
|
|
|
|
#include "net.h"
|
|
|
|
#include "key.h"
|
|
|
|
#include "util.h"
|
|
|
|
#include "base58.h"
|
2016-11-24 19:12:05 +01:00
|
|
|
#include "governance-object.h"
|
2016-04-09 21:57:53 +02:00
|
|
|
#include "masternode.h"
|
2016-11-13 18:52:34 +01:00
|
|
|
#include "governance-exceptions.h"
|
2016-04-14 00:41:40 +02:00
|
|
|
#include "governance-vote.h"
|
2016-11-13 18:52:34 +01:00
|
|
|
#include "governance-votedb.h"
|
2016-05-25 18:56:58 +02:00
|
|
|
#include "masternodeman.h"
|
2016-04-09 21:57:53 +02:00
|
|
|
#include <boost/lexical_cast.hpp>
|
|
|
|
#include "init.h"
|
2016-08-17 09:08:25 +02:00
|
|
|
#include <univalue.h>
|
|
|
|
#include "utilstrencodings.h"
|
2016-11-13 18:52:34 +01:00
|
|
|
#include "cachemap.h"
|
|
|
|
#include "cachemultimap.h"
|
2016-04-09 21:57:53 +02:00
|
|
|
|
2016-04-16 19:19:17 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2016-09-12 09:40:00 +02:00
|
|
|
class CGovernanceManager;
|
2016-11-13 18:52:34 +01:00
|
|
|
class CGovernanceTriggerManager;
|
2016-09-12 09:40:00 +02:00
|
|
|
class CGovernanceObject;
|
|
|
|
class CGovernanceVote;
|
2016-08-17 09:08:25 +02:00
|
|
|
|
2016-09-12 09:40:00 +02:00
|
|
|
extern std::map<uint256, int64_t> mapAskedForGovernanceObject;
|
|
|
|
extern CGovernanceManager governance;
|
2016-04-09 21:57:53 +02:00
|
|
|
|
2016-11-24 19:12:05 +01:00
|
|
|
typedef std::pair<CGovernanceObject, int64_t> object_time_pair_t;
|
|
|
|
|
2016-04-09 21:57:53 +02:00
|
|
|
//
|
2016-04-09 22:31:01 +02:00
|
|
|
// Governance Manager : Contains all proposals for the budget
|
2016-04-09 21:57:53 +02:00
|
|
|
//
|
2016-04-09 22:31:01 +02:00
|
|
|
class CGovernanceManager
|
2016-04-09 21:57:53 +02:00
|
|
|
{
|
2016-11-13 18:52:34 +01:00
|
|
|
friend class CGovernanceObject;
|
|
|
|
|
2016-08-17 09:08:25 +02:00
|
|
|
public: // Types
|
2016-12-08 21:00:49 +01:00
|
|
|
struct last_object_rec {
|
2016-12-11 07:17:38 +01:00
|
|
|
last_object_rec(int64_t nLastTriggerTimeIn = 0, int64_t nLastWatchdogTimeIn = 0, bool fStatusOKIn = true)
|
|
|
|
: nLastTriggerTime(nLastTriggerTimeIn),
|
|
|
|
nLastWatchdogTime(nLastWatchdogTimeIn),
|
|
|
|
fStatusOK(fStatusOKIn)
|
2016-12-08 21:00:49 +01:00
|
|
|
{}
|
|
|
|
|
|
|
|
ADD_SERIALIZE_METHODS;
|
|
|
|
|
|
|
|
template <typename Stream, typename Operation>
|
|
|
|
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion)
|
|
|
|
{
|
2016-12-11 07:17:38 +01:00
|
|
|
READWRITE(nLastTriggerTime);
|
|
|
|
READWRITE(nLastWatchdogTime);
|
|
|
|
READWRITE(fStatusOK);
|
2016-12-08 21:00:49 +01:00
|
|
|
}
|
|
|
|
|
2016-12-11 07:17:38 +01:00
|
|
|
int64_t nLastTriggerTime;
|
|
|
|
int64_t nLastWatchdogTime;
|
|
|
|
bool fStatusOK;
|
2016-12-08 21:00:49 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2016-08-17 09:08:25 +02:00
|
|
|
typedef std::map<uint256, CGovernanceObject> object_m_t;
|
|
|
|
|
|
|
|
typedef object_m_t::iterator object_m_it;
|
|
|
|
|
|
|
|
typedef object_m_t::const_iterator object_m_cit;
|
|
|
|
|
2016-11-13 18:52:34 +01:00
|
|
|
typedef CacheMap<uint256, CGovernanceObject*> object_ref_cache_t;
|
|
|
|
|
2016-08-17 09:08:25 +02:00
|
|
|
typedef std::map<uint256, int> count_m_t;
|
|
|
|
|
|
|
|
typedef count_m_t::iterator count_m_it;
|
|
|
|
|
|
|
|
typedef count_m_t::const_iterator count_m_cit;
|
|
|
|
|
|
|
|
typedef std::map<uint256, CGovernanceVote> vote_m_t;
|
|
|
|
|
|
|
|
typedef vote_m_t::iterator vote_m_it;
|
|
|
|
|
|
|
|
typedef vote_m_t::const_iterator vote_m_cit;
|
|
|
|
|
2016-11-13 18:52:34 +01:00
|
|
|
typedef CacheMap<uint256, CGovernanceVote> vote_cache_t;
|
2016-08-17 09:08:25 +02:00
|
|
|
|
2016-11-24 19:12:05 +01:00
|
|
|
typedef CacheMultiMap<uint256, vote_time_pair_t> vote_mcache_t;
|
2016-08-17 09:08:25 +02:00
|
|
|
|
|
|
|
typedef object_m_t::size_type size_type;
|
|
|
|
|
2016-12-08 21:00:49 +01:00
|
|
|
typedef std::map<COutPoint, last_object_rec > txout_m_t;
|
2016-09-15 08:49:24 +02:00
|
|
|
|
|
|
|
typedef txout_m_t::iterator txout_m_it;
|
|
|
|
|
|
|
|
typedef txout_m_t::const_iterator txout_m_cit;
|
|
|
|
|
2016-11-13 18:52:34 +01:00
|
|
|
typedef std::set<uint256> hash_s_t;
|
|
|
|
|
|
|
|
typedef hash_s_t::iterator hash_s_it;
|
|
|
|
|
|
|
|
typedef hash_s_t::const_iterator hash_s_cit;
|
|
|
|
|
2016-11-24 19:12:05 +01:00
|
|
|
typedef std::map<uint256, object_time_pair_t> object_time_m_t;
|
|
|
|
|
|
|
|
typedef object_time_m_t::iterator object_time_m_it;
|
|
|
|
|
|
|
|
typedef object_time_m_t::const_iterator object_time_m_cit;
|
|
|
|
|
2016-11-25 15:08:48 +01:00
|
|
|
typedef std::map<uint256, int64_t> hash_time_m_t;
|
|
|
|
|
|
|
|
typedef hash_time_m_t::iterator hash_time_m_it;
|
|
|
|
|
|
|
|
typedef hash_time_m_t::const_iterator hash_time_m_cit;
|
|
|
|
|
2016-04-09 21:57:53 +02:00
|
|
|
private:
|
2016-11-13 18:52:34 +01:00
|
|
|
static const int MAX_CACHE_SIZE = 1000000;
|
|
|
|
|
|
|
|
static const std::string SERIALIZATION_VERSION_STRING;
|
2016-04-09 21:57:53 +02:00
|
|
|
|
|
|
|
// Keep track of current block index
|
|
|
|
const CBlockIndex *pCurrentBlockIndex;
|
|
|
|
|
2016-04-19 18:51:15 +02:00
|
|
|
int64_t nTimeLastDiff;
|
2016-08-17 09:08:25 +02:00
|
|
|
int nCachedBlockHeight;
|
2016-04-19 18:51:15 +02:00
|
|
|
|
2016-08-17 09:08:25 +02:00
|
|
|
// keep track of the scanning errors
|
|
|
|
object_m_t mapObjects;
|
2016-04-09 21:57:53 +02:00
|
|
|
|
2016-08-17 09:08:25 +02:00
|
|
|
count_m_t mapSeenGovernanceObjects;
|
2016-06-08 08:57:16 +02:00
|
|
|
|
2016-11-24 19:12:05 +01:00
|
|
|
object_time_m_t mapMasternodeOrphanObjects;
|
2016-11-12 02:51:45 +01:00
|
|
|
|
2016-11-25 15:08:48 +01:00
|
|
|
hash_time_m_t mapWatchdogObjects;
|
|
|
|
|
2016-11-13 18:52:34 +01:00
|
|
|
object_ref_cache_t mapVoteToObject;
|
|
|
|
|
|
|
|
vote_cache_t mapInvalidVotes;
|
|
|
|
|
|
|
|
vote_mcache_t mapOrphanVotes;
|
2016-04-09 21:57:53 +02:00
|
|
|
|
2016-12-08 21:00:49 +01:00
|
|
|
txout_m_t mapLastMasternodeObject;
|
2016-09-05 01:44:10 +02:00
|
|
|
|
2016-11-13 18:52:34 +01:00
|
|
|
hash_s_t setRequestedObjects;
|
|
|
|
|
|
|
|
hash_s_t setRequestedVotes;
|
|
|
|
|
2016-11-18 15:17:22 +01:00
|
|
|
bool fRateChecksEnabled;
|
|
|
|
|
2016-08-17 09:08:25 +02:00
|
|
|
public:
|
|
|
|
// critical section to protect the inner data structures
|
|
|
|
mutable CCriticalSection cs;
|
2016-09-12 09:40:00 +02:00
|
|
|
|
2016-09-05 01:44:10 +02:00
|
|
|
CGovernanceManager();
|
2016-04-09 21:57:53 +02:00
|
|
|
|
2016-11-13 18:52:34 +01:00
|
|
|
virtual ~CGovernanceManager() {}
|
|
|
|
|
2016-04-09 21:57:53 +02:00
|
|
|
int CountProposalInventoryItems()
|
|
|
|
{
|
2016-11-13 18:52:34 +01:00
|
|
|
// TODO What is this for ?
|
|
|
|
return mapSeenGovernanceObjects.size();
|
|
|
|
//return mapSeenGovernanceObjects.size() + mapSeenVotes.size();
|
2016-04-09 21:57:53 +02:00
|
|
|
}
|
|
|
|
|
2016-11-13 18:52:34 +01:00
|
|
|
/**
|
|
|
|
* This is called by AlreadyHave in main.cpp as part of the inventory
|
|
|
|
* retrieval process. Returns true if we want to retrieve the object, otherwise
|
|
|
|
* false. (Note logic is inverted in AlreadyHave).
|
|
|
|
*/
|
|
|
|
bool ConfirmInventoryRequest(const CInv& inv);
|
|
|
|
|
2016-04-14 00:41:40 +02:00
|
|
|
void Sync(CNode* node, uint256 nProp);
|
2016-11-13 18:52:34 +01:00
|
|
|
|
2016-04-09 21:57:53 +02:00
|
|
|
void ProcessMessage(CNode* pfrom, std::string& strCommand, CDataStream& vRecv);
|
2016-11-13 18:52:34 +01:00
|
|
|
|
2016-04-09 21:57:53 +02:00
|
|
|
void NewBlock();
|
2016-04-09 22:31:01 +02:00
|
|
|
|
2016-06-08 08:57:16 +02:00
|
|
|
CGovernanceObject *FindGovernanceObject(const uint256& nHash);
|
2016-08-17 09:08:25 +02:00
|
|
|
|
2016-11-13 18:52:34 +01:00
|
|
|
std::vector<CGovernanceVote> GetMatchingVotes(const uint256& nParentHash);
|
2016-11-22 20:26:36 +01:00
|
|
|
std::vector<CGovernanceVote> GetCurrentVotes(const uint256& nParentHash, const CTxIn& mnCollateralOutpointFilter);
|
2016-08-17 09:08:25 +02:00
|
|
|
std::vector<CGovernanceObject*> GetAllNewerThan(int64_t nMoreThanTime);
|
2016-04-09 22:31:01 +02:00
|
|
|
|
2016-04-09 21:57:53 +02:00
|
|
|
bool IsBudgetPaymentBlock(int nBlockHeight);
|
2016-05-28 12:31:44 +02:00
|
|
|
bool AddGovernanceObject (CGovernanceObject& govobj);
|
2016-08-17 09:08:25 +02:00
|
|
|
|
2016-04-09 21:57:53 +02:00
|
|
|
std::string GetRequiredPaymentsString(int nBlockHeight);
|
2016-11-13 18:52:34 +01:00
|
|
|
|
2016-08-17 09:08:25 +02:00
|
|
|
void UpdateCachesAndClean();
|
2016-04-09 21:57:53 +02:00
|
|
|
|
2016-11-13 18:52:34 +01:00
|
|
|
void CheckAndRemove() {UpdateCachesAndClean();}
|
2016-08-17 09:08:25 +02:00
|
|
|
|
2016-09-12 09:40:00 +02:00
|
|
|
void Clear()
|
|
|
|
{
|
2016-04-09 21:57:53 +02:00
|
|
|
LOCK(cs);
|
|
|
|
|
2016-08-17 09:08:25 +02:00
|
|
|
LogPrint("gobject", "Governance object manager was cleared\n");
|
2016-05-13 18:03:01 +02:00
|
|
|
mapObjects.clear();
|
2016-05-23 19:53:05 +02:00
|
|
|
mapSeenGovernanceObjects.clear();
|
2016-11-29 01:01:03 +01:00
|
|
|
mapWatchdogObjects.clear();
|
2016-11-13 18:52:34 +01:00
|
|
|
mapVoteToObject.Clear();
|
|
|
|
mapInvalidVotes.Clear();
|
|
|
|
mapOrphanVotes.Clear();
|
2016-12-08 21:00:49 +01:00
|
|
|
mapLastMasternodeObject.clear();
|
2016-04-09 21:57:53 +02:00
|
|
|
}
|
2016-09-12 09:40:00 +02:00
|
|
|
|
2016-04-09 21:57:53 +02:00
|
|
|
std::string ToString() const;
|
|
|
|
|
|
|
|
ADD_SERIALIZE_METHODS;
|
|
|
|
|
|
|
|
template <typename Stream, typename Operation>
|
|
|
|
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
|
2016-08-17 09:08:25 +02:00
|
|
|
LOCK(cs);
|
2016-11-13 18:52:34 +01:00
|
|
|
std::string strVersion;
|
|
|
|
if(ser_action.ForRead()) {
|
|
|
|
READWRITE(strVersion);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
strVersion = SERIALIZATION_VERSION_STRING;
|
|
|
|
READWRITE(strVersion);
|
|
|
|
}
|
2016-05-23 19:53:05 +02:00
|
|
|
READWRITE(mapSeenGovernanceObjects);
|
2016-11-13 18:52:34 +01:00
|
|
|
READWRITE(mapInvalidVotes);
|
2016-05-23 19:58:39 +02:00
|
|
|
READWRITE(mapOrphanVotes);
|
2016-05-13 18:03:01 +02:00
|
|
|
READWRITE(mapObjects);
|
2016-11-29 01:01:03 +01:00
|
|
|
READWRITE(mapWatchdogObjects);
|
2016-12-08 21:00:49 +01:00
|
|
|
READWRITE(mapLastMasternodeObject);
|
2016-11-13 18:52:34 +01:00
|
|
|
if(ser_action.ForRead() && (strVersion != SERIALIZATION_VERSION_STRING)) {
|
|
|
|
Clear();
|
|
|
|
return;
|
|
|
|
}
|
2016-04-09 21:57:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void UpdatedBlockTip(const CBlockIndex *pindex);
|
2016-09-12 09:40:00 +02:00
|
|
|
int64_t GetLastDiffTime() { return nTimeLastDiff; }
|
|
|
|
void UpdateLastDiffTime(int64_t nTimeIn) { nTimeLastDiff = nTimeIn; }
|
2016-08-17 09:08:25 +02:00
|
|
|
|
|
|
|
int GetCachedBlockHeight() { return nCachedBlockHeight; }
|
|
|
|
|
|
|
|
// Accessors for thread-safe access to maps
|
|
|
|
bool HaveObjectForHash(uint256 nHash);
|
|
|
|
|
|
|
|
bool HaveVoteForHash(uint256 nHash);
|
|
|
|
|
|
|
|
bool SerializeObjectForHash(uint256 nHash, CDataStream& ss);
|
|
|
|
|
|
|
|
bool SerializeVoteForHash(uint256 nHash, CDataStream& ss);
|
|
|
|
|
|
|
|
void AddSeenGovernanceObject(uint256 nHash, int status);
|
|
|
|
|
|
|
|
void AddSeenVote(uint256 nHash, int status);
|
|
|
|
|
2016-12-08 21:00:49 +01:00
|
|
|
bool MasternodeRateCheck(const CGovernanceObject& govobj, bool fUpdateLast = false);
|
2016-09-05 01:44:10 +02:00
|
|
|
|
2016-12-11 07:17:38 +01:00
|
|
|
bool MasternodeRateCheck(const CGovernanceObject& govobj, bool fUpdateLast, bool fForce, bool& fRateCheckBypassed);
|
|
|
|
|
2016-12-07 05:16:34 +01:00
|
|
|
bool ProcessVoteAndRelay(const CGovernanceVote& vote, CGovernanceException& exception) {
|
|
|
|
bool fOK = ProcessVote(NULL, vote, exception);
|
|
|
|
if(fOK) {
|
|
|
|
vote.Relay();
|
|
|
|
}
|
|
|
|
return fOK;
|
2016-11-13 18:52:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void CheckMasternodeOrphanVotes();
|
|
|
|
|
2016-11-12 02:51:45 +01:00
|
|
|
void CheckMasternodeOrphanObjects();
|
|
|
|
|
2016-11-18 15:17:22 +01:00
|
|
|
bool AreRateChecksEnabled() const {
|
|
|
|
LOCK(cs);
|
|
|
|
return fRateChecksEnabled;
|
|
|
|
}
|
|
|
|
|
2016-11-28 15:21:50 +01:00
|
|
|
void InitOnLoad();
|
|
|
|
|
2016-11-05 17:13:30 +01:00
|
|
|
private:
|
2016-11-13 18:52:34 +01:00
|
|
|
void RequestGovernanceObject(CNode* pfrom, const uint256& nHash);
|
|
|
|
|
|
|
|
void AddInvalidVote(const CGovernanceVote& vote)
|
|
|
|
{
|
|
|
|
mapInvalidVotes.Insert(vote.GetHash(), vote);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AddOrphanVote(const CGovernanceVote& vote)
|
|
|
|
{
|
2016-11-24 19:12:05 +01:00
|
|
|
mapOrphanVotes.Insert(vote.GetHash(), vote_time_pair_t(vote, GetAdjustedTime() + GOVERNANCE_ORPHAN_EXPIRATION_TIME));
|
2016-11-13 18:52:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ProcessVote(CNode* pfrom, const CGovernanceVote& vote, CGovernanceException& exception);
|
|
|
|
|
|
|
|
/// Called to indicate a requested object has been received
|
|
|
|
bool AcceptObjectMessage(const uint256& nHash);
|
|
|
|
|
|
|
|
/// Called to indicate a requested vote has been received
|
|
|
|
bool AcceptVoteMessage(const uint256& nHash);
|
|
|
|
|
|
|
|
static bool AcceptMessage(const uint256& nHash, hash_s_t& setHash);
|
|
|
|
|
2016-12-02 13:53:18 +01:00
|
|
|
void CheckOrphanVotes(CGovernanceObject& govobj, CGovernanceException& exception);
|
2016-11-13 18:52:34 +01:00
|
|
|
|
|
|
|
void RebuildIndexes();
|
|
|
|
|
|
|
|
/// Returns MN index, handling the case of index rebuilds
|
|
|
|
int GetMasternodeIndex(const CTxIn& masternodeVin);
|
|
|
|
|
|
|
|
void RebuildVoteMaps();
|
|
|
|
|
2016-11-05 17:13:30 +01:00
|
|
|
void AddCachedTriggers();
|
|
|
|
|
2016-04-09 21:57:53 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|