neobytes/src/masternode-sync.cpp

432 lines
16 KiB
C++
Raw Normal View History

// Copyright (c) 2014-2016 The Dash Core developers
2015-07-15 04:44:58 +02:00
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include "main.h"
2015-07-29 05:26:08 +02:00
#include "activemasternode.h"
2015-07-15 04:44:58 +02:00
#include "masternode-sync.h"
2015-07-19 01:20:48 +02:00
#include "masternode-payments.h"
2016-04-15 04:54:11 +02:00
#include "governance.h"
2015-07-15 04:44:58 +02:00
#include "masternode.h"
#include "masternodeman.h"
#include "spork.h"
2015-07-15 04:44:58 +02:00
#include "util.h"
#include "addrman.h"
class CMasternodeSync;
CMasternodeSync masternodeSync;
CMasternodeSync::CMasternodeSync()
{
2015-08-04 20:21:27 +02:00
Reset();
2015-07-15 04:44:58 +02:00
}
bool CMasternodeSync::IsSynced()
{
2015-07-23 03:53:17 +02:00
return RequestedMasternodeAssets == MASTERNODE_SYNC_FINISHED;
2015-07-15 04:44:58 +02:00
}
bool CMasternodeSync::IsBlockchainSynced()
{
static bool fBlockchainSynced = false;
2015-08-04 23:15:24 +02:00
static int64_t lastProcess = GetTime();
2015-08-11 23:54:42 +02:00
// if the last call to this function was more than 60 minutes ago (client was in sleep mode) reset the sync process
if(GetTime() - lastProcess > 60*60) {
2015-08-04 23:15:24 +02:00
Reset();
fBlockchainSynced = false;
}
lastProcess = GetTime();
if(fBlockchainSynced) return true;
2015-08-03 22:42:18 +02:00
if (fImporting || fReindex) return false;
if(!pCurrentBlockIndex) return false;
if(pCurrentBlockIndex->nTime + 60*60 < GetTime()) return false;
2015-08-03 22:42:18 +02:00
fBlockchainSynced = true;
return true;
}
2015-08-04 20:21:27 +02:00
void CMasternodeSync::Reset()
{
lastMasternodeList = GetTime();
lastMasternodeWinner = GetTime();
lastBudgetItem = GetTime();
2015-08-05 04:10:47 +02:00
mapSeenSyncMNB.clear();
mapSeenSyncMNW.clear();
mapSeenSyncBudget.clear();
2015-08-04 20:21:27 +02:00
lastFailure = 0;
nCountFailures = 0;
sumMasternodeList = 0;
sumMasternodeWinner = 0;
sumBudgetItemProp = 0;
sumBudgetItemFin = 0;
countMasternodeList = 0;
countMasternodeWinner = 0;
countBudgetItemProp = 0;
countBudgetItemFin = 0;
RequestedMasternodeAssets = MASTERNODE_SYNC_INITIAL;
RequestedMasternodeAttempt = 0;
nAssetSyncStarted = GetTime();
2015-08-04 20:21:27 +02:00
}
void CMasternodeSync::AddedMasternodeList(uint256 hash)
2015-07-15 04:44:58 +02:00
{
if(mnodeman.mapSeenMasternodeBroadcast.count(hash)) {
lastMasternodeList = GetTime();
mapSeenSyncMNB[hash]++;
} else {
lastMasternodeList = GetTime();
mapSeenSyncMNB.insert(make_pair(hash, 1));
}
2015-07-15 04:44:58 +02:00
}
void CMasternodeSync::AddedMasternodeWinner(uint256 hash)
2015-07-15 04:44:58 +02:00
{
if(mnpayments.mapMasternodePayeeVotes.count(hash)) {
lastMasternodeWinner = GetTime();
mapSeenSyncMNW[hash]++;
} else {
lastMasternodeWinner = GetTime();
mapSeenSyncMNW.insert(make_pair(hash, 1));
}
2015-07-15 04:44:58 +02:00
}
2015-07-29 10:08:15 +02:00
bool CMasternodeSync::IsBudgetPropEmpty()
{
return sumBudgetItemProp==0 && countBudgetItemProp>0;
}
bool CMasternodeSync::IsBudgetFinEmpty()
{
return sumBudgetItemFin==0 && countBudgetItemFin>0;
}
std::string CMasternodeSync::GetAssetName()
{
switch(RequestedMasternodeAssets)
{
case(MASTERNODE_SYNC_INITIAL):
return "MASTERNODE_SYNC_INITIAL";
case(MASTERNODE_SYNC_FAILED): // should never be used here actually, use Reset() instead
return "MASTERNODE_SYNC_FAILED";
case(MASTERNODE_SYNC_SPORKS):
return "MASTERNODE_SYNC_SPORKS";
case(MASTERNODE_SYNC_LIST):
return "MASTERNODE_SYNC_LIST";
case(MASTERNODE_SYNC_MNW):
return "MASTERNODE_SYNC_MNW";
case(MASTERNODE_SYNC_GOVERNANCE):
return "MASTERNODE_SYNC_GOVERNANCE";
}
}
2015-07-15 04:44:58 +02:00
void CMasternodeSync::GetNextAsset()
{
switch(RequestedMasternodeAssets)
{
case(MASTERNODE_SYNC_INITIAL):
2015-08-04 23:33:47 +02:00
case(MASTERNODE_SYNC_FAILED): // should never be used here actually, use Reset() instead
2015-07-30 10:51:48 +02:00
ClearFulfilledRequest();
2015-08-04 23:33:47 +02:00
RequestedMasternodeAssets = MASTERNODE_SYNC_SPORKS;
break;
case(MASTERNODE_SYNC_SPORKS):
lastMasternodeList = GetTime();
2015-07-15 04:44:58 +02:00
RequestedMasternodeAssets = MASTERNODE_SYNC_LIST;
break;
case(MASTERNODE_SYNC_LIST):
lastMasternodeWinner = GetTime();
2015-07-15 04:44:58 +02:00
RequestedMasternodeAssets = MASTERNODE_SYNC_MNW;
break;
case(MASTERNODE_SYNC_MNW):
lastBudgetItem = GetTime();
RequestedMasternodeAssets = MASTERNODE_SYNC_GOVERNANCE;
2015-07-15 04:44:58 +02:00
break;
case(MASTERNODE_SYNC_GOVERNANCE):
LogPrintf("CMasternodeSync::GetNextAsset - Sync has finished\n");
RequestedMasternodeAssets = MASTERNODE_SYNC_FINISHED;
uiInterface.NotifyAdditionalDataSyncProgressChanged(1);
//try to activate our masternode if possible
activeMasternode.ManageState();
2015-07-15 04:44:58 +02:00
break;
}
RequestedMasternodeAttempt = 0;
nAssetSyncStarted = GetTime();
2015-07-15 04:44:58 +02:00
}
std::string CMasternodeSync::GetSyncStatus()
{
switch (masternodeSync.RequestedMasternodeAssets) {
2015-09-02 10:20:18 +02:00
case MASTERNODE_SYNC_INITIAL: return _("Synchronization pending...");
case MASTERNODE_SYNC_SPORKS: return _("Synchronizing sporks...");
case MASTERNODE_SYNC_LIST: return _("Synchronizing masternodes...");
case MASTERNODE_SYNC_MNW: return _("Synchronizing masternode winners...");
case MASTERNODE_SYNC_GOVERNANCE: return _("Synchronizing governance objects...");
case MASTERNODE_SYNC_FAILED: return _("Synchronization failed");
case MASTERNODE_SYNC_FINISHED: return _("Synchronization finished");
}
return "";
}
2015-07-29 06:16:11 +02:00
void CMasternodeSync::ProcessMessage(CNode* pfrom, std::string& strCommand, CDataStream& vRecv)
{
if (strCommand == NetMsgType::SYNCSTATUSCOUNT) { //Sync status count
2015-07-29 06:16:11 +02:00
int nItemID;
int nCount;
vRecv >> nItemID >> nCount;
if(RequestedMasternodeAssets >= MASTERNODE_SYNC_FINISHED) return;
//this means we will receive no further communication
switch(nItemID)
{
case(MASTERNODE_SYNC_LIST):
if(nItemID != RequestedMasternodeAssets) return;
sumMasternodeList += nCount;
countMasternodeList++;
break;
case(MASTERNODE_SYNC_MNW):
if(nItemID != RequestedMasternodeAssets) return;
sumMasternodeWinner += nCount;
countMasternodeWinner++;
break;
case(MASTERNODE_SYNC_GOVOBJ):
if(RequestedMasternodeAssets != MASTERNODE_SYNC_GOVERNANCE) return;
sumBudgetItemProp += nCount;
countBudgetItemProp++;
break;
case(MASTERNODE_SYNC_GOVERNANCE_FIN):
if(RequestedMasternodeAssets != MASTERNODE_SYNC_GOVERNANCE) return;
sumBudgetItemFin += nCount;
countBudgetItemFin++;
break;
2015-07-29 06:16:11 +02:00
}
2015-07-30 10:51:48 +02:00
LogPrintf("CMasternodeSync:ProcessMessage - ssc - got inventory count %d %d\n", nItemID, nCount);
}
}
void CMasternodeSync::ClearFulfilledRequest()
{
2015-07-30 15:44:18 +02:00
TRY_LOCK(cs_vNodes, lockRecv);
if(!lockRecv) return;
2015-07-30 10:51:48 +02:00
BOOST_FOREACH(CNode* pnode, vNodes)
{
2016-05-24 22:14:47 +02:00
pnode->ClearFulfilledRequest("spork-sync");
pnode->ClearFulfilledRequest("masternode-winner-sync");
pnode->ClearFulfilledRequest("governance-sync");
pnode->ClearFulfilledRequest("masternode-sync");
2015-07-29 06:16:11 +02:00
}
}
void CMasternodeSync::Process()
2015-07-15 04:44:58 +02:00
{
2015-07-19 10:19:54 +02:00
static int tick = 0;
if(tick++ % 6 != 0) return;
if(!pCurrentBlockIndex) return;
//the actual count of masternodes we have currently
int nMnCount = mnodeman.CountEnabled();
2015-07-19 10:19:54 +02:00
// RESET SYNCING INCASE OF FAILURE
{
if(IsSynced()) {
/*
Resync if we lose all masternodes from sleep/wake or failure to sync originally
*/
if(nMnCount == 0) {
Reset();
} else {
//if syncing is complete and we have masternodes, return
return;
}
}
//try syncing again
if(RequestedMasternodeAssets == MASTERNODE_SYNC_FAILED && lastFailure + (1*60) < GetTime()) {
2015-08-04 23:33:47 +02:00
Reset();
} else if (RequestedMasternodeAssets == MASTERNODE_SYNC_FAILED) {
2015-07-19 10:19:54 +02:00
return;
}
2015-07-18 01:49:41 +02:00
}
2015-07-15 04:44:58 +02:00
// INITIAL SYNC SETUP / LOG REPORTING
double nSyncProgress = double(RequestedMasternodeAttempt + (RequestedMasternodeAssets - 1) * 8) / (8*4);
LogPrintf("CMasternodeSync::Process() - tick %d RequestedMasternodeAttempt %d RequestedMasternodeAssets %d nSyncProgress %f\n", tick, RequestedMasternodeAttempt, RequestedMasternodeAssets, nSyncProgress);
uiInterface.NotifyAdditionalDataSyncProgressChanged(nSyncProgress);
// sporks synced but blockchain is not, wait until we're almost at a recent block to continue
if(Params().NetworkIDString() != CBaseChainParams::REGTEST &&
!IsBlockchainSynced() && RequestedMasternodeAssets > MASTERNODE_SYNC_SPORKS) return;
2015-07-15 04:44:58 +02:00
TRY_LOCK(cs_vNodes, lockRecv);
if(!lockRecv) return;
2015-08-03 22:42:18 +02:00
if(RequestedMasternodeAssets == MASTERNODE_SYNC_INITIAL) GetNextAsset();
2015-07-30 15:44:18 +02:00
BOOST_FOREACH(CNode* pnode, vNodes)
{
// QUICK MODE (REGTEST ONLY!)
if(Params().NetworkIDString() == CBaseChainParams::REGTEST)
{
if(RequestedMasternodeAttempt <= 2) {
pnode->PushMessage(NetMsgType::GETSPORKS); //get current network sporks
} else if(RequestedMasternodeAttempt < 4) {
mnodeman.DsegUpdate(pnode);
} else if(RequestedMasternodeAttempt < 6) {
2015-09-02 14:13:32 +02:00
int nMnCount = mnodeman.CountEnabled();
pnode->PushMessage(NetMsgType::MNWINNERSSYNC, nMnCount); //sync payees
uint256 n = uint256();
pnode->PushMessage(NetMsgType::MNGOVERNANCESYNC, n); //sync masternode votes
} else {
RequestedMasternodeAssets = MASTERNODE_SYNC_FINISHED;
}
RequestedMasternodeAttempt++;
2015-07-19 01:25:52 +02:00
return;
}
// NORMAL NETWORK MODE - TESTNET/MAINNET
{
// SPORK : ALWAYS ASK FOR SPORKS AS WE SYNC (we skip this mode now)
if(!pnode->HasFulfilledRequest("spork-sync")) {
// only request once from each peer
pnode->FulfilledRequest("spork-sync");
2015-07-15 04:44:58 +02:00
// get current network sporks
pnode->PushMessage(NetMsgType::GETSPORKS);
// we always ask for sporks, so just skip this
if(RequestedMasternodeAssets == MASTERNODE_SYNC_SPORKS) GetNextAsset();
continue; // always get sporks first, switch to the next node without waiting for the next tick
}
// MNLIST : SYNC MASTERNODE LIST FROM OTHER CONNECTED CLIENTS
2015-07-15 04:44:58 +02:00
if(RequestedMasternodeAssets == MASTERNODE_SYNC_LIST) {
// check for timeout first
if(lastMasternodeList < GetTime() - MASTERNODE_SYNC_TIMEOUT) {
LogPrintf("CMasternodeSync::Process -- tick %d asset %d -- timeout\n", tick, RequestedMasternodeAssets);
if(RequestedMasternodeAttempt == 0)
LogPrintf("CMasternodeSync::Process -- WARNING: failed to sync %s\n", GetAssetName());
GetNextAsset();
2015-07-15 04:44:58 +02:00
return;
}
// check for data
// if we have enough masternodes in or list, switch to the next asset
/* Note: Is this activing up? It's probably related to int CMasternodeMan::GetEstimatedMasternodes(int nBlock)
Surely doesn't work right for testnet currently */
// try to fetch data from at least one peer though
if(RequestedMasternodeAssets > 0 && nMnCount > mnodeman.GetEstimatedMasternodes(pCurrentBlockIndex->nHeight)*0.9) {
LogPrintf("CMasternodeSync::Process -- tick %d asset %d -- found enough data\n", tick, RequestedMasternodeAssets);
GetNextAsset();
return;
}
// only request once from each peer
if(pnode->HasFulfilledRequest("masternode-sync")) continue;
pnode->FulfilledRequest("masternode-sync");
if (pnode->nVersion < mnpayments.GetMinMasternodePaymentsProto()) continue;
RequestedMasternodeAttempt++;
mnodeman.DsegUpdate(pnode);
2016-02-04 23:48:23 +01:00
return; //this will cause each peer to get one request each six seconds for the various assets we need
}
2015-07-15 04:44:58 +02:00
// MNW : SYNC MASTERNODE WINNERS FROM OTHER CONNECTED CLIENTS
if(RequestedMasternodeAssets == MASTERNODE_SYNC_MNW) {
// check for timeout first
// This might take a lot longer than MASTERNODE_SYNC_TIMEOUT minutes due to new blocks,
// but that should be OK and it should timeout eventually.
if(lastMasternodeWinner < GetTime() - MASTERNODE_SYNC_TIMEOUT) {
LogPrintf("CMasternodeSync::Process -- tick %d asset %d -- timeout\n", tick, RequestedMasternodeAssets);
if(RequestedMasternodeAttempt == 0)
LogPrintf("CMasternodeSync::Process -- WARNING: failed to sync %s\n", GetAssetName());
GetNextAsset();
2015-07-15 04:44:58 +02:00
return;
}
// check for data
2016-05-29 20:35:09 +02:00
// if mnpayments already has enough blocks and votes, move to the next asset
// try to fetch data from at least one peer though
if(RequestedMasternodeAssets > 0 && mnpayments.IsEnoughData(nMnCount)) {
LogPrintf("CMasternodeSync::Process -- tick %d asset %d -- found enough data\n", tick, RequestedMasternodeAssets);
2016-05-29 20:35:09 +02:00
GetNextAsset();
return;
2015-08-14 05:56:58 +02:00
}
// only request once from each peer
if(pnode->HasFulfilledRequest("masternode-winner-sync")) continue;
pnode->FulfilledRequest("masternode-winner-sync");
if (pnode->nVersion < mnpayments.GetMinMasternodePaymentsProto()) continue;
RequestedMasternodeAttempt++;
pnode->PushMessage(NetMsgType::MNWINNERSSYNC, nMnCount); //sync payees
2016-02-04 23:48:23 +01:00
return; //this will cause each peer to get one request each six seconds for the various assets we need
}
// GOVOBJ : SYNC GOVERNANCE ITEMS FROM OUR PEERS
if(RequestedMasternodeAssets == MASTERNODE_SYNC_GOVERNANCE) {
// check for timeout first
if(lastBudgetItem < GetTime() - MASTERNODE_SYNC_TIMEOUT){
LogPrintf("CMasternodeSync::Process -- tick %d asset %d -- timeout\n", tick, RequestedMasternodeAssets);
if(RequestedMasternodeAttempt == 0)
LogPrintf("CMasternodeSync::Process -- WARNING: failed to sync %s\n", GetAssetName());
GetNextAsset();
return;
}
// check for data
2016-05-24 21:25:33 +02:00
// if(countBudgetItemProp > 0 && countBudgetItemFin)
// {
// if(governance.CountProposalInventoryItems() >= (sumBudgetItemProp / countBudgetItemProp)*0.9)
// {
// if(governance.CountFinalizedInventoryItems() >= (sumBudgetItemFin / countBudgetItemFin)*0.9)
// {
// GetNextAsset();
// return;
// }
// }
// }
// only request once from each peer
if(pnode->HasFulfilledRequest("governance-sync")) continue;
pnode->FulfilledRequest("governance-sync");
2016-05-24 21:25:33 +02:00
if (pnode->nVersion < MSG_GOVERNANCE_PEER_PROTO_VERSION) continue;
2016-05-24 21:25:33 +02:00
RequestedMasternodeAttempt++;
pnode->PushMessage(NetMsgType::MNGOVERNANCESYNC, uint256()); //sync masternode votes
2016-05-24 21:25:33 +02:00
return; //this will cause each peer to get one request each six seconds for the various assets we need
}
}
2015-07-15 04:44:58 +02:00
}
}
void CMasternodeSync::UpdatedBlockTip(const CBlockIndex *pindex)
{
pCurrentBlockIndex = pindex;
}
2016-05-09 18:31:11 +02:00
void CMasternodeSync::AddedBudgetItem(uint256 hash)
{
// skip this for now
return;
}