PIVX Core  5.6.99
P2P Digital Currency
masternode_meta_manager.h
Go to the documentation of this file.
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
21 {
22  friend class CMasternodeMetaMan;
23 private:
24  mutable Mutex cs;
26  // Connection data
27  int64_t lastOutboundAttempt{0};
28  int64_t lastOutboundSuccess{0};
29 
30 public:
31  CMasternodeMetaInfo() = default;
32  explicit CMasternodeMetaInfo(const uint256& _proTxHash) : proTxHash(_proTxHash) {}
34  proTxHash(ref.proTxHash),
37 
39  READWRITE(obj.proTxHash, obj.lastOutboundAttempt, obj.lastOutboundSuccess);
40  }
41 
42  const uint256& GetProTxHash() const { return proTxHash; }
44  int64_t GetLastOutboundAttempt() const { LOCK(cs); return lastOutboundAttempt; }
46  int64_t GetLastOutboundSuccess() const { LOCK(cs); return lastOutboundSuccess; }
47 };
48 
49 typedef std::shared_ptr<CMasternodeMetaInfo> CMasternodeMetaInfoPtr;
50 
52 {
53 private:
54  static const std::string SERIALIZATION_VERSION_STRING;
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  inline void Serialize(Stream& s) const {
68  std::vector<CMasternodeMetaInfo> tmpMetaInfo;
69  for (auto& p : metaInfos) {
70  tmpMetaInfo.emplace_back(*p.second);
71  }
72  s << tmpMetaInfo;
73  }
74 
75  template <typename Stream>
76  inline void Unserialize(Stream& s) {
77  Clear();
79  std::string strVersion;
80  s >> strVersion;
81  if (strVersion != SERIALIZATION_VERSION_STRING) {
82  return;
83  }
84 
85  std::vector<CMasternodeMetaInfo> tmpMetaInfo;
86  s >> tmpMetaInfo;
87  for (auto& mm : tmpMetaInfo) {
88  metaInfos.emplace(mm.GetProTxHash(), std::make_shared<CMasternodeMetaInfo>(std::move(mm)));
89  }
90  }
91 };
92 
94 
95 #endif // PIVX_TIERTWO_MASTERNODE_META_MANAGER_H
int64_t GetLastOutboundSuccess() const
SERIALIZE_METHODS(CMasternodeMetaInfo, obj)
const uint256 & GetProTxHash() const
void SetLastOutboundAttempt(int64_t t)
CMasternodeMetaInfo(const CMasternodeMetaInfo &ref)
CMasternodeMetaInfo()=default
void SetLastOutboundSuccess(int64_t t)
int64_t GetLastOutboundAttempt() const
CMasternodeMetaInfo(const uint256 &_proTxHash)
static const std::string SERIALIZATION_VERSION_STRING
std::map< uint256, CMasternodeMetaInfoPtr > metaInfos
void Serialize(Stream &s) const
CMasternodeMetaInfoPtr GetMetaInfo(const uint256 &proTxHash, bool fCreate=true)
256-bit opaque blob.
Definition: uint256.h:138
@ LOCK
Definition: lockunlock.h:16
std::shared_ptr< CMasternodeMetaInfo > CMasternodeMetaInfoPtr
CMasternodeMetaMan g_mmetaman
@ proTxHash
Definition: rpcevo.cpp:50
#define READWRITE(...)
Definition: serialize.h:183