neobytes/src/masternode-payments.h

290 lines
8.0 KiB
C
Raw Normal View History

2015-04-16 21:58:09 +02:00
// Copyright (c) 2014-2016 The Dash Core developers
2015-04-16 21:58:09 +02:00
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#ifndef MASTERNODE_PAYMENTS_H
#define MASTERNODE_PAYMENTS_H
Merge #944: V0.12.1.x governance pr - part 1 - base functionality 068c178 Added DBG macro in util.h to facilitate debugging - This macro allows debugging statements (typically printf's or cout's) to be activated or deactivated with a single comment. Uncomment the line: //#define ENABLE_DASH_DEBUG in util.h to enable debugging statements. - When commented any code wrapped with the DBG() macro will simply be removed by the preprocessor. When not commented all such wrapped statements will be present. - For maximum effectiveness it is best that util.h be the first effective include in all source files. It is also possible to enable the macro for a single file by temporarily adding #define ENABLE_DASH_DEBUG to the top of the file. - Code committed to non-development branches should always have the define commented. d125d9b V0.12.1.x -- merging trigger/generic object/superblock changes for testnet phase II - This commit contains the core governance system changes for 0.12.1. Any unrelated changes have either been removed or moved to separate commits. 120724c File mode fixes - Changed mode 0755->0644 on several source files. c7f9e11 Updated todo reminders - Added reminder to revert temporary reduction of number of votes required to trigger superblock to 1 for testing 92adc98 Made CSuperblockManager::IsValidSuperblockHeight an inline function - This is for efficiency since this function is called often and is only 1 line of code. c050ed7 Added comment explaining rationale for no LOCK(cs) in CSuperblock::IsValid dc933fe Removed unused CSuperblockManager::IsBlockValid function decec88 Moved calls to SuperblockManager::IsValidSuperblockHeight into IsSuperblockTriggered. - Since calls to the later function are always protected by the former there's no reason to keep these separate and this simplifies the code in masternode-payments.cpp. 8672885 Reestablished expected value check for non-superblocks in IsBlockValueValid b01cbe0 Changes to IsBlockValueValid to fix rpc test failure a937c76 Changed include order to allow per file activation of the DBG macro d116aa5 Fixed IsValidSuperblockHeight logic - Note this has an effect on testing because we can now only create 1 superblock per day. Devs may need to temporarily change testnet params for easier testing. 2d0c2de Convert superblock payments to CAmount - We assume that payment values in JSON are in units of DASH for consistency with other RPC functions, such as createrawtransaction. 376b833 Revert temporary testing value for nAbsVoteReq - Also ensure that number of votes required is never smaller than 1 8c89f4b Cleaned up CSuperblock error handling - Exceptions are now thrown consistently rather than using a mix of exceptions and return code checking. Exceptions are now caught only in AddNewTrigger when the CSuperblock constructor is called. Unnecessary object status members have been removed. d7c8a6b Removed utilstrencodings header - This appears to help with travis tests, for unknown reasons. c4dfc7a Fixed some minor code review issues 63c3580 Reverted locking change in miner. - This should have been done in the original PR but was overlooked. 4ab72de Fixed variable name to match common practice and bracket formatting 886a678 Improvements to vote conversion code - Replaced redundantly defined function with inclusion of governance-vote.h - Replaced magic numbers with their corresponding constant symbols 0a37966 Reordered governance message handling
2016-08-17 09:08:25 +02:00
#include "util.h"
#include "core_io.h"
2015-04-16 21:58:09 +02:00
#include "key.h"
#include "main.h"
#include "masternode.h"
#include "utilstrencodings.h"
#include <boost/lexical_cast.hpp>
2015-04-16 21:58:09 +02:00
using namespace std;
//! minimum peer version that can receive and send masternode payment messages,
// vote for masternode winner and be elected as a winner
// V1 - Last protocol version before update
// V2 - Newest protocol version
static const int MIN_MASTERNODE_PAYMENT_PROTO_VERSION_1 = 70103;
static const int MIN_MASTERNODE_PAYMENT_PROTO_VERSION_2 = 70201;
extern CCriticalSection cs_vecPayments;
extern CCriticalSection cs_mapMasternodeBlocks;
extern CCriticalSection cs_mapMasternodePayeeVotes;
2015-04-16 21:58:09 +02:00
class CMasternodePayments;
class CMasternodePaymentWinner;
class CMasternodeBlockPayees;
2015-04-16 21:58:09 +02:00
extern CMasternodePayments mnpayments;
#define MNPAYMENTS_SIGNATURES_REQUIRED 6
#define MNPAYMENTS_SIGNATURES_TOTAL 10
2015-04-16 21:58:09 +02:00
void ProcessMessage(CNode* pfrom, std::string& strCommand, CDataStream& vRecv);
bool IsReferenceNode(CTxIn& vin);
/// TODO: all 4 functions do not belong here really, they should be refactored/moved somewhere (main.cpp ?)
bool IsBlockValueValid(const CBlock& block, int nBlockHeight, CAmount blockReward);
bool IsBlockPayeeValid(const CTransaction& txNew, int nBlockHeight, CAmount blockReward);
void FillBlockPayments(CMutableTransaction& txNew, int nBlockHeight, CAmount blockReward, CTxOut& txoutMasternodeRet, std::vector<CTxOut>& voutSuperblockRet);
std::string GetRequiredPaymentsString(int nBlockHeight);
class CMasternodePayee
{
public:
CScript scriptPubKey;
int nVotes;
CMasternodePayee() {
scriptPubKey = CScript();
nVotes = 0;
}
2015-08-30 01:48:19 +02:00
CMasternodePayee(CScript payee, int nVotesIn) {
scriptPubKey = payee;
2015-06-22 16:20:34 +02:00
nVotes = nVotesIn;
}
ADD_SERIALIZE_METHODS;
template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
READWRITE(*(CScriptBase*)(&scriptPubKey));
READWRITE(nVotes);
}
};
// Keep track of votes for payees from masternodes
class CMasternodeBlockPayees
{
public:
int nBlockHeight;
std::vector<CMasternodePayee> vecPayments;
CMasternodeBlockPayees(){
nBlockHeight = 0;
vecPayments.clear();
}
CMasternodeBlockPayees(int nBlockHeightIn) {
nBlockHeight = nBlockHeightIn;
vecPayments.clear();
}
2015-08-30 01:48:19 +02:00
void AddPayee(CScript payeeIn, int nIncrement){
LOCK(cs_vecPayments);
BOOST_FOREACH(CMasternodePayee& payee, vecPayments){
if(payee.scriptPubKey == payeeIn) {
payee.nVotes += nIncrement;
return;
}
}
2015-08-30 01:48:19 +02:00
CMasternodePayee c(payeeIn, nIncrement);
vecPayments.push_back(c);
}
2015-04-16 21:58:09 +02:00
bool GetPayee(CScript& payee)
{
LOCK(cs_vecPayments);
int nVotes = -1;
BOOST_FOREACH(CMasternodePayee& p, vecPayments){
if(p.nVotes > nVotes){
payee = p.scriptPubKey;
nVotes = p.nVotes;
}
}
return (nVotes > -1);
}
bool HasPayeeWithVotes(CScript payee, int nVotesReq)
{
LOCK(cs_vecPayments);
BOOST_FOREACH(CMasternodePayee& p, vecPayments){
if(p.nVotes >= nVotesReq && p.scriptPubKey == payee) return true;
}
return false;
}
bool IsTransactionValid(const CTransaction& txNew);
std::string GetRequiredPaymentsString();
ADD_SERIALIZE_METHODS;
template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
READWRITE(nBlockHeight);
READWRITE(vecPayments);
}
};
2015-04-16 21:58:09 +02:00
// for storing the winning payments
class CMasternodePaymentWinner
{
public:
CTxIn vinMasternode;
2015-04-16 21:58:09 +02:00
int nBlockHeight;
CScript payee;
2015-04-16 21:58:09 +02:00
std::vector<unsigned char> vchSig;
CMasternodePaymentWinner() {
nBlockHeight = 0;
vinMasternode = CTxIn();
payee = CScript();
}
CMasternodePaymentWinner(CTxIn vinIn) {
nBlockHeight = 0;
vinMasternode = vinIn;
payee = CScript();
2015-04-16 21:58:09 +02:00
}
uint256 GetHash(){
2015-05-28 00:07:53 +02:00
CHashWriter ss(SER_GETHASH, PROTOCOL_VERSION);
ss << *(CScriptBase*)(&payee);
ss << nBlockHeight;
ss << vinMasternode.prevout;
2015-05-28 00:07:53 +02:00
return ss.GetHash();
}
2015-04-16 21:58:09 +02:00
bool Sign(CKey& keyMasternode, CPubKey& pubKeyMasternode);
bool IsValid(CNode* pnode, int nValidationHeight, std::string& strError);
bool SignatureValid();
void Relay();
void AddPayee(CScript payeeIn){
payee = payeeIn;
2015-04-16 21:58:09 +02:00
}
2015-07-24 16:12:48 +02:00
2015-04-16 21:58:09 +02:00
ADD_SERIALIZE_METHODS;
template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
READWRITE(vinMasternode);
2015-04-16 21:58:09 +02:00
READWRITE(nBlockHeight);
READWRITE(*(CScriptBase*)(&payee));
2015-04-16 21:58:09 +02:00
READWRITE(vchSig);
}
std::string ToString()
{
std::string ret = "";
ret += vinMasternode.ToString();
ret += ", " + boost::lexical_cast<std::string>(nBlockHeight);
ret += ", " + ScriptToAsmStr(payee);
ret += ", " + boost::lexical_cast<std::string>((int)vchSig.size());
return ret;
}
2015-04-16 21:58:09 +02:00
};
//
// Masternode Payments Class
// Keeps track of who should get paid for which blocks
//
class CMasternodePayments
{
private:
const int nMinBlocksToStore;
const float nStorageCoeff;
2016-05-29 20:35:09 +02:00
// Keep track of current block index
const CBlockIndex *pCurrentBlockIndex;
2015-04-16 21:58:09 +02:00
public:
std::map<uint256, CMasternodePaymentWinner> mapMasternodePayeeVotes;
std::map<int, CMasternodeBlockPayees> mapMasternodeBlocks;
std::map<uint256, int> mapMasternodesLastVote; //Hash(BEGIN(prevout.hash), END(prevout.n)), nBlockHeight
2015-04-16 21:58:09 +02:00
CMasternodePayments() : nMinBlocksToStore(4000), nStorageCoeff(1.25) {
2015-04-16 21:58:09 +02:00
}
void Clear() {
LOCK2(cs_mapMasternodeBlocks, cs_mapMasternodePayeeVotes);
mapMasternodeBlocks.clear();
mapMasternodePayeeVotes.clear();
}
2015-08-30 01:48:19 +02:00
bool AddWinningMasternode(CMasternodePaymentWinner& winner);
2015-04-16 21:58:09 +02:00
bool ProcessBlock(int nBlockHeight);
void Sync(CNode* node, int nCountNeeded);
2016-04-13 19:49:47 +02:00
void CheckAndRemove();
2015-04-16 21:58:09 +02:00
int LastPayment(CMasternode& mn);
bool GetBlockPayee(int nBlockHeight, CScript& payee);
bool IsTransactionValid(const CTransaction& txNew, int nBlockHeight);
bool IsScheduled(CMasternode& mn, int nNotBlockHeight);
2015-07-24 16:12:48 +02:00
bool CanVote(COutPoint outMasternode, int nBlockHeight) {
LOCK(cs_mapMasternodePayeeVotes);
if(mapMasternodesLastVote.count(Hash(BEGIN(outMasternode.hash), END(outMasternode.n)))) {
if(mapMasternodesLastVote[Hash(BEGIN(outMasternode.hash), END(outMasternode.n))] == nBlockHeight) {
2015-07-24 16:12:48 +02:00
return false;
}
}
//record this masternode voted
mapMasternodesLastVote[Hash(BEGIN(outMasternode.hash), END(outMasternode.n))] = nBlockHeight;
2015-07-24 16:12:48 +02:00
return true;
}
int GetMinMasternodePaymentsProto();
void ProcessMessage(CNode* pfrom, std::string& strCommand, CDataStream& vRecv);
std::string GetRequiredPaymentsString(int nBlockHeight);
void FillBlockPayee(CMutableTransaction& txNew, int nBlockHeight, CAmount blockReward, CTxOut& txoutMasternodeRet);
std::string ToString() const;
2015-07-22 00:14:54 +02:00
int GetOldestBlock();
int GetNewestBlock();
int GetBlockCount()
{
2016-02-04 23:48:23 +01:00
return mapMasternodeBlocks.size();
}
int GetVoteCount()
{
return mapMasternodePayeeVotes.size();
}
2016-05-29 20:35:09 +02:00
bool IsEnoughData(int nMnCount);
ADD_SERIALIZE_METHODS;
template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
READWRITE(mapMasternodePayeeVotes);
READWRITE(mapMasternodeBlocks);
}
void UpdatedBlockTip(const CBlockIndex *pindex);
2015-04-16 21:58:09 +02:00
};
#endif