Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ src_libbitcoin_server_la_LIBADD = ${bitcoin_node_LIBS}
src_libbitcoin_server_la_SOURCES = \
src/configuration.cpp \
src/error.cpp \
src/estimator.cpp \
src/parser.cpp \
src/server_node.cpp \
src/settings.cpp \
Expand Down Expand Up @@ -65,6 +66,7 @@ test_libbitcoin_server_test_LDADD = src/libbitcoin-server.la ${boost_unit_test_f
test_libbitcoin_server_test_SOURCES = \
test/configuration.cpp \
test/error.cpp \
test/estimator.cpp \
test/main.cpp \
test/settings.cpp \
test/test.cpp \
Expand Down Expand Up @@ -128,6 +130,7 @@ include_bitcoin_server_HEADERS = \
include/bitcoin/server/configuration.hpp \
include/bitcoin/server/define.hpp \
include/bitcoin/server/error.hpp \
include/bitcoin/server/estimator.hpp \
include/bitcoin/server/parser.hpp \
include/bitcoin/server/server_node.hpp \
include/bitcoin/server/settings.hpp \
Expand Down
8 changes: 8 additions & 0 deletions builds/cmake/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ add_definitions(
add_library( ${CANONICAL_LIB_NAME}
"../../src/configuration.cpp"
"../../src/error.cpp"
"../../src/estimator.cpp"
"../../src/parser.cpp"
"../../src/server_node.cpp"
"../../src/settings.cpp"
Expand Down Expand Up @@ -288,10 +289,17 @@ if (with-tests)
add_executable( libbitcoin-server-test
"../../test/configuration.cpp"
"../../test/error.cpp"
"../../test/estimator.cpp"
"../../test/main.cpp"
"../../test/settings.cpp"
"../../test/test.cpp"
"../../test/test.hpp"
"../../test/endpoints/README.md"
"../../test/endpoints/bitcoind_rpc.py"
"../../test/endpoints/configuration.py"
"../../test/endpoints/electrum.py"
"../../test/endpoints/native.py"
"../../test/endpoints/utils.py"
"../../test/parsers/bitcoind_query.cpp"
"../../test/parsers/bitcoind_target.cpp"
"../../test/parsers/native_query.cpp"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,11 @@
<Import Project="$(ProjectDir)$(ProjectName).props" />
</ImportGroup>
<ItemGroup>
<ClCompile Include="..\..\..\..\test\configuration.cpp" />
<ClCompile Include="..\..\..\..\test\configuration.cpp">
<ObjectFileName>$(IntDir)test_configuration.obj</ObjectFileName>
</ClCompile>
<ClCompile Include="..\..\..\..\test\error.cpp" />
<ClCompile Include="..\..\..\..\test\estimator.cpp" />
<ClCompile Include="..\..\..\..\test\main.cpp" />
<ClCompile Include="..\..\..\..\test\parsers\bitcoind_query.cpp" />
<ClCompile Include="..\..\..\..\test\parsers\bitcoind_target.cpp" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@
<Filter Include="src">
<UniqueIdentifier>{66A0E586-2E3A-448F-0000-000000000000}</UniqueIdentifier>
</Filter>
<Filter Include="src\parsers">
<Filter Include="src\endpoints">
<UniqueIdentifier>{66A0E586-2E3A-448F-0000-000000000001}</UniqueIdentifier>
</Filter>
<Filter Include="src\parsers">
<UniqueIdentifier>{66A0E586-2E3A-448F-0000-000000000002}</UniqueIdentifier>
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\..\..\..\test\configuration.cpp">
Expand All @@ -21,6 +24,9 @@
<ClCompile Include="..\..\..\..\test\error.cpp">
<Filter>src</Filter>
</ClCompile>
<ClCompile Include="..\..\..\..\test\estimator.cpp">
<Filter>src</Filter>
</ClCompile>
<ClCompile Include="..\..\..\..\test\main.cpp">
<Filter>src</Filter>
</ClCompile>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@
<ItemGroup>
<ClCompile Include="..\..\..\..\src\configuration.cpp" />
<ClCompile Include="..\..\..\..\src\error.cpp" />
<ClCompile Include="..\..\..\..\src\estimator.cpp" />
<ClCompile Include="..\..\..\..\src\parser.cpp" />
<ClCompile Include="..\..\..\..\src\parsers\bitcoind_query.cpp" />
<ClCompile Include="..\..\..\..\src\parsers\bitcoind_target.cpp" />
Expand Down Expand Up @@ -151,6 +152,7 @@
<ClInclude Include="..\..\..\..\include\bitcoin\server\configuration.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\server\define.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\server\error.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\server\estimator.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\server\interfaces\bitcoind_rest.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\server\interfaces\bitcoind_rpc.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\server\interfaces\electrum.hpp" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@
<ClCompile Include="..\..\..\..\src\error.cpp">
<Filter>src</Filter>
</ClCompile>
<ClCompile Include="..\..\..\..\src\estimator.cpp">
<Filter>src</Filter>
</ClCompile>
<ClCompile Include="..\..\..\..\src\parser.cpp">
<Filter>src</Filter>
</ClCompile>
Expand Down Expand Up @@ -137,6 +140,9 @@
<ClInclude Include="..\..\..\..\include\bitcoin\server\error.hpp">
<Filter>include\bitcoin\server</Filter>
</ClInclude>
<ClInclude Include="..\..\..\..\include\bitcoin\server\estimator.hpp">
<Filter>include\bitcoin\server</Filter>
</ClInclude>
<ClInclude Include="..\..\..\..\include\bitcoin\server\interfaces\bitcoind_rest.hpp">
<Filter>include\bitcoin\server\interfaces</Filter>
</ClInclude>
Expand Down
1 change: 1 addition & 0 deletions include/bitcoin/server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <bitcoin/server/configuration.hpp>
#include <bitcoin/server/define.hpp>
#include <bitcoin/server/error.hpp>
#include <bitcoin/server/estimator.hpp>
#include <bitcoin/server/parser.hpp>
#include <bitcoin/server/server_node.hpp>
#include <bitcoin/server/settings.hpp>
Expand Down
148 changes: 148 additions & 0 deletions include/bitcoin/server/estimator.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
/**
* Copyright (c) 2011-2025 libbitcoin developers (see AUTHORS)
*
* This file is part of libbitcoin.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef LIBBITCOIN_SERVER_ESTIMATOR_HPP
#define LIBBITCOIN_SERVER_ESTIMATOR_HPP

#include <atomic>
#include <bitcoin/server.hpp>

namespace libbitcoin {
namespace server {

class BCS_API estimator
{
public:
DELETE_COPY_MOVE_DESTRUCT(estimator);

/// Estimation modes.
enum class mode
{
basic,
markov,
economical,
conservative
};

estimator() NOEXCEPT {};

/// Fee estimation in satoshis / transaction virtual size.
/// Pass zero to target next block for confirmation, range:0..1007.
uint64_t estimate(size_t target, mode mode) const NOEXCEPT;

/// Populate accumulator.
bool initialize(std::atomic_bool& cancel, const node::query& query,
size_t top, size_t count) NOEXCEPT;

/// Update accumulator.
bool push(const node::query& query) NOEXCEPT;
bool pop(const node::query& query) NOEXCEPT;

/// Top height of accumulator.
size_t top_height() const NOEXCEPT;

protected:
using rates = database::fee_rates;
using rate_sets = database::fee_rate_sets;

/// Bucket count sizing parameters.
enum horizon : size_t
{
small = 12,
medium = 48,
large = 1008
};

/// Estimation confidences.
struct confidence
{
static constexpr double low = 0.60;
static constexpr double mid = 0.85;
static constexpr double high = 0.95;
};

/// Bucket count sizing parameters.
struct sizing
{
static constexpr double min = 0.1;
static constexpr double max = 100'000.0;
static constexpr double step = 1.05;

/// Derived from min/max/step above.
static constexpr size_t count = 283;
};

/// Accumulator (persistent, decay-weighted counters).
struct accumulator
{
template <size_t Depth>
struct bucket
{
/// Total scaled txs in bucket.
std::atomic<size_t> total{};

/// confirmed[n]: scaled txs confirmed in > n blocks.
std::array<std::atomic<size_t>, Depth> confirmed;
};

/// Current block height of accumulated state.
size_t top_height{};

/// Accumulated scaled fee in decayed buckets by horizon.
/// Array count is the half life of the decay it implies.
std::array<bucket<horizon::small>, sizing::count> small{};
std::array<bucket<horizon::medium>, sizing::count> medium{};
std::array<bucket<horizon::large>, sizing::count> large{};
};

// C++23: make consteval (std::pow).
static inline double decay_rate() NOEXCEPT
{
static const auto rate = std::pow(0.5, 1.0 / sizing::count);
return rate;
}

static inline double to_scale_term(size_t age) NOEXCEPT
{
return std::pow(decay_rate(), age);
}

static inline double to_scale_factor(bool push) NOEXCEPT
{
return std::pow(decay_rate(), push ? +1.0 : -1.0);
}

const accumulator& history() const NOEXCEPT;
bool initialize(const rate_sets& blocks) NOEXCEPT;
bool push(const rates& block) NOEXCEPT;
bool pop(const rates& block) NOEXCEPT;
uint64_t compute(size_t target, double confidence,
bool markov=false) const NOEXCEPT;

private:
bool update(const rates& block, size_t height, bool push) NOEXCEPT;
void decay(auto& buckets, double factor) NOEXCEPT;
void decay(bool push) NOEXCEPT;

accumulator fees_{};
};

} // namespace server
} // namespace libbitcoin

#endif
Loading
Loading