Skip to content

Commit 5b886f2

Browse files
committed
tx fees, policy: periodically flush fee estimates to fee_estimates.dat
This reduces chances of having old estimates in fee_estimates.dat.
1 parent 681ecac commit 5b886f2

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

src/init.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1253,7 +1253,13 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
12531253
assert(!node.fee_estimator);
12541254
// Don't initialize fee estimation with old data if we don't relay transactions,
12551255
// as they would never get updated.
1256-
if (!ignores_incoming_txs) node.fee_estimator = std::make_unique<CBlockPolicyEstimator>(FeeestPath(args));
1256+
if (!ignores_incoming_txs) {
1257+
node.fee_estimator = std::make_unique<CBlockPolicyEstimator>(FeeestPath(args));
1258+
1259+
// Flush estimates to disk periodically
1260+
CBlockPolicyEstimator* fee_estimator = node.fee_estimator.get();
1261+
node.scheduler->scheduleEvery([fee_estimator] { fee_estimator->FlushFeeEstimates(); }, FEE_FLUSH_INTERVAL);
1262+
}
12571263

12581264
// Check port numbers
12591265
for (const std::string port_option : {

src/policy/fees.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -903,10 +903,16 @@ CFeeRate CBlockPolicyEstimator::estimateSmartFee(int confTarget, FeeCalculation
903903

904904
void CBlockPolicyEstimator::Flush() {
905905
FlushUnconfirmed();
906+
FlushFeeEstimates();
907+
}
906908

909+
void CBlockPolicyEstimator::FlushFeeEstimates()
910+
{
907911
AutoFile est_file{fsbridge::fopen(m_estimation_filepath, "wb")};
908912
if (est_file.IsNull() || !Write(est_file)) {
909913
LogPrintf("Failed to write fee estimates to %s. Continue anyway.\n", fs::PathToString(m_estimation_filepath));
914+
} else {
915+
LogPrintf("Flushed fee estimates to %s.\n", fs::PathToString(m_estimation_filepath.filename()));
910916
}
911917
}
912918

src/policy/fees.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,17 @@
1414
#include <util/fs.h>
1515

1616
#include <array>
17+
#include <chrono>
1718
#include <map>
1819
#include <memory>
1920
#include <set>
2021
#include <string>
2122
#include <vector>
2223

24+
25+
// How often to flush fee estimates to fee_estimates.dat.
26+
static constexpr std::chrono::hours FEE_FLUSH_INTERVAL{1};
27+
2328
class AutoFile;
2429
class CTxMemPoolEntry;
2530
class TxConfirmStats;
@@ -239,6 +244,10 @@ class CBlockPolicyEstimator
239244
void Flush()
240245
EXCLUSIVE_LOCKS_REQUIRED(!m_cs_fee_estimator);
241246

247+
/** Record current fee estimations. */
248+
void FlushFeeEstimates()
249+
EXCLUSIVE_LOCKS_REQUIRED(!m_cs_fee_estimator);
250+
242251
private:
243252
mutable Mutex m_cs_fee_estimator;
244253

0 commit comments

Comments
 (0)