Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fee Estimator updates from Validation Interface/CScheduler thread #28368

Conversation

ismaelsadeeq
Copy link
Member

@ismaelsadeeq ismaelsadeeq commented Aug 30, 2023

This is an attempt to #11775

This Pr will enable fee estimator to listen to ValidationInterface notifications to process new transactions added and removed from the mempool.

This PR includes the following changes:

  • Added a new callback to the Validation Interface MempoolTransactionsRemovedForConnectedBlock, which notifies listeners about the transactions that have been removed due to a new block being connected, along with the height at which the transactions were removed.
  • Modified the TransactionAddedToMempool callback parameter to include additional information about the transaction needed for fee estimation.
  • Updated CBlockPolicyEstimator to process transactions using CTransactionRef instead of CTxMempoolEntry.
  • Implemented the CValidationInterface interface in CBlockPolicyEstimater and overridden the TransactionAddedToMempool, TransactionRemovedFromMempool, and MempoolTransactionsRemovedForConnectedBlock methods to receive updates from their notifications.

Prior to this PR, the fee estimator updates from the mempool, i.e whenever a new block is connected all transactions in the block that are in our mempool are going to be removed using the removeForBlock function in txmempool.cpp.

This removal triggered updates to the fee estimator. As a result, the fee estimator would block mempool's cs until it finished updating every time a new block was connected.
Instead of being blocked only on mempool tx removal, we were blocking on both tx removal and fee estimator updating.
If we want to further improve fee estimation, or add heavy-calulation steps to it, it is currently not viable as we would be slowing down block relay in the process

This PR is smaller in terms of the changes made compared to #11775, as it focuses solely on enabling fee estimator updates from the validationInterface/cscheduler thread notifications.

I have not split the validation interface because, as I understand it, the rationale behind the split in #11775 was to have MempoolInterface signals come from the mempool and CValidationInterface events come from validation. I believe this separation can be achieved in a separate refactoring PR when the need arises.

Also left out some commits from #11775

  • Some refactoring which are no longer needed.
  • Handle reorgs much better in fee estimator.
  • Track witness hash malleation in fee estimator

I believe they are a separate change that can come in a follow-up after this.

@DrahtBot
Copy link
Contributor

DrahtBot commented Aug 30, 2023

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

Code Coverage

For detailed information about the code coverage, see the test coverage report.

Reviews

See the guideline for information on the review process.

Type Reviewers
ACK TheCharlatan, willcl-ark, achow101
Concept ACK darosior, vincenzopalazzo, pablomartin4btc
Approach ACK hernanmarino, glozow

If your review is incorrectly listed, please react with 👎 to this comment and the bot will ignore it on the next update.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #28960 (kernel: Remove dependency on CScheduler by TheCharlatan)
  • #28886 (refactor: Replace sets of txiter with CTxMemPoolEntryRefs by TheCharlatan)
  • #28830 ([refactor] Check CTxMemPool options in ctor by TheCharlatan)
  • #28690 (build: Introduce internal kernel library by TheCharlatan)
  • #28687 ([POC][WIP] C++20 std::views::reverse by stickies-v)
  • #28335 (RFC: Remove boost usage from kernel api / headers by TheCharlatan)
  • #27277 (Move log messages: tx enqueue to mempool, allocation to blockstorage by Sjors)
  • #25038 (policy: nVersion=3 and Package RBF by glozow)

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.

@ismaelsadeeq ismaelsadeeq marked this pull request as ready for review August 30, 2023 10:50
@ismaelsadeeq ismaelsadeeq marked this pull request as draft August 30, 2023 10:51
@willcl-ark
Copy link
Member

Nice work!

So if correctly implemented this should speed up block relay, and enable (future) fee estimator changes to be added without blocking critical codepaths.

It would be nice to try and benchmark this somehow, but I'm unsure immediately what the best way to do this would be...

@glozow @TheCharlatan @instagibbs would be curious to know what y'all think of the approach here.

@instagibbs
Copy link
Member

I haven't spent much/any time on the interfaces themselves, will defer to others

src/init.cpp Outdated Show resolved Hide resolved
Copy link

@vincenzopalazzo vincenzopalazzo left a comment

Choose a reason for hiding this comment

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

I did a first pass on this but I should take a look in a more deep way

src/txmempool.cpp Outdated Show resolved Hide resolved
@glozow
Copy link
Member

glozow commented Aug 31, 2023

Concept ACK! Background fee estimator would be really nice (fwiw it seems like this was always the plan: #10199 (comment) and #11775 had a lot of fans). Updating asynchronously instead of blocking removeForBlock / ConnectTip should make things faster - I personally don't need a bench to be convinced though it'd be nice to see. I don't think CTxMemPool should know anything about there being a fee estimator; the dependency should be the other way around. I agree that if we want to pursue having multiple fee estimator approaches (e.g. #27995) it would be best for them to be in the background so we don't need to worry about the performance impact on every mempool operation. I can also imagine using this to test fee estimator PRs, as it'd be easy to create 2 fee estimators that use the exact same mempool data and compare their results.

I haven't reviewed the new interface closely yet but will do as soon as I can.

@darosior
Copy link
Member

Concept ACK. Not sure i'll be able to review anytime soon though.

Copy link
Member

@glozow glozow left a comment

Choose a reason for hiding this comment

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

Did a first pass

src/txmempool.cpp Show resolved Hide resolved
src/txmempool.cpp Outdated Show resolved Hide resolved
src/test/fuzz/tx_pool.cpp Outdated Show resolved Hide resolved
src/validation.cpp Outdated Show resolved Hide resolved
src/policy/fees.cpp Outdated Show resolved Hide resolved
src/txmempool.cpp Outdated Show resolved Hide resolved
@ismaelsadeeq ismaelsadeeq force-pushed the 08-2023-fee-estimator-updates-from-validation-interface-signal branch from a7250f6 to b45345d Compare September 1, 2023 10:35
@ismaelsadeeq ismaelsadeeq marked this pull request as ready for review September 1, 2023 10:35
@ismaelsadeeq ismaelsadeeq force-pushed the 08-2023-fee-estimator-updates-from-validation-interface-signal branch from b45345d to 2f6d2b1 Compare September 1, 2023 10:45
@ismaelsadeeq ismaelsadeeq force-pushed the 08-2023-fee-estimator-updates-from-validation-interface-signal branch from 2f6d2b1 to db4849c Compare September 1, 2023 10:55
@DrahtBot DrahtBot removed the CI failed label Sep 1, 2023
src/validationinterface.h Outdated Show resolved Hide resolved
src/init.cpp Outdated Show resolved Hide resolved
src/validationinterface.h Outdated Show resolved Hide resolved
src/policy/fees.cpp Outdated Show resolved Hide resolved
@ismaelsadeeq ismaelsadeeq force-pushed the 08-2023-fee-estimator-updates-from-validation-interface-signal branch from db4849c to 9ae8482 Compare September 3, 2023 17:35
Copy link
Contributor

@TheCharlatan TheCharlatan left a comment

Choose a reason for hiding this comment

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

Re-ACK 91504cb

@DrahtBot DrahtBot requested review from hernanmarino and vincenzopalazzo and removed request for hernanmarino and vincenzopalazzo November 22, 2023 12:39
Copy link
Member

@willcl-ark willcl-ark left a comment

Choose a reason for hiding this comment

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

ACK 91504cb

Glad to see the fee estimator being decoupled in this way and keen to see more development of additional estimators in the future e.g. as per #27995

I gave the code a review and it seems well thought-out to me at this stage; left a few nits which can be addressed in the case that you re-touch things.

@@ -140,6 +150,7 @@ class CValidationInterface {
virtual void BlockConnected(ChainstateRole role, const std::shared_ptr<const CBlock> &block, const CBlockIndex *pindex) {}
/**
* Notifies listeners of a block being disconnected
* Provides the block that was connected.
Copy link
Member

Choose a reason for hiding this comment

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

nit if you retouch bfcd401:

Suggested change
* Provides the block that was connected.
* Provides the block that was disconnected.

@@ -208,4 +208,33 @@ struct RemovedMempoolTransactionInfo {
: info{entry.GetSharedTx(), entry.GetFee(), entry.GetTxSize(), entry.GetHeight()} {}
};

struct NewMempoolTransactionInfo {
Copy link
Member

Choose a reason for hiding this comment

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

In dff5ad3

Worth adding a doxygen comment here? Even something simple like:

/**
 * Holds information about new transactions added to the mempool.
 * In addition to TransactionInfo includes limit bypass, package, chain and parent info.
 */

struct NewMempoolTransactionInfo {
TransactionInfo info;
/*
* This boolean indicates whether the transaction was added
Copy link
Member

Choose a reason for hiding this comment

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

In dff5ad3

You've called this m_from_disconnected_block, but described it as whether the tx was added without enforcing mempool fee limits. This seems incorrect or confusing.

SubmitPackage() is calling this using args.m_bypass_limits so i think the comment is correct, and the variable should be renamed?

@@ -285,8 +285,12 @@ void Shutdown(NodeContext& node)
DumpMempool(*node.mempool, MempoolPath(*node.args));
}

// Drop transactions we were still watching, and record fee estimations.
if (node.fee_estimator) node.fee_estimator->Flush();
// Drop transactions we were still watching, record fee estimations and Unregister
Copy link
Member

Choose a reason for hiding this comment

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

micro nit, if you end up touching 7145239 again:

Suggested change
// Drop transactions we were still watching, record fee estimations and Unregister
// Drop transactions we were still watching, record fee estimations and unregister

const auto tx_info = NewMempoolTransactionInfo(entry.GetSharedTx(), entry.GetFee(),
entry.GetTxSize(), entry.GetHeight(),
/* m_from_disconnected_block */ false,
/* m_submitted_in_package */ false,
Copy link
Member

Choose a reason for hiding this comment

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

In 7145239

Would we want to use fuzzed_data_provider.ConsumeBool() for m_submitted_in_package?

@DrahtBot DrahtBot requested review from vincenzopalazzo and hernanmarino and removed request for hernanmarino and vincenzopalazzo November 24, 2023 12:56
@ismaelsadeeq
Copy link
Member Author

Thanks for your review @willcl-ark will address nits if there is need to retouch.

@achow101
Copy link
Member

achow101 commented Dec 1, 2023

ACK 91504cb

@DrahtBot DrahtBot requested review from hernanmarino and vincenzopalazzo and removed request for vincenzopalazzo and hernanmarino December 1, 2023 19:58
@achow101 achow101 merged commit a97a892 into bitcoin:master Dec 1, 2023
16 checks passed
Copy link
Member

@pablomartin4btc pablomartin4btc left a comment

Choose a reason for hiding this comment

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

post merge tACK 91504cb

Tested manually using RPC commands estimatesmartfee and estimaterawfee (following cases from /test/functional/feature_fee_estimation.py).

glozow added a commit that referenced this pull request Jan 3, 2024
b1318dc test: change `m_submitted_in_package` input to fuzz data provider boolean (ismaelsadeeq)
5615e16 tx fees: update `m_from_disconnected_block` to `m_mempool_limit_bypassed` (ismaelsadeeq)
fcd4296 doc: fix typo and update incorrect comment (ismaelsadeeq)
562664d test: wait for fee estimator to catch up before estimating fees (ismaelsadeeq)

Pull request description:

  This is a simple PR that does two things
  1.   Fixes #29000 by waiting for the fee estimator to catch up after `removeForBlock` calls before calling `estimateFee` in the `BlockPolicyEstimates` unit test.

  2. Addressed some outstanding review comments from #28368
  - Updated `NewMempoolTransactionInfo::m_from_disconnected_block` to `NewMempoolTransactionInfo::m_mempool_limit_bypassed` which now correctly indicates what the boolean does.
  - Changed  input of `processTransaction`'s tx_info  `m_submitted_in_package` input from false to fuzz data provider boolean.
  - Fixed some typos, and update incorrect comment

ACKs for top commit:
  martinus:
    re-ACK b1318dc
  glozow:
    utACK b1318dc

Tree-SHA512: 45268729bc044da4748fe004524e0df696d2ec92c5bd053db9aad6e15675f3838429b2a7b9061a6b694be4dc319d1782a876b44df506ddd439d62ad07252d0e1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet