Skip to content

Commit 5ba81e5

Browse files
committed
Read and Write fee estimate file directly from CBlockPolicyEstimator
1 parent 14e10aa commit 5ba81e5

File tree

5 files changed

+38
-55
lines changed

5 files changed

+38
-55
lines changed

src/init.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "netbase.h"
2626
#include "net.h"
2727
#include "net_processing.h"
28+
#include "policy/fees.h"
2829
#include "policy/policy.h"
2930
#include "rpc/server.h"
3031
#include "rpc/register.h"
@@ -215,7 +216,7 @@ void Shutdown()
215216
fs::path est_path = GetDataDir() / FEE_ESTIMATES_FILENAME;
216217
CAutoFile est_fileout(fsbridge::fopen(est_path, "wb"), SER_DISK, CLIENT_VERSION);
217218
if (!est_fileout.IsNull())
218-
mempool.WriteFeeEstimates(est_fileout);
219+
::feeEstimator.Write(est_fileout);
219220
else
220221
LogPrintf("%s: Failed to write fee estimates to %s\n", __func__, est_path.string());
221222
fFeeEstimatesInitialized = false;
@@ -1550,7 +1551,7 @@ bool AppInitMain(boost::thread_group& threadGroup, CScheduler& scheduler)
15501551
CAutoFile est_filein(fsbridge::fopen(est_path, "rb"), SER_DISK, CLIENT_VERSION);
15511552
// Allowed to fail as this file IS missing on first startup.
15521553
if (!est_filein.IsNull())
1553-
mempool.ReadFeeEstimates(est_filein);
1554+
::feeEstimator.Read(est_filein);
15541555
fFeeEstimatesInitialized = true;
15551556

15561557
// ********************************************************* Step 8: load wallet

src/policy/fees.cpp

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "policy/policy.h"
88

99
#include "amount.h"
10+
#include "clientversion.h"
1011
#include "primitives/transaction.h"
1112
#include "random.h"
1213
#include "streams.h"
@@ -173,7 +174,7 @@ double TxConfirmStats::EstimateMedianVal(int confTarget, double sufficientTxVal,
173174
return median;
174175
}
175176

176-
void TxConfirmStats::Write(CAutoFile& fileout)
177+
void TxConfirmStats::Write(CAutoFile& fileout) const
177178
{
178179
fileout << decay;
179180
fileout << buckets;
@@ -464,21 +465,40 @@ CFeeRate CBlockPolicyEstimator::estimateSmartFee(int confTarget, int *answerFoun
464465
return CFeeRate(median);
465466
}
466467

467-
void CBlockPolicyEstimator::Write(CAutoFile& fileout)
468+
bool CBlockPolicyEstimator::Write(CAutoFile& fileout) const
468469
{
469-
LOCK(cs_feeEstimator);
470-
fileout << nBestSeenHeight;
471-
feeStats.Write(fileout);
470+
try {
471+
LOCK(cs_feeEstimator);
472+
fileout << 139900; // version required to read: 0.13.99 or later
473+
fileout << CLIENT_VERSION; // version that wrote the file
474+
fileout << nBestSeenHeight;
475+
feeStats.Write(fileout);
476+
}
477+
catch (const std::exception&) {
478+
LogPrintf("CBlockPolicyEstimator::Write(): unable to read policy estimator data (non-fatal)\n");
479+
return false;
480+
}
481+
return true;
472482
}
473483

474-
void CBlockPolicyEstimator::Read(CAutoFile& filein, int nFileVersion)
484+
bool CBlockPolicyEstimator::Read(CAutoFile& filein)
475485
{
476-
LOCK(cs_feeEstimator);
477-
int nFileBestSeenHeight;
478-
filein >> nFileBestSeenHeight;
479-
feeStats.Read(filein);
480-
nBestSeenHeight = nFileBestSeenHeight;
481-
// if nVersionThatWrote < 139900 then another TxConfirmStats (for priority) follows but can be ignored.
486+
try {
487+
LOCK(cs_feeEstimator);
488+
int nVersionRequired, nVersionThatWrote, nFileBestSeenHeight;
489+
filein >> nVersionRequired >> nVersionThatWrote;
490+
if (nVersionRequired > CLIENT_VERSION)
491+
return error("CBlockPolicyEstimator::Read(): up-version (%d) fee estimate file", nVersionRequired);
492+
filein >> nFileBestSeenHeight;
493+
feeStats.Read(filein);
494+
nBestSeenHeight = nFileBestSeenHeight;
495+
// if nVersionThatWrote < 139900 then another TxConfirmStats (for priority) follows but can be ignored.
496+
}
497+
catch (const std::exception&) {
498+
LogPrintf("CBlockPolicyEstimator::Read(): unable to read policy estimator data (non-fatal)\n");
499+
return false;
500+
}
501+
return true;
482502
}
483503

484504
FeeFilterRounder::FeeFilterRounder(const CFeeRate& minIncrementalFee)

src/policy/fees.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ class TxConfirmStats
156156
unsigned int GetMaxConfirms() const { return confAvg.size(); }
157157

158158
/** Write state of estimation data to a file*/
159-
void Write(CAutoFile& fileout);
159+
void Write(CAutoFile& fileout) const;
160160

161161
/**
162162
* Read saved state of estimation data from a file and replace all internal data structures and
@@ -226,10 +226,10 @@ class CBlockPolicyEstimator
226226
CFeeRate estimateSmartFee(int confTarget, int *answerFoundAtTarget, const CTxMemPool& pool) const;
227227

228228
/** Write estimation data to a file */
229-
void Write(CAutoFile& fileout);
229+
bool Write(CAutoFile& fileout) const;
230230

231231
/** Read estimation data from a file */
232-
void Read(CAutoFile& filein, int nFileVersion);
232+
bool Read(CAutoFile& filein);
233233

234234
private:
235235
CFeeRate minTrackedFee; //!< Passed to constructor to avoid dependency on main

src/txmempool.cpp

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
#include "txmempool.h"
77

8-
#include "clientversion.h"
98
#include "consensus/consensus.h"
109
#include "consensus/validation.h"
1110
#include "validation.h"
@@ -16,7 +15,6 @@
1615
#include "util.h"
1716
#include "utilmoneystr.h"
1817
#include "utiltime.h"
19-
#include "version.h"
2018

2119
CTxMemPoolEntry::CTxMemPoolEntry(const CTransactionRef& _tx, const CAmount& _nFee,
2220
int64_t _nTime, unsigned int _entryHeight,
@@ -843,38 +841,6 @@ TxMempoolInfo CTxMemPool::info(const uint256& hash) const
843841
return GetInfo(i);
844842
}
845843

846-
bool
847-
CTxMemPool::WriteFeeEstimates(CAutoFile& fileout) const
848-
{
849-
try {
850-
fileout << 139900; // version required to read: 0.13.99 or later
851-
fileout << CLIENT_VERSION; // version that wrote the file
852-
minerPolicyEstimator->Write(fileout);
853-
}
854-
catch (const std::exception&) {
855-
LogPrintf("CTxMemPool::WriteFeeEstimates(): unable to write policy estimator data (non-fatal)\n");
856-
return false;
857-
}
858-
return true;
859-
}
860-
861-
bool
862-
CTxMemPool::ReadFeeEstimates(CAutoFile& filein)
863-
{
864-
try {
865-
int nVersionRequired, nVersionThatWrote;
866-
filein >> nVersionRequired >> nVersionThatWrote;
867-
if (nVersionRequired > CLIENT_VERSION)
868-
return error("CTxMemPool::ReadFeeEstimates(): up-version (%d) fee estimate file", nVersionRequired);
869-
minerPolicyEstimator->Read(filein, nVersionThatWrote);
870-
}
871-
catch (const std::exception&) {
872-
LogPrintf("CTxMemPool::ReadFeeEstimates(): unable to read policy estimator data (non-fatal)\n");
873-
return false;
874-
}
875-
return true;
876-
}
877-
878844
void CTxMemPool::PrioritiseTransaction(const uint256& hash, const CAmount& nFeeDelta)
879845
{
880846
{

src/txmempool.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -617,10 +617,6 @@ class CTxMemPool
617617
TxMempoolInfo info(const uint256& hash) const;
618618
std::vector<TxMempoolInfo> infoAll() const;
619619

620-
/** Write/Read estimates to disk */
621-
bool WriteFeeEstimates(CAutoFile& fileout) const;
622-
bool ReadFeeEstimates(CAutoFile& filein);
623-
624620
size_t DynamicMemoryUsage() const;
625621

626622
boost::signals2::signal<void (CTransactionRef)> NotifyEntryAdded;

0 commit comments

Comments
 (0)