Skip to content

Commit

Permalink
Clean up chaser start/stop and error handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
evoskuil committed Feb 16, 2024
1 parent 66a8271 commit c4d083c
Show file tree
Hide file tree
Showing 15 changed files with 82 additions and 75 deletions.
9 changes: 6 additions & 3 deletions include/bitcoin/node/chasers/chaser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion include/bitcoin/node/chasers/chaser_candidate.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion include/bitcoin/node/chasers/chaser_check.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion include/bitcoin/node/chasers/chaser_confirm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion include/bitcoin/node/chasers/chaser_connect.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 1 addition & 2 deletions include/bitcoin/node/chasers/chaser_header.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
#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 @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion include/bitcoin/node/chasers/chaser_transaction.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
23 changes: 10 additions & 13 deletions src/chasers/chaser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand All @@ -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();
}

Expand Down
15 changes: 8 additions & 7 deletions src/chasers/chaser_candidate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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
{
Expand Down
18 changes: 10 additions & 8 deletions src/chasers/chaser_check.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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
{
Expand Down
15 changes: 8 additions & 7 deletions src/chasers/chaser_confirm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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
{
Expand Down
15 changes: 8 additions & 7 deletions src/chasers/chaser_connect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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
{
Expand Down
17 changes: 9 additions & 8 deletions src/chasers/chaser_header.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,21 @@ 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())
{
}

// 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
Expand All @@ -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
{
Expand Down Expand Up @@ -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
{
Expand Down
15 changes: 8 additions & 7 deletions src/chasers/chaser_transaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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
{
Expand Down
17 changes: 9 additions & 8 deletions src/full_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down

0 comments on commit c4d083c

Please sign in to comment.