fees: introduce locks to forecaster manager

This commit is contained in:
ismaelsadeeq 2025-03-05 19:22:57 +01:00
parent 02a0639602
commit 491b4799e7
No known key found for this signature in database
GPG key ID: 0E3908F364989888
2 changed files with 16 additions and 5 deletions

View file

@ -13,11 +13,13 @@
void FeeRateForecasterManager::RegisterForecaster(std::shared_ptr<Forecaster> forecaster)
{
LOCK(cs);
forecasters.emplace(forecaster->GetForecastType(), forecaster);
}
CBlockPolicyEstimator* FeeRateForecasterManager::GetBlockPolicyEstimator()
{
LOCK(cs);
Assert(forecasters.contains(ForecastType::BLOCK_POLICY));
Forecaster* block_policy_estimator = forecasters.find(ForecastType::BLOCK_POLICY)->second.get();
return dynamic_cast<CBlockPolicyEstimator*>(block_policy_estimator);
@ -26,6 +28,7 @@ CBlockPolicyEstimator* FeeRateForecasterManager::GetBlockPolicyEstimator()
std::pair<ForecastResult, std::vector<std::string>> FeeRateForecasterManager::ForecastFeeRateFromForecasters(
int target, bool conservative) const
{
LOCK(cs);
std::vector<std::string> err_messages;
ForecastResult feerate_forecast;
@ -66,6 +69,7 @@ std::pair<ForecastResult, std::vector<std::string>> FeeRateForecasterManager::Fo
unsigned int FeeRateForecasterManager::MaximumTarget() const
{
LOCK(cs);
unsigned int maximum_target{0};
for (const auto& forecaster : forecasters) {
maximum_target = std::max(maximum_target, forecaster.second->MaximumTarget());

View file

@ -5,6 +5,9 @@
#ifndef BITCOIN_POLICY_FEES_FORECASTER_MAN_H
#define BITCOIN_POLICY_FEES_FORECASTER_MAN_H
#include <sync.h>
#include <threadsafety.h>
#include <memory>
#include <optional>
#include <unordered_map>
@ -22,18 +25,21 @@ class FeeRateForecasterManager
{
private:
//! Map of all registered forecasters to their shared pointers.
std::unordered_map<ForecastType, std::shared_ptr<Forecaster>> forecasters;
std::unordered_map<ForecastType, std::shared_ptr<Forecaster>> forecasters GUARDED_BY(cs);
mutable Mutex cs;
public:
/**
* Register a forecaster.
* @param[in] forecaster shared pointer to a Forecaster instance.
*/
void RegisterForecaster(std::shared_ptr<Forecaster> forecaster);
void RegisterForecaster(std::shared_ptr<Forecaster> forecaster) EXCLUSIVE_LOCKS_REQUIRED(!cs);
/*
* Return the pointer to block policy estimator.
*/
CBlockPolicyEstimator* GetBlockPolicyEstimator();
CBlockPolicyEstimator* GetBlockPolicyEstimator() EXCLUSIVE_LOCKS_REQUIRED(!cs);
/**
* Get a fee rate forecast from all registered forecasters for a given confirmation target.
@ -44,12 +50,13 @@ public:
* @param[in] conservative True if the package cannot be fee bumped later.
* @return A pair consisting of the forecast result and a vector of forecaster names.
*/
std::pair<ForecastResult, std::vector<std::string>> ForecastFeeRateFromForecasters(int target, bool conservative) const;
std::pair<ForecastResult, std::vector<std::string>> ForecastFeeRateFromForecasters(int target, bool conservative) const
EXCLUSIVE_LOCKS_REQUIRED(!cs);
/**
* @brief Returns the maximum supported confirmation target from all forecasters.
*/
unsigned int MaximumTarget() const;
unsigned int MaximumTarget() const EXCLUSIVE_LOCKS_REQUIRED(!cs);
};
#endif // BITCOIN_POLICY_FEES_FORECASTER_MAN_H