Skip to content

Commit 01f8ee4

Browse files
ismaelsadeeqfanquake
authored andcommitted
tx fees, policy: read stale fee estimates with a regtest-only option
If -acceptstalefeeestimates option is passed stale fee estimates can now be read when operating in regtest environments. Additionally, this commit updates all declarations of the CBlockPolicyEstimator class to include a the second constructor variable. Github-Pull: #27622 Rebased-From: cf219f2
1 parent 1c98029 commit 01f8ee4

File tree

6 files changed

+16
-7
lines changed

6 files changed

+16
-7
lines changed

src/init.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
#include <util/system.h>
7777
#include <util/thread.h>
7878
#include <util/threadnames.h>
79+
#include <util/time.h>
7980
#include <util/translation.h>
8081
#include <validation.h>
8182
#include <validationinterface.h>
@@ -561,6 +562,7 @@ void SetupServerArgs(ArgsManager& argsman)
561562
argsman.AddArg("-acceptnonstdtxn", strprintf("Relay and mine \"non-standard\" transactions (%sdefault: %u)", "testnet/regtest only; ", !testnetChainParams->RequireStandard()), ArgsManager::ALLOW_ANY | ArgsManager::DEBUG_ONLY, OptionsCategory::NODE_RELAY);
562563
argsman.AddArg("-incrementalrelayfee=<amt>", strprintf("Fee rate (in %s/kvB) used to define cost of relay, used for mempool limiting and replacement policy. (default: %s)", CURRENCY_UNIT, FormatMoney(DEFAULT_INCREMENTAL_RELAY_FEE)), ArgsManager::ALLOW_ANY | ArgsManager::DEBUG_ONLY, OptionsCategory::NODE_RELAY);
563564
argsman.AddArg("-dustrelayfee=<amt>", strprintf("Fee rate (in %s/kvB) used to define dust, the value of an output such that it will cost more than its value in fees at this fee rate to spend it. (default: %s)", CURRENCY_UNIT, FormatMoney(DUST_RELAY_TX_FEE)), ArgsManager::ALLOW_ANY | ArgsManager::DEBUG_ONLY, OptionsCategory::NODE_RELAY);
565+
argsman.AddArg("-acceptstalefeeestimates", strprintf("Read fee estimates even if they are stale (%sdefault: %u) fee estimates are considered stale if they are %s hours old", "regtest only; ", DEFAULT_ACCEPT_STALE_FEE_ESTIMATES, Ticks<std::chrono::hours>(MAX_FILE_AGE)), ArgsManager::ALLOW_ANY | ArgsManager::DEBUG_ONLY, OptionsCategory::DEBUG_TEST);
564566
argsman.AddArg("-bytespersigop", strprintf("Equivalent bytes per sigop in transactions for relay and mining (default: %u)", DEFAULT_BYTES_PER_SIGOP), ArgsManager::ALLOW_ANY, OptionsCategory::NODE_RELAY);
565567
argsman.AddArg("-datacarrier", strprintf("Relay and mine data carrier transactions (default: %u)", DEFAULT_ACCEPT_DATACARRIER), ArgsManager::ALLOW_ANY, OptionsCategory::NODE_RELAY);
566568
argsman.AddArg("-datacarriersize", strprintf("Maximum size of data in data carrier transactions we relay and mine (default: %u)", MAX_OP_RETURN_RELAY), ArgsManager::ALLOW_ANY, OptionsCategory::NODE_RELAY);
@@ -1254,7 +1256,11 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
12541256
// Don't initialize fee estimation with old data if we don't relay transactions,
12551257
// as they would never get updated.
12561258
if (!ignores_incoming_txs) {
1257-
node.fee_estimator = std::make_unique<CBlockPolicyEstimator>(FeeestPath(args));
1259+
bool read_stale_estimates = args.GetBoolArg("-acceptstalefeeestimates", DEFAULT_ACCEPT_STALE_FEE_ESTIMATES);
1260+
if (read_stale_estimates && (chainparams.NetworkIDString() != CBaseChainParams::REGTEST)) {
1261+
return InitError(strprintf(_("acceptstalefeeestimates is not supported on %s chain."), chainparams.NetworkIDString()));
1262+
}
1263+
node.fee_estimator = std::make_unique<CBlockPolicyEstimator>(FeeestPath(args), read_stale_estimates);
12581264

12591265
// Flush estimates to disk periodically
12601266
CBlockPolicyEstimator* fee_estimator = node.fee_estimator.get();

src/policy/fees.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,7 @@ bool CBlockPolicyEstimator::_removeTx(const uint256& hash, bool inBlock)
528528
}
529529
}
530530

531-
CBlockPolicyEstimator::CBlockPolicyEstimator(const fs::path& estimation_filepath)
531+
CBlockPolicyEstimator::CBlockPolicyEstimator(const fs::path& estimation_filepath, const bool read_stale_estimates)
532532
: m_estimation_filepath{estimation_filepath}, nBestSeenHeight{0}, firstRecordedHeight{0}, historicalFirst{0}, historicalBest{0}, trackedTxs{0}, untrackedTxs{0}
533533
{
534534
static_assert(MIN_BUCKET_FEERATE > 0, "Min feerate must be nonzero");
@@ -556,7 +556,7 @@ CBlockPolicyEstimator::CBlockPolicyEstimator(const fs::path& estimation_filepath
556556

557557
std::chrono::hours file_age = GetFeeEstimatorFileAge();
558558
// fee estimate file must not be too old to avoid wrong fee estimates.
559-
if (file_age > MAX_FILE_AGE) {
559+
if (file_age > MAX_FILE_AGE && !read_stale_estimates) {
560560
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));
561561
return;
562562
}

src/policy/fees.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ static constexpr std::chrono::hours FEE_FLUSH_INTERVAL{1};
3030
*/
3131
static constexpr std::chrono::hours MAX_FILE_AGE{60};
3232

33+
// Whether we allow importing a fee_estimates file older than MAX_FILE_AGE.
34+
static constexpr bool DEFAULT_ACCEPT_STALE_FEE_ESTIMATES{false};
35+
3336
class AutoFile;
3437
class CTxMemPoolEntry;
3538
class TxConfirmStats;
@@ -193,7 +196,7 @@ class CBlockPolicyEstimator
193196
const fs::path m_estimation_filepath;
194197
public:
195198
/** Create new BlockPolicyEstimator and initialize stats tracking classes with default values */
196-
CBlockPolicyEstimator(const fs::path& estimation_filepath);
199+
CBlockPolicyEstimator(const fs::path& estimation_filepath, const bool read_stale_estimates);
197200
~CBlockPolicyEstimator();
198201

199202
/** Process all the transactions that have been included in a block */

src/test/fuzz/policy_estimator.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ void initialize_policy_estimator()
2929
FUZZ_TARGET_INIT(policy_estimator, initialize_policy_estimator)
3030
{
3131
FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
32-
CBlockPolicyEstimator block_policy_estimator{FeeestPath(*g_setup->m_node.args)};
32+
CBlockPolicyEstimator block_policy_estimator{FeeestPath(*g_setup->m_node.args), DEFAULT_ACCEPT_STALE_FEE_ESTIMATES};
3333
LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 10000) {
3434
CallOneOf(
3535
fuzzed_data_provider,

src/test/fuzz/policy_estimator_io.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ FUZZ_TARGET_INIT(policy_estimator_io, initialize_policy_estimator_io)
2828
FuzzedAutoFileProvider fuzzed_auto_file_provider = ConsumeAutoFile(fuzzed_data_provider);
2929
AutoFile fuzzed_auto_file{fuzzed_auto_file_provider.open()};
3030
// Re-using block_policy_estimator across runs to avoid costly creation of CBlockPolicyEstimator object.
31-
static CBlockPolicyEstimator block_policy_estimator{FeeestPath(*g_setup->m_node.args)};
31+
static CBlockPolicyEstimator block_policy_estimator{FeeestPath(*g_setup->m_node.args), DEFAULT_ACCEPT_STALE_FEE_ESTIMATES};
3232
if (block_policy_estimator.Read(fuzzed_auto_file)) {
3333
block_policy_estimator.Write(fuzzed_auto_file);
3434
}

src/test/util/setup_common.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ ChainTestingSetup::ChainTestingSetup(const std::string& chainName, const std::ve
186186
m_node.scheduler->m_service_thread = std::thread(util::TraceThread, "scheduler", [&] { m_node.scheduler->serviceQueue(); });
187187
GetMainSignals().RegisterBackgroundSignalScheduler(*m_node.scheduler);
188188

189-
m_node.fee_estimator = std::make_unique<CBlockPolicyEstimator>(FeeestPath(*m_node.args));
189+
m_node.fee_estimator = std::make_unique<CBlockPolicyEstimator>(FeeestPath(*m_node.args), DEFAULT_ACCEPT_STALE_FEE_ESTIMATES);
190190
m_node.mempool = std::make_unique<CTxMemPool>(MemPoolOptionsForTest(m_node));
191191

192192
m_cache_sizes = CalculateCacheSizes(m_args);

0 commit comments

Comments
 (0)