PIVX Core  5.6.99
P2P Digital Currency
tiertwo_sync_state.h
Go to the documentation of this file.
1 // Copyright (c) 2021 The PIVX Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or https://www.opensource.org/licenses/mit-license.php.
4 
5 #ifndef PIVX_TIERTWO_TIERTWO_SYNC_STATE_H
6 #define PIVX_TIERTWO_TIERTWO_SYNC_STATE_H
7 
8 #include <atomic>
9 #include <map>
10 
11 #define MASTERNODE_SYNC_INITIAL 0
12 #define MASTERNODE_SYNC_SPORKS 1
13 #define MASTERNODE_SYNC_LIST 2
14 #define MASTERNODE_SYNC_MNW 3
15 #define MASTERNODE_SYNC_BUDGET 4
16 #define MASTERNODE_SYNC_BUDGET_PROP 10
17 #define MASTERNODE_SYNC_BUDGET_FIN 11
18 #define MASTERNODE_SYNC_FAILED 998
19 #define MASTERNODE_SYNC_FINISHED 999
20 
21 // Sync threshold
22 #define MASTERNODE_SYNC_THRESHOLD 2
23 
24 // Chain sync update window.
25 // Be careful with this value. The smaller the value is, the more the tiertwo sync locks 'g_best_block_mutex'.
26 #define CHAIN_SYNC_UPDATE_TIME 30
27 
28 class uint256;
29 
31 public:
32  bool IsBlockchainSynced() const { return fBlockchainSynced; };
36 
37  // Update seen maps
38  void AddedMasternodeList(const uint256& hash);
39  void AddedMasternodeWinner(const uint256& hash);
40  void AddedBudgetItem(const uint256& hash);
41 
42  int64_t GetlastMasternodeList() const { return lastMasternodeList; }
43  int64_t GetlastMasternodeWinner() const { return lastMasternodeWinner; }
44  int64_t GetlastBudgetItem() const { return lastBudgetItem; }
45 
47 
48  void EraseSeenMNB(const uint256& hash) { mapSeenSyncMNB.erase(hash); }
49  void EraseSeenMNW(const uint256& hash) { mapSeenSyncMNW.erase(hash); }
50  void EraseSeenSyncBudget(const uint256& hash) { mapSeenSyncBudget.erase(hash); }
51 
52  // Reset seen data
53  void ResetData();
54 
55  // Only called from masternodesync and unit tests.
56  void SetBlockchainSync(bool f, int64_t cur_time) {
59  };
60  void SetCurrentSyncPhase(int sync_phase) { m_current_sync_phase = sync_phase; };
61  int GetSyncPhase() const { return m_current_sync_phase; }
62 
63  // True if the last chain sync update was more than CHAIN_SYNC_UPDATE_TIME seconds ago
64  bool CanUpdateChainSync(int64_t cur_time) const { return cur_time > last_blockchain_sync_update_time + CHAIN_SYNC_UPDATE_TIME; }
65 
66 private:
67  std::atomic<bool> fBlockchainSynced{false};
68  std::atomic<int64_t> last_blockchain_sync_update_time{0};
69  std::atomic<int> m_current_sync_phase{0};
70 
71  // Seen elements
72  std::map<uint256, int> mapSeenSyncMNB;
73  std::map<uint256, int> mapSeenSyncMNW;
74  std::map<uint256, int> mapSeenSyncBudget;
75  // Last seen time
76  int64_t lastMasternodeList{0};
78  int64_t lastBudgetItem{0};
79 };
80 
82 
83 #endif // PIVX_TIERTWO_TIERTWO_SYNC_STATE_H
bool IsSporkListSynced() const
std::map< uint256, int > mapSeenSyncMNB
void EraseSeenMNB(const uint256 &hash)
bool IsBlockchainSynced() const
void AddedMasternodeList(const uint256 &hash)
std::map< uint256, int > mapSeenSyncBudget
void AddedMasternodeWinner(const uint256 &hash)
std::atomic< int64_t > last_blockchain_sync_update_time
void EraseSeenSyncBudget(const uint256 &hash)
bool IsSynced() const
bool IsMasternodeListSynced() const
void EraseSeenMNW(const uint256 &hash)
int64_t GetlastMasternodeWinner() const
int64_t GetlastMasternodeList() const
std::atomic< bool > fBlockchainSynced
int GetSyncPhase() const
void AddedBudgetItem(const uint256 &hash)
bool CanUpdateChainSync(int64_t cur_time) const
std::atomic< int > m_current_sync_phase
void SetBlockchainSync(bool f, int64_t cur_time)
void SetCurrentSyncPhase(int sync_phase)
std::map< uint256, int > mapSeenSyncMNW
int64_t GetlastBudgetItem() const
256-bit opaque blob.
Definition: uint256.h:138
#define MASTERNODE_SYNC_SPORKS
#define CHAIN_SYNC_UPDATE_TIME
TierTwoSyncState g_tiertwo_sync_state
#define MASTERNODE_SYNC_FINISHED
#define MASTERNODE_SYNC_LIST