Skip to content

Commit

Permalink
Move memory class to own files and inject from full_node.
Browse files Browse the repository at this point in the history
  • Loading branch information
evoskuil committed Jul 31, 2024
1 parent 76413e3 commit fc4c414
Show file tree
Hide file tree
Showing 12 changed files with 101 additions and 7 deletions.
2 changes: 2 additions & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions builds/cmake/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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"
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 @@ -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" />
Expand Down Expand Up @@ -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" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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>
Expand Down
1 change: 1 addition & 0 deletions include/bitcoin/node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
6 changes: 6 additions & 0 deletions include/bitcoin/node/full_node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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.
/// -----------------------------------------------------------------------
Expand All @@ -162,6 +167,7 @@ class BCN_API full_node

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

// These are protected by strand.
Expand Down
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
Expand Up @@ -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);
}
};

Expand Down
3 changes: 3 additions & 0 deletions include/bitcoin/node/sessions/session.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
/// -----------------------------------------------------------------------

Expand Down
7 changes: 7 additions & 0 deletions src/full_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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),
Expand Down Expand Up @@ -386,6 +388,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.
// ----------------------------------------------------------------------------

Expand Down
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
Expand Up @@ -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.
// ----------------------------------------------------------------------------

Expand Down

0 comments on commit fc4c414

Please sign in to comment.