aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorismaelsadeeq <ask4ismailsadiq@gmail.com>2023-06-08 13:14:11 +0100
committerfanquake <fanquake@gmail.com>2023-09-26 16:26:55 +0100
commit77979e0172d0bc86bfbc60f6a652e26c77722e29 (patch)
tree3abf15ae12cb092f518925c65da08aae6be96d09
parent67b6d99aead0d1b2030bc3e88256d279477894b5 (diff)
tx fees, policy: periodically flush fee estimates to fee_estimates.dat
This reduces chances of having old estimates in fee_estimates.dat. Github-Pull: #27622 Rebased-From: 5b886f2b436eaa8c2b7de58dc4644dc6223040da
-rw-r--r--src/init.cpp8
-rw-r--r--src/policy/fees.cpp6
-rw-r--r--src/policy/fees.h9
3 files changed, 22 insertions, 1 deletions
diff --git a/src/init.cpp b/src/init.cpp
index 25b40c6c6e..7fcd1fee2f 100644
--- a/src/init.cpp
+++ b/src/init.cpp
@@ -1253,7 +1253,13 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
assert(!node.fee_estimator);
// Don't initialize fee estimation with old data if we don't relay transactions,
// as they would never get updated.
- if (!ignores_incoming_txs) node.fee_estimator = std::make_unique<CBlockPolicyEstimator>(FeeestPath(args));
+ if (!ignores_incoming_txs) {
+ node.fee_estimator = std::make_unique<CBlockPolicyEstimator>(FeeestPath(args));
+
+ // Flush estimates to disk periodically
+ CBlockPolicyEstimator* fee_estimator = node.fee_estimator.get();
+ node.scheduler->scheduleEvery([fee_estimator] { fee_estimator->FlushFeeEstimates(); }, FEE_FLUSH_INTERVAL);
+ }
// sanitize comments per BIP-0014, format user agent and check total size
std::vector<std::string> uacomments;
diff --git a/src/policy/fees.cpp b/src/policy/fees.cpp
index 2b940be07e..6b8d52b2c5 100644
--- a/src/policy/fees.cpp
+++ b/src/policy/fees.cpp
@@ -903,10 +903,16 @@ CFeeRate CBlockPolicyEstimator::estimateSmartFee(int confTarget, FeeCalculation
void CBlockPolicyEstimator::Flush() {
FlushUnconfirmed();
+ FlushFeeEstimates();
+}
+void CBlockPolicyEstimator::FlushFeeEstimates()
+{
AutoFile est_file{fsbridge::fopen(m_estimation_filepath, "wb")};
if (est_file.IsNull() || !Write(est_file)) {
LogPrintf("Failed to write fee estimates to %s. Continue anyway.\n", fs::PathToString(m_estimation_filepath));
+ } else {
+ LogPrintf("Flushed fee estimates to %s.\n", fs::PathToString(m_estimation_filepath.filename()));
}
}
diff --git a/src/policy/fees.h b/src/policy/fees.h
index e4628bf853..cb0bf0563e 100644
--- a/src/policy/fees.h
+++ b/src/policy/fees.h
@@ -14,12 +14,17 @@
#include <uint256.h>
#include <array>
+#include <chrono>
#include <map>
#include <memory>
#include <set>
#include <string>
#include <vector>
+
+// How often to flush fee estimates to fee_estimates.dat.
+static constexpr std::chrono::hours FEE_FLUSH_INTERVAL{1};
+
class AutoFile;
class CTxMemPoolEntry;
class TxConfirmStats;
@@ -239,6 +244,10 @@ public:
void Flush()
EXCLUSIVE_LOCKS_REQUIRED(!m_cs_fee_estimator);
+ /** Record current fee estimations. */
+ void FlushFeeEstimates()
+ EXCLUSIVE_LOCKS_REQUIRED(!m_cs_fee_estimator);
+
private:
mutable Mutex m_cs_fee_estimator;