PIVX Core  5.6.99
P2P Digital Currency
masternode-sync.h
Go to the documentation of this file.
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;
20 
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 
31 {
32 public:
33  int64_t lastFailure;
35 
36  std::atomic<int64_t> lastProcess;
37 
38  // sum of all counts
43  // peers that reported counts
48 
49  // Count peers we've requested the list from
51 
52  // Time when current masternode asset sync started
54 
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
98 
99  // Mark sync timeout
100  void syncTimeout(const std::string& reason);
101 };
102 
103 #endif // PIVX_MASTERNODE_SYNC_H
std::map< NodeId, TierTwoPeerData > peersSyncState
void ProcessSyncStatusMsg(int nItemID, int itemCount)
void CheckAndUpdateSyncStatus()
void UpdateBlockchainSynced(bool isRegTestNet)
bool SyncWithNode(CNode *pnode, bool fLegacyMnObsolete)
void RequestDataTo(CNode *pnode, const char *msg, bool forceRequest, Args &&... args)
void PushMessage(CNode *pnode, const char *msg, Args &&... args)
static int GetNextAsset(int currentAsset)
int RequestedMasternodeAttempt
void SyncRegtest(CNode *pnode)
std::atomic< int64_t > lastProcess
int64_t nAssetSyncStarted
std::string GetSyncStatus()
bool UpdatePeerSyncState(const NodeId &id, const char *msg, const int nextSyncStatus)
bool MessageDispatcher(CNode *pfrom, std::string &strCommand, CDataStream &vRecv)
void syncTimeout(const std::string &reason)
Information about a peer.
Definition: net.h:669
CMasternodeSync masternodeSync
int NodeId
Definition: net.h:109
std::map< const char *, std::pair< int64_t, bool > > mapMsgData