Skip to content

Commit 03d8df4

Browse files
authored
Merge pull request #634 from evoskuil/master
Adapt to db ren of get_branch_fees, comments, style, delint.
2 parents 880a1c7 + 196a033 commit 03d8df4

2 files changed

Lines changed: 17 additions & 13 deletions

File tree

include/bitcoin/server/estimator.hpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class BCS_API estimator
3434
enum class mode
3535
{
3636
basic,
37-
markov,
37+
geometric,
3838
economical,
3939
conservative
4040
};
@@ -110,18 +110,20 @@ class BCS_API estimator
110110
std::array<bucket<horizon::large>, sizing::count> large{};
111111
};
112112

113-
// C++23: make consteval (std::pow).
113+
// C++23: make consteval.
114114
static inline double decay_rate() NOEXCEPT
115115
{
116116
static const auto rate = std::pow(0.5, 1.0 / sizing::count);
117117
return rate;
118118
}
119119

120+
// C++23: make constexpr.
120121
static inline double to_scale_term(size_t age) NOEXCEPT
121122
{
122123
return std::pow(decay_rate(), age);
123124
}
124125

126+
// C++23: make constexpr.
125127
static inline double to_scale_factor(bool push) NOEXCEPT
126128
{
127129
return std::pow(decay_rate(), push ? +1.0 : -1.0);
@@ -132,7 +134,7 @@ class BCS_API estimator
132134
bool push(const rates& block) NOEXCEPT;
133135
bool pop(const rates& block) NOEXCEPT;
134136
uint64_t compute(size_t target, double confidence,
135-
bool markov=false) const NOEXCEPT;
137+
bool geometric=false) const NOEXCEPT;
136138

137139
private:
138140
bool update(const rates& block, size_t height, bool push) NOEXCEPT;

src/estimator.cpp

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ uint64_t estimator::estimate(size_t target, mode mode) const NOEXCEPT
5252
estimate = compute(target, confidence::high);
5353
break;
5454
}
55-
case mode::markov:
55+
case mode::geometric:
5656
{
5757
estimate = compute(target, confidence::high, true);
5858
break;
@@ -88,7 +88,7 @@ bool estimator::initialize(std::atomic_bool& cancel, const node::query& query,
8888
size_t top, size_t count) NOEXCEPT
8989
{
9090
rate_sets blocks{};
91-
return query.get_block_fees(cancel, blocks, top, count) &&
91+
return query.get_branch_fees(cancel, blocks, top, count) &&
9292
initialize(blocks);
9393
}
9494

@@ -160,33 +160,35 @@ bool estimator::pop(const rates& block) NOEXCEPT
160160
}
161161

162162
uint64_t estimator::compute(size_t target, double confidence,
163-
bool markov) const NOEXCEPT
163+
bool geometric) const NOEXCEPT
164164
{
165165
const auto threshold = [](double part, double total, size_t) NOEXCEPT
166166
{
167167
return part / total;
168168
};
169169

170170
// Geometric distribution approximation, not a full Markov process.
171-
const auto geometric = [](double part, double total, size_t target) NOEXCEPT
171+
const auto markov = [](double part, double total, size_t target) NOEXCEPT
172172
{
173173
return system::power(part / total, target);
174174
};
175175

176176
const auto call = [&](const auto& buckets) NOEXCEPT
177177
{
178+
BC_PUSH_WARNING(NO_UNGUARDED_POINTERS)
179+
const auto& contribution = geometric ? markov : threshold;
180+
BC_POP_WARNING()
181+
178182
constexpr auto magic_number = 2u;
179183
const auto at_least_four = magic_number * add1(target);
180-
const auto& contribution = markov ? geometric : threshold;
181-
182184
double total{}, part{};
183185
auto index = buckets.size();
184186
auto found = index;
185187
for (const auto& bucket: std::views::reverse(buckets))
186188
{
187189
--index;
188190
total += to_floating(bucket.total.load(relaxed));
189-
part += to_floating(bucket.confirmed[target].load(relaxed));
191+
part += to_floating(bucket.confirmed.at(target).load(relaxed));
190192
if (total < at_least_four)
191193
continue;
192194

@@ -258,7 +260,7 @@ bool estimator::update(const rates& block, size_t height, bool push) NOEXCEPT
258260

259261
// Clamp overflow to last bin.
260262
const auto bin = std::log(rate / sizing::min) / growth;
261-
++counts[std::min(to_floored_integer(bin), sub1(sizing::count))];
263+
++counts.at(std::min(to_floored_integer(bin), sub1(sizing::count)));
262264
}
263265

264266
// At age zero scale term is one.
@@ -278,13 +280,13 @@ bool estimator::update(const rates& block, size_t height, bool push) NOEXCEPT
278280
continue;
279281
}
280282

281-
auto& bucket = buckets[bin++];
283+
auto& bucket = buckets.at(bin++);
282284
const auto scaled = to_floored_integer(count * scale);
283285
const auto signed_term = push ? scaled : twos_complement(scaled);
284286

285287
bucket.total.fetch_add(signed_term, relaxed);
286288
for (auto target = age; target < depth; ++target)
287-
bucket.confirmed[target].fetch_add(signed_term, relaxed);
289+
bucket.confirmed.at(target).fetch_add(signed_term, relaxed);
288290
}
289291
};
290292

0 commit comments

Comments
 (0)