Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix chaser and performance handler stranding. #530

Merged
merged 4 commits into from
Feb 11, 2024
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
5 changes: 4 additions & 1 deletion include/bitcoin/node/chasers/chaser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,13 @@ class BCN_API chaser
/// Close the node.
void close(const code& ec) NOEXCEPT;

/// The chaser's strand.
network::asio::strand& strand() NOEXCEPT;

/// True if the current thread is on the chaser strand.
bool stranded() const NOEXCEPT;

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

/// Set chaser event (does not require network strand).
Expand Down
7 changes: 5 additions & 2 deletions include/bitcoin/node/chasers/chaser_candidate.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,17 @@ namespace node {
class full_node;

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

chaser_candidate(full_node& node) NOEXCEPT;

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

} // namespace node
Expand Down
4 changes: 2 additions & 2 deletions include/bitcoin/node/chasers/chaser_check.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,17 @@ namespace node {
class full_node;

/// Chase down blocks for the candidate header chain.
/// Notify subscribers with "block checked" event.
class BCN_API chaser_check
: public chaser, protected network::tracker<chaser_check>
{
public:
typedef std::unique_ptr<chaser_check> ptr;
typedef std::unique_ptr<chaser_check> uptr;

chaser_check(full_node& node) NOEXCEPT;

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

} // namespace node
Expand Down
7 changes: 5 additions & 2 deletions include/bitcoin/node/chasers/chaser_confirm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,17 @@ namespace node {
class full_node;

/// Chase down valid blocks for confirmation.
/// Notify subscribers with "block confirmed" event.
class BCN_API chaser_confirm
: public chaser, protected network::tracker<chaser_confirm>
{
public:
typedef std::unique_ptr<chaser_confirm> ptr;
typedef std::unique_ptr<chaser_confirm> uptr;

chaser_confirm(full_node& node) NOEXCEPT;

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

} // namespace node
Expand Down
7 changes: 5 additions & 2 deletions include/bitcoin/node/chasers/chaser_connect.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,17 @@ namespace node {
class full_node;

/// Chase down blocks in the the candidate header chain for validation.
/// Notify subscribers with the "block connected" event.
class BCN_API chaser_connect
: public chaser, protected network::tracker<chaser_connect>
{
public:
typedef std::unique_ptr<chaser_connect> ptr;
typedef std::unique_ptr<chaser_connect> uptr;

chaser_connect(full_node& node) NOEXCEPT;

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

} // namespace node
Expand Down
7 changes: 5 additions & 2 deletions include/bitcoin/node/chasers/chaser_header.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,17 @@ namespace node {
class full_node;

/// Chase down stronger header branches for the candidate chain.
/// Notify subscribers with "strong header" event.
class BCN_API chaser_header
: public chaser, protected network::tracker<chaser_header>
{
public:
typedef std::unique_ptr<chaser_header> ptr;
typedef std::unique_ptr<chaser_header> uptr;

chaser_header(full_node& node) NOEXCEPT;

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

} // namespace node
Expand Down
6 changes: 5 additions & 1 deletion include/bitcoin/node/chasers/chaser_transaction.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,13 @@ class BCN_API chaser_transaction
: public chaser, protected network::tracker<chaser_transaction>
{
public:
typedef std::unique_ptr<chaser_transaction> ptr;
typedef std::unique_ptr<chaser_transaction> uptr;

chaser_transaction(full_node& node) NOEXCEPT;

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

} // namespace node
Expand Down
13 changes: 7 additions & 6 deletions include/bitcoin/node/full_node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ class BCN_API full_node
protected:
virtual code create_chasers() NOEXCEPT;
virtual void stop_chasers() NOEXCEPT;
virtual void delete_chasers() NOEXCEPT;

/// Session attachments.
/// -----------------------------------------------------------------------
Expand All @@ -89,12 +90,12 @@ class BCN_API full_node

// These are protected by strand.
chaser::event_subscriber event_subscriber_;
chaser_header::ptr chaser_header_{};
chaser_check::ptr chaser_check_{};
chaser_connect::ptr chaser_connect_{};
chaser_confirm::ptr chaser_confirm_{};
chaser_transaction::ptr chaser_transaction_{};
chaser_candidate::ptr chaser_candidate_{};
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_{};
};

} // namespace node
Expand Down
3 changes: 3 additions & 0 deletions include/bitcoin/node/protocols/protocol_block_in.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ class BCN_API protocol_block_in
network::messages::get_data create_get_data(
const network::messages::inventory& message) const NOEXCEPT;


void do_handle_performance(const code& ec) NOEXCEPT;

// Thread safe.
const bool report_performance_;
const network::messages::inventory::type_id block_type_;
Expand Down
7 changes: 7 additions & 0 deletions src/chasers/chaser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
namespace libbitcoin {
namespace node {

using namespace network;

BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)

chaser::chaser(full_node& node) NOEXCEPT
Expand All @@ -48,6 +50,11 @@ void chaser::close(const code& ec) NOEXCEPT
node_.close();
}

asio::strand& chaser::strand() NOEXCEPT
{
return strand_;
}

bool chaser::stranded() const NOEXCEPT
{
return strand_.running_in_this_thread();
Expand Down
32 changes: 32 additions & 0 deletions src/chasers/chaser_candidate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,51 @@
*/
#include <bitcoin/node/chasers/chaser_candidate.hpp>

#include <functional>
#include <bitcoin/network.hpp>
#include <bitcoin/node/error.hpp>
#include <bitcoin/node/full_node.hpp>
#include <bitcoin/node/chasers/chaser.hpp>

namespace libbitcoin {
namespace node {

using namespace std::placeholders;

BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)

chaser_candidate::chaser_candidate(full_node& node) NOEXCEPT
: chaser(node),
tracker<chaser_candidate>(node.log)
{
subscribe(std::bind(&chaser_candidate::handle_event, this, _1, _2));
}

void chaser_candidate::handle_event(const code& ec, chase value) NOEXCEPT
{
boost::asio::post(strand(),
std::bind(&chaser_candidate::do_handle_event, this, ec, value));
}

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

// The code should be error::service_stopped when error::stop is set.
if (ec)
return;

switch (value)
{
case chase::start:
// TODO: initialize.
break;
case chase::transaction:
// TODO: handle transaction graph change (may issue 'candidate').
break;
default:
return;
}
}

BC_POP_WARNING()
Expand Down
6 changes: 6 additions & 0 deletions src/chasers/chaser_check.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ chaser_check::chaser_check(full_node& node) NOEXCEPT
}

void chaser_check::handle_event(const code& ec, chase value) NOEXCEPT
{
boost::asio::post(strand(),
std::bind(&chaser_check::do_handle_event, this, ec, value));
}

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

Expand Down
32 changes: 32 additions & 0 deletions src/chasers/chaser_confirm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,51 @@
*/
#include <bitcoin/node/chasers/chaser_confirm.hpp>

#include <functional>
#include <bitcoin/network.hpp>
#include <bitcoin/node/error.hpp>
#include <bitcoin/node/full_node.hpp>
#include <bitcoin/node/chasers/chaser.hpp>

namespace libbitcoin {
namespace node {

using namespace std::placeholders;

BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)

chaser_confirm::chaser_confirm(full_node& node) NOEXCEPT
: chaser(node),
tracker<chaser_confirm>(node.log)
{
subscribe(std::bind(&chaser_confirm::handle_event, this, _1, _2));
}

void chaser_confirm::handle_event(const code& ec, chase value) NOEXCEPT
{
boost::asio::post(strand(),
std::bind(&chaser_confirm::do_handle_event, this, ec, value));
}

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

// The code should be error::service_stopped when error::stop is set.
if (ec)
return;

switch (value)
{
case chase::start:
// TODO: initialize.
break;
case chase::connected:
// TODO: handle new strong connected branch (may issue 'confirmed').
break;
default:
return;
}
}

BC_POP_WARNING()
Expand Down
32 changes: 32 additions & 0 deletions src/chasers/chaser_connect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,51 @@
*/
#include <bitcoin/node/chasers/chaser_connect.hpp>

#include <functional>
#include <bitcoin/network.hpp>
#include <bitcoin/node/error.hpp>
#include <bitcoin/node/full_node.hpp>
#include <bitcoin/node/chasers/chaser.hpp>

namespace libbitcoin {
namespace node {

using namespace std::placeholders;

BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)

chaser_connect::chaser_connect(full_node& node) NOEXCEPT
: chaser(node),
tracker<chaser_connect>(node.log)
{
subscribe(std::bind(&chaser_connect::handle_event, this, _1, _2));
}

void chaser_connect::handle_event(const code& ec, chase value) NOEXCEPT
{
boost::asio::post(strand(),
std::bind(&chaser_connect::do_handle_event, this, ec, value));
}

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

// The code should be error::service_stopped when error::stop is set.
if (ec)
return;

switch (value)
{
case chase::start:
// TODO: initialize.
break;
case chase::checked:
// TODO: handle the new checked blocks (may issue 'connected').
break;
default:
return;
}
}

BC_POP_WARNING()
Expand Down
Loading
Loading