Refactor CMasternodeSync (#956)

This commit is contained in:
UdjinM6 2016-08-28 14:12:14 +04:00 committed by GitHub
parent f678ea48bd
commit 7b10f9aaa6
8 changed files with 214 additions and 228 deletions

View File

@ -2313,8 +2313,9 @@ void CDarksendPool::UpdatedBlockTip(const CBlockIndex *pindex)
pCurrentBlockIndex = pindex;
LogPrint("privatesend", "CDarksendPool::UpdatedBlockTip -- pCurrentBlockIndex->nHeight: %d\n", pCurrentBlockIndex->nHeight);
if(!fLiteMode && masternodeSync.RequestedMasternodeAssets > MASTERNODE_SYNC_LIST)
if(!fLiteMode && masternodeSync.GetAssetID() > MASTERNODE_SYNC_LIST) {
NewBlock();
}
}
//TODO: Rename/move to core
@ -2337,7 +2338,7 @@ void ThreadCheckDarkSendPool()
MilliSleep(1000);
// try to sync from all available nodes, one step at a time
masternodeSync.Process();
masternodeSync.ProcessTick();
if(masternodeSync.IsBlockchainSynced() && !ShutdownRequested()) {

View File

@ -1065,7 +1065,7 @@ void CGovernanceManager::UpdatedBlockTip(const CBlockIndex *pindex)
// TO REPROCESS OBJECTS WE SHOULD BE SYNCED
if(!fLiteMode && masternodeSync.RequestedMasternodeAssets > MASTERNODE_SYNC_LIST)
if(!fLiteMode && masternodeSync.GetAssetID() > MASTERNODE_SYNC_LIST)
NewBlock();
}

View File

@ -295,7 +295,7 @@ void CMasternodePayments::ProcessMessage(CNode* pfrom, std::string& strCommand,
if(!pCurrentBlockIndex) return;
// can't really verify it until masternode list is synced, reject it for now
if (masternodeSync.RequestedMasternodeAssets < MASTERNODE_SYNC_MNW) return;
if (masternodeSync.GetAssetID() < MASTERNODE_SYNC_MNW) return;
if(mapMasternodePayeeVotes.count(winner.GetHash())) {
LogPrint("mnpayments", "MNWINNER -- Already seen: hash=%s, nHeight=%d\n", winner.GetHash().ToString(), pCurrentBlockIndex->nHeight);
@ -760,6 +760,7 @@ void CMasternodePayments::UpdatedBlockTip(const CBlockIndex *pindex)
pCurrentBlockIndex = pindex;
LogPrint("mnpayments", "pCurrentBlockIndex->nHeight: %d\n", pCurrentBlockIndex->nHeight);
if(!fLiteMode && masternodeSync.RequestedMasternodeAssets > MASTERNODE_SYNC_LIST)
ProcessBlock(pindex->nHeight+10);
if (!fLiteMode && masternodeSync.GetAssetID() > MASTERNODE_SYNC_LIST) {
ProcessBlock(pindex->nHeight + 10);
}
}

View File

@ -2,30 +2,19 @@
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include "main.h"
#include "activemasternode.h"
#include "masternode-sync.h"
#include "masternode-payments.h"
#include "governance.h"
#include "main.h"
#include "masternode.h"
#include "masternode-payments.h"
#include "masternode-sync.h"
#include "masternodeman.h"
#include "spork.h"
#include "util.h"
#include "addrman.h"
class CMasternodeSync;
CMasternodeSync masternodeSync;
CMasternodeSync::CMasternodeSync()
{
Reset();
}
bool CMasternodeSync::IsSynced()
{
return RequestedMasternodeAssets == MASTERNODE_SYNC_FINISHED;
}
bool CMasternodeSync::IsBlockchainSynced()
{
static bool fBlockchainSynced = false;
@ -50,126 +39,117 @@ bool CMasternodeSync::IsBlockchainSynced()
return true;
}
void CMasternodeSync::Fail()
{
nTimeLastFailure = GetTime();
nRequestedMasternodeAssets = MASTERNODE_SYNC_FAILED;
}
void CMasternodeSync::Reset()
{
lastMasternodeList = GetTime();
lastMasternodeWinner = GetTime();
lastBudgetItem = GetTime();
nRequestedMasternodeAssets = MASTERNODE_SYNC_INITIAL;
nRequestedMasternodeAttempt = 0;
nTimeAssetSyncStarted = GetTime();
nTimeLastMasternodeList = GetTime();
nTimeLastMasternodeWinner = GetTime();
nTimeLastBudgetItem = GetTime();
nTimeLastFailure = 0;
nCountFailures = 0;
nSumMasternodeList = 0;
nSumMasternodeWinner = 0;
nSumBudgetItemProp = 0;
nSumBudgetItemFin = 0;
nCountMasternodeList = 0;
nCountMasternodeWinner = 0;
nCountBudgetItemProp = 0;
nCountBudgetItemFin = 0;
mapSeenSyncMNB.clear();
mapSeenSyncMNW.clear();
mapSeenSyncBudget.clear();
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();
}
void CMasternodeSync::AddedMasternodeList(uint256 hash)
{
if(mnodeman.mapSeenMasternodeBroadcast.count(hash)) {
lastMasternodeList = GetTime();
nTimeLastMasternodeList = GetTime();
mapSeenSyncMNB[hash]++;
} else {
lastMasternodeList = GetTime();
mapSeenSyncMNB.insert(make_pair(hash, 1));
nTimeLastMasternodeList = GetTime();
mapSeenSyncMNB.insert(std::make_pair(hash, 1));
}
}
void CMasternodeSync::AddedMasternodeWinner(uint256 hash)
{
if(mnpayments.mapMasternodePayeeVotes.count(hash)) {
lastMasternodeWinner = GetTime();
nTimeLastMasternodeWinner = GetTime();
mapSeenSyncMNW[hash]++;
} else {
lastMasternodeWinner = GetTime();
mapSeenSyncMNW.insert(make_pair(hash, 1));
nTimeLastMasternodeWinner = GetTime();
mapSeenSyncMNW.insert(std::make_pair(hash, 1));
}
}
bool CMasternodeSync::IsBudgetPropEmpty()
{
return sumBudgetItemProp==0 && countBudgetItemProp>0;
}
bool CMasternodeSync::IsBudgetFinEmpty()
{
return sumBudgetItemFin==0 && countBudgetItemFin>0;
}
std::string CMasternodeSync::GetAssetName()
{
switch(RequestedMasternodeAssets)
switch(nRequestedMasternodeAssets)
{
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";
case(MASTERNODE_SYNC_INITIAL): return "MASTERNODE_SYNC_INITIAL";
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";
case(MASTERNODE_SYNC_FAILED): return "MASTERNODE_SYNC_FAILED";
case MASTERNODE_SYNC_FINISHED: return "MASTERNODE_SYNC_FINISHED";
default: return "UNKNOWN";
}
return "error";
}
void CMasternodeSync::GetNextAsset()
void CMasternodeSync::SwitchToNextAsset()
{
switch(RequestedMasternodeAssets)
switch(nRequestedMasternodeAssets)
{
case(MASTERNODE_SYNC_INITIAL):
case(MASTERNODE_SYNC_FAILED): // should never be used here actually, use Reset() instead
ClearFulfilledRequest();
RequestedMasternodeAssets = MASTERNODE_SYNC_SPORKS;
nRequestedMasternodeAssets = MASTERNODE_SYNC_SPORKS;
break;
case(MASTERNODE_SYNC_SPORKS):
lastMasternodeList = GetTime();
RequestedMasternodeAssets = MASTERNODE_SYNC_LIST;
nTimeLastMasternodeList = GetTime();
nRequestedMasternodeAssets = MASTERNODE_SYNC_LIST;
break;
case(MASTERNODE_SYNC_LIST):
lastMasternodeWinner = GetTime();
RequestedMasternodeAssets = MASTERNODE_SYNC_MNW;
nTimeLastMasternodeWinner = GetTime();
nRequestedMasternodeAssets = MASTERNODE_SYNC_MNW;
break;
case(MASTERNODE_SYNC_MNW):
lastBudgetItem = GetTime();
RequestedMasternodeAssets = MASTERNODE_SYNC_GOVERNANCE;
nTimeLastBudgetItem = GetTime();
nRequestedMasternodeAssets = MASTERNODE_SYNC_GOVERNANCE;
break;
case(MASTERNODE_SYNC_GOVERNANCE):
LogPrintf("CMasternodeSync::GetNextAsset - Sync has finished\n");
RequestedMasternodeAssets = MASTERNODE_SYNC_FINISHED;
LogPrintf("CMasternodeSync::SwitchToNextAsset -- Sync has finished\n");
nRequestedMasternodeAssets = MASTERNODE_SYNC_FINISHED;
uiInterface.NotifyAdditionalDataSyncProgressChanged(1);
//try to activate our masternode if possible
activeMasternode.ManageState();
break;
}
RequestedMasternodeAttempt = 0;
nAssetSyncStarted = GetTime();
nRequestedMasternodeAttempt = 0;
nTimeAssetSyncStarted = GetTime();
}
std::string CMasternodeSync::GetSyncStatus()
{
switch (masternodeSync.RequestedMasternodeAssets) {
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");
switch (masternodeSync.nRequestedMasternodeAssets) {
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");
default: return "";
}
return "";
}
void CMasternodeSync::ProcessMessage(CNode* pfrom, std::string& strCommand, CDataStream& vRecv)
@ -179,34 +159,34 @@ void CMasternodeSync::ProcessMessage(CNode* pfrom, std::string& strCommand, CDat
int nCount;
vRecv >> nItemID >> nCount;
if(RequestedMasternodeAssets >= MASTERNODE_SYNC_FINISHED) return;
if(nRequestedMasternodeAssets >= 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++;
if(nItemID != nRequestedMasternodeAssets) return;
nSumMasternodeList += nCount;
nCountMasternodeList++;
break;
case(MASTERNODE_SYNC_MNW):
if(nItemID != RequestedMasternodeAssets) return;
sumMasternodeWinner += nCount;
countMasternodeWinner++;
if(nItemID != nRequestedMasternodeAssets) return;
nSumMasternodeWinner += nCount;
nCountMasternodeWinner++;
break;
case(MASTERNODE_SYNC_GOVOBJ):
if(RequestedMasternodeAssets != MASTERNODE_SYNC_GOVERNANCE) return;
sumBudgetItemProp += nCount;
countBudgetItemProp++;
if(nRequestedMasternodeAssets != MASTERNODE_SYNC_GOVERNANCE) return;
nSumBudgetItemProp += nCount;
nCountBudgetItemProp++;
break;
case(MASTERNODE_SYNC_GOVERNANCE_FIN):
if(RequestedMasternodeAssets != MASTERNODE_SYNC_GOVERNANCE) return;
sumBudgetItemFin += nCount;
countBudgetItemFin++;
if(nRequestedMasternodeAssets != MASTERNODE_SYNC_GOVERNANCE) return;
nSumBudgetItemFin += nCount;
nCountBudgetItemFin++;
break;
}
LogPrintf("CMasternodeSync:ProcessMessage - ssc - got inventory count %d %d\n", nItemID, nCount);
LogPrintf("CMasternodeSync::ProcessMessage -- SYNCSTATUSCOUNT -- got inventory count: nItemID=%d nCount=%d\n", nItemID, nCount);
}
}
@ -224,10 +204,10 @@ void CMasternodeSync::ClearFulfilledRequest()
}
}
void CMasternodeSync::Process()
void CMasternodeSync::ProcessTick()
{
static int tick = 0;
if(tick++ % 6 != 0) return;
static int nTick = 0;
if(nTick++ % 6 != 0) return;
if(!pCurrentBlockIndex) return;
//the actual count of masternodes we have currently
@ -248,45 +228,45 @@ void CMasternodeSync::Process()
}
//try syncing again
if(RequestedMasternodeAssets == MASTERNODE_SYNC_FAILED && lastFailure + (1*60) < GetTime()) {
Reset();
} else if (RequestedMasternodeAssets == MASTERNODE_SYNC_FAILED) {
if(IsFailed()) {
if(nTimeLastFailure + (1*60) < GetTime()) // 1 minute cooldown after failed sync
Reset();
return;
}
}
// 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);
double nSyncProgress = double(nRequestedMasternodeAttempt + (nRequestedMasternodeAssets - 1) * 8) / (8*4);
LogPrintf("CMasternodeSync::Process -- nTick %d nRequestedMasternodeAssets %d nRequestedMasternodeAttempt %d nSyncProgress %f\n", nTick, nRequestedMasternodeAssets, nRequestedMasternodeAttempt, 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;
!IsBlockchainSynced() && nRequestedMasternodeAssets > MASTERNODE_SYNC_SPORKS) return;
TRY_LOCK(cs_vNodes, lockRecv);
if(!lockRecv) return;
if(RequestedMasternodeAssets == MASTERNODE_SYNC_INITIAL) GetNextAsset();
if(nRequestedMasternodeAssets == MASTERNODE_SYNC_INITIAL) SwitchToNextAsset();
BOOST_FOREACH(CNode* pnode, vNodes)
{
// QUICK MODE (REGTEST ONLY!)
if(Params().NetworkIDString() == CBaseChainParams::REGTEST)
{
if(RequestedMasternodeAttempt <= 2) {
if(nRequestedMasternodeAttempt <= 2) {
pnode->PushMessage(NetMsgType::GETSPORKS); //get current network sporks
} else if(RequestedMasternodeAttempt < 4) {
} else if(nRequestedMasternodeAttempt < 4) {
mnodeman.DsegUpdate(pnode);
} else if(RequestedMasternodeAttempt < 6) {
} else if(nRequestedMasternodeAttempt < 6) {
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;
nRequestedMasternodeAssets = MASTERNODE_SYNC_FINISHED;
}
RequestedMasternodeAttempt++;
nRequestedMasternodeAttempt++;
return;
}
@ -302,20 +282,21 @@ void CMasternodeSync::Process()
pnode->PushMessage(NetMsgType::GETSPORKS);
// we always ask for sporks, so just skip this
if(RequestedMasternodeAssets == MASTERNODE_SYNC_SPORKS) GetNextAsset();
if(nRequestedMasternodeAssets == MASTERNODE_SYNC_SPORKS) SwitchToNextAsset();
continue; // always get sporks first, switch to the next node without waiting for the next tick
}
// MNLIST : SYNC MASTERNODE LIST FROM OTHER CONNECTED CLIENTS
if(RequestedMasternodeAssets == MASTERNODE_SYNC_LIST) {
if(nRequestedMasternodeAssets == 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)
if(nTimeLastMasternodeList < GetTime() - MASTERNODE_SYNC_TIMEOUT_SECONDS) {
LogPrintf("CMasternodeSync::Process -- nTick %d nRequestedMasternodeAssets %d -- timeout\n", nTick, nRequestedMasternodeAssets);
if (nRequestedMasternodeAttempt == 0) {
LogPrintf("CMasternodeSync::Process -- WARNING: failed to sync %s\n", GetAssetName());
GetNextAsset();
}
SwitchToNextAsset();
return;
}
@ -323,10 +304,10 @@ void CMasternodeSync::Process()
// 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();
// try to fetch data from at least two peers though
if(nRequestedMasternodeAttempt > 1 && nMnCount > mnodeman.GetEstimatedMasternodes(pCurrentBlockIndex->nHeight)*0.9) {
LogPrintf("CMasternodeSync::Process -- nTick %d nRequestedMasternodeAssets %d -- found enough data\n", nTick, nRequestedMasternodeAssets);
SwitchToNextAsset();
return;
}
@ -335,7 +316,7 @@ void CMasternodeSync::Process()
pnode->FulfilledRequest("masternode-sync");
if (pnode->nVersion < mnpayments.GetMinMasternodePaymentsProto()) continue;
RequestedMasternodeAttempt++;
nRequestedMasternodeAttempt++;
mnodeman.DsegUpdate(pnode);
@ -344,24 +325,25 @@ void CMasternodeSync::Process()
// MNW : SYNC MASTERNODE WINNERS FROM OTHER CONNECTED CLIENTS
if(RequestedMasternodeAssets == MASTERNODE_SYNC_MNW) {
if(nRequestedMasternodeAssets == MASTERNODE_SYNC_MNW) {
// check for timeout first
// This might take a lot longer than MASTERNODE_SYNC_TIMEOUT minutes due to new blocks,
// This might take a lot longer than MASTERNODE_SYNC_TIMEOUT_SECONDS 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)
if(nTimeLastMasternodeWinner < GetTime() - MASTERNODE_SYNC_TIMEOUT_SECONDS) {
LogPrintf("CMasternodeSync::Process -- nTick %d nRequestedMasternodeAssets %d -- timeout\n", nTick, nRequestedMasternodeAssets);
if (nRequestedMasternodeAttempt == 0) {
LogPrintf("CMasternodeSync::Process -- WARNING: failed to sync %s\n", GetAssetName());
GetNextAsset();
}
SwitchToNextAsset();
return;
}
// check for data
// 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);
GetNextAsset();
// if mnpayments already has enough blocks and votes, switch to the next asset
// try to fetch data from at least two peers though
if(nRequestedMasternodeAttempt > 1 && mnpayments.IsEnoughData(nMnCount)) {
LogPrintf("CMasternodeSync::Process -- nTick %d nRequestedMasternodeAssets %d -- found enough data\n", nTick, nRequestedMasternodeAssets);
SwitchToNextAsset();
return;
}
@ -369,8 +351,8 @@ void CMasternodeSync::Process()
if(pnode->HasFulfilledRequest("masternode-winner-sync")) continue;
pnode->FulfilledRequest("masternode-winner-sync");
if (pnode->nVersion < mnpayments.GetMinMasternodePaymentsProto()) continue;
RequestedMasternodeAttempt++;
if(pnode->nVersion < mnpayments.GetMinMasternodePaymentsProto()) continue;
nRequestedMasternodeAttempt++;
pnode->PushMessage(NetMsgType::MNWINNERSSYNC, nMnCount); //sync payees
@ -380,36 +362,37 @@ void CMasternodeSync::Process()
// GOVOBJ : SYNC GOVERNANCE ITEMS FROM OUR PEERS
if(RequestedMasternodeAssets == MASTERNODE_SYNC_GOVERNANCE) {
if(nRequestedMasternodeAssets == 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)
if(nTimeLastBudgetItem < GetTime() - MASTERNODE_SYNC_TIMEOUT_SECONDS){
LogPrintf("CMasternodeSync::Process -- nTick %d nRequestedMasternodeAssets %d -- timeout\n", nTick, nRequestedMasternodeAssets);
if(nRequestedMasternodeAttempt == 0) {
LogPrintf("CMasternodeSync::Process -- WARNING: failed to sync %s\n", GetAssetName());
GetNextAsset();
// it's kind of ok to skip this for now, hopefully we'll catch up later?
}
SwitchToNextAsset();
return;
}
// check for data
// if(countBudgetItemProp > 0 && countBudgetItemFin)
// if(nCountBudgetItemProp > 0 && nCountBudgetItemFin)
// {
// if(governance.CountProposalInventoryItems() >= (sumBudgetItemProp / countBudgetItemProp)*0.9)
// if(governance.CountProposalInventoryItems() >= (nSumBudgetItemProp / nCountBudgetItemProp)*0.9)
// {
// if(governance.CountFinalizedInventoryItems() >= (sumBudgetItemFin / countBudgetItemFin)*0.9)
// if(governance.CountFinalizedInventoryItems() >= (nSumBudgetItemFin / nCountBudgetItemFin)*0.9)
// {
// GetNextAsset();
// SwitchToNextAsset();
// return;
// }
// }
// }
// only request once from each peer
if(pnode->HasFulfilledRequest("governance-sync")) continue;
pnode->FulfilledRequest("governance-sync");
if (pnode->nVersion < MSG_GOVERNANCE_PEER_PROTO_VERSION) continue;
RequestedMasternodeAttempt++;
nRequestedMasternodeAttempt++;
pnode->PushMessage(NetMsgType::MNGOVERNANCESYNC, uint256()); //sync masternode votes

View File

@ -1,23 +1,28 @@
// Copyright (c) 2014-2016 The Dash Core developers
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#ifndef MASTERNODE_SYNC_H
#define MASTERNODE_SYNC_H
#define MASTERNODE_SYNC_INITIAL 0
#define MASTERNODE_SYNC_SPORKS 1
#define MASTERNODE_SYNC_LIST 2
#define MASTERNODE_SYNC_MNW 3
#define MASTERNODE_SYNC_GOVERNANCE 4
#define MASTERNODE_SYNC_GOVOBJ 10
#define MASTERNODE_SYNC_GOVERNANCE_FIN 11
#define MASTERNODE_SYNC_FAILED 998
#define MASTERNODE_SYNC_FINISHED 999
#include "chain.h"
#include "net.h"
#define MASTERNODE_SYNC_TIMEOUT 30 // our blocks are 2.5 minutes so 30 seconds should be fine
#include <univalue.h>
class CMasternodeSync;
static const int MASTERNODE_SYNC_INITIAL = 0;
static const int MASTERNODE_SYNC_SPORKS = 1;
static const int MASTERNODE_SYNC_LIST = 2;
static const int MASTERNODE_SYNC_MNW = 3;
static const int MASTERNODE_SYNC_GOVERNANCE = 4;
static const int MASTERNODE_SYNC_GOVOBJ = 10;
static const int MASTERNODE_SYNC_GOVERNANCE_FIN = 11;
static const int MASTERNODE_SYNC_FAILED = 998;
static const int MASTERNODE_SYNC_FINISHED = 999;
static const int MASTERNODE_SYNC_TIMEOUT_SECONDS = 30; // our blocks are 2.5 minutes so 30 seconds should be fine
extern CMasternodeSync masternodeSync;
//
@ -26,55 +31,68 @@ extern CMasternodeSync masternodeSync;
class CMasternodeSync
{
private:
// Keep track of current asset
int nRequestedMasternodeAssets;
// Count peers we've requested the asset from
int nRequestedMasternodeAttempt;
// Time when current masternode asset sync started
int64_t nTimeAssetSyncStarted;
// Last time when we received some masternode asset ...
int64_t nTimeLastMasternodeList;
int64_t nTimeLastMasternodeWinner;
int64_t nTimeLastBudgetItem;
// ... or failed
int64_t nTimeLastFailure;
// How many times we failed
int nCountFailures;
// Sum of all data counts
int nSumMasternodeList;
int nSumMasternodeWinner;
int nSumBudgetItemProp;
int nSumBudgetItemFin;
// Number of peers that reported data counts
int nCountMasternodeList;
int nCountMasternodeWinner;
int nCountBudgetItemProp;
int nCountBudgetItemFin;
// Keep track of current block index
const CBlockIndex *pCurrentBlockIndex;
void Fail();
void ClearFulfilledRequest();
public:
std::map<uint256, int> mapSeenSyncMNB;
std::map<uint256, int> mapSeenSyncMNW;
std::map<uint256, int> mapSeenSyncBudget;
int64_t lastMasternodeList;
int64_t lastMasternodeWinner;
int64_t lastBudgetItem;
int64_t lastFailure;
int nCountFailures;
// sum of all counts
int sumMasternodeList;
int sumMasternodeWinner;
int sumBudgetItemProp;
int sumBudgetItemFin;
// peers that reported counts
int countMasternodeList;
int countMasternodeWinner;
int countBudgetItemProp;
int countBudgetItemFin;
// Count peers we've requested the list from
int RequestedMasternodeAssets;
int RequestedMasternodeAttempt;
// Time when current masternode asset sync started
int64_t nAssetSyncStarted;
// Keep track of current block index
const CBlockIndex *pCurrentBlockIndex;
CMasternodeSync();
CMasternodeSync() { Reset(); }
void AddedMasternodeList(uint256 hash);
void AddedMasternodeWinner(uint256 hash);
void AddedBudgetItem(uint256 hash);
void GetNextAsset();
bool IsFailed() { return nRequestedMasternodeAssets == MASTERNODE_SYNC_FAILED; }
bool IsSynced() { return nRequestedMasternodeAssets == MASTERNODE_SYNC_FINISHED; }
bool IsBlockchainSynced();
int GetAssetID() { return nRequestedMasternodeAssets; }
int GetAttempt() { return nRequestedMasternodeAttempt; }
std::string GetAssetName();
std::string GetSyncStatus();
void ProcessMessage(CNode* pfrom, std::string& strCommand, CDataStream& vRecv);
bool IsBudgetFinEmpty();
bool IsBudgetPropEmpty();
void Reset();
void Process();
bool IsSynced();
bool IsBlockchainSynced();
void ClearFulfilledRequest();
void SwitchToNextAsset();
void ProcessMessage(CNode* pfrom, std::string& strCommand, CDataStream& vRecv);
void ProcessTick();
void UpdatedBlockTip(const CBlockIndex *pindex);
};

View File

@ -363,8 +363,7 @@ void MasternodeList::on_startAllButton_clicked()
void MasternodeList::on_startMissingButton_clicked()
{
if(masternodeSync.RequestedMasternodeAssets <= MASTERNODE_SYNC_LIST ||
masternodeSync.RequestedMasternodeAssets == MASTERNODE_SYNC_FAILED) {
if(masternodeSync.GetAssetID() <= MASTERNODE_SYNC_LIST || masternodeSync.IsFailed()) {
QMessageBox::critical(this, tr("Command is not available right now"),
tr("You can't use this command until masternode list is synced"));
return;

View File

@ -320,9 +320,8 @@ UniValue masternode(const UniValue& params, bool fHelp)
}
if((strCommand == "start-missing" || strCommand == "start-disabled") &&
(masternodeSync.RequestedMasternodeAssets <= MASTERNODE_SYNC_LIST ||
masternodeSync.RequestedMasternodeAssets == MASTERNODE_SYNC_FAILED)) {
throw JSONRPCError(RPC_CLIENT_IN_INITIAL_DOWNLOAD, "You can't use this command until masternode list is synced");
(masternodeSync.GetAssetID() <= MASTERNODE_SYNC_LIST || masternodeSync.IsFailed())) {
throw JSONRPCError(RPC_CLIENT_IN_INITIAL_DOWNLOAD, "You can't use this command until masternode list is synced");
}
int successful = 0;

View File

@ -150,34 +150,19 @@ UniValue mnsync(const UniValue& params, bool fHelp)
std::string strMode = params[0].get_str();
if(strMode == "status") {
UniValue obj(UniValue::VOBJ);
obj.push_back(Pair("IsBlockchainSynced", masternodeSync.IsBlockchainSynced()));
obj.push_back(Pair("IsSynced", masternodeSync.IsSynced()));
obj.push_back(Pair("CurrentSyncingAssetName", masternodeSync.GetAssetName()));
obj.push_back(Pair("lastMasternodeList", masternodeSync.lastMasternodeList));
obj.push_back(Pair("lastMasternodeWinner", masternodeSync.lastMasternodeWinner));
obj.push_back(Pair("lastBudgetItem", masternodeSync.lastBudgetItem));
obj.push_back(Pair("lastFailure", masternodeSync.lastFailure));
obj.push_back(Pair("nCountFailures", masternodeSync.nCountFailures));
obj.push_back(Pair("sumMasternodeList", masternodeSync.sumMasternodeList));
obj.push_back(Pair("sumMasternodeWinner", masternodeSync.sumMasternodeWinner));
obj.push_back(Pair("sumBudgetItemProp", masternodeSync.sumBudgetItemProp));
obj.push_back(Pair("sumBudgetItemFin", masternodeSync.sumBudgetItemFin));
obj.push_back(Pair("countMasternodeList", masternodeSync.countMasternodeList));
obj.push_back(Pair("countMasternodeWinner", masternodeSync.countMasternodeWinner));
obj.push_back(Pair("countBudgetItemProp", masternodeSync.countBudgetItemProp));
obj.push_back(Pair("countBudgetItemFin", masternodeSync.countBudgetItemFin));
obj.push_back(Pair("RequestedMasternodeAssets", masternodeSync.RequestedMasternodeAssets));
obj.push_back(Pair("RequestedMasternodeAttempt", masternodeSync.RequestedMasternodeAttempt));
return obj;
UniValue objStatus(UniValue::VOBJ);
objStatus.push_back(Pair("AssetID", masternodeSync.GetAssetID()));
objStatus.push_back(Pair("AssetName", masternodeSync.GetAssetName()));
objStatus.push_back(Pair("Attempt", masternodeSync.GetAttempt()));
objStatus.push_back(Pair("IsBlockchainSynced", masternodeSync.IsBlockchainSynced()));
objStatus.push_back(Pair("IsSynced", masternodeSync.IsSynced()));
objStatus.push_back(Pair("IsFailed", masternodeSync.IsFailed()));
return objStatus;
}
if(strMode == "next")
{
masternodeSync.GetNextAsset();
masternodeSync.SwitchToNextAsset();
return "sync updated to " + masternodeSync.GetAssetName();
}