|
24 | 24 |
|
25 | 25 | #include <algorithm> |
26 | 26 | #include <cassert> |
| 27 | +#include <chrono> |
27 | 28 | #include <cmath> |
28 | 29 | #include <cstddef> |
29 | 30 | #include <cstdint> |
@@ -545,9 +546,22 @@ CBlockPolicyEstimator::CBlockPolicyEstimator(const fs::path& estimation_filepath |
545 | 546 | shortStats = std::unique_ptr<TxConfirmStats>(new TxConfirmStats(buckets, bucketMap, SHORT_BLOCK_PERIODS, SHORT_DECAY, SHORT_SCALE)); |
546 | 547 | longStats = std::unique_ptr<TxConfirmStats>(new TxConfirmStats(buckets, bucketMap, LONG_BLOCK_PERIODS, LONG_DECAY, LONG_SCALE)); |
547 | 548 |
|
548 | | - // If the fee estimation file is present, read recorded estimations |
549 | 549 | AutoFile est_file{fsbridge::fopen(m_estimation_filepath, "rb")}; |
550 | | - if (est_file.IsNull() || !Read(est_file)) { |
| 550 | + |
| 551 | + // Whenever the fee estimation file is not present return early |
| 552 | + if (est_file.IsNull()) { |
| 553 | + LogPrintf("%s is not found. Continue anyway.\n", fs::PathToString(m_estimation_filepath)); |
| 554 | + return; |
| 555 | + } |
| 556 | + |
| 557 | + std::chrono::hours file_age = GetFeeEstimatorFileAge(); |
| 558 | + // fee estimate file must not be too old to avoid wrong fee estimates. |
| 559 | + if (file_age > MAX_FILE_AGE) { |
| 560 | + LogPrintf("Fee estimation file %s too old (age=%lld > %lld hours) and will not be used to avoid serving stale estimates.\n", fs::PathToString(m_estimation_filepath), Ticks<std::chrono::hours>(file_age), Ticks<std::chrono::hours>(MAX_FILE_AGE)); |
| 561 | + return; |
| 562 | + } |
| 563 | + |
| 564 | + if (!Read(est_file)) { |
551 | 565 | LogPrintf("Failed to read fee estimates from %s. Continue anyway.\n", fs::PathToString(m_estimation_filepath)); |
552 | 566 | } |
553 | 567 | } |
@@ -1016,6 +1030,13 @@ void CBlockPolicyEstimator::FlushUnconfirmed() { |
1016 | 1030 | LogPrint(BCLog::ESTIMATEFEE, "Recorded %u unconfirmed txs from mempool in %gs\n", num_entries, (endclear - startclear)*0.000001); |
1017 | 1031 | } |
1018 | 1032 |
|
| 1033 | +std::chrono::hours CBlockPolicyEstimator::GetFeeEstimatorFileAge() |
| 1034 | +{ |
| 1035 | + auto file_time = std::filesystem::last_write_time(m_estimation_filepath); |
| 1036 | + auto now = std::filesystem::file_time_type::clock::now(); |
| 1037 | + return std::chrono::duration_cast<std::chrono::hours>(now - file_time); |
| 1038 | +} |
| 1039 | + |
1019 | 1040 | FeeFilterRounder::FeeFilterRounder(const CFeeRate& minIncrementalFee) |
1020 | 1041 | { |
1021 | 1042 | CAmount minFeeLimit = std::max(CAmount(1), minIncrementalFee.GetFeePerK() / 2); |
|
0 commit comments