mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-01-26 11:13:23 -03:00
feestimator: encapsulate estimation file logic
This moves the fee_estimates file management to the CBlockPolicyEstimator Flush() method. Co-authored-by: John Newbery <john@johnnewbery.com> Signed-off-by: Antoine Poinsot <darosior@protonmail.com>
This commit is contained in:
parent
e8ea6ad9c1
commit
4e28753f60
3 changed files with 24 additions and 19 deletions
21
src/init.cpp
21
src/init.cpp
|
@ -98,8 +98,6 @@ static const bool DEFAULT_STOPAFTERBLOCKIMPORT = false;
|
||||||
#define MIN_CORE_FILEDESCRIPTORS 150
|
#define MIN_CORE_FILEDESCRIPTORS 150
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
static const char* FEE_ESTIMATES_FILENAME="fee_estimates.dat";
|
|
||||||
|
|
||||||
static const char* DEFAULT_ASMAP_FILENAME="ip_asn.map";
|
static const char* DEFAULT_ASMAP_FILENAME="ip_asn.map";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -235,17 +233,8 @@ void Shutdown(NodeContext& node)
|
||||||
DumpMempool(*node.mempool);
|
DumpMempool(*node.mempool);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (node.fee_estimator) {
|
// Drop transactions we were still watching, and record fee estimations.
|
||||||
node.fee_estimator->FlushUnconfirmed();
|
if (node.fee_estimator) node.fee_estimator->Flush();
|
||||||
fs::path est_path = GetDataDir() / FEE_ESTIMATES_FILENAME;
|
|
||||||
CAutoFile est_fileout(fsbridge::fopen(est_path, "wb"), SER_DISK, CLIENT_VERSION);
|
|
||||||
if (!est_fileout.IsNull()) {
|
|
||||||
node.fee_estimator->Write(est_fileout);
|
|
||||||
} else {
|
|
||||||
LogPrintf("%s: Failed to write fee estimates to %s\n", __func__, est_path.string());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// FlushStateToDisk generates a ChainStateFlushed callback, which we should avoid missing
|
// FlushStateToDisk generates a ChainStateFlushed callback, which we should avoid missing
|
||||||
if (node.chainman) {
|
if (node.chainman) {
|
||||||
|
@ -1790,12 +1779,6 @@ bool AppInitMain(const util::Ref& context, NodeContext& node, interfaces::BlockA
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
fs::path est_path = GetDataDir() / FEE_ESTIMATES_FILENAME;
|
|
||||||
CAutoFile est_filein(fsbridge::fopen(est_path, "rb"), SER_DISK, CLIENT_VERSION);
|
|
||||||
// Allowed to fail as this file IS missing on first startup.
|
|
||||||
if (node.fee_estimator && !est_filein.IsNull()) {
|
|
||||||
node.fee_estimator->Read(est_filein);
|
|
||||||
}
|
|
||||||
// ********************************************************* Step 8: start indexers
|
// ********************************************************* Step 8: start indexers
|
||||||
if (args.GetBoolArg("-txindex", DEFAULT_TXINDEX)) {
|
if (args.GetBoolArg("-txindex", DEFAULT_TXINDEX)) {
|
||||||
g_txindex = MakeUnique<TxIndex>(nTxIndexCache, false, fReindex);
|
g_txindex = MakeUnique<TxIndex>(nTxIndexCache, false, fReindex);
|
||||||
|
|
|
@ -10,6 +10,8 @@
|
||||||
#include <txmempool.h>
|
#include <txmempool.h>
|
||||||
#include <util/system.h>
|
#include <util/system.h>
|
||||||
|
|
||||||
|
static const char* FEE_ESTIMATES_FILENAME="fee_estimates.dat";
|
||||||
|
|
||||||
static constexpr double INF_FEERATE = 1e99;
|
static constexpr double INF_FEERATE = 1e99;
|
||||||
|
|
||||||
std::string StringForFeeEstimateHorizon(FeeEstimateHorizon horizon) {
|
std::string StringForFeeEstimateHorizon(FeeEstimateHorizon horizon) {
|
||||||
|
@ -489,6 +491,7 @@ CBlockPolicyEstimator::CBlockPolicyEstimator()
|
||||||
{
|
{
|
||||||
static_assert(MIN_BUCKET_FEERATE > 0, "Min feerate must be nonzero");
|
static_assert(MIN_BUCKET_FEERATE > 0, "Min feerate must be nonzero");
|
||||||
size_t bucketIndex = 0;
|
size_t bucketIndex = 0;
|
||||||
|
|
||||||
for (double bucketBoundary = MIN_BUCKET_FEERATE; bucketBoundary <= MAX_BUCKET_FEERATE; bucketBoundary *= FEE_SPACING, bucketIndex++) {
|
for (double bucketBoundary = MIN_BUCKET_FEERATE; bucketBoundary <= MAX_BUCKET_FEERATE; bucketBoundary *= FEE_SPACING, bucketIndex++) {
|
||||||
buckets.push_back(bucketBoundary);
|
buckets.push_back(bucketBoundary);
|
||||||
bucketMap[bucketBoundary] = bucketIndex;
|
bucketMap[bucketBoundary] = bucketIndex;
|
||||||
|
@ -500,6 +503,13 @@ CBlockPolicyEstimator::CBlockPolicyEstimator()
|
||||||
feeStats = std::unique_ptr<TxConfirmStats>(new TxConfirmStats(buckets, bucketMap, MED_BLOCK_PERIODS, MED_DECAY, MED_SCALE));
|
feeStats = std::unique_ptr<TxConfirmStats>(new TxConfirmStats(buckets, bucketMap, MED_BLOCK_PERIODS, MED_DECAY, MED_SCALE));
|
||||||
shortStats = std::unique_ptr<TxConfirmStats>(new TxConfirmStats(buckets, bucketMap, SHORT_BLOCK_PERIODS, SHORT_DECAY, SHORT_SCALE));
|
shortStats = std::unique_ptr<TxConfirmStats>(new TxConfirmStats(buckets, bucketMap, SHORT_BLOCK_PERIODS, SHORT_DECAY, SHORT_SCALE));
|
||||||
longStats = std::unique_ptr<TxConfirmStats>(new TxConfirmStats(buckets, bucketMap, LONG_BLOCK_PERIODS, LONG_DECAY, LONG_SCALE));
|
longStats = std::unique_ptr<TxConfirmStats>(new TxConfirmStats(buckets, bucketMap, LONG_BLOCK_PERIODS, LONG_DECAY, LONG_SCALE));
|
||||||
|
|
||||||
|
// If the fee estimation file is present, read recorded estimations
|
||||||
|
fs::path est_filepath = GetDataDir() / FEE_ESTIMATES_FILENAME;
|
||||||
|
CAutoFile est_file(fsbridge::fopen(est_filepath, "rb"), SER_DISK, CLIENT_VERSION);
|
||||||
|
if (est_file.IsNull() || !Read(est_file)) {
|
||||||
|
LogPrintf("Failed to read fee estimates from %s. Continue anyway.\n", est_filepath.string());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
CBlockPolicyEstimator::~CBlockPolicyEstimator()
|
CBlockPolicyEstimator::~CBlockPolicyEstimator()
|
||||||
|
@ -856,6 +866,15 @@ CFeeRate CBlockPolicyEstimator::estimateSmartFee(int confTarget, FeeCalculation
|
||||||
return CFeeRate(llround(median));
|
return CFeeRate(llround(median));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void CBlockPolicyEstimator::Flush() {
|
||||||
|
FlushUnconfirmed();
|
||||||
|
|
||||||
|
fs::path est_filepath = GetDataDir() / FEE_ESTIMATES_FILENAME;
|
||||||
|
CAutoFile est_file(fsbridge::fopen(est_filepath, "wb"), SER_DISK, CLIENT_VERSION);
|
||||||
|
if (est_file.IsNull() || !Write(est_file)) {
|
||||||
|
LogPrintf("Failed to write fee estimates to %s\n", est_filepath.string());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
bool CBlockPolicyEstimator::Write(CAutoFile& fileout) const
|
bool CBlockPolicyEstimator::Write(CAutoFile& fileout) const
|
||||||
{
|
{
|
||||||
|
|
|
@ -215,6 +215,9 @@ public:
|
||||||
/** Calculation of highest target that estimates are tracked for */
|
/** Calculation of highest target that estimates are tracked for */
|
||||||
unsigned int HighestTargetTracked(FeeEstimateHorizon horizon) const;
|
unsigned int HighestTargetTracked(FeeEstimateHorizon horizon) const;
|
||||||
|
|
||||||
|
/** Drop still unconfirmed transactions and record current estimations, if the fee estimation file is present. */
|
||||||
|
void Flush();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
mutable RecursiveMutex m_cs_fee_estimator;
|
mutable RecursiveMutex m_cs_fee_estimator;
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue