Skip to content

Commit e7a5bf6

Browse files
vasildhebasto
andcommitted
fees: make the class FeeFilterRounder thread-safe
So that its methods can be called concurrently by different threads on the same object. Currently it has just one method (`round()`). Co-authored-by: Hennadii Stepanov <[email protected]>
1 parent 2074d7d commit e7a5bf6

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

src/policy/fees.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1010,8 +1010,10 @@ FeeFilterRounder::FeeFilterRounder(const CFeeRate& minIncrementalFee)
10101010
CAmount FeeFilterRounder::round(CAmount currentMinFee)
10111011
{
10121012
std::set<double>::iterator it = feeset.lower_bound(currentMinFee);
1013-
if ((it != feeset.begin() && insecure_rand.rand32() % 3 != 0) || it == feeset.end()) {
1014-
it--;
1013+
if (it == feeset.end() ||
1014+
(it != feeset.begin() &&
1015+
WITH_LOCK(m_insecure_rand_mutex, return insecure_rand.rand32()) % 3 != 0)) {
1016+
--it;
10151017
}
10161018
return static_cast<CAmount>(*it);
10171019
}

src/policy/fees.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -299,12 +299,13 @@ class FeeFilterRounder
299299
/** Create new FeeFilterRounder */
300300
explicit FeeFilterRounder(const CFeeRate& minIncrementalFee);
301301

302-
/** Quantize a minimum fee for privacy purpose before broadcast. Not thread-safe due to use of FastRandomContext */
302+
/** Quantize a minimum fee for privacy purpose before broadcast. */
303303
CAmount round(CAmount currentMinFee);
304304

305305
private:
306306
std::set<double> feeset;
307-
FastRandomContext insecure_rand;
307+
Mutex m_insecure_rand_mutex;
308+
FastRandomContext insecure_rand GUARDED_BY(m_insecure_rand_mutex);
308309
};
309310

310311
#endif // BITCOIN_POLICY_FEES_H

0 commit comments

Comments
 (0)