diff --git a/Makefile.am b/Makefile.am index d2d9e102a..9e31bef5a 100644 --- a/Makefile.am +++ b/Makefile.am @@ -94,6 +94,7 @@ src_libbitcoin_network_la_SOURCES = \ src/net/deadline.cpp \ src/net/distributor.cpp \ src/net/hosts.cpp \ + src/net/memory.cpp \ src/net/proxy.cpp \ src/net/socket.cpp \ src/protocols/protocol.cpp \ @@ -192,6 +193,7 @@ test_libbitcoin_network_test_SOURCES = \ test/net/deadline.cpp \ test/net/distributor.cpp \ test/net/hosts.cpp \ + test/net/memory.cpp \ test/net/proxy.cpp \ test/net/socket.cpp \ test/protocols/protocol.cpp \ @@ -342,6 +344,7 @@ include_bitcoin_network_net_HEADERS = \ include/bitcoin/network/net/deadline.hpp \ include/bitcoin/network/net/distributor.hpp \ include/bitcoin/network/net/hosts.hpp \ + include/bitcoin/network/net/memory.hpp \ include/bitcoin/network/net/net.hpp \ include/bitcoin/network/net/proxy.hpp \ include/bitcoin/network/net/socket.hpp diff --git a/builds/cmake/CMakeLists.txt b/builds/cmake/CMakeLists.txt index feb9afdfd..e386d29eb 100644 --- a/builds/cmake/CMakeLists.txt +++ b/builds/cmake/CMakeLists.txt @@ -277,6 +277,7 @@ add_library( ${CANONICAL_LIB_NAME} "../../src/net/deadline.cpp" "../../src/net/distributor.cpp" "../../src/net/hosts.cpp" + "../../src/net/memory.cpp" "../../src/net/proxy.cpp" "../../src/net/socket.cpp" "../../src/protocols/protocol.cpp" @@ -400,6 +401,7 @@ if (with-tests) "../../test/net/deadline.cpp" "../../test/net/distributor.cpp" "../../test/net/hosts.cpp" + "../../test/net/memory.cpp" "../../test/net/proxy.cpp" "../../test/net/socket.cpp" "../../test/protocols/protocol.cpp" diff --git a/builds/msvc/vs2022/libbitcoin-network-test/libbitcoin-network-test.vcxproj b/builds/msvc/vs2022/libbitcoin-network-test/libbitcoin-network-test.vcxproj index 51fef795a..58c2537ff 100644 --- a/builds/msvc/vs2022/libbitcoin-network-test/libbitcoin-network-test.vcxproj +++ b/builds/msvc/vs2022/libbitcoin-network-test/libbitcoin-network-test.vcxproj @@ -139,6 +139,7 @@ + diff --git a/builds/msvc/vs2022/libbitcoin-network-test/libbitcoin-network-test.vcxproj.filters b/builds/msvc/vs2022/libbitcoin-network-test/libbitcoin-network-test.vcxproj.filters index d74dc5c0b..8b5e35809 100644 --- a/builds/msvc/vs2022/libbitcoin-network-test/libbitcoin-network-test.vcxproj.filters +++ b/builds/msvc/vs2022/libbitcoin-network-test/libbitcoin-network-test.vcxproj.filters @@ -231,6 +231,9 @@ src\net + + src\net + src\net diff --git a/builds/msvc/vs2022/libbitcoin-network/libbitcoin-network.vcxproj b/builds/msvc/vs2022/libbitcoin-network/libbitcoin-network.vcxproj index 50c2a37dc..b4a5abe05 100644 --- a/builds/msvc/vs2022/libbitcoin-network/libbitcoin-network.vcxproj +++ b/builds/msvc/vs2022/libbitcoin-network/libbitcoin-network.vcxproj @@ -134,6 +134,7 @@ + @@ -239,6 +240,7 @@ + diff --git a/builds/msvc/vs2022/libbitcoin-network/libbitcoin-network.vcxproj.filters b/builds/msvc/vs2022/libbitcoin-network/libbitcoin-network.vcxproj.filters index 817aaaa70..00c8c0900 100644 --- a/builds/msvc/vs2022/libbitcoin-network/libbitcoin-network.vcxproj.filters +++ b/builds/msvc/vs2022/libbitcoin-network/libbitcoin-network.vcxproj.filters @@ -252,6 +252,9 @@ src\net + + src\net + src\net @@ -563,6 +566,9 @@ include\bitcoin\network\net + + include\bitcoin\network\net + include\bitcoin\network\net diff --git a/include/bitcoin/network.hpp b/include/bitcoin/network.hpp index 81fcbe363..7ef8b2147 100644 --- a/include/bitcoin/network.hpp +++ b/include/bitcoin/network.hpp @@ -100,6 +100,7 @@ #include #include #include +#include #include #include #include diff --git a/include/bitcoin/network/net/memory.hpp b/include/bitcoin/network/net/memory.hpp new file mode 100644 index 000000000..11099dbec --- /dev/null +++ b/include/bitcoin/network/net/memory.hpp @@ -0,0 +1,50 @@ +/** + * 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_NETWORK_NET_MEMORY_HPP +#define LIBBITCOIN_NETWORK_NET_MEMORY_HPP + +#include +#include +#include +#include + +namespace libbitcoin { +namespace network { + +class BCT_API memory +{ +public: + DELETE_COPY_MOVE_DESTRUCT(memory); + + memory() NOEXCEPT; + memory(arena* arena) NOEXCEPT; + + arena* get_arena() NOEXCEPT; + retainer::ptr get_retainer() NOEXCEPT; + + +private: + arena* arena_; + std::shared_mutex remap_mutex_{}; +}; + +} // namespace network +} // namespace libbitcoin + +#endif diff --git a/include/bitcoin/network/net/net.hpp b/include/bitcoin/network/net/net.hpp index f2e8cc794..bb6333517 100644 --- a/include/bitcoin/network/net/net.hpp +++ b/include/bitcoin/network/net/net.hpp @@ -26,6 +26,7 @@ #include #include #include +#include #include #include diff --git a/include/bitcoin/network/sessions/session.hpp b/include/bitcoin/network/sessions/session.hpp index 364744f7c..74fecbc2b 100644 --- a/include/bitcoin/network/sessions/session.hpp +++ b/include/bitcoin/network/sessions/session.hpp @@ -190,6 +190,7 @@ class BCT_API session /// Subscribe to session stop notification, obtain unsubscribe key. virtual object_key subscribe_stop(notifier&& handler) NOEXCEPT; virtual bool notify(object_key key) NOEXCEPT; + virtual object_key create_key() NOEXCEPT; /// Remove self from network close subscription (for session early stop). virtual void unsubscribe_close() NOEXCEPT; @@ -232,8 +233,6 @@ class BCT_API session asio::strand& strand() NOEXCEPT; private: - object_key create_key() NOEXCEPT; - void handle_channel_start(const code& ec, const channel::ptr& channel, const result_handler& started, const result_handler& stopped) NOEXCEPT; diff --git a/src/messages/block.cpp b/src/messages/block.cpp index 963d1721b..e384fa912 100644 --- a/src/messages/block.cpp +++ b/src/messages/block.cpp @@ -19,11 +19,13 @@ #include #include +#include #include #include #include #include #include +#include namespace libbitcoin { namespace network { @@ -40,8 +42,11 @@ const uint32_t block::version_maximum = level::maximum_protocol; typename block::cptr block::deserialize(uint32_t version, const system::data_chunk& data, bool witness) NOEXCEPT { + static memory memory{}; system::istream source{ data }; - system::byte_reader reader{ source }; + system::byte_reader reader{ source, memory.get_arena() }; + + // message, block and block_ptr are not allocated by reader's arena. const auto message = to_shared(deserialize(version, reader, witness)); if (!reader) return nullptr; @@ -81,6 +86,7 @@ typename block::cptr block::deserialize(uint32_t version, std::advance(start, full); } + message->block_ptr->set_retainer(memory.get_retainer()); return message; } diff --git a/src/net/memory.cpp b/src/net/memory.cpp new file mode 100644 index 000000000..c942c9875 --- /dev/null +++ b/src/net/memory.cpp @@ -0,0 +1,52 @@ +/** + * 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 . + */ +#include + +#include +#include +#include + +namespace libbitcoin { +namespace network { + +memory::memory() NOEXCEPT + : memory(default_arena::get()) +{ +} + +memory::memory(arena* arena) NOEXCEPT + : arena_(arena) +{ +} + +arena* memory::get_arena() NOEXCEPT +{ + return arena_; +} + +retainer::ptr memory::get_retainer() NOEXCEPT +{ + // Takes a shared lock on remap_mutex_ until destruct, blocking remap. + BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT) + return std::make_unique(remap_mutex_); + BC_POP_WARNING() +} + +} // namespace network +} // namespace libbitcoin diff --git a/test/net/memory.cpp b/test/net/memory.cpp new file mode 100644 index 000000000..fccc77f6e --- /dev/null +++ b/test/net/memory.cpp @@ -0,0 +1,28 @@ +/** + * 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 . + */ +#include "../test.hpp" + +BOOST_AUTO_TEST_SUITE(memory_tests) + +BOOST_AUTO_TEST_CASE(memory_test) +{ + BOOST_REQUIRE(true); +} + +BOOST_AUTO_TEST_SUITE_END()