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
2 changes: 1 addition & 1 deletion include/bitcoin/node/chase.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ enum class chase
block,

/// The store has been pruned (once, following the first current block).
/// Issued by 'snapshot' and handled by none.
/// Issued by 'snapshot' and handled by 'transaction'.
pruned,

/// The confirmed chain is no longer current.
Expand Down
6 changes: 6 additions & 0 deletions include/bitcoin/node/chasers/chaser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,12 @@ class BCN_API chaser
/// Reset store disk full condition.
virtual code reload(const store::event_handler& handler) NOEXCEPT;

/// Mining.
/// -----------------------------------------------------------------------

/// Build a template of pooled txs, parents first, and its height.
virtual void request_template(template_handler&& handler) NOEXCEPT;

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

Expand Down
19 changes: 18 additions & 1 deletion include/bitcoin/node/chasers/chaser_template.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,27 @@ class BCN_API chaser_template

code start() NOEXCEPT override;

/// The latest template of pooled txs, parents first, and its height.
virtual void get_template(template_handler&& handler) NOEXCEPT;

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

virtual void do_transaction(transaction_t value) NOEXCEPT;
virtual void do_update() NOEXCEPT;
virtual void do_get_template(const template_handler& handler) NOEXCEPT;

private:
void build() NOEXCEPT;
void handle_build(const code& ec, size_t height,
const database::tx_links& links) NOEXCEPT;
void do_build(const code& ec, size_t height,
const database::tx_links& links) NOEXCEPT;

// These are protected by strand.
database::tx_links links_{};
size_t height_{};
bool building_{};
bool stale_{};
};

} // namespace node
Expand Down
20 changes: 20 additions & 0 deletions include/bitcoin/node/chasers/chaser_transaction.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ class BCN_API chaser_transaction
const database::tx_links& links, const database::header_link& link,
submit_handler&& handler) NOEXCEPT;

/// Build a template of pooled txs, parents first, and its height.
virtual void build_template(template_handler&& handler) NOEXCEPT;

/// Lowest rate (per virtual kilobyte) confirmed by the target block.
virtual void estimate(size_t target, estimate_mode mode,
estimate_handler&& handler) NOEXCEPT;

protected:
using chaser::broadcast;

Expand All @@ -62,6 +69,9 @@ class BCN_API chaser_transaction
const system::chain::transactions_cptr& txs,
const database::tx_links& links, const database::header_link& link,
const submit_handler& handler) NOEXCEPT;
virtual void do_build_template(const template_handler& handler) NOEXCEPT;
virtual void do_estimate(size_t target, estimate_mode mode,
const estimate_handler& handler) NOEXCEPT;

/// Recompute the pool context, closing the pool if not current, and map
/// scan starts: built once current, extended by block until pooling, and
Expand All @@ -71,6 +81,9 @@ class BCN_API chaser_transaction
/// Pool the unpooled txs of a reorganized block.
virtual void do_reorganized(header_t link) NOEXCEPT;

/// Raise the arrivals floor above the pruned block.
virtual void do_pruned(header_t link) NOEXCEPT;

/// Map scan starts from the confirmed top (scan threadpool).
virtual void do_scan(const database::header_link& top, size_t bound,
bool remap) NOEXCEPT;
Expand All @@ -80,6 +93,7 @@ class BCN_API chaser_transaction

private:
static size_t to_rate(const system::chain::transaction& tx) NOEXCEPT;
system::pool::algorithm::totals_t block_limit() const NOEXCEPT;

code validate(size_t& index, const system::chain::transaction_cptrs& txs,
const std::vector<bool>& stored) NOEXCEPT;
Expand All @@ -94,15 +108,21 @@ class BCN_API chaser_transaction
const code& ec) NOEXCEPT;
void broadcast(const system::chain::transaction& tx) const NOEXCEPT;
void start_scan(bool remap) NOEXCEPT;
uint64_t arriving(uint64_t rate, estimate_mode mode) const NOEXCEPT;

// These are thread safe.
network::threadpool scan_threadpool_;
stopper stopping_{};

// These are protected by strand.
system::chain::context pool_{};
query::pool_projection projection_{};
query::pool_arrivals arrivals_{};
size_t floor_{};
bool pooling_{};
bool scanning_{};
bool projected_{};
bool arrived_{};
};

} // namespace node
Expand Down
16 changes: 16 additions & 0 deletions include/bitcoin/node/define.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,25 @@ typedef std::error_code code;
/// Estimate types.
enum class estimate_mode
{
/// The pool as it is, with no arrivals.
basic,

/// Arrivals weighted by recency, each block half of the one above it.
geometric,

/// Arrivals at their mean over the economical window.
economical,

/// Arrivals at their peak over the conservative window.
conservative,

unknown
};
constexpr size_t maximum_estimate_horizon = 1008;

/// Blocks of arrivals (two hours and one day at ten minutes per block).
constexpr size_t economical_estimate_window = 12;
constexpr size_t conservative_estimate_window = 144;
typedef std::function<void(const code&, uint64_t)> estimate_handler;

/// Organization types.
Expand All @@ -58,6 +70,10 @@ typedef std::shared_ptr<database::associations> map_ptr;
typedef std::function<void(const code&, const map_ptr&,
const job::ptr&)> map_handler;

/// Mining types.
typedef std::function<void(const code&, size_t,
const database::tx_links&)> template_handler;

/// Event desubscriber.
typedef network::desubscriber<object_key, event_value> event_subscriber;
typedef event_subscriber::handler event_notifier;
Expand Down
8 changes: 7 additions & 1 deletion include/bitcoin/node/full_node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,12 @@ class BCN_API full_node
const database::tx_links& links, const database::header_link& link,
submit_handler&& handler) NOEXCEPT;

/// Build a template of pooled txs, parents first, and its height.
virtual void build_template(template_handler&& handler) NOEXCEPT;

/// The latest template of pooled txs, parents first, and its height.
virtual void get_template(template_handler&& handler) NOEXCEPT;

/// Manage download queue.
virtual void get_hashes(map_handler&& handler) NOEXCEPT;
virtual void put_hashes(const map_ptr& map,
Expand Down Expand Up @@ -170,7 +176,7 @@ class BCN_API full_node
/// Methods.
/// -----------------------------------------------------------------------

/// Get current fee estimate.
/// Lowest rate (per virtual kilobyte) confirmed by the target block.
void estimate(size_t target, estimate_mode mode,
estimate_handler&& handler) NOEXCEPT;

Expand Down
2 changes: 1 addition & 1 deletion include/bitcoin/node/protocols/protocol.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ class BCN_API protocol
virtual void connect(const network::config::endpoint& endpoint,
network::net::channel_notifier&& handler) NOEXCEPT;

/// Get current fee estimate.
/// Lowest rate (per virtual kilobyte) confirmed by the target block.
void estimate(size_t target, estimate_mode mode,
estimate_handler&& handler) NOEXCEPT;

Expand Down
2 changes: 1 addition & 1 deletion include/bitcoin/node/sessions/session.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ class BCN_API session
virtual void connect(const network::config::endpoint& endpoint,
network::net::channel_notifier&& handler) NOEXCEPT;

/// Get current fee estimate.
/// Lowest rate (per virtual kilobyte) confirmed by the target block.
void estimate(size_t target, estimate_mode mode,
estimate_handler&& handler) NOEXCEPT;

Expand Down
22 changes: 20 additions & 2 deletions include/bitcoin/node/settings.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,29 @@ class BCN_API settings
settings() NOEXCEPT;
settings(system::chain::selection context) NOEXCEPT;

/// [pool] settings.
struct BCN_API transaction_pool
{
DEFAULT_COPY_MOVE_DESTRUCT(transaction_pool);
transaction_pool() NOEXCEPT;

/// Properties.
double minimum_fee_rate;
uint32_t maximum_outcomes;
uint32_t bytes_per_sigop;
uint32_t coinbase_weight;
uint32_t maximum_chunk_depth;

/// Helpers.
virtual uint64_t minimum_fee_rate_() const NOEXCEPT;
};

/// The network of the defaults.
system::chain::selection network;

/// The transaction pool.
transaction_pool pool;

/// Properties.
uint32_t threads;
bool delay_inbound;
Expand All @@ -51,7 +71,6 @@ class BCN_API settings
bool cpu_batch_verify;
bool cpu_batch_silent;
float allowed_deviation;
double minimum_fee_rate;
uint64_t batch_verify;
uint64_t batch_silent;
uint16_t current_connections;
Expand All @@ -70,7 +89,6 @@ class BCN_API settings
virtual size_t threads_() const NOEXCEPT;
virtual size_t maximum_height_() const NOEXCEPT;
virtual size_t maximum_concurrency_() const NOEXCEPT;
virtual uint64_t minimum_fee_rate_() const NOEXCEPT;
virtual uint64_t services_provided() const NOEXCEPT;
virtual uint64_t services_required() const NOEXCEPT;
virtual bool use_gpu_() const NOEXCEPT;
Expand Down
5 changes: 5 additions & 0 deletions src/chasers/chaser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ void chaser::resume() NOEXCEPT
node_.resume();
}

void chaser::request_template(template_handler&& handler) NOEXCEPT
{
node_.build_template(std::move(handler));
}

code chaser::prune(const store::event_handler& handler) NOEXCEPT
{
return node_.prune(handler);
Expand Down
81 changes: 76 additions & 5 deletions src/chasers/chaser_template.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,23 @@ chaser_template::chaser_template(full_node& node) NOEXCEPT
// start
// ----------------------------------------------------------------------------

// TODO: initialize template state.
code chaser_template::start() NOEXCEPT
{
SUBSCRIBE_CHASE(handle_chase, _1, _2);
return error::success;
}

// methods
// ----------------------------------------------------------------------------

void chaser_template::get_template(template_handler&& handler) NOEXCEPT
{
if (closed())
return;

POST(do_get_template, std::move(handler));
}

// event handlers
// ----------------------------------------------------------------------------

Expand All @@ -60,12 +70,13 @@ bool chaser_template::handle_chase(const code&, event_value value) NOEXCEPT
if (suspended())
return true;

// TODO: also handle confirmed/unconfirmed.
switch (to_chase(value))
{
case chase::transaction:
case chase::organized:
case chase::reorganized:
{
POST(do_transaction, to_payload<chase::transaction>(value).link);
POST(do_update);
break;
}
case chase::stop:
Expand All @@ -81,10 +92,70 @@ bool chaser_template::handle_chase(const code&, event_value value) NOEXCEPT
return true;
}

// TODO: handle transaction graph change (may issue 'candidate').
void chaser_template::do_transaction(transaction_t) NOEXCEPT
// Pool changes during a build coalesce into one following build.
void chaser_template::do_update() NOEXCEPT
{
BC_ASSERT(stranded());

if (building_)
{
stale_ = true;
return;
}

build();
}

void chaser_template::do_get_template(const template_handler& handler) NOEXCEPT
{
BC_ASSERT(stranded());
handler(error::success, height_, links_);
}

// build
// ----------------------------------------------------------------------------

// private
void chaser_template::build() NOEXCEPT
{
BC_ASSERT(stranded());

building_ = true;
stale_ = false;
request_template(BIND(handle_build, _1, _2, _3));
}

// private
void chaser_template::handle_build(const code& ec, size_t height,
const database::tx_links& links) NOEXCEPT
{
POST(do_build, ec, height, links);
}

// private
void chaser_template::do_build(const code& ec, size_t height,
const database::tx_links& links) NOEXCEPT
{
BC_ASSERT(stranded());

building_ = false;
if (closed() ||
ec == network::error::service_stopped ||
ec == error::pooling_disabled)
return;

if (ec)
{
fault(ec);
return;
}

links_ = links;
height_ = height;
notify(error::success, chases::template_{ height });

if (stale_)
build();
}

BC_POP_WARNING()
Expand Down
Loading
Loading