Skip to content
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
4 changes: 3 additions & 1 deletion Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ src_libbitcoin_node_la_SOURCES = \
src/sessions/session.cpp \
src/sessions/session_inbound.cpp \
src/sessions/session_manual.cpp \
src/sessions/session_outbound.cpp
src/sessions/session_outbound.cpp \
src/sessions/session_tcp.cpp

# local: test/libbitcoin-node-test
#------------------------------------------------------------------------------
Expand Down Expand Up @@ -200,6 +201,7 @@ include_bitcoin_node_sessions_HEADERS = \
include/bitcoin/node/sessions/session_inbound.hpp \
include/bitcoin/node/sessions/session_manual.hpp \
include/bitcoin/node/sessions/session_outbound.hpp \
include/bitcoin/node/sessions/session_tcp.hpp \
include/bitcoin/node/sessions/sessions.hpp

# files => ${bash_completiondir}
Expand Down
3 changes: 2 additions & 1 deletion builds/cmake/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,8 @@ add_library( ${CANONICAL_LIB_NAME}
"../../src/sessions/session.cpp"
"../../src/sessions/session_inbound.cpp"
"../../src/sessions/session_manual.cpp"
"../../src/sessions/session_outbound.cpp" )
"../../src/sessions/session_outbound.cpp"
"../../src/sessions/session_tcp.cpp" )

# ${CANONICAL_LIB_NAME} project specific include directory normalization for build.
#------------------------------------------------------------------------------
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 @@ -157,6 +157,7 @@
<ClCompile Include="..\..\..\..\src\sessions\session_inbound.cpp" />
<ClCompile Include="..\..\..\..\src\sessions\session_manual.cpp" />
<ClCompile Include="..\..\..\..\src\sessions\session_outbound.cpp" />
<ClCompile Include="..\..\..\..\src\sessions\session_tcp.cpp" />
<ClCompile Include="..\..\..\..\src\settings.cpp" />
</ItemGroup>
<ItemGroup>
Expand Down Expand Up @@ -204,6 +205,7 @@
<ClInclude Include="..\..\..\..\include\bitcoin\node\sessions\session_inbound.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\node\sessions\session_manual.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\node\sessions\session_outbound.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\node\sessions\session_tcp.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\node\sessions\sessions.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\node\settings.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\node\version.hpp" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,9 @@
<ClCompile Include="..\..\..\..\src\sessions\session_outbound.cpp">
<Filter>src\sessions</Filter>
</ClCompile>
<ClCompile Include="..\..\..\..\src\sessions\session_tcp.cpp">
<Filter>src\sessions</Filter>
</ClCompile>
<ClCompile Include="..\..\..\..\src\settings.cpp">
<Filter>src</Filter>
</ClCompile>
Expand Down Expand Up @@ -293,6 +296,9 @@
<ClInclude Include="..\..\..\..\include\bitcoin\node\sessions\session_outbound.hpp">
<Filter>include\bitcoin\node\sessions</Filter>
</ClInclude>
<ClInclude Include="..\..\..\..\include\bitcoin\node\sessions\session_tcp.hpp">
<Filter>include\bitcoin\node\sessions</Filter>
</ClInclude>
<ClInclude Include="..\..\..\..\include\bitcoin\node\sessions\sessions.hpp">
<Filter>include\bitcoin\node\sessions</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 @@ -61,6 +61,7 @@
#include <bitcoin/node/sessions/session_inbound.hpp>
#include <bitcoin/node/sessions/session_manual.hpp>
#include <bitcoin/node/sessions/session_outbound.hpp>
#include <bitcoin/node/sessions/session_tcp.hpp>
#include <bitcoin/node/sessions/sessions.hpp>

#endif
19 changes: 11 additions & 8 deletions include/bitcoin/node/full_node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@
#include <bitcoin/node/chasers/chasers.hpp>
#include <bitcoin/node/configuration.hpp>
#include <bitcoin/node/define.hpp>
////#include <bitcoin/node/protocols/protocols.hpp>
////#include <bitcoin/node/sessions/sessions.hpp>
#include <bitcoin/node/sessions/sessions.hpp>

namespace libbitcoin {
namespace node {
Expand All @@ -45,6 +44,7 @@ class BCN_API full_node
using store = node::store;
using query = node::query;
using memory_controller = block_memory;
using result_handler = network::result_handler;
typedef std::shared_ptr<full_node> ptr;

/// Constructors.
Expand All @@ -59,10 +59,10 @@ class BCN_API full_node
/// -----------------------------------------------------------------------

/// Start the node (seed and manual services).
void start(network::result_handler&& handler) NOEXCEPT override;
void start(result_handler&& handler) NOEXCEPT override;

/// Run the node (inbound/outbound services and blockchain chasers).
void run(network::result_handler&& handler) NOEXCEPT override;
void run(result_handler&& handler) NOEXCEPT override;

/// Close the node.
void close() NOEXCEPT override;
Expand All @@ -81,7 +81,7 @@ class BCN_API full_node
/// Manage download queue.
virtual void get_hashes(map_handler&& handler) NOEXCEPT;
virtual void put_hashes(const map_ptr& map,
network::result_handler&& handler) NOEXCEPT;
result_handler&& handler) NOEXCEPT;

/// Events.
/// -----------------------------------------------------------------------
Expand Down Expand Up @@ -149,7 +149,7 @@ class BCN_API full_node

/// Handle performance, base returns false (implied terminate).
virtual void performance(object_key channel, uint64_t speed,
network::result_handler&& handler) NOEXCEPT;
result_handler&& handler) NOEXCEPT;

/// Get the memory resource.
virtual network::memory& get_memory() NOEXCEPT;
Expand All @@ -160,14 +160,17 @@ class BCN_API full_node
network::session_manual::ptr attach_manual_session() NOEXCEPT override;
network::session_inbound::ptr attach_inbound_session() NOEXCEPT override;
network::session_outbound::ptr attach_outbound_session() NOEXCEPT override;
virtual session_explore::ptr attach_explore_session() NOEXCEPT;

/// Virtual handlers.
/// -----------------------------------------------------------------------
void do_start(const network::result_handler& handler) NOEXCEPT override;
void do_run(const network::result_handler& handler) NOEXCEPT override;
void do_start(const result_handler& handler) NOEXCEPT override;
void do_run(const result_handler& handler) NOEXCEPT override;
void do_close() NOEXCEPT override;

private:
void start_explore(const code& ec, const result_handler& handler) NOEXCEPT;

void do_subscribe_events(const event_notifier& handler,
const event_completer& complete) NOEXCEPT;
void do_notify(const code& ec, chase event_, event_value value) NOEXCEPT;
Expand Down
5 changes: 5 additions & 0 deletions include/bitcoin/node/protocols/protocol.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@
namespace libbitcoin {
namespace node {

///////////////////////////////////////////////////////////////////////////////
// TODO: ... -> node::protocol_tcp -> node::protocol -> network::protocol. //
// TODO: ... -> node::protocol_peer -> node::protocol -> network::protocol. //
///////////////////////////////////////////////////////////////////////////////

/// Abstract base for node protocols, thread safe.
class BCN_API protocol
: public network::protocol_peer
Expand Down
30 changes: 17 additions & 13 deletions include/bitcoin/node/sessions/attach.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#define LIBBITCOIN_NODE_SESSIONS_ATTACH_HPP

#include <memory>
#include <utility>
#include <bitcoin/network.hpp>
#include <bitcoin/node/define.hpp>
#include <bitcoin/node/protocols/protocols.hpp>
Expand All @@ -30,7 +31,7 @@ namespace node {

class full_node;

/// Session base class template for protocol attachment.
/// Session base class template for network session attachment.
/// node::session does not derive from network::session (siblings).
/// This avoids the diamond inheritance problem between network/node.
/// Protocol contructors are templatized on Session, obtaining session.
Expand All @@ -39,8 +40,10 @@ class attach
: public Session, public node::session
{
public:
attach(full_node& node, uint64_t identifier) NOEXCEPT
: Session(node, identifier), session(node)
template <typename... Args>
attach(full_node& node, uint64_t identifier, Args&&... args) NOEXCEPT
: Session(node, identifier, std::forward<Args>(args)...),
node::session(node)
{
}

Expand All @@ -56,21 +59,22 @@ class attach
Session::attach_handshake(channel, std::move(handler));
}

void attach_protocols(const network::channel::ptr& channel) NOEXCEPT override
void attach_protocols(
const network::channel::ptr& channel) NOEXCEPT override
{
using namespace system;
using namespace network::messages::peer;
const auto relay = config().network.enable_relay;
const auto delay = config().node.delay_inbound;
const auto headers = config().node.headers_first;
const auto relay = this->config().network.enable_relay;
const auto delay = this->config().node.delay_inbound;
const auto headers = this->config().node.headers_first;
const auto node_network = to_bool(bit_and<uint64_t>
(
config().network.services_maximum,
this->config().network.services_maximum,
network::messages::peer::service::node_network
));
const auto node_client_filters = to_bool(bit_and<uint64_t>
(
config().network.services_maximum,
this->config().network.services_maximum,
network::messages::peer::service::node_client_filters
));

Expand Down Expand Up @@ -158,12 +162,12 @@ class attach
}
}

network::channel::ptr create_channel(const network::socket::ptr& socket) NOEXCEPT override
network::channel::ptr create_channel(
const network::socket::ptr& socket) NOEXCEPT override
{
return std::static_pointer_cast<network::channel>(
std::make_shared<node::channel_peer>(node::session::get_memory(),
network::session::log, socket, node::session::config(),
network::session::create_key()));
std::make_shared<node::channel_peer>(this->get_memory(), this->log,
socket, this->config(), this->create_key()));
}
};

Expand Down
57 changes: 57 additions & 0 deletions include/bitcoin/node/sessions/session_tcp.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/**
* Copyright (c) 2011-2025 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_SESSIONS_SESSION_TCP_HPP
#define LIBBITCOIN_NODE_SESSIONS_SESSION_TCP_HPP

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

namespace libbitcoin {
namespace node {

class full_node;

class session_tcp
: public network::session_tcp, public node::session
{
public:
typedef std::shared_ptr<session_tcp> ptr;
using options_t = network::session_tcp::options_t;

session_tcp(full_node& node, uint64_t identifier,
const options_t& options) NOEXCEPT;

protected:
inline bool enabled() const NOEXCEPT override;
};

// TODO: move all sessions up from network.
////using session_web = network::session_server<protocol_web, session_tcp>;
using session_explore = network::session_server<protocol_explore, session_tcp>;
////using session_websocket = network::session_server<protocol_websocket, session_tcp>;
////using session_bitcoind = network::session_server<protocol_bitcoind, session_tcp>;
////using session_electrum = network::session_server<protocol_electrum, session_tcp>;
////using session_stratum_v1 = network::session_server<protocol_stratum_v1, session_tcp>;
////using session_stratum_v2 = network::session_server<protocol_stratum_v2, session_tcp>;

} // namespace node
} // namespace libbitcoin

#endif
1 change: 1 addition & 0 deletions include/bitcoin/node/sessions/sessions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,6 @@
#include <bitcoin/node/sessions/session_inbound.hpp>
#include <bitcoin/node/sessions/session_manual.hpp>
#include <bitcoin/node/sessions/session_outbound.hpp>
#include <bitcoin/node/sessions/session_tcp.hpp>

#endif
32 changes: 29 additions & 3 deletions src/full_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@
#include <bitcoin/network.hpp>
#include <bitcoin/node/chasers/chasers.hpp>
#include <bitcoin/node/define.hpp>
////#include <bitcoin/node/protocols/protocols.hpp>
////#include <bitcoin/node/sessions/sessions.hpp>
#include <bitcoin/node/sessions/sessions.hpp>

namespace libbitcoin {
namespace node {

using namespace system;
using namespace database;
using namespace network;
using namespace std::placeholders;

BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)

Expand Down Expand Up @@ -117,7 +117,28 @@ void full_node::do_run(const result_handler& handler) NOEXCEPT
// This will kick off lagging validations even if not current.
do_notify(error::success, chase::start, height_t{});

net::do_run(handler);
// Start services after network is running.
net::do_run(std::bind(&full_node::start_explore, this, _1, handler));
}

void full_node::start_explore(const code& ec,
const result_handler& handler) NOEXCEPT
{
BC_ASSERT_MSG(stranded(), "strand");

if (ec)
{
handler(ec);
return;
}

if (!config().network.explore.enabled())
{
handler(ec);
return;
}

attach_explore_session()->start(move_copy(handler));
}

void full_node::close() NOEXCEPT
Expand Down Expand Up @@ -433,6 +454,11 @@ network::session_outbound::ptr full_node::attach_outbound_session() NOEXCEPT
return attach<node::session_outbound>(*this);
}

session_explore::ptr full_node::attach_explore_session() NOEXCEPT
{
return net::attach<session_explore>(*this, config_.network.explore);
}

BC_POP_WARNING()

} // namespace node
Expand Down
41 changes: 41 additions & 0 deletions src/sessions/session_tcp.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* Copyright (c) 2011-2025 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/>.
*/
#include <bitcoin/node/sessions/session_tcp.hpp>

#include <bitcoin/node/full_node.hpp>

namespace libbitcoin {
namespace node {

session_tcp::session_tcp(full_node& node, uint64_t identifier,
const options_t& options) NOEXCEPT
: network::session_tcp(node, identifier, options),
node::session(node)
{
}

// Inbound connection attempts are dropped unless confirmed chain is current.
// Used instead of suspension because suspension has independent start/stop.
bool session_tcp::enabled() const NOEXCEPT
{
return !config().node.delay_inbound || is_recent();
}

} // namespace node
} // namespace libbitcoin
Loading