diff --git a/builds/gnu/Makefile.am b/builds/gnu/Makefile.am
index 7ae93ad5..7590e12e 100644
--- a/builds/gnu/Makefile.am
+++ b/builds/gnu/Makefile.am
@@ -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 \
diff --git a/builds/msvc/vs2026/libbitcoin-node/libbitcoin-node.vcxproj b/builds/msvc/vs2026/libbitcoin-node/libbitcoin-node.vcxproj
index d01808ff..e1b32ddb 100644
--- a/builds/msvc/vs2026/libbitcoin-node/libbitcoin-node.vcxproj
+++ b/builds/msvc/vs2026/libbitcoin-node/libbitcoin-node.vcxproj
@@ -222,6 +222,7 @@
+
diff --git a/builds/msvc/vs2026/libbitcoin-node/libbitcoin-node.vcxproj.filters b/builds/msvc/vs2026/libbitcoin-node/libbitcoin-node.vcxproj.filters
index 43adfddf..409de67e 100644
--- a/builds/msvc/vs2026/libbitcoin-node/libbitcoin-node.vcxproj.filters
+++ b/builds/msvc/vs2026/libbitcoin-node/libbitcoin-node.vcxproj.filters
@@ -380,6 +380,9 @@
include\bitcoin\node
+
+ include\bitcoin\node\types
+
include\bitcoin\node\types
diff --git a/include/bitcoin/node.hpp b/include/bitcoin/node.hpp
index 7b07fade..4f1a2837 100644
--- a/include/bitcoin/node.hpp
+++ b/include/bitcoin/node.hpp
@@ -70,6 +70,7 @@
#include
#include
#include
+#include
#include
#include
#include
diff --git a/include/bitcoin/node/chase.hpp b/include/bitcoin/node/chase.hpp
index f612785c..58081638 100644
--- a/include/bitcoin/node/chase.hpp
+++ b/include/bitcoin/node/chase.hpp
@@ -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_,
diff --git a/include/bitcoin/node/chasers/chaser.hpp b/include/bitcoin/node/chasers/chaser.hpp
index fad6bee4..76996692 100644
--- a/include/bitcoin/node/chasers/chaser.hpp
+++ b/include/bitcoin/node/chasers/chaser.hpp
@@ -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.
/// -----------------------------------------------------------------------
diff --git a/include/bitcoin/node/chasers/chaser_template.hpp b/include/bitcoin/node/chasers/chaser_template.hpp
index d85d8521..8563fb48 100644
--- a/include/bitcoin/node/chasers/chaser_template.hpp
+++ b/include/bitcoin/node/chasers/chaser_template.hpp
@@ -19,6 +19,8 @@
#ifndef LIBBITCOIN_NODE_CHASERS_CHASER_TEMPLATE_HPP
#define LIBBITCOIN_NODE_CHASERS_CHASER_TEMPLATE_HPP
+#include
+#include
#include
#include
@@ -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
{
@@ -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 generation_{};
+
+ // These are protected by strand.
+ block_template::cptr held_{};
+ std::vector waiting_{};
+ network::deadline::ptr timer_{};
+ uint64_t built_{};
+ size_t moves_{};
+ size_t moved_{};
+ size_t demand_{};
+ bool building_{};
};
} // namespace node
diff --git a/include/bitcoin/node/chasers/chaser_transaction.hpp b/include/bitcoin/node/chasers/chaser_transaction.hpp
index ab03ecd9..7caaf8cd 100644
--- a/include/bitcoin/node/chasers/chaser_transaction.hpp
+++ b/include/bitcoin/node/chasers/chaser_transaction.hpp
@@ -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;
@@ -303,6 +306,7 @@ class BCN_API chaser_transaction
std::atomic_bool pooling_{};
std::atomic writers_{};
std::atomic reserved_{};
+ std::atomic generation_{};
const uint64_t outbound_excluded_;
const uint64_t inbound_excluded_;
diff --git a/include/bitcoin/node/full_node.hpp b/include/bitcoin/node/full_node.hpp
index f5f8a0f6..5216297a 100644
--- a/include/bitcoin/node/full_node.hpp
+++ b/include/bitcoin/node/full_node.hpp
@@ -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;
diff --git a/include/bitcoin/node/protocols/protocol.hpp b/include/bitcoin/node/protocols/protocol.hpp
index da5f10c9..8e7407d8 100644
--- a/include/bitcoin/node/protocols/protocol.hpp
+++ b/include/bitcoin/node/protocols/protocol.hpp
@@ -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,
diff --git a/include/bitcoin/node/protocols/protocol_transaction_in_106.hpp b/include/bitcoin/node/protocols/protocol_transaction_in_106.hpp
index e8ae9227..0be9f618 100644
--- a/include/bitcoin/node/protocols/protocol_transaction_in_106.hpp
+++ b/include/bitcoin/node/protocols/protocol_transaction_in_106.hpp
@@ -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(
- session->node_settings().pool.maximum_requests)),
+ session->network_settings().maximum_requests)),
request_timer_(system::emplace_shared(session->log,
- channel->strand(), session->node_settings().pool.request_timeout()))
+ channel->strand(), session->network_settings().request_timeout()))
{
}
diff --git a/include/bitcoin/node/sessions/session.hpp b/include/bitcoin/node/sessions/session.hpp
index f73dea30..cc5da4df 100644
--- a/include/bitcoin/node/sessions/session.hpp
+++ b/include/bitcoin/node/sessions/session.hpp
@@ -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,
diff --git a/include/bitcoin/node/settings.hpp b/include/bitcoin/node/settings.hpp
index 54d25a61..fc1f5cd0 100644
--- a/include/bitcoin/node/settings.hpp
+++ b/include/bitcoin/node/settings.hpp
@@ -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;
};
diff --git a/include/bitcoin/node/types/block_template.hpp b/include/bitcoin/node/types/block_template.hpp
new file mode 100644
index 00000000..c42f683c
--- /dev/null
+++ b/include/bitcoin/node/types/block_template.hpp
@@ -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 .
+ */
+#ifndef LIBBITCOIN_NODE_TYPES_BLOCK_TEMPLATE_HPP
+#define LIBBITCOIN_NODE_TYPES_BLOCK_TEMPLATE_HPP
+
+#include
+#include
+
+namespace libbitcoin {
+namespace node {
+
+/// Template of the next block, its txs parents first.
+struct block_template
+{
+ typedef std::shared_ptr 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
diff --git a/include/bitcoin/node/types/type.hpp b/include/bitcoin/node/types/type.hpp
index 860f9653..29adc9a4 100644
--- a/include/bitcoin/node/types/type.hpp
+++ b/include/bitcoin/node/types/type.hpp
@@ -21,6 +21,7 @@
#include
#include
+#include
#include
namespace libbitcoin {
@@ -45,6 +46,8 @@ typedef std::function template_handler;
+typedef std::function block_template_handler;
/// Confirmable types.
typedef std::function
#include
#include
#include
diff --git a/src/chasers/chaser.cpp b/src/chasers/chaser.cpp
index 6b4881a2..324a3aa2 100644
--- a/src/chasers/chaser.cpp
+++ b/src/chasers/chaser.cpp
@@ -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);
diff --git a/src/chasers/chaser_template.cpp b/src/chasers/chaser_template.cpp
index b3adeda4..b0280039 100644
--- a/src/chasers/chaser_template.cpp
+++ b/src/chasers/chaser_template.cpp
@@ -18,6 +18,8 @@
*/
#include
+#include
+#include
#include
#include
#include
@@ -28,6 +30,8 @@ namespace node {
#define CLASS chaser_template
using namespace system;
+using namespace network;
+using namespace std::chrono;
using namespace std::placeholders;
BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)
@@ -37,18 +41,92 @@ chaser_template::chaser_template(full_node& node) NOEXCEPT
{
}
-// start
+// start/stop
// ----------------------------------------------------------------------------
code chaser_template::start() NOEXCEPT
{
+ // Construct is too early to create the unstarted timer.
+ timer_ = emplace_shared(log, strand());
+
+ SUBSCRIBE_CHASE(handle_chase, _1, _2);
return error::success;
}
+void chaser_template::stopping(const code& ec) NOEXCEPT
+{
+ POST(do_stopping, ec);
+}
+
+void chaser_template::do_stopping(const code&) NOEXCEPT
+{
+ BC_ASSERT(stranded());
+
+ if (timer_)
+ {
+ timer_->stop();
+ timer_.reset();
+ }
+
+ complete(network::error::service_stopped);
+}
+
+// event handlers
+// ----------------------------------------------------------------------------
+
+bool chaser_template::handle_chase(const code&, event_value value) NOEXCEPT
+{
+ if (closed())
+ return false;
+
+ switch (to_chase(value))
+ {
+ case chase::organized:
+ case chase::reorganized:
+ {
+ POST(do_moved, false);
+ break;
+ }
+ case chase::block_settled:
+ {
+ POST(do_moved, true);
+ break;
+ }
+ case chase::stop:
+ {
+ return false;
+ }
+ default:
+ {
+ break;
+ }
+ }
+
+ return true;
+}
+
+// The settled block has been applied to the pool, so its template is built
+// if demanded, and otherwise upon the next request.
+void chaser_template::do_moved(bool settled) NOEXCEPT
+{
+ BC_ASSERT(stranded());
+ if (closed())
+ return;
+
+ ++moves_;
+ if (settled && is_nonzero(demand_))
+ build();
+}
+
// methods
// ----------------------------------------------------------------------------
-void chaser_template::get_template(template_handler&& handler) NOEXCEPT
+uint64_t chaser_template::template_generation() const NOEXCEPT
+{
+ return generation_.load(std::memory_order_relaxed);
+}
+
+void chaser_template::get_template(block_template_handler&& handler) NOEXCEPT
{
if (closed())
return;
@@ -56,50 +134,224 @@ void chaser_template::get_template(template_handler&& handler) NOEXCEPT
POST(do_get_template, std::move(handler));
}
-void chaser_template::do_get_template(const template_handler& handler) NOEXCEPT
+void chaser_template::do_get_template(
+ const block_template_handler& handler) NOEXCEPT
+{
+ BC_ASSERT(stranded());
+
+ if (closed())
+ {
+ handler(network::error::service_stopped, {});
+ return;
+ }
+
+ if (held_ && (moved_ == moves_) && (built_ == pool_generation()))
+ {
+ handler(error::success, held_);
+ return;
+ }
+
+ waiting_.push_back(handler);
+ build();
+}
+
+void chaser_template::demand_template(bool demand) NOEXCEPT
+{
+ if (closed())
+ return;
+
+ POST(do_demand_template, demand);
+}
+
+void chaser_template::do_demand_template(bool demand) NOEXCEPT
{
BC_ASSERT(stranded());
- request_template(BIND(handle_build, _1, _2, _3, handler));
+
+ if (demand)
+ {
+ if (is_zero(demand_++))
+ start_timer();
+ }
+ else if (is_nonzero(demand_) && is_zero(--demand_) && timer_)
+ {
+ timer_->stop();
+ }
}
// build
// ----------------------------------------------------------------------------
+// private
+void chaser_template::build() NOEXCEPT
+{
+ BC_ASSERT(stranded());
+ if (building_)
+ return;
+
+ building_ = true;
+ request_template(
+ BIND(handle_build, _1, _2, _3, pool_generation(), moves_));
+}
+
// private
void chaser_template::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
{
- POST(do_build, ec, height, links, handler);
+ POST(do_build, ec, height, links, generation, moves);
}
// private
+// A build that the chain moved under is not of its top, so is built again.
void chaser_template::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
{
BC_ASSERT(stranded());
+ building_ = false;
if (closed())
{
- handler(network::error::service_stopped, {}, {});
+ complete(network::error::service_stopped);
return;
}
if (ec == network::error::service_stopped ||
ec == error::pooling_disabled)
{
- handler(ec, {}, {});
+ held_.reset();
+ complete(ec);
return;
}
if (ec)
{
- handler(fault(ec), {}, {});
+ complete(fault(ec));
+ return;
+ }
+
+ if (moves != moves_)
+ {
+ build();
return;
}
+ built_ = generation;
+ if (const auto failed = replace(height, links))
+ {
+ complete(fault(failed));
+ return;
+ }
+
+ moved_ = moves;
+ complete(error::success);
+}
+
+// private
+// The template held is replaced when it is not of the chain top, or when the
+// fees of the build exceed its own by more than the configured increase.
+code chaser_template::replace(size_t height,
+ const database::tx_links& links) NOEXCEPT
+{
+ BC_ASSERT(stranded());
+ const auto& query = archive();
+
+ uint64_t fees{};
+ for (const auto& link: links)
+ {
+ query::pool_totals totals{};
+ if (!query.get_pooled_totals(totals, link))
+ return database::error::integrity;
+
+ fees = ceilinged_add(fees, totals.fee);
+ }
+
+ const auto increase = node_settings().pool.template_fee_increase;
+ const auto current = held_ && (moved_ == moves_) &&
+ (held_->height == height);
+
+ if (current && (floored_subtract(fees, held_->fees) <= increase))
+ return error::success;
+
+ // Leaves of the coinbase are null, as it is not of the template.
+ hashes txids{ null_hash };
+ hashes wtxids{ null_hash };
+ txids.reserve(add1(links.size()));
+ wtxids.reserve(add1(links.size()));
+ for (const auto& link: links)
+ {
+ const auto row = query.to_pool_row(link);
+ if (row.is_terminal())
+ return database::error::integrity;
+
+ txids.push_back(query.get_tx_key(link));
+ wtxids.push_back(query.get_wtxid_key(row));
+ }
+
+ const auto root = merkle_root(std::move(wtxids));
+ const auto sequence = add1(template_generation());
+ held_ = to_shared(block_template
+ {
+ height,
+ sequence,
+ fees,
+ links,
+ sha256::double_hash(root, null_hash),
+ chain::block::merkle_branch(zero, std::move(txids))
+ });
+
+ generation_.store(sequence, std::memory_order_relaxed);
fire(events::template_issued, links.size());
notify(error::success, chases::template_{ height });
- handler(error::success, height, links);
+ return error::success;
+}
+
+// private
+void chaser_template::complete(const code& ec) NOEXCEPT
+{
+ BC_ASSERT(stranded());
+ const auto handlers = std::exchange(waiting_, {});
+ for (const auto& handler: handlers)
+ handler(ec, ec ? block_template::cptr{} : held_);
+}
+
+// timer
+// ----------------------------------------------------------------------------
+
+// private
+void chaser_template::start_timer() NOEXCEPT
+{
+ BC_ASSERT(stranded());
+
+ const auto interval = node_settings().pool.template_interval();
+ if (!timer_ || is_zero(interval.count()))
+ return;
+
+ timer_->start(BIND(handle_timer, _1),
+ duration_cast(interval));
+}
+
+// private
+void chaser_template::handle_timer(const code& ec) NOEXCEPT
+{
+ BC_ASSERT(stranded());
+
+ if (closed() || !timer_ || ec == network::error::operation_canceled)
+ return;
+
+ if (ec && ec != network::error::operation_timeout)
+ {
+ LOGF("Template chaser timer fault, " << ec.message());
+ return;
+ }
+
+ if (is_zero(demand_))
+ return;
+
+ if ((moved_ != moves_) || (built_ != pool_generation()))
+ build();
+
+ start_timer();
}
BC_POP_WARNING()
diff --git a/src/chasers/chaser_transaction.cpp b/src/chasers/chaser_transaction.cpp
index e09c6753..37fc4fa3 100644
--- a/src/chasers/chaser_transaction.cpp
+++ b/src/chasers/chaser_transaction.cpp
@@ -479,6 +479,7 @@ void chaser_transaction::do_organized(header_t link) NOEXCEPT
}
top_ = link;
+ ++generation_;
do_bump();
}
@@ -570,6 +571,7 @@ bool chaser_transaction::repool(header_t link) NOEXCEPT
return false;
}
+ ++generation_;
notify(error::success, chases::transaction{ fk });
broadcast(*tx);
}
diff --git a/src/chasers/chaser_transaction_compact.cpp b/src/chasers/chaser_transaction_compact.cpp
index 2d655605..221ecbcf 100644
--- a/src/chasers/chaser_transaction_compact.cpp
+++ b/src/chasers/chaser_transaction_compact.cpp
@@ -223,6 +223,7 @@ code chaser_transaction::fill(database::tx_link& out,
ctx.is_enabled(flags::bip341_rule) && !query.set_silent(out, tx))
return fault(error::transaction6);
+ ++generation_;
notify(error::success, chases::transaction{ out });
broadcast(tx);
return error::success;
@@ -251,6 +252,7 @@ code chaser_transaction::fill(database::tx_link& out,
if (!coinbase)
{
fire(events::tx_archived, to_rate(tx));
+ ++generation_;
notify(error::success, chases::transaction{ out });
broadcast(tx);
}
diff --git a/src/chasers/chaser_transaction_query.cpp b/src/chasers/chaser_transaction_query.cpp
index 88155b65..85f818be 100644
--- a/src/chasers/chaser_transaction_query.cpp
+++ b/src/chasers/chaser_transaction_query.cpp
@@ -82,6 +82,11 @@ uint64_t chaser_transaction::arriving(const query::pool_arrivals& arrivals,
}
}
+uint64_t chaser_transaction::pool_generation() const NOEXCEPT
+{
+ return generation_.load(std::memory_order_relaxed);
+}
+
void chaser_transaction::build_template(template_handler&& handler) NOEXCEPT
{
if (closed())
diff --git a/src/chasers/chaser_transaction_submit.cpp b/src/chasers/chaser_transaction_submit.cpp
index 9f5c7bec..4ff11585 100644
--- a/src/chasers/chaser_transaction_submit.cpp
+++ b/src/chasers/chaser_transaction_submit.cpp
@@ -486,6 +486,7 @@ code chaser_transaction::admit(database::tx_link& link,
return fault(error::transaction6);
fire(events::tx_archived, to_rate(tx));
+ ++generation_;
notify(error::success, chases::transaction{ link });
broadcast(tx);
return error::success;
diff --git a/src/full_node.cpp b/src/full_node.cpp
index 7fd1a1ad..33e6ec63 100644
--- a/src/full_node.cpp
+++ b/src/full_node.cpp
@@ -233,11 +233,26 @@ void full_node::subscribe_pool(pool_notifier&& handler) NOEXCEPT
chaser_transaction_.subscribe_pool(std::move(handler));
}
-void full_node::get_template(template_handler&& handler) NOEXCEPT
+uint64_t full_node::pool_generation() const NOEXCEPT
+{
+ return chaser_transaction_.pool_generation();
+}
+
+void full_node::get_template(block_template_handler&& handler) NOEXCEPT
{
chaser_template_.get_template(std::move(handler));
}
+void full_node::demand_template(bool demand) NOEXCEPT
+{
+ chaser_template_.demand_template(demand);
+}
+
+uint64_t full_node::template_generation() const NOEXCEPT
+{
+ return chaser_template_.template_generation();
+}
+
void full_node::get_hashes(map_handler&& handler) NOEXCEPT
{
chaser_download_.get_hashes(std::move(handler));
diff --git a/src/protocols/protocol.cpp b/src/protocols/protocol.cpp
index 3a63a3bd..75e34aa9 100644
--- a/src/protocols/protocol.cpp
+++ b/src/protocols/protocol.cpp
@@ -95,11 +95,21 @@ size_t protocol::inbound_channel_count() const NOEXCEPT
// Methods.
// ----------------------------------------------------------------------------
-void protocol::get_template(template_handler&& handler) NOEXCEPT
+void protocol::get_template(block_template_handler&& handler) NOEXCEPT
{
session_->get_template(std::move(handler));
}
+void protocol::demand_template(bool demand) NOEXCEPT
+{
+ session_->demand_template(demand);
+}
+
+uint64_t protocol::template_generation() const NOEXCEPT
+{
+ return session_->template_generation();
+}
+
void protocol::estimate(size_t target, estimate_mode mode,
estimate_handler&& handler) NOEXCEPT
{
diff --git a/src/sessions/session.cpp b/src/sessions/session.cpp
index 561ecc77..14d6367f 100644
--- a/src/sessions/session.cpp
+++ b/src/sessions/session.cpp
@@ -202,11 +202,21 @@ void session::unsubscribe_chase(object_key key) NOEXCEPT
// Methods.
// ----------------------------------------------------------------------------
-void session::get_template(template_handler&& handler) NOEXCEPT
+void session::get_template(block_template_handler&& handler) NOEXCEPT
{
node_.get_template(std::move(handler));
}
+void session::demand_template(bool demand) NOEXCEPT
+{
+ node_.demand_template(demand);
+}
+
+uint64_t session::template_generation() const NOEXCEPT
+{
+ return node_.template_generation();
+}
+
void session::estimate(size_t target, estimate_mode mode,
estimate_handler&& handler) NOEXCEPT
{
diff --git a/src/settings.cpp b/src/settings.cpp
index 76baef07..934e0833 100644
--- a/src/settings.cpp
+++ b/src/settings.cpp
@@ -37,8 +37,8 @@ settings::transaction_pool::transaction_pool() NOEXCEPT
maximum_chunk_depth{ 25 },
estimate_cache_seconds{ 7 },
estimate_room{ 1'000 },
- maximum_requests{ 500'000 },
- request_timeout_seconds{ 60 },
+ template_interval_seconds{ 30 },
+ template_fee_increase{ 0 },
thread_priority{ true }
{
}
@@ -65,9 +65,9 @@ settings::transaction_pool::estimate_cache() const NOEXCEPT
}
network::steady_clock::duration
-settings::transaction_pool::request_timeout() const NOEXCEPT
+settings::transaction_pool::template_interval() const NOEXCEPT
{
- return network::seconds(request_timeout_seconds);
+ return network::seconds(template_interval_seconds);
}
network::processing_priority
diff --git a/test/chasers/chaser_template.cpp b/test/chasers/chaser_template.cpp
index 37c5c6b1..72642da4 100644
--- a/test/chasers/chaser_template.cpp
+++ b/test/chasers/chaser_template.cpp
@@ -104,12 +104,13 @@ struct chaser_template_setup_fixture
{
using built = std::tuple;
- inline chaser_template_setup_fixture()
- : p2p_setup_fixture(stored_parent, [](configuration& config)
+ inline chaser_template_setup_fixture(uint64_t increase=zero)
+ : p2p_setup_fixture(stored_parent, [=](configuration& config)
{
using deployment = chain::chain_state::deployment;
config.network.enable_relay = true;
config.node.currency_window_minutes = 0;
+ config.node.pool.template_fee_increase = increase;
constexpr auto always = deployment::always_active;
config.bitcoin.bip9_bit0_deployment.start = always;
})
@@ -128,19 +129,35 @@ struct chaser_template_setup_fixture
return promise.get_future().get();
}
+ // The template held upon request, null if none.
+ node::block_template::cptr held()
+ {
+ std::promise promise{};
+ node_.get_template([&](const code&,
+ const node::block_template::cptr& held) NOEXCEPT
+ {
+ promise.set_value(held);
+ });
+
+ return promise.get_future().get();
+ }
+
built get_template()
{
std::promise promise{};
- node_.get_template([&](const code& ec, size_t height,
- const database::tx_links& links) NOEXCEPT
+ node_.get_template([&](const code& ec,
+ const node::block_template::cptr& held) NOEXCEPT
{
- promise.set_value({ ec, height, links });
+ if (held)
+ promise.set_value({ ec, held->height, held->links });
+ else
+ promise.set_value({ ec, zero, {} });
});
return promise.get_future().get();
}
- // The links of a template built upon request.
+ // The links of the template held upon request.
database::tx_links requested()
{
return std::get<2>(get_template());
@@ -160,6 +177,16 @@ struct chaser_template_setup_fixture
}
};
+// A template is replaced only by fees of more than a thousand over its own.
+struct chaser_template_increase_setup_fixture
+ : chaser_template_setup_fixture
+{
+ inline chaser_template_increase_setup_fixture()
+ : chaser_template_setup_fixture(1'000)
+ {
+ }
+};
+
BC_FIXTURE_TEST_SUITE(chaser_template_tests, chaser_template_setup_fixture)
BC_AUTO_TEST_CASE(chaser_template__get_template__started__empty)
@@ -228,6 +255,61 @@ BC_AUTO_TEST_CASE(chaser_template__get_template__child_of_losing_rival__rival_on
BC_REQUIRE(requested() == database::tx_links{ link(second) });
}
+BC_AUTO_TEST_CASE(chaser_template__get_template__first__sequence_one)
+{
+ BC_REQUIRE_EQUAL(node_.template_generation(), 0u);
+ BC_REQUIRE_EQUAL(held()->sequence, 1u);
+ BC_REQUIRE_EQUAL(node_.template_generation(), 1u);
+}
+
+BC_AUTO_TEST_CASE(chaser_template__get_template__pool_unchanged__same_template)
+{
+ const auto first = held();
+ BC_REQUIRE(held() == first);
+}
+
+BC_AUTO_TEST_CASE(chaser_template__get_template__fee_pooled__replaced_with_fees)
+{
+ const auto first = held();
+ const auto ec = submit(package({ spend(parent_value - 500u) }));
+ const auto second = held();
+ BC_REQUIRE_EQUAL(ec, node::error::success);
+ BC_REQUIRE_EQUAL(first->fees, 0u);
+ BC_REQUIRE_EQUAL(second->sequence, 2u);
+ BC_REQUIRE_EQUAL(second->fees, 500u);
+}
+
+BC_AUTO_TEST_CASE(chaser_template__get_template__empty__null_commitment_no_branch)
+{
+ const auto empty = held();
+ BC_REQUIRE(empty->branch.empty());
+ BC_REQUIRE_EQUAL(empty->commitment, sha256::double_hash(null_hash, null_hash));
+}
+
+BC_AUTO_TEST_CASE(chaser_template__get_template__single__branch_of_txid)
+{
+ const auto tx = spend(parent_value);
+ const auto ec = submit(package({ tx }));
+ BC_REQUIRE_EQUAL(ec, node::error::success);
+ BC_REQUIRE(held()->branch == hashes{ tx->hash(false) });
+}
+
+BC_FIXTURE_TEST_CASE(chaser_template__get_template__fees_at_increase__not_replaced, chaser_template_increase_setup_fixture)
+{
+ const auto first = held();
+ const auto ec = submit(package({ spend(parent_value - 1'000u) }));
+ BC_REQUIRE_EQUAL(ec, node::error::success);
+ BC_REQUIRE(held() == first);
+}
+
+BC_FIXTURE_TEST_CASE(chaser_template__get_template__fees_over_increase__replaced, chaser_template_increase_setup_fixture)
+{
+ const auto first = held();
+ const auto ec = submit(package({ spend(parent_value - 1'001u) }));
+ BC_REQUIRE_EQUAL(ec, node::error::success);
+ BC_REQUIRE_EQUAL(held()->sequence, 2u);
+}
+
BC_AUTO_TEST_SUITE_END()
BC_POP_WARNING()
diff --git a/test/chasers/chaser_transaction_setup_fixture.hpp b/test/chasers/chaser_transaction_setup_fixture.hpp
index 56f1528c..79c5dd53 100644
--- a/test/chasers/chaser_transaction_setup_fixture.hpp
+++ b/test/chasers/chaser_transaction_setup_fixture.hpp
@@ -889,8 +889,8 @@ struct chaser_transaction_requests_setup_fixture
{
config.network.enable_relay = true;
config.node.currency_window_minutes = 0;
- config.node.pool.maximum_requests = 2;
- config.node.pool.request_timeout_seconds = 0;
+ config.network.maximum_requests = 2;
+ config.network.request_timeout_seconds = 0;
})
{
}
@@ -904,7 +904,7 @@ struct chaser_transaction_request_timeout_setup_fixture
{
config.network.enable_relay = true;
config.node.currency_window_minutes = 0;
- config.node.pool.request_timeout_seconds = 1;
+ config.network.request_timeout_seconds = 1;
})
{
}
diff --git a/test/settings.cpp b/test/settings.cpp
index 8fe55d55..c1ce916d 100644
--- a/test/settings.cpp
+++ b/test/settings.cpp
@@ -220,9 +220,9 @@ BC_AUTO_TEST_CASE(settings__pool__default__expected)
BC_REQUIRE_EQUAL(pool.estimate_cache_seconds, 7_u16);
BC_REQUIRE_EQUAL(pool.estimate_room, 1'000_u32);
BC_REQUIRE(pool.estimate_cache() == steady_clock::duration(seconds(7)));
- BC_REQUIRE_EQUAL(pool.maximum_requests, 500'000_u32);
- BC_REQUIRE_EQUAL(pool.request_timeout_seconds, 60_u16);
- BC_REQUIRE(pool.request_timeout() == steady_clock::duration(seconds(60)));
+ BC_REQUIRE_EQUAL(pool.template_interval_seconds, 30_u16);
+ BC_REQUIRE_EQUAL(pool.template_fee_increase, 0_u64);
+ BC_REQUIRE(pool.template_interval() == steady_clock::duration(seconds(30)));
BC_REQUIRE(pool.thread_priority);
BC_REQUIRE(pool.thread_priority_() == network::processing_priority::high);
}