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
329 changes: 292 additions & 37 deletions include/bitcoin/node/chase.hpp

Large diffs are not rendered by default.

5 changes: 2 additions & 3 deletions include/bitcoin/node/chasers/chaser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,10 @@ class BCN_API chaser
virtual object_key subscribe_chase(event_notifier&& handler) NOEXCEPT;

/// Set event (does not require node strand).
virtual void notify(const code& ec, chase event_,
event_value value) const NOEXCEPT;
virtual void notify(const code& ec, event_value value) const NOEXCEPT;

/// Set event to one subscriber (does not require node strand).
virtual void notify_one(object_key key, const code& ec, chase event_,
virtual void notify_one(object_key key, const code& ec,
event_value value) const NOEXCEPT;

/// Strand.
Expand Down
3 changes: 1 addition & 2 deletions include/bitcoin/node/chasers/chaser_check.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@ class BCN_API chaser_check

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

/// block tracking
virtual void do_bump(height_t height) NOEXCEPT;
Expand Down
3 changes: 1 addition & 2 deletions include/bitcoin/node/chasers/chaser_confirm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ class BCN_API chaser_confirm
using header_links = database::header_links;
using header_states = database::header_states;

virtual bool handle_chase(const code& ec, chase event_,
event_value value) NOEXCEPT;
virtual bool handle_chase(const code& ec, event_value value) NOEXCEPT;

virtual void do_regressed(height_t branch_point) NOEXCEPT;
virtual void do_validated(height_t height) NOEXCEPT;
Expand Down
3 changes: 1 addition & 2 deletions include/bitcoin/node/chasers/chaser_estimate.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ class BCN_API chaser_estimate
bool initialized() const NOEXCEPT;

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

virtual void do_initialize(header_t link) NOEXCEPT;
virtual void do_organized(header_t link) NOEXCEPT;
Expand Down
21 changes: 14 additions & 7 deletions include/bitcoin/node/chasers/chaser_organize.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,7 @@ class chaser_organize
/// -----------------------------------------------------------------------

/// Handle chaser events.
virtual bool handle_chase(const code&, chase event_,
event_value value) NOEXCEPT;
virtual bool handle_chase(const code&, event_value value) NOEXCEPT;

/// Organize a discovered Block, prioritized accepts a tied branch.
virtual void do_organize(typename Block::cptr block, bool prioritized,
Expand Down Expand Up @@ -120,32 +119,40 @@ class chaser_organize
// Template differentiators.
// ------------------------------------------------------------------------

static constexpr bool is_block_
{
is_same_type<Block, system::chain::block>
};

using chase_object = iif<is_block_, chases::blocks, chases::headers>;

static constexpr bool is_block() NOEXCEPT
{
return is_same_type<Block, system::chain::block>;
return is_block_;
}

static constexpr auto error_duplicate() NOEXCEPT
{
return is_block() ? error::duplicate_block : error::duplicate_header;
}

static constexpr auto error_orphan() NOEXCEPT
{
return is_block() ? error::orphan_block : error::orphan_header;
}
static constexpr auto chase_object() NOEXCEPT
{
return is_block() ? chase::blocks : chase::headers;
}

static constexpr auto events_object_archived() NOEXCEPT
{
return is_block() ? events::block_archived : events::header_archived;
}

static constexpr auto events_object_organized() NOEXCEPT
{
// Using header because block organization is in confirmation chaser.
////return is_block() ? events::block_organized : events::header_organized;
return events::header_organized;
}

static constexpr auto events_object_reorganized() NOEXCEPT
{
// Using header because block reorganized is in confirmation chaser.
Expand Down
3 changes: 1 addition & 2 deletions include/bitcoin/node/chasers/chaser_snapshot.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ class BCN_API chaser_snapshot
////virtual void do_valid(height_t height) NOEXCEPT;
////virtual void do_confirm(height_t height) NOEXCEPT;

virtual bool handle_chase(const code& ec, chase event_,
event_value value) NOEXCEPT;
virtual bool handle_chase(const code& ec, event_value value) NOEXCEPT;

private:
void take_snapshot(height_t height) NOEXCEPT;
Expand Down
3 changes: 1 addition & 2 deletions include/bitcoin/node/chasers/chaser_storage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ class BCN_API chaser_storage
protected:
virtual void do_reload() NOEXCEPT;
virtual void do_space(size_t space) NOEXCEPT;
virtual bool handle_chase(const code& ec, chase event_,
event_value value) NOEXCEPT;
virtual bool handle_chase(const code& ec, event_value value) NOEXCEPT;

private:
void do_stopping(const code& ec) NOEXCEPT;
Expand Down
3 changes: 1 addition & 2 deletions include/bitcoin/node/chasers/chaser_template.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ class BCN_API chaser_template
code start() NOEXCEPT override;

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

virtual void do_transaction(transaction_t value) NOEXCEPT;
};
Expand Down
3 changes: 1 addition & 2 deletions include/bitcoin/node/chasers/chaser_transaction.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ class BCN_API chaser_transaction
bool test, submit_handler&& handler) NOEXCEPT;

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

virtual void do_submit(const system::chain::transactions_cptr& txs,
bool test, const submit_handler& handler) NOEXCEPT;
Expand Down
3 changes: 1 addition & 2 deletions include/bitcoin/node/chasers/chaser_validate.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ class BCN_API chaser_validate
BIND_TO(method, args));
}

virtual bool handle_chase(const code& ec, chase event_,
event_value value) NOEXCEPT;
virtual bool handle_chase(const code& ec, event_value value) NOEXCEPT;

virtual void do_regressed(height_t branch_point) NOEXCEPT;
virtual void do_checked(height_t height) NOEXCEPT;
Expand Down
30 changes: 1 addition & 29 deletions include/bitcoin/node/define.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
#endif

/// For common types below.
#include <bitcoin/database.hpp>
#include <bitcoin/network.hpp>

namespace libbitcoin {
Expand All @@ -64,41 +63,14 @@ typedef std::shared_ptr<database::associations> map_ptr;
typedef std::function<void(const code&, const map_ptr&,
const job::ptr&)> map_handler;

/// Event desubscriber key type.
using object_key = uint64_t;

/// Event value types.
using count_t = size_t;
using height_t = size_t;
using peer_t = uint64_t;
using object_t = object_key;
using header_t = database::header_link::integer;
using transaction_t = database::tx_link::integer;

/// std::variant types must be distinct, and xcode size_t is neither uint32_t
/// nor uint64_t, so this ensures we have the distinct set of necessary types.
using event_value =
iif<is_same_type<std::size_t, uint64_t>,
std::variant<uint32_t, size_t>,
iif<is_same_type<std::size_t, uint32_t>,
std::variant<uint64_t, size_t>,
std::variant<uint64_t, uint32_t, size_t>>>;

/// Event desubscriber.
typedef network::desubscriber<object_key, chase, event_value> event_subscriber;
typedef network::desubscriber<object_key, event_value> event_subscriber;
typedef event_subscriber::handler event_notifier;
typedef event_subscriber::completer event_completer;

// Inventory messages.
using type_id = network::messages::peer::inventory_item::type_id;

// NDEBUG MSVC
////static_assert(sizeof(uint64_t) == 8u);
////static_assert(sizeof(block_t) == 16u);
////static_assert(sizeof(xblock_t) == 32u);
////static_assert(sizeof(event_value) == 24u);
////static_assert(sizeof(xevent_value) == 40u);

} // namespace node
} // namespace libbitcoin

Expand Down
9 changes: 4 additions & 5 deletions include/bitcoin/node/full_node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,10 @@ class BCN_API full_node
/// -----------------------------------------------------------------------

/// Set chaser event.
virtual void notify(const code& ec, chase event_,
event_value value) NOEXCEPT;
virtual void notify(const code& ec, event_value value) NOEXCEPT;

/// Set chaser event for the given subscriber only.
virtual void notify_one(object_key key, const code& ec, chase event_,
virtual void notify_one(object_key key, const code& ec,
event_value value) NOEXCEPT;

/// Call from chaser start() methods (requires strand).
Expand Down Expand Up @@ -186,8 +185,8 @@ class BCN_API full_node
private:
void do_subscribe_chase(const event_notifier& handler,
const event_completer& complete) NOEXCEPT;
void do_notify(const code& ec, chase event_, event_value value) NOEXCEPT;
void do_notify_one(object_key key, const code& ec, chase event_,
void do_notify(const code& ec, event_value value) NOEXCEPT;
void do_notify_one(object_key key, const code& ec,
event_value value) NOEXCEPT;

// These are thread safe.
Expand Down
43 changes: 30 additions & 13 deletions include/bitcoin/node/impl/chasers/chaser_organize.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ code CLASS::start() NOEXCEPT
<< state_->height() << "].");

update_checkpoint(top);
SUBSCRIBE_CHASE(handle_chase, _1, _2, _3);
SUBSCRIBE_CHASE(handle_chase, _1, _2);
return error::success;
}

Expand Down Expand Up @@ -89,23 +89,40 @@ void CLASS::prioritize(const system::hash_digest& hash,
// ----------------------------------------------------------------------------

TEMPLATE
bool CLASS::handle_chase(const code&, chase event_, event_value value) NOEXCEPT
bool CLASS::handle_chase(const code&, event_value value) NOEXCEPT
{
if (closed())
return false;

switch (event_)
switch (to_chase(value))
{
// Roll back the candidate chain to confirmed top (via fork point).
case chase::unchecked:
{
if (database_settings().mark_unconfirmable)
{
POST(do_disorganize, to_payload<chase::unchecked>(value).link);
}

break;
}
case chase::unvalid:
{
if (database_settings().mark_unconfirmable)
{
POST(do_disorganize, to_payload<chase::unvalid>(value).link);
}

break;
}
case chase::unconfirmable:
{
if (!database_settings().mark_unconfirmable)
break;
if (database_settings().mark_unconfirmable)
{
POST(do_disorganize,
to_payload<chase::unconfirmable>(value).link);
}

// Roll back the candidate chain to confirmed top (via fork point).
BC_ASSERT(std::holds_alternative<header_t>(value));
POST(do_disorganize, std::get<header_t>(value));
break;
}
case chase::stop:
Expand Down Expand Up @@ -273,7 +290,7 @@ void CLASS::do_organize(typename Block::cptr block, bool prioritized,
// Reset chasers to the branch point.
if (regress)
{
notify(error::success, chase::regressed, branch_point);
notify(error::success, chases::regressed{ branch_point });
}

// Push stored strong headers to candidate chain.
Expand Down Expand Up @@ -322,14 +339,14 @@ void CLASS::do_organize(typename Block::cptr block, bool prioritized,
// be skipped, resulting in stall until restart at which time the start
// event will advance through all downloaded candidates and progress on
// arrivals. This bumps validation once for current strong headers.
notify(error::success, chase::bump, add1(branch_point));
notify(error::success, chases::bump{ add1(branch_point) });
bumped_ = true;
}

// chase::headers | chase::blocks
// This prevents download stall, the check chaser races ahead.
// Start block downloads, which upon completion bumps validation.
notify(error::success, chase_object(), branch_point);
notify(error::success, chase_object{ branch_point });
}

// Logs from candidate block parent to the candidate (forward sequential).
Expand Down Expand Up @@ -486,10 +503,10 @@ void CLASS::do_disorganize(header_t link) NOEXCEPT
state_ = state;

// Candidate is same as confirmed, reset chasers to new top.
notify(error::success, chase::disorganized, fork_point);
notify(error::success, chases::disorganized{ fork_point });

// Reset all connections to ensure that new connections exist.
notify(error::success, chase::suspend, {});
notify(error::success, chases::suspend{});
}

// Private setters
Expand Down
3 changes: 1 addition & 2 deletions include/bitcoin/node/protocols/protocol_block_in_31800.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@ class BCN_API protocol_block_in_31800
virtual void do_get_downloads(count_t count) NOEXCEPT;

/// Handle chaser events.
virtual bool handle_chase(const code& ec, chase event_,
event_value value) NOEXCEPT;
virtual bool handle_chase(const code& ec, event_value value) NOEXCEPT;

/// Manage work splitting.
bool is_idle() const NOEXCEPT override;
Expand Down
3 changes: 1 addition & 2 deletions include/bitcoin/node/protocols/protocol_block_out_106.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@ class BCN_API protocol_block_out_106
virtual bool superseded() const NOEXCEPT;

/// Handle chaser events.
virtual bool handle_chase(const code& ec, chase event_,
event_value value) NOEXCEPT;
virtual bool handle_chase(const code& ec, event_value value) NOEXCEPT;

/// Process block announcement.
virtual bool do_announce(header_t link) NOEXCEPT;
Expand Down
3 changes: 1 addition & 2 deletions include/bitcoin/node/protocols/protocol_header_out_70012.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ class BCN_API protocol_header_out_70012

protected:
/// Handle chaser events.
virtual bool handle_chase(const code& ec, chase event_,
event_value value) NOEXCEPT;
virtual bool handle_chase(const code& ec, event_value value) NOEXCEPT;

/// Process block announcement.
virtual bool do_announce(header_t link) NOEXCEPT;
Expand Down
3 changes: 1 addition & 2 deletions include/bitcoin/node/protocols/protocol_observer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ class BCN_API protocol_observer

protected:
/// Handle chaser events.
virtual bool handle_chase(const code& ec, chase event_,
event_value value) NOEXCEPT;
virtual bool handle_chase(const code& ec, event_value value) NOEXCEPT;

/// Accept incoming inventory message.
virtual bool handle_receive_inventory(const code& ec,
Expand Down
5 changes: 2 additions & 3 deletions include/bitcoin/node/protocols/protocol_peer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,10 @@ class BCN_API protocol_peer
using network::protocol_peer::notify;

/// Set a chaser event.
virtual void notify(const code& ec, chase event_,
event_value value) const NOEXCEPT;
virtual void notify(const code& ec, event_value value) const NOEXCEPT;

/// Set a chaser event.
virtual void notify_one(object_key key, const code& ec, chase event_,
virtual void notify_one(object_key key, const code& ec,
event_value value) const NOEXCEPT;

private:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ class BCN_API protocol_transaction_out_106
using inventory_items = network::messages::peer::inventory_items;

/// Handle chaser events.
virtual bool handle_chase(const code& ec, chase event_,
event_value value) NOEXCEPT;
virtual bool handle_chase(const code& ec, event_value value) NOEXCEPT;

/// Process tx announcement.
virtual bool do_announce(transaction_t link) NOEXCEPT;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ class BCN_API protocol_transaction_out_70013

protected:
/// Handle chaser events.
bool handle_chase(const code& ec, chase event_,
event_value value) NOEXCEPT override;
bool handle_chase(const code& ec, event_value value) NOEXCEPT override;

/// Capture the peer's minimum fee rate for announcements.
virtual bool handle_receive_fee_filter(const code& ec,
Expand Down
Loading
Loading