Skip to content

fees: prevent redundant estimates flushes #32748

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

ismaelsadeeq
Copy link
Member

@ismaelsadeeq ismaelsadeeq commented Jun 14, 2025

This PR does two things:

  1. It prevents redundant writes to the fee_estimates.dat file when there is no usable data available. For example, during IBD, blocks are still being connected, but unnecessary flushes still occur every hour.

  2. It updates the flushing log to use debug-level logging under the estimatefee category. It also ensures the log consistently includes only the full file path.

@DrahtBot
Copy link
Contributor

DrahtBot commented Jun 14, 2025

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

Code Coverage & Benchmarks

For details see: https://corecheck.dev/bitcoin/bitcoin/pulls/32748.

Reviews

See the guideline for information on the review process.

Type Reviewers
Concept ACK Eunovo

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:

  • #29641 (scripted-diff: Use LogInfo over LogPrintf [WIP, NOMERGE, DRAFT] by maflcko)

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.

LLM Linter (✨ experimental)

Possible typos and grammar issues:

  • “but not have enough stats to provide an estimate” -> “but may not have enough stats to provide an estimate” [missing verb in the second clause]

drahtbot_id_4_m

@ismaelsadeeq ismaelsadeeq force-pushed the 06-2025-prevent-redundant-estimate-flushes branch from 21746c6 to 48a3e8f Compare June 14, 2025 06:05
@DrahtBot
Copy link
Contributor

🚧 At least one of the CI tasks failed.
Task fuzzer,address,undefined,integer, no depends: https://github.com/bitcoin/bitcoin/runs/44091090061
LLM reason (✨ experimental): The CI failure is caused by a thread-safety analysis error in policy_estimator.cpp due to unsafe mutex usage during a function call.

Hints

Try to run the tests locally, according to the documentation. However, a CI failure may still
happen due to a number of reasons, for example:

  • Possibly due to a silent merge conflict (the changes in this pull request being
    incompatible with the current code in the target branch). If so, make sure to rebase on the latest
    commit of the target branch.

  • A sanitizer issue, which can only be found by compiling with the sanitizer and running the
    affected test.

  • An intermittent issue.

Leave a comment here, if you need help tracking down a confusing failure.

Copy link
Contributor

@Eunovo Eunovo 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 48a3e8f

Left some comments about logging.

@ismaelsadeeq ismaelsadeeq force-pushed the 06-2025-prevent-redundant-estimate-flushes branch from 48a3e8f to 458fdac Compare June 19, 2025 13:34
Copy link
Member Author

@ismaelsadeeq ismaelsadeeq left a comment

Choose a reason for hiding this comment

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

Addressed comments by @Eunovo thanks for review 21f0deaf1..458fdac

@ismaelsadeeq ismaelsadeeq force-pushed the 06-2025-prevent-redundant-estimate-flushes branch from 458fdac to 4a0672f Compare June 19, 2025 15:33
Comment on lines 963 to 968
{
LOCK(m_cs_fee_estimator);
// We should only flush the fee estimates if usable estimates are available.
// This prevents redundant flushes e.g during IBD.
if (!MaxUsableEstimate()) return;
}
Copy link
Member

@furszy furszy Jul 3, 2025

Choose a reason for hiding this comment

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

nit: could write this in a single line instead.

if (WITH_LOCK(m_cs_fee_estimator, return !MaxUsableEstimate())) return;

Also, it would be nice to explain why MaxUsableEstimate returning 0 means "nothing to do". It is not immediately clear to me without checking the MaxUsableEstimate internals.

Copy link
Member Author

@ismaelsadeeq ismaelsadeeq Jul 5, 2025

Choose a reason for hiding this comment

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

Taken, also added an explanation.

I also note a behavior change introduced by this. It is possible that we may track some unconfirmed transactions and, in rare cases, see some unconfirmed transactions get evicted. However, since we have no confirmations yet, if we shut down in this state, we will lose that data and have to start from scratch. To mitigate this, we need a way to detect when we are in such a state.

I see two possible approaches:

  1. Brute-force check : Go through all the stats to ensure we have no useful data.
  2. Smart tracking : Add some of flag or tracker to each stat that indicates whether there is any useful data available. This would require extra processing when adding transactions to the mempool and, in rare cases, when transactions are evicted from the mempool.

Also related to this: checking whether MaxUsableEstimate is non-zero is not sufficient to decide whether we should avoid updating the unconfirmed circular buffer or decaying all exponential averages.

Additionally, simply tracking whether we have ever seen an unconfirmed transaction or a failure is not enough. There is an edge case where you may have seen that event,but if the relevant data has since decayed, you could end up updating with effectively empty stats. The indicator would need to account for data decay to provide an accurate signal.

Given this, I am starting to doubt whether the performance gain is worth the added complexity cc @l0rinc .

- Also log the full file path of fee_estimates.dat consistently
  not just the filename.
@ismaelsadeeq ismaelsadeeq force-pushed the 06-2025-prevent-redundant-estimate-flushes branch from 45d35d3 to 3a4a4ba Compare August 12, 2025 08:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants