dash/src/masternode-payments.cpp

447 lines
14 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.
#include "masternode-payments.h"
#include "masternode-budget.h"
2015-04-16 21:58:09 +02:00
#include "masternodeman.h"
#include "darksend.h"
#include "util.h"
#include "sync.h"
#include "addrman.h"
#include <boost/lexical_cast.hpp>
CCriticalSection cs_masternodepayments;
/** Object for who's going to get paid on which blocks */
CMasternodePayments masternodePayments;
map<uint256, CMasternodePaymentWinner> mapMasternodePayeeVotes;
map<uint256, CMasternodeBlockPayees> mapMasternodeBlocks;
2015-04-16 21:58:09 +02:00
bool IsBlockPayeeValid(const CTransaction& txNew, int64_t nBlockHeight)
{
//check if it's a budget block
if(budget.IsBudgetBlock(nBlockHeight)){
if(budget.IsTransactionValid(txNew, nBlockHeight)){
return true;
}
}
//check for masternode payee
if(masternodePayments.IsTransactionValid(txNew, nBlockHeight))
{
return true;
}
return false;
}
void CMasternodePayments::ProcessMessageMasternodePayments(CNode* pfrom, std::string& strCommand, CDataStream& vRecv)
2015-04-16 21:58:09 +02:00
{
if(IsInitialBlockDownload()) return;
if (strCommand == "mnget") { //Masternode Payments Request Sync
if(fLiteMode) return; //disable all Darksend/Masternode related functionality
if(pfrom->HasFulfilledRequest("mnget")) {
LogPrintf("mnget - peer already asked me for the list\n");
Misbehaving(pfrom->GetId(), 20);
return;
}
pfrom->FulfilledRequest("mnget");
masternodePayments.Sync(pfrom);
LogPrintf("mnget - Sent Masternode winners to %s\n", pfrom->addr.ToString().c_str());
}
else if (strCommand == "mnw") { //Masternode Payments Declare Winner
LOCK(cs_masternodepayments);
//this is required in litemode
CMasternodePaymentWinner winner;
vRecv >> winner;
if(chainActive.Tip() == NULL) return;
if(mapMasternodePayeeVotes.count(winner.GetHash())){
if(fDebug) LogPrintf("mnw - Already seen - %s bestHeight %d\n", winner.GetHash().ToString().c_str(), chainActive.Tip()->nHeight);
return;
2015-04-16 21:58:09 +02:00
}
if(winner.nBlockHeight < chainActive.Tip()->nHeight - 10 || winner.nBlockHeight > chainActive.Tip()->nHeight+20){
LogPrintf("mnw - winner out of range - Height %d bestHeight %d\n", winner.nBlockHeight, chainActive.Tip()->nHeight);
2015-04-16 21:58:09 +02:00
return;
}
if(!winner.IsValid()){
LogPrintf("mnw - invalid message\n");
2015-04-16 21:58:09 +02:00
return;
}
if(!winner.SignatureValid()){
2015-04-16 21:58:09 +02:00
LogPrintf("mnw - invalid signature\n");
Misbehaving(pfrom->GetId(), 100);
return;
}
CTxDestination address1;
ExtractDestination(winner.payee.scriptPubKey, address1);
CBitcoinAddress address2(address1);
if(fDebug) LogPrintf("mnw - winning vote - Addr %s Height %d bestHeight %d\n", address2.ToString().c_str(), winner.nBlockHeight, chainActive.Tip()->nHeight);
2015-04-16 21:58:09 +02:00
if(masternodePayments.AddWinningMasternode(winner)){
winner.Relay();
2015-04-16 21:58:09 +02:00
}
}
}
bool CMasternodePaymentWinner::Sign(CKey& keyMasternode, CPubKey& pubKeyMasternode)
2015-04-16 21:58:09 +02:00
{
std::string errorMessage;
std::string strMasterNodeSignMessage;
std::string strMessage = vinMasternode.prevout.ToStringShort() +
boost::lexical_cast<std::string>(nBlockHeight) +
payee.ToString();
if(!darkSendSigner.SignMessage(strMessage, errorMessage, vchSig, keyMasternode)) {
LogPrintf("CMasternodePing::Sign() - Error: %s\n", errorMessage.c_str());
return false;
}
2015-04-16 21:58:09 +02:00
if(!darkSendSigner.VerifyMessage(pubKeyMasternode, vchSig, strMessage, errorMessage)) {
LogPrintf("CMasternodePing::Sign() - Error: %s\n", errorMessage.c_str());
2015-04-16 21:58:09 +02:00
return false;
}
return true;
}
bool CMasternodePayments::GetBlockPayee(int nBlockHeight, CScript& payee)
2015-04-16 21:58:09 +02:00
{
if(!mapMasternodeBlocks.count(nBlockHeight)){
return mapMasternodeBlocks[nBlockHeight].GetPayee(payee);
}
2015-04-16 21:58:09 +02:00
return false;
}
2015-04-16 21:58:09 +02:00
bool CMasternodePayments::AddWinningMasternode(CMasternodePaymentWinner& winnerIn)
{
uint256 blockHash = 0;
if(!GetBlockHash(blockHash, winnerIn.nBlockHeight-576)) {
2015-04-16 21:58:09 +02:00
return false;
}
if(mapMasternodePayeeVotes.count(winnerIn.GetHash())){
return false;
2015-04-16 21:58:09 +02:00
}
mapMasternodePayeeVotes[winnerIn.GetHash()] = winnerIn;
if(!mapMasternodeBlocks.count(winnerIn.nBlockHeight)){
CMasternodeBlockPayees blockPayees;
mapMasternodeBlocks[winnerIn.nBlockHeight] = blockPayees;
2015-04-16 21:58:09 +02:00
}
int n = 1;
if(IsReferenceNode(winnerIn.vinMasternode)) n = 100;
mapMasternodeBlocks[winnerIn.nBlockHeight].AddPayee(winnerIn.payee.scriptPubKey, winnerIn.payee.nValue, n);
2015-04-16 21:58:09 +02:00
return true;
}
bool CMasternodeBlockPayees::IsTransactionValid(const CTransaction& txNew)
2015-04-16 21:58:09 +02:00
{
int nMaxSignatures = 0;
std::string strPayeesPossible = "";
//require at least 6 signatures
BOOST_FOREACH(CMasternodePayee& payee, vecPayments)
2015-05-16 04:53:53 +02:00
if(payee.nVotes >= nMaxSignatures && payee.nVotes >= MNPAYMENTS_SIGNATURES_REQUIRED)
nMaxSignatures = payee.nVotes;
// if we don't have at least 6 signatures on a payee, approve whichever is the longest chain
if(nMaxSignatures == 0) return true;
BOOST_FOREACH(CMasternodePayee& payee, vecPayments)
{
bool found = false;
BOOST_FOREACH(CTxOut out, txNew.vout)
if(payee.scriptPubKey == out.scriptPubKey && payee.nValue == out.nValue)
found = true;
2015-04-16 21:58:09 +02:00
//if 2 contenders are within +/- 20%, take either
// If nMaxSignatures == 6, the min signatures = 2
// If nMaxSignatures == 10, the min signatures = 6
// If nMaxSignatures == 15, the min signatures = 11
if(payee.nVotes >= nMaxSignatures-(MNPAYMENTS_SIGNATURES_TOTAL/5)){
if(found) return true;
2015-04-16 21:58:09 +02:00
CTxDestination address1;
ExtractDestination(payee.scriptPubKey, address1);
CBitcoinAddress address2(address1);
2015-04-16 21:58:09 +02:00
if(strPayeesPossible == ""){
strPayeesPossible += address2.ToString() + ":" + boost::lexical_cast<std::string>(payee.nValue);
} else {
strPayeesPossible += "," + address2.ToString() + ":" + boost::lexical_cast<std::string>(payee.nValue);
}
2015-04-16 21:58:09 +02:00
}
}
LogPrintf("CMasternodePayments::IsTransactionValid - Missing required payment - %s\n", strPayeesPossible.c_str());
return false;
2015-04-16 21:58:09 +02:00
}
std::string CMasternodeBlockPayees::GetRequiredPaymentsString()
2015-04-16 21:58:09 +02:00
{
std::string ret = "Unknown";
BOOST_FOREACH(CMasternodePayee& payee, vecPayments)
{
if(payee.nVotes >= MNPAYMENTS_SIGNATURES_REQUIRED){
CTxDestination address1;
ExtractDestination(payee.scriptPubKey, address1);
CBitcoinAddress address2(address1);
if(ret != "Unknown"){
ret += ", " + address2.ToString() + ":" + boost::lexical_cast<std::string>(payee.nValue);
} else {
ret = address2.ToString() + ":" + boost::lexical_cast<std::string>(payee.nValue);
}
2015-04-16 21:58:09 +02:00
}
}
return ret;
2015-04-16 21:58:09 +02:00
}
std::string CMasternodePayments::GetRequiredPaymentsString(int nBlockHeight)
2015-04-16 21:58:09 +02:00
{
if(mapMasternodeBlocks.count(nBlockHeight)){
return mapMasternodeBlocks[nBlockHeight].GetRequiredPaymentsString();
2015-04-16 21:58:09 +02:00
}
return "Unknown";
}
2015-04-16 21:58:09 +02:00
bool CMasternodePayments::IsTransactionValid(const CTransaction& txNew, int nBlockHeight)
{
if(mapMasternodeBlocks.count(nBlockHeight)){
return mapMasternodeBlocks[nBlockHeight].IsTransactionValid(txNew);
2015-04-16 21:58:09 +02:00
}
return true;
2015-04-16 21:58:09 +02:00
}
void CMasternodePayments::CleanPaymentList()
{
LOCK(cs_masternodepayments);
if(chainActive.Tip() == NULL) return;
int nLimit = std::max(((int)mnodeman.size())*2, 1000);
std::map<uint256, CMasternodePaymentWinner>::iterator it = mapMasternodePayeeVotes.begin();
while(it != mapMasternodePayeeVotes.end()) {
CMasternodePaymentWinner winner = (*it).second;
if(chainActive.Tip()->nHeight - winner.nBlockHeight > nLimit){
if(fDebug) LogPrintf("CMasternodePayments::CleanPaymentList - Removing old Masternode payment - block %d\n", winner.nBlockHeight);
mapMasternodePayeeVotes.erase(it++);
} else {
++it;
2015-04-16 21:58:09 +02:00
}
}
2015-04-16 21:58:09 +02:00
}
bool IsReferenceNode(CTxIn& vin)
2015-04-16 21:58:09 +02:00
{
//reference node - hybrid mode
if(vin.prevout.ToStringShort() == "099c01bea63abd1692f60806bb646fa1d288e2d049281225f17e499024084e28-0") return true; // mainnet
if(vin.prevout.ToStringShort() == "testnet-0") return true; // testnet
if(vin.prevout.ToStringShort() == "regtest-0") return true; // regtest
2015-04-16 21:58:09 +02:00
return false;
}
2015-04-16 21:58:09 +02:00
bool CMasternodePaymentWinner::IsValid()
{
if(IsReferenceNode(vinMasternode)) return true;
2015-04-16 21:58:09 +02:00
int n = mnodeman.GetMasternodeRank(vinMasternode, nBlockHeight, MIN_MNPAYMENTS_PROTO_VERSION);
2015-04-16 21:58:09 +02:00
if(n == -1)
2015-04-16 21:58:09 +02:00
{
if(fDebug) LogPrintf("CMasternodePaymentWinner::IsValid - Unknown Masternode\n");
return false;
2015-04-16 21:58:09 +02:00
}
if(n > MNPAYMENTS_SIGNATURES_TOTAL)
2015-04-16 21:58:09 +02:00
{
if(fDebug) LogPrintf("CMasternodePaymentWinner::IsValid - Masternode not in the top %d (%d)\n", MNPAYMENTS_SIGNATURES_TOTAL, n);
return false;
}
return true;
}
bool CMasternodePayments::ProcessBlock(int nBlockHeight)
{
LOCK(cs_masternodepayments);
2015-04-16 21:58:09 +02:00
if(!fMasterNode) return false;
//reference node - hybrid mode
if(!IsReferenceNode(activeMasternode.vin)){
int n = mnodeman.GetMasternodeRank(activeMasternode.vin, nBlockHeight, MIN_MNPAYMENTS_PROTO_VERSION);
if(n == -1)
{
if(fDebug) LogPrintf("CMasternodePayments::ProcessBlock - Unknown Masternode\n");
return false;
2015-04-16 21:58:09 +02:00
}
if(n > MNPAYMENTS_SIGNATURES_TOTAL)
{
if(fDebug) LogPrintf("CMasternodePayments::ProcessBlock - Masternode not in the top %d (%d)\n", MNPAYMENTS_SIGNATURES_TOTAL, n);
return false;
}
2015-04-16 21:58:09 +02:00
}
if(nBlockHeight <= nLastBlockHeight) return false;
CBlockIndex* pindexPrev = chainActive.Tip();
if(pindexPrev == NULL) return false;
CAmount blockValue = GetBlockValue(pindexPrev->nBits, pindexPrev->nHeight, 0);
CAmount masternodePayment = GetMasternodePayment(pindexPrev->nHeight+1, blockValue);
CMasternodePaymentWinner newWinner(activeMasternode.vin);
2015-04-16 21:58:09 +02:00
if(budget.IsBudgetPaymentBlock(nBlockHeight)){
//is budget payment block
CScript payee;
GetMasternodeBudgetEscrow(payee);
newWinner.AddPayee(payee, masternodePayment);
} else {
CScript payeeSource;
uint256 hash;
if(!GetBlockHash(hash, nBlockHeight-10)) return false;
unsigned int nHash;
memcpy(&nHash, &hash, 2);
LogPrintf(" ProcessBlock Start nHeight %d. \n", nBlockHeight);
// pay to the oldest MN that still had no payment but its input is old enough and it was active long enough
CMasternode *pmn = mnodeman.GetNextMasternodeInQueueForPayment();
if(pmn != NULL)
{
LogPrintf(" Found by FindOldestNotInVec \n");
2015-04-16 21:58:09 +02:00
newWinner.nBlockHeight = nBlockHeight;
2015-04-16 21:58:09 +02:00
CScript payee;
if(pmn->donationPercentage > 0 && (nHash % 100) <= (unsigned int)pmn->donationPercentage) {
payee = pmn->donationAddress;
} else {
payee = GetScriptForDestination(pmn->pubkey.GetID());
}
2015-04-16 21:58:09 +02:00
payeeSource = GetScriptForDestination(pmn->pubkey.GetID());
newWinner.AddPayee(payee, masternodePayment);
CTxDestination address1;
ExtractDestination(payee, address1);
CBitcoinAddress address2(address1);
CTxDestination address3;
ExtractDestination(payeeSource, address3);
CBitcoinAddress address4(address3);
LogPrintf("Winner payee %s nHeight %d vin source %s. \n", address2.ToString().c_str(), newWinner.nBlockHeight, address4.ToString().c_str());
} else {
LogPrintf(" Failed to find masternode to pay\n");
}
2015-04-16 21:58:09 +02:00
}
2015-04-16 21:58:09 +02:00
std::string errorMessage;
CPubKey pubKeyMasternode;
CKey keyMasternode;
2015-04-16 21:58:09 +02:00
if(!darkSendSigner.SetKey(strMasterNodePrivKey, errorMessage, keyMasternode, pubKeyMasternode))
{
LogPrintf("CMasternodePayments::ProcessBlock() - Error upon calling SetKey: %s\n", errorMessage.c_str());
return false;
}
2015-04-16 21:58:09 +02:00
if(newWinner.Sign(keyMasternode, pubKeyMasternode))
2015-04-16 21:58:09 +02:00
{
if(AddWinningMasternode(newWinner))
{
newWinner.Relay();
2015-04-16 21:58:09 +02:00
nLastBlockHeight = nBlockHeight;
return true;
}
}
2015-04-16 21:58:09 +02:00
return false;
}
void CMasternodePaymentWinner::Relay()
2015-04-16 21:58:09 +02:00
{
CInv inv(MSG_MASTERNODE_WINNER, GetHash());
2015-04-16 21:58:09 +02:00
vector<CInv> vInv;
vInv.push_back(inv);
LOCK(cs_vNodes);
BOOST_FOREACH(CNode* pnode, vNodes){
pnode->PushMessage("inv", vInv);
}
}
bool CMasternodePaymentWinner::SignatureValid()
2015-04-16 21:58:09 +02:00
{
CMasternode* pmn = mnodeman.Find(vinMasternode);
2015-04-16 21:58:09 +02:00
if(pmn != NULL)
{
std::string strMessage = vinMasternode.prevout.ToStringShort() +
boost::lexical_cast<std::string>(nBlockHeight) +
payee.ToString();
2015-04-16 21:58:09 +02:00
std::string errorMessage = "";
if(!darkSendSigner.VerifyMessage(pmn->pubkey2, vchSig, strMessage, errorMessage)){
return error("CMasternodePaymentWinner::SignatureValid() - Got bad Masternode address signature %s \n", vinMasternode.ToString().c_str());
}
2015-04-16 21:58:09 +02:00
return true;
}
return false;
2015-04-16 21:58:09 +02:00
}
void CMasternodePayments::Sync(CNode* node)
{
LOCK(cs_masternodepayments);
if(chainActive.Tip() == NULL) return;
std::map<uint256, CMasternodePaymentWinner>::iterator it = mapMasternodePayeeVotes.begin();
while(it != mapMasternodePayeeVotes.end()) {
CMasternodePaymentWinner winner = (*it).second;
if(winner.nBlockHeight >= chainActive.Tip()->nHeight-10 && winner.nBlockHeight <= chainActive.Tip()->nHeight + 20)
node->PushMessage("mnw", winner);
++it;
}
2015-05-16 04:53:53 +02:00
}