Skip to content

Commit

Permalink
Merge pull request #576 from evoskuil/master
Browse files Browse the repository at this point in the history
Style, refactor to simplify protocol.
  • Loading branch information
evoskuil authored Mar 17, 2024
2 parents de18fbc + c32fbe1 commit d60ec6c
Show file tree
Hide file tree
Showing 12 changed files with 347 additions and 206 deletions.
2 changes: 2 additions & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ src_libbitcoin_node_la_SOURCES = \
src/protocols/protocol_header_out_31800.cpp \
src/protocols/protocol_header_out_70012.cpp \
src/protocols/protocol_observer.cpp \
src/protocols/protocol_performer.cpp \
src/protocols/protocol_transaction_in.cpp \
src/protocols/protocol_transaction_out.cpp \
src/sessions/session.cpp \
Expand Down Expand Up @@ -155,6 +156,7 @@ include_bitcoin_node_protocols_HEADERS = \
include/bitcoin/node/protocols/protocol_header_out_31800.hpp \
include/bitcoin/node/protocols/protocol_header_out_70012.hpp \
include/bitcoin/node/protocols/protocol_observer.hpp \
include/bitcoin/node/protocols/protocol_performer.hpp \
include/bitcoin/node/protocols/protocol_transaction_in.hpp \
include/bitcoin/node/protocols/protocol_transaction_out.hpp \
include/bitcoin/node/protocols/protocols.hpp
Expand Down
1 change: 1 addition & 0 deletions builds/cmake/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,7 @@ add_library( ${CANONICAL_LIB_NAME}
"../../src/protocols/protocol_header_out_31800.cpp"
"../../src/protocols/protocol_header_out_70012.cpp"
"../../src/protocols/protocol_observer.cpp"
"../../src/protocols/protocol_performer.cpp"
"../../src/protocols/protocol_transaction_in.cpp"
"../../src/protocols/protocol_transaction_out.cpp"
"../../src/sessions/session.cpp"
Expand Down
2 changes: 2 additions & 0 deletions builds/msvc/vs2022/libbitcoin-node/libbitcoin-node.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@
<ClCompile Include="..\..\..\..\src\protocols\protocol_header_out_31800.cpp" />
<ClCompile Include="..\..\..\..\src\protocols\protocol_header_out_70012.cpp" />
<ClCompile Include="..\..\..\..\src\protocols\protocol_observer.cpp" />
<ClCompile Include="..\..\..\..\src\protocols\protocol_performer.cpp" />
<ClCompile Include="..\..\..\..\src\protocols\protocol_transaction_in.cpp" />
<ClCompile Include="..\..\..\..\src\protocols\protocol_transaction_out.cpp" />
<ClCompile Include="..\..\..\..\src\sessions\session.cpp" />
Expand Down Expand Up @@ -128,6 +129,7 @@
<ClInclude Include="..\..\..\..\include\bitcoin\node\protocols\protocol_header_out_31800.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\node\protocols\protocol_header_out_70012.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\node\protocols\protocol_observer.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\node\protocols\protocol_performer.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\node\protocols\protocol_transaction_in.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\node\protocols\protocol_transaction_out.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\node\protocols\protocols.hpp" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@
<ClCompile Include="..\..\..\..\src\protocols\protocol_observer.cpp">
<Filter>src\protocols</Filter>
</ClCompile>
<ClCompile Include="..\..\..\..\src\protocols\protocol_performer.cpp">
<Filter>src\protocols</Filter>
</ClCompile>
<ClCompile Include="..\..\..\..\src\protocols\protocol_transaction_in.cpp">
<Filter>src\protocols</Filter>
</ClCompile>
Expand Down Expand Up @@ -209,6 +212,9 @@
<ClInclude Include="..\..\..\..\include\bitcoin\node\protocols\protocol_observer.hpp">
<Filter>include\bitcoin\node\protocols</Filter>
</ClInclude>
<ClInclude Include="..\..\..\..\include\bitcoin\node\protocols\protocol_performer.hpp">
<Filter>include\bitcoin\node\protocols</Filter>
</ClInclude>
<ClInclude Include="..\..\..\..\include\bitcoin\node\protocols\protocol_transaction_in.hpp">
<Filter>include\bitcoin\node\protocols</Filter>
</ClInclude>
Expand Down
1 change: 1 addition & 0 deletions include/bitcoin/node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
#include <bitcoin/node/protocols/protocol_header_out_31800.hpp>
#include <bitcoin/node/protocols/protocol_header_out_70012.hpp>
#include <bitcoin/node/protocols/protocol_observer.hpp>
#include <bitcoin/node/protocols/protocol_performer.hpp>
#include <bitcoin/node/protocols/protocol_transaction_in.hpp>
#include <bitcoin/node/protocols/protocol_transaction_out.hpp>
#include <bitcoin/node/protocols/protocols.hpp>
Expand Down
50 changes: 18 additions & 32 deletions include/bitcoin/node/protocols/protocol_block_in_31800.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,34 +23,25 @@
#include <bitcoin/network.hpp>
#include <bitcoin/node/chasers/chasers.hpp>
#include <bitcoin/node/define.hpp>
#include <bitcoin/node/protocols/protocol.hpp>
#include <bitcoin/node/protocols/protocol_performer.hpp>

namespace libbitcoin {
namespace node {

/// This does NOT inhereit from protocol_block_in.
/// This does NOT inherit from protocol_block_in.
class BCN_API protocol_block_in_31800
: public node::protocol,
protected network::tracker<protocol_block_in_31800>
: public protocol_performer
{
public:
typedef std::shared_ptr<protocol_block_in_31800> ptr;
using type_id = network::messages::inventory::type_id;
using map_ptr = chaser_check::map_ptr;

BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)
template <typename Session>
protocol_block_in_31800(Session& session,
const channel_ptr& channel, bool performance) NOEXCEPT
: node::protocol(session, channel),
network::tracker<protocol_block_in_31800>(session.log),
drop_stalled_(performance &&
to_bool(session.config().node.sample_period_seconds)),
use_deviation_(session.config().node.allowed_deviation > 0.0),
: protocol_performer(session, channel, performance),
block_type_(session.config().network.witness_node() ?
type_id::witness_block : type_id::block),
performance_timer_(std::make_shared<network::deadline>(session.log,
channel->strand(), session.config().node.sample_period())),
map_(std::make_shared<database::associations>())
{
}
Expand All @@ -61,27 +52,29 @@ class BCN_API protocol_block_in_31800
void stopping(const code& ec) NOEXCEPT override;

protected:
/// Performance polling.
virtual void start_performance() NOEXCEPT;
virtual void pause_performance() NOEXCEPT;
virtual void stop_performance() NOEXCEPT;
/// Determine if block passes check validation.
virtual code validate(const system::chain::block& block,
const system::chain::context& ctx) const NOEXCEPT;

/// Get published download identifiers.
virtual void handle_event(const code& ec,
chaser::chase event_, chaser::link value) NOEXCEPT;
virtual void do_get_downloads(chaser::count_t count) NOEXCEPT;

/// Manage work splitting.
virtual bool is_idle() const NOEXCEPT;
virtual void do_split(chaser::channel_t channel) NOEXCEPT;
void do_pause(chaser::channel_t channel) NOEXCEPT;
void do_resume(chaser::channel_t channel) NOEXCEPT;
virtual void do_pause(chaser::channel_t channel) NOEXCEPT;
virtual void do_resume(chaser::channel_t channel) NOEXCEPT;

/// Accept incoming block message.
/// Check incoming block message.
virtual bool handle_receive_block(const code& ec,
const network::messages::block::cptr& message) NOEXCEPT;

private:
void handle_performance_timer(const code& ec) NOEXCEPT;
void handle_send_performance(const code& ec) NOEXCEPT;
void do_handle_performance(const code& ec) NOEXCEPT;
using map_ptr = chaser_check::map_ptr;
using type_id = network::messages::inventory::type_id;
static constexpr size_t minimum_for_stall_divide = 2;

void send_get_data(const map_ptr& map) NOEXCEPT;
network::messages::get_data create_get_data(
Expand All @@ -92,17 +85,10 @@ class BCN_API protocol_block_in_31800
void handle_put_hashes(const code& ec) NOEXCEPT;
void handle_get_hashes(const code& ec, const map_ptr& map) NOEXCEPT;

void send_performance(uint64_t rate) NOEXCEPT;

// These are thread safe.
const bool drop_stalled_;
const bool use_deviation_;
// This is thread safe.
const network::messages::inventory::type_id block_type_;

// These are protected by strand.
uint64_t bytes_{ zero };
network::steady_clock::time_point start_{};
network::deadline::ptr performance_timer_;
// This is protected by strand.
map_ptr map_;
};

Expand Down
76 changes: 76 additions & 0 deletions include/bitcoin/node/protocols/protocol_performer.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/**
* Copyright (c) 2011-2023 libbitcoin developers (see AUTHORS)
*
* This file is part of libbitcoin.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef LIBBITCOIN_NODE_PROTOCOL_PERFORMER_HPP
#define LIBBITCOIN_NODE_PROTOCOL_PERFORMER_HPP

#include <bitcoin/network.hpp>
#include <bitcoin/node/define.hpp>
#include <bitcoin/node/protocols/protocol.hpp>

namespace libbitcoin {
namespace node {

/// Abstract base protocol for performance standard deviation measurement.
class BCN_API protocol_performer
: public node::protocol,
protected network::tracker<protocol_performer>
{
public:
virtual void start_performance() NOEXCEPT;
virtual void pause_performance() NOEXCEPT;
virtual void stop_performance() NOEXCEPT;
virtual void count(size_t bytes) NOEXCEPT;

protected:
template <typename Session>
protocol_performer(Session& session, const channel_ptr& channel,
bool enable) NOEXCEPT
: node::protocol(session, channel),
network::tracker<protocol_performer>(session.log),
use_deviation_(session.config().node.allowed_deviation > 0.0),
drop_stall_(enable &&
to_bool(session.config().node.sample_period_seconds)),
performance_timer_(std::make_shared<network::deadline>(session.log,
channel->strand(), session.config().node.sample_period()))
{
}

virtual bool is_idle() const NOEXCEPT = 0;

private:
void handle_performance_timer(const code& ec) NOEXCEPT;
void handle_send_performance(const code& ec) NOEXCEPT;
void do_handle_performance(const code& ec) NOEXCEPT;

void send_performance(uint64_t rate) NOEXCEPT;

// These are thread safe.
const bool use_deviation_;
const bool drop_stall_;

// These are protected by strand.
uint64_t bytes_{ zero };
network::steady_clock::time_point start_{};
network::deadline::ptr performance_timer_;
};

} // namespace node
} // namespace libbitcoin

#endif
1 change: 1 addition & 0 deletions include/bitcoin/node/protocols/protocols.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include <bitcoin/node/protocols/protocol_header_out_31800.hpp>
#include <bitcoin/node/protocols/protocol_header_out_70012.hpp>
#include <bitcoin/node/protocols/protocol_observer.hpp>
#include <bitcoin/node/protocols/protocol_performer.hpp>
#include <bitcoin/node/protocols/protocol_transaction_in.hpp>
#include <bitcoin/node/protocols/protocol_transaction_out.hpp>

Expand Down
6 changes: 3 additions & 3 deletions include/bitcoin/node/sessions/session_outbound.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,18 @@ class BCN_API session_outbound
session_outbound(full_node& node, uint64_t identifier) NOEXCEPT;

void start(network::result_handler&& handler) NOEXCEPT override;

virtual void performance(uint64_t channel, uint64_t speed,
network::result_handler&& handler) NOEXCEPT;

protected:
virtual void handle_event(const code& ec,
chaser::chase event_, chaser::link value) NOEXCEPT;
virtual void split(chaser::channel_t channel) NOEXCEPT;
virtual void do_performance(uint64_t channel, uint64_t speed,
const network::result_handler& handler) NOEXCEPT;

private:
void do_performance(uint64_t channel, uint64_t speed,
const network::result_handler& handler) NOEXCEPT;
static constexpr size_t minimum_for_standard_deviation = 3;

// This is thread safe.
const float allowed_deviation_;
Expand Down
Loading

0 comments on commit d60ec6c

Please sign in to comment.