forked from dashpay/dash
-
Notifications
You must be signed in to change notification settings - Fork 714
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce masternode net metadata manager
- Loading branch information
Showing
7 changed files
with
160 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// Copyright (c) 2014-2021 The Dash Core developers | ||
// Copyright (c) 2021 The PIVX Core developers | ||
// Distributed under the X11 software license, see the accompanying | ||
// file COPYING or https://www.opensource.org/licenses/mit-license.php. | ||
|
||
#include "tiertwo/masternode_meta_manager.h" | ||
#include <sstream> | ||
|
||
CMasternodeMetaMan g_mmetaman; | ||
|
||
const std::string CMasternodeMetaMan::SERIALIZATION_VERSION_STRING = "CMasternodeMetaMan-Version-2"; | ||
|
||
CMasternodeMetaInfoPtr CMasternodeMetaMan::GetMetaInfo(const uint256& proTxHash, bool fCreate) | ||
{ | ||
LOCK(cs_metaman); | ||
auto it = metaInfos.find(proTxHash); | ||
if (it != metaInfos.end()) { | ||
return it->second; | ||
} | ||
if (!fCreate) { | ||
return nullptr; | ||
} | ||
it = metaInfos.emplace(proTxHash, std::make_shared<CMasternodeMetaInfo>(proTxHash)).first; | ||
return it->second; | ||
} | ||
|
||
|
||
void CMasternodeMetaMan::Clear() | ||
{ | ||
LOCK(cs_metaman); | ||
metaInfos.clear(); | ||
} | ||
|
||
std::string CMasternodeMetaMan::ToString() | ||
{ | ||
LOCK(cs_metaman); | ||
std::ostringstream info; | ||
|
||
info << "Masternodes: meta infos object count: " << (int)metaInfos.size(); | ||
return info.str(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
// Copyright (c) 2014-2021 The Dash Core developers | ||
// Copyright (c) 2021 The PIVX Core developers | ||
// Distributed under the X11 software license, see the accompanying | ||
// file COPYING or https://www.opensource.org/licenses/mit-license.php. | ||
|
||
#ifndef PIVX_MASTERNODE_META_MANAGER_H | ||
#define PIVX_MASTERNODE_META_MANAGER_H | ||
|
||
#include "serialize.h" | ||
#include "sync.h" | ||
#include "uint256.h" | ||
|
||
#include <memory> | ||
|
||
// Holds extra (non-deterministic) information about masternodes | ||
// This is mostly local information, e.g. last connection attempt | ||
class CMasternodeMetaInfo | ||
{ | ||
friend class CMasternodeMetaMan; | ||
private: | ||
mutable Mutex cs; | ||
uint256 proTxHash; | ||
// Connection data | ||
int64_t lastOutboundAttempt{0}; | ||
int64_t lastOutboundSuccess{0}; | ||
|
||
public: | ||
CMasternodeMetaInfo() = default; | ||
explicit CMasternodeMetaInfo(const uint256& _proTxHash) : proTxHash(_proTxHash) {} | ||
CMasternodeMetaInfo(const CMasternodeMetaInfo& ref) : | ||
proTxHash(ref.proTxHash), | ||
lastOutboundAttempt(ref.lastOutboundAttempt), | ||
lastOutboundSuccess(ref.lastOutboundSuccess) {} | ||
|
||
SERIALIZE_METHODS(CMasternodeMetaInfo, obj) { | ||
READWRITE(obj.proTxHash, obj.lastOutboundAttempt, obj.lastOutboundSuccess); | ||
} | ||
|
||
const uint256& GetProTxHash() const { return proTxHash; } | ||
void SetLastOutboundAttempt(int64_t t) { LOCK(cs); lastOutboundAttempt = t; } | ||
int64_t GetLastOutboundAttempt() { LOCK(cs); return lastOutboundAttempt; } | ||
void SetLastOutboundSuccess(int64_t t) { LOCK(cs); lastOutboundSuccess = t; } | ||
int64_t GetLastOutboundSuccess() { LOCK(cs); return lastOutboundSuccess; } | ||
}; | ||
|
||
typedef std::shared_ptr<CMasternodeMetaInfo> CMasternodeMetaInfoPtr; | ||
|
||
class CMasternodeMetaMan | ||
{ | ||
private: | ||
static const std::string SERIALIZATION_VERSION_STRING; | ||
mutable RecursiveMutex cs_metaman; | ||
std::map<uint256, CMasternodeMetaInfoPtr> metaInfos; | ||
|
||
public: | ||
// Return the stored metadata info from an specific MN | ||
CMasternodeMetaInfoPtr GetMetaInfo(const uint256& proTxHash, bool fCreate = true); | ||
void Clear(); | ||
std::string ToString(); | ||
|
||
template <typename Stream> | ||
inline void Serialize(Stream& s) const { | ||
LOCK(cs_metaman); | ||
s << SERIALIZATION_VERSION_STRING; | ||
std::vector<CMasternodeMetaInfo> tmpMetaInfo; | ||
for (auto& p : metaInfos) { | ||
tmpMetaInfo.emplace_back(*p.second); | ||
} | ||
s << tmpMetaInfo; | ||
} | ||
|
||
template <typename Stream> | ||
inline void Unserialize(Stream& s) { | ||
Clear(); | ||
LOCK(cs_metaman); | ||
std::string strVersion; | ||
s >> strVersion; | ||
if (strVersion != SERIALIZATION_VERSION_STRING) { | ||
return; | ||
} | ||
|
||
std::vector<CMasternodeMetaInfo> tmpMetaInfo; | ||
s >> tmpMetaInfo; | ||
for (auto& mm : tmpMetaInfo) { | ||
metaInfos.emplace(mm.GetProTxHash(), std::make_shared<CMasternodeMetaInfo>(std::move(mm))); | ||
} | ||
} | ||
}; | ||
|
||
extern CMasternodeMetaMan g_mmetaman; | ||
|
||
#endif //PIVX_MASTERNODE_META_MANAGER_H |