Skip to content

Commit 1ac7d59

Browse files
author
MarcoFalke
committed
Merge #13128: policy: Add Clang thread safety annotations for variables guarded by cs_feeEstimator
dae1423 Add locking annotations to feeStats, shortStats and longStats (practicalswift) 764e42f scripted-diff: Rename from cs_feeEstimator to m_cs_fee_estimator (practicalswift) 9a789d4 policy: Add Clang thread safety annotations for variables guarded by cs_feeEstimator (practicalswift) Pull request description: * Add Clang thread safety annotations for variables guarded by `cs_feeEstimator` * ~~Add missing `cs_feeEstimator` locks~~ Tree-SHA512: 24b1d876ad53524ee8989b9658ac1a1b2766ebb3b27a1f84601d207e74d090e33738b814afac2a1f5bcd37565abcb361c6e5adae212840ff1ca32c3c42953391
2 parents a4564b9 + dae1423 commit 1ac7d59

File tree

2 files changed

+29
-28
lines changed

2 files changed

+29
-28
lines changed

src/policy/fees.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -511,7 +511,7 @@ void TxConfirmStats::removeTx(unsigned int entryHeight, unsigned int nBestSeenHe
511511
// of no harm to try to remove them again.
512512
bool CBlockPolicyEstimator::removeTx(uint256 hash, bool inBlock)
513513
{
514-
LOCK(cs_feeEstimator);
514+
LOCK(m_cs_fee_estimator);
515515
std::map<uint256, TxStatsInfo>::iterator pos = mapMemPoolTxs.find(hash);
516516
if (pos != mapMemPoolTxs.end()) {
517517
feeStats->removeTx(pos->second.blockHeight, nBestSeenHeight, pos->second.bucketIndex, inBlock);
@@ -548,7 +548,7 @@ CBlockPolicyEstimator::~CBlockPolicyEstimator()
548548

549549
void CBlockPolicyEstimator::processTransaction(const CTxMemPoolEntry& entry, bool validFeeEstimate)
550550
{
551-
LOCK(cs_feeEstimator);
551+
LOCK(m_cs_fee_estimator);
552552
unsigned int txHeight = entry.GetHeight();
553553
uint256 hash = entry.GetTx().GetHash();
554554
if (mapMemPoolTxs.count(hash)) {
@@ -615,7 +615,7 @@ bool CBlockPolicyEstimator::processBlockTx(unsigned int nBlockHeight, const CTxM
615615
void CBlockPolicyEstimator::processBlock(unsigned int nBlockHeight,
616616
std::vector<const CTxMemPoolEntry*>& entries)
617617
{
618-
LOCK(cs_feeEstimator);
618+
LOCK(m_cs_fee_estimator);
619619
if (nBlockHeight <= nBestSeenHeight) {
620620
// Ignore side chains and re-orgs; assuming they are random
621621
// they don't affect the estimate.
@@ -693,7 +693,7 @@ CFeeRate CBlockPolicyEstimator::estimateRawFee(int confTarget, double successThr
693693
}
694694
}
695695

696-
LOCK(cs_feeEstimator);
696+
LOCK(m_cs_fee_estimator);
697697
// Return failure if trying to analyze a target we're not tracking
698698
if (confTarget <= 0 || (unsigned int)confTarget > stats->GetMaxConfirms())
699699
return CFeeRate(0);
@@ -710,6 +710,7 @@ CFeeRate CBlockPolicyEstimator::estimateRawFee(int confTarget, double successThr
710710

711711
unsigned int CBlockPolicyEstimator::HighestTargetTracked(FeeEstimateHorizon horizon) const
712712
{
713+
LOCK(m_cs_fee_estimator);
713714
switch (horizon) {
714715
case FeeEstimateHorizon::SHORT_HALFLIFE: {
715716
return shortStats->GetMaxConfirms();
@@ -819,7 +820,7 @@ double CBlockPolicyEstimator::estimateConservativeFee(unsigned int doubleTarget,
819820
*/
820821
CFeeRate CBlockPolicyEstimator::estimateSmartFee(int confTarget, FeeCalculation *feeCalc, bool conservative) const
821822
{
822-
LOCK(cs_feeEstimator);
823+
LOCK(m_cs_fee_estimator);
823824

824825
if (feeCalc) {
825826
feeCalc->desiredTarget = confTarget;
@@ -899,7 +900,7 @@ CFeeRate CBlockPolicyEstimator::estimateSmartFee(int confTarget, FeeCalculation
899900
bool CBlockPolicyEstimator::Write(CAutoFile& fileout) const
900901
{
901902
try {
902-
LOCK(cs_feeEstimator);
903+
LOCK(m_cs_fee_estimator);
903904
fileout << 149900; // version required to read: 0.14.99 or later
904905
fileout << CLIENT_VERSION; // version that wrote the file
905906
fileout << nBestSeenHeight;
@@ -924,7 +925,7 @@ bool CBlockPolicyEstimator::Write(CAutoFile& fileout) const
924925
bool CBlockPolicyEstimator::Read(CAutoFile& filein)
925926
{
926927
try {
927-
LOCK(cs_feeEstimator);
928+
LOCK(m_cs_fee_estimator);
928929
int nVersionRequired, nVersionThatWrote;
929930
filein >> nVersionRequired >> nVersionThatWrote;
930931
if (nVersionRequired > CLIENT_VERSION)
@@ -983,7 +984,7 @@ bool CBlockPolicyEstimator::Read(CAutoFile& filein)
983984

984985
void CBlockPolicyEstimator::FlushUnconfirmed() {
985986
int64_t startclear = GetTimeMicros();
986-
LOCK(cs_feeEstimator);
987+
LOCK(m_cs_fee_estimator);
987988
size_t num_entries = mapMemPoolTxs.size();
988989
// Remove every entry in mapMemPoolTxs
989990
while (!mapMemPoolTxs.empty()) {

src/policy/fees.h

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -228,10 +228,12 @@ class CBlockPolicyEstimator
228228
unsigned int HighestTargetTracked(FeeEstimateHorizon horizon) const;
229229

230230
private:
231-
unsigned int nBestSeenHeight;
232-
unsigned int firstRecordedHeight;
233-
unsigned int historicalFirst;
234-
unsigned int historicalBest;
231+
mutable CCriticalSection m_cs_fee_estimator;
232+
233+
unsigned int nBestSeenHeight GUARDED_BY(m_cs_fee_estimator);
234+
unsigned int firstRecordedHeight GUARDED_BY(m_cs_fee_estimator);
235+
unsigned int historicalFirst GUARDED_BY(m_cs_fee_estimator);
236+
unsigned int historicalBest GUARDED_BY(m_cs_fee_estimator);
235237

236238
struct TxStatsInfo
237239
{
@@ -241,34 +243,32 @@ class CBlockPolicyEstimator
241243
};
242244

243245
// map of txids to information about that transaction
244-
std::map<uint256, TxStatsInfo> mapMemPoolTxs;
246+
std::map<uint256, TxStatsInfo> mapMemPoolTxs GUARDED_BY(m_cs_fee_estimator);
245247

246248
/** Classes to track historical data on transaction confirmations */
247-
std::unique_ptr<TxConfirmStats> feeStats;
248-
std::unique_ptr<TxConfirmStats> shortStats;
249-
std::unique_ptr<TxConfirmStats> longStats;
250-
251-
unsigned int trackedTxs;
252-
unsigned int untrackedTxs;
249+
std::unique_ptr<TxConfirmStats> feeStats PT_GUARDED_BY(m_cs_fee_estimator);
250+
std::unique_ptr<TxConfirmStats> shortStats PT_GUARDED_BY(m_cs_fee_estimator);
251+
std::unique_ptr<TxConfirmStats> longStats PT_GUARDED_BY(m_cs_fee_estimator);
253252

254-
std::vector<double> buckets; // The upper-bound of the range for the bucket (inclusive)
255-
std::map<double, unsigned int> bucketMap; // Map of bucket upper-bound to index into all vectors by bucket
253+
unsigned int trackedTxs GUARDED_BY(m_cs_fee_estimator);
254+
unsigned int untrackedTxs GUARDED_BY(m_cs_fee_estimator);
256255

257-
mutable CCriticalSection cs_feeEstimator;
256+
std::vector<double> buckets GUARDED_BY(m_cs_fee_estimator); // The upper-bound of the range for the bucket (inclusive)
257+
std::map<double, unsigned int> bucketMap GUARDED_BY(m_cs_fee_estimator); // Map of bucket upper-bound to index into all vectors by bucket
258258

259259
/** Process a transaction confirmed in a block*/
260-
bool processBlockTx(unsigned int nBlockHeight, const CTxMemPoolEntry* entry);
260+
bool processBlockTx(unsigned int nBlockHeight, const CTxMemPoolEntry* entry) EXCLUSIVE_LOCKS_REQUIRED(m_cs_fee_estimator);
261261

262262
/** Helper for estimateSmartFee */
263-
double estimateCombinedFee(unsigned int confTarget, double successThreshold, bool checkShorterHorizon, EstimationResult *result) const;
263+
double estimateCombinedFee(unsigned int confTarget, double successThreshold, bool checkShorterHorizon, EstimationResult *result) const EXCLUSIVE_LOCKS_REQUIRED(m_cs_fee_estimator);
264264
/** Helper for estimateSmartFee */
265-
double estimateConservativeFee(unsigned int doubleTarget, EstimationResult *result) const;
265+
double estimateConservativeFee(unsigned int doubleTarget, EstimationResult *result) const EXCLUSIVE_LOCKS_REQUIRED(m_cs_fee_estimator);
266266
/** Number of blocks of data recorded while fee estimates have been running */
267-
unsigned int BlockSpan() const;
267+
unsigned int BlockSpan() const EXCLUSIVE_LOCKS_REQUIRED(m_cs_fee_estimator);
268268
/** Number of blocks of recorded fee estimate data represented in saved data file */
269-
unsigned int HistoricalBlockSpan() const;
269+
unsigned int HistoricalBlockSpan() const EXCLUSIVE_LOCKS_REQUIRED(m_cs_fee_estimator);
270270
/** Calculation of highest target that reasonable estimate can be provided for */
271-
unsigned int MaxUsableEstimate() const;
271+
unsigned int MaxUsableEstimate() const EXCLUSIVE_LOCKS_REQUIRED(m_cs_fee_estimator);
272272
};
273273

274274
class FeeFilterRounder

0 commit comments

Comments
 (0)