Skip to content

Conversation

@hebasto
Copy link
Member

@hebasto hebasto commented Jun 13, 2020

The FastRandomContext class is documented as not thread-safe.
This PR adds a relevant note to the FeeFilterRounder::round() function declaration.

Close #19254

Copy link
Member

@maflcko maflcko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My preference would be to just document it. Unless this is the only random context without a lock held in the net_processing loop.

As soon as the net_processing loop is run in parallel, our sanitizers should point out any unsafe code, so there should be no risk in leaving the code as-is.

/** Create new FeeFilterRounder */
explicit FeeFilterRounder(const CFeeRate& minIncrementalFee);

/** Quantize a minimum fee for privacy purpose before broadcast **/
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/** Quantize a minimum fee for privacy purpose before broadcast **/
/** Quantize a minimum fee for privacy purpose before broadcast . Not thread-safe due to use of FastRandomContext **/

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is not going to be changed, it's probably better to make it an explicit global.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I am not suggestion to never change this. This can be changed when net_processing is multi-threaded. Until then documentation and sanitizers are good enough to leave the code as is for now.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't it as thread safe as a class A { int a = 0; public: int increment() { return ++a; } }, which we'd solve by saying static A my_a GUARDED_BY(cs_main); ? And both could be made parallelisable by saying static thread_local A my_a; -- it's just that GUARDED_BY doesn't work with block-scope statics?

Just adding // protected by cs_main at the declaration of filterRounder, or moving it to be a module-level static and adding a GUARDED_BY(cs_main) seems better to me?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@DrahtBot
Copy link
Contributor

DrahtBot commented Jun 14, 2020

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

Conflicts

No conflicts as of last run.

@naumenkogs
Copy link
Contributor

Concept ACK.

I don't have a strong opinion on taking either approach (mutex vs documentation-only)

std::set<double> feeset;
FastRandomContext insecure_rand;
mutable Mutex m_random_context_mutex;
mutable FastRandomContext m_insecure_rand GUARDED_BY(m_random_context_mutex);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it'd make more sense to mark feeset as const, than marking the whole object as const, and the parts that change as mutable (given const X x;, then a = x.foo(); b = x.foo(); a == b ought to be true imo).

Adding a static std::set<double> make_feeset(const CFeeRate&) member function and using that to initialise feeset via the initializer list looks like it's enough?

/** Create new FeeFilterRounder */
explicit FeeFilterRounder(const CFeeRate& minIncrementalFee);

/** Quantize a minimum fee for privacy purpose before broadcast **/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't it as thread safe as a class A { int a = 0; public: int increment() { return ++a; } }, which we'd solve by saying static A my_a GUARDED_BY(cs_main); ? And both could be made parallelisable by saying static thread_local A my_a; -- it's just that GUARDED_BY doesn't work with block-scope statics?

Just adding // protected by cs_main at the declaration of filterRounder, or moving it to be a module-level static and adding a GUARDED_BY(cs_main) seems better to me?

CAmount currentFilter = m_mempool.GetMinFee(gArgs.GetArg("-maxmempool", DEFAULT_MAX_MEMPOOL_SIZE) * 1000000).GetFeePerK();
int64_t timeNow = GetTimeMicros();
if (timeNow > pto->m_tx_relay->nextSendTimeFeeFilter) {
static CFeeRate default_feerate(DEFAULT_MIN_RELAY_TX_FEE);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can make default_feerate const too -- could make it constexpr even, so long as the constructors were also marked constexpr.

@hebasto
Copy link
Member Author

hebasto commented Jul 5, 2020

Updated 877cca5 -> d842e6a (pr19268.01 -> pr19268.02):

@hebasto hebasto changed the title refactor: Make FeeFilterRounder thread safe doc: Add non-thread-safe note to FeeFilterRounder::round() Jul 5, 2020
@maflcko
Copy link
Member

maflcko commented Jul 5, 2020

self ACK d842e6a

@practicalswift
Copy link
Contributor

ACK d842e6a: explicit is better than implicit

@naumenkogs
Copy link
Contributor

ACK d842e6a

@maflcko maflcko merged commit 07c83ce into bitcoin:master Jul 14, 2020
@hebasto hebasto deleted the 200613-fee branch July 14, 2020 07:34
sidhujag pushed a commit to syscoin/syscoin that referenced this pull request Jul 14, 2020
…r::round()

d842e6a doc: Add non-thread-safe note to FeeFilterRounder::round() (Hennadii Stepanov)

Pull request description:

  The `FastRandomContext` class is documented as not thread-safe.
  This PR adds a relevant note to the `FeeFilterRounder::round()` function declaration.

  Close bitcoin#19254

ACKs for top commit:
  MarcoFalke:
    self ACK d842e6a
  practicalswift:
    ACK d842e6a: explicit is better than implicit
  naumenkogs:
    ACK d842e6a

Tree-SHA512: 538508f24b9cb29baece6a64108e2c5fc3960768c6475c4f2baf48a4a7bdb96dcef1a74d21a4822e1f8635e1375362986da4e3a20f5644129046a354c4b0a8a0
Fabcien pushed a commit to Bitcoin-ABC/bitcoin-abc that referenced this pull request Aug 31, 2021
Summary:
Co-authored-by: MarcoFalke <[email protected]>
This is a backport of [[bitcoin/bitcoin#19268 | core#19268]]

Test Plan: Proof-reading, check that our implementations of `FeeFilterRounder::round` are similar enough that the comment applies.

Reviewers: #bitcoin_abc, majcosta

Reviewed By: #bitcoin_abc, majcosta

Differential Revision: https://reviews.bitcoinabc.org/D9995
@bitcoin bitcoin locked as resolved and limited conversation to collaborators Feb 15, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

static FeeFilterRounder in net_processing needs a mutex (or documentation that concurrent access is not allowed)

7 participants