Skip to content

Commit c4dd598

Browse files
ismaelsadeeqfanquake
authored andcommitted
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: 5b886f2
1 parent c36770c commit c4dd598

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
@@ -1251,7 +1251,13 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
12511251
assert(!node.fee_estimator);
12521252
// Don't initialize fee estimation with old data if we don't relay transactions,
12531253
// as they would never get updated.
1254-
if (!ignores_incoming_txs) node.fee_estimator = std::make_unique<CBlockPolicyEstimator>(FeeestPath(args));
1254+
if (!ignores_incoming_txs) {
1255+
node.fee_estimator = std::make_unique<CBlockPolicyEstimator>(FeeestPath(args));
1256+
1257+
// Flush estimates to disk periodically
1258+
CBlockPolicyEstimator* fee_estimator = node.fee_estimator.get();
1259+
node.scheduler->scheduleEvery([fee_estimator] { fee_estimator->FlushFeeEstimates(); }, FEE_FLUSH_INTERVAL);
1260+
}
12551261

12561262
// Check port numbers
12571263
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)