Line data Source code
1 : // Copyright (c) 2014-2015 The Dash developers 2 : // Copyright (c) 2015-2022 The PIVX Core developers 3 : // Distributed under the MIT software license, see the accompanying 4 : // file COPYING or https://www.opensource.org/licenses/mit-license.php. 5 : 6 : #ifndef PIVX_MASTERNODE_SYNC_H 7 : #define PIVX_MASTERNODE_SYNC_H 8 : 9 : #include "net.h" // for NodeId 10 : #include "uint256.h" 11 : 12 : #include <atomic> 13 : #include <string> 14 : #include <map> 15 : 16 : #define MASTERNODE_SYNC_TIMEOUT 5 17 : 18 : class CMasternodeSync; 19 : extern CMasternodeSync masternodeSync; 20 : 21 8618 : struct TierTwoPeerData { 22 : // map of message --> last request timestamp, bool hasResponseArrived. 23 : std::map<const char*, std::pair<int64_t, bool>> mapMsgData; 24 : }; 25 : 26 : // 27 : // CMasternodeSync : Sync masternode assets in stages 28 : // 29 : 30 : class CMasternodeSync 31 : { 32 : public: 33 : int64_t lastFailure; 34 : int nCountFailures; 35 : 36 : std::atomic<int64_t> lastProcess; 37 : 38 : // sum of all counts 39 : int sumMasternodeList; 40 : int sumMasternodeWinner; 41 : int sumBudgetItemProp; 42 : int sumBudgetItemFin; 43 : // peers that reported counts 44 : int countMasternodeList; 45 : int countMasternodeWinner; 46 : int countBudgetItemProp; 47 : int countBudgetItemFin; 48 : 49 : // Count peers we've requested the list from 50 : int RequestedMasternodeAttempt; 51 : 52 : // Time when current masternode asset sync started 53 : int64_t nAssetSyncStarted; 54 : 55 : CMasternodeSync(); 56 : 57 : void SwitchToNextAsset(); 58 : std::string GetSyncStatus(); 59 : void ProcessSyncStatusMsg(int nItemID, int itemCount); 60 : bool IsBudgetFinEmpty(); 61 : bool IsBudgetPropEmpty(); 62 : 63 : void Reset(); 64 : void Process(); 65 : /* 66 : * Process sync with a single node. 67 : * If it returns false, the Process() step is complete. 68 : * Otherwise Process() calls it again for a different node. 69 : */ 70 : bool SyncWithNode(CNode* pnode, bool fLegacyMnObsolete); 71 : bool NotCompleted(); 72 : void UpdateBlockchainSynced(bool isRegTestNet); 73 : void ClearFulfilledRequest(); 74 : 75 : // Sync message dispatcher 76 : bool MessageDispatcher(CNode* pfrom, std::string& strCommand, CDataStream& vRecv); 77 : 78 : private: 79 : 80 : // Tier two sync node state 81 : // map of nodeID --> TierTwoPeerData 82 : std::map<NodeId, TierTwoPeerData> peersSyncState; 83 : static int GetNextAsset(int currentAsset); 84 : 85 : void SyncRegtest(CNode* pnode); 86 : 87 : template <typename... Args> 88 : void RequestDataTo(CNode* pnode, const char* msg, bool forceRequest, Args&&... args); 89 : 90 : template <typename... Args> 91 : void PushMessage(CNode* pnode, const char* msg, Args&&... args); 92 : 93 : // update peer sync state data 94 : bool UpdatePeerSyncState(const NodeId& id, const char* msg, const int nextSyncStatus); 95 : 96 : // Check if an update is needed 97 : void CheckAndUpdateSyncStatus(); 98 : 99 : // Mark sync timeout 100 : void syncTimeout(const std::string& reason); 101 : }; 102 : 103 : #endif // PIVX_MASTERNODE_SYNC_H