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

Move memory class to own files and inject from full_node. #663

Merged
merged 2 commits into from
Jul 31, 2024
Merged
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
2 changes: 2 additions & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
@@ -38,6 +38,7 @@ src_libbitcoin_node_la_SOURCES = \
src/configuration.cpp \
src/error.cpp \
src/full_node.cpp \
src/memory.cpp \
src/parser.cpp \
src/settings.cpp \
src/chasers/chaser.cpp \
@@ -130,6 +131,7 @@ include_bitcoin_node_HEADERS = \
include/bitcoin/node/error.hpp \
include/bitcoin/node/events.hpp \
include/bitcoin/node/full_node.hpp \
include/bitcoin/node/memory.hpp \
include/bitcoin/node/parser.hpp \
include/bitcoin/node/settings.hpp \
include/bitcoin/node/version.hpp
1 change: 1 addition & 0 deletions builds/cmake/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -248,6 +248,7 @@ add_library( ${CANONICAL_LIB_NAME}
"../../src/configuration.cpp"
"../../src/error.cpp"
"../../src/full_node.cpp"
"../../src/memory.cpp"
"../../src/parser.cpp"
"../../src/settings.cpp"
"../../src/chasers/chaser.cpp"
2 changes: 2 additions & 0 deletions builds/msvc/vs2022/libbitcoin-node/libbitcoin-node.vcxproj
Original file line number Diff line number Diff line change
@@ -87,6 +87,7 @@
<ClCompile Include="..\..\..\..\src\configuration.cpp" />
<ClCompile Include="..\..\..\..\src\error.cpp" />
<ClCompile Include="..\..\..\..\src\full_node.cpp" />
<ClCompile Include="..\..\..\..\src\memory.cpp" />
<ClCompile Include="..\..\..\..\src\parser.cpp" />
<ClCompile Include="..\..\..\..\src\protocols\protocol.cpp" />
<ClCompile Include="..\..\..\..\src\protocols\protocol_block_in.cpp" />
@@ -127,6 +128,7 @@
<ClInclude Include="..\..\..\..\include\bitcoin\node\error.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\node\events.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\node\full_node.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\node\memory.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\node\parser.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\node\protocols\protocol.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\node\protocols\protocol_block_in.hpp" />
Original file line number Diff line number Diff line change
@@ -90,6 +90,9 @@
<ClCompile Include="..\..\..\..\src\full_node.cpp">
<Filter>src</Filter>
</ClCompile>
<ClCompile Include="..\..\..\..\src\memory.cpp">
<Filter>src</Filter>
</ClCompile>
<ClCompile Include="..\..\..\..\src\parser.cpp">
<Filter>src</Filter>
</ClCompile>
@@ -206,6 +209,9 @@
<ClInclude Include="..\..\..\..\include\bitcoin\node\full_node.hpp">
<Filter>include\bitcoin\node</Filter>
</ClInclude>
<ClInclude Include="..\..\..\..\include\bitcoin\node\memory.hpp">
<Filter>include\bitcoin\node</Filter>
</ClInclude>
<ClInclude Include="..\..\..\..\include\bitcoin\node\parser.hpp">
<Filter>include\bitcoin\node</Filter>
</ClInclude>
1 change: 1 addition & 0 deletions include/bitcoin/node.hpp
Original file line number Diff line number Diff line change
@@ -22,6 +22,7 @@
#include <bitcoin/node/error.hpp>
#include <bitcoin/node/events.hpp>
#include <bitcoin/node/full_node.hpp>
#include <bitcoin/node/memory.hpp>
#include <bitcoin/node/parser.hpp>
#include <bitcoin/node/settings.hpp>
#include <bitcoin/node/version.hpp>
8 changes: 6 additions & 2 deletions include/bitcoin/node/full_node.hpp
Original file line number Diff line number Diff line change
@@ -23,6 +23,8 @@
#include <bitcoin/network.hpp>
#include <bitcoin/node/chasers/chasers.hpp>
#include <bitcoin/node/configuration.hpp>
#include <bitcoin/node/define.hpp>
#include <bitcoin/node/memory.hpp>

namespace libbitcoin {
namespace node {
@@ -139,6 +141,9 @@ class BCN_API full_node
/// The specified timestamp is current.
virtual bool is_current(uint32_t timestamp) const NOEXCEPT;

/// Get the memory resource.
virtual network::memory& get_memory() NOEXCEPT;

protected:
/// Session attachments.
/// -----------------------------------------------------------------------
@@ -153,7 +158,6 @@ class BCN_API full_node
void do_close() NOEXCEPT override;

private:
object_key create_key() 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;
@@ -162,6 +166,7 @@ class BCN_API full_node

// These are thread safe.
const configuration& config_;
memory memory_;
query& query_;

// These are protected by strand.
@@ -176,7 +181,6 @@ class BCN_API full_node
chaser_snapshot chaser_snapshot_;
chaser_storage chaser_storage_;
event_subscriber event_subscriber_;
object_key keys_{};
};

} // namespace node
38 changes: 38 additions & 0 deletions include/bitcoin/node/memory.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* Copyright (c) 2011-2024 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_MEMORY_HPP
#define LIBBITCOIN_NODE_MEMORY_HPP

#include <bitcoin/network.hpp>

namespace libbitcoin {
namespace node {

class memory
: public network::memory
{
public:
using base = network::memory;
using base::base;
};

} // namespace node
} // namespace libbitcoin

#endif
10 changes: 3 additions & 7 deletions include/bitcoin/node/sessions/attach.hpp
Original file line number Diff line number Diff line change
@@ -106,13 +106,9 @@ class attach
network::channel::ptr create_channel(const network::socket::ptr& socket,
bool quiet) NOEXCEPT override
{
// TODO: replace message memory resource (affects only block messages).
static network::memory memory{};

// Channel id must be created using create_key().
const auto id = network::session::create_key();
return std::make_shared<network::channel>(memory, network::session::log,
socket, network::session::settings(), id, quiet);
return std::make_shared<network::channel>(session::get_memory(),
network::session::log, socket, network::session::settings(),
network::session::create_key(), quiet);
}
};

3 changes: 3 additions & 0 deletions include/bitcoin/node/sessions/session.hpp
Original file line number Diff line number Diff line change
@@ -93,6 +93,9 @@ class BCN_API session
virtual void performance(object_key channel, uint64_t speed,
network::result_handler&& handler) NOEXCEPT;

/// Get the memory resource.
virtual network::memory& get_memory() const NOEXCEPT;

/// Suspensions.
/// -----------------------------------------------------------------------

23 changes: 7 additions & 16 deletions src/full_node.cpp
Original file line number Diff line number Diff line change
@@ -24,6 +24,7 @@
#include <bitcoin/network.hpp>
#include <bitcoin/node/chasers/chasers.hpp>
#include <bitcoin/node/define.hpp>
#include <bitcoin/node/memory.hpp>
#include <bitcoin/node/sessions/sessions.hpp>

namespace libbitcoin {
@@ -40,6 +41,7 @@ full_node::full_node(query& query, const configuration& configuration,
const logger& log) NOEXCEPT
: p2p(configuration.network, log),
config_(configuration),
memory_(),
query_(query),
chaser_block_(*this),
chaser_header_(*this),
@@ -241,22 +243,6 @@ void full_node::unsubscribe_events(object_key key) NOEXCEPT
notify_one(key, network::error::service_stopped, chase::stop, {});
}

// private
// At one object/session/ns, this overflows in ~585 years (and handled).
// Could just use channel.identifier() if we didn't have subscribed chasers.
object_key full_node::create_key() NOEXCEPT
{
BC_ASSERT(stranded());

if (is_zero(++keys_))
{
BC_ASSERT_MSG(false, "overflow");
LOGF("Session object overflow.");
}

return keys_;
}

// Blocks.
// ----------------------------------------------------------------------------

@@ -386,6 +372,11 @@ bool full_node::is_current(uint32_t timestamp) const NOEXCEPT
return time >= current;
}

network::memory& full_node::get_memory() NOEXCEPT
{
return memory_;
}

// Session attachments.
// ----------------------------------------------------------------------------

27 changes: 27 additions & 0 deletions src/memory.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* 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/>.
*/
#include <bitcoin/node/memory.hpp>

#include <bitcoin/network.hpp>

namespace libbitcoin {
namespace node {

} // namespace node
} // namespace libbitcoin
5 changes: 5 additions & 0 deletions src/sessions/session.cpp
Original file line number Diff line number Diff line change
@@ -131,6 +131,11 @@ void session::performance(object_key, uint64_t,
std::bind(handler, system::error::not_implemented));
}

network::memory& session::get_memory() const NOEXCEPT
{
return node_.get_memory();
}

// Suspensions.
// ----------------------------------------------------------------------------