Skip to content

Conversation

Empact
Copy link
Contributor

@Empact Empact commented Aug 13, 2018

A simple interface which make the implementations independent of one another.

Note I also removed the inBlock argument to the public CBlockPolicyEstimator::removeTx, as all block-related removal calls are internal to the class. This simplifies the extracted interface and reduces CBlockPolicyEstimator surface area.

@Empact Empact force-pushed the mempool-observer branch 2 times, most recently from 45ef07c to 2a44b30 Compare August 13, 2018 01:50
@DrahtBot
Copy link
Contributor

DrahtBot commented Aug 13, 2018

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

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #16066 (mempool: Skip estimator if block is older than X by promag)
  • #15946 (Allow maintaining the blockfilterindex when using prune by jonasschnelli)
  • #14193 (validation: Add missing mempool locks by MarcoFalke)
  • #10443 (Add fee_est tool for debugging fee estimation code by ryanofsky)

If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

@practicalswift
Copy link
Contributor

Concept ACK

Thanks for working on these circular dependencies!

Copy link
Contributor

@l2a5b1 l2a5b1 left a comment

Choose a reason for hiding this comment

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

Concept ACK. Thanks @Empact!

/** Remove a transaction from the mempool tracking stats
* Removal is not inBlock as in-block removals are handled internally.
*/
bool removeTx(uint256 hash);
Copy link
Contributor

Choose a reason for hiding this comment

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

You can mark the declarations of interfaces::MempoolObserver methods in CBlockPolicyEstimator with override:

    void processBlock(unsigned int nBlockHeight,
                      std::vector<const CTxMemPoolEntry*>& entries) override;

    void processTransaction(const CTxMemPoolEntry& entry, bool validFeeEstimate) override;

    bool removeTx(uint256 hash) override;

@@ -198,8 +199,10 @@ class CBlockPolicyEstimator
/** Process a transaction accepted to the mempool*/
void processTransaction(const CTxMemPoolEntry& entry, bool validFeeEstimate);

/** Remove a transaction from the mempool tracking stats*/
bool removeTx(uint256 hash, bool inBlock);
/** Remove a transaction from the mempool tracking stats
Copy link
Contributor

Choose a reason for hiding this comment

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

To prevent redundant documentation, maybe you can declare the interfaces::MempoolObserver methods in CBlockPolicyEstimator without copying the documentation from the method declarations in interfaces::MempoolObserver.

    void processBlock(unsigned int nBlockHeight,
                      std::vector<const CTxMemPoolEntry*>& entries);

    void processTransaction(const CTxMemPoolEntry& entry, bool validFeeEstimate);

    /**
     * Removal is not inBlock as in-block removals are handled internally.
     */
    bool removeTx(uint256 hash);

@Empact
Copy link
Contributor Author

Empact commented Aug 14, 2018

Marked methods override and made the comments more appropriate

@Empact
Copy link
Contributor Author

Empact commented Aug 15, 2018

Made the removeTx return type void and added interface arg comments.

@l2a5b1
Copy link
Contributor

l2a5b1 commented Aug 15, 2018

utACK 56ec018 when squashed.

One nit: I was wondering about the location of mempool_observer.h. If the file belongs in src/interfaces, where it currently resides, you may want to add an entry for the MempoolObserver interface to https://github.com/bitcoin/bitcoin/blob/master/src/interfaces/README.md, which contains a list of headers.

@Empact Empact force-pushed the mempool-observer branch 2 times, most recently from d8c390b to 7b31ea2 Compare August 15, 2018 17:14
@Empact
Copy link
Contributor Author

Empact commented Aug 15, 2018

Thanks for calling out the readme. Fixed, rebased, and squashed.

@l2a5b1
Copy link
Contributor

l2a5b1 commented Aug 15, 2018

yw, nice refactor! utACK f58e17e

@DrahtBot DrahtBot mentioned this pull request Aug 23, 2018
@@ -509,6 +509,12 @@ void TxConfirmStats::removeTx(unsigned int entryHeight, unsigned int nBestSeenHe
// tracked. Txs that were part of a block have already been removed in
// processBlockTx to ensure they are never double tracked, but it is
// of no harm to try to remove them again.
void CBlockPolicyEstimator::removeTx(uint256 hash)
Copy link
Contributor

Choose a reason for hiding this comment

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

hash should be passed by const reference?

@Empact
Copy link
Contributor Author

Empact commented Sep 11, 2018

Changed removeTx and processBlock to accept const hash references.

@maflcko
Copy link
Member

maflcko commented Mar 25, 2019

copy-paste from #15638 (comment) by @ryanofsky:

While I think in some cases fixing lint-circular-dependencies errors can make code organization better, it can just as easily make it worse.

An example would be the circular dependency between txmempool and policy/fees. The fee estimation code needs access to the mempool entry struct, and the mempool code needs to call simple fee estimator add/remove functions when mempool entries get added and removed.

The simple and obvious way to write this code is to declare the mempool entry struct in txmempool.h and the add/remove functions in fees.h and have a circular dependency between the two object files. As long as both object files are part of the same static library, this is perfectly fine and causes no problems. But if you want to appease the lint checker, you only have bad and mediocre options:

  • Combining files that shouldn't be combined: merging fees and mempool files.
  • Splitting and scattering code around unnecessarily: declaring mempool entry struct in a separate file from the mempool struct.
  • Introducing unnecessary complexity: Registering fee estimator add/remove functions with the mempool at runtime, affecting runtime performance, and potentially introducing initialization bugs.

It's possible changes like these would be good for other reasons, but in general I think cleaning up spew from lint-circular-dependencies is not a good reason to make a code change if you can't point to another actual reason for the change.

@ryanofsky
Copy link
Contributor

re: #13949 (comment)

Wow, I didn't know about (or maybe forgot about?) this PR. Looking over this change, it may not be a bad idea if there are justifications for it other than fixing circular dependencies. In the pasted comment, I was actually complaining circular dependency errors in the context of #10443, not this PR.

@maflcko
Copy link
Member

maflcko commented Mar 25, 2019

This refactoring change is +80-17. Generally I'd prefer if refactoring simplified things, not made them harder

@Empact Empact force-pushed the mempool-observer branch 2 times, most recently from e87926c to 8501bdc Compare April 3, 2019 04:28
@Empact
Copy link
Contributor Author

Empact commented Apr 3, 2019

Now +70 −16, with +43 coming from the interface header itself.

I would argue this does make things simpler, by making CTxMemPool independent of CBlockPolicyEstimator. Both objects are now simpler because they both relate only to an abstract interface, which means there are no demands placed on either class apart from the capabilities and limitations of the interface.

A practical benefit is that this makes CTxMemPool more flexible, for example it introduces the possibility of testing it indirectly via an observer object.

@l2a5b1
Copy link
Contributor

l2a5b1 commented Apr 3, 2019

utACK 8501bdc. The introduction of the MempoolObserver interface seems an elegant way to break the circular dependency.

This is an abstract interface which presents removes the dependency between
CBlockPolicyEstimator and CTxMemPool
@DrahtBot
Copy link
Contributor

DrahtBot commented Jul 3, 2019

Needs rebase

@Empact
Copy link
Contributor Author

Empact commented Feb 20, 2020

Closing in favor of #17786

@Empact Empact closed this Feb 20, 2020
@Empact Empact deleted the mempool-observer branch September 7, 2020 23:37
@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.

7 participants