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-04-09 22:31:01 +02:00
|
|
|
#ifndef GOVERANCE_H
|
|
|
|
#define GOVERANCE_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"
|
|
|
|
#include "masternode.h"
|
2016-04-14 00:41:40 +02:00
|
|
|
#include "governance-vote.h"
|
2016-04-09 21:57:53 +02:00
|
|
|
#include <boost/lexical_cast.hpp>
|
|
|
|
#include "init.h"
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
extern CCriticalSection cs_budget;
|
|
|
|
|
2016-04-09 22:31:01 +02:00
|
|
|
class CGovernanceManager;
|
2016-04-09 21:57:53 +02:00
|
|
|
class CBudgetProposal;
|
|
|
|
class CBudgetVote;
|
2016-04-14 20:53:46 +02:00
|
|
|
class CNode;
|
2016-04-09 21:57:53 +02:00
|
|
|
|
|
|
|
// todo - 12.1 - change BUDGET_ to GOVERNANCE_ (cherry pick)
|
2016-04-10 02:43:15 +02:00
|
|
|
static const CAmount GOVERNANCE_FEE_TX = (5*COIN);
|
|
|
|
static const int64_t GOVERNANCE_FEE_CONFIRMATIONS = 6;
|
|
|
|
static const int64_t GOVERNANCE_UPDATE_MIN = 60*60;
|
2016-04-09 21:57:53 +02:00
|
|
|
|
2016-04-14 03:52:26 +02:00
|
|
|
extern std::vector<CBudgetProposal> vecImmatureBudgetProposals;
|
2016-04-12 18:00:19 +02:00
|
|
|
extern std::map<uint256, int64_t> askedForSourceProposalOrBudget;
|
2016-04-10 16:46:19 +02:00
|
|
|
extern CGovernanceManager governance;
|
2016-04-09 21:57:53 +02:00
|
|
|
|
|
|
|
//Check the collateral transaction for the budget proposal/finalized budget
|
2016-04-12 18:00:19 +02:00
|
|
|
extern bool IsCollateralValid(uint256 nTxCollateralHash, uint256 nExpectedHash, std::string& strError, int64_t& nTime, int& nConf, CAmount minFee);
|
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
|
|
|
{
|
|
|
|
private:
|
|
|
|
|
|
|
|
//hold txes until they mature enough to use
|
|
|
|
map<uint256, CTransaction> mapCollateral;
|
|
|
|
// Keep track of current block index
|
|
|
|
const CBlockIndex *pCurrentBlockIndex;
|
|
|
|
|
|
|
|
public:
|
|
|
|
// critical section to protect the inner data structures
|
|
|
|
mutable CCriticalSection cs;
|
|
|
|
|
|
|
|
// keep track of the scanning errors I've seen
|
|
|
|
map<uint256, CBudgetProposal> mapProposals;
|
|
|
|
|
2016-04-14 00:41:40 +02:00
|
|
|
// todo - 12.1 - move to private for better encapsulation
|
2016-04-14 03:52:26 +02:00
|
|
|
std::map<uint256, CBudgetProposal> mapSeenMasternodeBudgetProposals;
|
2016-04-09 21:57:53 +02:00
|
|
|
std::map<uint256, CBudgetVote> mapSeenMasternodeBudgetVotes;
|
|
|
|
std::map<uint256, CBudgetVote> mapOrphanMasternodeBudgetVotes;
|
2016-04-14 03:52:26 +02:00
|
|
|
// parent hash vote hash vote
|
2016-04-14 20:07:59 +02:00
|
|
|
std::map<uint256, std::map<uint256, CBudgetVote> > mapVotes;
|
2016-04-09 21:57:53 +02:00
|
|
|
|
2016-04-09 22:31:01 +02:00
|
|
|
CGovernanceManager() {
|
2016-04-09 21:57:53 +02:00
|
|
|
mapProposals.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ClearSeen() {
|
|
|
|
mapSeenMasternodeBudgetProposals.clear();
|
|
|
|
mapSeenMasternodeBudgetVotes.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
int CountProposalInventoryItems()
|
|
|
|
{
|
|
|
|
return mapSeenMasternodeBudgetProposals.size() + mapSeenMasternodeBudgetVotes.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
int sizeProposals() {return (int)mapProposals.size();}
|
|
|
|
|
2016-04-14 00:41:40 +02:00
|
|
|
// description: incremental sync with our peers
|
|
|
|
// note: incremental syncing seems excessive, well just have clients ask for specific objects and their votes
|
|
|
|
// note: 12.1 - remove
|
|
|
|
//void ResetSync();
|
|
|
|
//void MarkSynced();
|
|
|
|
void Sync(CNode* node, uint256 nProp);
|
2016-04-09 21:57:53 +02:00
|
|
|
|
|
|
|
void ProcessMessage(CNode* pfrom, std::string& strCommand, CDataStream& vRecv);
|
|
|
|
void NewBlock();
|
2016-04-09 22:31:01 +02:00
|
|
|
|
2016-04-09 21:57:53 +02:00
|
|
|
CBudgetProposal *FindProposal(const std::string &strProposalName);
|
|
|
|
CBudgetProposal *FindProposal(uint256 nHash);
|
|
|
|
|
|
|
|
std::vector<CBudgetProposal*> GetAllProposals();
|
2016-04-09 22:31:01 +02:00
|
|
|
|
2016-04-09 21:57:53 +02:00
|
|
|
bool IsBudgetPaymentBlock(int nBlockHeight);
|
|
|
|
bool AddProposal(CBudgetProposal& budgetProposal);
|
|
|
|
bool UpdateProposal(CBudgetVote& vote, CNode* pfrom, std::string& strError);
|
2016-04-14 03:52:26 +02:00
|
|
|
bool AddOrUpdateVote(CBudgetVote& vote, std::string& strError);
|
2016-04-09 21:57:53 +02:00
|
|
|
bool PropExists(uint256 nHash);
|
|
|
|
std::string GetRequiredPaymentsString(int nBlockHeight);
|
2016-04-14 22:01:15 +02:00
|
|
|
void CleanAndRemove(bool fSignatureCheck);
|
|
|
|
int CountMatchingVotes(int nVoteTypeIn, int nVoteOutcomeIn);
|
2016-04-09 21:57:53 +02:00
|
|
|
|
|
|
|
void CheckOrphanVotes();
|
|
|
|
void Clear(){
|
|
|
|
LOCK(cs);
|
|
|
|
|
|
|
|
LogPrintf("Budget object cleared\n");
|
|
|
|
mapProposals.clear();
|
|
|
|
mapSeenMasternodeBudgetProposals.clear();
|
|
|
|
mapSeenMasternodeBudgetVotes.clear();
|
|
|
|
mapOrphanMasternodeBudgetVotes.clear();
|
|
|
|
}
|
|
|
|
void CheckAndRemove();
|
|
|
|
std::string ToString() const;
|
|
|
|
|
|
|
|
ADD_SERIALIZE_METHODS;
|
|
|
|
|
|
|
|
template <typename Stream, typename Operation>
|
|
|
|
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
|
|
|
|
READWRITE(mapSeenMasternodeBudgetProposals);
|
|
|
|
READWRITE(mapSeenMasternodeBudgetVotes);
|
|
|
|
READWRITE(mapOrphanMasternodeBudgetVotes);
|
|
|
|
READWRITE(mapProposals);
|
2016-04-14 03:52:26 +02:00
|
|
|
READWRITE(mapVotes);
|
2016-04-09 21:57:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void UpdatedBlockTip(const CBlockIndex *pindex);
|
|
|
|
};
|
|
|
|
|
2016-04-14 03:52:26 +02:00
|
|
|
/**
|
|
|
|
* Governance objects can hold any time of data
|
|
|
|
* --------------------------------------------
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
// todo - 12.1 - add payload obj to CGovernanceObj
|
|
|
|
// union GovernanceObjectPayload
|
|
|
|
// {
|
|
|
|
// CAmount nAmount;
|
|
|
|
// int nValue;
|
|
|
|
// bool bIsActive;
|
|
|
|
// std::string strPayload;
|
|
|
|
// double fValue;
|
|
|
|
// }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Governance object payload types
|
|
|
|
* --------------------------------------------
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
// todo - 12.1 - add payload obj to CGovernanceObj
|
|
|
|
// enum class GovernanceObjectPayloadType {
|
|
|
|
// CAmount,
|
|
|
|
// Int,
|
|
|
|
// Bool,
|
|
|
|
// String,
|
|
|
|
// Double
|
|
|
|
// };
|
|
|
|
|
2016-04-09 21:57:53 +02:00
|
|
|
//
|
|
|
|
// Budget Proposal : Contains the masternode votes for each budget
|
|
|
|
//
|
|
|
|
|
|
|
|
class CBudgetProposal
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
// critical section to protect the inner data structures
|
|
|
|
mutable CCriticalSection cs;
|
|
|
|
CAmount nAlloted;
|
|
|
|
|
|
|
|
public:
|
|
|
|
bool fValid;
|
|
|
|
std::string strProposalName;
|
|
|
|
|
|
|
|
/*
|
|
|
|
json object with name, short-description, long-description, pdf-url and any other info
|
|
|
|
This allows the proposal website to stay 100% decentralized
|
|
|
|
*/
|
|
|
|
std::string strURL;
|
2016-04-14 22:01:15 +02:00
|
|
|
int nStartTime;
|
|
|
|
int nEndTime;
|
2016-04-14 20:53:46 +02:00
|
|
|
CAmount nAmount; // 12.1 - remove
|
2016-04-14 03:52:26 +02:00
|
|
|
// int nPriority; //budget is sorted by this integer before funding votecount
|
|
|
|
// GovernanceObjectPayloadType payloadType;
|
|
|
|
// GovernanceObjectPayload payload;
|
|
|
|
CScript address; //todo rename to addressOwner;
|
2016-04-09 21:57:53 +02:00
|
|
|
int64_t nTime;
|
|
|
|
uint256 nFeeTXHash;
|
2016-04-14 03:52:26 +02:00
|
|
|
uint256 nHashParent; // 12.1 - remove
|
2016-04-09 21:57:53 +02:00
|
|
|
|
|
|
|
//cache object
|
|
|
|
|
|
|
|
CBudgetProposal();
|
|
|
|
CBudgetProposal(const CBudgetProposal& other);
|
2016-04-14 23:28:33 +02:00
|
|
|
CBudgetProposal(std::string strProposalNameIn, std::string strURLIn, int nPaymentCount, CScript addressIn, CAmount nAmountIn, int64_t nStartTimeIn, int64_t nEndTimeIn, uint256 nFeeTXHashIn);
|
2016-04-09 21:57:53 +02:00
|
|
|
|
|
|
|
bool HasMinimumRequiredSupport();
|
|
|
|
bool IsValid(const CBlockIndex* pindex, std::string& strError, bool fCheckCollateral=true);
|
|
|
|
bool IsEstablished();
|
2016-04-14 22:01:15 +02:00
|
|
|
bool NetworkWillPay();
|
2016-04-09 21:57:53 +02:00
|
|
|
|
|
|
|
std::string GetName() {return strProposalName; }
|
|
|
|
std::string GetURL() {return strURL; }
|
2016-04-14 22:01:15 +02:00
|
|
|
int GetStartTime() {return nStartTime;}
|
|
|
|
int GetEndTime() {return nEndTime;}
|
2016-04-09 21:57:53 +02:00
|
|
|
CScript GetPayee() {return address;}
|
2016-04-14 23:28:33 +02:00
|
|
|
int IsActive(int64_t nTime) {return nTime > nStartTime && nTime < nEndTime;}
|
2016-04-09 21:57:53 +02:00
|
|
|
int GetAbsoluteYesCount();
|
|
|
|
int GetYesCount();
|
|
|
|
int GetNoCount();
|
|
|
|
int GetAbstainCount();
|
|
|
|
CAmount GetAmount() {return nAmount;}
|
|
|
|
|
|
|
|
void CleanAndRemove(bool fSignatureCheck);
|
|
|
|
|
|
|
|
uint256 GetHash(){
|
|
|
|
CHashWriter ss(SER_GETHASH, PROTOCOL_VERSION);
|
|
|
|
ss << strProposalName;
|
|
|
|
ss << strURL;
|
2016-04-14 22:01:15 +02:00
|
|
|
ss << nStartTime;
|
|
|
|
ss << nEndTime;
|
2016-04-09 21:57:53 +02:00
|
|
|
ss << nAmount;
|
|
|
|
ss << *(CScriptBase*)(&address);
|
|
|
|
uint256 h1 = ss.GetHash();
|
|
|
|
|
|
|
|
return h1;
|
|
|
|
}
|
2016-04-14 20:53:46 +02:00
|
|
|
void Relay();
|
2016-04-09 21:57:53 +02:00
|
|
|
|
|
|
|
ADD_SERIALIZE_METHODS;
|
|
|
|
|
|
|
|
template <typename Stream, typename Operation>
|
|
|
|
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
|
|
|
|
//for syncing with other clients
|
|
|
|
READWRITE(LIMITED_STRING(strProposalName, 20));
|
|
|
|
READWRITE(LIMITED_STRING(strURL, 64));
|
|
|
|
READWRITE(nTime);
|
2016-04-14 22:01:15 +02:00
|
|
|
READWRITE(nStartTime);
|
|
|
|
READWRITE(nEndTime);
|
2016-04-09 21:57:53 +02:00
|
|
|
READWRITE(nAmount);
|
|
|
|
|
|
|
|
READWRITE(*(CScriptBase*)(&address));
|
|
|
|
READWRITE(nTime);
|
|
|
|
READWRITE(nFeeTXHash);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
#endif
|