neobytes/src/masternodeman.cpp

1120 lines
38 KiB
C++
Raw Normal View History

2015-03-18 00:06:58 +01:00
// Copyright (c) 2014-2015 The Dash developers
2015-02-24 15:02:22 +01:00
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include "masternodeman.h"
2015-03-23 13:59:22 +01:00
#include "masternode.h"
#include "activemasternode.h"
#include "darksend.h"
#include "util.h"
#include "addrman.h"
#include "spork.h"
#include <boost/lexical_cast.hpp>
#include <boost/filesystem.hpp>
/** Masternode manager */
CMasternodeMan mnodeman;
struct CompareLastPaid
{
bool operator()(const pair<int64_t, CTxIn>& t1,
const pair<int64_t, CTxIn>& t2) const
{
return t1.first < t2.first;
}
};
struct CompareScoreTxIn
{
bool operator()(const pair<int64_t, CTxIn>& t1,
const pair<int64_t, CTxIn>& t2) const
{
return t1.first < t2.first;
}
};
struct CompareScoreMN
{
bool operator()(const pair<int64_t, CMasternode>& t1,
const pair<int64_t, CMasternode>& t2) const
{
return t1.first < t2.first;
}
};
//
// CMasternodeDB
//
CMasternodeDB::CMasternodeDB()
{
pathMN = GetDataDir() / "mncache.dat";
strMagicMessage = "MasternodeCache";
}
bool CMasternodeDB::Write(const CMasternodeMan& mnodemanToSave)
{
int64_t nStart = GetTimeMillis();
// serialize, checksum data up to that point, then append checksum
CDataStream ssMasternodes(SER_DISK, CLIENT_VERSION);
ssMasternodes << strMagicMessage; // masternode cache file specific magic message
ssMasternodes << FLATDATA(Params().MessageStart()); // network specific magic number
ssMasternodes << mnodemanToSave;
uint256 hash = Hash(ssMasternodes.begin(), ssMasternodes.end());
ssMasternodes << hash;
// open output file, and associate with CAutoFile
FILE *file = fopen(pathMN.string().c_str(), "wb");
2015-04-03 00:51:08 +02:00
CAutoFile fileout(file, SER_DISK, CLIENT_VERSION);
if (fileout.IsNull())
return error("%s : Failed to open file %s", __func__, pathMN.string());
// Write and commit header, data
try {
fileout << ssMasternodes;
}
catch (std::exception &e) {
return error("%s : Serialize or I/O error - %s", __func__, e.what());
}
2015-04-03 00:51:08 +02:00
// FileCommit(fileout);
fileout.fclose();
LogPrintf("Written info to mncache.dat %dms\n", GetTimeMillis() - nStart);
LogPrintf(" %s\n", mnodemanToSave.ToString());
return true;
}
CMasternodeDB::ReadResult CMasternodeDB::Read(CMasternodeMan& mnodemanToLoad, bool fDryRun)
{
int64_t nStart = GetTimeMillis();
// open input file, and associate with CAutoFile
FILE *file = fopen(pathMN.string().c_str(), "rb");
2015-04-03 00:51:08 +02:00
CAutoFile filein(file, SER_DISK, CLIENT_VERSION);
if (filein.IsNull())
{
error("%s : Failed to open file %s", __func__, pathMN.string());
return FileError;
}
// use file size to size memory buffer
int fileSize = boost::filesystem::file_size(pathMN);
int dataSize = fileSize - sizeof(uint256);
// Don't try to resize to a negative number if file is small
if (dataSize < 0)
dataSize = 0;
vector<unsigned char> vchData;
vchData.resize(dataSize);
uint256 hashIn;
// read data and checksum from file
try {
filein.read((char *)&vchData[0], dataSize);
filein >> hashIn;
}
catch (std::exception &e) {
error("%s : Deserialize or I/O error - %s", __func__, e.what());
return HashReadError;
}
filein.fclose();
CDataStream ssMasternodes(vchData, SER_DISK, CLIENT_VERSION);
// verify stored checksum matches input data
uint256 hashTmp = Hash(ssMasternodes.begin(), ssMasternodes.end());
if (hashIn != hashTmp)
{
error("%s : Checksum mismatch, data corrupted", __func__);
return IncorrectHash;
}
unsigned char pchMsgTmp[4];
std::string strMagicMessageTmp;
try {
// de-serialize file header (masternode cache file specific magic message) and ..
ssMasternodes >> strMagicMessageTmp;
// ... verify the message matches predefined one
if (strMagicMessage != strMagicMessageTmp)
{
error("%s : Invalid masternode cache magic message", __func__);
return IncorrectMagicMessage;
}
// de-serialize file header (network specific magic number) and ..
ssMasternodes >> FLATDATA(pchMsgTmp);
// ... verify the network matches ours
if (memcmp(pchMsgTmp, Params().MessageStart(), sizeof(pchMsgTmp)))
{
error("%s : Invalid network magic number", __func__);
return IncorrectMagicNumber;
}
// de-serialize data into CMasternodeMan object
ssMasternodes >> mnodemanToLoad;
}
catch (std::exception &e) {
mnodemanToLoad.Clear();
error("%s : Deserialize or I/O error - %s", __func__, e.what());
return IncorrectFormat;
}
LogPrintf("Loaded info from mncache.dat %dms\n", GetTimeMillis() - nStart);
LogPrintf(" %s\n", mnodemanToLoad.ToString());
if(!fDryRun) {
LogPrintf("Masternode manager - cleaning....\n");
mnodemanToLoad.CheckAndRemove(true);
LogPrintf("Masternode manager - result:\n");
LogPrintf(" %s\n", mnodemanToLoad.ToString());
}
return Ok;
}
void DumpMasternodes()
{
int64_t nStart = GetTimeMillis();
CMasternodeDB mndb;
CMasternodeMan tempMnodeman;
LogPrintf("Verifying mncache.dat format...\n");
CMasternodeDB::ReadResult readResult = mndb.Read(tempMnodeman, true);
// there was an error and it was not an error on file opening => do not proceed
if (readResult == CMasternodeDB::FileError)
LogPrintf("Missing masternode cache file - mncache.dat, will try to recreate\n");
else if (readResult != CMasternodeDB::Ok)
{
LogPrintf("Error reading mncache.dat: ");
if(readResult == CMasternodeDB::IncorrectFormat)
LogPrintf("magic is ok but data has invalid format, will try to recreate\n");
else
{
LogPrintf("file format is unknown or invalid, please fix it manually\n");
return;
}
}
LogPrintf("Writting info to mncache.dat...\n");
mndb.Write(mnodeman);
LogPrintf("Masternode dump finished %dms\n", GetTimeMillis() - nStart);
}
2015-03-06 18:25:48 +01:00
CMasternodeMan::CMasternodeMan() {
nDsqCount = 0;
}
bool CMasternodeMan::Add(CMasternode &mn)
{
LOCK(cs);
if (!mn.IsEnabled())
return false;
CMasternode *pmn = Find(mn.vin);
2015-02-25 12:54:03 +01:00
if (pmn == NULL)
{
LogPrint("masternode", "CMasternodeMan: Adding new Masternode %s - %i now\n", mn.addr.ToString(), size() + 1);
vMasternodes.push_back(mn);
return true;
}
return false;
}
2015-08-07 05:07:40 +02:00
void CMasternodeMan::AskForMN(CNode* pnode, CTxIn &vin)
{
std::map<COutPoint, int64_t>::iterator i = mWeAskedForMasternodeListEntry.find(vin.prevout);
if (i != mWeAskedForMasternodeListEntry.end())
{
int64_t t = (*i).second;
if (GetTime() < t) return; // we've asked recently
}
// ask for the mnb info once from the node that sent mnp
LogPrintf("CMasternodeMan::AskForMN - Asking node for missing entry, vin: %s\n", vin.ToString());
pnode->PushMessage("dseg", vin);
int64_t askAgain = GetTime() + MASTERNODE_MIN_MNP_SECONDS;
mWeAskedForMasternodeListEntry[vin.prevout] = askAgain;
}
void CMasternodeMan::Check()
{
LOCK(cs);
2015-06-24 18:41:03 +02:00
BOOST_FOREACH(CMasternode& mn, vMasternodes) {
mn.Check();
2015-06-24 18:41:03 +02:00
}
}
void CMasternodeMan::CheckAndRemove(bool forceExpiredRemoval)
{
Check();
2015-07-30 15:44:18 +02:00
LOCK(cs);
//remove inactive and outdated
vector<CMasternode>::iterator it = vMasternodes.begin();
while(it != vMasternodes.end()){
if((*it).activeState == CMasternode::MASTERNODE_REMOVE ||
(*it).activeState == CMasternode::MASTERNODE_VIN_SPENT ||
(forceExpiredRemoval && (*it).activeState == CMasternode::MASTERNODE_EXPIRED) ||
(*it).protocolVersion < masternodePayments.GetMinMasternodePaymentsProto()) {
LogPrint("masternode", "CMasternodeMan: Removing inactive Masternode %s - %i now\n", (*it).addr.ToString(), size() - 1);
//erase all of the broadcasts we've seen from this vin
// -- if we missed a few pings and the node was removed, this will allow is to get it back without them
// sending a brand new mnb
map<uint256, CMasternodeBroadcast>::iterator it3 = mapSeenMasternodeBroadcast.begin();
while(it3 != mapSeenMasternodeBroadcast.end()){
if((*it3).second.vin == (*it).vin){
masternodeSync.mapSeenSyncMNB.erase((*it3).first);
mapSeenMasternodeBroadcast.erase(it3++);
} else {
++it3;
}
}
// allow us to ask for this masternode again if we see another ping
map<COutPoint, int64_t>::iterator it2 = mWeAskedForMasternodeListEntry.begin();
while(it2 != mWeAskedForMasternodeListEntry.end()){
if((*it2).first == (*it).vin.prevout){
mWeAskedForMasternodeListEntry.erase(it2++);
} else {
++it2;
}
}
it = vMasternodes.erase(it);
} else {
++it;
}
}
2015-03-05 09:10:15 +01:00
// check who's asked for the Masternode list
map<CNetAddr, int64_t>::iterator it1 = mAskedUsForMasternodeList.begin();
while(it1 != mAskedUsForMasternodeList.end()){
if((*it1).second < GetTime()) {
mAskedUsForMasternodeList.erase(it1++);
} else {
++it1;
}
}
2015-03-05 09:10:15 +01:00
// check who we asked for the Masternode list
it1 = mWeAskedForMasternodeList.begin();
while(it1 != mWeAskedForMasternodeList.end()){
if((*it1).second < GetTime()){
mWeAskedForMasternodeList.erase(it1++);
} else {
++it1;
}
}
2015-03-05 09:10:15 +01:00
// check which Masternodes we've asked for
map<COutPoint, int64_t>::iterator it2 = mWeAskedForMasternodeListEntry.begin();
while(it2 != mWeAskedForMasternodeListEntry.end()){
if((*it2).second < GetTime()){
mWeAskedForMasternodeListEntry.erase(it2++);
} else {
++it2;
}
}
2015-08-29 05:25:55 +02:00
// remove expired mapSeenMasternodeBroadcast
map<uint256, CMasternodeBroadcast>::iterator it3 = mapSeenMasternodeBroadcast.begin();
while(it3 != mapSeenMasternodeBroadcast.end()){
if((*it3).second.lastPing.sigTime < GetTime()-(MASTERNODE_REMOVAL_SECONDS*2)){
mapSeenMasternodeBroadcast.erase(it3++);
masternodeSync.mapSeenSyncMNB.erase((*it3).second.GetHash());
} else {
++it3;
}
}
// remove expired mapSeenMasternodePing
map<uint256, CMasternodePing>::iterator it4 = mapSeenMasternodePing.begin();
while(it4 != mapSeenMasternodePing.end()){
if((*it4).second.sigTime < GetTime()-(MASTERNODE_REMOVAL_SECONDS*2)){
mapSeenMasternodePing.erase(it4++);
} else {
++it4;
}
}
}
2015-03-01 01:04:17 +01:00
void CMasternodeMan::Clear()
{
LOCK(cs);
vMasternodes.clear();
mAskedUsForMasternodeList.clear();
mWeAskedForMasternodeList.clear();
mWeAskedForMasternodeListEntry.clear();
2015-03-06 18:25:48 +01:00
nDsqCount = 0;
2015-03-01 01:04:17 +01:00
}
2015-07-19 01:20:48 +02:00
int CMasternodeMan::CountEnabled(int protocolVersion)
{
int i = 0;
protocolVersion = protocolVersion == -1 ? masternodePayments.GetMinMasternodePaymentsProto() : protocolVersion;
BOOST_FOREACH(CMasternode& mn, vMasternodes) {
mn.Check();
if(mn.protocolVersion < protocolVersion || !mn.IsEnabled()) continue;
i++;
}
return i;
}
void CMasternodeMan::DsegUpdate(CNode* pnode)
{
LOCK(cs);
if(Params().NetworkID() == CBaseChainParams::MAIN) {
if(!(pnode->addr.IsRFC1918() || pnode->addr.IsLocal())){
std::map<CNetAddr, int64_t>::iterator it = mWeAskedForMasternodeList.find(pnode->addr);
if (it != mWeAskedForMasternodeList.end())
{
if (GetTime() < (*it).second) {
LogPrintf("dseg - we already asked %s for the list; skipping...\n", pnode->addr.ToString());
return;
}
2015-07-02 17:07:30 +02:00
}
}
}
pnode->PushMessage("dseg", CTxIn());
int64_t askAgain = GetTime() + MASTERNODES_DSEG_SECONDS;
mWeAskedForMasternodeList[pnode->addr] = askAgain;
}
CMasternode *CMasternodeMan::Find(const CScript &payee)
{
LOCK(cs);
CScript payee2;
BOOST_FOREACH(CMasternode& mn, vMasternodes)
{
payee2 = GetScriptForDestination(mn.pubkey.GetID());
if(payee2 == payee)
return &mn;
}
return NULL;
}
CMasternode *CMasternodeMan::Find(const CTxIn &vin)
{
LOCK(cs);
BOOST_FOREACH(CMasternode& mn, vMasternodes)
{
2015-04-09 21:17:32 +02:00
if(mn.vin.prevout == vin.prevout)
return &mn;
}
return NULL;
}
2015-04-07 21:59:30 +02:00
CMasternode *CMasternodeMan::Find(const CPubKey &pubKeyMasternode)
{
LOCK(cs);
BOOST_FOREACH(CMasternode& mn, vMasternodes)
{
if(mn.pubkey2 == pubKeyMasternode)
return &mn;
}
return NULL;
}
//
// Deterministically select the oldest/best masternode to pay on the network
//
CMasternode* CMasternodeMan::GetNextMasternodeInQueueForPayment(int nBlockHeight, bool fFilterSigTime, int& nCount)
{
LOCK(cs);
CMasternode *pBestMasternode = NULL;
std::vector<pair<int64_t, CTxIn> > vecMasternodeLastPaid;
/*
Make a vector with all of the last paid times
*/
int nMnCount = CountEnabled();
BOOST_FOREACH(CMasternode &mn, vMasternodes)
{
mn.Check();
if(!mn.IsEnabled()) continue;
// //check protocol version
if(mn.protocolVersion < masternodePayments.GetMinMasternodePaymentsProto()) continue;
//it's in the list (up to 8 entries ahead of current block to allow propagation) -- so let's skip it
if(masternodePayments.IsScheduled(mn, nBlockHeight)) continue;
//it's too new, wait for a cycle
if(fFilterSigTime && mn.sigTime + (nMnCount*2.6*60) > GetAdjustedTime()) continue;
//make sure it has as many confirmations as there are masternodes
if(mn.GetMasternodeInputAge() < nMnCount) continue;
vecMasternodeLastPaid.push_back(make_pair(mn.SecondsSincePayment(), mn.vin));
}
nCount = (int)vecMasternodeLastPaid.size();
//when the network is in the process of upgrading, don't penalize nodes that recently restarted
if(fFilterSigTime && nCount < nMnCount/3) return GetNextMasternodeInQueueForPayment(nBlockHeight, false, nCount);
// Sort them high to low
sort(vecMasternodeLastPaid.rbegin(), vecMasternodeLastPaid.rend(), CompareLastPaid());
// Look at 1/10 of the oldest nodes (by last payment), calculate their scores and pay the best one
// -- This doesn't look at who is being paid in the +8-10 blocks, allowing for double payments very rarely
// -- 1/100 payments should be a double payment on mainnet - (1/(3000/10))*2
// -- (chance per block * chances before IsScheduled will fire)
int nTenthNetwork = CountEnabled()/10;
int nCountTenth = 0;
uint256 nHigh = 0;
BOOST_FOREACH (PAIRTYPE(int64_t, CTxIn)& s, vecMasternodeLastPaid){
CMasternode* pmn = Find(s.second);
if(!pmn) break;
uint256 n = pmn->CalculateScore(1, nBlockHeight-100);
if(n > nHigh){
nHigh = n;
pBestMasternode = pmn;
}
nCountTenth++;
if(nCountTenth >= nTenthNetwork) break;
}
return pBestMasternode;
}
CMasternode *CMasternodeMan::FindRandomNotInVec(std::vector<CTxIn> &vecToExclude, int protocolVersion)
{
LOCK(cs);
protocolVersion = protocolVersion == -1 ? masternodePayments.GetMinMasternodePaymentsProto() : protocolVersion;
int nCountEnabled = CountEnabled(protocolVersion);
LogPrintf("CMasternodeMan::FindRandomNotInVec - nCountEnabled - vecToExclude.size() %d\n", nCountEnabled - vecToExclude.size());
if(nCountEnabled - vecToExclude.size() < 1) return NULL;
int rand = GetRandInt(nCountEnabled - vecToExclude.size());
LogPrintf("CMasternodeMan::FindRandomNotInVec - rand %d\n", rand);
bool found;
BOOST_FOREACH(CMasternode &mn, vMasternodes) {
if(mn.protocolVersion < protocolVersion || !mn.IsEnabled()) continue;
found = false;
BOOST_FOREACH(CTxIn &usedVin, vecToExclude) {
if(mn.vin.prevout == usedVin.prevout) {
found = true;
break;
}
}
if(found) continue;
if(--rand < 1) {
return &mn;
}
}
return NULL;
}
CMasternode* CMasternodeMan::GetCurrentMasterNode(int mod, int64_t nBlockHeight, int minProtocol)
{
int64_t score = 0;
CMasternode* winner = NULL;
// scan for winner
BOOST_FOREACH(CMasternode& mn, vMasternodes) {
mn.Check();
if(mn.protocolVersion < minProtocol || !mn.IsEnabled()) continue;
2015-03-05 09:10:15 +01:00
// calculate the score for each Masternode
uint256 n = mn.CalculateScore(mod, nBlockHeight);
int64_t n2 = n.GetCompact(false);
// determine the winner
if(n2 > score){
score = n2;
winner = &mn;
}
}
return winner;
}
int CMasternodeMan::GetMasternodeRank(const CTxIn& vin, int64_t nBlockHeight, int minProtocol, bool fOnlyActive)
{
std::vector<pair<int64_t, CTxIn> > vecMasternodeScores;
2015-03-23 13:59:22 +01:00
//make sure we know about this block
uint256 hash = 0;
if(!GetBlockHash(hash, nBlockHeight)) return -1;
// scan for winner
BOOST_FOREACH(CMasternode& mn, vMasternodes) {
if(mn.protocolVersion < minProtocol) continue;
if(fOnlyActive) {
mn.Check();
if(!mn.IsEnabled()) continue;
}
uint256 n = mn.CalculateScore(1, nBlockHeight);
int64_t n2 = n.GetCompact(false);
vecMasternodeScores.push_back(make_pair(n2, mn.vin));
}
sort(vecMasternodeScores.rbegin(), vecMasternodeScores.rend(), CompareScoreTxIn());
2015-03-02 00:09:33 +01:00
int rank = 0;
BOOST_FOREACH (PAIRTYPE(int64_t, CTxIn)& s, vecMasternodeScores){
rank++;
if(s.second.prevout == vin.prevout) {
return rank;
}
}
return -1;
}
std::vector<pair<int, CMasternode> > CMasternodeMan::GetMasternodeRanks(int64_t nBlockHeight, int minProtocol)
{
std::vector<pair<int64_t, CMasternode> > vecMasternodeScores;
std::vector<pair<int, CMasternode> > vecMasternodeRanks;
2015-03-23 13:59:22 +01:00
//make sure we know about this block
uint256 hash = 0;
if(!GetBlockHash(hash, nBlockHeight)) return vecMasternodeRanks;
// scan for winner
BOOST_FOREACH(CMasternode& mn, vMasternodes) {
mn.Check();
if(mn.protocolVersion < minProtocol) continue;
if(!mn.IsEnabled()) {
continue;
}
uint256 n = mn.CalculateScore(1, nBlockHeight);
int64_t n2 = n.GetCompact(false);
vecMasternodeScores.push_back(make_pair(n2, mn));
}
sort(vecMasternodeScores.rbegin(), vecMasternodeScores.rend(), CompareScoreMN());
int rank = 0;
BOOST_FOREACH (PAIRTYPE(int64_t, CMasternode)& s, vecMasternodeScores){
rank++;
vecMasternodeRanks.push_back(make_pair(rank, s.second));
}
return vecMasternodeRanks;
}
CMasternode* CMasternodeMan::GetMasternodeByRank(int nRank, int64_t nBlockHeight, int minProtocol, bool fOnlyActive)
2015-03-02 00:09:33 +01:00
{
std::vector<pair<int64_t, CTxIn> > vecMasternodeScores;
2015-03-02 00:09:33 +01:00
// scan for winner
BOOST_FOREACH(CMasternode& mn, vMasternodes) {
if(mn.protocolVersion < minProtocol) continue;
if(fOnlyActive) {
mn.Check();
if(!mn.IsEnabled()) continue;
2015-03-02 00:09:33 +01:00
}
uint256 n = mn.CalculateScore(1, nBlockHeight);
int64_t n2 = n.GetCompact(false);
2015-03-02 00:09:33 +01:00
vecMasternodeScores.push_back(make_pair(n2, mn.vin));
}
sort(vecMasternodeScores.rbegin(), vecMasternodeScores.rend(), CompareScoreTxIn());
2015-03-02 00:09:33 +01:00
int rank = 0;
BOOST_FOREACH (PAIRTYPE(int64_t, CTxIn)& s, vecMasternodeScores){
2015-03-02 00:09:33 +01:00
rank++;
if(rank == nRank) {
return Find(s.second);
}
}
return NULL;
}
void CMasternodeMan::ProcessMasternodeConnections()
{
//we don't care about this for regtest
2015-04-03 00:51:08 +02:00
if(Params().NetworkID() == CBaseChainParams::REGTEST) return;
LOCK(cs_vNodes);
BOOST_FOREACH(CNode* pnode, vNodes) {
2015-03-02 00:09:33 +01:00
if(pnode->fDarkSendMaster){
if(darkSendPool.pSubmittedToMasternode != NULL && pnode->addr == darkSendPool.pSubmittedToMasternode->addr) continue;
LogPrintf("Closing Masternode connection %s \n", pnode->addr.ToString());
2015-07-30 15:44:18 +02:00
pnode->fDisconnect = true;
2015-03-02 00:09:33 +01:00
}
}
}
void CMasternodeMan::ProcessMessage(CNode* pfrom, std::string& strCommand, CDataStream& vRecv)
{
2015-03-05 09:10:15 +01:00
if(fLiteMode) return; //disable all Darksend/Masternode related functionality
if(!masternodeSync.IsBlockchainSynced()) return;
2015-03-31 23:21:59 +02:00
LOCK(cs_process_message);
if (strCommand == "mnb") { //Masternode Broadcast
CMasternodeBroadcast mnb;
vRecv >> mnb;
if(mapSeenMasternodeBroadcast.count(mnb.GetHash())) { //seen
masternodeSync.AddedMasternodeList(mnb.GetHash());
return;
}
mapSeenMasternodeBroadcast.insert(make_pair(mnb.GetHash(), mnb));
int nDoS = 0;
if(!mnb.CheckAndUpdate(nDoS)){
if(nDoS > 0)
Misbehaving(pfrom->GetId(), nDoS);
//failed
return;
}
2015-03-05 09:10:15 +01:00
// make sure the vout that was signed is related to the transaction that spawned the Masternode
// - this is expensive, so it's only done once per Masternode
if(!darkSendSigner.IsVinAssociatedWithPubkey(mnb.vin, mnb.pubkey)) {
LogPrintf("mnb - Got mismatched pubkey and vin\n");
Misbehaving(pfrom->GetId(), 33);
return;
}
// make sure it's still unspent
// - this is checked later by .check() in many places and by ThreadCheckDarkSendPool()
if(mnb.CheckInputsAndAdd(nDoS)) {
// use this as a peer
addrman.Add(CAddress(mnb.addr), pfrom->addr, 2*60*60);
masternodeSync.AddedMasternodeList(mnb.GetHash());
} else {
LogPrintf("mnb - Rejected Masternode entry %s\n", mnb.addr.ToString());
if (nDoS > 0)
Misbehaving(pfrom->GetId(), nDoS);
}
}
else if (strCommand == "mnp") { //Masternode Ping
CMasternodePing mnp;
vRecv >> mnp;
LogPrint("masternode", "mnp - Masternode ping, vin: %s\n", mnp.vin.ToString());
if(mapSeenMasternodePing.count(mnp.GetHash())) return; //seen
mapSeenMasternodePing.insert(make_pair(mnp.GetHash(), mnp));
int nDoS = 0;
if(mnp.CheckAndUpdate(nDoS)) return;
if(nDoS > 0) {
2015-08-07 05:07:40 +02:00
// if anything significant failed, mark that node
Misbehaving(pfrom->GetId(), nDoS);
2015-08-07 05:07:40 +02:00
} else {
// if nothing significant failed, search existing Masternode list
CMasternode* pmn = Find(mnp.vin);
2015-08-07 05:07:40 +02:00
// if it's known, don't ask for the mnb, just return
if(pmn != NULL) return;
}
2015-08-07 05:07:40 +02:00
// something significant is broken or mn is unknown,
// we might have to ask for a masternode entry once
AskForMN(pfrom, mnp.vin);
2015-03-05 09:10:15 +01:00
} else if (strCommand == "dseg") { //Get Masternode list or specific entry
CTxIn vin;
vRecv >> vin;
if(vin == CTxIn()) { //only should ask for this once
//local network
2015-07-02 17:07:30 +02:00
bool isLocal = (pfrom->addr.IsRFC1918() || pfrom->addr.IsLocal());
if(!isLocal && Params().NetworkID() == CBaseChainParams::MAIN) {
std::map<CNetAddr, int64_t>::iterator i = mAskedUsForMasternodeList.find(pfrom->addr);
2015-04-03 00:51:08 +02:00
if (i != mAskedUsForMasternodeList.end()){
int64_t t = (*i).second;
if (GetTime() < t) {
Misbehaving(pfrom->GetId(), 34);
LogPrintf("dseg - peer already asked me for the list\n");
return;
}
}
int64_t askAgain = GetTime() + MASTERNODES_DSEG_SECONDS;
mAskedUsForMasternodeList[pfrom->addr] = askAgain;
}
} //else, asking for a specific node which is ok
2015-07-29 06:16:11 +02:00
2015-07-26 04:27:40 +02:00
std::vector<CInv> vInv;
int i = 0;
BOOST_FOREACH(CMasternode& mn, vMasternodes) {
if(mn.addr.IsRFC1918()) continue; //local network
2015-04-03 00:51:08 +02:00
if(mn.IsEnabled()) {
LogPrint("masternode", "dseg - Sending Masternode entry - %s \n", mn.addr.ToString());
if(vin == CTxIn() || vin == mn.vin){
CMasternodeBroadcast mnb = CMasternodeBroadcast(mn);
uint256 hash = mnb.GetHash();
CInv inv(MSG_MASTERNODE_ANNOUNCE, hash);
2015-07-26 04:27:40 +02:00
vInv.push_back(inv);
if(!mapSeenMasternodeBroadcast.count(hash)) mapSeenMasternodeBroadcast.insert(make_pair(hash, mnb));
if(vin == mn.vin) {
LogPrintf("dseg - Sent 1 Masternode entries to %s\n", pfrom->addr.ToString());
break;
}
}
i++;
}
}
2015-07-29 06:16:11 +02:00
if(vin == CTxIn()) pfrom->PushMessage("ssc", MASTERNODE_SYNC_LIST, (int)vInv.size());
2015-07-26 04:27:40 +02:00
if(vInv.size() > 0) pfrom->PushMessage("inv", vInv);
2015-08-14 18:15:50 +02:00
if(vin == CTxIn()) LogPrintf("dseg - Sent %d Masternode entries to %s\n", i, pfrom->addr.ToString());
}
/*
* IT'S SAFE TO REMOVE THIS IN FURTHER VERSIONS
* AFTER MIGRATION TO V12 IS DONE
*/
// Light version for OLD MASSTERNODES - fake pings, no self-activation
else if (strCommand == "dsee") { //DarkSend Election Entry
if(IsSporkActive(SPORK_10_MASTERNODE_PAY_UPDATED_NODES)) return;
2015-08-09 02:01:07 +02:00
CTxIn vin;
CService addr;
CPubKey pubkey;
CPubKey pubkey2;
vector<unsigned char> vchSig;
int64_t sigTime;
int count;
int current;
int64_t lastUpdated;
int protocolVersion;
CScript donationAddress;
int donationPercentage;
std::string strMessage;
vRecv >> vin >> addr >> vchSig >> sigTime >> pubkey >> pubkey2 >> count >> current >> lastUpdated >> protocolVersion >> donationAddress >> donationPercentage;
// make sure signature isn't in the future (past is OK)
if (sigTime > GetAdjustedTime() + 60 * 60) {
LogPrintf("dsee - Signature rejected, too far into the future %s\n", vin.ToString().c_str());
2015-08-14 05:18:08 +02:00
Misbehaving(pfrom->GetId(), 1);
2015-08-09 02:01:07 +02:00
return;
}
std::string vchPubKey(pubkey.begin(), pubkey.end());
std::string vchPubKey2(pubkey2.begin(), pubkey2.end());
strMessage = addr.ToString() + boost::lexical_cast<std::string>(sigTime) + vchPubKey + vchPubKey2 + boost::lexical_cast<std::string>(protocolVersion) + donationAddress.ToString() + boost::lexical_cast<std::string>(donationPercentage);
if(protocolVersion < masternodePayments.GetMinMasternodePaymentsProto()) {
LogPrintf("dsee - ignoring outdated Masternode %s protocol version %d < %d\n", vin.ToString().c_str(), protocolVersion, masternodePayments.GetMinMasternodePaymentsProto());
2015-08-14 05:18:08 +02:00
Misbehaving(pfrom->GetId(), 1);
2015-08-09 02:01:07 +02:00
return;
}
CScript pubkeyScript;
pubkeyScript = GetScriptForDestination(pubkey.GetID());
if(pubkeyScript.size() != 25) {
LogPrintf("dsee - pubkey the wrong size\n");
2015-08-14 05:18:08 +02:00
Misbehaving(pfrom->GetId(), 100);
2015-08-09 02:01:07 +02:00
return;
}
CScript pubkeyScript2;
pubkeyScript2 = GetScriptForDestination(pubkey2.GetID());
if(pubkeyScript2.size() != 25) {
LogPrintf("dsee - pubkey2 the wrong size\n");
2015-08-14 05:18:08 +02:00
Misbehaving(pfrom->GetId(), 100);
2015-08-09 02:01:07 +02:00
return;
}
if(!vin.scriptSig.empty()) {
LogPrintf("dsee - Ignore Not Empty ScriptSig %s\n",vin.ToString());
2015-08-14 05:18:08 +02:00
Misbehaving(pfrom->GetId(), 100);
2015-08-09 02:01:07 +02:00
return;
}
std::string errorMessage = "";
if(!darkSendSigner.VerifyMessage(pubkey, vchSig, strMessage, errorMessage)){
LogPrintf("dsee - Got bad Masternode address signature\n");
Misbehaving(pfrom->GetId(), 100);
return;
}
if(Params().NetworkID() == CBaseChainParams::MAIN){
if(addr.GetPort() != 9999) return;
} else if(addr.GetPort() == 9999) return;
//search existing Masternode list, this is where we update existing Masternodes with new dsee broadcasts
CMasternode* pmn = this->Find(vin);
if(pmn != NULL)
{
// count == -1 when it's a new entry
// e.g. We don't want the entry relayed/time updated when we're syncing the list
// mn.pubkey = pubkey, IsVinAssociatedWithPubkey is validated once below,
// after that they just need to match
2015-08-14 07:08:32 +02:00
if(count == -1 && pmn->pubkey == pubkey && (GetAdjustedTime() - pmn->nLastDsee > MASTERNODE_MIN_MNB_SECONDS)){
2015-08-12 14:27:58 +02:00
if(pmn->protocolVersion > GETHEADERS_VERSION && sigTime - pmn->lastPing.sigTime < MASTERNODE_MIN_MNB_SECONDS) return;
2015-08-14 07:08:32 +02:00
if(pmn->nLastDsee < sigTime){ //take the newest entry
2015-08-09 02:01:07 +02:00
LogPrintf("dsee - Got updated entry for %s\n", addr.ToString().c_str());
2015-08-10 12:16:49 +02:00
if(pmn->protocolVersion < GETHEADERS_VERSION) {
pmn->pubkey2 = pubkey2;
pmn->sigTime = sigTime;
pmn->sig = vchSig;
pmn->protocolVersion = protocolVersion;
pmn->addr = addr;
//fake ping
pmn->lastPing = CMasternodePing(vin);
}
2015-08-14 07:08:32 +02:00
pmn->nLastDsee = sigTime;
2015-08-09 02:01:07 +02:00
pmn->Check();
if(pmn->IsEnabled()) {
TRY_LOCK(cs_vNodes, lockNodes);
if(!lockNodes) return;
BOOST_FOREACH(CNode* pnode, vNodes)
if(pnode->nVersion >= masternodePayments.GetMinMasternodePaymentsProto())
pnode->PushMessage("dsee", vin, addr, vchSig, sigTime, pubkey, pubkey2, count, current, lastUpdated, protocolVersion, donationAddress, donationPercentage);
}
}
}
return;
}
2015-08-12 16:29:43 +02:00
static std::map<COutPoint, CPubKey> mapSeenDsee;
if(mapSeenDsee.count(vin.prevout) && mapSeenDsee[vin.prevout] == pubkey) {
LogPrint("mastenrode", "dsee - already seen this vin %s\n", vin.prevout.ToString());
return;
}
mapSeenDsee.insert(make_pair(vin.prevout, pubkey));
2015-08-09 02:01:07 +02:00
// make sure the vout that was signed is related to the transaction that spawned the Masternode
// - this is expensive, so it's only done once per Masternode
if(!darkSendSigner.IsVinAssociatedWithPubkey(vin, pubkey)) {
LogPrintf("dsee - Got mismatched pubkey and vin\n");
Misbehaving(pfrom->GetId(), 100);
return;
}
2015-08-12 14:27:58 +02:00
LogPrint("masternode", "dsee - Got NEW OLD Masternode entry %s\n", addr.ToString().c_str());
2015-08-09 02:01:07 +02:00
// make sure it's still unspent
// - this is checked later by .check() in many places and by ThreadCheckDarkSendPool()
CValidationState state;
CMutableTransaction tx = CMutableTransaction();
CTxOut vout = CTxOut(999.99*COIN, darkSendPool.collateralPubKey);
tx.vin.push_back(vin);
tx.vout.push_back(vout);
2015-08-10 00:32:09 +02:00
bool fAcceptable = false;
{
TRY_LOCK(cs_main, lockMain);
if(!lockMain) return;
fAcceptable = AcceptableInputs(mempool, state, CTransaction(tx), false, NULL);
}
if(fAcceptable){
2015-08-09 02:01:07 +02:00
if(GetInputAge(vin) < MASTERNODE_MIN_CONFIRMATIONS){
LogPrintf("dsee - Input must have least %d confirmations\n", MASTERNODE_MIN_CONFIRMATIONS);
Misbehaving(pfrom->GetId(), 20);
return;
}
// verify that sig time is legit in past
// should be at least not earlier than block when 1000 DASH tx got MASTERNODE_MIN_CONFIRMATIONS
uint256 hashBlock = 0;
CTransaction tx2;
GetTransaction(vin.prevout.hash, tx2, hashBlock, true);
BlockMap::iterator mi = mapBlockIndex.find(hashBlock);
if (mi != mapBlockIndex.end() && (*mi).second)
{
CBlockIndex* pMNIndex = (*mi).second; // block for 1000 DASH tx -> 1 confirmation
CBlockIndex* pConfIndex = chainActive[pMNIndex->nHeight + MASTERNODE_MIN_CONFIRMATIONS - 1]; // block where tx got MASTERNODE_MIN_CONFIRMATIONS
if(pConfIndex->GetBlockTime() > sigTime)
{
LogPrintf("mnb - Bad sigTime %d for Masternode %20s %105s (%i conf block is at %d)\n",
sigTime, addr.ToString(), vin.ToString(), MASTERNODE_MIN_CONFIRMATIONS, pConfIndex->GetBlockTime());
return;
}
}
// use this as a peer
addrman.Add(CAddress(addr), pfrom->addr, 2*60*60);
// add Masternode
2015-08-09 02:01:07 +02:00
CMasternode mn = CMasternode();
mn.addr = addr;
mn.vin = vin;
mn.pubkey = pubkey;
mn.sig = vchSig;
mn.sigTime = sigTime;
mn.pubkey2 = pubkey2;
mn.protocolVersion = protocolVersion;
// fake ping
mn.lastPing = CMasternodePing(vin);
mn.Check(true);
// add v11 masternodes, v12 should be added by mnb only
2015-08-12 03:48:43 +02:00
if(protocolVersion < GETHEADERS_VERSION) {
2015-08-12 14:27:58 +02:00
LogPrint("masternode", "dsee - Accepted OLD Masternode entry %i %i\n", count, current);
2015-08-12 03:48:43 +02:00
Add(mn);
}
2015-08-09 02:01:07 +02:00
if(mn.IsEnabled()) {
TRY_LOCK(cs_vNodes, lockNodes);
if(!lockNodes) return;
BOOST_FOREACH(CNode* pnode, vNodes)
if(pnode->nVersion >= masternodePayments.GetMinMasternodePaymentsProto())
pnode->PushMessage("dsee", vin, addr, vchSig, sigTime, pubkey, pubkey2, count, current, lastUpdated, protocolVersion, donationAddress, donationPercentage);
}
} else {
LogPrintf("dsee - Rejected Masternode entry %s\n", addr.ToString().c_str());
int nDoS = 0;
if (state.IsInvalid(nDoS))
{
LogPrintf("dsee - %s from %s %s was not accepted into the memory pool\n", tx.GetHash().ToString().c_str(),
pfrom->addr.ToString().c_str(), pfrom->cleanSubVer.c_str());
if (nDoS > 0)
Misbehaving(pfrom->GetId(), nDoS);
}
}
}
else if (strCommand == "dseep") { //DarkSend Election Entry Ping
if(IsSporkActive(SPORK_10_MASTERNODE_PAY_UPDATED_NODES)) return;
2015-08-09 02:01:07 +02:00
CTxIn vin;
vector<unsigned char> vchSig;
int64_t sigTime;
bool stop;
vRecv >> vin >> vchSig >> sigTime >> stop;
//LogPrintf("dseep - Received: vin: %s sigTime: %lld stop: %s\n", vin.ToString().c_str(), sigTime, stop ? "true" : "false");
if (sigTime > GetAdjustedTime() + 60 * 60) {
LogPrintf("dseep - Signature rejected, too far into the future %s\n", vin.ToString().c_str());
2015-08-14 05:18:08 +02:00
Misbehaving(pfrom->GetId(), 1);
2015-08-09 02:01:07 +02:00
return;
}
if (sigTime <= GetAdjustedTime() - 60 * 60) {
LogPrintf("dseep - Signature rejected, too far into the past %s - %d %d \n", vin.ToString().c_str(), sigTime, GetAdjustedTime());
2015-08-14 05:18:08 +02:00
Misbehaving(pfrom->GetId(), 1);
2015-08-09 02:01:07 +02:00
return;
}
2015-08-14 03:14:08 +02:00
std::map<COutPoint, int64_t>::iterator i = mWeAskedForMasternodeListEntry.find(vin.prevout);
if (i != mWeAskedForMasternodeListEntry.end())
{
int64_t t = (*i).second;
if (GetTime() < t) return; // we've asked recently
}
2015-08-09 02:01:07 +02:00
// see if we have this Masternode
CMasternode* pmn = this->Find(vin);
if(pmn != NULL && pmn->protocolVersion >= masternodePayments.GetMinMasternodePaymentsProto())
{
// LogPrintf("dseep - Found corresponding mn for vin: %s\n", vin.ToString().c_str());
// take this only if it's newer
2015-08-14 03:14:08 +02:00
if(sigTime - pmn->nLastDseep > MASTERNODE_MIN_MNP_SECONDS)
2015-08-09 02:01:07 +02:00
{
std::string strMessage = pmn->addr.ToString() + boost::lexical_cast<std::string>(sigTime) + boost::lexical_cast<std::string>(stop);
std::string errorMessage = "";
if(!darkSendSigner.VerifyMessage(pmn->pubkey2, vchSig, strMessage, errorMessage))
{
LogPrintf("dseep - Got bad Masternode address signature %s \n", vin.ToString().c_str());
//Misbehaving(pfrom->GetId(), 100);
return;
}
// fake ping for v11 masternodes, ignore for v12
2015-08-10 12:16:49 +02:00
if(pmn->protocolVersion < GETHEADERS_VERSION) pmn->lastPing = CMasternodePing(vin);
2015-08-14 03:14:08 +02:00
pmn->nLastDseep = sigTime;
2015-08-09 02:01:07 +02:00
pmn->Check();
if(pmn->IsEnabled()) {
TRY_LOCK(cs_vNodes, lockNodes);
if(!lockNodes) return;
2015-08-14 03:14:08 +02:00
LogPrint("masternode", "dseep - relaying %s \n", vin.ToString().c_str());
2015-08-09 02:01:07 +02:00
BOOST_FOREACH(CNode* pnode, vNodes)
if(pnode->nVersion >= masternodePayments.GetMinMasternodePaymentsProto())
pnode->PushMessage("dseep", vin, vchSig, sigTime, stop);
}
}
return;
}
2015-08-14 03:14:08 +02:00
LogPrint("masternode", "dseep - Couldn't find Masternode entry %s %s\n", vin.ToString(), pfrom->addr.ToString());
2015-08-09 02:01:07 +02:00
AskForMN(pfrom, vin);
}
/*
* END OF "REMOVE"
*/
}
void CMasternodeMan::Remove(CTxIn vin)
{
LOCK(cs);
vector<CMasternode>::iterator it = vMasternodes.begin();
while(it != vMasternodes.end()){
if((*it).vin == vin){
LogPrint("masternode", "CMasternodeMan: Removing Masternode %s - %i now\n", (*it).addr.ToString(), size() - 1);
vMasternodes.erase(it);
break;
}
++it;
}
}
std::string CMasternodeMan::ToString() const
{
std::ostringstream info;
2015-03-05 09:10:15 +01:00
info << "Masternodes: " << (int)vMasternodes.size() <<
", peers who asked us for Masternode list: " << (int)mAskedUsForMasternodeList.size() <<
", peers we asked for Masternode list: " << (int)mWeAskedForMasternodeList.size() <<
2015-03-06 18:25:48 +01:00
", entries in Masternode list we asked for: " << (int)mWeAskedForMasternodeListEntry.size() <<
", nDsqCount: " << (int)nDsqCount;
return info.str();
}