2015-07-15 04:44:58 +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 "main.h"
# include "masternode-sync.h"
2015-07-19 01:20:48 +02:00
# include "masternode-payments.h"
2015-07-15 04:44:58 +02:00
# include "masternode.h"
2015-07-17 11:17:15 +02:00
# include "masternodeman.h"
2015-07-15 04:44:58 +02:00
# include "util.h"
# include "addrman.h"
class CMasternodeSync ;
CMasternodeSync masternodeSync ;
CMasternodeSync : : CMasternodeSync ( )
{
lastMasternodeList = 0 ;
lastMasternodeWinner = 0 ;
lastBudgetItem = 0 ;
2015-07-17 12:26:24 +02:00
RequestedMasternodeAssets = MASTERNODE_SYNC_INITIAL ;
2015-07-15 04:44:58 +02:00
RequestedMasternodeAttempt = 0 ;
}
bool CMasternodeSync : : IsSynced ( )
{
2015-07-17 12:26:24 +02:00
return ( RequestedMasternodeAssets = = MASTERNODE_SYNC_FINISHED ) ;
2015-07-15 04:44:58 +02:00
}
void CMasternodeSync : : AddedMasternodeList ( )
{
lastMasternodeList = GetTime ( ) ;
}
void CMasternodeSync : : AddedMasternodeWinner ( )
{
lastMasternodeWinner = GetTime ( ) ;
}
void CMasternodeSync : : AddedBudgetItem ( )
{
lastBudgetItem = GetTime ( ) ;
}
void CMasternodeSync : : GetNextAsset ( )
{
switch ( RequestedMasternodeAssets )
{
2015-07-17 12:26:24 +02:00
case ( MASTERNODE_SYNC_INITIAL ) :
2015-07-19 01:25:52 +02:00
lastMasternodeList = 0 ;
lastMasternodeWinner = 0 ;
lastBudgetItem = 0 ;
2015-07-17 12:26:24 +02:00
RequestedMasternodeAssets = MASTERNODE_SYNC_SPORKS ;
2015-07-17 05:03:42 +02:00
break ;
2015-07-17 12:26:24 +02:00
case ( MASTERNODE_SYNC_SPORKS ) :
2015-07-15 04:44:58 +02:00
RequestedMasternodeAssets = MASTERNODE_SYNC_LIST ;
break ;
case ( MASTERNODE_SYNC_LIST ) :
RequestedMasternodeAssets = MASTERNODE_SYNC_MNW ;
break ;
case ( MASTERNODE_SYNC_MNW ) :
RequestedMasternodeAssets = MASTERNODE_SYNC_BUDGET ;
break ;
case ( MASTERNODE_SYNC_BUDGET ) :
2015-07-19 17:49:46 +02:00
LogPrintf ( " CMasternodeSync::GetNextAsset - Sync has finished \n " ) ;
2015-07-17 12:26:24 +02:00
RequestedMasternodeAssets = MASTERNODE_SYNC_FINISHED ;
2015-07-15 04:44:58 +02:00
break ;
}
2015-07-17 11:17:15 +02:00
RequestedMasternodeAttempt = 0 ;
2015-07-15 04:44:58 +02:00
}
2015-07-17 11:17:15 +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 + + % MASTERNODE_SYNC_TIMEOUT ! = 0 ) return ;
CBlockIndex * pindexPrev = chainActive . Tip ( ) ;
if ( pindexPrev = = NULL ) return ;
2015-07-17 11:17:15 +02:00
2015-07-18 01:49:41 +02:00
if ( IsSynced ( ) ) {
/*
Resync if we lose all masternodes from sleep / wake or failure to sync originally
*/
if ( mnodeman . CountEnabled ( ) = = 0 ) {
RequestedMasternodeAssets = MASTERNODE_SYNC_INITIAL ;
2015-07-19 10:19:54 +02:00
} else
return ;
2015-07-18 01:49:41 +02:00
}
2015-07-15 04:44:58 +02:00
2015-07-19 10:19:54 +02:00
if ( fDebug ) LogPrintf ( " CMasternodeSync::Process() - tick %d RequestedMasternodeAssets %d \n " , tick , RequestedMasternodeAssets ) ;
2015-07-17 11:17:15 +02:00
2015-07-17 12:26:24 +02:00
if ( RequestedMasternodeAssets = = MASTERNODE_SYNC_INITIAL ) GetNextAsset ( ) ;
2015-07-15 04:44:58 +02:00
2015-07-17 11:17:15 +02:00
LOCK ( cs_vNodes ) ;
BOOST_FOREACH ( CNode * pnode , vNodes )
{
2015-07-17 05:03:42 +02:00
2015-07-19 01:25:52 +02:00
//set to synced
2015-07-19 16:51:37 +02:00
if ( Params ( ) . NetworkID ( ) = = CBaseChainParams : : REGTEST & & tick > = 10 ) {
2015-07-19 01:25:52 +02:00
LogPrintf ( " CMasternodeSync::Process - Sync has finished \n " ) ;
RequestedMasternodeAssets = MASTERNODE_SYNC_FINISHED ;
RequestedMasternodeAttempt = 0 ;
}
2015-07-17 05:03:42 +02:00
2015-07-19 01:25:52 +02:00
if ( RequestedMasternodeAssets = = MASTERNODE_SYNC_SPORKS ) {
if ( pnode - > HasFulfilledRequest ( " getspork " ) ) continue ;
pnode - > FulfilledRequest ( " getspork " ) ;
if ( RequestedMasternodeAttempt < = 2 ) {
pnode - > PushMessage ( " getsporks " ) ; //get current network sporks
if ( RequestedMasternodeAttempt = = 2 ) GetNextAsset ( ) ;
RequestedMasternodeAttempt + + ;
2015-07-17 11:17:15 +02:00
}
2015-07-19 01:25:52 +02:00
return ;
}
2015-07-19 10:19:54 +02:00
//don't begin syncing until we're almost at a recent block
2015-07-19 16:51:37 +02:00
if ( pindexPrev - > nHeight + 4 < pindexBestHeader - > nHeight & & pindexPrev - > nTime + 600 < GetTime ( ) ) return ;
2015-07-15 04:44:58 +02:00
2015-07-20 07:03:36 +02:00
if ( pnode - > nVersion > = masternodePayments . GetMinMasternodePaymentsProto ( ) ) {
2015-07-15 04:44:58 +02:00
2015-07-19 01:25:52 +02:00
if ( RequestedMasternodeAssets = = MASTERNODE_SYNC_LIST ) {
if ( fDebug ) LogPrintf ( " CMasternodeSync::Process() - lastMasternodeList %lld (GetTime() - MASTERNODE_SYNC_TIMEOUT) %lld \n " , lastMasternodeList , GetTime ( ) - MASTERNODE_SYNC_TIMEOUT ) ;
2015-07-17 11:17:15 +02:00
if ( lastMasternodeList > 0 & & lastMasternodeList < GetTime ( ) - MASTERNODE_SYNC_TIMEOUT ) { //hasn't received a new item in the last five seconds, so we'll move to the
GetNextAsset ( ) ;
2015-07-15 04:44:58 +02:00
return ;
}
2015-07-17 11:17:15 +02:00
if ( pnode - > HasFulfilledRequest ( " mnsync " ) ) continue ;
pnode - > FulfilledRequest ( " mnsync " ) ;
2015-07-15 04:44:58 +02:00
2015-07-17 11:17:15 +02:00
if ( ( lastMasternodeList = = 0 | | lastMasternodeList > GetTime ( ) - MASTERNODE_SYNC_TIMEOUT )
& & RequestedMasternodeAttempt < = 2 ) {
mnodeman . DsegUpdate ( pnode ) ;
RequestedMasternodeAttempt + + ;
}
return ;
}
2015-07-15 04:44:58 +02:00
2015-07-19 01:25:52 +02:00
if ( RequestedMasternodeAssets = = MASTERNODE_SYNC_MNW ) {
2015-07-17 11:17:15 +02:00
if ( lastMasternodeWinner > 0 & & lastMasternodeWinner < GetTime ( ) - MASTERNODE_SYNC_TIMEOUT ) { //hasn't received a new item in the last five seconds, so we'll move to the
GetNextAsset ( ) ;
2015-07-15 04:44:58 +02:00
return ;
}
2015-07-17 11:17:15 +02:00
if ( pnode - > HasFulfilledRequest ( " mnwsync " ) ) continue ;
pnode - > FulfilledRequest ( " mnwsync " ) ;
if ( ( lastMasternodeWinner = = 0 | | lastMasternodeWinner > GetTime ( ) - MASTERNODE_SYNC_TIMEOUT )
& & RequestedMasternodeAttempt < = 2 ) {
pnode - > PushMessage ( " mnget " ) ; //sync payees
RequestedMasternodeAttempt + + ;
}
return ;
}
2015-07-19 01:25:52 +02:00
}
if ( pnode - > nVersion > = MIN_BUDGET_PEER_PROTO_VERSION ) {
2015-07-17 11:17:15 +02:00
if ( RequestedMasternodeAssets = = MASTERNODE_SYNC_BUDGET ) {
if ( lastBudgetItem > 0 & & lastBudgetItem < GetTime ( ) - MASTERNODE_SYNC_TIMEOUT ) { //hasn't received a new item in the last five seconds, so we'll move to the
GetNextAsset ( ) ;
2015-07-15 04:44:58 +02:00
return ;
}
2015-07-17 11:17:15 +02:00
if ( pnode - > HasFulfilledRequest ( " busync " ) ) continue ;
pnode - > FulfilledRequest ( " busync " ) ;
if ( ( lastBudgetItem = = 0 | | lastBudgetItem > GetTime ( ) - MASTERNODE_SYNC_TIMEOUT )
& & RequestedMasternodeAttempt < = 2 ) {
uint256 n = 0 ;
pnode - > PushMessage ( " mnvs " , n ) ; //sync masternode votes
RequestedMasternodeAttempt + + ;
}
return ;
2015-07-15 04:44:58 +02:00
}
2015-07-17 11:17:15 +02:00
2015-07-15 04:44:58 +02:00
}
}
2015-07-17 11:17:15 +02:00
}