Skip to content

Commit 4e28753

Browse files
darosiorjnewbery
andcommitted
feestimator: encapsulate estimation file logic
This moves the fee_estimates file management to the CBlockPolicyEstimator Flush() method. Co-authored-by: John Newbery <[email protected]> Signed-off-by: Antoine Poinsot <[email protected]>
1 parent e8ea6ad commit 4e28753

File tree

3 files changed

+24
-19
lines changed

3 files changed

+24
-19
lines changed

src/init.cpp

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,6 @@ static const bool DEFAULT_STOPAFTERBLOCKIMPORT = false;
9898
#define MIN_CORE_FILEDESCRIPTORS 150
9999
#endif
100100

101-
static const char* FEE_ESTIMATES_FILENAME="fee_estimates.dat";
102-
103101
static const char* DEFAULT_ASMAP_FILENAME="ip_asn.map";
104102

105103
/**
@@ -235,17 +233,8 @@ void Shutdown(NodeContext& node)
235233
DumpMempool(*node.mempool);
236234
}
237235

238-
if (node.fee_estimator) {
239-
node.fee_estimator->FlushUnconfirmed();
240-
fs::path est_path = GetDataDir() / FEE_ESTIMATES_FILENAME;
241-
CAutoFile est_fileout(fsbridge::fopen(est_path, "wb"), SER_DISK, CLIENT_VERSION);
242-
if (!est_fileout.IsNull()) {
243-
node.fee_estimator->Write(est_fileout);
244-
} else {
245-
LogPrintf("%s: Failed to write fee estimates to %s\n", __func__, est_path.string());
246-
}
247-
}
248-
236+
// Drop transactions we were still watching, and record fee estimations.
237+
if (node.fee_estimator) node.fee_estimator->Flush();
249238

250239
// FlushStateToDisk generates a ChainStateFlushed callback, which we should avoid missing
251240
if (node.chainman) {
@@ -1790,12 +1779,6 @@ bool AppInitMain(const util::Ref& context, NodeContext& node, interfaces::BlockA
17901779
return false;
17911780
}
17921781

1793-
fs::path est_path = GetDataDir() / FEE_ESTIMATES_FILENAME;
1794-
CAutoFile est_filein(fsbridge::fopen(est_path, "rb"), SER_DISK, CLIENT_VERSION);
1795-
// Allowed to fail as this file IS missing on first startup.
1796-
if (node.fee_estimator && !est_filein.IsNull()) {
1797-
node.fee_estimator->Read(est_filein);
1798-
}
17991782
// ********************************************************* Step 8: start indexers
18001783
if (args.GetBoolArg("-txindex", DEFAULT_TXINDEX)) {
18011784
g_txindex = MakeUnique<TxIndex>(nTxIndexCache, false, fReindex);

src/policy/fees.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
#include <txmempool.h>
1111
#include <util/system.h>
1212

13+
static const char* FEE_ESTIMATES_FILENAME="fee_estimates.dat";
14+
1315
static constexpr double INF_FEERATE = 1e99;
1416

1517
std::string StringForFeeEstimateHorizon(FeeEstimateHorizon horizon) {
@@ -489,6 +491,7 @@ CBlockPolicyEstimator::CBlockPolicyEstimator()
489491
{
490492
static_assert(MIN_BUCKET_FEERATE > 0, "Min feerate must be nonzero");
491493
size_t bucketIndex = 0;
494+
492495
for (double bucketBoundary = MIN_BUCKET_FEERATE; bucketBoundary <= MAX_BUCKET_FEERATE; bucketBoundary *= FEE_SPACING, bucketIndex++) {
493496
buckets.push_back(bucketBoundary);
494497
bucketMap[bucketBoundary] = bucketIndex;
@@ -500,6 +503,13 @@ CBlockPolicyEstimator::CBlockPolicyEstimator()
500503
feeStats = std::unique_ptr<TxConfirmStats>(new TxConfirmStats(buckets, bucketMap, MED_BLOCK_PERIODS, MED_DECAY, MED_SCALE));
501504
shortStats = std::unique_ptr<TxConfirmStats>(new TxConfirmStats(buckets, bucketMap, SHORT_BLOCK_PERIODS, SHORT_DECAY, SHORT_SCALE));
502505
longStats = std::unique_ptr<TxConfirmStats>(new TxConfirmStats(buckets, bucketMap, LONG_BLOCK_PERIODS, LONG_DECAY, LONG_SCALE));
506+
507+
// If the fee estimation file is present, read recorded estimations
508+
fs::path est_filepath = GetDataDir() / FEE_ESTIMATES_FILENAME;
509+
CAutoFile est_file(fsbridge::fopen(est_filepath, "rb"), SER_DISK, CLIENT_VERSION);
510+
if (est_file.IsNull() || !Read(est_file)) {
511+
LogPrintf("Failed to read fee estimates from %s. Continue anyway.\n", est_filepath.string());
512+
}
503513
}
504514

505515
CBlockPolicyEstimator::~CBlockPolicyEstimator()
@@ -856,6 +866,15 @@ CFeeRate CBlockPolicyEstimator::estimateSmartFee(int confTarget, FeeCalculation
856866
return CFeeRate(llround(median));
857867
}
858868

869+
void CBlockPolicyEstimator::Flush() {
870+
FlushUnconfirmed();
871+
872+
fs::path est_filepath = GetDataDir() / FEE_ESTIMATES_FILENAME;
873+
CAutoFile est_file(fsbridge::fopen(est_filepath, "wb"), SER_DISK, CLIENT_VERSION);
874+
if (est_file.IsNull() || !Write(est_file)) {
875+
LogPrintf("Failed to write fee estimates to %s\n", est_filepath.string());
876+
}
877+
}
859878

860879
bool CBlockPolicyEstimator::Write(CAutoFile& fileout) const
861880
{

src/policy/fees.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,9 @@ class CBlockPolicyEstimator
215215
/** Calculation of highest target that estimates are tracked for */
216216
unsigned int HighestTargetTracked(FeeEstimateHorizon horizon) const;
217217

218+
/** Drop still unconfirmed transactions and record current estimations, if the fee estimation file is present. */
219+
void Flush();
220+
218221
private:
219222
mutable RecursiveMutex m_cs_fee_estimator;
220223

0 commit comments

Comments
 (0)