Skip to content

Commit 59ef8ab

Browse files
authored
Merge pull request #636 from evoskuil/master
Test and patch fee estimator.
2 parents 02dd96c + f4359eb commit 59ef8ab

3 files changed

Lines changed: 417 additions & 8 deletions

File tree

include/bitcoin/server/estimator.hpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#define LIBBITCOIN_SERVER_ESTIMATOR_HPP
2121

2222
#include <atomic>
23+
#include <memory>
2324
#include <bitcoin/server.hpp>
2425

2526
namespace libbitcoin {
@@ -28,6 +29,8 @@ namespace server {
2829
class BCS_API estimator
2930
{
3031
public:
32+
typedef std::shared_ptr<estimator> ptr;
33+
3134
DELETE_COPY_MOVE_DESTRUCT(estimator);
3235

3336
/// Estimation modes.
@@ -39,7 +42,8 @@ class BCS_API estimator
3942
conservative
4043
};
4144

42-
estimator() NOEXCEPT {};
45+
/// Factory method to create a shared instance on the heap.
46+
static ptr create() NOEXCEPT;
4347

4448
/// Fee estimation in satoshis / transaction virtual size.
4549
/// Pass zero to target next block for confirmation, range:0..1007.
@@ -120,7 +124,7 @@ class BCS_API estimator
120124
// C++23: make constexpr.
121125
static inline double to_scale_term(size_t age) NOEXCEPT
122126
{
123-
return std::pow(decay_rate(), age);
127+
return system::power(decay_rate(), age);
124128
}
125129

126130
// C++23: make constexpr.
@@ -129,6 +133,8 @@ class BCS_API estimator
129133
return std::pow(decay_rate(), push ? +1.0 : -1.0);
130134
}
131135

136+
estimator() NOEXCEPT {};
137+
accumulator& history() NOEXCEPT;
132138
const accumulator& history() const NOEXCEPT;
133139
bool initialize(const rate_sets& blocks) NOEXCEPT;
134140
bool push(const rates& block) NOEXCEPT;

src/estimator.cpp

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ constexpr auto relaxed = std::memory_order_relaxed;
3535
// public
3636
// ----------------------------------------------------------------------------
3737

38+
estimator::ptr estimator::create() NOEXCEPT
39+
{
40+
return { new estimator{} ,[](estimator* ptr) NOEXCEPT { delete ptr; } };
41+
}
42+
3843
uint64_t estimator::estimate(size_t target, mode mode) const NOEXCEPT
3944
{
4045
// max_uint64 is failure sentinel (and unachievable/invalid as a fee).
@@ -128,6 +133,11 @@ size_t estimator::top_height() const NOEXCEPT
128133
// protected
129134
// ----------------------------------------------------------------------------
130135

136+
estimator::accumulator& estimator::history() NOEXCEPT
137+
{
138+
return fees_;
139+
}
140+
131141
const estimator::accumulator& estimator::history() const NOEXCEPT
132142
{
133143
return fees_;
@@ -139,11 +149,12 @@ bool estimator::initialize(const rate_sets& blocks) NOEXCEPT
139149
if (is_zero(count))
140150
return true;
141151

142-
const auto top = top_height();
143-
auto height = top - sub1(count);
144-
if (system::is_subtract_overflow(top, sub1(count)))
152+
if (system::is_add_overflow(fees_.top_height, sub1(count)))
145153
return false;
146154

155+
auto height = fees_.top_height;
156+
fees_.top_height += sub1(count);
157+
147158
// TODO: could be parallel by block.
148159
for (const auto& block: blocks)
149160
if (!update(block, height++, true))
@@ -162,8 +173,9 @@ bool estimator::push(const rates& block) NOEXCEPT
162173
// Blocks must be pushed in order (but independent of chain index).
163174
bool estimator::pop(const rates& block) NOEXCEPT
164175
{
165-
const auto result = update(block, fees_.top_height--, false);
176+
const auto result = update(block, fees_.top_height, false);
166177
decay(false);
178+
--fees_.top_height;
167179
return result;
168180
}
169181

0 commit comments

Comments
 (0)