Skip to content

Commit

Permalink
locking improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
pedro-at-decenomy committed May 24, 2024
1 parent 5e53d46 commit 43ab2e8
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 32 deletions.
19 changes: 9 additions & 10 deletions src/masternode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,17 +189,11 @@ void CMasternode::Check(bool forceCheck)
{
if (ShutdownRequested()) return;

const Consensus::Params& consensus = Params().GetConsensus();

// todo: add LOCK(cs) but be careful with the AcceptableInputs() below that requires cs_main.

if (!forceCheck && (GetTime() - lastTimeChecked < MASTERNODE_CHECK_SECONDS)) return;
lastTimeChecked = GetTime();


//once spent, stop doing the checks
if (activeState == MASTERNODE_VIN_SPENT) return;

if (!forceCheck && (GetTime() - lastTimeChecked < MASTERNODE_CHECK_SECONDS)) return;
lastTimeChecked = GetTime();

if (!IsPingedWithin(MASTERNODE_REMOVAL_SECONDS)) {
activeState = MASTERNODE_REMOVE;
Expand All @@ -216,7 +210,10 @@ void CMasternode::Check(bool forceCheck)
return;
}

if (!unitTest && lastTimeChecked - lastTimeCollateralChecked > MINUTE_IN_SECONDS) {
if (!unitTest &&
forceCheck &&
lastTimeChecked - lastTimeCollateralChecked > MINUTE_IN_SECONDS
) {
lastTimeCollateralChecked = lastTimeChecked;
CValidationState state;
CMutableTransaction tx = CMutableTransaction();
Expand All @@ -235,6 +232,8 @@ void CMasternode::Check(bool forceCheck)
}
}

const auto& consensus = Params().GetConsensus();

// ----------- burn address scanning -----------
if (!consensus.mBurnAddresses.empty()) {

Expand Down Expand Up @@ -684,7 +683,7 @@ bool CMasternodeBroadcast::CheckAndUpdate(int& nDos)
//take the newest entry
LogPrint(BCLog::MASTERNODE, "mnb - Got updated entry for %s\n", vin.prevout.ToStringShort());
if (pmn->UpdateFromNewBroadcast((*this))) {
pmn->Check();
pmn->Check(true);
if (pmn->IsEnabled()) Relay();
}
masternodeSync.AddedMasternodeList(GetHash());
Expand Down
33 changes: 19 additions & 14 deletions src/masternodeman.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -265,18 +265,26 @@ void CMasternodeMan::AskForMN(CNode* pnode, const CTxIn& vin)
mWeAskedForMasternodeListEntry[vin.prevout] = askAgain;
}

void CMasternodeMan::Check()
void CMasternodeMan::Check(bool forceCheck)
{
LOCK2(cs_main, cs);
if(forceCheck) {
LOCK2(cs_main, cs);

for (auto mn : vMasternodes) {
mn->Check();
for (auto mn : vMasternodes) {
mn->Check(forceCheck);
}
} else {
LOCK(cs);

for (auto mn : vMasternodes) {
mn->Check();
}
}
}

void CMasternodeMan::CheckAndRemove(bool forceExpiredRemoval)
{
Check();
Check(true);

LOCK(cs);

Expand Down Expand Up @@ -442,7 +450,7 @@ int CMasternodeMan::CountEnabled()
{
int i = 0;

LOCK2(cs_main, cs);
LOCK(cs);

for (auto mn : vMasternodes) {
mn->Check();
Expand All @@ -455,7 +463,7 @@ int CMasternodeMan::CountEnabled()

void CMasternodeMan::CountNetworks(int& ipv4, int& ipv6, int& onion)
{
LOCK2(cs_main, cs);
LOCK(cs);

for (auto mn : vMasternodes) {
mn->Check();
Expand Down Expand Up @@ -561,7 +569,7 @@ CMasternode* CMasternodeMan::GetNextMasternodeInQueueForPayment(int nBlockHeight
vEligibleTxIns.clear();
int nMnCount = 0;
{
LOCK2(cs_main, cs);
LOCK(cs);

nMnCount = CountEnabled();
for (auto mn : vMasternodes) {
Expand Down Expand Up @@ -656,7 +664,7 @@ CMasternode* CMasternodeMan::GetCurrentMasterNode(int mod, int64_t nBlockHeight)
int64_t score = 0;
CMasternode* winner = NULL;

LOCK2(cs_main, cs);
LOCK(cs);

// scan for winner
for (auto mn : vMasternodes) {
Expand Down Expand Up @@ -692,7 +700,7 @@ int CMasternodeMan::GetMasternodeRank(const CTxIn& vin, int64_t nBlockHeight)
uint256 hash;
if (!GetBlockHash(hash, nBlockHeight)) return defaultValue;

LOCK2(cs_main, cs);
LOCK(cs);

// scan for winner
for (auto mn : vMasternodes) {
Expand Down Expand Up @@ -1002,10 +1010,7 @@ void ThreadCheckMasternodes()
MilliSleep(1000);
boost::this_thread::interruption_point();
// try to sync from all available nodes, one step at a time
{
LOCK(cs_main);
masternodeSync.Process();
}
masternodeSync.Process();

if (masternodeSync.IsBlockchainSynced()) {
c++;
Expand Down
2 changes: 1 addition & 1 deletion src/masternodeman.h
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ class CMasternodeMan
void AskForMN(CNode* pnode, const CTxIn& vin);

/// Check all Masternodes
void Check();
void Check(bool forceCheck = false);

/// Check all Masternodes and remove inactive
void CheckAndRemove(bool forceExpiredRemoval = false);
Expand Down
17 changes: 10 additions & 7 deletions src/miner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -209,8 +209,11 @@ CBlockTemplate* CreateNewBlock(const CScript& scriptPubKeyIn, CWallet* pwallet,
// Collect memory pool transactions into the block
CAmount nFees = 0;

LOCK(cs_main);

{
LOCK2(cs_main, mempool.cs);
LOCK(mempool.cs);

CCoinsViewCache view(pcoinsTip);

// Priority order to process transactions
Expand Down Expand Up @@ -408,13 +411,13 @@ CBlockTemplate* CreateNewBlock(const CScript& scriptPubKeyIn, CWallet* pwallet,
return nullptr;
}
}
}

CValidationState state;
if (!TestBlockValidity(state, *pblock, pindexPrev, false, false)) {
LogPrintf("CreateNewBlock() : TestBlockValidity failed\n");
mempool.clear();
return nullptr;
}
CValidationState state;
if (!TestBlockValidity(state, *pblock, pindexPrev, false, false)) {
LogPrintf("CreateNewBlock() : TestBlockValidity failed\n");
mempool.clear();
return nullptr;
}

return pblocktemplate.release();
Expand Down

0 comments on commit 43ab2e8

Please sign in to comment.