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
1 change: 1 addition & 0 deletions builds/gnu/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ include_bitcoin_node_typesdir = \
${includedir}/bitcoin/node/types

include_bitcoin_node_types_HEADERS = \
${srcdir}/../../include/bitcoin/node/types/block_template.hpp \
${srcdir}/../../include/bitcoin/node/types/constants.hpp \
${srcdir}/../../include/bitcoin/node/types/estimate_mode.hpp \
${srcdir}/../../include/bitcoin/node/types/pool_event.hpp \
Expand Down
1 change: 1 addition & 0 deletions builds/msvc/vs2026/libbitcoin-node/libbitcoin-node.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@
<ClInclude Include="..\..\..\..\include\bitcoin\node\sessions\session_peer.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\node\sessions\sessions.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\node\settings.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\node\types\block_template.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\node\types\constants.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\node\types\estimate_mode.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\node\types\pool_event.hpp" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,9 @@
<ClInclude Include="..\..\..\..\include\bitcoin\node\settings.hpp">
<Filter>include\bitcoin\node</Filter>
</ClInclude>
<ClInclude Include="..\..\..\..\include\bitcoin\node\types\block_template.hpp">
<Filter>include\bitcoin\node\types</Filter>
</ClInclude>
<ClInclude Include="..\..\..\..\include\bitcoin\node\types\constants.hpp">
<Filter>include\bitcoin\node\types</Filter>
</ClInclude>
Expand Down
1 change: 1 addition & 0 deletions include/bitcoin/node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
#include <bitcoin/node/sessions/session_outbound.hpp>
#include <bitcoin/node/sessions/session_peer.hpp>
#include <bitcoin/node/sessions/sessions.hpp>
#include <bitcoin/node/types/block_template.hpp>
#include <bitcoin/node/types/constants.hpp>
#include <bitcoin/node/types/estimate_mode.hpp>
#include <bitcoin/node/types/pool_event.hpp>
Expand Down
2 changes: 1 addition & 1 deletion include/bitcoin/node/chase.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ enum class chase
/// Issued by 'transaction' and handled by 'protocol_transaction_out'.
relay,

/// Candidate block (template) has been created, upon request.
/// Template of the next block has been replaced.
/// Issued by 'template' and handled by [miners].
template_,

Expand Down
3 changes: 3 additions & 0 deletions include/bitcoin/node/chasers/chaser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ class BCN_API chaser
/// Build a template of pooled txs, parents first, and its height.
virtual void request_template(template_handler&& handler) NOEXCEPT;

/// Count of changes to the pool.
virtual uint64_t pool_generation() const NOEXCEPT;

/// Events.
/// -----------------------------------------------------------------------

Expand Down
51 changes: 43 additions & 8 deletions include/bitcoin/node/chasers/chaser_template.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
#ifndef LIBBITCOIN_NODE_CHASERS_CHASER_TEMPLATE_HPP
#define LIBBITCOIN_NODE_CHASERS_CHASER_TEMPLATE_HPP

#include <atomic>
#include <vector>
#include <bitcoin/node/chasers/chaser.hpp>
#include <bitcoin/node/define.hpp>

Expand All @@ -27,7 +29,7 @@ namespace node {

class full_node;

/// Construct template blocks upon request.
/// Hold the template of the next block, replaced as chain and pool move.
class BCN_API chaser_template
: public chaser
{
Expand All @@ -37,20 +39,53 @@ class BCN_API chaser_template
chaser_template(full_node& node) NOEXCEPT;

code start() NOEXCEPT override;
void stopping(const code& ec) NOEXCEPT override;

/// Template of pooled txs built on request, parents first, and its height.
virtual void get_template(template_handler&& handler) NOEXCEPT;
/// The template held, built first if the chain or pool has moved.
virtual void get_template(block_template_handler&& handler) NOEXCEPT;

/// Count a demand for template replacement by interval (or uncount).
virtual void demand_template(bool demand) NOEXCEPT;

/// Sequence of the template held, zero if none yet (thread safe).
virtual uint64_t template_generation() const NOEXCEPT;

protected:
virtual void do_get_template(const template_handler& handler) NOEXCEPT;
virtual bool handle_chase(const code& ec, event_value value) NOEXCEPT;

virtual void do_stopping(const code& ec) NOEXCEPT;
virtual void do_get_template(
const block_template_handler& handler) NOEXCEPT;
virtual void do_demand_template(bool demand) NOEXCEPT;

/// The chain has moved, so the template held is not of its top.
virtual void do_moved(bool settled) NOEXCEPT;

private:
void build() NOEXCEPT;
void handle_build(const code& ec, size_t height,
const database::tx_links& links,
const template_handler& handler) NOEXCEPT;
const database::tx_links& links, uint64_t generation,
size_t moves) NOEXCEPT;
void do_build(const code& ec, size_t height,
const database::tx_links& links,
const template_handler& handler) NOEXCEPT;
const database::tx_links& links, uint64_t generation,
size_t moves) NOEXCEPT;
code replace(size_t height, const database::tx_links& links) NOEXCEPT;
void complete(const code& ec) NOEXCEPT;
void start_timer() NOEXCEPT;
void handle_timer(const code& ec) NOEXCEPT;

// This is thread safe.
std::atomic<uint64_t> generation_{};

// These are protected by strand.
block_template::cptr held_{};
std::vector<block_template_handler> waiting_{};
network::deadline::ptr timer_{};
uint64_t built_{};
size_t moves_{};
size_t moved_{};
size_t demand_{};
bool building_{};
};

} // namespace node
Expand Down
4 changes: 4 additions & 0 deletions include/bitcoin/node/chasers/chaser_transaction.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ class BCN_API chaser_transaction
virtual void get_confirmable_fees(
confirmable_fees_handler&& handler) NOEXCEPT;

/// Count of changes to the pool (thread safe).
virtual uint64_t pool_generation() const NOEXCEPT;

/// Subscribe to pool scans (notified on the strand).
virtual void subscribe_pool(pool_notifier&& handler) NOEXCEPT;

Expand Down Expand Up @@ -303,6 +306,7 @@ class BCN_API chaser_transaction
std::atomic_bool pooling_{};
std::atomic<size_t> writers_{};
std::atomic<uint64_t> reserved_{};
std::atomic<uint64_t> generation_{};
const uint64_t outbound_excluded_;
const uint64_t inbound_excluded_;

Expand Down
13 changes: 11 additions & 2 deletions include/bitcoin/node/full_node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,17 @@ class BCN_API full_node
/// Subscribe to pool scans (notified on the transaction strand).
virtual void subscribe_pool(pool_notifier&& handler) NOEXCEPT;

/// Template of pooled txs built on request, parents first, and its height.
virtual void get_template(template_handler&& handler) NOEXCEPT;
/// Count of changes to the pool (thread safe).
virtual uint64_t pool_generation() const NOEXCEPT;

/// The template held, built first if the chain or pool has moved.
virtual void get_template(block_template_handler&& handler) NOEXCEPT;

/// Count a demand for template replacement by interval (or uncount).
virtual void demand_template(bool demand) NOEXCEPT;

/// Sequence of the template held, zero if none yet (thread safe).
virtual uint64_t template_generation() const NOEXCEPT;

/// Manage download queue.
virtual void get_hashes(map_handler&& handler) NOEXCEPT;
Expand Down
10 changes: 8 additions & 2 deletions include/bitcoin/node/protocols/protocol.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,14 @@ class BCN_API protocol
virtual void connect(const network::config::endpoint& endpoint,
network::net::channel_notifier&& handler) NOEXCEPT;

/// Template of pooled txs built on request, parents first, and its height.
virtual void get_template(template_handler&& handler) NOEXCEPT;
/// The template held, built first if the chain or pool has moved.
virtual void get_template(block_template_handler&& handler) NOEXCEPT;

/// Count a demand for template replacement by interval (or uncount).
virtual void demand_template(bool demand) NOEXCEPT;

/// Sequence of the template held, zero if none yet (thread safe).
virtual uint64_t template_generation() const NOEXCEPT;

/// Lowest rate (per virtual kilobyte) confirmed by the target block.
void estimate(size_t target, estimate_mode mode,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ class BCN_API protocol_transaction_in_106
tx_type_(session->node_settings().require_witness ?
type_id::witness_tx : type_id::transaction),
maximum_requests_(system::possible_wide_cast<size_t>(
session->node_settings().pool.maximum_requests)),
session->network_settings().maximum_requests)),
request_timer_(system::emplace_shared<network::deadline>(session->log,
channel->strand(), session->node_settings().pool.request_timeout()))
channel->strand(), session->network_settings().request_timeout()))
{
}

Expand Down
10 changes: 8 additions & 2 deletions include/bitcoin/node/sessions/session.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,14 @@ class BCN_API session
virtual void connect(const network::config::endpoint& endpoint,
network::net::channel_notifier&& handler) NOEXCEPT;

/// Template of pooled txs built on request, parents first, and its height.
virtual void get_template(template_handler&& handler) NOEXCEPT;
/// The template held, built first if the chain or pool has moved.
virtual void get_template(block_template_handler&& handler) NOEXCEPT;

/// Count a demand for template replacement by interval (or uncount).
virtual void demand_template(bool demand) NOEXCEPT;

/// Sequence of the template held, zero if none yet (thread safe).
virtual uint64_t template_generation() const NOEXCEPT;

/// Lowest rate (per virtual kilobyte) confirmed by the target block.
void estimate(size_t target, estimate_mode mode,
Expand Down
6 changes: 3 additions & 3 deletions include/bitcoin/node/settings.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,15 @@ class BCN_API settings
uint32_t maximum_chunk_depth;
uint16_t estimate_cache_seconds;
uint32_t estimate_room;
uint32_t maximum_requests;
uint16_t request_timeout_seconds;
uint16_t template_interval_seconds;
uint64_t template_fee_increase;
bool thread_priority;

/// Helpers.
virtual uint64_t minimum_fee_rate_() const NOEXCEPT;
virtual uint64_t cutoff_rate_() const NOEXCEPT;
virtual network::steady_clock::duration estimate_cache() const NOEXCEPT;
virtual network::steady_clock::duration request_timeout() const NOEXCEPT;
virtual network::steady_clock::duration template_interval() const NOEXCEPT;
virtual network::processing_priority thread_priority_() const NOEXCEPT;
};

Expand Down
55 changes: 55 additions & 0 deletions include/bitcoin/node/types/block_template.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
* Copyright (c) 2011-2026 libbitcoin developers
*
* 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_NODE_TYPES_BLOCK_TEMPLATE_HPP
#define LIBBITCOIN_NODE_TYPES_BLOCK_TEMPLATE_HPP

#include <memory>
#include <bitcoin/node/chase.hpp>

namespace libbitcoin {
namespace node {

/// Template of the next block, its txs parents first.
struct block_template
{
typedef std::shared_ptr<const block_template> cptr;

/// Height of the block.
size_t height;

/// Count of templates held to this one.
uint64_t sequence;

/// Sum of the fees of the txs.
uint64_t fees;

/// The txs, less the coinbase.
database::tx_links links;

/// Witness commitment of the coinbase, its reserved value null.
system::hash_digest commitment;

/// Merkle branch of the coinbase.
system::hashes branch;
};

} // namespace node
} // namespace libbitcoin

#endif
3 changes: 3 additions & 0 deletions include/bitcoin/node/types/type.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

#include <bitcoin/network.hpp>
#include <bitcoin/node/chase.hpp>
#include <bitcoin/node/types/block_template.hpp>
#include <bitcoin/node/types/pool_event.hpp>

namespace libbitcoin {
Expand All @@ -45,6 +46,8 @@ typedef std::function<void(const code&, const map_ptr&,
/// Mining types.
typedef std::function<void(const code&, size_t,
const database::tx_links&)> template_handler;
typedef std::function<void(const code&,
const block_template::cptr&)> block_template_handler;

/// Confirmable types.
typedef std::function<void(const code&,
Expand Down
1 change: 1 addition & 0 deletions include/bitcoin/node/types/types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#ifndef LIBBITCOIN_NODE_TYPES_TYPES_HPP
#define LIBBITCOIN_NODE_TYPES_TYPES_HPP

#include <bitcoin/node/types/block_template.hpp>
#include <bitcoin/node/types/constants.hpp>
#include <bitcoin/node/types/estimate_mode.hpp>
#include <bitcoin/node/types/pool_event.hpp>
Expand Down
5 changes: 5 additions & 0 deletions src/chasers/chaser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ void chaser::request_template(template_handler&& handler) NOEXCEPT
node_.build_template(std::move(handler));
}

uint64_t chaser::pool_generation() const NOEXCEPT
{
return node_.pool_generation();
}

code chaser::prune(const store::event_handler& handler) NOEXCEPT
{
return node_.prune(handler);
Expand Down
Loading
Loading