Skip to content

Commit 86ff2cf

Browse files
committed
Remove the remaining fee estimation globals
This moves the CBlockPolicyEstimator to the NodeContext, which get rids of two globals and allows us to conditionally create the CBlockPolicyEstimator (and to remove a circular dep). Signed-off-by: Antoine Poinsot <[email protected]>
1 parent 03bfeee commit 86ff2cf

File tree

11 files changed

+48
-27
lines changed

11 files changed

+48
-27
lines changed

src/init.cpp

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,6 @@
8585
#include <zmq/zmqrpc.h>
8686
#endif
8787

88-
static bool fFeeEstimatesInitialized = false;
8988
static const bool DEFAULT_PROXYRANDOMIZE = true;
9089
static const bool DEFAULT_REST_ENABLE = false;
9190
static const bool DEFAULT_STOPAFTERBLOCKIMPORT = false;
@@ -236,18 +235,18 @@ void Shutdown(NodeContext& node)
236235
DumpMempool(*node.mempool);
237236
}
238237

239-
if (fFeeEstimatesInitialized)
240-
{
241-
::feeEstimator.FlushUnconfirmed();
238+
if (node.fee_estimator) {
239+
node.fee_estimator->FlushUnconfirmed();
242240
fs::path est_path = GetDataDir() / FEE_ESTIMATES_FILENAME;
243241
CAutoFile est_fileout(fsbridge::fopen(est_path, "wb"), SER_DISK, CLIENT_VERSION);
244-
if (!est_fileout.IsNull())
245-
::feeEstimator.Write(est_fileout);
246-
else
242+
if (!est_fileout.IsNull()) {
243+
node.fee_estimator->Write(est_fileout);
244+
} else {
247245
LogPrintf("%s: Failed to write fee estimates to %s\n", __func__, est_path.string());
248-
fFeeEstimatesInitialized = false;
246+
}
249247
}
250248

249+
251250
// FlushStateToDisk generates a ChainStateFlushed callback, which we should avoid missing
252251
if (node.chainman) {
253252
LOCK(cs_main);
@@ -304,6 +303,7 @@ void Shutdown(NodeContext& node)
304303
globalVerifyHandle.reset();
305304
ECC_Stop();
306305
node.mempool.reset();
306+
node.fee_estimator.reset();
307307
node.chainman = nullptr;
308308
node.scheduler.reset();
309309

@@ -1389,9 +1389,12 @@ bool AppInitMain(const util::Ref& context, NodeContext& node, interfaces::BlockA
13891389
assert(!node.connman);
13901390
node.connman = MakeUnique<CConnman>(GetRand(std::numeric_limits<uint64_t>::max()), GetRand(std::numeric_limits<uint64_t>::max()), args.GetBoolArg("-networkactive", true));
13911391

1392+
assert(!node.fee_estimator);
1393+
node.fee_estimator = std::make_unique<CBlockPolicyEstimator>();
1394+
13921395
assert(!node.mempool);
13931396
int check_ratio = std::min<int>(std::max<int>(args.GetArg("-checkmempool", chainparams.DefaultConsistencyChecks() ? 1 : 0), 0), 1000000);
1394-
node.mempool = MakeUnique<CTxMemPool>(&::feeEstimator, check_ratio);
1397+
node.mempool = std::make_unique<CTxMemPool>(node.fee_estimator.get(), check_ratio);
13951398

13961399
assert(!node.chainman);
13971400
node.chainman = &g_chainman;
@@ -1788,10 +1791,9 @@ bool AppInitMain(const util::Ref& context, NodeContext& node, interfaces::BlockA
17881791
fs::path est_path = GetDataDir() / FEE_ESTIMATES_FILENAME;
17891792
CAutoFile est_filein(fsbridge::fopen(est_path, "rb"), SER_DISK, CLIENT_VERSION);
17901793
// Allowed to fail as this file IS missing on first startup.
1791-
if (!est_filein.IsNull())
1792-
::feeEstimator.Read(est_filein);
1793-
fFeeEstimatesInitialized = true;
1794-
1794+
if (node.fee_estimator && !est_filein.IsNull()) {
1795+
node.fee_estimator->Read(est_filein);
1796+
}
17951797
// ********************************************************* Step 8: start indexers
17961798
if (args.GetBoolArg("-txindex", DEFAULT_TXINDEX)) {
17971799
g_txindex = MakeUnique<TxIndex>(nTxIndexCache, false, fReindex);

src/node/context.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <interfaces/chain.h>
99
#include <net.h>
1010
#include <net_processing.h>
11+
#include <policy/fees.h>
1112
#include <scheduler.h>
1213
#include <txmempool.h>
1314

src/node/context.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
class ArgsManager;
1414
class BanMan;
15+
class CBlockPolicyEstimator;
1516
class CConnman;
1617
class CScheduler;
1718
class CTxMemPool;
@@ -36,6 +37,7 @@ class WalletClient;
3637
struct NodeContext {
3738
std::unique_ptr<CConnman> connman;
3839
std::unique_ptr<CTxMemPool> mempool;
40+
std::unique_ptr<CBlockPolicyEstimator> fee_estimator;
3941
std::unique_ptr<PeerManager> peerman;
4042
ChainstateManager* chainman{nullptr}; // Currently a raw pointer because the memory is not managed by this struct
4143
std::unique_ptr<BanMan> banman;

src/node/interfaces.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -592,11 +592,13 @@ class ChainImpl : public Chain
592592
}
593593
CFeeRate estimateSmartFee(int num_blocks, bool conservative, FeeCalculation* calc) override
594594
{
595-
return ::feeEstimator.estimateSmartFee(num_blocks, calc, conservative);
595+
if (!m_node.fee_estimator) return {};
596+
return m_node.fee_estimator->estimateSmartFee(num_blocks, calc, conservative);
596597
}
597598
unsigned int estimateMaxBlocks() override
598599
{
599-
return ::feeEstimator.HighestTargetTracked(FeeEstimateHorizon::LONG_HALFLIFE);
600+
if (!m_node.fee_estimator) return 0;
601+
return m_node.fee_estimator->HighestTargetTracked(FeeEstimateHorizon::LONG_HALFLIFE);
600602
}
601603
CFeeRate mempoolMinFee() override
602604
{

src/rpc/blockchain.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <node/coinstats.h>
1818
#include <node/context.h>
1919
#include <node/utxo_snapshot.h>
20+
#include <policy/fees.h>
2021
#include <policy/feerate.h>
2122
#include <policy/policy.h>
2223
#include <policy/rbf.h>
@@ -81,6 +82,15 @@ ChainstateManager& EnsureChainman(const util::Ref& context)
8182
return *node.chainman;
8283
}
8384

85+
CBlockPolicyEstimator& EnsureFeeEstimator(const util::Ref& context)
86+
{
87+
NodeContext& node = EnsureNodeContext(context);
88+
if (!node.fee_estimator) {
89+
throw JSONRPCError(RPC_INTERNAL_ERROR, "Fee estimation disabled");
90+
}
91+
return *node.fee_estimator;
92+
}
93+
8494
/* Calculate the difficulty for a given block index.
8595
*/
8696
double GetDifficulty(const CBlockIndex* blockindex)

src/rpc/blockchain.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ extern RecursiveMutex cs_main;
1515

1616
class CBlock;
1717
class CBlockIndex;
18+
class CBlockPolicyEstimator;
1819
class CTxMemPool;
1920
class ChainstateManager;
2021
class UniValue;
@@ -54,5 +55,6 @@ void CalculatePercentilesByWeight(CAmount result[NUM_GETBLOCKSTATS_PERCENTILES],
5455
NodeContext& EnsureNodeContext(const util::Ref& context);
5556
CTxMemPool& EnsureMemPool(const util::Ref& context);
5657
ChainstateManager& EnsureChainman(const util::Ref& context);
58+
CBlockPolicyEstimator& EnsureFeeEstimator(const util::Ref& context);
5759

5860
#endif

src/rpc/mining.cpp

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1059,7 +1059,10 @@ static RPCHelpMan estimatesmartfee()
10591059
{
10601060
RPCTypeCheck(request.params, {UniValue::VNUM, UniValue::VSTR});
10611061
RPCTypeCheckArgument(request.params[0], UniValue::VNUM);
1062-
unsigned int max_target = ::feeEstimator.HighestTargetTracked(FeeEstimateHorizon::LONG_HALFLIFE);
1062+
1063+
CBlockPolicyEstimator& fee_estimator = EnsureFeeEstimator(request.context);
1064+
1065+
unsigned int max_target = fee_estimator.HighestTargetTracked(FeeEstimateHorizon::LONG_HALFLIFE);
10631066
unsigned int conf_target = ParseConfirmTarget(request.params[0], max_target);
10641067
bool conservative = true;
10651068
if (!request.params[1].isNull()) {
@@ -1073,7 +1076,7 @@ static RPCHelpMan estimatesmartfee()
10731076
UniValue result(UniValue::VOBJ);
10741077
UniValue errors(UniValue::VARR);
10751078
FeeCalculation feeCalc;
1076-
CFeeRate feeRate = ::feeEstimator.estimateSmartFee(conf_target, &feeCalc, conservative);
1079+
CFeeRate feeRate = fee_estimator.estimateSmartFee(conf_target, &feeCalc, conservative);
10771080
if (feeRate != CFeeRate(0)) {
10781081
result.pushKV("feerate", ValueFromAmount(feeRate.GetFeePerK()));
10791082
} else {
@@ -1144,7 +1147,10 @@ static RPCHelpMan estimaterawfee()
11441147
{
11451148
RPCTypeCheck(request.params, {UniValue::VNUM, UniValue::VNUM}, true);
11461149
RPCTypeCheckArgument(request.params[0], UniValue::VNUM);
1147-
unsigned int max_target = ::feeEstimator.HighestTargetTracked(FeeEstimateHorizon::LONG_HALFLIFE);
1150+
1151+
CBlockPolicyEstimator& fee_estimator = EnsureFeeEstimator(request.context);
1152+
1153+
unsigned int max_target = fee_estimator.HighestTargetTracked(FeeEstimateHorizon::LONG_HALFLIFE);
11481154
unsigned int conf_target = ParseConfirmTarget(request.params[0], max_target);
11491155
double threshold = 0.95;
11501156
if (!request.params[1].isNull()) {
@@ -1161,9 +1167,9 @@ static RPCHelpMan estimaterawfee()
11611167
EstimationResult buckets;
11621168

11631169
// Only output results for horizons which track the target
1164-
if (conf_target > ::feeEstimator.HighestTargetTracked(horizon)) continue;
1170+
if (conf_target > fee_estimator.HighestTargetTracked(horizon)) continue;
11651171

1166-
feeRate = ::feeEstimator.estimateRawFee(conf_target, threshold, horizon, &buckets);
1172+
feeRate = fee_estimator.estimateRawFee(conf_target, threshold, horizon, &buckets);
11671173
UniValue horizon_result(UniValue::VOBJ);
11681174
UniValue errors(UniValue::VARR);
11691175
UniValue passbucket(UniValue::VOBJ);

src/test/util/setup_common.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <net.h>
1717
#include <net_processing.h>
1818
#include <noui.h>
19+
#include <policy/fees.h>
1920
#include <pow.h>
2021
#include <rpc/blockchain.h>
2122
#include <rpc/register.h>
@@ -141,7 +142,8 @@ TestingSetup::TestingSetup(const std::string& chainName, const std::vector<const
141142

142143
pblocktree.reset(new CBlockTreeDB(1 << 20, true));
143144

144-
m_node.mempool = MakeUnique<CTxMemPool>(&::feeEstimator, 1);
145+
m_node.fee_estimator = std::make_unique<CBlockPolicyEstimator>();
146+
m_node.mempool = std::make_unique<CTxMemPool>(m_node.fee_estimator.get(), 1);
145147

146148
m_node.chainman = &::g_chainman;
147149
m_node.chainman->InitializeChainstate(*m_node.mempool);

src/validation.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
#include <logging/timer.h>
2323
#include <node/ui_interface.h>
2424
#include <optional.h>
25-
#include <policy/fees.h>
2625
#include <policy/policy.h>
2726
#include <policy/settings.h>
2827
#include <pow.h>
@@ -148,8 +147,6 @@ arith_uint256 nMinimumChainWork;
148147

149148
CFeeRate minRelayTxFee = CFeeRate(DEFAULT_MIN_RELAY_TX_FEE);
150149

151-
CBlockPolicyEstimator feeEstimator;
152-
153150
// Internal stuff
154151
namespace {
155152
CBlockIndex* pindexBestInvalid = nullptr;

src/validation.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ class CChainParams;
4242
class CInv;
4343
class CConnman;
4444
class CScriptCheck;
45-
class CBlockPolicyEstimator;
4645
class CTxMemPool;
4746
class ChainstateManager;
4847
class TxValidationState;
@@ -110,7 +109,6 @@ enum class SynchronizationState {
110109
};
111110

112111
extern RecursiveMutex cs_main;
113-
extern CBlockPolicyEstimator feeEstimator;
114112
typedef std::unordered_map<uint256, CBlockIndex*, BlockHasher> BlockMap;
115113
extern Mutex g_best_block_mutex;
116114
extern std::condition_variable g_best_block_cv;

0 commit comments

Comments
 (0)