Skip to content

Commit 877cca5

Browse files
committed
refactor: Make FeeFilterRounder thread safe
1 parent eac65d9 commit 877cca5

File tree

3 files changed

+7
-5
lines changed

3 files changed

+7
-5
lines changed

src/net_processing.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4396,7 +4396,7 @@ bool PeerLogicValidation::SendMessages(CNode* pto)
43964396
int64_t timeNow = GetTimeMicros();
43974397
if (timeNow > pto->m_tx_relay->nextSendTimeFeeFilter) {
43984398
static CFeeRate default_feerate(DEFAULT_MIN_RELAY_TX_FEE);
4399-
static FeeFilterRounder filterRounder(default_feerate);
4399+
static const FeeFilterRounder filterRounder(default_feerate);
44004400
CAmount filterToSend = filterRounder.round(currentFilter);
44014401
// We always have a fee filter of at least minRelayTxFee
44024402
filterToSend = std::max(filterToSend, ::minRelayTxFee.GetFeePerK());

src/policy/fees.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -968,10 +968,11 @@ FeeFilterRounder::FeeFilterRounder(const CFeeRate& minIncrementalFee)
968968
}
969969
}
970970

971-
CAmount FeeFilterRounder::round(CAmount currentMinFee)
971+
CAmount FeeFilterRounder::round(CAmount currentMinFee) const
972972
{
973973
std::set<double>::iterator it = feeset.lower_bound(currentMinFee);
974-
if ((it != feeset.begin() && insecure_rand.rand32() % 3 != 0) || it == feeset.end()) {
974+
const uint32_t rand = WITH_LOCK(m_random_context_mutex, return m_insecure_rand.rand32());
975+
if ((it != feeset.begin() && rand % 3 != 0) || it == feeset.end()) {
975976
it--;
976977
}
977978
return static_cast<CAmount>(*it);

src/policy/fees.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -281,11 +281,12 @@ class FeeFilterRounder
281281
explicit FeeFilterRounder(const CFeeRate& minIncrementalFee);
282282

283283
/** Quantize a minimum fee for privacy purpose before broadcast **/
284-
CAmount round(CAmount currentMinFee);
284+
CAmount round(CAmount currentMinFee) const;
285285

286286
private:
287287
std::set<double> feeset;
288-
FastRandomContext insecure_rand;
288+
mutable Mutex m_random_context_mutex;
289+
mutable FastRandomContext m_insecure_rand GUARDED_BY(m_random_context_mutex);
289290
};
290291

291292
#endif // BITCOIN_POLICY_FEES_H

0 commit comments

Comments
 (0)