|
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 | }
|
@@ -1017,6 +1031,13 @@ void CBlockPolicyEstimator::FlushUnconfirmed()
|
1017 | 1031 | LogPrint(BCLog::ESTIMATEFEE, "Recorded %u unconfirmed txs from mempool in %gs\n", num_entries, Ticks<SecondsDouble>(endclear - startclear));
|
1018 | 1032 | }
|
1019 | 1033 |
|
| 1034 | +std::chrono::hours CBlockPolicyEstimator::GetFeeEstimatorFileAge() |
| 1035 | +{ |
| 1036 | + auto file_time = std::filesystem::last_write_time(m_estimation_filepath); |
| 1037 | + auto now = std::filesystem::file_time_type::clock::now(); |
| 1038 | + return std::chrono::duration_cast<std::chrono::hours>(now - file_time); |
| 1039 | +} |
| 1040 | + |
1020 | 1041 | static std::set<double> MakeFeeSet(const CFeeRate& min_incremental_fee,
|
1021 | 1042 | double max_filter_fee_rate,
|
1022 | 1043 | double fee_filter_spacing)
|
|
0 commit comments