From 0cc57d5f6352407f2d8d708a9bfb4c3139090939 Mon Sep 17 00:00:00 2001 From: evoskuil Date: Fri, 16 Feb 2024 12:55:11 -0500 Subject: [PATCH] Refactor to stack allocate chasers. --- include/bitcoin/node/chasers/chaser.hpp | 11 ++- .../bitcoin/node/chasers/chaser_candidate.hpp | 15 ++-- include/bitcoin/node/chasers/chaser_check.hpp | 9 ++- .../bitcoin/node/chasers/chaser_confirm.hpp | 15 ++-- .../bitcoin/node/chasers/chaser_connect.hpp | 15 ++-- .../bitcoin/node/chasers/chaser_header.hpp | 10 +-- .../node/chasers/chaser_transaction.hpp | 16 +++-- include/bitcoin/node/full_node.hpp | 21 +++--- src/chasers/chaser.cpp | 14 ++-- src/chasers/chaser_candidate.cpp | 24 +++---- src/chasers/chaser_check.cpp | 18 ++--- src/chasers/chaser_confirm.cpp | 23 +++---- src/chasers/chaser_connect.cpp | 23 +++---- src/chasers/chaser_header.cpp | 27 ++------ src/chasers/chaser_transaction.cpp | 15 ++-- src/full_node.cpp | 69 ++++++------------- 16 files changed, 137 insertions(+), 188 deletions(-) diff --git a/include/bitcoin/node/chasers/chaser.hpp b/include/bitcoin/node/chasers/chaser.hpp index a89ea36b..4f689f38 100644 --- a/include/bitcoin/node/chasers/chaser.hpp +++ b/include/bitcoin/node/chasers/chaser.hpp @@ -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, @@ -86,8 +83,8 @@ class BCN_API chaser typedef database::query 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; @@ -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; diff --git a/include/bitcoin/node/chasers/chaser_candidate.hpp b/include/bitcoin/node/chasers/chaser_candidate.hpp index b73f7593..afd5ce24 100644 --- a/include/bitcoin/node/chasers/chaser_candidate.hpp +++ b/include/bitcoin/node/chasers/chaser_candidate.hpp @@ -19,6 +19,7 @@ #ifndef LIBBITCOIN_NODE_CHASERS_CHASER_CANDIDATE_HPP #define LIBBITCOIN_NODE_CHASERS_CHASER_CANDIDATE_HPP +#include #include #include #include @@ -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 + : public chaser { public: - typedef std::unique_ptr 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; }; diff --git a/include/bitcoin/node/chasers/chaser_check.hpp b/include/bitcoin/node/chasers/chaser_check.hpp index 3e592011..00b9a1ef 100644 --- a/include/bitcoin/node/chasers/chaser_check.hpp +++ b/include/bitcoin/node/chasers/chaser_check.hpp @@ -19,6 +19,7 @@ #ifndef LIBBITCOIN_NODE_CHASERS_CHASER_CHECK_HPP #define LIBBITCOIN_NODE_CHASERS_CHASER_CHECK_HPP +#include #include #include #include @@ -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 + : public chaser { public: - typedef std::unique_ptr 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; diff --git a/include/bitcoin/node/chasers/chaser_confirm.hpp b/include/bitcoin/node/chasers/chaser_confirm.hpp index 7d0cc122..9638a28f 100644 --- a/include/bitcoin/node/chasers/chaser_confirm.hpp +++ b/include/bitcoin/node/chasers/chaser_confirm.hpp @@ -19,6 +19,7 @@ #ifndef LIBBITCOIN_NODE_CHASERS_CHASER_CONFIRM_HPP #define LIBBITCOIN_NODE_CHASERS_CHASER_CONFIRM_HPP +#include #include #include #include @@ -30,17 +31,19 @@ class full_node; /// Chase down valid blocks for confirmation. class BCN_API chaser_confirm - : public chaser, protected network::tracker + : public chaser { public: - typedef std::unique_ptr 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; }; diff --git a/include/bitcoin/node/chasers/chaser_connect.hpp b/include/bitcoin/node/chasers/chaser_connect.hpp index 47c76283..4442237e 100644 --- a/include/bitcoin/node/chasers/chaser_connect.hpp +++ b/include/bitcoin/node/chasers/chaser_connect.hpp @@ -19,6 +19,7 @@ #ifndef LIBBITCOIN_NODE_CHASERS_CHASER_CONNECT_HPP #define LIBBITCOIN_NODE_CHASERS_CHASER_CONNECT_HPP +#include #include #include #include @@ -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 + : public chaser { public: - typedef std::unique_ptr 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; }; diff --git a/include/bitcoin/node/chasers/chaser_header.hpp b/include/bitcoin/node/chasers/chaser_header.hpp index 336bb991..034b5948 100644 --- a/include/bitcoin/node/chasers/chaser_header.hpp +++ b/include/bitcoin/node/chasers/chaser_header.hpp @@ -19,6 +19,7 @@ #ifndef LIBBITCOIN_NODE_CHASERS_CHASER_HEADER_HPP #define LIBBITCOIN_NODE_CHASERS_CHASER_HEADER_HPP +#include #include #include #include @@ -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 + : public chaser { public: - typedef std::unique_ptr 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 header_links; /// Handlers. - virtual void handle_start() NOEXCEPT; virtual void handle_event(const code& ec, chase event_, link value) NOEXCEPT; diff --git a/include/bitcoin/node/chasers/chaser_transaction.hpp b/include/bitcoin/node/chasers/chaser_transaction.hpp index 53fa4d0d..6ebf7f5d 100644 --- a/include/bitcoin/node/chasers/chaser_transaction.hpp +++ b/include/bitcoin/node/chasers/chaser_transaction.hpp @@ -19,6 +19,7 @@ #ifndef LIBBITCOIN_NODE_CHASERS_CHASER_TRANSACTION_HPP #define LIBBITCOIN_NODE_CHASERS_CHASER_TRANSACTION_HPP +#include #include #include #include @@ -30,19 +31,20 @@ class full_node; /// Chase down unconfirmed transactions. class BCN_API chaser_transaction - : public chaser, protected network::tracker + : public chaser { public: - typedef std::unique_ptr 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; }; diff --git a/include/bitcoin/node/full_node.hpp b/include/bitcoin/node/full_node.hpp index ca11fffe..224981d4 100644 --- a/include/bitcoin/node/full_node.hpp +++ b/include/bitcoin/node/full_node.hpp @@ -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. /// ----------------------------------------------------------------------- @@ -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; @@ -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 diff --git a/src/chasers/chaser.cpp b/src/chasers/chaser.cpp index 6495ef33..dbba211e 100644 --- a/src/chasers/chaser.cpp +++ b/src/chasers/chaser.cpp @@ -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). diff --git a/src/chasers/chaser_candidate.cpp b/src/chasers/chaser_candidate.cpp index e69e2c3e..26e8ca4a 100644 --- a/src/chasers/chaser_candidate.cpp +++ b/src/chasers/chaser_candidate.cpp @@ -19,6 +19,7 @@ #include #include +#include #include #include #include @@ -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(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_, @@ -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"); @@ -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(value)); + handle_transaction(std::get(value)); break; } default: @@ -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() diff --git a/src/chasers/chaser_check.cpp b/src/chasers/chaser_check.cpp index 7cc53946..03d39dfc 100644 --- a/src/chasers/chaser_check.cpp +++ b/src/chasers/chaser_check.cpp @@ -33,11 +33,10 @@ using namespace std::placeholders; BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT) +// Requires subscriber_ protection (call from node construct or node.strand). chaser_check::chaser_check(full_node& node) NOEXCEPT - : chaser(node), - tracker(node.log) + : chaser(node) { - subscribe(std::bind(&chaser_check::handle_event, this, _1, _2, _3)); } void chaser_check::handle_event(const code& ec, chase event_, @@ -57,11 +56,6 @@ void chaser_check::do_handle_event(const code& ec, chase event_, switch (event_) { - case chase::start: - { - handle_start(); - break; - } case chase::header: { BC_ASSERT(std::holds_alternative(value)); @@ -74,17 +68,17 @@ void chaser_check::do_handle_event(const code& ec, chase event_, } // TODO: initialize check state. -void chaser_check::handle_start() NOEXCEPT +bool chaser_check::start() NOEXCEPT { - BC_ASSERT_MSG(stranded(), "chaser_check"); // get_all_unassociated_above(0) + return subscribe(std::bind(&chaser_check::handle_event, + this, _1, _2, _3)); } // TODO: handle the new strong branch (may issue 'checked'). void chaser_check::handle_header(height_t branch_point) NOEXCEPT { - BC_ASSERT_MSG(stranded(), "chaser_check"); - LOGN("Candidate organization above height (" << branch_point << ")."); + LOGN("Handle candidate organization above height (" << branch_point << ")."); // get_all_unassociated_above(branch_point) } diff --git a/src/chasers/chaser_confirm.cpp b/src/chasers/chaser_confirm.cpp index ff78dd33..1171e73f 100644 --- a/src/chasers/chaser_confirm.cpp +++ b/src/chasers/chaser_confirm.cpp @@ -31,11 +31,10 @@ using namespace std::placeholders; BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT) +// Requires subscriber_ protection (call from node construct or node.strand). chaser_confirm::chaser_confirm(full_node& node) NOEXCEPT - : chaser(node), - tracker(node.log) + : chaser(node) { - subscribe(std::bind(&chaser_confirm::handle_event, this, _1, _2, _3)); } void chaser_confirm::handle_event(const code& ec, chase event_, @@ -46,7 +45,7 @@ void chaser_confirm::handle_event(const code& ec, chase event_, } void chaser_confirm::do_handle_event(const code& ec, chase event_, - link) NOEXCEPT + link value) NOEXCEPT { BC_ASSERT_MSG(stranded(), "chaser_confirm"); @@ -55,14 +54,10 @@ void chaser_confirm::do_handle_event(const code& ec, chase event_, switch (event_) { - case chase::start: - { - handle_start(); - break; - } case chase::connected: { - handle_connected(); + BC_ASSERT(std::holds_alternative(value)); + handle_connected(std::get(value)); break; } default: @@ -71,15 +66,17 @@ void chaser_confirm::do_handle_event(const code& ec, chase event_, } // TODO: initialize confirm state. -void chaser_confirm::handle_start() NOEXCEPT +bool chaser_confirm::start() NOEXCEPT { - BC_ASSERT_MSG(stranded(), "chaser_confirm"); + return subscribe(std::bind(&chaser_confirm::handle_event, + this, _1, _2, _3)); } // TODO: handle new strong connected branch (may issue 'confirmed'). -void chaser_confirm::handle_connected() NOEXCEPT +void chaser_confirm::handle_connected(header_t block) NOEXCEPT { BC_ASSERT_MSG(stranded(), "chaser_confirm"); + LOGN("Handle connected block (" << block << ")."); } BC_POP_WARNING() diff --git a/src/chasers/chaser_connect.cpp b/src/chasers/chaser_connect.cpp index e6f0cf73..c7a3f4fa 100644 --- a/src/chasers/chaser_connect.cpp +++ b/src/chasers/chaser_connect.cpp @@ -31,11 +31,10 @@ using namespace std::placeholders; BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT) +// Requires subscriber_ protection (call from node construct or node.strand). chaser_connect::chaser_connect(full_node& node) NOEXCEPT - : chaser(node), - tracker(node.log) + : chaser(node) { - subscribe(std::bind(&chaser_connect::handle_event, this, _1, _2, _3)); } void chaser_connect::handle_event(const code& ec, chase event_, @@ -46,7 +45,7 @@ void chaser_connect::handle_event(const code& ec, chase event_, } void chaser_connect::do_handle_event(const code& ec, chase event_, - link) NOEXCEPT + link value) NOEXCEPT { BC_ASSERT_MSG(stranded(), "chaser_connect"); @@ -55,14 +54,10 @@ void chaser_connect::do_handle_event(const code& ec, chase event_, switch (event_) { - case chase::start: - { - handle_start(); - break; - } case chase::checked: { - handle_checked(); + BC_ASSERT(std::holds_alternative(value)); + handle_checked(std::get(value)); break; } default: @@ -71,15 +66,17 @@ void chaser_connect::do_handle_event(const code& ec, chase event_, } // TODO: initialize connect state. -void chaser_connect::handle_start() NOEXCEPT +bool chaser_connect::start() NOEXCEPT { - BC_ASSERT_MSG(stranded(), "chaser_connect"); + return subscribe(std::bind(&chaser_connect::handle_event, + this, _1, _2, _3)); } // TODO: handle the new checked blocks (may issue 'connected'). -void chaser_connect::handle_checked() NOEXCEPT +void chaser_connect::handle_checked(header_t block) NOEXCEPT { BC_ASSERT_MSG(stranded(), "chaser_connect"); + LOGN("Handle candidate organization above height (" << block << ")."); } BC_POP_WARNING() diff --git a/src/chasers/chaser_header.cpp b/src/chasers/chaser_header.cpp index f62761d1..363fba6c 100644 --- a/src/chasers/chaser_header.cpp +++ b/src/chasers/chaser_header.cpp @@ -34,14 +34,12 @@ using namespace std::placeholders; BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT) +// Requires subscriber_ protection (call from node construct or node.strand). chaser_header::chaser_header(full_node& node) NOEXCEPT : chaser(node), currency_window_(node.node_settings().currency_window()), - use_currency_window_(currency_window_ != wall_clock::duration::zero()), - tracker(node.log) + use_currency_window_(currency_window_ != wall_clock::duration::zero()) { - subscribe(std::bind(&chaser_header::handle_event, - this, _1, _2, _3)); } // protected @@ -54,29 +52,16 @@ void chaser_header::handle_event(const code& ec, chase event_, } // private -void chaser_header::do_handle_event(const code& ec, chase event_, link) NOEXCEPT +void chaser_header::do_handle_event(const code&, chase, link) NOEXCEPT { BC_ASSERT_MSG(stranded(), "chaser_header"); - - if (ec) - return; - - switch (event_) - { - case chase::start: - { - handle_start(); - break; - } - default: - return; - } } // protected -void chaser_header::handle_start() NOEXCEPT +bool chaser_header::start() NOEXCEPT { - BC_ASSERT_MSG(stranded(), "chaser_header"); + return subscribe(std::bind(&chaser_header::handle_event, + this, _1, _2, _3)); } void chaser_header::organize(const chain::header::cptr& header, diff --git a/src/chasers/chaser_transaction.cpp b/src/chasers/chaser_transaction.cpp index 7069dcb5..757eaa81 100644 --- a/src/chasers/chaser_transaction.cpp +++ b/src/chasers/chaser_transaction.cpp @@ -32,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_transaction::chaser_transaction(full_node& node) NOEXCEPT - : chaser(node), - tracker(node.log) + : chaser(node) { - subscribe(std::bind(&chaser_transaction::handle_event, this, _1, _2, _3)); } void chaser_transaction::handle_event(const code& ec, chase event_, @@ -56,11 +55,6 @@ void chaser_transaction::do_handle_event(const code& ec, chase event_, switch (event_) { - case chase::start: - { - handle_start(); - break; - } case chase::confirmed: { handle_confirmed(); @@ -72,9 +66,10 @@ void chaser_transaction::do_handle_event(const code& ec, chase event_, } // TODO: initialize tx graph from store, log and stop on error. -void chaser_transaction::handle_start() NOEXCEPT +bool chaser_transaction::start() NOEXCEPT { - BC_ASSERT_MSG(stranded(), "chaser_transaction"); + return subscribe(std::bind(&chaser_transaction::handle_event, + this, _1, _2, _3)); } // TODO: handle the new confirmed blocks (may issue 'transaction'). diff --git a/src/full_node.cpp b/src/full_node.cpp index ca3da86d..a709a999 100644 --- a/src/full_node.cpp +++ b/src/full_node.cpp @@ -40,7 +40,17 @@ full_node::full_node(query& query, const configuration& configuration, : p2p(configuration.network, log), config_(configuration), query_(query), - event_subscriber_(strand()) + event_subscriber_(strand()), + chaser_header_(*this), + chaser_check_(*this), + chaser_connect_(*this), + chaser_confirm_(*this), + chaser_transaction_(*this), + chaser_candidate_(*this) +{ +} + +full_node::~full_node() NOEXCEPT { } @@ -63,11 +73,14 @@ void full_node::do_start(const result_handler& handler) NOEXCEPT { BC_ASSERT_MSG(stranded(), "full_node"); - const auto ec = create_chasers(); - - if (ec) + if (!chaser_header_.start() || + !chaser_check_.start() || + !chaser_connect_.start() || + !chaser_confirm_.start() || + !chaser_transaction_.start() || + !chaser_candidate_.start()) { - handler(ec); + handler(error::unknown); return; } @@ -105,57 +118,19 @@ void full_node::do_close() NOEXCEPT { BC_ASSERT_MSG(stranded(), "full_node"); - stop_chasers(); + event_subscriber_.stop(network::error::service_stopped, + chaser::chase::stop, {}); + p2p::do_close(); - delete_chasers(); } // Chasers. // ---------------------------------------------------------------------------- -code full_node::create_chasers() NOEXCEPT -{ - BC_ASSERT_MSG(stranded(), "full_node"); - - // Create and subscribe all chasers. - chaser_header_ = std::make_unique(*this); - chaser_check_ = std::make_unique(*this); - chaser_connect_ = std::make_unique(*this); - chaser_confirm_ = std::make_unique(*this); - chaser_transaction_ = std::make_unique(*this); - chaser_candidate_ = std::make_unique(*this); - - // Post start event to all chasers. - event_subscriber_.notify(error::success, chaser::chase::start, {}); - return error::success; -} - -void full_node::stop_chasers() NOEXCEPT -{ - BC_ASSERT_MSG(stranded(), "full_node"); - - event_subscriber_.stop(network::error::service_stopped, - chaser::chase::stop, {}); -} - -// These should be reset upon destruct, which could only follow close(), which -// ensures that the threadpool is coalesced. Yet without explicit delete, msvc -// asserts on process termination. -void full_node::delete_chasers() NOEXCEPT -{ - chaser_header_.reset(); - chaser_check_.reset(); - chaser_connect_.reset(); - chaser_confirm_.reset(); - chaser_transaction_.reset(); - chaser_candidate_.reset(); -} - void full_node::organize(const system::chain::header::cptr& header, system::chain::context&& context) NOEXCEPT { - // TODO: guard against nullptr (or don't use pointers). - chaser_header_->organize(header, std::move(context)); + chaser_header_.organize(header, std::move(context)); } // Properties.