masternode-meta.*: add thread annotations, atomic usage, remove unneeded locks (#4466)

* masternode-meta.*: add thread annotations, atomic usage, remove unneeded locks

* masternode-meta.cpp lock cs for metaInfos

* Implement (un)serialization for atomic

* partial revert

Co-authored-by: UdjinM6 <UdjinM6@users.noreply.github.com>
This commit is contained in:
PastaPastaPasta 2021-09-30 17:00:52 -04:00 committed by GitHub
parent c4a9b3d6d0
commit a37a63ffd6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 49 additions and 29 deletions

View File

@ -62,7 +62,6 @@ CMasternodeMetaInfoPtr CMasternodeMetaMan::GetMetaInfo(const uint256& proTxHash,
// masternodes before we ever see a masternode that we know already mixed someone's funds earlier. // masternodes before we ever see a masternode that we know already mixed someone's funds earlier.
int64_t CMasternodeMetaMan::GetDsqThreshold(const uint256& proTxHash, int nMnCount) int64_t CMasternodeMetaMan::GetDsqThreshold(const uint256& proTxHash, int nMnCount)
{ {
LOCK(cs);
auto metaInfo = GetMetaInfo(proTxHash); auto metaInfo = GetMetaInfo(proTxHash);
if (metaInfo == nullptr) { if (metaInfo == nullptr) {
// return a threshold which is slightly above nDsqCount i.e. a no-go // return a threshold which is slightly above nDsqCount i.e. a no-go
@ -73,26 +72,20 @@ int64_t CMasternodeMetaMan::GetDsqThreshold(const uint256& proTxHash, int nMnCou
void CMasternodeMetaMan::AllowMixing(const uint256& proTxHash) void CMasternodeMetaMan::AllowMixing(const uint256& proTxHash)
{ {
LOCK(cs);
auto mm = GetMetaInfo(proTxHash); auto mm = GetMetaInfo(proTxHash);
nDsqCount++; nDsqCount++;
LOCK(mm->cs); mm->nLastDsq = nDsqCount.load();
mm->nLastDsq = nDsqCount;
mm->nMixingTxCount = 0; mm->nMixingTxCount = 0;
} }
void CMasternodeMetaMan::DisallowMixing(const uint256& proTxHash) void CMasternodeMetaMan::DisallowMixing(const uint256& proTxHash)
{ {
LOCK(cs);
auto mm = GetMetaInfo(proTxHash); auto mm = GetMetaInfo(proTxHash);
LOCK(mm->cs);
mm->nMixingTxCount++; mm->nMixingTxCount++;
} }
bool CMasternodeMetaMan::AddGovernanceVote(const uint256& proTxHash, const uint256& nGovernanceObjectHash) bool CMasternodeMetaMan::AddGovernanceVote(const uint256& proTxHash, const uint256& nGovernanceObjectHash)
{ {
LOCK(cs);
auto mm = GetMetaInfo(proTxHash); auto mm = GetMetaInfo(proTxHash);
mm->AddGovernanceVote(nGovernanceObjectHash); mm->AddGovernanceVote(nGovernanceObjectHash);
return true; return true;
@ -124,7 +117,7 @@ void CMasternodeMetaMan::Clear()
std::string CMasternodeMetaMan::ToString() const std::string CMasternodeMetaMan::ToString() const
{ {
std::ostringstream info; std::ostringstream info;
LOCK(cs);
info << "Masternodes: meta infos object count: " << (int)metaInfos.size() << info << "Masternodes: meta infos object count: " << (int)metaInfos.size() <<
", nDsqCount: " << (int)nDsqCount; ", nDsqCount: " << (int)nDsqCount;
return info.str(); return info.str();

View File

@ -9,6 +9,7 @@
#include <univalue.h> #include <univalue.h>
#include <atomic>
#include <uint256.h> #include <uint256.h>
#include <sync.h> #include <sync.h>
@ -25,28 +26,28 @@ class CMasternodeMetaInfo
private: private:
mutable CCriticalSection cs; mutable CCriticalSection cs;
uint256 proTxHash; uint256 proTxHash GUARDED_BY(cs);
//the dsq count from the last dsq broadcast of this node //the dsq count from the last dsq broadcast of this node
int64_t nLastDsq = 0; std::atomic<int64_t> nLastDsq{0};
int nMixingTxCount = 0; std::atomic<int> nMixingTxCount{0};
// KEEP TRACK OF GOVERNANCE ITEMS EACH MASTERNODE HAS VOTE UPON FOR RECALCULATION // KEEP TRACK OF GOVERNANCE ITEMS EACH MASTERNODE HAS VOTE UPON FOR RECALCULATION
std::map<uint256, int> mapGovernanceObjectsVotedOn; std::map<uint256, int> mapGovernanceObjectsVotedOn GUARDED_BY(cs);
int64_t lastOutboundAttempt = 0; std::atomic<int64_t> lastOutboundAttempt{0};
int64_t lastOutboundSuccess = 0; std::atomic<int64_t> lastOutboundSuccess{0};
public: public:
CMasternodeMetaInfo() = default; CMasternodeMetaInfo() = default;
explicit CMasternodeMetaInfo(const uint256& _proTxHash) : proTxHash(_proTxHash) {} explicit CMasternodeMetaInfo(const uint256& _proTxHash) : proTxHash(_proTxHash) {}
CMasternodeMetaInfo(const CMasternodeMetaInfo& ref) : CMasternodeMetaInfo(const CMasternodeMetaInfo& ref) :
proTxHash(ref.proTxHash), proTxHash(ref.proTxHash),
nLastDsq(ref.nLastDsq), nLastDsq(ref.nLastDsq.load()),
nMixingTxCount(ref.nMixingTxCount), nMixingTxCount(ref.nMixingTxCount.load()),
mapGovernanceObjectsVotedOn(ref.mapGovernanceObjectsVotedOn), mapGovernanceObjectsVotedOn(ref.mapGovernanceObjectsVotedOn),
lastOutboundAttempt(ref.lastOutboundAttempt), lastOutboundAttempt(ref.lastOutboundAttempt.load()),
lastOutboundSuccess(ref.lastOutboundSuccess) lastOutboundSuccess(ref.lastOutboundSuccess.load())
{ {
} }
@ -67,8 +68,8 @@ public:
public: public:
const uint256& GetProTxHash() const { LOCK(cs); return proTxHash; } const uint256& GetProTxHash() const { LOCK(cs); return proTxHash; }
int64_t GetLastDsq() const { LOCK(cs); return nLastDsq; } int64_t GetLastDsq() const { return nLastDsq; }
int GetMixingTxCount() const { LOCK(cs); return nMixingTxCount; } int GetMixingTxCount() const { return nMixingTxCount; }
bool IsValidForMixingTxes() const { return GetMixingTxCount() <= MASTERNODE_MAX_MIXING_TXES; } bool IsValidForMixingTxes() const { return GetMixingTxCount() <= MASTERNODE_MAX_MIXING_TXES; }
@ -77,10 +78,10 @@ public:
void RemoveGovernanceObject(const uint256& nGovernanceObjectHash); void RemoveGovernanceObject(const uint256& nGovernanceObjectHash);
void SetLastOutboundAttempt(int64_t t) { LOCK(cs); lastOutboundAttempt = t; } void SetLastOutboundAttempt(int64_t t) { lastOutboundAttempt = t; }
int64_t GetLastOutboundAttempt() const { LOCK(cs); return lastOutboundAttempt; } int64_t GetLastOutboundAttempt() const { return lastOutboundAttempt; }
void SetLastOutboundSuccess(int64_t t) { LOCK(cs); lastOutboundSuccess = t; } void SetLastOutboundSuccess(int64_t t) { lastOutboundSuccess = t; }
int64_t GetLastOutboundSuccess() const { LOCK(cs); return lastOutboundSuccess; } int64_t GetLastOutboundSuccess() const { return lastOutboundSuccess; }
}; };
typedef std::shared_ptr<CMasternodeMetaInfo> CMasternodeMetaInfoPtr; typedef std::shared_ptr<CMasternodeMetaInfo> CMasternodeMetaInfoPtr;
@ -91,11 +92,11 @@ private:
mutable CCriticalSection cs; mutable CCriticalSection cs;
std::map<uint256, CMasternodeMetaInfoPtr> metaInfos; std::map<uint256, CMasternodeMetaInfoPtr> metaInfos GUARDED_BY(cs);
std::vector<uint256> vecDirtyGovernanceObjectHashes; std::vector<uint256> vecDirtyGovernanceObjectHashes GUARDED_BY(cs);
// keep track of dsq count to prevent masternodes from gaming coinjoin queue // keep track of dsq count to prevent masternodes from gaming coinjoin queue
int64_t nDsqCount = 0; std::atomic<int64_t> nDsqCount{0};
public: public:
template<typename Stream> template<typename Stream>
@ -130,7 +131,7 @@ public:
public: public:
CMasternodeMetaInfoPtr GetMetaInfo(const uint256& proTxHash, bool fCreate = true); CMasternodeMetaInfoPtr GetMetaInfo(const uint256& proTxHash, bool fCreate = true);
int64_t GetDsqCount() const { LOCK(cs); return nDsqCount; } int64_t GetDsqCount() const { return nDsqCount; }
int64_t GetDsqThreshold(const uint256& proTxHash, int nMnCount); int64_t GetDsqThreshold(const uint256& proTxHash, int nMnCount);
void AllowMixing(const uint256& proTxHash); void AllowMixing(const uint256& proTxHash);

View File

@ -10,6 +10,7 @@
#include <algorithm> #include <algorithm>
#include <assert.h> #include <assert.h>
#include <atomic>
#include <ios> #include <ios>
#include <limits> #include <limits>
#include <list> #include <list>
@ -879,6 +880,12 @@ template<typename Stream, typename T> void Unserialize(Stream& os, std::shared_p
template<typename Stream, typename T> void Serialize(Stream& os, const std::unique_ptr<const T>& p); template<typename Stream, typename T> void Serialize(Stream& os, const std::unique_ptr<const T>& p);
template<typename Stream, typename T> void Unserialize(Stream& os, std::unique_ptr<const T>& p); template<typename Stream, typename T> void Unserialize(Stream& os, std::unique_ptr<const T>& p);
/**
* atomic
*/
template<typename Stream, typename T> void Serialize(Stream& os, const std::atomic<T>& a);
template<typename Stream, typename T> void Unserialize(Stream& is, std::atomic<T>& a);
/** /**
@ -1311,6 +1318,25 @@ void Unserialize(Stream& is, std::shared_ptr<T>& p)
/**
* atomic
*/
template<typename Stream, typename T>
void Serialize(Stream& os, const std::atomic<T>& a)
{
Serialize(os, a.load());
}
template<typename Stream, typename T>
void Unserialize(Stream& is, std::atomic<T>& a)
{
T val;
Unserialize(is, val);
a.store(val);
}
/** /**
* Support for SERIALIZE_METHODS and READWRITE macro. * Support for SERIALIZE_METHODS and READWRITE macro.
*/ */