dash/src/masternode-payments.h

211 lines
5.3 KiB
C
Raw Normal View History

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"
#include <boost/lexical_cast.hpp>
2015-04-16 21:58:09 +02:00
using namespace std;
class CMasternodePayments;
class CMasternodePaymentWinner;
class CMasternodeBlockPayees;
2015-04-16 21:58:09 +02:00
extern CMasternodePayments masternodePayments;
extern std::map<uint256, CMasternodePaymentWinner> mapMasternodePayeeVotes;
extern std::map<uint256, CMasternodeBlockPayees> mapMasternodeBlocks;
#define MNPAYMENTS_SIGNATURES_REQUIRED 6
#define MNPAYMENTS_SIGNATURES_TOTAL 10
2015-04-16 21:58:09 +02:00
void ProcessMessageMasternodePayments(CNode* pfrom, std::string& strCommand, CDataStream& vRecv);
bool IsReferenceNode(CTxIn& vin);
bool IsBlockPayeeValid(const CTransaction& txNew, int nBlockHeight);
std::string GetRequiredPaymentsString(int nBlockHeight);
bool IsBlockValueValid(const CBlock& block, int64_t nExpectedValue);
void FillBlockPayee(CMutableTransaction& txNew, int64_t nFees);
class CMasternodePayee
{
public:
CScript scriptPubKey;
int nVotes;
CMasternodePayee() {
scriptPubKey = CScript();
nVotes = 0;
}
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(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();
}
void AddPayee(CScript payeeIn, int nIncrement){
BOOST_FOREACH(CMasternodePayee& payee, vecPayments){
if(payee.scriptPubKey == payeeIn) {
payee.nVotes += nIncrement;
return;
}
}
CMasternodePayee c(payeeIn, nIncrement);
vecPayments.push_back(c);
}
2015-04-16 21:58:09 +02:00
bool GetPayee(CScript& payee)
{
int nVotes = -1;
BOOST_FOREACH(CMasternodePayee& p, vecPayments){
if(p.nVotes > nVotes){
payee = p.scriptPubKey;
nVotes = p.nVotes;
}
}
return (nVotes > -1);
}
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 << 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();
bool SignatureValid();
void Relay();
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) {
READWRITE(vinMasternode);
2015-04-16 21:58:09 +02:00
READWRITE(nBlockHeight);
READWRITE(payee);
READWRITE(vchSig);
}
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() {
nSyncedFromPeer = 0;
nLastBlockHeight = 0;
2015-04-16 21:58:09 +02:00
}
bool AddWinningMasternode(CMasternodePaymentWinner& winner);
bool ProcessBlock(int nBlockHeight);
2015-04-16 21:58:09 +02:00
void Sync(CNode* node);
void CleanPaymentList();
int LastPayment(CMasternode& mn);
bool GetBlockPayee(int nBlockHeight, CScript& payee);
bool IsTransactionValid(const CTransaction& txNew, int nBlockHeight);
bool IsScheduled(CMasternode& mn, int nNotBlockHeight);
int GetMinMasternodePaymentsProto();
void ProcessMessageMasternodePayments(CNode* pfrom, std::string& strCommand, CDataStream& vRecv);
std::string GetRequiredPaymentsString(int nBlockHeight);
void FillBlockPayee(CMutableTransaction& txNew, int64_t nFees);
2015-04-16 21:58:09 +02:00
};
#endif