Line data Source code
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 : 30 : class TierTwoSyncState { 31 : public: 32 359405 : bool IsBlockchainSynced() const { return fBlockchainSynced; }; 33 175520 : bool IsSynced() const { return m_current_sync_phase == MASTERNODE_SYNC_FINISHED; } 34 96 : bool IsSporkListSynced() const { return m_current_sync_phase > MASTERNODE_SYNC_SPORKS; } 35 4 : bool IsMasternodeListSynced() const { return m_current_sync_phase > MASTERNODE_SYNC_LIST; } 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 245 : int64_t GetlastMasternodeList() const { return lastMasternodeList; } 43 245 : int64_t GetlastMasternodeWinner() const { return lastMasternodeWinner; } 44 245 : int64_t GetlastBudgetItem() const { return lastBudgetItem; } 45 : 46 0 : void ResetLastBudgetItem() { lastBudgetItem = 0; } 47 : 48 42 : void EraseSeenMNB(const uint256& hash) { mapSeenSyncMNB.erase(hash); } 49 0 : 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 4505 : void SetBlockchainSync(bool f, int64_t cur_time) { 57 4505 : fBlockchainSynced = f; 58 538 : last_blockchain_sync_update_time = cur_time; 59 3967 : }; 60 1526 : void SetCurrentSyncPhase(int sync_phase) { m_current_sync_phase = sync_phase; }; 61 111564 : 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 0 : 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}; 77 : int64_t lastMasternodeWinner{0}; 78 : int64_t lastBudgetItem{0}; 79 : }; 80 : 81 : extern TierTwoSyncState g_tiertwo_sync_state; 82 : 83 : #endif // PIVX_TIERTWO_TIERTWO_SYNC_STATE_H