From c4d083c0b535bf9ea13c8dd8d341fb306182a797 Mon Sep 17 00:00:00 2001 From: evoskuil Date: Fri, 16 Feb 2024 15:33:35 -0500 Subject: [PATCH] Clean up chaser start/stop and error handling. --- include/bitcoin/node/chasers/chaser.hpp | 9 +++++--- .../bitcoin/node/chasers/chaser_candidate.hpp | 2 +- include/bitcoin/node/chasers/chaser_check.hpp | 2 +- .../bitcoin/node/chasers/chaser_confirm.hpp | 2 +- .../bitcoin/node/chasers/chaser_connect.hpp | 2 +- .../bitcoin/node/chasers/chaser_header.hpp | 3 +-- .../node/chasers/chaser_transaction.hpp | 2 +- src/chasers/chaser.cpp | 23 ++++++++----------- src/chasers/chaser_candidate.cpp | 15 ++++++------ src/chasers/chaser_check.cpp | 18 ++++++++------- src/chasers/chaser_confirm.cpp | 15 ++++++------ src/chasers/chaser_connect.cpp | 15 ++++++------ src/chasers/chaser_header.cpp | 17 +++++++------- src/chasers/chaser_transaction.cpp | 15 ++++++------ src/full_node.cpp | 17 +++++++------- 15 files changed, 82 insertions(+), 75 deletions(-) diff --git a/include/bitcoin/node/chasers/chaser.hpp b/include/bitcoin/node/chasers/chaser.hpp index 4f689f38..d241cb4d 100644 --- a/include/bitcoin/node/chasers/chaser.hpp +++ b/include/bitcoin/node/chasers/chaser.hpp @@ -84,7 +84,7 @@ class BCN_API chaser DELETE_COPY_MOVE(chaser); /// Synchronously subscribe to notify and asynchronously initialize state. - virtual bool start() NOEXCEPT = 0; + virtual code start() NOEXCEPT = 0; protected: chaser(full_node& node) NOEXCEPT; @@ -99,13 +99,16 @@ class BCN_API chaser /// True if the current thread is on the chaser strand. bool stranded() const NOEXCEPT; + /// True if the current thread is on the node strand. + bool node_stranded() const NOEXCEPT; + /// Subscribe to chaser events. - bool subscribe(event_handler&& handler) NOEXCEPT; + code subscribe(event_handler&& handler) NOEXCEPT; /// Set chaser event (does not require network strand). void notify(const code& ec, chase event_, link value) NOEXCEPT; - /// Close the node. + /// Close the node in case of failure. void stop(const code& ec) NOEXCEPT; private: diff --git a/include/bitcoin/node/chasers/chaser_candidate.hpp b/include/bitcoin/node/chasers/chaser_candidate.hpp index afd5ce24..bbfc00cc 100644 --- a/include/bitcoin/node/chasers/chaser_candidate.hpp +++ b/include/bitcoin/node/chasers/chaser_candidate.hpp @@ -36,7 +36,7 @@ class BCN_API chaser_candidate public: chaser_candidate(full_node& node) NOEXCEPT; - virtual bool start() NOEXCEPT; + virtual code start() NOEXCEPT; protected: virtual void handle_transaction(transaction_t value) NOEXCEPT; diff --git a/include/bitcoin/node/chasers/chaser_check.hpp b/include/bitcoin/node/chasers/chaser_check.hpp index 00b9a1ef..cb141298 100644 --- a/include/bitcoin/node/chasers/chaser_check.hpp +++ b/include/bitcoin/node/chasers/chaser_check.hpp @@ -36,7 +36,7 @@ class BCN_API chaser_check public: chaser_check(full_node& node) NOEXCEPT; - virtual bool start() NOEXCEPT; + virtual code start() NOEXCEPT; virtual void checked(const system::chain::block::cptr& block) NOEXCEPT; protected: diff --git a/include/bitcoin/node/chasers/chaser_confirm.hpp b/include/bitcoin/node/chasers/chaser_confirm.hpp index 9638a28f..d6ca4c3b 100644 --- a/include/bitcoin/node/chasers/chaser_confirm.hpp +++ b/include/bitcoin/node/chasers/chaser_confirm.hpp @@ -36,7 +36,7 @@ class BCN_API chaser_confirm public: chaser_confirm(full_node& node) NOEXCEPT; - virtual bool start() NOEXCEPT; + virtual code start() NOEXCEPT; protected: virtual void handle_connected(header_t block) NOEXCEPT; diff --git a/include/bitcoin/node/chasers/chaser_connect.hpp b/include/bitcoin/node/chasers/chaser_connect.hpp index 4442237e..dd8c3aab 100644 --- a/include/bitcoin/node/chasers/chaser_connect.hpp +++ b/include/bitcoin/node/chasers/chaser_connect.hpp @@ -36,7 +36,7 @@ class BCN_API chaser_connect public: chaser_connect(full_node& node) NOEXCEPT; - virtual bool start() NOEXCEPT; + virtual code start() NOEXCEPT; protected: virtual void handle_checked(header_t block) NOEXCEPT; diff --git a/include/bitcoin/node/chasers/chaser_header.hpp b/include/bitcoin/node/chasers/chaser_header.hpp index 034b5948..79f16297 100644 --- a/include/bitcoin/node/chasers/chaser_header.hpp +++ b/include/bitcoin/node/chasers/chaser_header.hpp @@ -19,7 +19,6 @@ #ifndef LIBBITCOIN_NODE_CHASERS_CHASER_HEADER_HPP #define LIBBITCOIN_NODE_CHASERS_CHASER_HEADER_HPP -#include #include #include #include @@ -40,7 +39,7 @@ class BCN_API chaser_header public: chaser_header(full_node& node) NOEXCEPT; - virtual bool start() NOEXCEPT; + virtual code start() NOEXCEPT; /// Organize the next header in sequence, relative to caller's peer. /// Causes a fault/stop if preceding headers have not been stored. diff --git a/include/bitcoin/node/chasers/chaser_transaction.hpp b/include/bitcoin/node/chasers/chaser_transaction.hpp index 6ebf7f5d..6f982e81 100644 --- a/include/bitcoin/node/chasers/chaser_transaction.hpp +++ b/include/bitcoin/node/chasers/chaser_transaction.hpp @@ -36,7 +36,7 @@ class BCN_API chaser_transaction public: chaser_transaction(full_node& node) NOEXCEPT; - virtual bool start() NOEXCEPT; + code start() NOEXCEPT override; virtual void store(const system::chain::transaction::cptr& block) NOEXCEPT; protected: diff --git a/src/chasers/chaser.cpp b/src/chasers/chaser.cpp index dbba211e..332c6790 100644 --- a/src/chasers/chaser.cpp +++ b/src/chasers/chaser.cpp @@ -58,18 +58,15 @@ bool chaser::stranded() const NOEXCEPT return strand_.running_in_this_thread(); } -bool chaser::subscribe(event_handler&& handler) NOEXCEPT +bool chaser::node_stranded() const NOEXCEPT { - BC_ASSERT_MSG(node_.stranded(), "chaser"); - const auto ec = subscriber_.subscribe(std::move(handler)); - - if (ec) - { - LOGF("Chaser subscribe fault, " << ec.message()); - return false; - } + return node_.stranded(); +} - return true; +code chaser::subscribe(event_handler&& handler) NOEXCEPT +{ + BC_ASSERT_MSG(node_stranded(), "chaser"); + return subscriber_.subscribe(std::move(handler)); } // Posts to network strand (call from chaser strands). @@ -82,13 +79,13 @@ void chaser::notify(const code& ec, chase event_, link value) NOEXCEPT // Executed on network strand (handler should bounce to chaser strand). void chaser::do_notify(const code& ec, chase event_, link value) NOEXCEPT { - BC_ASSERT_MSG(node_.stranded(), "chaser"); + BC_ASSERT_MSG(node_stranded(), "chaser"); subscriber_.notify(ec, event_, value); } -void chaser::stop(const code&) NOEXCEPT +void chaser::stop(const code& ec) NOEXCEPT { - ////LOGF("Chaser fault, " << ec.message()); + LOGF("Chaser fault, " << ec.message()); node_.close(); } diff --git a/src/chasers/chaser_candidate.cpp b/src/chasers/chaser_candidate.cpp index 26e8ca4a..0d0cb81e 100644 --- a/src/chasers/chaser_candidate.cpp +++ b/src/chasers/chaser_candidate.cpp @@ -38,6 +38,14 @@ chaser_candidate::chaser_candidate(full_node& node) NOEXCEPT { } +// TODO: initialize candidate state. +code chaser_candidate::start() NOEXCEPT +{ + BC_ASSERT_MSG(node_stranded(), "chaser_check"); + return subscribe(std::bind(&chaser_candidate::handle_event, + this, _1, _2, _3)); +} + void chaser_candidate::handle_event(const code& ec, chase event_, link value) NOEXCEPT { @@ -66,13 +74,6 @@ void chaser_candidate::do_handle_event(const code& ec, chase event_, } } -// TODO: initialize candidate state. -bool chaser_candidate::start() NOEXCEPT -{ - 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(transaction_t tx) NOEXCEPT { diff --git a/src/chasers/chaser_check.cpp b/src/chasers/chaser_check.cpp index 03d39dfc..c3b979f7 100644 --- a/src/chasers/chaser_check.cpp +++ b/src/chasers/chaser_check.cpp @@ -39,6 +39,16 @@ chaser_check::chaser_check(full_node& node) NOEXCEPT { } +// TODO: initialize check state. +code chaser_check::start() NOEXCEPT +{ + BC_ASSERT_MSG(node_stranded(), "chaser_check"); + + // get_all_unassociated_above(0) + return subscribe(std::bind(&chaser_check::handle_event, + this, _1, _2, _3)); +} + void chaser_check::handle_event(const code& ec, chase event_, link value) NOEXCEPT { @@ -67,14 +77,6 @@ void chaser_check::do_handle_event(const code& ec, chase event_, } } -// TODO: initialize check state. -bool chaser_check::start() NOEXCEPT -{ - // 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 { diff --git a/src/chasers/chaser_confirm.cpp b/src/chasers/chaser_confirm.cpp index 1171e73f..c6254a0b 100644 --- a/src/chasers/chaser_confirm.cpp +++ b/src/chasers/chaser_confirm.cpp @@ -37,6 +37,14 @@ chaser_confirm::chaser_confirm(full_node& node) NOEXCEPT { } +// TODO: initialize confirm state. +code chaser_confirm::start() NOEXCEPT +{ + BC_ASSERT_MSG(node_stranded(), "chaser_confirm"); + return subscribe(std::bind(&chaser_confirm::handle_event, + this, _1, _2, _3)); +} + void chaser_confirm::handle_event(const code& ec, chase event_, link value) NOEXCEPT { @@ -65,13 +73,6 @@ void chaser_confirm::do_handle_event(const code& ec, chase event_, } } -// TODO: initialize confirm state. -bool chaser_confirm::start() NOEXCEPT -{ - 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(header_t block) NOEXCEPT { diff --git a/src/chasers/chaser_connect.cpp b/src/chasers/chaser_connect.cpp index c7a3f4fa..8e1a3463 100644 --- a/src/chasers/chaser_connect.cpp +++ b/src/chasers/chaser_connect.cpp @@ -37,6 +37,14 @@ chaser_connect::chaser_connect(full_node& node) NOEXCEPT { } +// TODO: initialize connect state. +code chaser_connect::start() NOEXCEPT +{ + BC_ASSERT_MSG(node_stranded(), "chaser_connect"); + return subscribe(std::bind(&chaser_connect::handle_event, + this, _1, _2, _3)); +} + void chaser_connect::handle_event(const code& ec, chase event_, link value) NOEXCEPT { @@ -65,13 +73,6 @@ void chaser_connect::do_handle_event(const code& ec, chase event_, } } -// TODO: initialize connect state. -bool chaser_connect::start() NOEXCEPT -{ - 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(header_t block) NOEXCEPT { diff --git a/src/chasers/chaser_header.cpp b/src/chasers/chaser_header.cpp index 363fba6c..d1abae02 100644 --- a/src/chasers/chaser_header.cpp +++ b/src/chasers/chaser_header.cpp @@ -34,7 +34,6 @@ 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()), @@ -42,6 +41,14 @@ chaser_header::chaser_header(full_node& node) NOEXCEPT { } +// protected +code chaser_header::start() NOEXCEPT +{ + BC_ASSERT_MSG(node_stranded(), "chaser_header"); + return subscribe(std::bind(&chaser_header::handle_event, + this, _1, _2, _3)); +} + // protected void chaser_header::handle_event(const code& ec, chase event_, link value) NOEXCEPT @@ -57,13 +64,6 @@ void chaser_header::do_handle_event(const code&, chase, link) NOEXCEPT BC_ASSERT_MSG(stranded(), "chaser_header"); } -// protected -bool chaser_header::start() NOEXCEPT -{ - return subscribe(std::bind(&chaser_header::handle_event, - this, _1, _2, _3)); -} - void chaser_header::organize(const chain::header::cptr& header, chain::context&& context) NOEXCEPT { @@ -258,6 +258,7 @@ bool chaser_header::get_is_strong(bool& strong, const uint256_t& work, return true; } +// protected void chaser_header::save(const chain::header::cptr& header, const chain::context& context) NOEXCEPT { diff --git a/src/chasers/chaser_transaction.cpp b/src/chasers/chaser_transaction.cpp index 757eaa81..ee068cb3 100644 --- a/src/chasers/chaser_transaction.cpp +++ b/src/chasers/chaser_transaction.cpp @@ -38,6 +38,14 @@ chaser_transaction::chaser_transaction(full_node& node) NOEXCEPT { } +// TODO: initialize tx graph from store, log and stop on error. +code chaser_transaction::start() NOEXCEPT +{ + BC_ASSERT_MSG(node_stranded(), "chaser_transaction"); + return subscribe(std::bind(&chaser_transaction::handle_event, + this, _1, _2, _3)); +} + void chaser_transaction::handle_event(const code& ec, chase event_, link value) NOEXCEPT { @@ -65,13 +73,6 @@ void chaser_transaction::do_handle_event(const code& ec, chase event_, } } -// TODO: initialize tx graph from store, log and stop on error. -bool chaser_transaction::start() NOEXCEPT -{ - return subscribe(std::bind(&chaser_transaction::handle_event, - this, _1, _2, _3)); -} - // TODO: handle the new confirmed blocks (may issue 'transaction'). void chaser_transaction::handle_confirmed() NOEXCEPT { diff --git a/src/full_node.cpp b/src/full_node.cpp index a709a999..ed644f88 100644 --- a/src/full_node.cpp +++ b/src/full_node.cpp @@ -72,15 +72,16 @@ void full_node::start(result_handler&& handler) NOEXCEPT void full_node::do_start(const result_handler& handler) NOEXCEPT { BC_ASSERT_MSG(stranded(), "full_node"); - - if (!chaser_header_.start() || - !chaser_check_.start() || - !chaser_connect_.start() || - !chaser_confirm_.start() || - !chaser_transaction_.start() || - !chaser_candidate_.start()) + code ec; + + if (((ec = chaser_header_.start())) || + ((ec = chaser_check_.start())) || + ((ec = chaser_connect_.start())) || + ((ec = chaser_confirm_.start())) || + ((ec = chaser_transaction_.start())) || + ((ec = chaser_candidate_.start()))) { - handler(error::unknown); + handler(ec); return; }