Repository navigation
Conversation
|
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. Code Coverage & BenchmarksFor details see: https://corecheck.dev/bitcoin/bitcoin/pulls/36095. ReviewsSee the guideline and AI policy for information on the review process.
If your review is incorrectly listed, please copy-paste ConflictsReviewers, this pull request conflicts with the following ones:
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. |
|
Concept ACK |
| static const uint64_t MEMPOOL_DUMP_VERSION{2}; | ||
|
|
||
| bool LoadMempool(CTxMemPool& pool, const fs::path& load_path, Chainstate& active_chainstate, ImportMempoolOptions&& opts) | ||
| MempoolLoadResult LoadMempool(CTxMemPool& pool, const fs::path& load_path, Chainstate& active_chainstate, ImportMempoolOptions&& opts) |
There was a problem hiding this comment.
If we are introducing a new return type, I think this may as well be util::Expected<MempoolRestoreStats, MempoolRestoreError>. There are many possible error cases here and all return {}
There was a problem hiding this comment.
@rustaceanrob Thanks for the suggestion. I agree that util::Expected could make sense if LoadMempool() also exposed structured failure reasons, but I think that would be a broader refactor.
This PR change has two independent outcomes: whether loading completed successfully and optional restoration statistics, which are collected only when needed.
An Expected<MempoolRestoreStats, MempoolRestoreError> cannot represent a successful load where statistics were intentionally not collected. With the current API, that would require something like Expected<std::optional<MempoolRestoreStats>, MempoolRestoreError> or collecting the statistics unconditionally.
The current callers also do not currently consume distinct failure reasons. Startup only needs to know whether loading completed, with interruption handled separately, while importmempool reports a generic error and directs users to the debug log.
I would prefer to preserve the existing error semantics in this PR. A follow-up could introduce structured load errors and decide whether callers such as importmempool should expose them. Does that sound reasonable?
There was a problem hiding this comment.
Sure, seems fine to leave for later, and can proceed as is.
FWIW, for the future, I think Expected<std::optional<MempoolRestoreStats>, MempoolRestoreError> is a very clear signal in the function signature of what exactly is happening. As you explained, we may have mempool restore stats but they are optional, and we may also fail for some number of reasons. The return type expresses that directly.
There was a problem hiding this comment.
With @ismaelsadeeq’s simplification, util::Expected became a much cleaner fit because the successful result is now just the snapshot weight.
LoadMempool() now returns util::Expected<uint64_t, MempoolLoadError>.
I’ve also added you as a co-author.
ismaelsadeeq
left a comment
There was a problem hiding this comment.
Concept ACK
I think this can be simplified. We can drop tracking the wtxids of the whole mempool (and the second GetIter pass) by accumulating the weight of the persisted transactions during load, then, after loading, querying the mempool's total transaction weight (can be cached, but in the diff below it is computed in-flight) and passing both weights to the mempool fee rate estimator.
Details
diff --git a/src/init.cpp b/src/init.cpp
index aa2a61fac1..8772eac736 100644
--- a/src/init.cpp
+++ b/src/init.cpp
@@ -2136,14 +2136,13 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
const auto load_result{LoadMempool(
*pool,
ShouldPersistMempool(args) ? MempoolPath(args) : fs::path{},
- chainman.ActiveChainstate(),
- {.collect_restore_stats = node.fee_estimator_man != nullptr})};
+ chainman.ActiveChainstate(), {})};
if (node.fee_estimator_man && !chainman.m_interrupt) {
- const auto restore_stats{load_result.restore_stats.value_or(node::MempoolRestoreStats{})};
+ // The mempool started empty, so its total weight after loading is
+ // effectively the portion of the snapshot that was restored.
+ const uint64_t restored_weight{WITH_LOCK(pool->cs, return pool->GetTotalTxWeight())};
node.fee_estimator_man->MempoolLoadCompleted(
- load_result.success && load_result.restore_stats.has_value(),
- restore_stats.total_tx_weight,
- restore_stats.restored_tx_weight);
+ load_result.success, load_result.snapshot_weight, restored_weight);
}
pool->SetLoadTried(!chainman.m_interrupt);
}
diff --git a/src/node/mempool_persist.cpp b/src/node/mempool_persist.cpp
index 04bd414ca0..20c683d78c 100644
--- a/src/node/mempool_persist.cpp
+++ b/src/node/mempool_persist.cpp
@@ -57,8 +57,7 @@ MempoolLoadResult LoadMempool(CTxMemPool& pool, const fs::path& load_path, Chain
int64_t already_there = 0;
int64_t unbroadcast = 0;
const auto now{NodeClock::now()};
- std::vector<Wtxid> snapshot_wtxids;
- uint64_t total_snapshot_tx_weight{0};
+ uint64_t snapshot_weight{0};
try {
uint64_t version;
@@ -94,10 +93,7 @@ MempoolLoadResult LoadMempool(CTxMemPool& pool, const fs::path& load_path, Chain
file >> TX_WITH_WITNESS(tx);
file >> nTime;
file >> nFeeDelta;
- if (opts.collect_restore_stats) {
- snapshot_wtxids.push_back(tx->GetWitnessHash());
- total_snapshot_tx_weight += static_cast<uint64_t>(GetTransactionWeight(*tx));
- }
+ snapshot_weight += static_cast<uint64_t>(GetTransactionWeight(*tx));
if (opts.use_current_time) {
nTime = TicksSinceEpoch<std::chrono::seconds>(now);
@@ -154,20 +150,9 @@ MempoolLoadResult LoadMempool(CTxMemPool& pool, const fs::path& load_path, Chain
}
LogInfo("Imported mempool transactions from file: %i succeeded, %i failed, %i expired, %i already there, %i waiting for initial broadcast\n", count, failed, expired, already_there, unbroadcast);
- std::optional<MempoolRestoreStats> restore_stats;
- if (opts.collect_restore_stats) {
- restore_stats.emplace();
- restore_stats->total_tx_weight = total_snapshot_tx_weight;
- LOCK(pool.cs);
- for (const auto& wtxid : snapshot_wtxids) {
- if (const auto it{pool.GetIter(wtxid)}) {
- restore_stats->restored_tx_weight += static_cast<uint64_t>((*it)->GetTxWeight());
- }
- }
- }
return {
.success = true,
- .restore_stats = std::move(restore_stats),
+ .snapshot_weight = snapshot_weight,
};
}
diff --git a/src/node/mempool_persist.h b/src/node/mempool_persist.h
index 8b9858a839..3bdc0fdd59 100644
--- a/src/node/mempool_persist.h
+++ b/src/node/mempool_persist.h
@@ -8,7 +8,6 @@
#include <util/fs.h>
#include <cstdint>
-#include <optional>
class Chainstate;
class CTxMemPool;
@@ -25,22 +24,13 @@ struct ImportMempoolOptions {
bool use_current_time{false};
bool apply_fee_delta_priority{true};
bool apply_unbroadcast_set{true};
- //! Collect final restored transaction weight for startup health validation.
- bool collect_restore_stats{false};
-};
-
-struct MempoolRestoreStats {
- //! Total transaction weight in the persisted mempool snapshot.
- uint64_t total_tx_weight{0};
- //! Weight of snapshot transactions present after loading finishes.
- uint64_t restored_tx_weight{0};
};
struct MempoolLoadResult {
//! Whether the mempool file was successfully deserialized.
bool success{false};
- //! Populated only when restore statistics were requested.
- std::optional<MempoolRestoreStats> restore_stats;
+ //! Total weight of the transactions contained in the snapshot file.
+ uint64_t snapshot_weight{0};
};
/** Import the file and attempt to add its contents to the mempool. */
diff --git a/src/txmempool.h b/src/txmempool.h
index 7d7aafde78..8da1ce6456 100644
--- a/src/txmempool.h
+++ b/src/txmempool.h
@@ -504,6 +504,18 @@ public:
return totalTxSize;
}
+ //! Sum of all mempool tx's weights (BIP 141). Computed in flight by walking
+ //! the pool, so this method should not be called in hot paths.
+ uint64_t GetTotalTxWeight() const EXCLUSIVE_LOCKS_REQUIRED(cs)
+ {
+ AssertLockHeld(cs);
+ uint64_t total_weight{0};
+ for (const auto& entry : mapTx) {
+ total_weight += entry.GetTxWeight();
+ }
+ return total_weight;
+ }
+
CAmount GetTotalFee() const EXCLUSIVE_LOCKS_REQUIRED(cs)
{
AssertLockHeld(cs);| constexpr double MEMPOOL_REPRESENTATION_THRESHOLD = 0.75; | ||
| //! Reuse persisted mined-block statistics only when the restored snapshot | ||
| //! meets the mempool representation threshold. | ||
| constexpr double MEMPOOL_SNAPSHOT_RESTORATION_THRESHOLD{ |
There was a problem hiding this comment.
In "fees: validate persisted mempool estimator data after restoration" 50d8d6e
I think we should not reuse the MEMPOOL_REPRESENTATION_THRESHOLD constant here just copy the .75 value there. The representations are orthogonal.
bbefa5c to
34e4393
Compare
|
@ismaelsadeeq Thanks for the suggestion. If I understand correctly, this relies on the mempool containing only transactions from the snapshot until loading finishes. For example, wallets can submit transactions while the background mempool load is still running. The existing loader explicitly accounts for this possibility:
See Consequently, the final mempool weight can include transactions that were not in mempool.dat and overstate the restored snapshot weight. Subtracting the initial mempool weight would not fully solve this because transactions can be inserted, removed, replaced, or evicted during loading. Or am I missing something? |
Correct, the mempool total can include wallet txs that were not in mempool.dat spapshot. But I don't think it's a problem. Most of what a wallet re-adds is already in the persisted snapshot — that's the already_there case in the comment — so it doesn't inflate the total. The only genuinely extra txs are wallet ones that were never persisted, which is most likely a small set and won't occur frequently. The effect, as you mentioned, is that it pushes the ratio up, so the worst case is we keep stats we could have dropped (user-wise effect they see a low fee rate estimate, because some high fee rate txs are not seen now); we never drop good ones, which is the case we actually care about. Also worth keeping in mind this is a heuristic check, and the mempool estimator only looks at the top block of the mempool anyway, not the whole thing so chasing exact whole-mempool weight is more precision than needed. So it would be nice if we keep this simpler approach. |
34e4393 to
66df06f
Compare
|
@ismaelsadeeq Thanks for the clarification. |
|
CI error seems unrelated. |
|
🚧 At least one of the CI tasks failed. HintsTry to run the tests locally, according to the documentation. However, a CI failure may still
Leave a comment here, if you need help tracking down a confusing failure. |
ismaelsadeeq
left a comment
There was a problem hiding this comment.
Approach ACK
Apologies for taking a while to get back to you, I was afk, now I'm back.
I took another look and have a couple of comments.
- 66df06f "fees: validate persisted mempool estimator data using post-load weight" does a lot, can you split it into two commits?
-
First: skip the initial persisted mined-block stats read when
-persistmempool=0, and gate the mempool-policy estimator's own read/write paths onpersistmempool. KeepIntervalFlush()andShutdownFlush()scheduled and calling the estimator unconditionally (they also flush the block-policy estimator); the mempool estimator no-ops its own read and flush when persistence is off.
(I think this is the commit that should be on the milestone for 32.0.) -
Second: use the mempool load result to decide whether to read the persisted mined-block statistics.
-
There is a narrow window where stale/unvalidated persisted statistics can be exposed. The fee-rate estimate itself is already safe, since EstimateFeeRate returns "Mempool not loaded yet" until
pool.SetLoadTried()is true, which runs after the load completes. But themempool_health_statisticsfield (backed byGetPrevBlockData) is not gated, so between estimator construction and the retention decision it can surface persisted mined-block stats that have not been validated against the post-load weight yet. We could gate that field too, but that is a not the right fix imo. A cleaner fix is to make it correct by construction: defer the persisted mined-block read until the mempool has finished loading and we have the load result, som_prev_mined_blocksonly ever holds a validated window. For mined-block notifications that arrive during startup (before the load completes) we can just ignore them rather than buffer and replay: they fire duringActivateBestChain(), beforeLoadMempool(), so the mempool is likely empty when they connect and they may record ~0 coverage, which is an artifact rather than a real signal, and buffering them would only fold that noise into the window. Possible follow-up (not this PR): if we want accurate coverage for the case where a node was down long enough for blocks to pile up, we could remember the last connected blocks' txids and, as the mempool loads, credit the weight of any snapshot tx that was mined in one of them. That reconstructs real coverage for those blocks and lets the retention numerator count the mined snapshot txs, but it adds node/policy plumbing for a fairly narrow case. -
For the API, should we pass the
MempoolLoadResult (util::Expected)directly into the load completed call instead of unpacking it into boolload_succeededandsnapshot_weightargs? The estimator can then measure the post-load weight itself viaGetTotalTxWeight()?. -
We should also reshape it so FeeRateEstimatorManager receives the mempool-load-completed notification and then tells MemPoolFeeRateEstimator to read the persisted mined-block stats after that. The manager owns the event; the mempool estimator owns the read.
musaHaruna
left a comment
There was a problem hiding this comment.
Approach ACK
For context, after the original mempool-based fee estimator PR was merged, I continued running experiments. That was how I also noticed similar problem and reproduced the mismatch documented in here.
This PR addresses the underlying problem comprehensively, including the scenario covered by my experimental test, by validating persisted estimator health against the mempool-loading outcome and post-load mempool weight.
I did a light pass through the code and noticed a few things:
- Could we call
assert_mempool_estimator_unavailable()again after the finalself.restart_node(0)in the expired-transactions case? The existing assertion verifies immediate in-memory invalidation, while the post-restart assertion would verify end-to-end that the cleared state survives shutdown and reload and that the old healthy window is not resurrected:
assert_mempool_estimator_unavailable()
self.restart_node(0)
assert_mempool_estimator_unavailable()- Could we also update the
-persistmempoolhelp text? The option now controls reading and writing the related mempool-policy estimator data, not onlymempool.dat. For example:
"Whether to save and load the mempool and persist related mempool-policy estimator data across restarts"
- Could snapshot_weight be represented as
std::optional<uint64_t>instead of usingbool load_succeededtogether withuint64_t snapshot_weight? The states would then be:
std::nullopt = load failed
std::optional<uint64_t>{0} = successfully loaded an empty snapshot
positive value = successfully loaded a nonempty snapshot
This would make contradictory states such as (false, 4) unrepresentable. The current production caller cannot generate that combination, although the interface permits it and the unit test constructs it deliberately.
The interface could look like:
void MempoolLoadCompleted(
std::optional<uint64_t> snapshot_weight,
uint64_t post_load_mempool_weight);The caller could translate the load result as follows:
std::optional<uint64_t> snapshot_weight;
if (load_result.has_value()) {
snapshot_weight = load_result.value();
}
node.fee_estimator_man->MempoolLoadCompleted(
snapshot_weight,
post_load_mempool_weight);The failed-load unit-test case would then become:
estimator.MempoolLoadCompleted(
/*snapshot_weight=*/std::nullopt,
/*post_load_mempool_weight=*/4);| uint64_t post_load_mempool_weight) | ||
| { | ||
| const double retention_ratio{snapshot_weight == 0 | ||
| ? (load_succeeded ? 1.0 : 0.0) |
There was a problem hiding this comment.
nit: nested ternary operators are hard to read
| m_cache.Clear(); | ||
| } | ||
|
|
||
| void MemPoolFeeRateEstimator::MempoolLoadCompleted(bool load_succeeded, |
There was a problem hiding this comment.
I think it would make more sense to make this a MempoolRestored function that is only called when the mempool is successfully loaded, checks the retention ratio, and then Reads if successful.
Return a util::Expected result from LoadMempool() containing either a typed load error or the total transaction weight encoded in the snapshot. Co-authored-by: ismaelsadeeq <[email protected]> Co-authored-by: Robert Netzke <[email protected]>
Use an empty mempool policy estimator path when mempool persistence is disabled. Make the estimator read and flush paths no-op for an empty path, while leaving periodic and shutdown flush scheduling unchanged. Co-authored-by: ismaelsadeeq <[email protected]> Co-authored-by: Robert Netzke <[email protected]>
66df06f to
52f77b6
Compare
Persisted mempool policy estimator data is only representative when startup leaves the node with a sufficiently populated mempool. Discard it when mempool.dat cannot be loaded or post-load mempool weight does not meet the snapshot retention threshold. Use final mempool weight as a deliberately permissive proxy. Transactions added independently before measurement may contribute, biasing the decision toward retaining potentially usable history without measuring exact snapshot overlap. Defer reading until mempool loading completes. Ignore pre-completion block notifications and suppress estimator flushes until then, preserving the persisted data if loading is interrupted. Add unit and functional tests for the retention threshold, load failure, partial expiry, missing or expired snapshots, and disabled persistence. Co-authored-by: ismaelsadeeq <[email protected]> Co-authored-by: Robert Netzke <[email protected]> Co-authored-by: Musa Haruna <[email protected]> Co-authored-by: stickies-v <[email protected]>
Log the typed load error when persisted mempool-policy estimator statistics cannot be restored. Keep NO_LOAD_PATH silent because it is the expected result when mempool persistence is disabled.
52f77b6 to
6314c03
Compare
|
🚧 At least one of the CI tasks failed. HintsTry to run the tests locally, according to the documentation. However, a CI failure may still
Leave a comment here, if you need help tracking down a confusing failure. |
|
Update since the previous push:
New co-authors added: Musa Haruna (@musaHaruna) and @stickies-v. |
I agree with this approach, I think we need to narrow it down for the milestone and only include the most important, most straightforward fixes. I think that would be just e9d7ec1?, and perhaps also checking the mempool load result (although I don't think that's a frequent occurrence?). The rest can be improved in a follow-up for the next release? I'm not yet sure what the retention threshold is used for beyond the mechanism we already have where new blocks give us a useful indication of mempool health? I think this fully covers the scenario of a clean, extended shutdown? |
|
Updated my previous comment to clarify what has changed since the last push and why.
The retention check runs once during startup, before restoring the saved recent-block statistics (
The 75% value is borrowed from
Happy to narrow this PR to e9d7ec1 for 32.0 and move the other changes to a follow-up if that is the preference here. |
|
Yep, we should keep this narrowed down. The minimal fix in e9d7ec1 should not discard the persisted mined-block stats completely, because they are still useful. Waiting another 6 blocks ~1 hr is unnecessary IMO. A better minimal fix when mempool fail to load or don't persist could be:
I also noticed a separate issue when blocks connect while the mempool is still loading. If After that, I think it is reasonable to assume most persisted transactions return on a clean load. Note: the mempool rebroadcast work in #21061 would have sped up the warm-up stage in these scenarios. |
|
See #36182 for another approach |
The mempool policy estimator reloads its mined-block statistics separately from
mempool.dat.Currently, those statistics are retained even when mempool persistence is disabled,
mempool.datis missing, or most snapshot transactions are not restored.This could make an empty or unrepresentative mempool appear healthy and produce misleading fee estimates.
This PR tracks the snapshot’s total and restored transaction weights during loading.
Persisted estimator data is retained only when loading succeeds and the restored weight meets the existing mempool representation threshold.
The data is preserved when startup is interrupted, and is neither read nor written when
-persistmempool=0.Tests cover partial restoration, disabled persistence, a missing snapshot, and expired snapshot transactions.