Skip to content

Commit

Permalink
Use __VA_ARGS__ to simplify variadic macro, update macros.
Browse files Browse the repository at this point in the history
  • Loading branch information
evoskuil committed Feb 29, 2024
1 parent 228401a commit 135cc79
Show file tree
Hide file tree
Showing 13 changed files with 60 additions and 62 deletions.
37 changes: 17 additions & 20 deletions include/bitcoin/node/chasers/chaser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,20 @@ class BCN_API chaser
virtual code start() NOEXCEPT = 0;

protected:
/// Bind a method (use BIND).
template <class Derived, typename Method, typename... Args>
auto bind(Method&& method, Args&&... args) NOEXCEPT
{
return BIND_THIS(method, args);
}

/// Post a method to channel strand (use POST).
template <class Derived, typename Method, typename... Args>
auto post(Method&& method, Args&&... args) NOEXCEPT
{
return boost::asio::post(strand(), BIND_THIS(method, args));
}

chaser(full_node& node) NOEXCEPT;
~chaser() NOEXCEPT;

Expand All @@ -128,7 +142,7 @@ class BCN_API chaser
bool node_stranded() const NOEXCEPT;

/// Subscribe to chaser events.
code subscribe(event_handler&& handler) NOEXCEPT;
code subscribe_event(event_handler&& handler) NOEXCEPT;

/// Set chaser event (does not require network strand).
void notify(const code& ec, chase event_, link value) NOEXCEPT;
Expand All @@ -144,25 +158,8 @@ class BCN_API chaser
event_subscriber& subscriber_;
};

// These are distinct from network binding macros because of binding 'this'.

#define BIND_1(method, p1) \
std::bind(&CLASS::method, this, p1)
#define BIND_2(method, p1, p2) \
std::bind(&CLASS::method, this, p1, p2)
#define BIND_3(method, p1, p2, p3) \
std::bind(&CLASS::method, this, p1, p2, p3)

#define POST_1(method, p1) \
boost::asio::post(strand(), BIND_1(method, p1));
#define POST_2(method, p1, p2) \
boost::asio::post(strand(), BIND_2(method, p1, p2));
#define POST_3(method, p1, p2, p3) \
boost::asio::post(strand(), BIND_3(method, p1, p2, p3));

#define POST_EVENT(method, type, p1) \
BC_ASSERT(std::holds_alternative<type>(p1)); \
POST_1(method, std::get<type>(p1));
#define SUBSCRIBE_EVENT(method, ...) \
subscribe_event(BIND(method, __VA_ARGS__))

} // namespace node
} // namespace libbitcoin
Expand Down
4 changes: 2 additions & 2 deletions src/chasers/chaser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ bool chaser::node_stranded() const NOEXCEPT
return node_.stranded();
}

code chaser::subscribe(event_handler&& handler) NOEXCEPT
code chaser::subscribe_event(event_handler&& handler) NOEXCEPT
{
BC_ASSERT_MSG(node_stranded(), "chaser");
return subscriber_.subscribe(std::move(handler));
Expand All @@ -86,7 +86,7 @@ code chaser::subscribe(event_handler&& handler) NOEXCEPT
// Posts to network strand (call from chaser strands).
void chaser::notify(const code& ec, chase event_, link value) NOEXCEPT
{
POST_3(do_notify, ec, event_, value);
POST(do_notify, ec, event_, value);
}

// Executed on network strand (handler should bounce to chaser strand).
Expand Down
6 changes: 3 additions & 3 deletions src/chasers/chaser_block.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ code chaser_block::start() NOEXCEPT
top_state_ = archive().get_candidate_chain_state(
config().bitcoin, archive().get_top_candidate());

return subscribe(BIND_3(handle_event, _1, _2, _3));
return SUBSCRIBE_EVENT(handle_event, _1, _2, _3);
}

// event handlers
Expand All @@ -72,7 +72,7 @@ void chaser_block::handle_event(const code&, chase event_,
{
if (event_ == chase::unconnected)
{
POST_EVENT(handle_unconnected, height_t, value);
POST(handle_unconnected, std::get<height_t>(value));
}
}

Expand All @@ -87,7 +87,7 @@ void chaser_block::handle_unconnected(height_t) NOEXCEPT
void chaser_block::organize(const block::cptr& block,
organize_handler&& handler) NOEXCEPT
{
POST_2(do_organize, block, std::move(handler));
POST(do_organize, block, std::move(handler));
}

void chaser_block::do_organize(const block::cptr& block_ptr,
Expand Down
5 changes: 3 additions & 2 deletions src/chasers/chaser_candidate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ chaser_candidate::~chaser_candidate() NOEXCEPT
code chaser_candidate::start() NOEXCEPT
{
BC_ASSERT_MSG(node_stranded(), "chaser_check");
return subscribe(BIND_3(handle_event, _1, _2, _3));

return SUBSCRIBE_EVENT(handle_event, _1, _2, _3);
}

// event handlers
Expand All @@ -61,7 +62,7 @@ void chaser_candidate::handle_event(const code&, chase event_,
{
if (event_ == chase::transaction)
{
POST_EVENT(handle_transaction, transaction_t, value);
POST(handle_transaction, std::get<transaction_t>(value));
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/chasers/chaser_check.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ code chaser_check::start() NOEXCEPT
map_ = std::make_shared<database::context_map>(
archive().get_all_unassociated_above(zero));

return subscribe(BIND_3(handle_event, _1, _2, _3));
return SUBSCRIBE_EVENT(handle_event, _1, _2, _3);
}

// event handlers
Expand All @@ -69,7 +69,7 @@ void chaser_check::handle_event(const code&, chase event_,
{
if (event_ == chase::header)
{
POST_EVENT(handle_header, height_t, value);
POST(handle_header, std::get<height_t>(value));
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/chasers/chaser_confirm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ chaser_confirm::~chaser_confirm() NOEXCEPT
code chaser_confirm::start() NOEXCEPT
{
BC_ASSERT_MSG(node_stranded(), "chaser_confirm");
return subscribe(BIND_3(handle_event, _1, _2, _3));
return SUBSCRIBE_EVENT(handle_event, _1, _2, _3);
}

// event handlers
Expand All @@ -60,7 +60,7 @@ void chaser_confirm::handle_event(const code&, chase event_,
{
if (event_ == chase::connected)
{
POST_EVENT(handle_connected, header_t, value);
POST(handle_connected, std::get<header_t>(value));
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/chasers/chaser_connect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ chaser_connect::~chaser_connect() NOEXCEPT
code chaser_connect::start() NOEXCEPT
{
BC_ASSERT_MSG(node_stranded(), "chaser_connect");
return subscribe(BIND_3(handle_event, _1, _2, _3));
return SUBSCRIBE_EVENT(handle_event, _1, _2, _3);
}

// event handlers
Expand All @@ -60,7 +60,7 @@ void chaser_connect::handle_event(const code&, chase event_,
{
if (event_ == chase::connected)
{
POST_EVENT(handle_checked, header_t, value);
POST(handle_checked, std::get<header_t>(value));
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/chasers/chaser_header.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ code chaser_header::start() NOEXCEPT
top_state_ = archive().get_candidate_chain_state(
config().bitcoin, archive().get_top_candidate());

return subscribe(BIND_3(handle_event, _1, _2, _3));
return SUBSCRIBE_EVENT(handle_event, _1, _2, _3);
}

// event handlers
Expand All @@ -80,7 +80,7 @@ void chaser_header::handle_event(const code&, chase event_,
{
if (event_ == chase::unchecked)
{
POST_EVENT(handle_unchecked, height_t, value);
POST(handle_unchecked, std::get<height_t>(value));
}
}

Expand All @@ -107,7 +107,7 @@ void chaser_header::handle_unchecked(height_t) NOEXCEPT
void chaser_header::organize(const header::cptr& header,
organize_handler&& handler) NOEXCEPT
{
POST_2(do_organize, header, std::move(handler));
POST(do_organize, header, std::move(handler));
}

void chaser_header::do_organize(const header::cptr& header_ptr,
Expand Down
4 changes: 2 additions & 2 deletions src/chasers/chaser_transaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ chaser_transaction::~chaser_transaction() NOEXCEPT
code chaser_transaction::start() NOEXCEPT
{
BC_ASSERT_MSG(node_stranded(), "chaser_transaction");
return subscribe(BIND_3(handle_event, _1, _2, _3));
return SUBSCRIBE_EVENT(handle_event, _1, _2, _3);
}

// event handlers
Expand All @@ -61,7 +61,7 @@ void chaser_transaction::handle_event(const code&, chase event_,
{
if (event_ == chase::confirmed)
{
POST_EVENT(handle_confirmed, header_t, value);
POST(handle_confirmed, std::get<header_t>(value));
}
}

Expand Down
14 changes: 7 additions & 7 deletions src/protocols/protocol_block_in.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ void protocol_block_in::start() NOEXCEPT
if (started())
return;

SUBSCRIBE_CHANNEL2(inventory, handle_receive_inventory, _1, _2);
SEND1(create_get_inventory(), handle_send, _1);
SUBSCRIBE_CHANNEL(inventory, handle_receive_inventory, _1, _2);
SEND(create_get_inventory(), handle_send, _1);
protocol::start();
}

Expand Down Expand Up @@ -90,7 +90,7 @@ bool protocol_block_in::handle_receive_inventory(const code& ec,
if (message->items.size() == max_get_blocks)
{
LOGP("Get inventory [" << authority() << "] (empty maximal).");
SEND1(create_get_inventory(message->items.back().hash),
SEND(create_get_inventory(message->items.back().hash),
handle_send, _1);
}

Expand All @@ -109,8 +109,8 @@ bool protocol_block_in::handle_receive_inventory(const code& ec,

// TODO: these should be limited in quantity for DOS protection.
// There is one block subscription for each received unexhausted inventory.
SUBSCRIBE_CHANNEL3(block, handle_receive_block, _1, _2, tracker);
SEND1(getter, handle_send, _1);
SUBSCRIBE_CHANNEL(block, handle_receive_block, _1, _2, tracker);
SEND(getter, handle_send, _1);
return true;
}

Expand Down Expand Up @@ -138,7 +138,7 @@ bool protocol_block_in::handle_receive_block(const code& ec,
// A job backlog will occur when organize is slower than download.
// This is not a material issue when checkpoints bypass validation.
// The backlog may take minutes to clear upon shutdown.
organize(block_ptr, BIND3(handle_organize, _1, _2, block_ptr));
organize(block_ptr, BIND(handle_organize, _1, _2, block_ptr));

// Order is reversed, so next is at back.
tracker->hashes.pop_back();
Expand All @@ -150,7 +150,7 @@ bool protocol_block_in::handle_receive_block(const code& ec,
if (tracker->announced == max_get_blocks)
{
LOGP("Get inventory [" << authority() << "] (exhausted maximal).");
SEND1(create_get_inventory(tracker->last), handle_send, _1);
SEND(create_get_inventory(tracker->last), handle_send, _1);
}
else
{
Expand Down
24 changes: 12 additions & 12 deletions src/protocols/protocol_block_in_31800.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,12 @@ void protocol_block_in_31800::handle_performance_timer(const code& ec) NOEXCEPT
// Bounces to network strand, performs work, then calls handler.
// Channel will continue to process blocks while this call excecutes on the
// network strand. Timer will not be restarted until this call completes.
performance(identifier(), rate, BIND1(handle_performance, ec));
performance(identifier(), rate, BIND(handle_performance, ec));
}

void protocol_block_in_31800::handle_performance(const code& ec) NOEXCEPT
{
POST1(do_handle_performance, ec);
POST(do_handle_performance, ec);
}

void protocol_block_in_31800::do_handle_performance(const code& ec) NOEXCEPT
Expand All @@ -99,7 +99,7 @@ void protocol_block_in_31800::do_handle_performance(const code& ec) NOEXCEPT
return;
};

performance_timer_->start(BIND1(handle_performance_timer, _1));
performance_timer_->start(BIND(handle_performance_timer, _1));
}

// Start/stop.
Expand All @@ -115,14 +115,14 @@ void protocol_block_in_31800::start() NOEXCEPT
if (report_performance_)
{
start_ = steady_clock::now();
performance_timer_->start(BIND1(handle_performance_timer, _1));
performance_timer_->start(BIND(handle_performance_timer, _1));
}

// TODO: Subscribe to chaser events through session/full_node.
/*subscribe*/(BIND3(handle_event, _1, _2, _3));
/*subscribe*/(BIND(handle_event, _1, _2, _3));

SUBSCRIBE_CHANNEL2(block, handle_receive_block, _1, _2);
get_hashes(BIND2(handle_get_hashes, _1, _2));
SUBSCRIBE_CHANNEL(block, handle_receive_block, _1, _2);
get_hashes(BIND(handle_get_hashes, _1, _2));
protocol::start();
}

Expand All @@ -132,7 +132,7 @@ void protocol_block_in_31800::handle_event(const code&,
if (event_ == chaser::chase::unassociated)
{
BC_ASSERT(std::holds_alternative<chaser::header_t>(value));
POST1(handle_unassociated, std::get<chaser::header_t>(value));
POST(handle_unassociated, std::get<chaser::header_t>(value));
}
}

Expand All @@ -147,7 +147,7 @@ void protocol_block_in_31800::stopping(const code& ec) NOEXCEPT
BC_ASSERT_MSG(stranded(), "protocol_block_in_31800");

performance_timer_->stop();
put_hashes(map_, BIND1(handle_put_hashes, _1));
put_hashes(map_, BIND(handle_put_hashes, _1));
protocol::stopping(ec);
}

Expand All @@ -174,13 +174,13 @@ void protocol_block_in_31800::handle_get_hashes(const code& ec,
return;
}

POST1(send_get_data, map);
POST(send_get_data, map);
}

void protocol_block_in_31800::send_get_data(const map_ptr& map) NOEXCEPT
{
BC_ASSERT_MSG(stranded(), "protocol_block_in_31800");
SEND1(create_get_data(map), handle_send, _1);
SEND(create_get_data(map), handle_send, _1);
}

void protocol_block_in_31800::handle_put_hashes(const code& ec) NOEXCEPT
Expand Down Expand Up @@ -252,7 +252,7 @@ bool protocol_block_in_31800::handle_receive_block(const code& ec,
if (map_->empty())
{
LOGP("Getting more block hashes for [" << authority() << "].");
get_hashes(BIND2(handle_get_hashes, _1, _2));
get_hashes(BIND(handle_get_hashes, _1, _2));
}

return true;
Expand Down
8 changes: 4 additions & 4 deletions src/protocols/protocol_header_in_31800.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ void protocol_header_in_31800::start() NOEXCEPT
if (started())
return;

SUBSCRIBE_CHANNEL2(headers, handle_receive_headers, _1, _2);
SEND1(create_get_headers(), handle_send, _1);
SUBSCRIBE_CHANNEL(headers, handle_receive_headers, _1, _2);
SEND(create_get_headers(), handle_send, _1);
protocol::start();
}

Expand Down Expand Up @@ -81,15 +81,15 @@ bool protocol_header_in_31800::handle_receive_headers(const code& ec,
if (!query.is_header(header_ptr->hash()))
{
// Asynchronous organization serves all channels.
organize(header_ptr, BIND3(handle_organize, _1, _2, header_ptr));
organize(header_ptr, BIND(handle_organize, _1, _2, header_ptr));
}
}

// Protocol presumes max_get_headers unless complete.
// The headers response to get_headers is limited to max_get_headers.
if (message->header_ptrs.size() == max_get_headers)
{
SEND1(create_get_headers(message->header_ptrs.back()->hash()),
SEND(create_get_headers(message->header_ptrs.back()->hash()),
handle_send, _1);
}
else
Expand Down
2 changes: 1 addition & 1 deletion src/protocols/protocol_header_in_70012.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ void protocol_header_in_70012::complete() NOEXCEPT

if (!sent_)
{
SEND1(send_headers{}, handle_send, _1);
SEND(send_headers{}, handle_send, _1);
LOGP("Request header announcements from [" << authority() << "].");
sent_ = true;
}
Expand Down

0 comments on commit 135cc79

Please sign in to comment.