Skip to content

Commit

Permalink
Refactor to stack allocate chasers.
Browse files Browse the repository at this point in the history
  • Loading branch information
evoskuil committed Feb 16, 2024
1 parent 374519c commit 0cc57d5
Show file tree
Hide file tree
Showing 16 changed files with 137 additions and 188 deletions.
11 changes: 4 additions & 7 deletions include/bitcoin/node/chasers/chaser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,6 @@ class BCN_API chaser
public:
enum class chase
{
/// Initialize chaser state ({}).
start,

/// A new strong branch exists (strong height_t).
/// Issued by 'header' and handled by 'check'.
header,
Expand Down Expand Up @@ -86,8 +83,8 @@ class BCN_API chaser
typedef database::query<store> query;
DELETE_COPY_MOVE(chaser);

// TODO: public method to check/store a block.
// TODO: not stranded, thread safe, and posts checked event.
/// Synchronously subscribe to notify and asynchronously initialize state.
virtual bool start() NOEXCEPT = 0;

protected:
chaser(full_node& node) NOEXCEPT;
Expand All @@ -102,8 +99,8 @@ class BCN_API chaser
/// True if the current thread is on the chaser strand.
bool stranded() const NOEXCEPT;

/// Subscribe to chaser events (must be non-virtual).
code subscribe(event_handler&& handler) NOEXCEPT;
/// Subscribe to chaser events.
bool subscribe(event_handler&& handler) NOEXCEPT;

/// Set chaser event (does not require network strand).
void notify(const code& ec, chase event_, link value) NOEXCEPT;
Expand Down
15 changes: 9 additions & 6 deletions include/bitcoin/node/chasers/chaser_candidate.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#ifndef LIBBITCOIN_NODE_CHASERS_CHASER_CANDIDATE_HPP
#define LIBBITCOIN_NODE_CHASERS_CHASER_CANDIDATE_HPP

#include <functional>
#include <bitcoin/network.hpp>
#include <bitcoin/node/define.hpp>
#include <bitcoin/node/chasers/chaser.hpp>
Expand All @@ -30,17 +31,19 @@ class full_node;

/// Construct candidate blocks upon modification of the transaction DAG.
class BCN_API chaser_candidate
: public chaser, protected network::tracker<chaser_candidate>
: public chaser
{
public:
typedef std::unique_ptr<chaser_candidate> uptr;

chaser_candidate(full_node& node) NOEXCEPT;

virtual bool start() NOEXCEPT;

protected:
virtual void handle_transaction(transaction_t value) NOEXCEPT;
virtual void handle_event(const code& ec, chase event_,
link value) NOEXCEPT;

private:
void handle_start() NOEXCEPT;
void handle_transaction() NOEXCEPT;
void handle_event(const code& ec, chase event_, link value) NOEXCEPT;
void do_handle_event(const code& ec, chase event_, link value) NOEXCEPT;
};

Expand Down
9 changes: 4 additions & 5 deletions include/bitcoin/node/chasers/chaser_check.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#ifndef LIBBITCOIN_NODE_CHASERS_CHASER_CHECK_HPP
#define LIBBITCOIN_NODE_CHASERS_CHASER_CHECK_HPP

#include <functional>
#include <bitcoin/network.hpp>
#include <bitcoin/node/define.hpp>
#include <bitcoin/node/chasers/chaser.hpp>
Expand All @@ -30,17 +31,15 @@ class full_node;

/// Chase down blocks for the candidate header chain.
class BCN_API chaser_check
: public chaser, protected network::tracker<chaser_check>
: public chaser
{
public:
typedef std::unique_ptr<chaser_check> uptr;

chaser_check(full_node& node) NOEXCEPT;

void checked(const system::chain::block::cptr& block) NOEXCEPT;
virtual bool start() NOEXCEPT;
virtual void checked(const system::chain::block::cptr& block) NOEXCEPT;

protected:
virtual void handle_start() NOEXCEPT;
virtual void handle_header(height_t branch_point) NOEXCEPT;
virtual void handle_event(const code& ec, chase event_,
link value) NOEXCEPT;
Expand Down
15 changes: 9 additions & 6 deletions include/bitcoin/node/chasers/chaser_confirm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#ifndef LIBBITCOIN_NODE_CHASERS_CHASER_CONFIRM_HPP
#define LIBBITCOIN_NODE_CHASERS_CHASER_CONFIRM_HPP

#include <functional>
#include <bitcoin/network.hpp>
#include <bitcoin/node/define.hpp>
#include <bitcoin/node/chasers/chaser.hpp>
Expand All @@ -30,17 +31,19 @@ class full_node;

/// Chase down valid blocks for confirmation.
class BCN_API chaser_confirm
: public chaser, protected network::tracker<chaser_confirm>
: public chaser
{
public:
typedef std::unique_ptr<chaser_confirm> uptr;

chaser_confirm(full_node& node) NOEXCEPT;

virtual bool start() NOEXCEPT;

protected:
virtual void handle_connected(header_t block) NOEXCEPT;
virtual void handle_event(const code& ec, chase event_,
link value) NOEXCEPT;

private:
void handle_start() NOEXCEPT;
void handle_connected() NOEXCEPT;
void handle_event(const code& ec, chase event_, link value) NOEXCEPT;
void do_handle_event(const code& ec, chase event_, link value) NOEXCEPT;
};

Expand Down
15 changes: 9 additions & 6 deletions include/bitcoin/node/chasers/chaser_connect.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#ifndef LIBBITCOIN_NODE_CHASERS_CHASER_CONNECT_HPP
#define LIBBITCOIN_NODE_CHASERS_CHASER_CONNECT_HPP

#include <functional>
#include <bitcoin/network.hpp>
#include <bitcoin/node/define.hpp>
#include <bitcoin/node/chasers/chaser.hpp>
Expand All @@ -30,17 +31,19 @@ class full_node;

/// Chase down blocks in the the candidate header chain for validation.
class BCN_API chaser_connect
: public chaser, protected network::tracker<chaser_connect>
: public chaser
{
public:
typedef std::unique_ptr<chaser_connect> uptr;

chaser_connect(full_node& node) NOEXCEPT;

virtual bool start() NOEXCEPT;

protected:
virtual void handle_checked(header_t block) NOEXCEPT;
virtual void handle_event(const code& ec, chase event_,
link value) NOEXCEPT;

private:
void handle_start() NOEXCEPT;
void handle_checked() NOEXCEPT;
void handle_event(const code& ec, chase event_, link value) NOEXCEPT;
void do_handle_event(const code& ec, chase event_, link value) NOEXCEPT;
};

Expand Down
10 changes: 5 additions & 5 deletions include/bitcoin/node/chasers/chaser_header.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#ifndef LIBBITCOIN_NODE_CHASERS_CHASER_HEADER_HPP
#define LIBBITCOIN_NODE_CHASERS_CHASER_HEADER_HPP

#include <functional>
#include <unordered_map>
#include <bitcoin/database.hpp>
#include <bitcoin/network.hpp>
Expand All @@ -34,24 +35,23 @@ class full_node;
/// Weak branches are retained in a hash table unless already store populated.
/// Strong branches reorganize the candidate chain and fire the 'header' event.
class BCN_API chaser_header
: public chaser, protected network::tracker<chaser_header>
: public chaser
{
public:
typedef std::unique_ptr<chaser_header> uptr;

chaser_header(full_node& node) NOEXCEPT;

virtual bool start() NOEXCEPT;

/// Organize the next header in sequence, relative to caller's peer.
/// Causes a fault/stop if preceding headers have not been stored.
/// Caller must validate the header and provide context.
void organize(const system::chain::header::cptr& header,
virtual void organize(const system::chain::header::cptr& header,
system::chain::context&& context) NOEXCEPT;

protected:
typedef std::vector<database::header_link> header_links;

/// Handlers.
virtual void handle_start() NOEXCEPT;
virtual void handle_event(const code& ec, chase event_,
link value) NOEXCEPT;

Expand Down
16 changes: 9 additions & 7 deletions include/bitcoin/node/chasers/chaser_transaction.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#ifndef LIBBITCOIN_NODE_CHASERS_CHASER_TRANSACTION_HPP
#define LIBBITCOIN_NODE_CHASERS_CHASER_TRANSACTION_HPP

#include <functional>
#include <bitcoin/network.hpp>
#include <bitcoin/node/define.hpp>
#include <bitcoin/node/chasers/chaser.hpp>
Expand All @@ -30,19 +31,20 @@ class full_node;

/// Chase down unconfirmed transactions.
class BCN_API chaser_transaction
: public chaser, protected network::tracker<chaser_transaction>
: public chaser
{
public:
typedef std::unique_ptr<chaser_transaction> uptr;

chaser_transaction(full_node& node) NOEXCEPT;

void store(const system::chain::transaction::cptr& block) NOEXCEPT;
virtual bool start() NOEXCEPT;
virtual void store(const system::chain::transaction::cptr& block) NOEXCEPT;

protected:
virtual void handle_confirmed() NOEXCEPT;
virtual void handle_event(const code& ec, chase event_,
link value) NOEXCEPT;

private:
void handle_start() NOEXCEPT;
void handle_confirmed() NOEXCEPT;
void handle_event(const code& ec, chase event_, link value) NOEXCEPT;
void do_handle_event(const code& ec, chase event_, link value) NOEXCEPT;
void do_store(const system::chain::transaction::cptr& header) NOEXCEPT;
};
Expand Down
21 changes: 8 additions & 13 deletions include/bitcoin/node/full_node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ class BCN_API full_node
full_node(query& query, const configuration& configuration,
const network::logger& log) NOEXCEPT;

~full_node() NOEXCEPT;

/// Sequences.
/// -----------------------------------------------------------------------

Expand Down Expand Up @@ -79,13 +81,6 @@ class BCN_API full_node
chaser::event_subscriber& event_subscriber() NOEXCEPT;

protected:

/// Chasers configuration.
/// -----------------------------------------------------------------------
virtual code create_chasers() NOEXCEPT;
virtual void stop_chasers() NOEXCEPT;
virtual void delete_chasers() NOEXCEPT;

/// Session attachments.
/// -----------------------------------------------------------------------
network::session_manual::ptr attach_manual_session() NOEXCEPT override;
Expand All @@ -105,12 +100,12 @@ class BCN_API full_node

// These are protected by strand.
chaser::event_subscriber event_subscriber_;
chaser_header::uptr chaser_header_{};
chaser_check::uptr chaser_check_{};
chaser_connect::uptr chaser_connect_{};
chaser_confirm::uptr chaser_confirm_{};
chaser_transaction::uptr chaser_transaction_{};
chaser_candidate::uptr chaser_candidate_{};
chaser_header chaser_header_;
chaser_check chaser_check_;
chaser_connect chaser_connect_;
chaser_confirm chaser_confirm_;
chaser_transaction chaser_transaction_;
chaser_candidate chaser_candidate_;
};

} // namespace node
Expand Down
14 changes: 10 additions & 4 deletions src/chasers/chaser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,18 @@ bool chaser::stranded() const NOEXCEPT
return strand_.running_in_this_thread();
}

// Must be non-virtual for constructor invoke.
// Requires network strand (call from node start).
code chaser::subscribe(event_handler&& handler) NOEXCEPT
bool chaser::subscribe(event_handler&& handler) NOEXCEPT
{
BC_ASSERT_MSG(node_.stranded(), "chaser");
return subscriber_.subscribe(std::move(handler));
const auto ec = subscriber_.subscribe(std::move(handler));

if (ec)
{
LOGF("Chaser subscribe fault, " << ec.message());
return false;
}

return true;
}

// Posts to network strand (call from chaser strands).
Expand Down
24 changes: 11 additions & 13 deletions src/chasers/chaser_candidate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <bitcoin/node/chasers/chaser_candidate.hpp>

#include <functional>
#include <variant>
#include <bitcoin/network.hpp>
#include <bitcoin/node/error.hpp>
#include <bitcoin/node/full_node.hpp>
Expand All @@ -31,11 +32,10 @@ using namespace std::placeholders;

BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)

// Requires subscriber_ protection (call from node construct or node.strand).
chaser_candidate::chaser_candidate(full_node& node) NOEXCEPT
: chaser(node),
tracker<chaser_candidate>(node.log)
: chaser(node)
{
subscribe(std::bind(&chaser_candidate::handle_event, this, _1, _2, _3));
}

void chaser_candidate::handle_event(const code& ec, chase event_,
Expand All @@ -46,7 +46,7 @@ void chaser_candidate::handle_event(const code& ec, chase event_,
}

void chaser_candidate::do_handle_event(const code& ec, chase event_,
link) NOEXCEPT
link value) NOEXCEPT
{
BC_ASSERT_MSG(stranded(), "chaser_candidate");

Expand All @@ -55,14 +55,10 @@ void chaser_candidate::do_handle_event(const code& ec, chase event_,

switch (event_)
{
case chase::start:
{
handle_start();
break;
}
case chase::transaction:
{
handle_transaction();
BC_ASSERT(std::holds_alternative<transaction_t>(value));
handle_transaction(std::get<transaction_t>(value));
break;
}
default:
Expand All @@ -71,15 +67,17 @@ void chaser_candidate::do_handle_event(const code& ec, chase event_,
}

// TODO: initialize candidate state.
void chaser_candidate::handle_start() NOEXCEPT
bool chaser_candidate::start() NOEXCEPT
{
BC_ASSERT_MSG(stranded(), "chaser_candidate");
return subscribe(std::bind(&chaser_candidate::handle_event,
this, _1, _2, _3));
}

// TODO: handle transaction graph change (may issue 'candidate').
void chaser_candidate::handle_transaction() NOEXCEPT
void chaser_candidate::handle_transaction(transaction_t tx) NOEXCEPT
{
BC_ASSERT_MSG(stranded(), "chaser_candidate");
LOGN("Handle transaction pool updated (" << tx << ").");
}

BC_POP_WARNING()
Expand Down
Loading

0 comments on commit 0cc57d5

Please sign in to comment.