From e7a5bf6be79e341e037305a4c2d8a1a510a8d709 Mon Sep 17 00:00:00 2001 From: Vasil Dimov Date: Mon, 21 Feb 2022 17:03:27 +0100 Subject: 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 <32963518+hebasto@users.noreply.github.com> --- src/policy/fees.cpp | 6 ++++-- src/policy/fees.h | 5 +++-- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/policy/fees.cpp b/src/policy/fees.cpp index 6499dbd97f..9f576e738a 100644 --- a/src/policy/fees.cpp +++ b/src/policy/fees.cpp @@ -1010,8 +1010,10 @@ FeeFilterRounder::FeeFilterRounder(const CFeeRate& minIncrementalFee) CAmount FeeFilterRounder::round(CAmount currentMinFee) { std::set::iterator it = feeset.lower_bound(currentMinFee); - if ((it != feeset.begin() && insecure_rand.rand32() % 3 != 0) || it == feeset.end()) { - it--; + if (it == feeset.end() || + (it != feeset.begin() && + WITH_LOCK(m_insecure_rand_mutex, return insecure_rand.rand32()) % 3 != 0)) { + --it; } return static_cast(*it); } diff --git a/src/policy/fees.h b/src/policy/fees.h index 6e25bb42b8..c7fa1a0b77 100644 --- a/src/policy/fees.h +++ b/src/policy/fees.h @@ -299,12 +299,13 @@ public: /** Create new FeeFilterRounder */ explicit FeeFilterRounder(const CFeeRate& minIncrementalFee); - /** Quantize a minimum fee for privacy purpose before broadcast. Not thread-safe due to use of FastRandomContext */ + /** Quantize a minimum fee for privacy purpose before broadcast. */ CAmount round(CAmount currentMinFee); private: std::set feeset; - FastRandomContext insecure_rand; + Mutex m_insecure_rand_mutex; + FastRandomContext insecure_rand GUARDED_BY(m_insecure_rand_mutex); }; #endif // BITCOIN_POLICY_FEES_H -- cgit v1.2.3