neobytes/src/masternode-payments.h

225 lines
7.0 KiB
C
Raw Normal View History

2016-12-20 14:26:45 +01:00
// Copyright (c) 2014-2017 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.
2015-04-16 21:58:09 +02:00
#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 "masternode.h"
#include "net_processing.h"
#include "utilstrencodings.h"
class CMasternodePayments;
class CMasternodePaymentVote;
class CMasternodeBlockPayees;
2015-04-16 21:58:09 +02:00
static const int MNPAYMENTS_SIGNATURES_REQUIRED = 6;
static const int MNPAYMENTS_SIGNATURES_TOTAL = 10;
2015-04-16 21:58:09 +02:00
//! minimum peer version that can receive and send masternode payment messages,
// vote for masternode and be elected as a payment winner
// V1 - Last protocol version before update
// V2 - Newest protocol version
static const int MIN_MASTERNODE_PAYMENT_PROTO_VERSION_1 = 70210;
2018-06-12 13:33:41 +02:00
static const int MIN_MASTERNODE_PAYMENT_PROTO_VERSION_2 = 70210;
2016-09-12 17:34:11 +02:00
extern CCriticalSection cs_vecPayees;
extern CCriticalSection cs_mapMasternodeBlocks;
extern CMasternodePayments mnpayments;
/// 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, std::string& strErrorRet);
bool IsBlockPayeeValid(const CTransaction& txNew, int nBlockHeight, CAmount blockReward);
void FillBlockPayments(CMutableTransaction& txNew, int nBlockHeight, CAmount blockReward, std::vector<CTxOut>& voutMasternodePaymentsRet, std::vector<CTxOut>& voutSuperblockPaymentsRet);
std::map<int, std::string> GetRequiredPaymentsStrings(int nStartHeight, int nEndHeight);
class CMasternodePayee
{
private:
CScript scriptPubKey;
std::vector<uint256> vecVoteHashes;
public:
CMasternodePayee() :
scriptPubKey(),
vecVoteHashes()
{}
CMasternodePayee(CScript payee, uint256 hashIn) :
scriptPubKey(payee),
vecVoteHashes()
{
vecVoteHashes.push_back(hashIn);
}
ADD_SERIALIZE_METHODS;
template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action) {
READWRITE(*(CScriptBase*)(&scriptPubKey));
READWRITE(vecVoteHashes);
}
CScript GetPayee() const { return scriptPubKey; }
void AddVoteHash(uint256 hashIn) { vecVoteHashes.push_back(hashIn); }
std::vector<uint256> GetVoteHashes() const { return vecVoteHashes; }
int GetVoteCount() const { return vecVoteHashes.size(); }
};
// Keep track of votes for payees from masternodes
class CMasternodeBlockPayees
{
public:
int nBlockHeight;
2016-09-12 17:34:11 +02:00
std::vector<CMasternodePayee> vecPayees;
CMasternodeBlockPayees() :
nBlockHeight(0),
vecPayees()
{}
CMasternodeBlockPayees(int nBlockHeightIn) :
nBlockHeight(nBlockHeightIn),
vecPayees()
{}
2016-09-12 17:34:11 +02:00
ADD_SERIALIZE_METHODS;
2016-09-12 17:34:11 +02:00
template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action) {
2016-09-12 17:34:11 +02:00
READWRITE(nBlockHeight);
READWRITE(vecPayees);
}
void AddPayee(const CMasternodePaymentVote& vote);
bool GetBestPayee(CScript& payeeRet) const;
bool HasPayeeWithVotes(const CScript& payeeIn, int nVotesReq) const;
bool IsTransactionValid(const CTransaction& txNew) const;
std::string GetRequiredPaymentsString() const;
};
2015-04-16 21:58:09 +02:00
// vote for the winning payment
class CMasternodePaymentVote
2015-04-16 21:58:09 +02:00
{
public:
COutPoint masternodeOutpoint;
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;
CMasternodePaymentVote() :
masternodeOutpoint(),
nBlockHeight(0),
payee(),
vchSig()
{}
2015-04-16 21:58:09 +02:00
CMasternodePaymentVote(COutPoint outpoint, int nBlockHeight, CScript payee) :
masternodeOutpoint(outpoint),
nBlockHeight(nBlockHeight),
payee(payee),
vchSig()
{}
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) {
READWRITE(masternodeOutpoint);
2015-04-16 21:58:09 +02:00
READWRITE(nBlockHeight);
READWRITE(*(CScriptBase*)(&payee));
if (!(s.GetType() & SER_GETHASH)) {
READWRITE(vchSig);
}
}
uint256 GetHash() const;
uint256 GetSignatureHash() const;
bool Sign();
bool CheckSignature(const CKeyID& keyIDOperator, int nValidationHeight, int &nDos) const;
bool IsValid(CNode* pnode, int nValidationHeight, std::string& strError, CConnman& connman) const;
void Relay(CConnman& connman) const;
bool IsVerified() const { return !vchSig.empty(); }
void MarkAsNotVerified() { vchSig.clear(); }
std::string ToString() const;
2015-04-16 21:58:09 +02:00
};
//
// Masternode Payments Class
// Keeps track of who should get paid for which blocks
//
class CMasternodePayments
{
private:
// masternode count times nStorageCoeff payments blocks should be stored ...
const float nStorageCoeff;
// ... but at least nMinBlocksToStore (payments blocks)
const int nMinBlocksToStore;
2016-05-29 20:35:09 +02:00
// Keep track of current block height
int nCachedBlockHeight;
2015-04-16 21:58:09 +02:00
public:
std::map<uint256, CMasternodePaymentVote> mapMasternodePaymentVotes;
std::map<int, CMasternodeBlockPayees> mapMasternodeBlocks;
std::map<COutPoint, int> mapMasternodesLastVote;
std::map<COutPoint, int> mapMasternodesDidNotVote;
2015-04-16 21:58:09 +02:00
CMasternodePayments() : nStorageCoeff(1.25), nMinBlocksToStore(6000) {}
2015-04-16 21:58:09 +02:00
ADD_SERIALIZE_METHODS;
template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action) {
READWRITE(mapMasternodePaymentVotes);
READWRITE(mapMasternodeBlocks);
}
void Clear();
bool AddOrUpdatePaymentVote(const CMasternodePaymentVote& vote);
bool HasVerifiedPaymentVote(const uint256& hashIn) const;
Eliminate remaining uses of g_connman in Dash-specific code. (#1635) This monstrous change eliminates all remaining uses of g_connman global variable in Dash-specific code. Unlike previous changes eliminating g_connman use that were isolated to particular modules, this one covers multiple modules simultaneously because they are so interdependent that change in one module was quickly spreading to others. This is mostly invariant change that was done by * changing all functions using g_connman to use connman argument, * changing all functions calling these functions to use connman argument, * repeating previous step until there's nothing to change. After multiple iterations, this process converged to final result, producing code that is mostly equivalent to original one, but passing CConnman instance through arguments instead of global variable. The only exception to equivalence of resulting code is that I had to create overload of CMasternodeMan::CheckAndRemove() method without arguments that does nothing just for use in CFlatDB<CMasternodeMan>::Dump() and CFlatDB<CMasternodeMan>::Load() methods. Normal CMasternodeMan::CheckAndRemove() overload now has argument of CConnman& type and is used everywhere else. The normal overload has this code in the beginning: if(!masternodeSync.IsMasternodeListSynced()) return; Masternode list is not synced yet when we load "mncache.dat" file, and we save "mncache.dat" file on shutdown, so I presume that it's OK to use overload that does nothing in both cases. Signed-off-by: Oleg Girko <ol@infoserver.lv>
2017-09-19 16:51:38 +02:00
bool ProcessBlock(int nBlockHeight, CConnman& connman);
void CheckBlockVotes(int nBlockHeight);
void Sync(CNode* node, CConnman& connman) const;
void RequestLowDataPaymentBlocks(CNode* pnode, CConnman& connman) const;
2016-04-13 19:49:47 +02:00
void CheckAndRemove();
2015-04-16 21:58:09 +02:00
bool GetBlockTxOuts(int nBlockHeight, CAmount blockReward, std::vector<CTxOut>& voutMasternodePaymentsRet) const;
bool IsTransactionValid(const CTransaction& txNew, int nBlockHeight, CAmount blockReward) const;
bool IsScheduled(const masternode_info_t& mnInfo, int nNotBlockHeight) const;
bool UpdateLastVote(const CMasternodePaymentVote& vote);
2015-07-24 16:12:48 +02:00
int GetMinMasternodePaymentsProto() const;
void ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStream& vRecv, CConnman& connman);
std::string GetRequiredPaymentsString(int nBlockHeight) const;
bool GetMasternodeTxOuts(int nBlockHeight, CAmount blockReward, std::vector<CTxOut>& voutMasternodePaymentsRet) const;
std::string ToString() const;
int GetBlockCount() const { return mapMasternodeBlocks.size(); }
int GetVoteCount() const { return mapMasternodePaymentVotes.size(); }
bool IsEnoughData() const;
int GetStorageLimit() const;
2016-05-29 20:35:09 +02:00
Eliminate remaining uses of g_connman in Dash-specific code. (#1635) This monstrous change eliminates all remaining uses of g_connman global variable in Dash-specific code. Unlike previous changes eliminating g_connman use that were isolated to particular modules, this one covers multiple modules simultaneously because they are so interdependent that change in one module was quickly spreading to others. This is mostly invariant change that was done by * changing all functions using g_connman to use connman argument, * changing all functions calling these functions to use connman argument, * repeating previous step until there's nothing to change. After multiple iterations, this process converged to final result, producing code that is mostly equivalent to original one, but passing CConnman instance through arguments instead of global variable. The only exception to equivalence of resulting code is that I had to create overload of CMasternodeMan::CheckAndRemove() method without arguments that does nothing just for use in CFlatDB<CMasternodeMan>::Dump() and CFlatDB<CMasternodeMan>::Load() methods. Normal CMasternodeMan::CheckAndRemove() overload now has argument of CConnman& type and is used everywhere else. The normal overload has this code in the beginning: if(!masternodeSync.IsMasternodeListSynced()) return; Masternode list is not synced yet when we load "mncache.dat" file, and we save "mncache.dat" file on shutdown, so I presume that it's OK to use overload that does nothing in both cases. Signed-off-by: Oleg Girko <ol@infoserver.lv>
2017-09-19 16:51:38 +02:00
void UpdatedBlockTip(const CBlockIndex *pindex, CConnman& connman);
void DoMaintenance();
2015-04-16 21:58:09 +02:00
};
#endif