Skip to content

Commit d1684be

Browse files
committed
fees: Pass in a filepath instead of referencing gArgs
1 parent 9a3d825 commit d1684be

File tree

9 files changed

+57
-16
lines changed

9 files changed

+57
-16
lines changed

src/Makefile.am

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ BITCOIN_CORE_H = \
206206
outputtype.h \
207207
policy/feerate.h \
208208
policy/fees.h \
209+
policy/fees_args.h \
209210
policy/packages.h \
210211
policy/policy.h \
211212
policy/rbf.h \
@@ -381,6 +382,7 @@ libbitcoin_node_a_SOURCES = \
381382
node/interface_ui.cpp \
382383
noui.cpp \
383384
policy/fees.cpp \
385+
policy/fees_args.cpp \
384386
policy/packages.cpp \
385387
policy/rbf.cpp \
386388
policy/settings.cpp \

src/init.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,11 @@
4040
#include <node/caches.h>
4141
#include <node/chainstate.h>
4242
#include <node/context.h>
43-
#include <node/miner.h>
4443
#include <node/interface_ui.h>
44+
#include <node/miner.h>
4545
#include <policy/feerate.h>
4646
#include <policy/fees.h>
47+
#include <policy/fees_args.h>
4748
#include <policy/policy.h>
4849
#include <policy/settings.h>
4950
#include <protocol.h>
@@ -1291,7 +1292,7 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
12911292
assert(!node.fee_estimator);
12921293
// Don't initialize fee estimation with old data if we don't relay transactions,
12931294
// as they would never get updated.
1294-
if (!ignores_incoming_txs) node.fee_estimator = std::make_unique<CBlockPolicyEstimator>();
1295+
if (!ignores_incoming_txs) node.fee_estimator = std::make_unique<CBlockPolicyEstimator>(FeeestPath(args));
12951296

12961297
// sanitize comments per BIP-0014, format user agent and check total size
12971298
std::vector<std::string> uacomments;

src/policy/fees.cpp

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@
3131
#include <stdexcept>
3232
#include <utility>
3333

34-
static const char* FEE_ESTIMATES_FILENAME = "fee_estimates.dat";
35-
3634
static constexpr double INF_FEERATE = 1e99;
3735

3836
std::string StringForFeeEstimateHorizon(FeeEstimateHorizon horizon)
@@ -529,8 +527,8 @@ bool CBlockPolicyEstimator::_removeTx(const uint256& hash, bool inBlock)
529527
}
530528
}
531529

532-
CBlockPolicyEstimator::CBlockPolicyEstimator()
533-
: nBestSeenHeight(0), firstRecordedHeight(0), historicalFirst(0), historicalBest(0), trackedTxs(0), untrackedTxs(0)
530+
CBlockPolicyEstimator::CBlockPolicyEstimator(const fs::path& estimation_filepath)
531+
: m_estimation_filepath{estimation_filepath}, nBestSeenHeight{0}, firstRecordedHeight{0}, historicalFirst{0}, historicalBest{0}, trackedTxs{0}, untrackedTxs{0}
534532
{
535533
static_assert(MIN_BUCKET_FEERATE > 0, "Min feerate must be nonzero");
536534
size_t bucketIndex = 0;
@@ -548,10 +546,9 @@ CBlockPolicyEstimator::CBlockPolicyEstimator()
548546
longStats = std::unique_ptr<TxConfirmStats>(new TxConfirmStats(buckets, bucketMap, LONG_BLOCK_PERIODS, LONG_DECAY, LONG_SCALE));
549547

550548
// If the fee estimation file is present, read recorded estimations
551-
fs::path est_filepath = gArgs.GetDataDirNet() / FEE_ESTIMATES_FILENAME;
552-
CAutoFile est_file(fsbridge::fopen(est_filepath, "rb"), SER_DISK, CLIENT_VERSION);
549+
CAutoFile est_file(fsbridge::fopen(m_estimation_filepath, "rb"), SER_DISK, CLIENT_VERSION);
553550
if (est_file.IsNull() || !Read(est_file)) {
554-
LogPrintf("Failed to read fee estimates from %s. Continue anyway.\n", fs::PathToString(est_filepath));
551+
LogPrintf("Failed to read fee estimates from %s. Continue anyway.\n", fs::PathToString(m_estimation_filepath));
555552
}
556553
}
557554

@@ -907,10 +904,9 @@ CFeeRate CBlockPolicyEstimator::estimateSmartFee(int confTarget, FeeCalculation
907904
void CBlockPolicyEstimator::Flush() {
908905
FlushUnconfirmed();
909906

910-
fs::path est_filepath = gArgs.GetDataDirNet() / FEE_ESTIMATES_FILENAME;
911-
CAutoFile est_file(fsbridge::fopen(est_filepath, "wb"), SER_DISK, CLIENT_VERSION);
907+
CAutoFile est_file(fsbridge::fopen(m_estimation_filepath, "wb"), SER_DISK, CLIENT_VERSION);
912908
if (est_file.IsNull() || !Write(est_file)) {
913-
LogPrintf("Failed to write fee estimates to %s. Continue anyway.\n", fs::PathToString(est_filepath));
909+
LogPrintf("Failed to write fee estimates to %s. Continue anyway.\n", fs::PathToString(m_estimation_filepath));
914910
}
915911
}
916912

src/policy/fees.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#define BITCOIN_POLICY_FEES_H
77

88
#include <consensus/amount.h>
9+
#include <fs.h>
910
#include <policy/feerate.h>
1011
#include <random.h>
1112
#include <sync.h>
@@ -179,9 +180,10 @@ class CBlockPolicyEstimator
179180
*/
180181
static constexpr double FEE_SPACING = 1.05;
181182

183+
const fs::path m_estimation_filepath;
182184
public:
183185
/** Create new BlockPolicyEstimator and initialize stats tracking classes with default values */
184-
CBlockPolicyEstimator();
186+
CBlockPolicyEstimator(const fs::path& estimation_filepath);
185187
~CBlockPolicyEstimator();
186188

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

src/policy/fees_args.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#include <policy/fees_args.h>
2+
3+
#include <util/system.h>
4+
5+
namespace {
6+
const char* FEE_ESTIMATES_FILENAME = "fee_estimates.dat";
7+
} // namespace
8+
9+
fs::path FeeestPath(const ArgsManager& argsman)
10+
{
11+
return argsman.GetDataDirNet() / FEE_ESTIMATES_FILENAME;
12+
}

src/policy/fees_args.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright (c) 2022 The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#ifndef BITCOIN_POLICY_FEES_ARGS_H
6+
#define BITCOIN_POLICY_FEES_ARGS_H
7+
8+
#include <fs.h>
9+
10+
class ArgsManager;
11+
12+
/** @return The fee estimates data file path. */
13+
fs::path FeeestPath(const ArgsManager& argsman);
14+
15+
#endif // BITCOIN_POLICY_FEES_ARGS_H

src/test/fuzz/policy_estimator.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

55
#include <policy/fees.h>
6+
#include <policy/fees_args.h>
67
#include <primitives/transaction.h>
78
#include <test/fuzz/FuzzedDataProvider.h>
89
#include <test/fuzz/fuzz.h>
@@ -15,15 +16,20 @@
1516
#include <string>
1617
#include <vector>
1718

19+
namespace {
20+
const BasicTestingSetup* g_setup;
21+
} // namespace
22+
1823
void initialize_policy_estimator()
1924
{
2025
static const auto testing_setup = MakeNoLogFileContext<>();
26+
g_setup = testing_setup.get();
2127
}
2228

2329
FUZZ_TARGET_INIT(policy_estimator, initialize_policy_estimator)
2430
{
2531
FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
26-
CBlockPolicyEstimator block_policy_estimator;
32+
CBlockPolicyEstimator block_policy_estimator{FeeestPath(*g_setup->m_node.args)};
2733
LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 10000) {
2834
CallOneOf(
2935
fuzzed_data_provider,

src/test/fuzz/policy_estimator_io.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

55
#include <policy/fees.h>
6+
#include <policy/fees_args.h>
67
#include <test/fuzz/FuzzedDataProvider.h>
78
#include <test/fuzz/fuzz.h>
89
#include <test/fuzz/util.h>
@@ -11,9 +12,14 @@
1112
#include <cstdint>
1213
#include <vector>
1314

15+
namespace {
16+
const BasicTestingSetup* g_setup;
17+
} // namespace
18+
1419
void initialize_policy_estimator_io()
1520
{
1621
static const auto testing_setup = MakeNoLogFileContext<>();
22+
g_setup = testing_setup.get();
1723
}
1824

1925
FUZZ_TARGET_INIT(policy_estimator_io, initialize_policy_estimator_io)
@@ -22,7 +28,7 @@ FUZZ_TARGET_INIT(policy_estimator_io, initialize_policy_estimator_io)
2228
FuzzedAutoFileProvider fuzzed_auto_file_provider = ConsumeAutoFile(fuzzed_data_provider);
2329
CAutoFile fuzzed_auto_file = fuzzed_auto_file_provider.open();
2430
// Re-using block_policy_estimator across runs to avoid costly creation of CBlockPolicyEstimator object.
25-
static CBlockPolicyEstimator block_policy_estimator;
31+
static CBlockPolicyEstimator block_policy_estimator{FeeestPath(*g_setup->m_node.args)};
2632
if (block_policy_estimator.Read(fuzzed_auto_file)) {
2733
block_policy_estimator.Write(fuzzed_auto_file);
2834
}

src/test/util/setup_common.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <node/miner.h>
2424
#include <noui.h>
2525
#include <policy/fees.h>
26+
#include <policy/fees_args.h>
2627
#include <pow.h>
2728
#include <rpc/blockchain.h>
2829
#include <rpc/register.h>
@@ -177,7 +178,7 @@ ChainTestingSetup::ChainTestingSetup(const std::string& chainName, const std::ve
177178
m_node.scheduler->m_service_thread = std::thread(util::TraceThread, "scheduler", [&] { m_node.scheduler->serviceQueue(); });
178179
GetMainSignals().RegisterBackgroundSignalScheduler(*m_node.scheduler);
179180

180-
m_node.fee_estimator = std::make_unique<CBlockPolicyEstimator>();
181+
m_node.fee_estimator = std::make_unique<CBlockPolicyEstimator>(FeeestPath(*m_node.args));
181182
m_node.mempool = std::make_unique<CTxMemPool>(MemPoolOptionsForTest(m_node));
182183

183184
m_cache_sizes = CalculateCacheSizes(m_args);

0 commit comments

Comments
 (0)