2015-04-16 21:58:09 +02:00
|
|
|
|
|
|
|
|
|
|
|
// Copyright (c) 2014-2015 The Dash developers
|
|
|
|
// 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
|
|
|
|
|
|
|
|
#include "key.h"
|
|
|
|
#include "main.h"
|
|
|
|
#include "masternode.h"
|
2015-05-27 21:47:01 +02:00
|
|
|
#include <boost/lexical_cast.hpp>
|
2015-04-16 21:58:09 +02:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
class CMasternodePayments;
|
|
|
|
class CMasternodePaymentWinner;
|
2015-04-22 16:33:44 +02:00
|
|
|
class CMasternodeBlockPayees;
|
2015-04-16 21:58:09 +02:00
|
|
|
|
|
|
|
extern CMasternodePayments masternodePayments;
|
2015-04-22 16:33:44 +02:00
|
|
|
extern std::map<uint256, CMasternodePaymentWinner> mapMasternodePayeeVotes;
|
|
|
|
extern std::map<uint256, CMasternodeBlockPayees> mapMasternodeBlocks;
|
|
|
|
|
2015-05-04 17:04:09 +02:00
|
|
|
#define MNPAYMENTS_SIGNATURES_REQUIRED 6
|
2015-06-21 19:06:25 +02:00
|
|
|
#define MNPAYMENTS_SIGNATURES_TOTAL 10
|
2015-04-16 21:58:09 +02:00
|
|
|
|
|
|
|
void ProcessMessageMasternodePayments(CNode* pfrom, std::string& strCommand, CDataStream& vRecv);
|
2015-04-22 16:33:44 +02:00
|
|
|
bool IsReferenceNode(CTxIn& vin);
|
2015-07-17 17:07:07 +02:00
|
|
|
bool IsBlockPayeeValid(const CTransaction& txNew, int nBlockHeight);
|
|
|
|
std::string GetRequiredPaymentsString(int nBlockHeight);
|
2015-07-04 16:49:49 +02:00
|
|
|
bool IsBlockValueValid(const CBlock& block, int64_t nExpectedValue);
|
2015-05-30 19:27:51 +02:00
|
|
|
void FillBlockPayee(CMutableTransaction& txNew, int64_t nFees);
|
2015-04-22 16:33:44 +02:00
|
|
|
|
2015-06-25 20:08:50 +02:00
|
|
|
class CMasternodePayee
|
2015-04-22 16:33:44 +02:00
|
|
|
{
|
|
|
|
public:
|
2015-06-25 20:08:50 +02:00
|
|
|
CScript scriptPubKey;
|
2015-04-22 16:33:44 +02:00
|
|
|
int nVotes;
|
|
|
|
|
|
|
|
CMasternodePayee() {
|
|
|
|
scriptPubKey = CScript();
|
|
|
|
nVotes = 0;
|
2015-06-25 20:08:50 +02:00
|
|
|
}
|
2015-04-22 16:33:44 +02:00
|
|
|
|
2015-06-25 20:08:50 +02:00
|
|
|
CMasternodePayee(CScript payee, int nVotesIn) {
|
2015-04-22 16:33:44 +02:00
|
|
|
scriptPubKey = payee;
|
2015-06-22 16:20:34 +02:00
|
|
|
nVotes = nVotesIn;
|
2015-04-22 16:33:44 +02:00
|
|
|
}
|
2015-06-25 20:08:50 +02:00
|
|
|
|
|
|
|
ADD_SERIALIZE_METHODS;
|
|
|
|
|
|
|
|
template <typename Stream, typename Operation>
|
|
|
|
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
|
|
|
|
READWRITE(scriptPubKey);
|
|
|
|
READWRITE(nVotes);
|
|
|
|
}
|
2015-04-22 16:33:44 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// 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-06-25 20:08:50 +02:00
|
|
|
void AddPayee(CScript payeeIn, int nIncrement){
|
2015-04-22 16:33:44 +02:00
|
|
|
BOOST_FOREACH(CMasternodePayee& payee, vecPayments){
|
2015-06-25 20:08:50 +02:00
|
|
|
if(payee.scriptPubKey == payeeIn) {
|
2015-04-22 16:33:44 +02:00
|
|
|
payee.nVotes += nIncrement;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-25 20:08:50 +02:00
|
|
|
CMasternodePayee c(payeeIn, nIncrement);
|
2015-04-22 16:33:44 +02:00
|
|
|
vecPayments.push_back(c);
|
|
|
|
}
|
2015-04-16 21:58:09 +02:00
|
|
|
|
2015-04-22 16:33:44 +02:00
|
|
|
bool GetPayee(CScript& payee)
|
|
|
|
{
|
|
|
|
int nVotes = -1;
|
|
|
|
BOOST_FOREACH(CMasternodePayee& p, vecPayments){
|
|
|
|
if(p.nVotes > nVotes){
|
2015-06-25 20:08:50 +02:00
|
|
|
payee = p.scriptPubKey;
|
2015-04-22 16:33:44 +02:00
|
|
|
nVotes = p.nVotes;
|
|
|
|
}
|
2015-06-25 20:08:50 +02:00
|
|
|
}
|
2015-04-22 16:33:44 +02:00
|
|
|
|
2015-05-28 19:45:31 +02:00
|
|
|
return (nVotes > -1);
|
2015-04-22 16:33:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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:
|
2015-04-22 16:33:44 +02:00
|
|
|
CTxIn vinMasternode;
|
|
|
|
|
2015-04-16 21:58:09 +02:00
|
|
|
int nBlockHeight;
|
2015-06-25 20:08:50 +02:00
|
|
|
CScript payee;
|
2015-04-16 21:58:09 +02:00
|
|
|
std::vector<unsigned char> vchSig;
|
|
|
|
|
|
|
|
CMasternodePaymentWinner() {
|
|
|
|
nBlockHeight = 0;
|
2015-04-22 16:33:44 +02:00
|
|
|
vinMasternode = CTxIn();
|
2015-06-25 20:08:50 +02:00
|
|
|
payee = CScript();
|
2015-04-22 16:33:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
CMasternodePaymentWinner(CTxIn vinIn) {
|
|
|
|
nBlockHeight = 0;
|
|
|
|
vinMasternode = vinIn;
|
2015-06-25 20:08:50 +02:00
|
|
|
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 << payee;
|
2015-05-28 19:45:31 +02:00
|
|
|
ss << nBlockHeight;
|
|
|
|
ss << vinMasternode.prevout;
|
2015-05-28 00:07:53 +02:00
|
|
|
|
2015-06-25 20:08:50 +02:00
|
|
|
return ss.GetHash();
|
2015-04-22 16:33:44 +02:00
|
|
|
}
|
2015-04-16 21:58:09 +02:00
|
|
|
|
2015-04-22 16:33:44 +02:00
|
|
|
bool Sign(CKey& keyMasternode, CPubKey& pubKeyMasternode);
|
|
|
|
bool IsValid();
|
|
|
|
bool SignatureValid();
|
|
|
|
void Relay();
|
|
|
|
|
2015-06-25 20:08:50 +02:00
|
|
|
void AddPayee(CScript payeeIn){
|
|
|
|
payee = payeeIn;
|
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) {
|
2015-04-22 16:33:44 +02:00
|
|
|
READWRITE(vinMasternode);
|
2015-04-16 21:58:09 +02:00
|
|
|
READWRITE(nBlockHeight);
|
|
|
|
READWRITE(payee);
|
|
|
|
READWRITE(vchSig);
|
2015-05-27 21:47:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string ToString()
|
|
|
|
{
|
|
|
|
std::string ret = "";
|
|
|
|
ret += vinMasternode.ToString();
|
|
|
|
ret += ", " + boost::lexical_cast<std::string>(nBlockHeight);
|
|
|
|
ret += ", " + payee.ToString();
|
|
|
|
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:
|
|
|
|
int nSyncedFromPeer;
|
|
|
|
int nLastBlockHeight;
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
CMasternodePayments() {
|
2015-04-22 16:33:44 +02:00
|
|
|
nSyncedFromPeer = 0;
|
|
|
|
nLastBlockHeight = 0;
|
2015-04-16 21:58:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool AddWinningMasternode(CMasternodePaymentWinner& winner);
|
|
|
|
bool ProcessBlock(int nBlockHeight);
|
2015-06-25 20:08:50 +02:00
|
|
|
|
2015-04-16 21:58:09 +02:00
|
|
|
void Sync(CNode* node);
|
|
|
|
void CleanPaymentList();
|
|
|
|
int LastPayment(CMasternode& mn);
|
|
|
|
|
2015-04-22 16:33:44 +02:00
|
|
|
bool GetBlockPayee(int nBlockHeight, CScript& payee);
|
|
|
|
bool IsTransactionValid(const CTransaction& txNew, int nBlockHeight);
|
2015-06-15 02:05:51 +02:00
|
|
|
bool IsScheduled(CMasternode& mn, int nNotBlockHeight);
|
2015-04-22 16:33:44 +02:00
|
|
|
|
2015-07-08 03:57:32 +02:00
|
|
|
int GetMinMasternodePaymentsProto();
|
2015-04-22 16:33:44 +02:00
|
|
|
void ProcessMessageMasternodePayments(CNode* pfrom, std::string& strCommand, CDataStream& vRecv);
|
|
|
|
std::string GetRequiredPaymentsString(int nBlockHeight);
|
2015-05-30 19:27:51 +02:00
|
|
|
void FillBlockPayee(CMutableTransaction& txNew, int64_t nFees);
|
2015-04-22 16:33:44 +02:00
|
|
|
|
2015-04-16 21:58:09 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2015-07-08 03:57:32 +02:00
|
|
|
#endif
|