From 8473938f03abb1a7e09f39ee230d7bc926544af7 Mon Sep 17 00:00:00 2001 From: "James C. Owens" Date: Sun, 27 Oct 2024 12:28:38 -0400 Subject: [PATCH] Add thread safety keywords to difficulty functions --- src/gridcoin/staking/difficulty.cpp | 31 ++++++++++++++++------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/src/gridcoin/staking/difficulty.cpp b/src/gridcoin/staking/difficulty.cpp index 0a0fd17c74..24426177c9 100644 --- a/src/gridcoin/staking/difficulty.cpp +++ b/src/gridcoin/staking/difficulty.cpp @@ -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; @@ -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 @@ -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. @@ -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 @@ -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 @@ -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. @@ -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; @@ -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) @@ -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) { @@ -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. @@ -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