fees: prevent redundant estimates flushes #32748

pull ismaelsadeeq wants to merge 2 commits into bitcoin:master from ismaelsadeeq:06-2025-prevent-redundant-estimate-flushes changing 2 files +40 −8
  1. ismaelsadeeq commented at 5:53 am on June 14, 2025: member

    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.

  2. DrahtBot commented at 5:53 am on June 14, 2025: contributor

    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:

    • #32967 (log: [refactor] Use info level for init logs by maflcko)
    • #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:

    • typo: “that data may is lost” → “that data may be lost” [incorrect verb form]
    • typo: “but not have enough stats to provide an estimate” → “but may not have enough stats to provide an estimate” [missing auxiliary/subject]

    drahtbot_id_4_m

  3. ismaelsadeeq force-pushed on Jun 14, 2025
  4. DrahtBot added the label CI failed on Jun 14, 2025
  5. DrahtBot commented at 6:05 am on June 14, 2025: 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.

    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.

  6. DrahtBot removed the label CI failed on Jun 14, 2025
  7. in src/policy/fees.cpp:563 in 48a3e8f577 outdated
    559@@ -560,18 +560,18 @@ CBlockPolicyEstimator::CBlockPolicyEstimator(const fs::path& estimation_filepath
    560     AutoFile est_file{fsbridge::fopen(m_estimation_filepath, "rb")};
    561 
    562     if (est_file.IsNull()) {
    563-        LogPrintf("%s is not found. Continue anyway.\n", fs::PathToString(m_estimation_filepath));
    564+        LogPrintf("%s is not found. Continue anyway.\n", fs::PathToString(m_estimation_filepath.filename()));
    


    Eunovo commented at 6:47 am on June 19, 2025:
    LogPrintf is deprecated, See https://github.com/bitcoin/bitcoin/blob/master/src/logging.h#L265-L266 Consisder using LogInfo instead

    ismaelsadeeq commented at 1:34 pm on June 19, 2025:
    Done
  8. in src/policy/fees.cpp:569 in 48a3e8f577 outdated
    566     }
    567 
    568     std::chrono::hours file_age = GetFeeEstimatorFileAge();
    569     if (file_age > MAX_FILE_AGE && !read_stale_estimates) {
    570-        LogPrintf("Fee estimation file %s too old (age=%lld > %lld hours) and will not be used to avoid serving stale estimates.\n", fs::PathToString(m_estimation_filepath), Ticks<std::chrono::hours>(file_age), Ticks<std::chrono::hours>(MAX_FILE_AGE));
    571+        LogPrintf("Fee estimation file %s too old (age=%lld > %lld hours) and will not be used to avoid serving stale estimates.\n", fs::PathToString(m_estimation_filepath.filename()), Ticks<std::chrono::hours>(file_age), Ticks<std::chrono::hours>(MAX_FILE_AGE));
    


    Eunovo commented at 7:11 am on June 19, 2025:
    • Also don’t log the full file path, just the filename.

    What’s the reason for this? This reduces debugging information and bitcoin-core generally uses file path logging over filename logging.


    ismaelsadeeq commented at 1:34 pm on June 19, 2025:
    Thats right, this is fixed now.
  9. Eunovo commented at 7:36 am on June 19, 2025: contributor
  10. ismaelsadeeq force-pushed on Jun 19, 2025
  11. ismaelsadeeq commented at 1:36 pm on June 19, 2025: member
    Addressed comments by @Eunovo thanks for review 21f0deaf1..458fdac
  12. ismaelsadeeq force-pushed on Jun 19, 2025
  13. DrahtBot added the label CI failed on Jun 19, 2025
  14. DrahtBot removed the label CI failed on Jun 19, 2025
  15. DrahtBot added the label Needs rebase on Jul 3, 2025
  16. in src/policy/fees.cpp:968 in 26c78d611d outdated
    959@@ -960,6 +960,13 @@ void CBlockPolicyEstimator::Flush() {
    960 
    961 void CBlockPolicyEstimator::FlushFeeEstimates()
    962 {
    963+    {
    964+        LOCK(m_cs_fee_estimator);
    965+        // We should only flush the fee estimates if usable estimates are available.
    966+        // This prevents redundant flushes e.g during IBD.
    967+        if (!MaxUsableEstimate()) return;
    968+    }
    


    furszy commented at 11:54 pm on July 3, 2025:

    nit: could write this in a single line instead.

    0if (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.


    ismaelsadeeq commented at 3:07 pm on July 5, 2025:

    Taken, also please add 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 .

  17. fees: prevent redundant estimates flushes a4b97f1fbd
  18. fees: make flushes log debug only
    - Also log the full file path of fee_estimates.dat consistently
      not just the filename.
    e2539c88f1
  19. ismaelsadeeq force-pushed on Jul 5, 2025
  20. DrahtBot removed the label Needs rebase on Jul 5, 2025
  21. DrahtBot added the label Needs rebase on Jul 25, 2025
  22. DrahtBot commented at 12:43 pm on July 25, 2025: contributor
    🐙 This pull request conflicts with the target branch and needs rebase.

github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2025-07-27 06:13 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me