Skip to content

Commit

Permalink
Introduce masternode net metadata manager
Browse files Browse the repository at this point in the history
  • Loading branch information
furszy committed Jan 7, 2022
1 parent c724137 commit 711b874
Show file tree
Hide file tree
Showing 7 changed files with 160 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,8 @@ set(COMMON_SOURCES
./src/spork.cpp
./src/sporkdb.cpp
./src/tiertwo_networksync.cpp
./src/tiertwo/masternode_meta_manager.cpp
./src/tiertwo/net_masternodes.cpp
./src/tiertwo/tiertwo_sync_state.cpp
./src/warnings.cpp
)
Expand Down
2 changes: 2 additions & 0 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ BITCOIN_CORE_H = \
llmq/quorums_commitment.h \
llmq/quorums_init.h \
llmq/quorums_utils.h \
tiertwo/masternode_meta_manager.h \
addressbook.h \
wallet/db.h \
flatfile.h \
Expand Down Expand Up @@ -357,6 +358,7 @@ libbitcoin_server_a_SOURCES = \
llmq/quorums_commitment.cpp \
llmq/quorums_init.cpp \
llmq/quorums_utils.cpp \
tiertwo/masternode_meta_manager.cpp \
httprpc.cpp \
httpserver.cpp \
init.cpp \
Expand Down
2 changes: 1 addition & 1 deletion src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1727,7 +1727,7 @@ bool AppInitMain()

// ********************************************************* Step 10: setup layer 2 data

LoadTierTwo(chain_active_height);
LoadTierTwo(chain_active_height, fReindexChainState);
if (!InitActiveMN()) return false;
RegisterTierTwoValidationInterface();

Expand Down
22 changes: 21 additions & 1 deletion src/tiertwo/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
#include "tiertwo/init.h"

#include "budget/budgetdb.h"
#include "flatdb.h"
#include "guiinterface.h"
#include "guiinterfaceutil.h"
#include "masternodeman.h"
#include "masternode-payments.h"
#include "masternodeconfig.h"
#include "tiertwo/masternode_meta_manager.h"
#include "validation.h"

#include <boost/thread.hpp>
Expand All @@ -27,7 +29,7 @@ static void LoadBlockHashesCache(CMasternodeMan& man)
}
}

bool LoadTierTwo(int chain_active_height)
bool LoadTierTwo(int chain_active_height, bool fReindexChainState)
{
// ################################# //
// ## Legacy Masternodes Manager ### //
Expand Down Expand Up @@ -77,6 +79,23 @@ bool LoadTierTwo(int chain_active_height)
LogPrintf("Error reading mnpayments.dat - cached data discarded\n");
}

// init mn metadata manager
bool fLoadCacheFiles = !(fReindex || fReindexChainState);
fs::path pathDB = GetDataDir();
std::string strDBName = "mnmetacache.dat";
uiInterface.InitMessage(_("Loading masternode cache..."));
CFlatDB<CMasternodeMetaMan> metadb(strDBName, "magicMasternodeMetaCache");
if (fLoadCacheFiles) {
if (!metadb.Load(g_mmetaman)) {
return UIError(_("Failed to load masternode metadata cache from") + "\n" + (pathDB / strDBName).string());
}
} else {
CMasternodeMetaMan mmetamanTmp;
if (!metadb.Dump(mmetamanTmp)) {
return UIError(_("Failed to clear masternode metadata cache at") + "\n" + (pathDB / strDBName).string());
}
}

return true;
}

Expand All @@ -92,6 +111,7 @@ void DumpTierTwo()
DumpMasternodes();
DumpBudgets(g_budgetman);
DumpMasternodePayments();
CFlatDB<CMasternodeMetaMan>("mnmetacache.dat", "magicMasternodeMetaCache").Dump(g_mmetaman);
}

void SetBudgetFinMode(const std::string& mode)
Expand Down
2 changes: 1 addition & 1 deletion src/tiertwo/init.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace boost {
}

/** Loads from disk all the tier two related objects */
bool LoadTierTwo(int chain_active_height);
bool LoadTierTwo(int chain_active_height, bool fReindexChainState);

/** Register all tier two objects */
void RegisterTierTwoValidationInterface();
Expand Down
41 changes: 41 additions & 0 deletions src/tiertwo/masternode_meta_manager.cpp
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();
}
92 changes: 92 additions & 0 deletions src/tiertwo/masternode_meta_manager.h
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

0 comments on commit 711b874

Please sign in to comment.