From fc4c4144d8c1c2b0f26d6b851c3cb3a693e0a7a6 Mon Sep 17 00:00:00 2001 From: evoskuil Date: Wed, 31 Jul 2024 08:30:16 -0400 Subject: [PATCH 1/2] Move memory class to own files and inject from full_node. --- Makefile.am | 2 + builds/cmake/CMakeLists.txt | 1 + .../libbitcoin-node/libbitcoin-node.vcxproj | 2 + .../libbitcoin-node.vcxproj.filters | 6 +++ include/bitcoin/node.hpp | 1 + include/bitcoin/node/full_node.hpp | 6 +++ include/bitcoin/node/memory.hpp | 38 +++++++++++++++++++ include/bitcoin/node/sessions/attach.hpp | 10 ++--- include/bitcoin/node/sessions/session.hpp | 3 ++ src/full_node.cpp | 7 ++++ src/memory.cpp | 27 +++++++++++++ src/sessions/session.cpp | 5 +++ 12 files changed, 101 insertions(+), 7 deletions(-) create mode 100644 include/bitcoin/node/memory.hpp create mode 100644 src/memory.cpp diff --git a/Makefile.am b/Makefile.am index 81386084..6467520a 100644 --- a/Makefile.am +++ b/Makefile.am @@ -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 diff --git a/builds/cmake/CMakeLists.txt b/builds/cmake/CMakeLists.txt index bcbcb815..c20b15ba 100644 --- a/builds/cmake/CMakeLists.txt +++ b/builds/cmake/CMakeLists.txt @@ -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" diff --git a/builds/msvc/vs2022/libbitcoin-node/libbitcoin-node.vcxproj b/builds/msvc/vs2022/libbitcoin-node/libbitcoin-node.vcxproj index d1974246..e4c92398 100644 --- a/builds/msvc/vs2022/libbitcoin-node/libbitcoin-node.vcxproj +++ b/builds/msvc/vs2022/libbitcoin-node/libbitcoin-node.vcxproj @@ -87,6 +87,7 @@ + @@ -127,6 +128,7 @@ + diff --git a/builds/msvc/vs2022/libbitcoin-node/libbitcoin-node.vcxproj.filters b/builds/msvc/vs2022/libbitcoin-node/libbitcoin-node.vcxproj.filters index 549c30b1..10774dbd 100644 --- a/builds/msvc/vs2022/libbitcoin-node/libbitcoin-node.vcxproj.filters +++ b/builds/msvc/vs2022/libbitcoin-node/libbitcoin-node.vcxproj.filters @@ -90,6 +90,9 @@ src + + src + src @@ -206,6 +209,9 @@ include\bitcoin\node + + include\bitcoin\node + include\bitcoin\node diff --git a/include/bitcoin/node.hpp b/include/bitcoin/node.hpp index 09b6025f..14acfe77 100644 --- a/include/bitcoin/node.hpp +++ b/include/bitcoin/node.hpp @@ -22,6 +22,7 @@ #include #include #include +#include #include #include #include diff --git a/include/bitcoin/node/full_node.hpp b/include/bitcoin/node/full_node.hpp index dc66fcc3..2bf86b82 100644 --- a/include/bitcoin/node/full_node.hpp +++ b/include/bitcoin/node/full_node.hpp @@ -23,6 +23,8 @@ #include #include #include +#include +#include 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. /// ----------------------------------------------------------------------- @@ -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. diff --git a/include/bitcoin/node/memory.hpp b/include/bitcoin/node/memory.hpp new file mode 100644 index 00000000..6734efad --- /dev/null +++ b/include/bitcoin/node/memory.hpp @@ -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 . + */ +#ifndef LIBBITCOIN_NODE_MEMORY_HPP +#define LIBBITCOIN_NODE_MEMORY_HPP + +#include + +namespace libbitcoin { +namespace node { + +class memory + : public network::memory +{ +public: + using base = network::memory; + using base::base; +}; + +} // namespace node +} // namespace libbitcoin + +#endif diff --git a/include/bitcoin/node/sessions/attach.hpp b/include/bitcoin/node/sessions/attach.hpp index c2761271..5e8177e2 100644 --- a/include/bitcoin/node/sessions/attach.hpp +++ b/include/bitcoin/node/sessions/attach.hpp @@ -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(memory, network::session::log, - socket, network::session::settings(), id, quiet); + return std::make_shared(session::get_memory(), + network::session::log, socket, network::session::settings(), + network::session::create_key(), quiet); } }; diff --git a/include/bitcoin/node/sessions/session.hpp b/include/bitcoin/node/sessions/session.hpp index b2fe19b7..ee22572d 100644 --- a/include/bitcoin/node/sessions/session.hpp +++ b/include/bitcoin/node/sessions/session.hpp @@ -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. /// ----------------------------------------------------------------------- diff --git a/src/full_node.cpp b/src/full_node.cpp index 4292adc9..a6bb9236 100644 --- a/src/full_node.cpp +++ b/src/full_node.cpp @@ -24,6 +24,7 @@ #include #include #include +#include #include 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), @@ -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. // ---------------------------------------------------------------------------- diff --git a/src/memory.cpp b/src/memory.cpp new file mode 100644 index 00000000..5911bc5e --- /dev/null +++ b/src/memory.cpp @@ -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 . + */ +#include + +#include + +namespace libbitcoin { +namespace node { + +} // namespace node +} // namespace libbitcoin diff --git a/src/sessions/session.cpp b/src/sessions/session.cpp index 6a2711fd..1fb619bf 100644 --- a/src/sessions/session.cpp +++ b/src/sessions/session.cpp @@ -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. // ---------------------------------------------------------------------------- From 34025e630ad3a769564f9a659b9a64b41312a765 Mon Sep 17 00:00:00 2001 From: evoskuil Date: Wed, 31 Jul 2024 08:53:25 -0400 Subject: [PATCH 2/2] Have full_node use reuse p2p::create_key(). --- include/bitcoin/node/full_node.hpp | 2 -- src/full_node.cpp | 16 ---------------- 2 files changed, 18 deletions(-) diff --git a/include/bitcoin/node/full_node.hpp b/include/bitcoin/node/full_node.hpp index 2bf86b82..566d9a10 100644 --- a/include/bitcoin/node/full_node.hpp +++ b/include/bitcoin/node/full_node.hpp @@ -158,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; @@ -182,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 diff --git a/src/full_node.cpp b/src/full_node.cpp index a6bb9236..e8504dde 100644 --- a/src/full_node.cpp +++ b/src/full_node.cpp @@ -243,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. // ----------------------------------------------------------------------------