Skip to content

Commit

Permalink
refactor: move activeMasternodeInfo{Cs} into CActiveMasternodeManager
Browse files Browse the repository at this point in the history
  • Loading branch information
kwvg committed Mar 24, 2024
1 parent b8c1f01 commit e5295de
Show file tree
Hide file tree
Showing 15 changed files with 97 additions and 101 deletions.
4 changes: 2 additions & 2 deletions src/coinjoin/coinjoin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ bool CCoinJoinQueue::Sign()
if (!fMasternodeMode) return false;

uint256 hash = GetSignatureHash();
CBLSSignature sig = WITH_LOCK(activeMasternodeInfoCs, return activeMasternodeInfo.blsKeyOperator->Sign(hash, false));
CBLSSignature sig = WITH_LOCK(::activeMasternodeManager->cs, return ::activeMasternodeManager->m_info.blsKeyOperator->Sign(hash, false));
if (!sig.IsValid()) {
return false;
}
Expand Down Expand Up @@ -104,7 +104,7 @@ bool CCoinJoinBroadcastTx::Sign()
if (!fMasternodeMode) return false;

uint256 hash = GetSignatureHash();
CBLSSignature sig = WITH_LOCK(activeMasternodeInfoCs, return activeMasternodeInfo.blsKeyOperator->Sign(hash, false));
CBLSSignature sig = WITH_LOCK(::activeMasternodeManager->cs, return ::activeMasternodeManager->m_info.blsKeyOperator->Sign(hash, false));
if (!sig.IsValid()) {
return false;
}
Expand Down
16 changes: 8 additions & 8 deletions src/coinjoin/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ void CCoinJoinServer::ProcessDSACCEPT(CNode& peer, CDataStream& vRecv)
LogPrint(BCLog::COINJOIN, "DSACCEPT -- nDenom %d (%s) txCollateral %s", dsa.nDenom, CoinJoin::DenominationToString(dsa.nDenom), dsa.txCollateral.ToString()); /* Continued */

auto mnList = m_dmnman.GetListAtChainTip();
auto dmn = WITH_LOCK(activeMasternodeInfoCs, return mnList.GetValidMNByCollateral(activeMasternodeInfo.outpoint));
auto dmn = WITH_LOCK(::activeMasternodeManager->cs, return mnList.GetValidMNByCollateral(::activeMasternodeManager->m_info.outpoint));
if (!dmn) {
PushStatus(peer, STATUS_REJECTED, ERR_MN_LIST);
return;
Expand All @@ -67,7 +67,7 @@ void CCoinJoinServer::ProcessDSACCEPT(CNode& peer, CDataStream& vRecv)
TRY_LOCK(cs_vecqueue, lockRecv);
if (!lockRecv) return;

auto mnOutpoint = WITH_LOCK(activeMasternodeInfoCs, return activeMasternodeInfo.outpoint);
auto mnOutpoint = WITH_LOCK(::activeMasternodeManager->cs, return ::activeMasternodeManager->m_info.outpoint);

if (ranges::any_of(vecCoinJoinQueue,
[&mnOutpoint](const auto& q){return q.masternodeOutpoint == mnOutpoint;})) {
Expand Down Expand Up @@ -331,8 +331,8 @@ void CCoinJoinServer::CommitFinalTransaction()
// create and sign masternode dstx transaction
if (!m_dstxman.GetDSTX(hashTx)) {
CCoinJoinBroadcastTx dstxNew(finalTransaction,
WITH_LOCK(activeMasternodeInfoCs, return activeMasternodeInfo.outpoint),
WITH_LOCK(activeMasternodeInfoCs, return activeMasternodeInfo.proTxHash),
WITH_LOCK(::activeMasternodeManager->cs, return ::activeMasternodeManager->m_info.outpoint),
WITH_LOCK(::activeMasternodeManager->cs, return ::activeMasternodeManager->m_info.proTxHash),
GetAdjustedTime());
dstxNew.Sign();
m_dstxman.AddDSTX(dstxNew);
Expand Down Expand Up @@ -499,8 +499,8 @@ void CCoinJoinServer::CheckForCompleteQueue()
SetState(POOL_STATE_ACCEPTING_ENTRIES);

CCoinJoinQueue dsq(nSessionDenom,
WITH_LOCK(activeMasternodeInfoCs, return activeMasternodeInfo.outpoint),
WITH_LOCK(activeMasternodeInfoCs, return activeMasternodeInfo.proTxHash),
WITH_LOCK(::activeMasternodeManager->cs, return ::activeMasternodeManager->m_info.outpoint),
WITH_LOCK(::activeMasternodeManager->cs, return ::activeMasternodeManager->m_info.proTxHash),
GetAdjustedTime(), true);
LogPrint(BCLog::COINJOIN, "CCoinJoinServer::CheckForCompleteQueue -- queue is ready, signing and relaying (%s) " /* Continued */
"with %d participants\n", dsq.ToString(), vecSessionCollaterals.size());
Expand Down Expand Up @@ -713,8 +713,8 @@ bool CCoinJoinServer::CreateNewSession(const CCoinJoinAccept& dsa, PoolMessage&
if (!fUnitTest) {
//broadcast that I'm accepting entries, only if it's the first entry through
CCoinJoinQueue dsq(nSessionDenom,
WITH_LOCK(activeMasternodeInfoCs, return activeMasternodeInfo.outpoint),
WITH_LOCK(activeMasternodeInfoCs, return activeMasternodeInfo.proTxHash),
WITH_LOCK(::activeMasternodeManager->cs, return ::activeMasternodeManager->m_info.outpoint),
WITH_LOCK(::activeMasternodeManager->cs, return ::activeMasternodeManager->m_info.proTxHash),
GetAdjustedTime(), false);
LogPrint(BCLog::COINJOIN, "CCoinJoinServer::CreateNewSession -- signing and relaying new queue: %s\n", dsq.ToString());
dsq.Sign();
Expand Down
12 changes: 6 additions & 6 deletions src/evo/mnauth.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ void CMNAuth::PushMNAUTH(CNode& peer, CConnman& connman, const CBlockIndex* tip)
{
if (!fMasternodeMode) return;

LOCK(activeMasternodeInfoCs);
if (activeMasternodeInfo.proTxHash.IsNull()) return;
LOCK(::activeMasternodeManager->cs);
if (::activeMasternodeManager->m_info.proTxHash.IsNull()) return;

uint256 signHash;
const auto receivedMNAuthChallenge = peer.GetReceivedMNAuthChallenge();
Expand All @@ -42,16 +42,16 @@ void CMNAuth::PushMNAUTH(CNode& peer, CConnman& connman, const CBlockIndex* tip)
nOurNodeVersion = gArgs.GetArg("-pushversion", PROTOCOL_VERSION);
}
const bool is_basic_scheme_active{DeploymentActiveAfter(tip, Params().GetConsensus(), Consensus::DEPLOYMENT_V19)};
const CBLSPublicKeyVersionWrapper pubKey(*activeMasternodeInfo.blsPubKeyOperator, !is_basic_scheme_active);
const CBLSPublicKeyVersionWrapper pubKey(*::activeMasternodeManager->m_info.blsPubKeyOperator, !is_basic_scheme_active);
if (peer.nVersion < MNAUTH_NODE_VER_VERSION || nOurNodeVersion < MNAUTH_NODE_VER_VERSION) {
signHash = ::SerializeHash(std::make_tuple(pubKey, receivedMNAuthChallenge, peer.IsInboundConn()));
} else {
signHash = ::SerializeHash(std::make_tuple(pubKey, receivedMNAuthChallenge, peer.IsInboundConn(), nOurNodeVersion));
}

CMNAuth mnauth;
mnauth.proRegTxHash = activeMasternodeInfo.proTxHash;
mnauth.sig = activeMasternodeInfo.blsKeyOperator->Sign(signHash);
mnauth.proRegTxHash = ::activeMasternodeManager->m_info.proTxHash;
mnauth.sig = ::activeMasternodeManager->m_info.blsKeyOperator->Sign(signHash);

LogPrint(BCLog::NET_NETCONN, "CMNAuth::%s -- Sending MNAUTH, peer=%d\n", __func__, peer.GetId());

Expand Down Expand Up @@ -128,7 +128,7 @@ PeerMsgRet CMNAuth::ProcessMessage(CNode& peer, CConnman& connman, const CDeterm
}

const uint256 myProTxHash = fMasternodeMode ?
WITH_LOCK(activeMasternodeInfoCs, return activeMasternodeInfo.proTxHash) :
WITH_LOCK(::activeMasternodeManager->cs, return ::activeMasternodeManager->m_info.proTxHash) :
uint256();

connman.ForEachNode([&](CNode* pnode2) {
Expand Down
16 changes: 8 additions & 8 deletions src/governance/governance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -692,14 +692,14 @@ std::optional<const CGovernanceObject> CGovernanceManager::CreateGovernanceTrigg
}

{
LOCK(activeMasternodeInfoCs);
if (mn_payees.front()->proTxHash != activeMasternodeInfo.proTxHash) {
LOCK(::activeMasternodeManager->cs);
if (mn_payees.front()->proTxHash != ::activeMasternodeManager->m_info.proTxHash) {
LogPrint(BCLog::GOBJECT, "CGovernanceManager::%s we are not the payee, skipping\n", __func__);
return std::nullopt;
}
gov_sb.SetMasternodeOutpoint(activeMasternodeInfo.outpoint);
gov_sb.Sign( *activeMasternodeInfo.blsKeyOperator);
} // activeMasternodeInfoCs
gov_sb.SetMasternodeOutpoint(::activeMasternodeManager->m_info.outpoint);
gov_sb.Sign( *::activeMasternodeManager->m_info.blsKeyOperator);
} // ::activeMasternodeManager->cs

if (std::string strError; !gov_sb.IsValidLocally(m_dmnman->GetListAtChainTip(), strError, true)) {
LogPrint(BCLog::GOBJECT, "CGovernanceManager::%s Created trigger is invalid:%s\n", __func__, strError);
Expand All @@ -720,7 +720,7 @@ void CGovernanceManager::VoteGovernanceTriggers(const std::optional<const CGover
{
// only active masternodes can vote on triggers
if (!fMasternodeMode) return;
if (WITH_LOCK(activeMasternodeInfoCs, return activeMasternodeInfo.proTxHash.IsNull())) return;
if (WITH_LOCK(::activeMasternodeManager->cs, return ::activeMasternodeManager->m_info.proTxHash.IsNull())) return;

LOCK2(cs_main, cs);

Expand Down Expand Up @@ -763,9 +763,9 @@ void CGovernanceManager::VoteGovernanceTriggers(const std::optional<const CGover

bool CGovernanceManager::VoteFundingTrigger(const uint256& nHash, const vote_outcome_enum_t outcome, CConnman& connman)
{
CGovernanceVote vote(WITH_LOCK(activeMasternodeInfoCs, return activeMasternodeInfo.outpoint), nHash, VOTE_SIGNAL_FUNDING, outcome);
CGovernanceVote vote(WITH_LOCK(::activeMasternodeManager->cs, return ::activeMasternodeManager->m_info.outpoint), nHash, VOTE_SIGNAL_FUNDING, outcome);
vote.SetTime(GetAdjustedTime());
vote.Sign(WITH_LOCK(activeMasternodeInfoCs, return *activeMasternodeInfo.blsKeyOperator));
vote.Sign(WITH_LOCK(::activeMasternodeManager->cs, return *::activeMasternodeManager->m_info.blsKeyOperator));

CGovernanceException exception;
if (!ProcessVoteAndRelay(vote, exception, connman)) {
Expand Down
28 changes: 14 additions & 14 deletions src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -369,14 +369,14 @@ void PrepareShutdown(NodeContext& node)
pdsNotificationInterface = nullptr;
}
if (fMasternodeMode) {
UnregisterValidationInterface(activeMasternodeManager.get());
UnregisterValidationInterface(::activeMasternodeManager.get());

LOCK(activeMasternodeInfoCs);
LOCK(::activeMasternodeManager->cs);
// make sure to clean up BLS keys before global destructors are called (they have allocated from the secure memory pool)
activeMasternodeInfo.blsKeyOperator.reset();
activeMasternodeInfo.blsPubKeyOperator.reset();
::activeMasternodeManager->m_info.blsKeyOperator.reset();
::activeMasternodeManager->m_info.blsPubKeyOperator.reset();

activeMasternodeManager.reset();
::activeMasternodeManager.reset();
}

node.chain_clients.clear();
Expand Down Expand Up @@ -1859,19 +1859,19 @@ bool AppInitMain(const CoreContext& context, NodeContext& node, interfaces::Bloc
fMasternodeMode = true;
{
// Create and register activeMasternodeManager, will init later in ThreadImport
activeMasternodeManager = std::make_unique<CActiveMasternodeManager>(*node.connman, ::deterministicMNManager);
::activeMasternodeManager = std::make_unique<CActiveMasternodeManager>(*node.connman, ::deterministicMNManager);

LOCK(activeMasternodeInfoCs);
assert(activeMasternodeInfo.blsKeyOperator == nullptr);
assert(activeMasternodeInfo.blsPubKeyOperator == nullptr);
activeMasternodeInfo.blsKeyOperator = std::make_unique<CBLSSecretKey>(keyOperator);
activeMasternodeInfo.blsPubKeyOperator = std::make_unique<CBLSPublicKey>(keyOperator.GetPublicKey());
LOCK(::activeMasternodeManager->cs);
assert(::activeMasternodeManager->m_info.blsKeyOperator == nullptr);
assert(::activeMasternodeManager->m_info.blsPubKeyOperator == nullptr);
::activeMasternodeManager->m_info.blsKeyOperator = std::make_unique<CBLSSecretKey>(keyOperator);
::activeMasternodeManager->m_info.blsPubKeyOperator = std::make_unique<CBLSPublicKey>(keyOperator.GetPublicKey());
// We don't know the actual scheme at this point, print both
LogPrintf("MASTERNODE:\n blsPubKeyOperator legacy: %s\n blsPubKeyOperator basic: %s\n",
activeMasternodeInfo.blsPubKeyOperator->ToString(true),
activeMasternodeInfo.blsPubKeyOperator->ToString(false));
::activeMasternodeManager->m_info.blsPubKeyOperator->ToString(true),
::activeMasternodeManager->m_info.blsPubKeyOperator->ToString(false));

RegisterValidationInterface(activeMasternodeManager.get());
RegisterValidationInterface(::activeMasternodeManager.get());
}
}

Expand Down
10 changes: 5 additions & 5 deletions src/llmq/dkgsession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ void CDKGSession::SendContributions(CDKGPendingMessages& pendingMessages)

logger.Batch("encrypted contributions. time=%d", t1.count());

qc.sig = WITH_LOCK(activeMasternodeInfoCs, return activeMasternodeInfo.blsKeyOperator->Sign(qc.GetSignHash()));
qc.sig = WITH_LOCK(::activeMasternodeManager->cs, return ::activeMasternodeManager->m_info.blsKeyOperator->Sign(qc.GetSignHash()));

logger.Flush();

Expand Down Expand Up @@ -316,7 +316,7 @@ void CDKGSession::ReceiveMessage(const CDKGContribution& qc, bool& retBan)

bool complain = false;
CBLSSecretKey skContribution;
if (!qc.contributions->Decrypt(*myIdx, WITH_LOCK(activeMasternodeInfoCs, return *activeMasternodeInfo.blsKeyOperator), skContribution, PROTOCOL_VERSION)) {
if (!qc.contributions->Decrypt(*myIdx, WITH_LOCK(::activeMasternodeManager->cs, return *::activeMasternodeManager->m_info.blsKeyOperator), skContribution, PROTOCOL_VERSION)) {
logger.Batch("contribution from %s could not be decrypted", member->dmn->proTxHash.ToString());
complain = true;
} else if (member->idx != myIdx && ShouldSimulateError(DKGError::type::COMPLAIN_LIE)) {
Expand Down Expand Up @@ -517,7 +517,7 @@ void CDKGSession::SendComplaint(CDKGPendingMessages& pendingMessages)

logger.Batch("sending complaint. badCount=%d, complaintCount=%d", badCount, complaintCount);

qc.sig = WITH_LOCK(activeMasternodeInfoCs, return activeMasternodeInfo.blsKeyOperator->Sign(qc.GetSignHash()));
qc.sig = WITH_LOCK(::activeMasternodeManager->cs, return ::activeMasternodeManager->m_info.blsKeyOperator->Sign(qc.GetSignHash()));

logger.Flush();

Expand Down Expand Up @@ -711,7 +711,7 @@ void CDKGSession::SendJustification(CDKGPendingMessages& pendingMessages, const
return;
}

qj.sig = WITH_LOCK(activeMasternodeInfoCs, return activeMasternodeInfo.blsKeyOperator->Sign(qj.GetSignHash()));
qj.sig = WITH_LOCK(::activeMasternodeManager->cs, return ::activeMasternodeManager->m_info.blsKeyOperator->Sign(qj.GetSignHash()));

logger.Flush();

Expand Down Expand Up @@ -1003,7 +1003,7 @@ void CDKGSession::SendCommitment(CDKGPendingMessages& pendingMessages)
(*commitmentHash.begin())++;
}

qc.sig = WITH_LOCK(activeMasternodeInfoCs, return activeMasternodeInfo.blsKeyOperator->Sign(commitmentHash));
qc.sig = WITH_LOCK(::activeMasternodeManager->cs, return ::activeMasternodeManager->m_info.blsKeyOperator->Sign(commitmentHash));
qc.quorumSig = skShare.Sign(commitmentHash);

if (lieType == 3) {
Expand Down
2 changes: 1 addition & 1 deletion src/llmq/dkgsessionhandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ bool CDKGSessionHandler::InitNewQuorum(const CBlockIndex* pQuorumBaseBlockIndex)
}

auto mns = utils::GetAllQuorumMembers(params.type, m_dmnman, pQuorumBaseBlockIndex);
if (!curSession->Init(pQuorumBaseBlockIndex, mns, WITH_LOCK(activeMasternodeInfoCs, return activeMasternodeInfo.proTxHash), quorumIndex)) {
if (!curSession->Init(pQuorumBaseBlockIndex, mns, WITH_LOCK(::activeMasternodeManager->cs, return ::activeMasternodeManager->m_info.proTxHash), quorumIndex)) {
LogPrintf("CDKGSessionManager::%s -- height[%d] quorum initialization failed for %s qi[%d] mns[%d]\n", __func__, pQuorumBaseBlockIndex->nHeight, curSession->params.name, quorumIndex, mns.size());
return false;
}
Expand Down
16 changes: 8 additions & 8 deletions src/llmq/quorums.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ bool CQuorum::SetVerificationVector(const std::vector<CBLSPublicKey>& quorumVecI

bool CQuorum::SetSecretKeyShare(const CBLSSecretKey& secretKeyShare)
{
if (!secretKeyShare.IsValid() || (secretKeyShare.GetPublicKey() != GetPubKeyShare(WITH_LOCK(activeMasternodeInfoCs, return GetMemberIndex(activeMasternodeInfo.proTxHash))))) {
if (!secretKeyShare.IsValid() || (secretKeyShare.GetPublicKey() != GetPubKeyShare(WITH_LOCK(::activeMasternodeManager->cs, return GetMemberIndex(::activeMasternodeManager->m_info.proTxHash))))) {
return false;
}
LOCK(cs);
Expand Down Expand Up @@ -251,7 +251,7 @@ void CQuorumManager::TriggerQuorumDataRecoveryThreads(const CBlockIndex* pIndex)

// First check if we are member of any quorum of this type
const uint256 proTxHash = fMasternodeMode ?
WITH_LOCK(activeMasternodeInfoCs, return activeMasternodeInfo.proTxHash) :
WITH_LOCK(::activeMasternodeManager->cs, return ::activeMasternodeManager->m_info.proTxHash) :
uint256();

bool fWeAreQuorumTypeMember = ranges::any_of(vecQuorums, [&proTxHash](const auto& pQuorum) {
Expand Down Expand Up @@ -345,7 +345,7 @@ void CQuorumManager::CheckQuorumConnections(const Consensus::LLMQParams& llmqPar
}

const uint256 myProTxHash = fMasternodeMode ?
WITH_LOCK(activeMasternodeInfoCs, return activeMasternodeInfo.proTxHash) :
WITH_LOCK(::activeMasternodeManager->cs, return ::activeMasternodeManager->m_info.proTxHash) :
uint256();

bool isISType = llmqParams.type == Params().GetConsensus().llmqTypeDIP0024InstantSend;
Expand Down Expand Up @@ -655,10 +655,10 @@ size_t CQuorumManager::GetQuorumRecoveryStartOffset(const CQuorumCPtr pQuorum, c
std::sort(vecProTxHashes.begin(), vecProTxHashes.end());
size_t nIndex{0};
{
LOCK(activeMasternodeInfoCs);
LOCK(::activeMasternodeManager->cs);
for (const auto i : irange::range(vecProTxHashes.size())) {
// cppcheck-suppress useStlAlgorithm
if (activeMasternodeInfo.proTxHash == vecProTxHashes[i]) {
if (::activeMasternodeManager->m_info.proTxHash == vecProTxHashes[i]) {
nIndex = i;
break;
}
Expand Down Expand Up @@ -834,7 +834,7 @@ PeerMsgRet CQuorumManager::ProcessMessage(CNode& pfrom, const std::string& msg_t

std::vector<CBLSSecretKey> vecSecretKeys;
vecSecretKeys.resize(vecEncrypted.size());
auto secret = WITH_LOCK(activeMasternodeInfoCs, return *activeMasternodeInfo.blsKeyOperator);
auto secret = WITH_LOCK(::activeMasternodeManager->cs, return *::activeMasternodeManager->m_info.blsKeyOperator);
for (const auto i : irange::range(vecEncrypted.size())) {
if (!vecEncrypted[i].Decrypt(memberIdx, secret, vecSecretKeys[i], PROTOCOL_VERSION)) {
return errorHandler("Failed to decrypt");
Expand Down Expand Up @@ -917,7 +917,7 @@ void CQuorumManager::StartQuorumDataRecoveryThread(const CQuorumCPtr pQuorum, co

vecMemberHashes.reserve(pQuorum->qc->validMembers.size());
for (auto& member : pQuorum->members) {
if (pQuorum->IsValidMember(member->proTxHash) && member->proTxHash != WITH_LOCK(activeMasternodeInfoCs, return activeMasternodeInfo.proTxHash)) {
if (pQuorum->IsValidMember(member->proTxHash) && member->proTxHash != WITH_LOCK(::activeMasternodeManager->cs, return ::activeMasternodeManager->m_info.proTxHash)) {
vecMemberHashes.push_back(member->proTxHash);
}
}
Expand Down Expand Up @@ -966,7 +966,7 @@ void CQuorumManager::StartQuorumDataRecoveryThread(const CQuorumCPtr pQuorum, co
printLog("Connect");
}

auto proTxHash = WITH_LOCK(activeMasternodeInfoCs, return activeMasternodeInfo.proTxHash);
auto proTxHash = WITH_LOCK(::activeMasternodeManager->cs, return ::activeMasternodeManager->m_info.proTxHash);
connman.ForEachNode([&](CNode* pNode) {
auto verifiedProRegTxHash = pNode->GetVerifiedProRegTxHash();
if (pCurrentMemberHash == nullptr || verifiedProRegTxHash != *pCurrentMemberHash) {
Expand Down
Loading

0 comments on commit e5295de

Please sign in to comment.