Skip to content

Commit

Permalink
Add thread safety keywords to difficulty functions
Browse files Browse the repository at this point in the history
  • Loading branch information
jamescowens committed Oct 27, 2024
1 parent 0460651 commit 8473938
Showing 1 changed file with 17 additions and 14 deletions.
31 changes: 17 additions & 14 deletions src/gridcoin/staking/difficulty.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ constexpr int64_t TARGET_TIMESPAN = 16 * 60; // 16 mins in seconds
const arith_uint256 PROOF_OF_STAKE_LIMIT = ~arith_uint256() >> 20;

// ppcoin: find last block index up to pindex
const CBlockIndex* GetLastBlockIndex(const CBlockIndex* pindex, bool fProofOfStake)
const CBlockIndex* GetLastBlockIndex(const CBlockIndex* pindex, bool fProofOfStake) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
{
while (pindex && pindex->pprev && (pindex->IsProofOfStake() != fProofOfStake))
pindex = pindex->pprev;
Expand All @@ -35,7 +35,7 @@ const CBlockIndex* GetLastBlockIndex(const CBlockIndex* pindex, bool fProofOfSta
// Functions
// -----------------------------------------------------------------------------

unsigned int GRC::GetNextTargetRequired(const CBlockIndex* pindexLast)
unsigned int GRC::GetNextTargetRequired(const CBlockIndex* pindexLast) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
{
if (pindexLast == nullptr) {
return PROOF_OF_STAKE_LIMIT.GetCompact(); // genesis block
Expand Down Expand Up @@ -89,7 +89,7 @@ unsigned int GRC::GetNextTargetRequired(const CBlockIndex* pindexLast)
return bnNew.GetCompact();
}

double GRC::GetDifficulty(const CBlockIndex* blockindex)
double GRC::GetDifficulty(const CBlockIndex* blockindex) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
{
// Floating point number that is a multiple of the minimum difficulty,
// minimum difficulty = 1.0.
Expand Down Expand Up @@ -124,18 +124,18 @@ double GRC::GetBlockDifficulty(unsigned int nBits)
return dDiff;
}

double GRC::GetCurrentDifficulty()
double GRC::GetCurrentDifficulty() EXCLUSIVE_LOCKS_REQUIRED(cs_main)
{
return GetDifficulty(GetLastBlockIndex(pindexBest, true));
}

double GRC::GetTargetDifficulty()
double GRC::GetTargetDifficulty() EXCLUSIVE_LOCKS_REQUIRED(cs_main)
{
return GetBlockDifficulty(GetNextTargetRequired(pindexBest));
}

// This requires a lock on cs_main when called.
double GRC::GetAverageDifficulty(unsigned int nPoSInterval)
double GRC::GetAverageDifficulty(unsigned int nPoSInterval) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
{
/*
* Diff is inversely related to Target (without the coinweight multiplier), but proportional to the
Expand Down Expand Up @@ -182,7 +182,7 @@ double GRC::GetAverageDifficulty(unsigned int nPoSInterval)
}

// This requires a lock on cs_main when called.
double GRC::GetSmoothedDifficulty(int64_t nStakeableBalance)
double GRC::GetSmoothedDifficulty(int64_t nStakeableBalance) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
{
// The smoothed difficulty is derived via a two step process. There is a coupling between the desired block
// span to compute difficulty and essentially the stakeable balance: If the balance is low, the ETTS is
Expand Down Expand Up @@ -255,7 +255,7 @@ uint64_t GRC::GetStakeWeight(const CWallet& wallet)
return weight;
}

double GRC::GetEstimatedNetworkWeight(unsigned int nPoSInterval)
double GRC::GetEstimatedNetworkWeight(unsigned int nPoSInterval) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
{
// The number of stakes to include in the average has been reduced to 40 (default) from 72. 72 stakes represented 1.8 hours at
// standard spacing. This is too long. 40 blocks is nominally 1 hour.
Expand All @@ -270,7 +270,7 @@ double GRC::GetEstimatedNetworkWeight(unsigned int nPoSInterval)
return result;
}

uint64_t GRC::GetAvgNetworkWeight(const unsigned int& block_interval, CBlockIndex* index_start)
uint64_t GRC::GetAvgNetworkWeight(const unsigned int& block_interval, CBlockIndex* index_start) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
{
CBlockIndex* pindex = nullptr;
arith_uint256 weight_sum = 0;
Expand All @@ -286,7 +286,7 @@ uint64_t GRC::GetAvgNetworkWeight(const unsigned int& block_interval, CBlockInde
};

if (index_start != nullptr) {
pindex = index_start;
pindex = index_start;
}

while (pindex && blocks < block_interval)
Expand All @@ -303,7 +303,7 @@ uint64_t GRC::GetAvgNetworkWeight(const unsigned int& block_interval, CBlockInde
return blocks ? (weight_sum / blocks).GetLow64() : 0;
}

uint64_t GRC::GetAvgNetworkWeight(CBlockIndex* index_start, CBlockIndex* index_end)
uint64_t GRC::GetAvgNetworkWeight(CBlockIndex* index_start, CBlockIndex* index_end) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
{
auto block_net_weight = [](CBlockIndex* index)
{
Expand All @@ -326,7 +326,7 @@ uint64_t GRC::GetAvgNetworkWeight(CBlockIndex* index_start, CBlockIndex* index_e
{
CBlockIndex* index;
arith_uint256 weight_sum;
unsigned int block_count;
unsigned int block_count = 0;

// If we are here, both index_start and index_end are not nullptrs.

Expand All @@ -342,9 +342,12 @@ uint64_t GRC::GetAvgNetworkWeight(CBlockIndex* index_start, CBlockIndex* index_e
throw std::invalid_argument("End index if specified must be at a higher height than start index.");
}

for (index = index_start, block_count = 0; index != index_end; index = index->pnext, ++block_count)
for (index = index_start; index != index_end; index = index->pnext)
{
weight_sum += block_net_weight(index);
if (index->IsProofOfStake()) {
weight_sum += block_net_weight(index);
++block_count;
}
}

// Get last block for inclusive interval
Expand Down

0 comments on commit 8473938

Please sign in to comment.