LCOV - code coverage report
Current view: top level - src/masternode - sync.h (source / functions) Hit Total Coverage
Test: test_dash_coverage.info Lines: 2 9 22.2 %
Date: 2026-06-25 07:23:51 Functions: 3 15 20.0 %

          Line data    Source code
       1             : // Copyright (c) 2014-2025 The Dash Core developers
       2             : // Distributed under the MIT/X11 software license, see the accompanying
       3             : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
       4             : #ifndef BITCOIN_MASTERNODE_SYNC_H
       5             : #define BITCOIN_MASTERNODE_SYNC_H
       6             : 
       7             : #include <atomic>
       8             : #include <memory>
       9             : #include <string>
      10             : 
      11             : class CBlockIndex;
      12             : 
      13             : /** Default for -syncmempool */
      14             : static const bool DEFAULT_SYNC_MEMPOOL = true;
      15             : 
      16             : static constexpr int MASTERNODE_SYNC_BLOCKCHAIN      = 1;
      17             : static constexpr int MASTERNODE_SYNC_GOVERNANCE      = 4;
      18             : static constexpr int MASTERNODE_SYNC_GOVOBJ          = 10;
      19             : static constexpr int MASTERNODE_SYNC_GOVOBJ_VOTE     = 11;
      20             : static constexpr int MASTERNODE_SYNC_FINISHED        = 999;
      21             : 
      22             : static constexpr int MASTERNODE_SYNC_TICK_SECONDS    = 6;
      23             : static constexpr int MASTERNODE_SYNC_TIMEOUT_SECONDS = 30; // our blocks are 2.5 minutes so 30 seconds should be fine
      24             : static constexpr int MASTERNODE_SYNC_RESET_SECONDS   = 900; // Reset fReachedBestHeader in CMasternodeSync::Reset if UpdateBlockTip hasn't been called for this seconds
      25             : 
      26             : class NodeSyncNotifier
      27             : {
      28             : public:
      29             :     virtual void SyncReset() = 0;
      30             :     virtual void SyncFinished() = 0;
      31             : 
      32         184 :     virtual ~NodeSyncNotifier() = default;
      33             : };
      34             : 
      35             : /** Stub implementation for use in chainstate-only (non-network) contexts.
      36             :  *  CMasternodeSync constructed with this notifier permanently returns
      37             :  *  IsBlockchainSynced()=false and IsSynced()=false, which correctly disables
      38             :  *  network-dependent validation paths.
      39             :  *
      40             :  *  Asserts on any call — if sync state is being advanced, a real notifier
      41             :  *  (NodeSyncNotifierImpl) must be used instead. */
      42             : class NullNodeSyncNotifier final : public NodeSyncNotifier
      43             : {
      44             : public:
      45             :     void SyncReset() override;
      46             :     void SyncFinished() override;
      47             : };
      48             : 
      49             : //
      50             : // CMasternodeSync : Sync masternode assets in stages
      51             : //
      52             : class CMasternodeSync
      53             : {
      54             : private:
      55             :     // Keep track of current asset
      56             :     std::atomic<int> nCurrentAsset{MASTERNODE_SYNC_BLOCKCHAIN};
      57             :     // Count peers we've requested the asset from
      58             :     std::atomic<int> nTriedPeerCount{0};
      59             : 
      60             :     // Time when current masternode asset sync started
      61             :     std::atomic<int64_t> nTimeAssetSyncStarted{0};
      62             :     // ... last bumped
      63             :     std::atomic<int64_t> nTimeLastBumped{0};
      64             : 
      65             :     /// Set to true if best header is reached in CMasternodeSync::UpdatedBlockTip
      66             :     std::atomic<bool> fReachedBestHeader{false};
      67             :     /// Last time UpdateBlockTip has been called
      68             :     std::atomic<int64_t> nTimeLastUpdateBlockTip{0};
      69             : 
      70             :     std::unique_ptr<NodeSyncNotifier> m_sync_notifier;
      71             : 
      72             : public:
      73             :     CMasternodeSync() = delete;
      74             :     CMasternodeSync(const CMasternodeSync&) = delete;
      75             :     CMasternodeSync& operator=(const CMasternodeSync&) = delete;
      76             :     explicit CMasternodeSync(std::unique_ptr<NodeSyncNotifier>&& sync_notifier);
      77             :     ~CMasternodeSync();
      78             : 
      79           0 :     bool IsBlockchainSynced() const { return nCurrentAsset > MASTERNODE_SYNC_BLOCKCHAIN; }
      80       49009 :     bool IsSynced() const { return nCurrentAsset == MASTERNODE_SYNC_FINISHED; }
      81             : 
      82           0 :     int GetAssetID() const { return nCurrentAsset; }
      83           0 :     int GetAttempt() const { return nTriedPeerCount; }
      84           0 :     void BumpAttempt() { ++nTriedPeerCount; }
      85             :     void BumpAssetLastTime(const std::string& strFuncName);
      86           0 :     int64_t GetLastBump() const { return nTimeLastBumped; }
      87           0 :     int64_t GetAssetStartTime() const { return nTimeAssetSyncStarted; }
      88             :     std::string GetAssetName() const;
      89             :     std::string GetSyncStatus() const;
      90           0 :     bool IsReachedBestHeader() const { return fReachedBestHeader; }
      91             : 
      92             :     void Reset(bool fForce = false, bool fNotifyReset = true);
      93             :     void SwitchToNextAsset();
      94             : 
      95             :     void AcceptedBlockHeader(const CBlockIndex *pindexNew);
      96             :     void NotifyHeaderTip(const CBlockIndex *pindexNew, bool fInitialDownload);
      97             :     void UpdatedBlockTip(const CBlockIndex *pindexTip, const CBlockIndex *pindexNew, bool fInitialDownload);
      98             : };
      99             : 
     100             : #endif // BITCOIN_MASTERNODE_SYNC_H

Generated by: LCOV version 1.16