Line data Source code
1 : // Copyright (c) 2014-2021 The Dash Core developers 2 : // Copyright (c) 2021-2022 The PIVX Core developers 3 : // Distributed under the X11 software license, see the accompanying 4 : // file COPYING or https://www.opensource.org/licenses/mit-license.php. 5 : 6 : #ifndef PIVX_TIERTWO_MASTERNODE_META_MANAGER_H 7 : #define PIVX_TIERTWO_MASTERNODE_META_MANAGER_H 8 : 9 : #include "serialize.h" 10 : #include "sync.h" 11 : #include "uint256.h" 12 : 13 : #include <memory> 14 : 15 : static const std::string MN_META_CACHE_FILENAME = "mnmetacache.dat"; 16 : static const std::string MN_META_CACHE_FILE_ID = "magicMasternodeMetaCache"; 17 : 18 : // Holds extra (non-deterministic) information about masternodes 19 : // This is mostly local information, e.g. last connection attempt 20 203 : class CMasternodeMetaInfo 21 : { 22 : friend class CMasternodeMetaMan; 23 : private: 24 : mutable Mutex cs; 25 : uint256 proTxHash; 26 : // Connection data 27 : int64_t lastOutboundAttempt{0}; 28 : int64_t lastOutboundSuccess{0}; 29 : 30 : public: 31 922 : CMasternodeMetaInfo() = default; 32 105 : explicit CMasternodeMetaInfo(const uint256& _proTxHash) : proTxHash(_proTxHash) {} 33 203 : CMasternodeMetaInfo(const CMasternodeMetaInfo& ref) : 34 : proTxHash(ref.proTxHash), 35 203 : lastOutboundAttempt(ref.lastOutboundAttempt), 36 203 : lastOutboundSuccess(ref.lastOutboundSuccess) {} 37 : 38 0 : SERIALIZE_METHODS(CMasternodeMetaInfo, obj) { 39 105 : READWRITE(obj.proTxHash, obj.lastOutboundAttempt, obj.lastOutboundSuccess); 40 : } 41 : 42 0 : const uint256& GetProTxHash() const { return proTxHash; } 43 132 : void SetLastOutboundAttempt(int64_t t) { LOCK(cs); lastOutboundAttempt = t; } 44 2113 : int64_t GetLastOutboundAttempt() const { LOCK(cs); return lastOutboundAttempt; } 45 215 : void SetLastOutboundSuccess(int64_t t) { LOCK(cs); lastOutboundSuccess = t; } 46 288 : int64_t GetLastOutboundSuccess() const { LOCK(cs); return lastOutboundSuccess; } 47 : }; 48 : 49 : typedef std::shared_ptr<CMasternodeMetaInfo> CMasternodeMetaInfoPtr; 50 : 51 14 : class CMasternodeMetaMan 52 : { 53 : private: 54 : static const std::string SERIALIZATION_VERSION_STRING; 55 : mutable RecursiveMutex cs_metaman; 56 : std::map<uint256, CMasternodeMetaInfoPtr> metaInfos; 57 : 58 : public: 59 : // Return the stored metadata info from an specific MN 60 : CMasternodeMetaInfoPtr GetMetaInfo(const uint256& proTxHash, bool fCreate = true); 61 : void Clear(); 62 : std::string ToString(); 63 : 64 : template <typename Stream> 65 385 : inline void Serialize(Stream& s) const { 66 385 : LOCK(cs_metaman); 67 385 : s << SERIALIZATION_VERSION_STRING; 68 385 : std::vector<CMasternodeMetaInfo> tmpMetaInfo; 69 490 : for (auto& p : metaInfos) { 70 105 : tmpMetaInfo.emplace_back(*p.second); 71 : } 72 385 : s << tmpMetaInfo; 73 385 : } 74 : 75 : template <typename Stream> 76 76 : inline void Unserialize(Stream& s) { 77 76 : Clear(); 78 152 : LOCK(cs_metaman); 79 152 : std::string strVersion; 80 76 : s >> strVersion; 81 76 : if (strVersion != SERIALIZATION_VERSION_STRING) { 82 0 : return; 83 : } 84 : 85 152 : std::vector<CMasternodeMetaInfo> tmpMetaInfo; 86 76 : s >> tmpMetaInfo; 87 76 : for (auto& mm : tmpMetaInfo) { 88 0 : metaInfos.emplace(mm.GetProTxHash(), std::make_shared<CMasternodeMetaInfo>(std::move(mm))); 89 : } 90 : } 91 : }; 92 : 93 : extern CMasternodeMetaMan g_mmetaman; 94 : 95 : #endif // PIVX_TIERTWO_MASTERNODE_META_MANAGER_H